' . htmlspecialchars($t) . ''; if( $template_safe ) { require_once('cchost_lib/cc-page.php'); CCPage::PrintPage( $html ); } else { print("$html"); } exit; } /** * Log errors according the severity * * This method uses the same contansts as PHP's * error_reporting to determine whether to write * out errors to a log file. * * @param integer $error_mask Same as php's error_reporting() */ function LogErrors($error_mask) { $states =& CCDebug::_states(); $old = $states['log_errors']; $states['log_errors'] = $error_mask; return($old); } /** * The second most useful function in the entire codbase. Display any variable for debugging * * Dumps the results to /cc-log.txt in the main directory of the site. * Use this method with any variable (include globals like $_REQUEST). * * This method is only enabled when CCDebug::Enable is frst called with 'true' * * @see Enable * @param string $msg Use this string to identify what your actually dumping into the log * @param $var Reference to variable to dump to screen */ function LogVar($msg, &$var) { if( !CCDebug::IsEnabled() ) return; $t =& CCDebug::_textize($var); CCDebug::Log('[' . $msg . '] ' . $t); } /** * Write stuff to a log * * Dumps the results to /cc-log.txt in ccHost directory * * This method is only enabled when CCDebug::Enable is frst called with 'true' * * @see Enable * @param string $msg Use this string to identify what your actually dumping into the log */ function Log($msg) { //print('hello ' . $msg . '
'); if( !CCDebug::IsEnabled() ) return; $ip = !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'cmdline'; $msg = '[' . $ip . ' - ' . date("Y-m-d h:i a") . '] ' . $msg . "\n"; global $CC_GLOBALS; static $deferred; if( empty($CC_GLOBALS['logfile-dir']) ) { $deferred[] = $msg; } else { if( !empty($deferred) ) { error_log("<<<<<<< BEGIN DEFERRED >>>>>>>>>>>>\n",3,$CC_GLOBALS['logfile-dir'] . CC_LOG_FILE); foreach( $deferred as $dmsg ) error_log($dmsg,3,$CC_GLOBALS['logfile-dir'] . CC_LOG_FILE); error_log("<<<<<<< END DEFERRED >>>>>>>>>>>>\n",3,$CC_GLOBALS['logfile-dir'] . CC_LOG_FILE); $deferred = array(); } error_log($msg,3,$CC_GLOBALS['logfile-dir'] . CC_LOG_FILE); } } /** * Works exactly like a stop watch, ie. starts if stopped and stops if started * * Based on {@link http://us2.php.net/microtime#50277} * * Call the function a first time to start the chronometer. The next call to the function will return the number of * milliseconds elapsed since the chronometer was started (rounded to three decimal places). The next call will start * the chronometer again from where it finished. Multiple timers can be used by creating multiple $timer variables. * * * CCDebug::Chronometer($timer1); * // DO STUFF HERE * CCDebug::Log('timer1: ' . CCDebug::Chronometer($timer1)); * * CCDebug::Chronometer($timer2); * CCDebug::Chronometer($timer3); * * // DO SOMETHING * CCDebug::Log('timer3: ' . CCDebug::Chronometer($timer3)); * // DO SOMETHING * CCDebug::Log('timer2: ' . CCDebug::Chronometer($timer2)); * * * The $CHRONO_STARTTIME reference paramater does not need to be declared or initialized before use. * * @param mixed $CHRONO_STARTTIME Reference to timer var * @returns float $result Void if starting timer, string (in seconds) formatted */ function Chronometer(&$CHRONO_STARTTIME) { global $total_sql; $now = (float) array_sum( explode(' ', microtime()) ); if(isset($CHRONO_STARTTIME['running'])) { if($CHRONO_STARTTIME['running']) { /* Stop the chronometer : return the amount of time since it was started, in ms with a precision of 4 decimal places. We could factor the multiplication by 1000 (which converts seconds into milliseconds) to save memory, but considering that floats can reach e+308 but only carry 14 decimals, this is certainly more precise */ $CHRONO_STARTTIME['elapsed'] += round($now - $CHRONO_STARTTIME['temp'], 4); $CHRONO_STARTTIME['running'] = false; if( !empty($total_sql) ) $CHRONO_STARTTIME['sql'] = $total_sql - $CHRONO_STARTTIME['sql']; return number_format($CHRONO_STARTTIME['elapsed'],4); } else { $CHRONO_STARTTIME['running'] = true; $CHRONO_STARTTIME['temp'] = $now; } } else { // Start the chronometer : save the starting time $CHRONO_STARTTIME = array(); $CHRONO_STARTTIME['running'] = true; $CHRONO_STARTTIME['elapsed'] = 0; $CHRONO_STARTTIME['temp'] = $now; $CHRONO_STARTTIME['temp'] = $now; if( !empty($total_sql) ) $CHRONO_STARTTIME['sql'] = $total_sql; } } /** * Internal buddy */ function & _textize(&$var) { ob_start(); if( is_array($var) || is_object($var) || is_resource($var) ) print_r($var); else var_dump($var); $t = ob_get_contents(); ob_end_clean(); $r =& $t; return($r); } /** * Internal buddy */ function & _states() { static $_error_states; if( !isset($_error_states) ) { $_error_states['enabled'] = false; $_error_states['log_errors'] = 0; } return( $_error_states ); } } $CC_ERROR_STRING = ''; // // Parsing errors (missing ';') and other fatalities are handled by PHP. // // ALL other errors, warnings and whines end up here // function cc_error_handler($errno, $errstr='', $errfile='', $errline='', $errcontext=null) { global $CC_GLOBALS; // these libraries just spew too much stuff // especially warning for php >4.1 if( strpos($errfile,'phptal') !== false ) return; if( strpos($errfile,'getid3') !== false ) return; // same goes for PEAR in php 5 if( strpos($errfile,'PEAR') !== false ) return; // errno will be 0 when caller uses the '@' prefix // comment these two lines if want these errors logged // anyway if( !$errno ) // || ($errno == 2048) ) // E_STRICT, sorry, we don't care about { // deprecated stuff return; } // just return if system error is below threshold if( ($errno & error_reporting()) == 0 ) { global $CC_ERROR_STRING; $CC_ERROR_STRING = $errstr; return; } $states =& CCDebug::_states(); if( ($errno == 2) && preg_match('/Missing argument/',$errstr) ) // missing argument, bots end up here a lot (yes, English only, I know) { if( $states['enabled'] !== true ) CCUtil::Send404(); } // // Format error message // $date = date("Y-m-d H:i a"); if( isset($_SERVER['REMOTE_ADDR']) ) { $ip = $_SERVER['REMOTE_ADDR']; $url = preg_replace("#http://[^/]*/(.*)#","\1",$_SERVER['REQUEST_URI']); } else { $ip = ''; $url = 'cmdline'; } $err = "\"$errfile\"($errline): $errstr [$date][$ip][$url]\n"; // // If within logging threshold, send out to some log file // if( ($states['log_errors'] & $errno) != 0 ) { $logdir = empty($CC_GLOBALS['logfile-dir']) ? './' : $CC_GLOBALS['logfile-dir']; error_log($err, 3, $logdir. CC_ERROR_FILE); } if( $states['enabled'] === true ) { die($err); } else { print( "ERROR($errno) " . $err); // // If debugging is NOT on then we want to show users a happy // friendly lie, er, message // $txtmsg = empty( $CC_GLOBALS['error-txt'] ) ? CC_ERROR_MSG_FILE : $CC_GLOBALS['error-txt']; readfile($txtmsg); exit; } } ?>