includes/functions.php
changeset 326 ab66d6d1f1f4
parent 313 854eecfada20
parent 325 e17cc42d77cf
child 328 dc838fd61a06
equal deleted inserted replaced
314:474f8be55943 326:ab66d6d1f1f4
     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.0.2 (Coblynau)
     5  * Version 1.0.3 (Dyrad)
     6  * Copyright (C) 2006-2007 Dan Fuhry
     6  * Copyright (C) 2006-2007 Dan Fuhry
     7  *
     7  *
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     8  * 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.
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    10  *
    10  *
   258 function get_page_title_ns($page_id, $namespace)
   258 function get_page_title_ns($page_id, $namespace)
   259 {
   259 {
   260   global $db, $session, $paths, $template, $plugins; // Common objects
   260   global $db, $session, $paths, $template, $plugins; // Common objects
   261 
   261 
   262   $page_id_key = $paths->nslist[ $namespace ] . $page_id;
   262   $page_id_key = $paths->nslist[ $namespace ] . $page_id;
   263   $page_data = $paths->pages[$page_id_key];
   263   if ( isset($paths->pages[$page_id_key]) )
       
   264   {
       
   265     $page_data = $paths->pages[$page_id_key];
       
   266   }
       
   267   else
       
   268   {
       
   269     $page_data = array();
       
   270   }
   264   $title = ( isset($page_data['name']) ) ? $page_data['name'] : $paths->nslist[$namespace] . str_replace('_', ' ', dirtify_page_id( $page_id ) );
   271   $title = ( isset($page_data['name']) ) ? $page_data['name'] : $paths->nslist[$namespace] . str_replace('_', ' ', dirtify_page_id( $page_id ) );
   265   return $title;
   272   return $title;
   266 }
   273 }
   267 
   274 
   268 /**
   275 /**
   565   {
   572   {
   566     // Show member pages and subcategories
   573     // Show member pages and subcategories
   567     $q = $db->sql_query('SELECT p.urlname, p.namespace, p.name, p.namespace=\'Category\' AS is_category FROM '.table_prefix.'categories AS c
   574     $q = $db->sql_query('SELECT p.urlname, p.namespace, p.name, p.namespace=\'Category\' AS is_category FROM '.table_prefix.'categories AS c
   568                            LEFT JOIN '.table_prefix.'pages AS p
   575                            LEFT JOIN '.table_prefix.'pages AS p
   569                              ON ( p.urlname = c.page_id AND p.namespace = c.namespace )
   576                              ON ( p.urlname = c.page_id AND p.namespace = c.namespace )
   570                            WHERE c.category_id=\'' . $db->escape($paths->cpage['urlname_nons']) . '\'
   577                            WHERE c.category_id=\'' . $db->escape($paths->page_id) . '\'
   571                            ORDER BY is_category DESC, p.name ASC;');
   578                            ORDER BY is_category DESC, p.name ASC;');
   572     if ( !$q )
   579     if ( !$q )
   573     {
   580     {
   574       $db->_die();
   581       $db->_die();
   575     }
   582     }
   677     echo '<div style="float: right;">';
   684     echo '<div style="float: right;">';
   678     echo '(<a href="#" onclick="ajaxCatToTag(); return false;">' . $lang->get('tags_catbox_link') . '</a>)';
   685     echo '(<a href="#" onclick="ajaxCatToTag(); return false;">' . $lang->get('tags_catbox_link') . '</a>)';
   679     echo '</div>';
   686     echo '</div>';
   680     echo '<div id="mdgCatBox">' . $lang->get('catedit_catbox_lbl_categories') . ' ';
   687     echo '<div id="mdgCatBox">' . $lang->get('catedit_catbox_lbl_categories') . ' ';
   681     
   688     
   682     $where = '( c.page_id=\'' . $db->escape($paths->cpage['urlname_nons']) . '\' AND c.namespace=\'' . $db->escape($paths->namespace) . '\' )';
   689     $where = '( c.page_id=\'' . $db->escape($paths->page_id) . '\' AND c.namespace=\'' . $db->escape($paths->namespace) . '\' )';
   683     $prefix = table_prefix;
   690     $prefix = table_prefix;
   684     $sql = <<<EOF
   691     $sql = <<<EOF
   685 SELECT c.category_id FROM {$prefix}categories AS c
   692 SELECT c.category_id FROM {$prefix}categories AS c
   686   LEFT JOIN {$prefix}pages AS p
   693   LEFT JOIN {$prefix}pages AS p
   687     ON ( ( p.urlname = c.page_id AND p.namespace = c.namespace ) OR ( p.urlname IS NULL AND p.namespace IS NULL ) )
   694     ON ( ( p.urlname = c.page_id AND p.namespace = c.namespace ) OR ( p.urlname IS NULL AND p.namespace IS NULL ) )
   729 
   736 
   730 function show_file_info()
   737 function show_file_info()
   731 {
   738 {
   732   global $db, $session, $paths, $template, $plugins; // Common objects
   739   global $db, $session, $paths, $template, $plugins; // Common objects
   733   if($paths->namespace != 'File') return null; // Prevent unnecessary work
   740   if($paths->namespace != 'File') return null; // Prevent unnecessary work
   734   $selfn = $paths->cpage['urlname_nons']; // substr($paths->page, strlen($paths->nslist['File']), strlen($paths->cpage));
   741   $selfn = $paths->page_id; // substr($paths->page, strlen($paths->nslist['File']), strlen($paths->cpage));
   735   if(substr($paths->cpage['name'], 0, strlen($paths->nslist['File']))==$paths->nslist['File']) $selfn = substr($paths->cpage['urlname_nons'], strlen($paths->nslist['File']), strlen($paths->cpage['urlname_nons']));
   742   if(substr($paths->cpage['name'], 0, strlen($paths->nslist['File']))==$paths->nslist['File']) $selfn = substr($paths->page_id, strlen($paths->nslist['File']), strlen($paths->page_id));
   736   $q = $db->sql_query('SELECT mimetype,time_id,size FROM '.table_prefix.'files WHERE page_id=\''.$selfn.'\' ORDER BY time_id DESC;');
   743   $q = $db->sql_query('SELECT mimetype,time_id,size FROM '.table_prefix.'files WHERE page_id=\''.$selfn.'\' ORDER BY time_id DESC;');
   737   if(!$q) $db->_die('The file type could not be fetched.');
   744   if(!$q) $db->_die('The file type could not be fetched.');
   738   if($db->numrows() < 1) { echo '<div class="mdg-comment" style="margin-left: 0;"><h3>Uploaded file</h3><p>There are no files uploaded with this name yet. <a href="'.makeUrlNS('Special', 'UploadFile/'.$paths->cpage['urlname_nons']).'">Upload a file...</a></p></div><br />'; return; }
   745   if($db->numrows() < 1) { echo '<div class="mdg-comment" style="margin-left: 0;"><h3>Uploaded file</h3><p>There are no files uploaded with this name yet. <a href="'.makeUrlNS('Special', 'UploadFile/'.$paths->page_id).'">Upload a file...</a></p></div><br />'; return; }
   739   $r = $db->fetchrow();
   746   $r = $db->fetchrow();
   740   $mimetype = $r['mimetype'];
   747   $mimetype = $r['mimetype'];
   741   $datestring = date('F d, Y h:i a', (int)$r['time_id']);
   748   $datestring = date('F d, Y h:i a', (int)$r['time_id']);
   742   echo '<div class="mdg-comment" style="margin-left: 0;"><p><h3>Uploaded file</h3></p><p>Type: '.$r['mimetype'].'<br />Size: ';
   749   echo '<div class="mdg-comment" style="margin-left: 0;"><p><h3>Uploaded file</h3></p><p>Type: '.$r['mimetype'].'<br />Size: ';
   743   $fs = $r['size'];
   750   $fs = $r['size'];
  1132       '1.0RC3' => 'Druid',
  1139       '1.0RC3' => 'Druid',
  1133       '1.0'    => 'Banshee',
  1140       '1.0'    => 'Banshee',
  1134       '1.0.1'  => 'Loch Ness',
  1141       '1.0.1'  => 'Loch Ness',
  1135       '1.0.1.1'=> 'Loch Ness internal bugfix build',
  1142       '1.0.1.1'=> 'Loch Ness internal bugfix build',
  1136       '1.0.2b1'=> 'Coblynau unstable',
  1143       '1.0.2b1'=> 'Coblynau unstable',
  1137       '1.0.2'  => 'Coblynau'
  1144       '1.0.2'  => 'Coblynau',
       
  1145       '1.0.3'  => 'Dyrad'
  1138     );
  1146     );
  1139   $version = enano_version();
  1147   $version = enano_version();
  1140   if ( isset($names[$version]) )
  1148   if ( isset($names[$version]) )
  1141   {
  1149   {
  1142     return $names[$version];
  1150     return $names[$version];
  2226  */
  2234  */
  2227 
  2235 
  2228 function paginate_array($q, $num_results, $result_url, $start = 0, $perpage = 10, $header = '', $footer = '')
  2236 function paginate_array($q, $num_results, $result_url, $start = 0, $perpage = 10, $header = '', $footer = '')
  2229 {
  2237 {
  2230   global $db, $session, $paths, $template, $plugins; // Common objects
  2238   global $db, $session, $paths, $template, $plugins; // Common objects
  2231   $parser = $template->makeParserText($tpl_text);
       
  2232   $num_pages = ceil ( $num_results / $perpage );
  2239   $num_pages = ceil ( $num_results / $perpage );
  2233   $out = '';
  2240   $out = '';
  2234   $i = 0;
  2241   $i = 0;
  2235   $this_page = ceil ( $start / $perpage );
  2242   $this_page = ceil ( $start / $perpage );
  2236 
  2243 
  2413  * @return string Cleaned text
  2420  * @return string Cleaned text
  2414  */
  2421  */
  2415 
  2422 
  2416 function sanitize_page_id($page_id)
  2423 function sanitize_page_id($page_id)
  2417 {
  2424 {
  2418 
  2425   global $db, $session, $paths, $template, $plugins; // Common objects
       
  2426   
       
  2427   if ( isset($paths->nslist['User']) )
       
  2428   {
       
  2429     if ( preg_match('/^' . preg_quote($paths->nslist['User']) . '/', $page_id) )
       
  2430     {
       
  2431       $ip = preg_replace('/^' . preg_quote($paths->nslist['User']) . '/', '', $page_id);
       
  2432       if ( is_valid_ip($ip) )
       
  2433       {
       
  2434         return $page_id;
       
  2435       }
       
  2436     }
       
  2437   }
       
  2438   
  2419   // Remove character escapes
  2439   // Remove character escapes
  2420   $page_id = dirtify_page_id($page_id);
  2440   $page_id = dirtify_page_id($page_id);
  2421 
  2441 
  2422   $pid_clean = preg_replace('/[\w\.\/:;\(\)@\[\]_-]/', 'X', $page_id);
  2442   $pid_clean = preg_replace('/[\w\.\/:;\(\)@\[\]_-]/', 'X', $page_id);
  2423   $pid_dirty = enano_str_split($pid_clean, 1);
  2443   $pid_dirty = enano_str_split($pid_clean, 1);
  2444     if ( $pid_dirty[$id] == 'X' )
  2464     if ( $pid_dirty[$id] == 'X' )
  2445       $page_id_cleaned .= $char;
  2465       $page_id_cleaned .= $char;
  2446     else
  2466     else
  2447       $page_id_cleaned .= $pid_dirty[$id];
  2467       $page_id_cleaned .= $pid_dirty[$id];
  2448   }
  2468   }
  2449 
  2469   
  2450   // global $mime_types;
  2470   // global $mime_types;
  2451 
  2471 
  2452   // $exts = array_keys($mime_types);
  2472   // $exts = array_keys($mime_types);
  2453   // $exts = '(' . implode('|', $exts) . ')';
  2473   // $exts = '(' . implode('|', $exts) . ')';
  2454 
  2474 
  2468   global $db, $session, $paths, $template, $plugins; // Common objects
  2488   global $db, $session, $paths, $template, $plugins; // Common objects
  2469   // First, replace spaces with underscores
  2489   // First, replace spaces with underscores
  2470   $page_id = str_replace(' ', '_', $page_id);
  2490   $page_id = str_replace(' ', '_', $page_id);
  2471 
  2491 
  2472   // Exception for userpages for IP addresses
  2492   // Exception for userpages for IP addresses
  2473   if ( preg_match('/^' . preg_quote($paths->nslist['User']) . '/', $page_id) )
  2493   if ( is_valid_ip($page_id) )
  2474   {
  2494   {
  2475     $ip = preg_replace('/^' . preg_quote($paths->nslist['User']) . '/', '', $page_id);
  2495     return $page_id;
  2476     if ( is_valid_ip($ip) )
       
  2477       return $page_id;
       
  2478   }
  2496   }
  2479 
  2497 
  2480   preg_match_all('/\.[A-Fa-f0-9][A-Fa-f0-9]/', $page_id, $matches);
  2498   preg_match_all('/\.[A-Fa-f0-9][A-Fa-f0-9]/', $page_id, $matches);
  2481 
  2499 
  2482   foreach ( $matches[0] as $id => $char )
  2500   foreach ( $matches[0] as $id => $char )
  2485     $char = strtolower($char);
  2503     $char = strtolower($char);
  2486     $char = intval(hexdec($char));
  2504     $char = intval(hexdec($char));
  2487     $char = chr($char);
  2505     $char = chr($char);
  2488     $page_id = str_replace($matches[0][$id], $char, $page_id);
  2506     $page_id = str_replace($matches[0][$id], $char, $page_id);
  2489   }
  2507   }
  2490 
  2508   
  2491   return $page_id;
  2509   return $page_id;
  2492 }
  2510 }
  2493 
  2511 
  2494 /**
  2512 /**
  2495  * Inserts commas into a number to make it more human-readable. Floating point-safe and doesn't flirt with the number like number_format() does.
  2513  * Inserts commas into a number to make it more human-readable. Floating point-safe and doesn't flirt with the number like number_format() does.
  2670  */
  2688  */
  2671 
  2689 
  2672 function sanitize_tag($tag)
  2690 function sanitize_tag($tag)
  2673 {
  2691 {
  2674   $tag = strtolower($tag);
  2692   $tag = strtolower($tag);
  2675   $tag = preg_replace('/[^\w _@\$%\^&-]+/', '', $tag);
  2693   $tag = preg_replace('/[^\w @\$%\^&-]+/', '', $tag);
       
  2694   $tag = str_replace('_', ' ', $tag);
  2676   $tag = trim($tag);
  2695   $tag = trim($tag);
  2677   return $tag;
  2696   return $tag;
  2678 }
  2697 }
  2679 
  2698 
  2680 /**
  2699 /**