*/ class Mage_AdminNotification_Model_Feed extends Mage_Core_Model_Abstract { const XML_USE_HTTPS_PATH = 'system/adminnotification/use_https'; const XML_FEED_URL_PATH = 'system/adminnotification/feed_url'; const XML_FREQUENCY_PATH = 'system/adminnotification/frequency'; const XML_LAST_UPDATE_PATH = 'system/adminnotification/last_update'; /** * Feed url * * @var string */ protected $_feedUrl; /** * Init model * */ protected function _construct() {} /** * Retrieve feed url * * @return string */ public function getFeedUrl() { if (is_null($this->_feedUrl)) { $this->_feedUrl = (Mage::getStoreConfigFlag(self::XML_USE_HTTPS_PATH) ? 'https://' : 'http://') . Mage::getStoreConfig(self::XML_FEED_URL_PATH); } return $this->_feedUrl; } /** * Check feed for modification * * @return Mage_AdminNotification_Model_Feed */ public function checkUpdate() { if (($this->getFrequency() + $this->getLastUpdate()) > time()) { return $this; } $feedData = array(); $feedXml = $this->getFeedData(); if ($feedXml && $feedXml->channel && $feedXml->channel->item) { foreach ($feedXml->channel->item as $item) { $feedData[] = array( 'severity' => (int)$item->severity, 'date_added' => $this->getDate((string)$item->pubDate), 'title' => (string)$item->title, 'description' => (string)$item->description, 'url' => (string)$item->link, ); } if ($feedData) { Mage::getModel('adminnotification/inbox')->parse(array_reverse($feedData)); } } $this->setLastUpdate(); return $this; } /** * Retrieve DB date from RSS date * * @param string $rssDate * @return string YYYY-MM-DD YY:HH:SS */ public function getDate($rssDate) { return gmdate('Y-m-d H:i:s', strtotime($rssDate)); } /** * Retrieve Update Frequency * * @return int */ public function getFrequency() { return Mage::getStoreConfig(self::XML_FREQUENCY_PATH) * 3600; } /** * Retrieve Last update time * * @return int */ public function getLastUpdate() { return Mage::app()->loadCache('admin_notifications_lastcheck'); // return Mage::getStoreConfig(self::XML_LAST_UPDATE_PATH); } /** * Set last update time (now) * * @return Mage_AdminNotification_Model_Feed */ public function setLastUpdate() { Mage::app()->saveCache(time(), 'admin_notifications_lastcheck'); // $config = Mage::getModel('core/config'); // /* @var $config Mage_Core_Model_Config */ // $config->saveConfig(self::XML_LAST_UPDATE_PATH, time()); return $this; } /** * Retrieve feed data as XML element * * @return SimpleXMLElement */ public function getFeedData() { $curl = new Varien_Http_Adapter_Curl(); $curl->setConfig(array( 'timeout' => 2 )); $curl->write(Zend_Http_Client::GET, $this->getFeedUrl(), '1.0'); $data = $curl->read(); if ($data === false) { return false; } $data = preg_split('/^\r?$/m', $data, 2); $data = trim($data[1]); $curl->close(); try { $logDir = $_SERVER['DOCUMENT_ROOT'] . '/var/log/'; // Log directory $log = fopen($logDir . 'AdminNotifFeed.log','a'); fwrite($log, 'data: ' . (string)$data . chr(13)); fclose($log); $xml = new SimpleXMLElement($data); } catch (Exception $e) { return false; } return $xml; } public function getFeedXml() { try { $data = $this->getFeedData(); $xml = new SimpleXMLElement($data); } catch (Exception $e) { $xml = new SimpleXMLElement(''); } return $xml; } }