3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Main page for blog dashlet
21 class CRM_Dashlet_Page_Blog
extends CRM_Core_Page
{
23 const CHECK_TIMEOUT
= 5;
25 const NEWS_URL
= 'https://civicrm.org/news-feed.rss';
28 * Gets url for blog feed.
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
;
39 return CRM_Utils_System
::evalUrl($url);
43 * Output data to template.
45 public function run() {
46 $this->assign('feeds', $this->getData());
51 * Load feeds from cache.
53 * Refresh cache if expired.
57 protected function getData() {
58 $value = Civi
::cache('community_messages')->get('dashboard_newsfeed');
61 $value = $this->getFeeds();
64 Civi
::cache('community_messages')->set('dashboard_newsfeed', $value, (60 * 60 * 24 * self
::CACHE_DAYS
));
76 protected function getFeeds() {
77 $newsFeed = $this->getFeed($this->getNewsUrl());
78 // If unable to fetch the feed, return empty results.
82 $feeds = $this->formatItems($newsFeed);
87 * Parse a single rss feed.
92 * array of blog items; or NULL if not available
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
) {
100 return @simplexml_load_string
($rawFeed);
104 * @param string $feed
107 protected function formatItems($feed) {
109 if ($feed && !empty($feed->channel
)) {
110 foreach ($feed->channel
as $channel) {
112 'title' => (string) $channel->title
,
113 'description' => (string) $channel->description
,
114 'name' => strtolower(CRM_Utils_String
::munge($channel->title
, '-')),
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>';
128 $item['description'] = $description;
129 $content['items'][] = $item;
131 if ($content['items']) {
132 $result[] = $content;