/xampp/htdocs/magento/app/code/core/Mage/AdminNotification/Model/Feed.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site · PHP · 194 lines · 93 code · 21 blank · 80 comment · 7 complexity · e8b7afa87f3239859e5814a0e57fa9f6 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_AdminNotification
  23. * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * AdminNotification Feed model
  28. *
  29. * @category Mage
  30. * @package Mage_AdminNotification
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_AdminNotification_Model_Feed extends Mage_Core_Model_Abstract
  34. {
  35. const XML_USE_HTTPS_PATH = 'system/adminnotification/use_https';
  36. const XML_FEED_URL_PATH = 'system/adminnotification/feed_url';
  37. const XML_FREQUENCY_PATH = 'system/adminnotification/frequency';
  38. const XML_LAST_UPDATE_PATH = 'system/adminnotification/last_update';
  39. /**
  40. * Feed url
  41. *
  42. * @var string
  43. */
  44. protected $_feedUrl;
  45. /**
  46. * Init model
  47. *
  48. */
  49. protected function _construct()
  50. {}
  51. /**
  52. * Retrieve feed url
  53. *
  54. * @return string
  55. */
  56. public function getFeedUrl()
  57. {
  58. if (is_null($this->_feedUrl)) {
  59. $this->_feedUrl = (Mage::getStoreConfigFlag(self::XML_USE_HTTPS_PATH) ? 'https://' : 'http://')
  60. . Mage::getStoreConfig(self::XML_FEED_URL_PATH);
  61. }
  62. return $this->_feedUrl;
  63. }
  64. /**
  65. * Check feed for modification
  66. *
  67. * @return Mage_AdminNotification_Model_Feed
  68. */
  69. public function checkUpdate()
  70. {
  71. if (($this->getFrequency() + $this->getLastUpdate()) > time()) {
  72. return $this;
  73. }
  74. $feedData = array();
  75. $feedXml = $this->getFeedData();
  76. if ($feedXml && $feedXml->channel && $feedXml->channel->item) {
  77. foreach ($feedXml->channel->item as $item) {
  78. $feedData[] = array(
  79. 'severity' => (int)$item->severity,
  80. 'date_added' => $this->getDate((string)$item->pubDate),
  81. 'title' => (string)$item->title,
  82. 'description' => (string)$item->description,
  83. 'url' => (string)$item->link,
  84. );
  85. }
  86. if ($feedData) {
  87. Mage::getModel('adminnotification/inbox')->parse(array_reverse($feedData));
  88. }
  89. }
  90. $this->setLastUpdate();
  91. return $this;
  92. }
  93. /**
  94. * Retrieve DB date from RSS date
  95. *
  96. * @param string $rssDate
  97. * @return string YYYY-MM-DD YY:HH:SS
  98. */
  99. public function getDate($rssDate)
  100. {
  101. return gmdate('Y-m-d H:i:s', strtotime($rssDate));
  102. }
  103. /**
  104. * Retrieve Update Frequency
  105. *
  106. * @return int
  107. */
  108. public function getFrequency()
  109. {
  110. return Mage::getStoreConfig(self::XML_FREQUENCY_PATH) * 3600;
  111. }
  112. /**
  113. * Retrieve Last update time
  114. *
  115. * @return int
  116. */
  117. public function getLastUpdate()
  118. {
  119. return Mage::app()->loadCache('admin_notifications_lastcheck');
  120. // return Mage::getStoreConfig(self::XML_LAST_UPDATE_PATH);
  121. }
  122. /**
  123. * Set last update time (now)
  124. *
  125. * @return Mage_AdminNotification_Model_Feed
  126. */
  127. public function setLastUpdate()
  128. {
  129. Mage::app()->saveCache(time(), 'admin_notifications_lastcheck');
  130. // $config = Mage::getModel('core/config');
  131. // /* @var $config Mage_Core_Model_Config */
  132. // $config->saveConfig(self::XML_LAST_UPDATE_PATH, time());
  133. return $this;
  134. }
  135. /**
  136. * Retrieve feed data as XML element
  137. *
  138. * @return SimpleXMLElement
  139. */
  140. public function getFeedData()
  141. {
  142. $curl = new Varien_Http_Adapter_Curl();
  143. $curl->setConfig(array(
  144. 'timeout' => 2
  145. ));
  146. $curl->write(Zend_Http_Client::GET, $this->getFeedUrl(), '1.0');
  147. $data = $curl->read();
  148. if ($data === false) {
  149. return false;
  150. }
  151. $data = preg_split('/^\r?$/m', $data, 2);
  152. $data = trim($data[1]);
  153. $curl->close();
  154. try {
  155. $xml = new SimpleXMLElement($data);
  156. }
  157. catch (Exception $e) {
  158. return false;
  159. }
  160. return $xml;
  161. }
  162. public function getFeedXml()
  163. {
  164. try {
  165. $data = $this->getFeedData();
  166. $xml = new SimpleXMLElement($data);
  167. }
  168. catch (Exception $e) {
  169. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>');
  170. }
  171. return $xml;
  172. }
  173. }