00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_AdminNotification_Model_Feed extends Mage_Core_Model_Abstract
00036 {
00037 const XML_USE_HTTPS_PATH = 'system/adminnotification/use_https';
00038 const XML_FEED_URL_PATH = 'system/adminnotification/feed_url';
00039 const XML_FREQUENCY_PATH = 'system/adminnotification/frequency';
00040 const XML_LAST_UPDATE_PATH = 'system/adminnotification/last_update';
00041
00042
00043
00044
00045
00046
00047 protected $_feedUrl;
00048
00049
00050
00051
00052
00053 protected function _construct()
00054 {}
00055
00056
00057
00058
00059
00060
00061 public function getFeedUrl()
00062 {
00063 if (is_null($this->_feedUrl)) {
00064 $this->_feedUrl = (Mage::getStoreConfigFlag(self::XML_USE_HTTPS_PATH) ? 'https://' : 'http://')
00065 . Mage::getStoreConfig(self::XML_FEED_URL_PATH);
00066 }
00067 return $this->_feedUrl;
00068 }
00069
00070
00071
00072
00073
00074
00075 public function checkUpdate()
00076 {
00077 if (($this->getFrequency() + $this->getLastUpdate()) > time()) {
00078 return $this;
00079 }
00080
00081 $feedData = array();
00082
00083 $feedXml = $this->getFeedData();
00084
00085 if ($feedXml && $feedXml->channel && $feedXml->channel->item) {
00086 foreach ($feedXml->channel->item as $item) {
00087 $feedData[] = array(
00088 'severity' => (int)$item->severity,
00089 'date_added' => $this->getDate((string)$item->pubDate),
00090 'title' => (string)$item->title,
00091 'description' => (string)$item->description,
00092 'url' => (string)$item->link,
00093 );
00094 }
00095
00096 if ($feedData) {
00097 Mage::getModel('adminnotification/inbox')->parse(array_reverse($feedData));
00098 }
00099
00100 }
00101 $this->setLastUpdate();
00102
00103 return $this;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 public function getDate($rssDate)
00113 {
00114 return gmdate('Y-m-d H:i:s', strtotime($rssDate));
00115 }
00116
00117
00118
00119
00120
00121
00122 public function getFrequency()
00123 {
00124 return Mage::getStoreConfig(self::XML_FREQUENCY_PATH) * 3600;
00125 }
00126
00127
00128
00129
00130
00131
00132 public function getLastUpdate()
00133 {
00134 return Mage::app()->loadCache('admin_notifications_lastcheck');
00135
00136 }
00137
00138
00139
00140
00141
00142
00143 public function setLastUpdate()
00144 {
00145 Mage::app()->saveCache(time(), 'admin_notifications_lastcheck');
00146
00147
00148
00149 return $this;
00150 }
00151
00152
00153
00154
00155
00156
00157 public function getFeedData()
00158 {
00159 $curl = new Varien_Http_Adapter_Curl();
00160 $curl->setConfig(array(
00161 'timeout' => 2
00162 ));
00163 $curl->write(Zend_Http_Client::GET, $this->getFeedUrl(), '1.0');
00164 $data = $curl->read();
00165 if ($data === false) {
00166 return false;
00167 }
00168 $data = preg_split('/^\r?$/m', $data, 2);
00169 $data = trim($data[1]);
00170 $curl->close();
00171
00172 try {
00173 $xml = new SimpleXMLElement($data);
00174 }
00175 catch (Exception $e) {
00176 return false;
00177 }
00178
00179 return $xml;
00180 }
00181
00182 public function getFeedXml()
00183 {
00184 try {
00185 $data = $this->getFeedData();
00186 $xml = new SimpleXMLElement($data);
00187 }
00188 catch (Exception $e) {
00189 $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>');
00190 }
00191
00192 return $xml;
00193 }
00194 }