/** * nexista_cache * * Output buffer utilizing PEAR_Cache * * @param object $init includes stuff * * @return boolean */ function Nexista_cache($init) { //configuration - move to xml $timers = true; //necessary stuff no matter what $init->process(); $uid = Nexista_Flow::get('//runtime/user_id'); $uri = $_SERVER['REQUEST_URI']; $cac = NX_PATH_CACHE . 'cache_' . md5($uid) . '_' . md5($uri); $exp = $init->getInfo('cacheExpiryTime'); //first things first - check for if-modified-since if ($ims = $_SERVER['HTTP_IF_MODIFIED_SINCE']) { if (is_file($cac)) { $ims = strtotime($ims); $lm = filemtime($cac); $etag = md5(file_get_contents($cac)); //$ctag = $_SERVER['HTTP_IF_NONE_MATCH']; if ($lm <= $ims) { // Hasn't been modified, is it still fresh? $fresh = $lm + $exp - time(); if ($fresh > 0) { header("HTTP/1.1 304 Not Modified"); // If its close to expiring, extend life if ($fresh < $exp / 10) { touch($cac); } exit; } // if not fresh, it will be rebuilt } } } include_once 'Cache/Lite.php'; /* TODO - Add autocleaning */ $options = array('cacheDir' => NX_PATH_CACHE, 'caching' => true, 'automaticCleaningFactor' => 0, 'readControl' => false, 'lifeTime' => $exp); $cache = new Cache_Lite($options); $content_type = $init->getInfo('content_type'); $cache_control = $init->getInfo('cache_control'); ob_start(); ob_start(); if ($output = $cache->get($uri, $uid)) { $cache_type = 'file cache'; $lm = filemtime($cac); header("Last-Modified: " . gmdate('D, d M Y H:i:s', $lm) . " GMT"); } else { header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); $cache_type = "no cache"; $output = $init->run(); if ($timers) { $server_time = Nexista_Debug::profile(); if ($content_type == 'text/html') { $output = str_replace('</body>', '', $output); $output = str_replace('</html>', '', $output); $output .= "\n\n<!--\nOriginal request required: {$server_time}! \n-->\n\n"; $output .= '</body></html>'; } elseif ($content_type == 'text/css') { $output .= "\n/* Original request required: {$server_time}! */"; } } if (!is_dir(NX_PATH_CACHE)) { @mkdir(NX_PATH_CACHE, 0700, true); } $cache->save($output, $uri, $uid); header("ETag: " . md5($output)); } if ($content_type == 'text/html') { $output = str_replace('</body>', '', $output); $output = str_replace('</html>', '', $output); } echo $output; if ($timers) { $server_time = Nexista_Debug::profile(); if ($content_type == 'text/html') { echo "<!--\n"; echo "Nexista Cache Information:\n"; echo "Output generated by {$cache_type} in {$server_time}.\n"; echo "Output sent with Cache-Control: {$cache_control} headers, which will affect future requests.\n"; if ($lm > 0) { echo " Last modified: {$lm}. "; echo "Cache created: " . gmdate('D, d M Y H:i:s', $lm) . " GMT\n"; } echo "Current time: " . gmdate('D, d M Y H:i:s') . " GMT\n-->\n"; echo "</body></html>"; } elseif ($content_type == "text/css") { echo "\n/* Output generated by {$cache_type} in {$server_time}. Last modified: {$lm}. */"; } elseif ($content_type == "application/javascript") { echo "\n// Output generated by {$cache_type} in {$server_time}. Last modified: {$lm}. "; } } ob_end_flush(); header('Content-Length: ' . ob_get_length()); ob_end_flush(); }
function nexista_final_notices($cacher = null, $mode) { $server_time = Nexista_Debug::profile(); $final_notices = "\n<script type=\"text/javascript\">\n done_loading({$server_time});\n if (typeof jQuery != 'undefined') {\n \$(document).ready(function()\n {\n done_loading_js();\n });\n }\n</script>\n"; return $final_notices; }
/** * Registers current active module * * This function is used to register the name of the current * active module/function in order to help trace debugging and profiling. * $type determines the type of message: * 'in' - sets function entry * 'out' - sets function exit - prints elapsed time * * @param string $type type of registration * @param string $function module name * * @return string elapsed time */ public static function register($type, $function) { switch ($type) { case 'in': $pos = 0; if (isset($GLOBALS['debugTrackModule'])) { $pos = @count($GLOBALS['debugTrackModule']); } $GLOBALS['debugTrackModule'][$pos]['name'] = $function; $GLOBALS['debugTrackModule'][$pos]['startTime'] = microtime(); $indent = $pos * 6; Nexista_Debug::message(str_pad('> ', $indent, '-', STR_PAD_LEFT) . '<b>' . $function . '</b>'); break; case 'out': $pos = @count($GLOBALS['debugTrackModule']) - 1; $indent = $pos * 6; $start_time = $GLOBALS['debugTrackModule'][$pos]['startTime']; $GLOBALS['debugTrackModule'][$pos]['elapsedTime'] = Nexista_Debug::profile($start_time); Nexista_Debug::message(str_pad('< ', $indent, "-", STR_PAD_LEFT) . '<b>' . $function . ' @ </b>' . $GLOBALS['debugTrackModule'][$pos]['elapsedTime'] . ' seconds'); unset($GLOBALS['debugTrackModule'][$pos]); break; } // This is for debugging the debug class. Probably don't need this now //Nexista_Debug::dump($GLOBALS['debugTrackModule']); }