includes/functions.php
changeset 734 904fbf10f112
parent 711 f70d764aab33
child 720 e2762777b170
equal deleted inserted replaced
712:331e009416d5 734:904fbf10f112
   269 function enano_date($string, $timestamp = false)
   269 function enano_date($string, $timestamp = false)
   270 {
   270 {
   271   if ( !is_int($timestamp) && !is_double($timestamp) && strval(intval($timestamp)) !== $timestamp )
   271   if ( !is_int($timestamp) && !is_double($timestamp) && strval(intval($timestamp)) !== $timestamp )
   272     $timestamp = time();
   272     $timestamp = time();
   273   
   273   
   274   /*
       
   275   // List of valid characters for date()
       
   276   $date_chars = 'dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZFcrU';
       
   277   // Split them into an array
       
   278   $date_chars = enano_str_split($date_chars);
       
   279   // Emulate date() formatting by replacing date characters with their
       
   280   // percentage-signed counterparts, but not escaped characters which
       
   281   // shouldn't be parsed.
       
   282   foreach ( $date_chars as $char )
       
   283   {
       
   284     $string = str_replace($char, "%$char", $string);
       
   285     $string = str_replace("\\%$char", $char, $string);
       
   286   }
       
   287   */
       
   288   
       
   289   // perform timestamp offset
   274   // perform timestamp offset
   290   global $timezone;
   275   global $timezone;
   291   // it's gonna be in minutes, so multiply by 60 to offset the unix timestamp
   276   // it's gonna be in minutes, so multiply by 60 to offset the unix timestamp
   292   $timestamp = $timestamp + ( $timezone * 60 );
   277   $timestamp = $timestamp + ( $timezone * 60 );
   293   
   278   
       
   279   // are we in DST?
       
   280   global $dst_params;
       
   281   if ( check_timestamp_dst($timestamp, $dst_params[0], $dst_params[1], $dst_params[2], $dst_params[3]) )
       
   282   {
       
   283     // offset for DST
       
   284     $timestamp += ( $dst_params[4] * 60 );
       
   285   }
       
   286   
   294   // Let PHP do the work for us =)
   287   // Let PHP do the work for us =)
   295   return gmdate($string, $timestamp);
   288   return gmdate($string, $timestamp);
       
   289 }
       
   290 
       
   291 /**
       
   292  * Determine if a timestamp is within DST.
       
   293  * @param int Timestamp
       
   294  * @param int Start month (1-12) of DST
       
   295  * @param int Which Sunday DST starts on (*_SUNDAY constants)
       
   296  * @param int End month of DST
       
   297  * @param int Which Sunday DST ends on
       
   298  * @return bool
       
   299  */
       
   300 
       
   301 function check_timestamp_dst($time, $start_month, $start_sunday, $end_month, $end_sunday)
       
   302 {
       
   303   static $sundays = array(FIRST_SUNDAY, SECOND_SUNDAY, THIRD_SUNDAY, LAST_SUNDAY);
       
   304   
       
   305   // perform timestamp offset
       
   306   global $timezone;
       
   307   // it's gonna be in minutes, so multiply by 60 to offset the unix timestamp
       
   308   $time = $time + ( $timezone * 60 );
       
   309   $year = intval(gmdate('Y', $time));
       
   310   
       
   311   // one-pass validation
       
   312   if ( !in_array($start_sunday, $sundays) || !in_array($end_sunday, $sundays) ||
       
   313        $start_month < 1 || $start_month > 12 || $end_month < 1 || $end_month > 12 )
       
   314     return false;
       
   315     
       
   316   // get timestamp of the selected sunday (start)
       
   317   $dst_start = get_sunday_timestamp($start_month, $start_sunday, $year);
       
   318   $dst_end   = get_sunday_timestamp($end_month, $end_sunday, $year);
       
   319   
       
   320   if ( $dst_start > $dst_end )
       
   321   {
       
   322     // start time is past the end time, this means we're in the southern hemisphere
       
   323     // as a result, if we're within the range, DST is NOT in progress.
       
   324     return !( $time >= $dst_start && $time <= $dst_end );
       
   325   }
       
   326   
       
   327   return $time >= $dst_start && $time <= $dst_end;
       
   328 }
       
   329 
       
   330 /**
       
   331  * Returns a timestamp for the given *_SUNDAY index.
       
   332  * @param int Month
       
   333  * @param int Which Sunday (FIRST, SECOND, THIRD, or LAST)
       
   334  * @param int Year that we're doing our calculations in
       
   335  * @return int
       
   336  */
       
   337 
       
   338 function get_sunday_timestamp($month, $sunday, $year)
       
   339 {
       
   340   $days_in_month = array(
       
   341     1 => 31,
       
   342     2 => $year % 4 == 0 && ( $year % 100 != 0 || ( $year % 100 == 0 && $year % 400 == 0 ) ) ? 29 : 28,
       
   343     3 => 31,
       
   344     4 => 30,
       
   345     5 => 31,
       
   346     6 => 30,
       
   347     7 => 31,
       
   348     8 => 31,
       
   349     9 => 30,
       
   350     10 => 31,
       
   351     11 => 30,
       
   352     12 => 31
       
   353   );
       
   354   
       
   355   $result = mktime(0, 0, 0, $month, 1, $year);
       
   356   
       
   357   // hack. allows a specific day of the month to be set instead of a sunday. not a good place to do this.
       
   358   if ( is_string($sunday) && substr($sunday, -1) === 'd' )
       
   359   {
       
   360     $result += 86400 * ( intval($sunday) - 1);
       
   361     return $result;
       
   362   }
       
   363   
       
   364   $tick = 0;
       
   365   $days_remaining = $days_in_month[$month];
       
   366   while ( true )
       
   367   {
       
   368     if ( date('D', $result) == 'Sun' )
       
   369     {
       
   370       $tick++;
       
   371       if ( ( $tick == 1 && $sunday == FIRST_SUNDAY ) ||
       
   372            ( $tick == 2 && $sunday == SECOND_SUNDAY ) ||
       
   373            ( $tick == 3 && $sunday == THIRD_SUNDAY ) ||
       
   374            ( $sunday == LAST_SUNDAY && $days_remaining < 7 ) )
       
   375         break;
       
   376     }
       
   377     $days_remaining--;
       
   378     $result += 86400;
       
   379   }
       
   380   
       
   381   return $result;
   296 }
   382 }
   297 
   383 
   298 /**
   384 /**
   299  * Tells you the title for the given page ID string
   385  * Tells you the title for the given page ID string
   300  * @param string Page ID string (ex: Special:Administration)
   386  * @param string Page ID string (ex: Special:Administration)
  2849  * @return bool true if valid, false otherwise
  2935  * @return bool true if valid, false otherwise
  2850  */
  2936  */
  2851 
  2937 
  2852 function is_valid_ip($ip)
  2938 function is_valid_ip($ip)
  2853 {
  2939 {
  2854   // These came from phpBB3.
  2940   // This next one came from phpBB3.
  2855   $ipv4 = '(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])';
  2941   $ipv4 = '(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])';
  2856   $ipv6 = '(?:(?:(?:[\dA-F]{1,4}:){6}(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?: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9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{1,4})';
  2857 
  2943 
  2858   if ( preg_match("/^{$ipv4}$/", $ip) || preg_match("/^{$ipv6}$/", $ip) )
  2944   if ( preg_match("/^{$ipv4}$/", $ip) || preg_match("/^{$ipv6}$/", $ip) )
  2859     return true;
  2945     return true;
  2860   else
  2946   else
  2861     return false;
  2947     return false;