<?php class NP_Cache extends NucleusPlugin { var $cache_dir = ''; function getMinNucleusVersion() { return 155; } // name of plugin function getName() { return 'Nucleus Cache'; } // author of plugin function getAuthor() { return 'Jim Burnett'; } // an URL to the plugin website // can also be of the form mailto:foo@bar.com function getURL() { return 'http://www.mediauncut.com/'; } // version of the plugin function getVersion() { return '1.1.01'; } // a description to be shown on the installed plugins listing function getDescription() { return 'Enables caching of nucleus items, homepage, searches and anything off index.php.'; } function getEventList() { return array('PreSkinParse', 'PostSkinParse','PreAddItem','PreUpdateItem'); } function install() { $this->createOption('cache_dir','Enter the directory (no trailing slash) of the cache directory: ','text','/tmp'); } function init() { $this->cache_dir = trim($this->getOption('cache_dir')); $this->obStarted = 0; } function event_PreSkinParse(&$data) { ob_start(); $bStart = 1; // don't start output buffering if we're not at the top level // (fixes problems with RSS feeds & ETAG) if (function_exists('ob_get_level') && (ob_get_level() > 0)) $bStart = 0; if ($bStart) $this->obStarted = ob_start("ob_gzhandler"); } function event_PreAddItem(&$data){ $this->deleteCacheFile($data); } function event_PreUpdateItem(&$data){ $this->deleteCacheFile($data); } function deleteCacheFile($data){ $url = createItemLink( $data['itemid']); $a = $url; $a = str_replace("/","-",$a); $a = str_replace("&","-",$a); $a = str_replace("?","-",$a); $a = str_replace("=","-",$a); $cache_file = $this->cache_dir."/cache" . $a; if ( file_exists( $cache_file )) unlink( $cache_file ); $cache_file = $this->cache_dir."/cache-"; if ( file_exists( $cache_file )) unlink( $cache_file ); } function event_PostSkinParse(&$data) { $a = getenv('REQUEST_URI'); $a = str_replace("/","-",$a); $a = str_replace("&","-",$a); $a = str_replace("?","-",$a); $a = str_replace("=","-",$a); $cache_file = $this->cache_dir."/cache" . $a; //If the cache file doesnt exists then check the date/time of it. if ( !file_exists( $cache_file )) { //can we open the cache file for writing? if (!$handle = fopen($cache_file, 'w')) { echo "Cannot open file ($cache_file)"; exit; } //ok write to the cache file.. if (fwrite($handle, ob_get_contents() ) === FALSE) { echo "Cannot write to file ($cache_file)"; exit; } } ob_end_flush(); } } ?>