1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_AdminNotification_Model_Feed extends Mage_Core_Model_Abstract
36: {
37: const XML_USE_HTTPS_PATH = 'system/adminnotification/use_https';
38: const XML_FEED_URL_PATH = 'system/adminnotification/feed_url';
39: const XML_FREQUENCY_PATH = 'system/adminnotification/frequency';
40: const XML_LAST_UPDATE_PATH = 'system/adminnotification/last_update';
41:
42: 43: 44: 45: 46:
47: protected $_feedUrl;
48:
49: 50: 51: 52:
53: protected function _construct()
54: {}
55:
56: 57: 58: 59: 60:
61: public function getFeedUrl()
62: {
63: if (is_null($this->_feedUrl)) {
64: $this->_feedUrl = (Mage::getStoreConfigFlag(self::XML_USE_HTTPS_PATH) ? 'https://' : 'http://')
65: . Mage::getStoreConfig(self::XML_FEED_URL_PATH);
66: }
67: return $this->_feedUrl;
68: }
69:
70: 71: 72: 73: 74:
75: public function checkUpdate()
76: {
77: if (($this->getFrequency() + $this->getLastUpdate()) > time()) {
78: return $this;
79: }
80:
81: $feedData = array();
82:
83: $feedXml = $this->getFeedData();
84:
85: if ($feedXml && $feedXml->channel && $feedXml->channel->item) {
86: foreach ($feedXml->channel->item as $item) {
87: $feedData[] = array(
88: 'severity' => (int)$item->severity,
89: 'date_added' => $this->getDate((string)$item->pubDate),
90: 'title' => (string)$item->title,
91: 'description' => (string)$item->description,
92: 'url' => (string)$item->link,
93: );
94: }
95:
96: if ($feedData) {
97: Mage::getModel('adminnotification/inbox')->parse(array_reverse($feedData));
98: }
99:
100: }
101: $this->setLastUpdate();
102:
103: return $this;
104: }
105:
106: 107: 108: 109: 110: 111:
112: public function getDate($rssDate)
113: {
114: return gmdate('Y-m-d H:i:s', strtotime($rssDate));
115: }
116:
117: 118: 119: 120: 121:
122: public function getFrequency()
123: {
124: return Mage::getStoreConfig(self::XML_FREQUENCY_PATH) * 3600;
125: }
126:
127: 128: 129: 130: 131:
132: public function getLastUpdate()
133: {
134: return Mage::app()->loadCache('admin_notifications_lastcheck');
135:
136: }
137:
138: 139: 140: 141: 142:
143: public function setLastUpdate()
144: {
145: Mage::app()->saveCache(time(), 'admin_notifications_lastcheck');
146:
147:
148:
149: return $this;
150: }
151:
152: 153: 154: 155: 156:
157: public function getFeedData()
158: {
159: $curl = new Varien_Http_Adapter_Curl();
160: $curl->setConfig(array(
161: 'timeout' => 2
162: ));
163: $curl->write(Zend_Http_Client::GET, $this->getFeedUrl(), '1.0');
164: $data = $curl->read();
165: if ($data === false) {
166: return false;
167: }
168: $data = preg_split('/^\r?$/m', $data, 2);
169: $data = trim($data[1]);
170: $curl->close();
171:
172: try {
173: $xml = new SimpleXMLElement($data);
174: }
175: catch (Exception $e) {
176: return false;
177: }
178:
179: return $xml;
180: }
181:
182: public function getFeedXml()
183: {
184: try {
185: $data = $this->getFeedData();
186: $xml = new SimpleXMLElement($data);
187: }
188: catch (Exception $e) {
189: $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>');
190: }
191:
192: return $xml;
193: }
194: }
195: