3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
= 'http://alert.civicrm.org/alert?prot=1&ver={ver}&uf={uf}&sid={sid}';
36 * Default time to wait before retrying
38 const DEFAULT_RETRY
= 7200; // 2 hours
41 * @var CRM_Utils_HttpClient
46 * @var CRM_Utils_Cache_Interface
53 protected $messagesUrl;
56 * @param CRM_Utils_Cache_Interface $cache
57 * @param CRM_Utils_HttpClient $client
59 public function __construct($cache, $client, $messagesUrl = NULL) {
60 $this->cache
= $cache;
61 $this->client
= $client;
62 if ($messagesUrl === NULL) {
63 $this->messagesUrl
= CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'community_messages_url', NULL, self
::DEFAULT_MESSAGES_URL
);
66 $this->messagesUrl
= $messagesUrl;
71 * Get the messages document
75 public function getDocument() {
76 if ($this->messagesUrl
=== FALSE) {
81 $document = $this->cache
->get('communityMessages');
83 if (empty($document) ||
!is_array($document)) {
85 'messages' => array(),
86 'expires' => 0, // ASAP
87 'ttl' => self
::DEFAULT_RETRY
,
88 'retry' => self
::DEFAULT_RETRY
,
93 if ($document['expires'] <= CRM_Utils_Time
::getTimeRaw()) {
94 $newDocument = $this->fetchDocument($this->messagesUrl
);
96 $document = $newDocument;
97 $document['expires'] = CRM_Utils_Time
::getTimeRaw() +
$document['ttl'];
100 $document['expires'] = CRM_Utils_Time
::getTimeRaw() +
$document['retry'];
106 $this->cache
->set('communityMessages', $document);
113 * Download document from URL and parse as JSON
116 * @return NULL|array parsed JSON
118 public function fetchDocument($url) {
119 list($status, $json) = $this->client
->get(CRM_Utils_System
::evalUrl($url));
120 if ($status != CRM_Utils_HttpClient
::STATUS_OK ||
empty($json)) {
123 $doc = json_decode($json, TRUE);
124 if (empty($doc) ||
json_last_error() != JSON_ERROR_NONE
) {
133 * @param callable $permChecker
134 * @param array $components
137 public function pick($permChecker, $components) {
138 throw new Exception('not implemented');
142 * @param string $markup
145 public static function evalMarkup($markup) {
146 throw new Exception('not implemented');