Merge pull request #11679 from jitendrapurohit/CRM-21776
[civicrm-core.git] / CRM / Dashlet / Page / Blog.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 * $Id$
33 *
34 */
35
36 /**
37 * Main page for blog dashlet
38 */
39 class CRM_Dashlet_Page_Blog extends CRM_Core_Page {
40
41 const CHECK_TIMEOUT = 5;
42 const CACHE_DAYS = 1;
43 const NEWS_URL = 'https://civicrm.org/news-feed.rss';
44
45 /**
46 * Gets url for blog feed.
47 *
48 * @return string
49 */
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}.
53 $url = Civi::settings()->get('blogUrl');
54 if ($url === '*default*') {
55 $url = self::NEWS_URL;
56 }
57 return CRM_Utils_System::evalUrl($url);
58 }
59
60 /**
61 * Output data to template.
62 */
63 public function run() {
64 $this->assign('feeds', $this->getData());
65 return parent::run();
66 }
67
68 /**
69 * Load feeds from cache.
70 *
71 * Refresh cache if expired.
72 *
73 * @return array
74 */
75 protected function getData() {
76 // Fetch data from cache
77 $cache = CRM_Core_DAO::executeQuery("SELECT data, created_date FROM civicrm_cache
78 WHERE group_name = 'dashboard' AND path = 'newsfeed'");
79 if ($cache->fetch()) {
80 $expire = time() - (60 * 60 * 24 * self::CACHE_DAYS);
81 // Refresh data after CACHE_DAYS
82 if (strtotime($cache->created_date) < $expire) {
83 $new_data = $this->getFeeds();
84 // If fetching the new rss feed was successful, return it
85 // Otherwise use the old cached data - it's better than nothing
86 if ($new_data) {
87 return $new_data;
88 }
89 }
90 return unserialize($cache->data);
91 }
92 return $this->getFeeds();
93 }
94
95 /**
96 * Fetch all feeds & cache results.
97 *
98 * @return array
99 */
100 protected function getFeeds() {
101 $newsFeed = $this->getFeed($this->getNewsUrl());
102 // If unable to fetch the feed, return empty results.
103 if (!$newsFeed) {
104 return array();
105 }
106 $feeds = $this->formatItems($newsFeed);
107 CRM_Core_BAO_Cache::setItem($feeds, 'dashboard', 'newsfeed');
108 return $feeds;
109 }
110
111 /**
112 * Parse a single rss feed.
113 *
114 * @param $url
115 *
116 * @return array|NULL
117 * array of blog items; or NULL if not available
118 */
119 protected function getFeed($url) {
120 $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT);
121 list ($status, $rawFeed) = $httpClient->get($url);
122 if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
123 return NULL;
124 }
125 return @simplexml_load_string($rawFeed);
126 }
127
128 /**
129 * @param string $feed
130 * @return array
131 */
132 protected function formatItems($feed) {
133 $result = array();
134 if ($feed && !empty($feed->channel)) {
135 foreach ($feed->channel as $channel) {
136 $content = array(
137 'title' => (string) $channel->title,
138 'description' => (string) $channel->description,
139 'name' => strtolower(CRM_Utils_String::munge($channel->title, '-')),
140 'items' => array(),
141 );
142 foreach ($channel->item as $item) {
143 $item = (array) $item;
144 $item['title'] = strip_tags($item['title']);
145 // Clean up description - remove tags & styles that would break dashboard layout
146 $description = preg_replace('#<h[1-3][^>]*>(.+?)</h[1-3][^>]*>#s', '<h4>$1</h4>', $item['description']);
147 $description = strip_tags($description, "<a><p><h4><h5><h6><b><i><em><strong><ol><ul><li><dd><dt><code><pre><br><hr>");
148 $description = preg_replace('/(<[^>]+) style=["\'].*?["\']/i', '$1', $description);
149 // Add paragraph markup if it's missing.
150 if (strpos($description, '<p') === FALSE) {
151 $description = '<p>' . $description . '</p>';
152 }
153 $item['description'] = $description;
154 $content['items'][] = $item;
155 }
156 if ($content['items']) {
157 $result[] = $content;
158 }
159 }
160 }
161 return $result;
162 }
163
164 }