3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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.
40 const DEFAULT_RETRY
= 7200;
43 * @var CRM_Utils_HttpClient
48 * @var CRM_Utils_Cache_Interface
55 protected $messagesUrl;
58 * Create default instance.
60 * @return CRM_Core_CommunityMessages
62 public static function create() {
63 return new CRM_Core_CommunityMessages(
64 Civi
::cache('community_messages'),
65 CRM_Utils_HttpClient
::singleton()
70 * @param CRM_Utils_Cache_Interface $cache
71 * @param CRM_Utils_HttpClient $client
72 * @param null $messagesUrl
74 public function __construct($cache, $client, $messagesUrl = NULL) {
75 $this->cache
= $cache;
76 $this->client
= $client;
77 if ($messagesUrl === NULL) {
78 $this->messagesUrl
= Civi
::settings()->get('communityMessagesUrl');
81 $this->messagesUrl
= $messagesUrl;
83 if ($this->messagesUrl
=== '*default*') {
84 $this->messagesUrl
= self
::DEFAULT_MESSAGES_URL
;
89 * Get the messages document (either from the cache or by downloading)
93 public function getDocument() {
95 $document = $this->cache
->get('communityMessages');
97 if (empty($document) ||
!is_array($document)) {
102 'ttl' => self
::DEFAULT_RETRY
,
103 'retry' => self
::DEFAULT_RETRY
,
108 if ($document['expires'] <= CRM_Utils_Time
::getTimeRaw()) {
109 $newDocument = $this->fetchDocument();
110 if ($newDocument && $this->validateDocument($newDocument)) {
111 $document = $newDocument;
112 $document['expires'] = CRM_Utils_Time
::getTimeRaw() +
$document['ttl'];
115 // keep the old messages for now, try again later
116 $document['expires'] = CRM_Utils_Time
::getTimeRaw() +
$document['retry'];
122 $this->cache
->set('communityMessages', $document);
129 * Download document from URL and parse as JSON.
134 public function fetchDocument() {
135 list($status, $json) = $this->client
->get($this->getRenderedUrl());
136 if ($status != CRM_Utils_HttpClient
::STATUS_OK ||
empty($json)) {
139 $doc = json_decode($json, TRUE);
140 if (empty($doc) ||
json_last_error() != JSON_ERROR_NONE
) {
147 * Get the final, usable URL string (after interpolating any variables)
149 * @return FALSE|string
151 public function getRenderedUrl() {
152 return CRM_Utils_System
::evalUrl($this->messagesUrl
);
158 public function isEnabled() {
159 return $this->messagesUrl
!== FALSE && $this->messagesUrl
!== 'FALSE';
163 * Pick a message to display.
167 public function pick() {
168 $document = $this->getDocument();
170 foreach ($document['messages'] as $message) {
171 if (!isset($message['perms'])) {
172 $message['perms'] = [self
::DEFAULT_PERMISSION
];
174 if (!CRM_Core_Permission
::checkAnyPerm($message['perms'])) {
178 if (isset($message['components'])) {
179 $enabled = array_keys(CRM_Core_Component
::getEnabledComponents());
180 if (count(array_intersect($enabled, $message['components'])) == 0) {
185 $messages[] = $message;
187 if (empty($messages)) {
191 $idx = rand(0, count($messages) - 1);
192 return $messages[$idx];
196 * @param string $markup
199 public static function evalMarkup($markup) {
200 $config = CRM_Core_Config
::singleton();
202 'resourceUrl' => rtrim($config->resourceBase
, '/'),
203 'ver' => CRM_Utils_System
::version(),
204 'uf' => $config->userFramework
,
205 'php' => phpversion(),
206 'sid' => CRM_Utils_System
::getSiteID(),
207 'baseUrl' => $config->userFrameworkBaseURL
,
208 'lang' => $config->lcMessages
,
209 'co' => $config->defaultContactCountry
,
212 foreach ($vals as $k => $v) {
213 $vars['%%' . $k . '%%'] = $v;
214 $vars['{{' . $k . '}}'] = urlencode($v);
216 return strtr($markup, $vars);
220 * Ensure that a document is well-formed
222 * @param array $document
225 public function validateDocument($document) {
226 if (!isset($document['ttl']) ||
!is_int($document['ttl'])) {
229 if (!isset($document['retry']) ||
!is_int($document['retry'])) {
232 if (!isset($document['messages']) ||
!is_array($document['messages'])) {
235 foreach ($document['messages'] as $message) {
236 // TODO validate $message['markup']