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 $refTime = CRM_Utils_Time
::getTimeRaw();
99 if ($document['expires'] <= $refTime) {
100 $newDocument = $this->fetchDocument();
101 if ($newDocument && $this->validateDocument($newDocument)) {
102 $document = $newDocument;
103 $document['expires'] = $refTime +
$document['ttl'];
106 // keep the old messages for now, try again later
107 $document['expires'] = $refTime +
$document['retry'];
113 $this->cache
->set('communityMessages', $document);
120 * Download document from URL and parse as JSON.
125 public function fetchDocument() {
126 list($status, $json) = $this->client
->get($this->getRenderedUrl());
127 if ($status != CRM_Utils_HttpClient
::STATUS_OK ||
empty($json)) {
130 $doc = json_decode($json, TRUE);
131 if (empty($doc) ||
json_last_error() != JSON_ERROR_NONE
) {
138 * Get the final, usable URL string (after interpolating any variables)
140 * @return FALSE|string
142 public function getRenderedUrl() {
143 return CRM_Utils_System
::evalUrl($this->messagesUrl
);
149 public function isEnabled() {
150 return $this->messagesUrl
!== FALSE && $this->messagesUrl
!== 'FALSE';
154 * Pick a message to display.
158 public function pick() {
159 $document = $this->getDocument();
161 foreach ($document['messages'] as $message) {
162 if (!isset($message['perms'])) {
163 $message['perms'] = [self
::DEFAULT_PERMISSION
];
165 if (!CRM_Core_Permission
::checkAnyPerm($message['perms'])) {
169 if (isset($message['components'])) {
170 $enabled = array_keys(CRM_Core_Component
::getEnabledComponents());
171 if (count(array_intersect($enabled, $message['components'])) == 0) {
176 $messages[] = $message;
178 if (empty($messages)) {
182 $idx = rand(0, count($messages) - 1);
183 return $messages[$idx];
187 * @param string $markup
190 public static function evalMarkup($markup) {
191 $config = CRM_Core_Config
::singleton();
193 'resourceUrl' => rtrim($config->resourceBase
, '/'),
194 'ver' => CRM_Utils_System
::version(),
195 'uf' => $config->userFramework
,
196 'php' => phpversion(),
197 'sid' => CRM_Utils_System
::getSiteID(),
198 'baseUrl' => $config->userFrameworkBaseURL
,
199 'lang' => $config->lcMessages
,
200 'co' => $config->defaultContactCountry
,
203 foreach ($vals as $k => $v) {
204 $vars['%%' . $k . '%%'] = $v;
205 $vars['{{' . $k . '}}'] = urlencode($v);
207 return strtr($markup, $vars);
211 * Ensure that a document is well-formed
213 * @param array $document
216 public function validateDocument($document) {
217 if (!isset($document['ttl']) ||
!is_int($document['ttl'])) {
220 if (!isset($document['retry']) ||
!is_int($document['retry'])) {
223 if (!isset($document['messages']) ||
!is_array($document['messages'])) {
226 foreach ($document['messages'] as $message) {
227 // TODO validate $message['markup']