includes/functions.php
changeset 391 85f91037cd4f
parent 387 92664d2efab8
child 406 7468a663315f
equal deleted inserted replaced
390:9bcc185dc151 391:85f91037cd4f
   319   // POST check added in 1.1.x because Firefox asks us if we want to "resend the form
   319   // POST check added in 1.1.x because Firefox asks us if we want to "resend the form
   320   // data to the new location", which can be confusing for some users.
   320   // data to the new location", which can be confusing for some users.
   321   if ( $timeout == 0 && empty($_POST) )
   321   if ( $timeout == 0 && empty($_POST) )
   322   {
   322   {
   323     header('Location: ' . $url);
   323     header('Location: ' . $url);
       
   324     header('Content-length: 0');
   324     header('HTTP/1.1 307 Temporary Redirect');
   325     header('HTTP/1.1 307 Temporary Redirect');
       
   326     
       
   327     // with 3xx codes HTTP clients expect a response of 0 bytes, so just die here
       
   328     exit();
   325   }
   329   }
   326   
   330   
   327   if ( !is_object($template) )
   331   if ( !is_object($template) )
   328   {
   332   {
   329     $template = new template_nodb();
   333     $template = new template_nodb();
   330     $template->load_theme('oxygen', 'bleu', false);
   334     $template->load_theme('oxygen', 'bleu', false);
   331     $template->tpl_strings['SITE_NAME'] = 'Enano';
   335     $template->tpl_strings['SITE_NAME'] = 'Enano';
   332     $template->tpl_strings['SITE_DESC'] = 'This site is experiencing a critical error and cannot load.';
   336     $template->tpl_strings['SITE_DESC'] = 'This site is experiencing a critical error and cannot load.';
   333     $template->tpl_strings['COPYRIGHT'] = 'Powered by Enano CMS - &copy; 2007 Dan Fuhry. This program is Free Software; see the <a href="' . scriptPath . '/install.php?mode=license">GPL file</a> included with this package for details.';
   337     $template->tpl_strings['COPYRIGHT'] = 'Powered by Enano CMS - &copy; 2006-2008 Dan Fuhry. This program is Free Software; see the <a href="' . scriptPath . '/install.php?mode=license">GPL file</a> included with this package for details.';
   334     $template->tpl_strings['PAGE_NAME'] = htmlspecialchars($title);
   338     $template->tpl_strings['PAGE_NAME'] = htmlspecialchars($title);
   335   }
   339   }
   336 
   340 
   337   $template->add_header('<meta http-equiv="refresh" content="' . $timeout . '; url=' . str_replace('"', '\\"', $url) . '" />');
   341   $template->add_header('<meta http-equiv="refresh" content="' . $timeout . '; url=' . str_replace('"', '\\"', $url) . '" />');
   338   $template->add_header('<script type="text/javascript">
   342   $template->add_header('<script type="text/javascript">
   433   $idx = $keyflop[$keyname];
   437   $idx = $keyflop[$keyname];
   434   while( $orig != $arr[$keylist[0]] ) {
   438   while( $orig != $arr[$keylist[0]] ) {
   435     // echo 'Keyname: '.$keylist[$idx] . '<br />'; flush(); ob_flush(); // Debugger
   439     // echo 'Keyname: '.$keylist[$idx] . '<br />'; flush(); ob_flush(); // Debugger
   436     if($idx < 0) return $arr;
   440     if($idx < 0) return $arr;
   437     if($keylist[$idx] == '' || $keylist[$idx] < 0 || !$keylist[$idx]) {
   441     if($keylist[$idx] == '' || $keylist[$idx] < 0 || !$keylist[$idx]) {
   438       /* echo 'Infinite loop caught in arrayItemTop(<br /><pre>';
       
   439       print_r($arr);
       
   440       echo '</pre><br />, '.$keyname.');<br /><br />EnanoCMS: Critical error during function call, exiting to prevent excessive server load.';
       
   441       exit; */
       
   442       return $arr;
   442       return $arr;
   443     }
   443     }
   444     $arr = arrayItemUp($arr, $keylist[$idx]);
   444     $arr = arrayItemUp($arr, $keylist[$idx]);
   445     $idx--;
   445     $idx--;
   446   }
   446   }
   611                            ORDER BY is_category DESC, p.name ASC;');
   611                            ORDER BY is_category DESC, p.name ASC;');
   612     if ( !$q )
   612     if ( !$q )
   613     {
   613     {
   614       $db->_die();
   614       $db->_die();
   615     }
   615     }
   616     echo '<h3>Subcategories</h3>';
   616     echo '<h3>' . $lang->get('onpage_cat_heading_subcategories') . '</h3>';
   617     echo '<div class="tblholder">';
   617     echo '<div class="tblholder">';
   618     echo '<table border="0" cellspacing="1" cellpadding="4">';
   618     echo '<table border="0" cellspacing="1" cellpadding="4">';
   619     echo '<tr>';
   619     echo '<tr>';
   620     $ticker = 0;
   620     $ticker = 0;
   621     $counter = 0;
   621     $counter = 0;
   634             echo '<td class="' . $class . '" style="width: 33.3%;"></td>';
   634             echo '<td class="' . $class . '" style="width: 33.3%;"></td>';
   635           }
   635           }
   636         }
   636         }
   637         else
   637         else
   638         {
   638         {
   639           echo '<td class="' . $class . '">No subcategories.</td>';
   639           echo '<td class="' . $class . '">' . $lang->get('onpage_cat_msg_no_subcategories') . '</td>';
   640         }
   640         }
   641         echo '</tr></table></div>' . "\n\n";
   641         echo '</tr></table></div>' . "\n\n";
   642         echo '<h3>Pages</h3>';
   642         echo '<h3>' . $lang->get('onpage_cat_heading_pages') . '</h3>';
   643         echo '<div class="tblholder">';
   643         echo '<div class="tblholder">';
   644         echo '<table border="0" cellspacing="1" cellpadding="4">';
   644         echo '<table border="0" cellspacing="1" cellpadding="4">';
   645         echo '<tr>';
   645         echo '<tr>';
   646         $counter = 0;
   646         $counter = 0;
   647         $ticker = -1;
   647         $ticker = -1;
   682           echo '<td class="' . $class . '" style="width: 33.3%;"></td>';
   682           echo '<td class="' . $class . '" style="width: 33.3%;"></td>';
   683         }
   683         }
   684       }
   684       }
   685       else
   685       else
   686       {
   686       {
   687         echo '<td class="' . $class . '">No subcategories.</td>';
   687         echo '<td class="' . $class . '">' . $lang->get('onpage_cat_msg_no_subcategories') . '</td>';
   688       }
   688       }
   689       echo '</tr></table></div>' . "\n\n";
   689       echo '</tr></table></div>' . "\n\n";
   690       echo '<h3>Pages</h3>';
   690       echo '<h3>' . $lang->get('onpage_cat_heading_pages') . '</h3>';
   691       echo '<div class="tblholder">';
   691       echo '<div class="tblholder">';
   692       echo '<table border="0" cellspacing="1" cellpadding="4">';
   692       echo '<table border="0" cellspacing="1" cellpadding="4">';
   693       echo '<tr>';
   693       echo '<tr>';
   694       $counter = 0;
   694       $counter = 0;
   695       $ticker = 0;
   695       $ticker = 0;
   704         echo '<td class="' . $class . '" style="width: 33.3%;"></td>';
   704         echo '<td class="' . $class . '" style="width: 33.3%;"></td>';
   705       }
   705       }
   706     }
   706     }
   707     else
   707     else
   708     {
   708     {
   709       echo '<td class="' . $class . '">No pages in this category.</td>';
   709       echo '<td class="' . $class . '">' . $lang->get('onpage_cat_msg_no_pages') . '</td>';
   710     }
   710     }
   711     echo '</tr></table></div>' . "\n\n";
   711     echo '</tr></table></div>' . "\n\n";
   712   }
   712   }
   713   
   713   
   714   if ( $paths->namespace != 'Special' && $paths->namespace != 'Admin' )
   714   if ( $paths->namespace != 'Special' && $paths->namespace != 'Admin' )
   768  */
   768  */
   769 
   769 
   770 function show_file_info()
   770 function show_file_info()
   771 {
   771 {
   772   global $db, $session, $paths, $template, $plugins; // Common objects
   772   global $db, $session, $paths, $template, $plugins; // Common objects
   773   if($paths->namespace != 'File') return null; // Prevent unnecessary work
   773   global $lang;
   774   $selfn = $paths->page_id; // substr($paths->page, strlen($paths->nslist['File']), strlen($paths->cpage));
   774   
   775   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));
   775   // Prevent unnecessary work
       
   776   if ( $paths->namespace != 'File' )
       
   777     return null;
       
   778   
       
   779   $selfn = $paths->page_id;
       
   780   if ( substr($paths->cpage['name'], 0, strlen($paths->nslist['File'])) == $paths->nslist['File'])
       
   781   {
       
   782     $selfn = substr($paths->page_id, strlen($paths->nslist['File']), strlen($paths->page_id));
       
   783   }
   776   $q = $db->sql_query('SELECT mimetype,time_id,size FROM '.table_prefix.'files WHERE page_id=\''.$selfn.'\' ORDER BY time_id DESC;');
   784   $q = $db->sql_query('SELECT mimetype,time_id,size FROM '.table_prefix.'files WHERE page_id=\''.$selfn.'\' ORDER BY time_id DESC;');
   777   if(!$q) $db->_die('The file type could not be fetched.');
   785   if ( !$q )
   778   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; }
   786   {
       
   787     $db->_die('The file type could not be fetched.');
       
   788   }
       
   789   
       
   790   if ( $db->numrows() < 1 )
       
   791   {
       
   792     echo '<div class="mdg-comment" style="margin-left: 0;">
       
   793             <h3>' . $lang->get('onpage_filebox_heading') . '</h3>
       
   794             <p>' . $lang->get('onpage_filebox_msg_not_found', array('upload_link' => makeUrlNS('Special', 'UploadFile/'.$paths->page_id))) . '</p>
       
   795           </div>
       
   796           <br />';
       
   797     return;
       
   798   }
   779   $r = $db->fetchrow();
   799   $r = $db->fetchrow();
   780   $mimetype = $r['mimetype'];
   800   $mimetype = $r['mimetype'];
   781   $datestring = enano_date('F d, Y h:i a', (int)$r['time_id']);
   801   $datestring = enano_date('F d, Y h:i a', (int)$r['time_id']);
   782   echo '<div class="mdg-comment" style="margin-left: 0;"><p><h3>Uploaded file</h3></p><p>Type: '.$r['mimetype'].'<br />Size: ';
   802   echo '<div class="mdg-comment" style="margin-left: 0;">
   783   $fs = $r['size'];
   803           <h3>' . $lang->get('onpage_filebox_heading') . '</h3>
   784   echo $fs.' bytes';
   804           <p>' . $lang->get('onpage_filebox_lbl_type') . ' '.$r['mimetype'].'<br />';
   785   $fs = (int)$fs;
   805   
   786   if($fs >= 1048576)
   806   $size = $r['size'] . ' ' . $lang->get('etc_unit_bytes');
   787   {
   807   if ( $r['size'] >= 1048576 )
   788     $fs = round($fs / 1048576, 1);
   808   {
   789     echo ' ('.$fs.' MB)';
   809     $size .= ' (' . ( round($r['size'] / 1048576, 1) ) . ' ' . $lang->get('etc_unit_megabytes_short') . ')';
   790   } elseif($fs >= 1024) {
   810   }
   791     $fs = round($fs / 1024, 1);
   811   else if ( $r['size'] >= 1024 )
   792     echo ' ('.$fs.' KB)';
   812   {
   793   }
   813     $size .= ' (' . ( round($r['size'] / 1024, 1) ) . ' ' . $lang->get('etc_unit_kilobytes_short') . ')';
   794   echo '<br />Uploaded: '.$datestring.'</p>';
   814   }
   795   if(substr($mimetype, 0, 6)!='image/' && ( substr($mimetype, 0, 5) != 'text/' || $mimetype == 'text/html' || $mimetype == 'text/javascript' ))
   815           
   796   {
   816   echo $lang->get('onpage_filebox_lbl_size', array('size' => $size));
   797     echo '<div class="warning-box">This file type may contain viruses or other code that could harm your computer. You should exercise caution if you download it.</div>';
   817   
   798   }
   818   echo '<br />' . $lang->get('onpage_filebox_lbl_uploaded') . ' ' . $datestring . '</p>';
   799   if(substr($mimetype, 0, 6)=='image/')
   819   if ( substr($mimetype, 0, 6) != 'image/' && ( substr($mimetype, 0, 5) != 'text/' || $mimetype == 'text/html' || $mimetype == 'text/javascript' ) )
   800   {
   820   {
   801     echo '<p><a href="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn).'"><img style="border: 0;" alt="'.$paths->page.'" src="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn.htmlspecialchars(urlSeparator).'preview').'" /></a></p>';
   821     echo '<div class="warning-box">
   802   }
   822             ' . $lang->get('onpage_filebox_msg_virus_warning') . '
   803   echo '<p><a href="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn.'/'.$r['time_id'].htmlspecialchars(urlSeparator).'download').'">Download this file</a>';
   823           </div>';
       
   824   }
       
   825   if ( substr($mimetype, 0, 6) == 'image/' )
       
   826   {
       
   827     echo '<p>
       
   828             <a href="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn).'">
       
   829               <img style="border: 0;" alt="'.$paths->page.'" src="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn.htmlspecialchars(urlSeparator).'preview').'" />
       
   830             </a>
       
   831           </p>';
       
   832   }
       
   833   echo '<p>
       
   834           <a href="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn.'/'.$r['time_id'].htmlspecialchars(urlSeparator).'download').'">
       
   835             ' . $lang->get('onpage_filebox_btn_download') . '
       
   836           </a>';
   804   if(!$paths->page_protected && ( $paths->wiki_mode || $session->get_permissions('upload_new_version') ))
   837   if(!$paths->page_protected && ( $paths->wiki_mode || $session->get_permissions('upload_new_version') ))
   805   {
   838   {
   806     echo '  |  <a href="'.makeUrlNS('Special', 'UploadFile'.'/'.$selfn).'">Upload new version</a>';
   839     echo '  |  <a href="'.makeUrlNS('Special', 'UploadFile'.'/'.$selfn).'">
       
   840             ' . $lang->get('onpage_filebox_btn_upload_new') . '
       
   841           </a>';
   807   }
   842   }
   808   echo '</p>';
   843   echo '</p>';
   809   if($db->numrows() > 1)
   844   if ( $db->numrows() > 1 )
   810   {
   845   {
   811     echo '<h3>File history</h3><p>';
   846     echo '<h3>' . $lang->get('onpage_filebox_heading_history') . '</h3><p>';
   812     while($r = $db->fetchrow())
   847     while ( $r = $db->fetchrow() )
   813     {
   848     {
   814       echo '(<a href="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn.'/'.$r['time_id'].htmlspecialchars(urlSeparator).'download').'">this ver</a>) ';
   849       echo '(<a href="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn.'/'.$r['time_id'].htmlspecialchars(urlSeparator).'download').'">' . $lang->get('onpage_filebox_btn_this_version') . '</a>) ';
   815       if($session->get_permissions('history_rollback'))
   850       if ( $session->get_permissions('history_rollback') )
   816         echo ' (<a href="#" onclick="ajaxRollback(\''.$r['time_id'].'\'); return false;">revert</a>) ';
   851         echo ' (<a href="#" onclick="ajaxRollback(\''.$r['time_id'].'\'); return false;">' . $lang->get('onpage_filebox_btn_revert') . '</a>) ';
   817       $mimetype = $r['mimetype'];
   852       $mimetype = $r['mimetype'];
   818       $datestring = enano_date('F d, Y h:i a', (int)$r['time_id']);
   853       $datestring = enano_date('F d, Y h:i a', (int)$r['time_id']);
       
   854       
   819       echo $datestring.': '.$r['mimetype'].', ';
   855       echo $datestring.': '.$r['mimetype'].', ';
       
   856       
   820       $fs = $r['size'];
   857       $fs = $r['size'];
   821       $fs = (int)$fs;
   858       $fs = (int)$fs;
       
   859       
   822       if($fs >= 1048576)
   860       if($fs >= 1048576)
   823       {
   861       {
   824         $fs = round($fs / 1048576, 1);
   862         $fs = round($fs / 1048576, 1);
   825         echo ' '.$fs.' MB';
   863         $size = $fs . ' ' . $lang->get('etc_unit_megabytes_short');
   826       } elseif($fs >= 1024) {
   864       }
       
   865       else
       
   866       if ( $fs >= 1024 )
       
   867       {
   827         $fs = round($fs / 1024, 1);
   868         $fs = round($fs / 1024, 1);
   828         echo ' '.$fs.' KB';
   869         $size = $fs . ' ' . $lang->get('etc_unit_kilobytes_short');
   829       } else {
   870       }
   830         echo ' '.$fs.' bytes';
   871       else
   831       }
   872       {
       
   873         $size = $fs . ' ' . $lang->get('etc_unit_bytes');
       
   874       }
       
   875       
       
   876       echo $size;
       
   877       
   832       echo '<br />';
   878       echo '<br />';
   833     }
   879     }
   834     echo '</p>';
   880     echo '</p>';
   835   }
   881   }
   836   $db->free_result();
   882   $db->free_result();
   874   {
   920   {
   875     eval($cmd);
   921     eval($cmd);
   876   }
   922   }
   877   show_file_info();
   923   show_file_info();
   878   show_category_info();
   924   show_category_info();
   879 }
       
   880 
       
   881 /**
       
   882  * Deprecated, do not use.
       
   883  */
       
   884 
       
   885 function password_prompt($id = false)
       
   886 {
       
   887   global $db, $session, $paths, $template, $plugins; // Common objects
       
   888   if(!$id) $id = $paths->page;
       
   889   if(isset($paths->pages[$id]['password']) && strlen($paths->pages[$id]['password']) == 40 && !isset($_REQUEST['pagepass']))
       
   890   {
       
   891     die_friendly('Password required', '<p>You must supply a password to access this page.</p><form action="'.makeUrl($paths->pages[$id]['urlname']).'" method="post"><p>Password: <input name="pagepass" type="password" /></p><p><input type="submit" value="Submit" /></p>');
       
   892   } elseif(isset($_REQUEST['pagepass'])) {
       
   893     $p = (preg_match('#^([a-f0-9]*){40}$#', $_REQUEST['pagepass'])) ? $_REQUEST['pagepass'] : sha1($_REQUEST['pagepass']);
       
   894     if($p != $paths->pages[$id]['password']) die_friendly('Password required', '<p style="color: red;">The password you entered is incorrect.</p><form action="'.makeUrl($paths->page).'" method="post"><p>Password: <input name="pagepass" type="password" /></p><p><input type="submit" value="Submit" /></p>');
       
   895   }
       
   896 }
       
   897 
       
   898 /**
       
   899  * Some sort of primitive hex converter from back in the day. Deprecated, do not use.
       
   900  * @param string Text to encode
       
   901  * @return string
       
   902  */
       
   903 
       
   904 function str_hex($string){
       
   905     $hex='';
       
   906     for ($i=0; $i < strlen($string); $i++){
       
   907         $hex .= ' '.dechex(ord($string[$i]));
       
   908     }
       
   909     return substr($hex, 1, strlen($hex));
       
   910 }
   925 }
   911 
   926 
   912 /**
   927 /**
   913  * Essentially an return code reader for a socket. Don't use this unless you're writing mail code and smtp_send_email doesn't cut it. Ported from phpBB's smtp.php.
   928  * Essentially an return code reader for a socket. Don't use this unless you're writing mail code and smtp_send_email doesn't cut it. Ported from phpBB's smtp.php.
   914  * @param socket A socket resource
   929  * @param socket A socket resource
  1182   if ( isset($names[$version]) )
  1197   if ( isset($names[$version]) )
  1183   {
  1198   {
  1184     return $names[$version];
  1199     return $names[$version];
  1185   }
  1200   }
  1186   return 'Anonymous build';
  1201   return 'Anonymous build';
  1187 }
       
  1188 
       
  1189 /**
       
  1190  * What kinda sh** was I thinking when I wrote this. Deprecated.
       
  1191  */
       
  1192 
       
  1193 function _dualurlenc($t) {
       
  1194   return rawurlencode(rawurlencode($t));
       
  1195 }
  1202 }
  1196 
  1203 
  1197 /**
  1204 /**
  1198  * Badly named function to send back eval'able Javascript code with an error message. Deprecated, use JSON instead.
  1205  * Badly named function to send back eval'able Javascript code with an error message. Deprecated, use JSON instead.
  1199  * @param string Message to send
  1206  * @param string Message to send