/** * Get contents from a url or a local file - Cache urls for given cachetime * * @param mixed $path The URL or file path * @return string | false */ static function get($path) { # is local file if (!preg_match("~^(http|https)~i", $path)) { if (file_exists($path)) { return file_get_contents($path); } return false; } # is url $dir = CHOQ_ACTIVE_MODULE_DIRECTORY . "/tmp"; $file = "{$dir}/filecontents." . md5($path); if (!file_exists($file) || filemtime($file) < time() - self::CACHETIME) { $data = self::loadUrl($path); if ($data === false) { RDR_Event::log(RDR_Event::TYPE_FEED_URLERROR, array("text" => $path)); return; } # writing to cache file_put_contents($file, $data); } else { # return cached file return file_get_contents($file); } return $data; }
/** * Load the View */ public function onLoad() { if (!inNormalMode()) { return; } # set time limit to max 10 minutes # if this limit is reached than the script stops and continue at next cron set_time_limit(RDR_Cron::MAXTIME); $cronPidFile = self::getPIDFile(); # skip when cron is already running if (self::isRunning()) { RDR_Event::log(RDR_Event::TYPE_CRON_RUNNING); return; } # create a tmp file that show us the cron pid file_put_contents($cronPidFile, time()); $param = $this->getParam("param"); if ($param != self::getHash()) { die("Not allowed"); } RDR_Event::log(RDR_Event::TYPE_CRON_START); RDR_Import::updateAllFeeds(); RDR_Event::log(RDR_Event::TYPE_CRON_END); RDR_Cleanup::cleanupEvents(); RDR_Cleanup::cleanupEntries(); RDR_FileContents::cleanupTmpFiles(); RDR_Proxy::cleanupTmpFiles(); # optimizing tables $generator = CHOQ_DB_Generator::create(db()); if ($generator instanceof CHOQ_DB_Generator_Mysql) { $generator->addModule("RDR"); $generator->optimizeTables(); } # delete tmp file that show us the cron pid unlink(CHOQ_ACTIVE_MODULE_DIRECTORY . "/tmp/cron.pid"); }
/** * Get Simple xml from a feed url * * @param mixed $url * @return SimpleXMLElement | bool */ private static function getSimpleXMLFromUrl($url) { $data = RDR_FileContents::get($url); if ($data === false) { return false; } $xml = @simplexml_load_string($data, null, LIBXML_NOCDATA); if (!$xml) { RDR_Event::log(RDR_Event::TYPE_SIMPLEXML_ERROR, array("text" => $url)); return false; } return $xml; }