| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * Main page for blog dashlet |
| 20 | */ |
| 21 | class CRM_Dashlet_Page_Blog extends CRM_Core_Page { |
| 22 | |
| 23 | const CHECK_TIMEOUT = 5; |
| 24 | const CACHE_DAYS = 1; |
| 25 | const NEWS_URL = 'https://civicrm.org/news-feed.rss'; |
| 26 | |
| 27 | /** |
| 28 | * Gets url for blog feed. |
| 29 | * |
| 30 | * @return string |
| 31 | */ |
| 32 | public function getNewsUrl() { |
| 33 | // Note: We use "*default*" as the default (rather than self::NEWS_URL) so that future |
| 34 | // developers can change NEWS_URL without needing to update {civicrm_setting}. |
| 35 | $url = Civi::settings()->get('blogUrl'); |
| 36 | if ($url === '*default*') { |
| 37 | $url = self::NEWS_URL; |
| 38 | } |
| 39 | return CRM_Utils_System::evalUrl($url); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Output data to template. |
| 44 | */ |
| 45 | public function run() { |
| 46 | $this->assign('feeds', $this->getData()); |
| 47 | return parent::run(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Load feeds from cache. |
| 52 | * |
| 53 | * Refresh cache if expired. |
| 54 | * |
| 55 | * @return array |
| 56 | */ |
| 57 | protected function getData() { |
| 58 | $value = Civi::cache('community_messages')->get('dashboard_newsfeed'); |
| 59 | |
| 60 | if (!$value) { |
| 61 | $value = $this->getFeeds(); |
| 62 | |
| 63 | if ($value) { |
| 64 | Civi::cache('community_messages')->set('dashboard_newsfeed', $value, (60 * 60 * 24 * self::CACHE_DAYS)); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return $value; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Fetch all feeds. |
| 73 | * |
| 74 | * @return array |
| 75 | */ |
| 76 | protected function getFeeds() { |
| 77 | $newsFeed = $this->getFeed($this->getNewsUrl()); |
| 78 | // If unable to fetch the feed, return empty results. |
| 79 | if (!$newsFeed) { |
| 80 | return []; |
| 81 | } |
| 82 | $feeds = $this->formatItems($newsFeed); |
| 83 | return $feeds; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Parse a single rss feed. |
| 88 | * |
| 89 | * @param $url |
| 90 | * |
| 91 | * @return array|NULL |
| 92 | * array of blog items; or NULL if not available |
| 93 | */ |
| 94 | protected function getFeed($url) { |
| 95 | $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT); |
| 96 | list ($status, $rawFeed) = $httpClient->get($url); |
| 97 | if ($status !== CRM_Utils_HttpClient::STATUS_OK) { |
| 98 | return NULL; |
| 99 | } |
| 100 | return @simplexml_load_string($rawFeed); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param string $feed |
| 105 | * @return array |
| 106 | */ |
| 107 | protected function formatItems($feed) { |
| 108 | $result = []; |
| 109 | if ($feed && !empty($feed->channel)) { |
| 110 | foreach ($feed->channel as $channel) { |
| 111 | $content = [ |
| 112 | 'title' => (string) $channel->title, |
| 113 | 'description' => (string) $channel->description, |
| 114 | 'name' => strtolower(CRM_Utils_String::munge($channel->title, '-')), |
| 115 | 'items' => [], |
| 116 | ]; |
| 117 | foreach ($channel->item as $item) { |
| 118 | $item = (array) $item; |
| 119 | $item['title'] = strip_tags($item['title']); |
| 120 | // Clean up description - remove tags & styles that would break dashboard layout |
| 121 | $description = preg_replace('#<h[1-3][^>]*>(.+?)</h[1-3][^>]*>#s', '<h4>$1</h4>', $item['description']); |
| 122 | $description = strip_tags($description, "<a><p><h4><h5><h6><b><i><em><strong><ol><ul><li><dd><dt><code><pre><br><hr>"); |
| 123 | $description = preg_replace('/(<[^>]+) style=["\'].*?["\']/i', '$1', $description); |
| 124 | // Add paragraph markup if it's missing. |
| 125 | if (strpos($description, '<p') === FALSE) { |
| 126 | $description = '<p>' . $description . '</p>'; |
| 127 | } |
| 128 | $item['description'] = $description; |
| 129 | $content['items'][] = $item; |
| 130 | } |
| 131 | if ($content['items']) { |
| 132 | $result[] = $content; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | return $result; |
| 137 | } |
| 138 | |
| 139 | } |