#CMS - CMS Made Simple #(c)2004 by Ted Kulp (wishy@users.sf.net) #This project's homepage is: http://cmsmadesimple.sf.net # #This program is free software; you can redistribute it and/or modify #it under the terms of the GNU General Public License as published by #the Free Software Foundation; either version 2 of the License, or #(at your option) any later version. # #This program is distributed in the hope that it will be useful, #but WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #GNU General Public License for more details. #You should have received a copy of the GNU General Public License #along with this program; if not, write to the Free Software #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # #$Id: misc.functions.php 4822 2008-07-08 15:27:05Z calguy1000 $ /** * Misc functions * * @package CMS */ /** * Redirects to relative URL on the current site * * @author http://www.edoceo.com/ * @since 0.1 */ function redirect($to, $noappend=false) { $_SERVER['PHP_SELF'] = null; global $gCms; if (isset($gCms)) $config =& $gCms->GetConfig(); else $config = array(); $schema = $_SERVER['SERVER_PORT'] == '443' ? 'https' : 'http'; $host = strlen($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME']; $components = parse_url($to); if(count($components) > 0) { $to = (isset($components['scheme']) && startswith($components['scheme'], 'http') ? $components['scheme'] : $schema) . '://'; $to .= isset($components['host']) ? $components['host'] : $host; $to .= isset($components['port']) ? ':' . $components['port'] : ''; if(isset($components['path'])) { if(in_array(substr($components['path'],0,1),array('\\','/')))//Path is absolute, just append. { $to .= $components['path']; } //Path is relative, append current directory first. else if (isset($_SERVER['PHP_SELF']) && !is_null($_SERVER['PHP_SELF'])) //Apache { $to .= (strlen(dirname($_SERVER['PHP_SELF'])) > 1 ? dirname($_SERVER['PHP_SELF']).'/' : '/') . $components['path']; } else if (isset($_SERVER['REQUEST_URI']) && !is_null($_SERVER['REQUEST_URI'])) //Lighttpd { if (endswith($_SERVER['REQUEST_URI'], '/')) $to .= (strlen($_SERVER['REQUEST_URI']) > 1 ? $_SERVER['REQUEST_URI'] : '/') . $components['path']; else $to .= (strlen(dirname($_SERVER['REQUEST_URI'])) > 1 ? dirname($_SERVER['REQUEST_URI']).'/' : '/') . $components['path']; } } $to .= isset($components['query']) ? '?' . $components['query'] : ''; $to .= isset($components['fragment']) ? '#' . $components['fragment'] : ''; } else { $to = $schema."://".$host."/".$to; } //If session trans-id is being used, and they is on yo website, add it. /* if (ini_get("session.use_trans_sid") != "0" && $noappend == false && strpos($to,$host) !== false) { if(strpos($to,'?') !== false)//If there are no arguments start a querystring { //$to = $to."?".session_name()."=".session_id(); } else//There are arguments, print an arg seperator { //$to = $to.ini_get('arg_separator.input').session_name()."=".session_id(); } } */ if (headers_sent() && !(isset($config) && $config['debug'] == true)) { // use javascript instead echo ' '; exit; } else { if (isset($config['debug']) && $config['debug'] == true) { echo "Debug is on. Redirecting disabled... Please click this link to continue.
"; echo "".$to."
"; echo '
'; global $sql_queries; if (FALSE == empty($sql_queries)) { echo "
".$sql_queries."
\n"; } foreach ($gCms->errors as $error) { echo $error; } echo '
'; exit(); } else { header("Location: $to"); exit(); } } } /** * Given a page ID or an alias, redirect to it */ function redirect_to_alias($alias) { global $gCms; $manager =& $gCms->GetHierarchyManager(); $node =& $manager->sureGetNodeByAlias($alias); $content =& $node->GetContent(); if (isset($content)) { if ($content->GetURL() != '') { redirect($content->GetURL()); } } } /** * Shows the difference in seconds between two microtime() values * * @since 0.3 */ function microtime_diff($a, $b) { list($a_dec, $a_sec) = explode(" ", $a); list($b_dec, $b_sec) = explode(" ", $b); return $b_sec - $a_sec + $b_dec - $a_dec; } /** * Joins a path together using proper directory separators * Taken from: http://www.php.net/manual/en/ref.dir.php * * @since 0.14 */ function cms_join_path() { $args = func_get_args(); return implode(DIRECTORY_SEPARATOR,$args); } /** * Shows a very close approximation of an Apache generated 404 error. * * Shows a very close approximation of an Apache generated 404 error. * It also sends the actual header along as well, so that generic * browser error pages (like what IE does) will be displayed. * * @since 0.3 */ #function ErrorHandler404($errno, $errmsg, $filename, $linenum, $vars) function ErrorHandler404() { #if ($errno == E_USER_WARNING) { @ob_end_clean(); header("HTTP/1.0 404 Not Found"); header("Status: 404 Not Found"); echo ' 404 Not Found

