includes/functions.php
changeset 1103 90225c988124
parent 1099 73abd46f5148
child 1110 1a3f374310ca
equal deleted inserted replaced
1102:faef5e62e1e0 1103:90225c988124
     1 <?php
     1 <?php
     2 
     2 
     3 /*
     3 /*
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     5  * Version 1.1.6 (Caoineag beta 1)
     5  * Copyright (C) 2006-2009 Dan Fuhry
     6  * Copyright (C) 2006-2008 Dan Fuhry
       
     7  *
     6  *
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     7  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
     8  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    10  *
     9  *
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
    10  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
   245   }
   244   }
   246   return $logged_in && getConfig('main_page_alt_enable', '0') == '1' ? getConfig('main_page_alt', getConfig('main_page', 'Main_Page')) : getConfig('main_page', 'Main_Page');
   245   return $logged_in && getConfig('main_page_alt_enable', '0') == '1' ? getConfig('main_page_alt', getConfig('main_page', 'Main_Page')) : getConfig('main_page', 'Main_Page');
   247 }
   246 }
   248 
   247 
   249 /**
   248 /**
       
   249  * Get the requested page title, taking into account all the different possible URL parsing schemes.
       
   250  * @param bool If true (default), runs the result through sanitize_page_id().
       
   251  * @param bool If true (default is false), and the return is a Special or Admin page, trims off anything beyond and including the first slash.
       
   252  * @return string
       
   253  */
       
   254 
       
   255 function get_title($sanitize = true, $chop_special = false)
       
   256 {
       
   257   $title = '';
       
   258   if ( isset($_GET['title']) )
       
   259   {
       
   260     $title = $_GET['title'];
       
   261   }
       
   262   else if ( isset($_SERVER['PATH_INFO']) )
       
   263   {
       
   264     // fix for apache + CGI (occurred on a GoDaddy server, thanks mm3)
       
   265     if ( @substr(@$_SERVER['GATEWAY_INTERFACE'], 0, 3) === 'CGI' && $_SERVER['PATH_INFO'] == scriptPath . '/index.php' )
       
   266     {
       
   267       // do nothing; ignore PATH_INFO
       
   268     }
       
   269     else
       
   270     {
       
   271       $title = substr($_SERVER['PATH_INFO'], ( strpos($_SERVER['PATH_INFO'], '/') ) + 1 );
       
   272     }
       
   273   }
       
   274   else
       
   275   {
       
   276     // This method really isn't supported because apache has a habit of passing dots as underscores, thus corrupting the request
       
   277     // If you really want to try it, the URI format is yoursite.com/?/Page_title
       
   278     if ( !empty($_SERVER['QUERY_STRING']) && substr($_SERVER['QUERY_STRING'], 0, 1) == '/' )
       
   279     {
       
   280       $pos = ( ($_ = strpos($_SERVER['QUERY_STRING'], '&')) !== false ) ? $_ - 1: 0x7FFFFFFF;
       
   281       $title = substr($_SERVER['QUERY_STRING'], 1, $pos);
       
   282     }
       
   283   }
       
   284   
       
   285   if ( $chop_special )
       
   286   {
       
   287     list(, $ns) = RenderMan::strToPageID($title);
       
   288     if ( $ns == 'Special' || $ns == 'Admin' )
       
   289     {
       
   290       list($title) = explode('/', $title);
       
   291     }
       
   292   }
       
   293   
       
   294   return ( $sanitize ) ? sanitize_page_id($title) : $title;
       
   295 }
       
   296  
       
   297 
       
   298 /**
   250  * Enano replacement for date(). Accounts for individual users' timezone preferences.
   299  * Enano replacement for date(). Accounts for individual users' timezone preferences.
   251  * @param string Date-formatted string
   300  * @param string Date-formatted string
   252  * @param int Optional - UNIX timestamp value to use. If omitted, the current time is used.
   301  * @param int Optional - UNIX timestamp value to use. If omitted, the current time is used.
   253  * @return string Formatted string
   302  * @return string Formatted string
   254  */
   303  */
   256 function enano_date($string, $timestamp = false)
   305 function enano_date($string, $timestamp = false)
   257 {
   306 {
   258   if ( !is_int($timestamp) && !is_double($timestamp) && strval(intval($timestamp)) !== $timestamp )
   307   if ( !is_int($timestamp) && !is_double($timestamp) && strval(intval($timestamp)) !== $timestamp )
   259     $timestamp = time();
   308     $timestamp = time();
   260   
   309   
       
   310   if ( is_int($string) )
       
   311   {
       
   312     global $session, $lang;
       
   313     $date_fmt = is_object($session) ? $session->date_format : DATE_4;
       
   314     $time_fmt = is_object($session) ? $session->time_format : TIME_24_NS;
       
   315     
       
   316     // within a week? use a relative date
       
   317     if ( $timestamp + ( 86400 * 7 ) >= time() && $string & ED_DATE && is_object($lang) && is_object($session) && !($string & ED_DATE_FULL) )
       
   318     {
       
   319       $relative_date = get_relative_date($timestamp);
       
   320       if ( $string === ED_DATE )
       
   321         // why do more work if we're done?
       
   322         return $relative_date;
       
   323     }
       
   324     
       
   325     $flags = $string;
       
   326     $string = array();
       
   327     if ( $flags & ED_DATE && !isset($relative_date) )
       
   328       $string[] = $date_fmt;
       
   329     if ( $flags & ED_TIME )
       
   330       $string[] = $time_fmt;
       
   331     
       
   332     $string = implode(' ', $string);
       
   333   }
       
   334   
   261   // perform timestamp offset
   335   // perform timestamp offset
   262   global $timezone;
   336   global $timezone;
   263   // it's gonna be in minutes, so multiply by 60 to offset the unix timestamp
   337   // it's gonna be in minutes, so multiply by 60 to offset the unix timestamp
   264   $timestamp = $timestamp + ( $timezone * 60 );
   338   $timestamp = $timestamp + ( $timezone * 60 );
   265   
   339   
   270     // offset for DST
   344     // offset for DST
   271     $timestamp += ( $dst_params[4] * 60 );
   345     $timestamp += ( $dst_params[4] * 60 );
   272   }
   346   }
   273   
   347   
   274   // Let PHP do the work for us =)
   348   // Let PHP do the work for us =)
   275   return gmdate($string, $timestamp);
   349   $result = gmdate($string, $timestamp);
       
   350   if ( isset($relative_date) )
       
   351   {
       
   352     $result = "$relative_date, $result";
       
   353   }
       
   354   return $result;
       
   355 }
       
   356 
       
   357 /**
       
   358  * Get a relative date ("Today"/"Yesterday"/"N days ago")
       
   359  * @param int Timestamp
       
   360  * @return string
       
   361  */
       
   362 
       
   363 function get_relative_date($time)
       
   364 {
       
   365   global $lang, $session;
       
   366   // Our formatting string to pass to enano_date()
       
   367   // This should not include minute/second info, only today's date in whatever format suits your fancy
       
   368   $formatstring = $session->date_format;
       
   369   // Today's date
       
   370   $today = enano_date($formatstring);
       
   371   // Yesterday's date
       
   372   $yesterday = enano_date($formatstring, (time() - (24*60*60)));
       
   373   // Date on the input
       
   374   $then = enano_date($formatstring, $time);
       
   375   // "X days ago" logic
       
   376   for ( $i = 2; $i <= 6; $i++ )
       
   377   {
       
   378     // hours_in_day * minutes_in_hour * seconds_in_minute * num_days
       
   379     $offset = 24 * 60 * 60 * $i;
       
   380     $days_ago = enano_date($formatstring, (time() - $offset));
       
   381     // so does the input timestamp match the date from $i days ago?
       
   382     if ( $then == $days_ago )
       
   383     {
       
   384       // yes, return $i
       
   385       return $lang->get('userfuncs_ml_date_daysago', array('days_ago' => $i));
       
   386     }
       
   387   }
       
   388   // either yesterday, today, or before 6 days ago
       
   389   switch($then)
       
   390   {
       
   391     case $today:
       
   392       return $lang->get('userfuncs_ml_date_today');
       
   393     case $yesterday:
       
   394       return $lang->get('userfuncs_ml_date_yesterday');
       
   395     default:
       
   396       return $then;
       
   397   }
       
   398   //     .--.
       
   399   //    |o_o |
       
   400   //    |!_/ |
       
   401   //   //   \ \
       
   402   //  (|     | )
       
   403   // /'\_   _/`\
       
   404   // \___)=(___/
       
   405   return 'Linux rocks!';
   276 }
   406 }
   277 
   407 
   278 /**
   408 /**
   279  * Determine if a timestamp is within DST.
   409  * Determine if a timestamp is within DST.
   280  * @param int Timestamp
   410  * @param int Timestamp
   483       // overridden token matches, continue exec
   613       // overridden token matches, continue exec
   484       return true;
   614       return true;
   485     }
   615     }
   486   }
   616   }
   487   
   617   
   488   ob_end_clean();
   618   @ob_end_clean();
   489   
   619   
   490   $output->set_title($lang->get('user_csrf_confirm_title'));
   620   $output->set_title($lang->get('user_csrf_confirm_title'));
   491   $output->header();
   621   $output->header();
   492   
   622   
   493   // initial info
   623   // initial info
   766 function die_semicritical($t, $p, $no_wrapper = false)
   896 function die_semicritical($t, $p, $no_wrapper = false)
   767 {
   897 {
   768   global $db, $session, $paths, $template, $plugins; // Common objects
   898   global $db, $session, $paths, $template, $plugins; // Common objects
   769   $db->close();
   899   $db->close();
   770   
   900   
   771   if ( ob_get_status() )
   901   if ( @ob_get_status() )
   772     ob_end_clean();
   902     ob_end_clean();
   773 
   903 
   774   // If the config hasn't been fetched yet, call grinding_halt.
   904   // If the config hasn't been fetched yet, call grinding_halt.
   775   if ( !defined('ENANO_CONFIG_FETCHED') )
   905   if ( !defined('ENANO_CONFIG_FETCHED') )
   776   {
   906   {
   807 
   937 
   808 function die_friendly($t, $p)
   938 function die_friendly($t, $p)
   809 {
   939 {
   810   global $db, $session, $paths, $template, $plugins; // Common objects
   940   global $db, $session, $paths, $template, $plugins; // Common objects
   811 
   941 
   812   if ( ob_get_status() )
   942   if ( @ob_get_status() )
   813     ob_end_clean();
   943     ob_end_clean();
   814 
   944 
   815   $paths->cpage['name'] = $t;
   945   $paths->cpage['name'] = $t;
   816   $template->tpl_strings['PAGE_NAME'] = $t;
   946   $template->tpl_strings['PAGE_NAME'] = $t;
   817   $template->header();
   947   $template->header();
   836     require( ENANO_ROOT . '/config.php' );
   966     require( ENANO_ROOT . '/config.php' );
   837 
   967 
   838   if ( is_object($db) )
   968   if ( is_object($db) )
   839     $db->close();
   969     $db->close();
   840   
   970   
   841   if ( ob_get_status() )
   971   if ( @ob_get_status() )
   842     ob_end_clean();
   972     ob_end_clean();
   843   
   973   
   844   if ( defined('ENANO_CLI') )
   974   if ( defined('ENANO_CLI') )
   845   {
   975   {
   846     // set console color
   976     // set console color
  1231       '1.1.1'  => 'Caoineag alpha 1',
  1361       '1.1.1'  => 'Caoineag alpha 1',
  1232       '1.1.2'  => 'Caoineag alpha 2',
  1362       '1.1.2'  => 'Caoineag alpha 2',
  1233       '1.1.3'  => 'Caoineag alpha 3',
  1363       '1.1.3'  => 'Caoineag alpha 3',
  1234       '1.1.4'  => 'Caoineag alpha 4',
  1364       '1.1.4'  => 'Caoineag alpha 4',
  1235       '1.1.5'  => 'Caoineag alpha 5',
  1365       '1.1.5'  => 'Caoineag alpha 5',
  1236       '1.1.6'  => 'Caoineag beta 1'
  1366       '1.1.6'  => 'Caoineag beta 1',
       
  1367       '1.1.7'  => 'Caoineag beta 2'
  1237     );
  1368     );
  1238   $version = enano_version();
  1369   $version = enano_version();
  1239   if ( isset($names[$version]) )
  1370   if ( isset($names[$version]) )
  1240   {
  1371   {
  1241     return $names[$version];
  1372     return $names[$version];
  3343   // expand address range.
  3474   // expand address range.
  3344   // this takes short ranges like:
  3475   // this takes short ranges like:
  3345   //   2001:470-471:054-b02b::5-bb
  3476   //   2001:470-471:054-b02b::5-bb
  3346   // up to:
  3477   // up to:
  3347   //   2001:0470-0471:0054-b02b:0000:0000:0000:0005-00bb
  3478   //   2001:0470-0471:0054-b02b:0000:0000:0000:0005-00bb
       
  3479   $range = preg_replace('/^:/', '0000:', $range);
  3348   $range = explode(':', $range);
  3480   $range = explode(':', $range);
  3349   $expanded = '';
  3481   $expanded = '';
  3350   $size = count($range);
  3482   $size = count($range);
  3351   foreach ( $range as $byteset )
  3483   foreach ( $range as $byteset )
  3352   {
  3484   {
  3564   else if ( $low_type == 'alph' && $high_type == 'num' )
  3696   else if ( $low_type == 'alph' && $high_type == 'num' )
  3565   {
  3697   {
  3566     // ???? this should never happen
  3698     // ???? this should never happen
  3567     return __hexdigitrange($high, $low); 
  3699     return __hexdigitrange($high, $low); 
  3568   }
  3700   }
       
  3701 }
       
  3702 
       
  3703 /**
       
  3704  * Expand an IPv6 address to full form
       
  3705  * @param string ::1, 2001:470:e054::2
       
  3706  * @return string 0000:0000:0000:0000:0000:0000:0000:0001, 2001:0470:e054:0000:0000:0000:0000:0002
       
  3707  */
       
  3708 
       
  3709 function expand_ipv6_address($addr)
       
  3710 {
       
  3711   $expanded = array();
       
  3712   $addr = explode(':', $addr);
       
  3713   foreach ( $addr as $i => $bytepair )
       
  3714   {
       
  3715     if ( empty($bytepair) )
       
  3716     {
       
  3717       // ::
       
  3718       while ( count($expanded) < (8 - count($addr) + $i + 1) )
       
  3719       {
       
  3720         $expanded[] = '0000';
       
  3721       }
       
  3722     }
       
  3723     else
       
  3724     {
       
  3725       while ( strlen($bytepair) < 4 )
       
  3726         $bytepair = "0$bytepair";
       
  3727       $expanded[] = $bytepair;
       
  3728     }
       
  3729   }
       
  3730   return implode(':', $expanded);
  3569 }
  3731 }
  3570 
  3732 
  3571 /**
  3733 /**
  3572  * Validates an e-mail address. Uses a compacted version of the regular expression generated by the scripts at <http://examples.oreilly.com/regex/>.
  3734  * Validates an e-mail address. Uses a compacted version of the regular expression generated by the scripts at <http://examples.oreilly.com/regex/>.
  3573  * @param string E-mail address
  3735  * @param string E-mail address