Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * Main page for blog dashlet | |
20 | */ | |
21 | class CRM_Dashlet_Page_Blog extends CRM_Core_Page { | |
22 | ||
14026aa8 TO |
23 | const CHECK_TIMEOUT = 5; |
24 | const CACHE_DAYS = 1; | |
618febd8 | 25 | const NEWS_URL = 'https://civicrm.org/news-feed.rss'; |
6a488035 | 26 | |
4b030206 | 27 | /** |
b6f31f2a | 28 | * Gets url for blog feed. |
4b030206 | 29 | * |
b6f31f2a | 30 | * @return string |
4b030206 | 31 | */ |
618febd8 CW |
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}. | |
aaffa79f | 35 | $url = Civi::settings()->get('blogUrl'); |
4b030206 | 36 | if ($url === '*default*') { |
618febd8 | 37 | $url = self::NEWS_URL; |
4b030206 TO |
38 | } |
39 | return CRM_Utils_System::evalUrl($url); | |
40 | } | |
41 | ||
b6f31f2a CW |
42 | /** |
43 | * Output data to template. | |
44 | */ | |
45 | public function run() { | |
b6f31f2a | 46 | $this->assign('feeds', $this->getData()); |
6a488035 TO |
47 | return parent::run(); |
48 | } | |
49 | ||
50 | /** | |
b6f31f2a CW |
51 | * Load feeds from cache. |
52 | * | |
53 | * Refresh cache if expired. | |
6a488035 TO |
54 | * |
55 | * @return array | |
6a488035 | 56 | */ |
b6f31f2a | 57 | protected function getData() { |
a03490f5 ML |
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)); | |
6a488035 | 65 | } |
6a488035 | 66 | } |
a03490f5 ML |
67 | |
68 | return $value; | |
6a488035 TO |
69 | } |
70 | ||
71 | /** | |
a03490f5 | 72 | * Fetch all feeds. |
b6f31f2a CW |
73 | * |
74 | * @return array | |
75 | */ | |
76 | protected function getFeeds() { | |
618febd8 CW |
77 | $newsFeed = $this->getFeed($this->getNewsUrl()); |
78 | // If unable to fetch the feed, return empty results. | |
79 | if (!$newsFeed) { | |
be2fb01f | 80 | return []; |
b6f31f2a | 81 | } |
618febd8 | 82 | $feeds = $this->formatItems($newsFeed); |
b6f31f2a CW |
83 | return $feeds; |
84 | } | |
85 | ||
86 | /** | |
87 | * Parse a single rss feed. | |
6a488035 | 88 | * |
77b97be7 EM |
89 | * @param $url |
90 | * | |
72b3a70c CW |
91 | * @return array|NULL |
92 | * array of blog items; or NULL if not available | |
6a488035 | 93 | */ |
b6f31f2a | 94 | protected function getFeed($url) { |
14026aa8 | 95 | $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT); |
8c2d418b | 96 | list ($status, $rawFeed) = $httpClient->get($url); |
14026aa8 TO |
97 | if ($status !== CRM_Utils_HttpClient::STATUS_OK) { |
98 | return NULL; | |
99 | } | |
b6f31f2a CW |
100 | return @simplexml_load_string($rawFeed); |
101 | } | |
14026aa8 | 102 | |
b6f31f2a CW |
103 | /** |
104 | * @param string $feed | |
105 | * @return array | |
106 | */ | |
107 | protected function formatItems($feed) { | |
be2fb01f | 108 | $result = []; |
618febd8 CW |
109 | if ($feed && !empty($feed->channel)) { |
110 | foreach ($feed->channel as $channel) { | |
be2fb01f | 111 | $content = [ |
618febd8 CW |
112 | 'title' => (string) $channel->title, |
113 | 'description' => (string) $channel->description, | |
114 | 'name' => strtolower(CRM_Utils_String::munge($channel->title, '-')), | |
be2fb01f CW |
115 | 'items' => [], |
116 | ]; | |
618febd8 CW |
117 | foreach ($channel->item as $item) { |
118 | $item = (array) $item; | |
119 | $item['title'] = strip_tags($item['title']); | |
e7df6b4f | 120 | // Clean up description - remove tags & styles that would break dashboard layout |
618febd8 | 121 | $description = preg_replace('#<h[1-3][^>]*>(.+?)</h[1-3][^>]*>#s', '<h4>$1</h4>', $item['description']); |
e7df6b4f CW |
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); | |
618febd8 CW |
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; | |
d383174a | 133 | } |
6a488035 TO |
134 | } |
135 | } | |
618febd8 | 136 | return $result; |
6a488035 | 137 | } |
96025800 | 138 | |
6a488035 | 139 | } |