CRM-12193 - Add testGetDocument_disabled; misc cleanup
[civicrm-core.git] / CRM / Core / CommunityMessages.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Manage the download, validation, and rendering of community messages
30 */
31 class CRM_Core_CommunityMessages {
32
33 const DEFAULT_MESSAGES_URL = 'http://alert.civicrm.org/alert?prot=1&ver={ver}&uf={uf}&sid={sid}';
34
35 /**
36 * Default time to wait before retrying
37 */
38 const DEFAULT_RETRY = 7200; // 2 hours
39
40 /**
41 * @var CRM_Utils_HttpClient
42 */
43 protected $client;
44
45 /**
46 * @var CRM_Utils_Cache_Interface
47 */
48 protected $cache;
49
50 /**
51 * @var FALSE|string
52 */
53 protected $messagesUrl;
54
55 /**
56 * @param CRM_Utils_Cache_Interface $cache
57 * @param CRM_Utils_HttpClient $client
58 */
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);
64 }
65 else {
66 $this->messagesUrl = $messagesUrl;
67 }
68 }
69
70 /**
71 * Get the messages document
72 *
73 * @return NULL|array
74 */
75 public function getDocument() {
76 if ($this->messagesUrl === FALSE) {
77 return NULL;
78 }
79
80 $isChanged = FALSE;
81 $document = $this->cache->get('communityMessages');
82
83 if (empty($document) || !is_array($document)) {
84 $document = array(
85 'messages' => array(),
86 'expires' => 0, // ASAP
87 'ttl' => self::DEFAULT_RETRY,
88 'retry' => self::DEFAULT_RETRY,
89 );
90 $isChanged = TRUE;
91 }
92
93 if ($document['expires'] <= CRM_Utils_Time::getTimeRaw()) {
94 $newDocument = $this->fetchDocument($this->messagesUrl);
95 if ($newDocument) {
96 $document = $newDocument;
97 $document['expires'] = CRM_Utils_Time::getTimeRaw() + $document['ttl'];
98 }
99 else {
100 $document['expires'] = CRM_Utils_Time::getTimeRaw() + $document['retry'];
101 }
102 $isChanged = TRUE;
103 }
104
105 if ($isChanged) {
106 $this->cache->set('communityMessages', $document);
107 }
108
109 return $document;
110 }
111
112 /**
113 * Download document from URL and parse as JSON
114 *
115 * @param string $url
116 * @return NULL|array parsed JSON
117 */
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)) {
121 return NULL;
122 }
123 $doc = json_decode($json, TRUE);
124 if (empty($doc) || json_last_error() != JSON_ERROR_NONE) {
125 return NULL;
126 }
127 return $doc;
128 }
129
130 /**
131 * Pick one message
132 *
133 * @param callable $permChecker
134 * @param array $components
135 * @return NULL|array
136 */
137 public function pick($permChecker, $components) {
138 throw new Exception('not implemented');
139 }
140
141 /**
142 * @param string $markup
143 * @return string
144 */
145 public static function evalMarkup($markup) {
146 throw new Exception('not implemented');
147 }
148
149 }