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 +--------------------------------------------------------------------+
13 * Manage the download, validation, and rendering of community messages
15 class CRM_Core_CommunityMessages
{
17 const DEFAULT_MESSAGES_URL
= 'https://alert.civicrm.org/alert?prot=1&ver={ver}&uf={uf}&sid={sid}&lang={lang}&co={co}';
18 const DEFAULT_PERMISSION
= 'administer CiviCRM';
21 * Default time to wait before retrying.
24 const DEFAULT_RETRY
= 7200;
27 * @var CRM_Utils_HttpClient
32 * @var CRM_Utils_Cache_Interface
37 * Url to retrieve community messages from.
39 * False means a retrieval will not be attempted.
43 protected $messagesUrl;
46 * Create default instance.
48 * @return CRM_Core_CommunityMessages
50 public static function create() {
51 return new CRM_Core_CommunityMessages(
52 Civi
::cache('community_messages'),
53 CRM_Utils_HttpClient
::singleton()
60 * @param CRM_Utils_Cache_Interface $cache
61 * @param CRM_Utils_HttpClient $client
62 * @param string|false $messagesUrl
64 public function __construct($cache, $client, $messagesUrl = NULL) {
65 $this->cache
= $cache;
66 $this->client
= $client;
67 if ($messagesUrl === NULL) {
68 $this->messagesUrl
= Civi
::settings()->get('communityMessagesUrl');
71 $this->messagesUrl
= $messagesUrl;
73 if ($this->messagesUrl
=== '*default*') {
74 $this->messagesUrl
= self
::DEFAULT_MESSAGES_URL
;
79 * Get the messages document (either from the cache or by downloading).
83 public function getDocument() {
85 $document = $this->cache
->get('communityMessages');
87 if (empty($document) ||
!is_array($document)) {
92 'ttl' => self
::DEFAULT_RETRY
,
93 'retry' => self
::DEFAULT_RETRY
,
98 if ($document['expires'] <= CRM_Utils_Time
::getTimeRaw()) {
99 $newDocument = $this->fetchDocument();
100 if ($newDocument && $this->validateDocument($newDocument)) {
101 $document = $newDocument;
102 $document['expires'] = CRM_Utils_Time
::getTimeRaw() +
$document['ttl'];
105 // keep the old messages for now, try again later
106 $document['expires'] = CRM_Utils_Time
::getTimeRaw() +
$document['retry'];
112 $this->cache
->set('communityMessages', $document);
119 * Download document from URL and parse as JSON.
124 public function fetchDocument() {
125 list($status, $json) = $this->client
->get($this->getRenderedUrl());
126 if ($status != CRM_Utils_HttpClient
::STATUS_OK ||
empty($json)) {
129 $doc = json_decode($json, TRUE);
130 if (empty($doc) ||
json_last_error() != JSON_ERROR_NONE
) {
137 * Get the final, usable URL string (after interpolating any variables)
139 * @return FALSE|string
141 public function getRenderedUrl() {
142 return CRM_Utils_System
::evalUrl($this->messagesUrl
);
148 public function isEnabled() {
149 return $this->messagesUrl
!== FALSE && $this->messagesUrl
!== 'FALSE';
153 * Pick a message to display.
157 public function pick() {
158 $document = $this->getDocument();
160 foreach ($document['messages'] as $message) {
161 if (!isset($message['perms'])) {
162 $message['perms'] = [self
::DEFAULT_PERMISSION
];
164 if (!CRM_Core_Permission
::checkAnyPerm($message['perms'])) {
168 if (isset($message['components'])) {
169 $enabled = array_keys(CRM_Core_Component
::getEnabledComponents());
170 if (count(array_intersect($enabled, $message['components'])) == 0) {
175 $messages[] = $message;
177 if (empty($messages)) {
181 $idx = rand(0, count($messages) - 1);
182 return $messages[$idx];
186 * @param string $markup
189 public static function evalMarkup($markup) {
190 $config = CRM_Core_Config
::singleton();
192 'resourceUrl' => rtrim($config->resourceBase
, '/'),
193 'ver' => CRM_Utils_System
::version(),
194 'uf' => $config->userFramework
,
195 'php' => phpversion(),
196 'sid' => CRM_Utils_System
::getSiteID(),
197 'baseUrl' => $config->userFrameworkBaseURL
,
198 'lang' => $config->lcMessages
,
199 'co' => $config->defaultContactCountry
,
202 foreach ($vals as $k => $v) {
203 $vars['%%' . $k . '%%'] = $v;
204 $vars['{{' . $k . '}}'] = urlencode($v);
206 return strtr($markup, $vars);
210 * Ensure that a document is well-formed
212 * @param array $document
215 public function validateDocument($document) {
216 if (!isset($document['ttl']) ||
!is_int($document['ttl'])) {
219 if (!isset($document['retry']) ||
!is_int($document['retry'])) {
222 if (!isset($document['messages']) ||
!is_array($document['messages'])) {
225 foreach ($document['messages'] as $message) {
226 // TODO validate $message['markup']