Commit | Line | Data |
---|---|---|
ecbe1139 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
ecbe1139 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
ecbe1139 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
ecbe1139 TO |
27 | |
28 | /** | |
29 | * Manage the download, validation, and rendering of community messages | |
30 | */ | |
31 | class CRM_Core_CommunityMessages { | |
32 | ||
20eff8a7 | 33 | const DEFAULT_MESSAGES_URL = 'https://alert.civicrm.org/alert?prot=1&ver={ver}&uf={uf}&sid={sid}&lang={lang}&co={co}'; |
dc92f2f8 | 34 | const DEFAULT_PERMISSION = 'administer CiviCRM'; |
e8977170 | 35 | |
ecbe1139 | 36 | /** |
0880a9d0 | 37 | * Default time to wait before retrying. |
ecbe1139 | 38 | */ |
518fa0ee SL |
39 | // 2 hours |
40 | const DEFAULT_RETRY = 7200; | |
ecbe1139 TO |
41 | |
42 | /** | |
43 | * @var CRM_Utils_HttpClient | |
44 | */ | |
45 | protected $client; | |
46 | ||
47 | /** | |
48 | * @var CRM_Utils_Cache_Interface | |
49 | */ | |
50 | protected $cache; | |
51 | ||
e8977170 TO |
52 | /** |
53 | * @var FALSE|string | |
54 | */ | |
55 | protected $messagesUrl; | |
56 | ||
847c93ac | 57 | /** |
0880a9d0 | 58 | * Create default instance. |
847c93ac TO |
59 | * |
60 | * @return CRM_Core_CommunityMessages | |
61 | */ | |
62 | public static function create() { | |
63 | return new CRM_Core_CommunityMessages( | |
a4704404 | 64 | Civi::cache('community_messages'), |
847c93ac TO |
65 | CRM_Utils_HttpClient::singleton() |
66 | ); | |
67 | } | |
68 | ||
ecbe1139 TO |
69 | /** |
70 | * @param CRM_Utils_Cache_Interface $cache | |
71 | * @param CRM_Utils_HttpClient $client | |
77b97be7 | 72 | * @param null $messagesUrl |
ecbe1139 | 73 | */ |
e8977170 | 74 | public function __construct($cache, $client, $messagesUrl = NULL) { |
ecbe1139 TO |
75 | $this->cache = $cache; |
76 | $this->client = $client; | |
e8977170 | 77 | if ($messagesUrl === NULL) { |
d356cdeb | 78 | $this->messagesUrl = Civi::settings()->get('communityMessagesUrl'); |
e8977170 TO |
79 | } |
80 | else { | |
81 | $this->messagesUrl = $messagesUrl; | |
82 | } | |
847c93ac TO |
83 | if ($this->messagesUrl === '*default*') { |
84 | $this->messagesUrl = self::DEFAULT_MESSAGES_URL; | |
85 | } | |
ecbe1139 TO |
86 | } |
87 | ||
88 | /** | |
d1b65097 | 89 | * Get the messages document (either from the cache or by downloading) |
ecbe1139 TO |
90 | * |
91 | * @return NULL|array | |
92 | */ | |
93 | public function getDocument() { | |
ecbe1139 TO |
94 | $isChanged = FALSE; |
95 | $document = $this->cache->get('communityMessages'); | |
96 | ||
97 | if (empty($document) || !is_array($document)) { | |
be2fb01f CW |
98 | $document = [ |
99 | 'messages' => [], | |
518fa0ee SL |
100 | // ASAP |
101 | 'expires' => 0, | |
ecbe1139 TO |
102 | 'ttl' => self::DEFAULT_RETRY, |
103 | 'retry' => self::DEFAULT_RETRY, | |
be2fb01f | 104 | ]; |
ecbe1139 TO |
105 | $isChanged = TRUE; |
106 | } | |
107 | ||
108 | if ($document['expires'] <= CRM_Utils_Time::getTimeRaw()) { | |
847c93ac | 109 | $newDocument = $this->fetchDocument(); |
8bfc3cb1 | 110 | if ($newDocument && $this->validateDocument($newDocument)) { |
ecbe1139 TO |
111 | $document = $newDocument; |
112 | $document['expires'] = CRM_Utils_Time::getTimeRaw() + $document['ttl']; | |
e8977170 TO |
113 | } |
114 | else { | |
d1b65097 | 115 | // keep the old messages for now, try again later |
ecbe1139 TO |
116 | $document['expires'] = CRM_Utils_Time::getTimeRaw() + $document['retry']; |
117 | } | |
118 | $isChanged = TRUE; | |
119 | } | |
120 | ||
121 | if ($isChanged) { | |
122 | $this->cache->set('communityMessages', $document); | |
123 | } | |
124 | ||
125 | return $document; | |
126 | } | |
127 | ||
128 | /** | |
0880a9d0 | 129 | * Download document from URL and parse as JSON. |
ecbe1139 | 130 | * |
72b3a70c CW |
131 | * @return NULL|array |
132 | * parsed JSON | |
ecbe1139 | 133 | */ |
847c93ac TO |
134 | public function fetchDocument() { |
135 | list($status, $json) = $this->client->get($this->getRenderedUrl()); | |
ecbe1139 TO |
136 | if ($status != CRM_Utils_HttpClient::STATUS_OK || empty($json)) { |
137 | return NULL; | |
138 | } | |
139 | $doc = json_decode($json, TRUE); | |
140 | if (empty($doc) || json_last_error() != JSON_ERROR_NONE) { | |
141 | return NULL; | |
142 | } | |
143 | return $doc; | |
144 | } | |
145 | ||
847c93ac TO |
146 | /** |
147 | * Get the final, usable URL string (after interpolating any variables) | |
148 | * | |
149 | * @return FALSE|string | |
150 | */ | |
151 | public function getRenderedUrl() { | |
152 | return CRM_Utils_System::evalUrl($this->messagesUrl); | |
153 | } | |
154 | ||
155 | /** | |
156 | * @return bool | |
157 | */ | |
158 | public function isEnabled() { | |
159 | return $this->messagesUrl !== FALSE && $this->messagesUrl !== 'FALSE'; | |
160 | } | |
161 | ||
ecbe1139 | 162 | /** |
0880a9d0 | 163 | * Pick a message to display. |
ecbe1139 | 164 | * |
ecbe1139 TO |
165 | * @return NULL|array |
166 | */ | |
dc92f2f8 TO |
167 | public function pick() { |
168 | $document = $this->getDocument(); | |
be2fb01f | 169 | $messages = []; |
dc92f2f8 TO |
170 | foreach ($document['messages'] as $message) { |
171 | if (!isset($message['perms'])) { | |
be2fb01f | 172 | $message['perms'] = [self::DEFAULT_PERMISSION]; |
dc92f2f8 TO |
173 | } |
174 | if (!CRM_Core_Permission::checkAnyPerm($message['perms'])) { | |
175 | continue; | |
176 | } | |
177 | ||
178 | if (isset($message['components'])) { | |
179 | $enabled = array_keys(CRM_Core_Component::getEnabledComponents()); | |
180 | if (count(array_intersect($enabled, $message['components'])) == 0) { | |
181 | continue; | |
182 | } | |
183 | } | |
184 | ||
185 | $messages[] = $message; | |
186 | } | |
187 | if (empty($messages)) { | |
188 | return NULL; | |
189 | } | |
190 | ||
191 | $idx = rand(0, count($messages) - 1); | |
192 | return $messages[$idx]; | |
ecbe1139 TO |
193 | } |
194 | ||
195 | /** | |
196 | * @param string $markup | |
197 | * @return string | |
198 | */ | |
199 | public static function evalMarkup($markup) { | |
d1b65097 | 200 | $config = CRM_Core_Config::singleton(); |
be2fb01f | 201 | $vals = [ |
d1b65097 TO |
202 | 'resourceUrl' => rtrim($config->resourceBase, '/'), |
203 | 'ver' => CRM_Utils_System::version(), | |
204 | 'uf' => $config->userFramework, | |
205 | 'php' => phpversion(), | |
202407d7 | 206 | 'sid' => CRM_Utils_System::getSiteID(), |
d1b65097 TO |
207 | 'baseUrl' => $config->userFrameworkBaseURL, |
208 | 'lang' => $config->lcMessages, | |
209 | 'co' => $config->defaultContactCountry, | |
be2fb01f CW |
210 | ]; |
211 | $vars = []; | |
d1b65097 | 212 | foreach ($vals as $k => $v) { |
847c93ac TO |
213 | $vars['%%' . $k . '%%'] = $v; |
214 | $vars['{{' . $k . '}}'] = urlencode($v); | |
d1b65097 TO |
215 | } |
216 | return strtr($markup, $vars); | |
ecbe1139 TO |
217 | } |
218 | ||
8bfc3cb1 TO |
219 | /** |
220 | * Ensure that a document is well-formed | |
221 | * | |
222 | * @param array $document | |
223 | * @return bool | |
224 | */ | |
225 | public function validateDocument($document) { | |
389bcebf | 226 | if (!isset($document['ttl']) || !is_int($document['ttl'])) { |
8bfc3cb1 TO |
227 | return FALSE; |
228 | } | |
389bcebf | 229 | if (!isset($document['retry']) || !is_int($document['retry'])) { |
8bfc3cb1 TO |
230 | return FALSE; |
231 | } | |
232 | if (!isset($document['messages']) || !is_array($document['messages'])) { | |
233 | return FALSE; | |
234 | } | |
235 | foreach ($document['messages'] as $message) { | |
236 | // TODO validate $message['markup'] | |
237 | } | |
238 | ||
239 | return TRUE; | |
240 | } | |
241 | ||
ecbe1139 | 242 | } |