Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Main page for blog dashlet | |
38 | */ | |
39 | class CRM_Dashlet_Page_Blog extends CRM_Core_Page { | |
40 | ||
14026aa8 TO |
41 | const CHECK_TIMEOUT = 5; |
42 | const CACHE_DAYS = 1; | |
618febd8 | 43 | const NEWS_URL = 'https://civicrm.org/news-feed.rss'; |
6a488035 | 44 | |
4b030206 | 45 | /** |
b6f31f2a | 46 | * Gets url for blog feed. |
4b030206 | 47 | * |
b6f31f2a | 48 | * @return string |
4b030206 | 49 | */ |
618febd8 CW |
50 | public function getNewsUrl() { |
51 | // Note: We use "*default*" as the default (rather than self::NEWS_URL) so that future | |
52 | // developers can change NEWS_URL without needing to update {civicrm_setting}. | |
aaffa79f | 53 | $url = Civi::settings()->get('blogUrl'); |
4b030206 | 54 | if ($url === '*default*') { |
618febd8 | 55 | $url = self::NEWS_URL; |
4b030206 TO |
56 | } |
57 | return CRM_Utils_System::evalUrl($url); | |
58 | } | |
59 | ||
b6f31f2a CW |
60 | /** |
61 | * Output data to template. | |
62 | */ | |
63 | public function run() { | |
b6f31f2a | 64 | $this->assign('feeds', $this->getData()); |
6a488035 TO |
65 | return parent::run(); |
66 | } | |
67 | ||
68 | /** | |
b6f31f2a CW |
69 | * Load feeds from cache. |
70 | * | |
71 | * Refresh cache if expired. | |
6a488035 TO |
72 | * |
73 | * @return array | |
6a488035 | 74 | */ |
b6f31f2a | 75 | protected function getData() { |
a03490f5 ML |
76 | $value = Civi::cache('community_messages')->get('dashboard_newsfeed'); |
77 | ||
78 | if (!$value) { | |
79 | $value = $this->getFeeds(); | |
80 | ||
81 | if ($value) { | |
82 | Civi::cache('community_messages')->set('dashboard_newsfeed', $value, (60 * 60 * 24 * self::CACHE_DAYS)); | |
6a488035 | 83 | } |
6a488035 | 84 | } |
a03490f5 ML |
85 | |
86 | return $value; | |
6a488035 TO |
87 | } |
88 | ||
89 | /** | |
a03490f5 | 90 | * Fetch all feeds. |
b6f31f2a CW |
91 | * |
92 | * @return array | |
93 | */ | |
94 | protected function getFeeds() { | |
618febd8 CW |
95 | $newsFeed = $this->getFeed($this->getNewsUrl()); |
96 | // If unable to fetch the feed, return empty results. | |
97 | if (!$newsFeed) { | |
98 | return array(); | |
b6f31f2a | 99 | } |
618febd8 | 100 | $feeds = $this->formatItems($newsFeed); |
b6f31f2a CW |
101 | return $feeds; |
102 | } | |
103 | ||
104 | /** | |
105 | * Parse a single rss feed. | |
6a488035 | 106 | * |
77b97be7 EM |
107 | * @param $url |
108 | * | |
72b3a70c CW |
109 | * @return array|NULL |
110 | * array of blog items; or NULL if not available | |
6a488035 | 111 | */ |
b6f31f2a | 112 | protected function getFeed($url) { |
14026aa8 | 113 | $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT); |
8c2d418b | 114 | list ($status, $rawFeed) = $httpClient->get($url); |
14026aa8 TO |
115 | if ($status !== CRM_Utils_HttpClient::STATUS_OK) { |
116 | return NULL; | |
117 | } | |
b6f31f2a CW |
118 | return @simplexml_load_string($rawFeed); |
119 | } | |
14026aa8 | 120 | |
b6f31f2a CW |
121 | /** |
122 | * @param string $feed | |
123 | * @return array | |
124 | */ | |
125 | protected function formatItems($feed) { | |
618febd8 CW |
126 | $result = array(); |
127 | if ($feed && !empty($feed->channel)) { | |
128 | foreach ($feed->channel as $channel) { | |
129 | $content = array( | |
130 | 'title' => (string) $channel->title, | |
131 | 'description' => (string) $channel->description, | |
132 | 'name' => strtolower(CRM_Utils_String::munge($channel->title, '-')), | |
133 | 'items' => array(), | |
134 | ); | |
135 | foreach ($channel->item as $item) { | |
136 | $item = (array) $item; | |
137 | $item['title'] = strip_tags($item['title']); | |
e7df6b4f | 138 | // Clean up description - remove tags & styles that would break dashboard layout |
618febd8 | 139 | $description = preg_replace('#<h[1-3][^>]*>(.+?)</h[1-3][^>]*>#s', '<h4>$1</h4>', $item['description']); |
e7df6b4f CW |
140 | $description = strip_tags($description, "<a><p><h4><h5><h6><b><i><em><strong><ol><ul><li><dd><dt><code><pre><br><hr>"); |
141 | $description = preg_replace('/(<[^>]+) style=["\'].*?["\']/i', '$1', $description); | |
618febd8 CW |
142 | // Add paragraph markup if it's missing. |
143 | if (strpos($description, '<p') === FALSE) { | |
144 | $description = '<p>' . $description . '</p>'; | |
145 | } | |
146 | $item['description'] = $description; | |
147 | $content['items'][] = $item; | |
148 | } | |
149 | if ($content['items']) { | |
150 | $result[] = $content; | |
d383174a | 151 | } |
6a488035 TO |
152 | } |
153 | } | |
618febd8 | 154 | return $result; |
6a488035 | 155 | } |
96025800 | 156 | |
6a488035 | 157 | } |