Not Found

The requested URL was not found on this server.

'; exit(); #} } /** * Simple template parser * * @since 0.6.1 */ function parse_template ($template, $tpl_array, $warn=0) { while ( list ($key,$val) = each ($tpl_array) ) { if (!(empty($key))) { if(gettype($val) != "string") { settype($val,"string"); } $template = eregi_replace('\{' . $key . '\}',$val,$template); } } if(!$warn) { // Silently remove anything not already found $template = ereg_replace('\{[A-Z0-9_]+\}', "", $template); } else { // Warn about unresolved template variables if (ereg('\{[A-Z0-9_]+\}',$template)) { $unknown = split("\n",$template); while (list ($Element,$Line) = each($unknown) ) { $UnkVar = $Line; if(!(empty($UnkVar))) { $this->show_unknowns($UnkVar); } } } } return $template; } // end parse_template(); function cms_htmlentities($string, $param=ENT_QUOTES, $charset="UTF-8", $convert_single_quotes = false) { $result = ""; #$result = htmlentities($string, $param, $charset); $result = my_htmlentities($string, $convert_single_quotes); return $result; } /** * Clean up the filename, and ensure that the filename resides underneath * the cms_root directory, if it does not replace it with the hardcoded * string CLEANED_FILENAME */ define('CLEANED_FILENAME','BAD_FILE'); function cms_cleanfile($filename) { $realpath = realpath($filename); if( $realpath === FALSE ) { return CLEANED_FILENAME; } // This ensures that the file specified is somewhere // underneath the cms root path global $gCms; $config =& $gCms->GetConfig(); if( strpos($realpath, $config['root_path']) !== 0 ) { return CLEANED_FILENAME; } return $realpath; } /** * Figures out the page name from the uri string. Has to use different logic * based on the type of httpd server. */ function cms_calculate_url() { $result = ''; global $gCms; $config =& $gCms->GetConfig(); //Apache /* if (isset($_SERVER["PHP_SELF"]) && !endswith($_SERVER['PHP_SELF'], 'index.php')) { $matches = array(); //Seems like PHP_SELF has whatever is after index.php in certain situations if (strpos($_SERVER['PHP_SELF'], 'index.php') !== FALSE) { if (preg_match('/.*index\.php\/(.*?)$/', $_SERVER['PHP_SELF'], $matches)) { $result = $matches[1]; } } else { $result = $_SERVER['PHP_SELF']; } } */ //lighttpd #else if (isset($_SERVER["REQUEST_URI"]) && !endswith($_SERVER['REQUEST_URI'], 'index.php')) //apache and lighttpd if (isset($_SERVER["REQUEST_URI"]) && !endswith($_SERVER['REQUEST_URI'], 'index.php')) { $matches = array(); if (preg_match('/.*index\.php\/(.*?)$/', $_SERVER['REQUEST_URI'], $matches)) { $result = $matches[1]; } } //trim off the extension, if there is one set if ($config['page_extension'] != '' && endswith($result, $config['page_extension'])) { $result = substr($result, 0, strlen($result) - strlen($config['page_extension'])); } return $result; } /** * Enter description here... * * @param unknown $val * @param integer $quote_style * @return unknown * * $quote_style may be one of: * ENT_COMPAT : Will convert double-quotes and leave single-quotes alone. * ENT_QUOTES : Will convert both double and single quotes. * ENT_NOQUOTES : Will leave both double and single quotes unconverted. */ function my_htmlentities($val, $convert_single_quotes = false) { if ($val == "") { return ""; } $val = str_replace( " ", " ", $val ); //Remove sneaky spaces // $val = str_replace( chr(0xCA), "", $val ); $val = str_replace( "&" , "&" , $val ); $val = str_replace( "" , "-->" , $val ); $val = preg_replace( "/