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