3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Manage the download, validation, and rendering of community messages
31 class CRM_Core_CommunityMessages
{
33 const DEFAULT_MESSAGES_URL
= 'https://alert.civicrm.org/alert?prot=1&ver={ver}&uf={uf}&sid={sid}&lang={lang}&co={co}';
34 const DEFAULT_PERMISSION
= 'administer CiviCRM';
37 * Default time to wait before retrying.
39 const DEFAULT_RETRY
= 7200; // 2 hours
42 * @var CRM_Utils_HttpClient
47 * @var CRM_Utils_Cache_Interface
54 protected $messagesUrl;
57 * Create default instance.
59 * @return CRM_Core_CommunityMessages
61 public static function create() {
62 return new CRM_Core_CommunityMessages(
63 Civi
::cache('community_messages'),
64 CRM_Utils_HttpClient
::singleton()
69 * @param CRM_Utils_Cache_Interface $cache
70 * @param CRM_Utils_HttpClient $client
71 * @param null $messagesUrl
73 public function __construct($cache, $client, $messagesUrl = NULL) {
74 $this->cache
= $cache;
75 $this->client
= $client;
76 if ($messagesUrl === NULL) {
77 $this->messagesUrl
= Civi
::settings()->get('communityMessagesUrl');
80 $this->messagesUrl
= $messagesUrl;
82 if ($this->messagesUrl
=== '*default*') {
83 $this->messagesUrl
= self
::DEFAULT_MESSAGES_URL
;
88 * Get the messages document (either from the cache or by downloading)
92 public function getDocument() {
94 $document = $this->cache
->get('communityMessages');
96 if (empty($document) ||
!is_array($document)) {
98 'messages' => array(),
99 'expires' => 0, // ASAP
100 'ttl' => self
::DEFAULT_RETRY
,
101 'retry' => self
::DEFAULT_RETRY
,
106 if ($document['expires'] <= CRM_Utils_Time
::getTimeRaw()) {
107 $newDocument = $this->fetchDocument();
108 if ($newDocument && $this->validateDocument($newDocument)) {
109 $document = $newDocument;
110 $document['expires'] = CRM_Utils_Time
::getTimeRaw() +
$document['ttl'];
113 // keep the old messages for now, try again later
114 $document['expires'] = CRM_Utils_Time
::getTimeRaw() +
$document['retry'];
120 $this->cache
->set('communityMessages', $document);
127 * Download document from URL and parse as JSON.
132 public function fetchDocument() {
133 list($status, $json) = $this->client
->get($this->getRenderedUrl());
134 if ($status != CRM_Utils_HttpClient
::STATUS_OK ||
empty($json)) {
137 $doc = json_decode($json, TRUE);
138 if (empty($doc) ||
json_last_error() != JSON_ERROR_NONE
) {
145 * Get the final, usable URL string (after interpolating any variables)
147 * @return FALSE|string
149 public function getRenderedUrl() {
150 return CRM_Utils_System
::evalUrl($this->messagesUrl
);
156 public function isEnabled() {
157 return $this->messagesUrl
!== FALSE && $this->messagesUrl
!== 'FALSE';
161 * Pick a message to display.
165 public function pick() {
166 $document = $this->getDocument();
168 foreach ($document['messages'] as $message) {
169 if (!isset($message['perms'])) {
170 $message['perms'] = array(self
::DEFAULT_PERMISSION
);
172 if (!CRM_Core_Permission
::checkAnyPerm($message['perms'])) {
176 if (isset($message['components'])) {
177 $enabled = array_keys(CRM_Core_Component
::getEnabledComponents());
178 if (count(array_intersect($enabled, $message['components'])) == 0) {
183 $messages[] = $message;
185 if (empty($messages)) {
189 $idx = rand(0, count($messages) - 1);
190 return $messages[$idx];
194 * @param string $markup
197 public static function evalMarkup($markup) {
198 $config = CRM_Core_Config
::singleton();
200 'resourceUrl' => rtrim($config->resourceBase
, '/'),
201 'ver' => CRM_Utils_System
::version(),
202 'uf' => $config->userFramework
,
203 'php' => phpversion(),
204 'sid' => CRM_Utils_System
::getSiteID(),
205 'baseUrl' => $config->userFrameworkBaseURL
,
206 'lang' => $config->lcMessages
,
207 'co' => $config->defaultContactCountry
,
210 foreach ($vals as $k => $v) {
211 $vars['%%' . $k . '%%'] = $v;
212 $vars['{{' . $k . '}}'] = urlencode($v);
214 return strtr($markup, $vars);
218 * Ensure that a document is well-formed
220 * @param array $document
223 public function validateDocument($document) {
224 if (!isset($document['ttl']) ||
!is_int($document['ttl'])) {
227 if (!isset($document['retry']) ||
!is_int($document['retry'])) {
230 if (!isset($document['messages']) ||
!is_array($document['messages'])) {
233 foreach ($document['messages'] as $message) {
234 // TODO validate $message['markup']