Merge pull request #13287 from mattwire/eventregister_rename
[civicrm-core.git] / CRM / Core / BAO / Cache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * BAO object for civicrm_cache table.
30 *
31 * This is a database cache and is persisted across sessions. Typically we use
32 * this to store meta data (like profile fields, custom fields etc).
33 *
34 * The group_name column is used for grouping together all cache elements that logically belong to the same set.
35 * Thus all session cache entries are grouped under 'CiviCRM Session'. This allows us to delete all entries of
36 * a specific group if needed.
37 *
38 * The path column allows us to differentiate between items in that group. Thus for the session cache, the path is
39 * the unique form name for each form (per user)
40 */
41 class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
42
43 /**
44 * When store session/form state, how long should the data be retained?
45 *
46 * @var int, number of second
47 */
48 const DEFAULT_SESSION_TTL = 172800; // Two days: 2*24*60*60
49
50 /**
51 * @var array ($cacheKey => $cacheValue)
52 */
53 static $_cache = NULL;
54
55 /**
56 * Retrieve an item from the DB cache.
57 *
58 * @param string $group
59 * (required) The group name of the item.
60 * @param string $path
61 * (required) The path under which this item is stored.
62 * @param int $componentID
63 * The optional component ID (so componenets can share the same name space).
64 *
65 * @return object
66 * The data if present in cache, else null
67 * @deprecated
68 */
69 public static function &getItem($group, $path, $componentID = NULL) {
70 if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
71 $value = $adapter::getItem($group, $path, $componentID);
72 return $value;
73 }
74
75 if (self::$_cache === NULL) {
76 self::$_cache = array();
77 }
78
79 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
80 if (!array_key_exists($argString, self::$_cache)) {
81 $cache = CRM_Utils_Cache::singleton();
82 $cleanKey = self::cleanKey($argString);
83 self::$_cache[$argString] = $cache->get($cleanKey);
84 if (self::$_cache[$argString] === NULL) {
85 $table = self::getTableName();
86 $where = self::whereCache($group, $path, $componentID);
87 $rawData = CRM_Core_DAO::singleValueQuery("SELECT data FROM $table WHERE $where");
88 $data = $rawData ? self::decode($rawData) : NULL;
89
90 self::$_cache[$argString] = $data;
91 if ($data !== NULL) {
92 // Do not cache 'null' as that is most likely a cache miss & we shouldn't then cache it.
93 $cache->set($cleanKey, self::$_cache[$argString]);
94 }
95 }
96 }
97 return self::$_cache[$argString];
98 }
99
100 /**
101 * Retrieve all items in a group.
102 *
103 * @param string $group
104 * (required) The group name of the item.
105 * @param int $componentID
106 * The optional component ID (so componenets can share the same name space).
107 *
108 * @return object
109 * The data if present in cache, else null
110 * @deprecated
111 */
112 public static function &getItems($group, $componentID = NULL) {
113 if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
114 return $adapter::getItems($group, $componentID);
115 }
116
117 if (self::$_cache === NULL) {
118 self::$_cache = array();
119 }
120
121 $argString = "CRM_CT_CI_{$group}_{$componentID}";
122 if (!array_key_exists($argString, self::$_cache)) {
123 $cache = CRM_Utils_Cache::singleton();
124 $cleanKey = self::cleanKey($argString);
125 self::$_cache[$argString] = $cache->get($cleanKey);
126 if (!self::$_cache[$argString]) {
127 $table = self::getTableName();
128 $where = self::whereCache($group, NULL, $componentID);
129 $dao = CRM_Core_DAO::executeQuery("SELECT path, data FROM $table WHERE $where");
130
131 $result = array();
132 while ($dao->fetch()) {
133 $result[$dao->path] = self::decode($dao->data);
134 }
135 $dao->free();
136
137 self::$_cache[$argString] = $result;
138 $cache->set($cleanKey, self::$_cache[$argString]);
139 }
140 }
141
142 return self::$_cache[$argString];
143 }
144
145 /**
146 * Store an item in the DB cache.
147 *
148 * @param object $data
149 * (required) A reference to the data that will be serialized and stored.
150 * @param string $group
151 * (required) The group name of the item.
152 * @param string $path
153 * (required) The path under which this item is stored.
154 * @param int $componentID
155 * The optional component ID (so componenets can share the same name space).
156 * @deprecated
157 */
158 public static function setItem(&$data, $group, $path, $componentID = NULL) {
159 if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
160 return $adapter::setItem($data, $group, $path, $componentID);
161 }
162
163 if (self::$_cache === NULL) {
164 self::$_cache = array();
165 }
166
167 // get a lock so that multiple ajax requests on the same page
168 // dont trample on each other
169 // CRM-11234
170 $lock = Civi::lockManager()->acquire("cache.{$group}_{$path}._{$componentID}");
171 if (!$lock->isAcquired()) {
172 CRM_Core_Error::fatal();
173 }
174
175 $table = self::getTableName();
176 $where = self::whereCache($group, $path, $componentID);
177 $dataExists = CRM_Core_DAO::singleValueQuery("SELECT COUNT(*) FROM $table WHERE {$where}");
178 $now = date('Y-m-d H:i:s'); // FIXME - Use SQL NOW() or CRM_Utils_Time?
179 $dataSerialized = self::encode($data);
180
181 // This table has a wonky index, so we cannot use REPLACE or
182 // "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE).
183 if ($dataExists) {
184 $sql = "UPDATE $table SET data = %1, created_date = %2 WHERE {$where}";
185 $args = array(
186 1 => array($dataSerialized, 'String'),
187 2 => array($now, 'String'),
188 );
189 $dao = CRM_Core_DAO::executeQuery($sql, $args, TRUE, NULL, FALSE, FALSE);
190 }
191 else {
192 $insert = CRM_Utils_SQL_Insert::into($table)
193 ->row(array(
194 'group_name' => $group,
195 'path' => $path,
196 'component_id' => $componentID,
197 'data' => $dataSerialized,
198 'created_date' => $now,
199 ));
200 $dao = CRM_Core_DAO::executeQuery($insert->toSQL(), array(), TRUE, NULL, FALSE, FALSE);
201 }
202
203 $lock->release();
204
205 $dao->free();
206
207 // cache coherency - refresh or remove dependent caches
208
209 $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
210 $cache = CRM_Utils_Cache::singleton();
211 $data = self::decode($dataSerialized);
212 self::$_cache[$argString] = $data;
213 $cache->set(self::cleanKey($argString), $data);
214
215 $argString = "CRM_CT_CI_{$group}_{$componentID}";
216 unset(self::$_cache[$argString]);
217 $cache->delete(self::cleanKey($argString));
218 }
219
220 /**
221 * Delete all the cache elements that belong to a group OR delete the entire cache if group is not specified.
222 *
223 * @param string $group
224 * The group name of the entries to be deleted.
225 * @param string $path
226 * Path of the item that needs to be deleted.
227 * @param bool $clearAll clear all caches
228 * @deprecated
229 */
230 public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
231 if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
232 return $adapter::deleteGroup($group, $path);
233 }
234 else {
235 $table = self::getTableName();
236 $where = self::whereCache($group, $path, NULL);
237 CRM_Core_DAO::executeQuery("DELETE FROM $table WHERE $where");
238 }
239
240 if ($clearAll) {
241 // also reset ACL Cache
242 CRM_ACL_BAO_Cache::resetCache();
243
244 // also reset memory cache if any
245 CRM_Utils_System::flushCache();
246 }
247 }
248
249 /**
250 * The next two functions are internal functions used to store and retrieve session from
251 * the database cache. This keeps the session to a limited size and allows us to
252 * create separate session scopes for each form in a tab
253 */
254
255 /**
256 * This function takes entries from the session array and stores it in the cache.
257 *
258 * It also deletes the entries from the $_SESSION object (for a smaller session size)
259 *
260 * @param array $names
261 * Array of session values that should be persisted.
262 * This is either a form name + qfKey or just a form name
263 * (in the case of profile)
264 * @param bool $resetSession
265 * Should session state be reset on completion of DB store?.
266 */
267 public static function storeSessionToCache($names, $resetSession = TRUE) {
268 foreach ($names as $key => $sessionName) {
269 if (is_array($sessionName)) {
270 $value = NULL;
271 if (!empty($_SESSION[$sessionName[0]][$sessionName[1]])) {
272 $value = $_SESSION[$sessionName[0]][$sessionName[1]];
273 }
274 $key = "{$sessionName[0]}_{$sessionName[1]}";
275 Civi::cache('session')->set($key, $value, self::pickSessionTtl($key));
276 if ($resetSession) {
277 $_SESSION[$sessionName[0]][$sessionName[1]] = NULL;
278 unset($_SESSION[$sessionName[0]][$sessionName[1]]);
279 }
280 }
281 else {
282 $value = NULL;
283 if (!empty($_SESSION[$sessionName])) {
284 $value = $_SESSION[$sessionName];
285 }
286 Civi::cache('session')->set($sessionName, $value, self::pickSessionTtl($sessionName));
287 if ($resetSession) {
288 $_SESSION[$sessionName] = NULL;
289 unset($_SESSION[$sessionName]);
290 }
291 }
292 }
293
294 self::cleanup();
295 }
296
297 /* Retrieve the session values from the cache and populate the $_SESSION array
298 *
299 * @param array $names
300 * Array of session values that should be persisted.
301 * This is either a form name + qfKey or just a form name
302 * (in the case of profile)
303 */
304
305 /**
306 * Restore session from cache.
307 *
308 * @param string $names
309 */
310 public static function restoreSessionFromCache($names) {
311 foreach ($names as $key => $sessionName) {
312 if (is_array($sessionName)) {
313 $value = Civi::cache('session')->get("{$sessionName[0]}_{$sessionName[1]}");
314 if ($value) {
315 $_SESSION[$sessionName[0]][$sessionName[1]] = $value;
316 }
317 }
318 else {
319 $value = Civi::cache('session')->get($sessionName);
320 if ($value) {
321 $_SESSION[$sessionName] = $value;
322 }
323 }
324 }
325 }
326
327 /**
328 * Determine how long session-state should be retained.
329 *
330 * @param string $sessionKey
331 * Ex: '_CRM_Admin_Form_Preferences_Display_f1a5f232e3d850a29a7a4d4079d7c37b_4654_container'
332 * Ex: 'CiviCRM_CRM_Admin_Form_Preferences_Display_f1a5f232e3d850a29a7a4d4079d7c37b_4654'
333 * @return int
334 * Number of seconds.
335 */
336 protected static function pickSessionTtl($sessionKey) {
337 $secureSessionTimeoutMinutes = (int) Civi::settings()->get('secure_cache_timeout_minutes');
338 if ($secureSessionTimeoutMinutes) {
339 $transactionPages = array(
340 'CRM_Contribute_Controller_Contribution',
341 'CRM_Event_Controller_Registration',
342 );
343 foreach ($transactionPages as $transactionPage) {
344 if (strpos($sessionKey, $transactionPage) !== FALSE) {
345 return $secureSessionTimeoutMinutes * 60;
346 }
347 }
348 }
349
350 return self::DEFAULT_SESSION_TTL;
351 }
352
353 /**
354 * Do periodic cleanup of the CiviCRM session table.
355 *
356 * Also delete all session cache entries which are a couple of days old.
357 * This keeps the session cache to a manageable size
358 * Delete Contribution page session caches more energetically.
359 *
360 * @param bool $session
361 * @param bool $table
362 * @param bool $prevNext
363 */
364 public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE, $expired = FALSE) {
365 // clean up the session cache every $cacheCleanUpNumber probabilistically
366 $cleanUpNumber = 757;
367
368 // clean up all sessions older than $cacheTimeIntervalDays days
369 $timeIntervalDays = 2;
370
371 if (mt_rand(1, 100000) % $cleanUpNumber == 0) {
372 $expired = $session = $table = $prevNext = TRUE;
373 }
374
375 if (!$session && !$table && !$prevNext && !$expired) {
376 return;
377 }
378
379 if ($prevNext) {
380 // delete all PrevNext caches
381 CRM_Core_BAO_PrevNextCache::cleanupCache();
382 }
383
384 if ($table) {
385 CRM_Core_Config::clearTempTables($timeIntervalDays . ' day');
386 }
387
388 if ($session) {
389 // Session caches are just regular caches, so they expire naturally per TTL.
390 $expired = TRUE;
391 }
392
393 if ($expired) {
394 $sql = "DELETE FROM civicrm_cache WHERE expired_date < %1";
395 $params = [
396 1 => [date(CRM_Utils_Cache_SqlGroup::TS_FMT, CRM_Utils_Time::getTimeRaw()), 'String'],
397 ];
398 CRM_Core_DAO::executeQuery($sql, $params);
399 }
400 }
401
402 /**
403 * (Quasi-private) Encode an object/array/string/int as a string.
404 *
405 * @param $mixed
406 * @return string
407 */
408 public static function encode($mixed) {
409 return base64_encode(serialize($mixed));
410 }
411
412 /**
413 * (Quasi-private) Decode an object/array/string/int from a string.
414 *
415 * @param $string
416 * @return mixed
417 */
418 public static function decode($string) {
419 // Upgrade support -- old records (serialize) always have this punctuation,
420 // and new records (base64) never do.
421 if (strpos($string, ':') !== FALSE || strpos($string, ';') !== FALSE) {
422 return unserialize($string);
423 }
424 else {
425 return unserialize(base64_decode($string));
426 }
427 }
428
429 /**
430 * Compose a SQL WHERE clause for the cache.
431 *
432 * Note: We need to use the cache during bootstrap, so we don't have
433 * full access to DAO services.
434 *
435 * @param string $group
436 * @param string|NULL $path
437 * Filter by path. If NULL, then return any paths.
438 * @param int|NULL $componentID
439 * Filter by component. If NULL, then look for explicitly NULL records.
440 * @return string
441 */
442 protected static function whereCache($group, $path, $componentID) {
443 $clauses = array();
444 $clauses[] = ('group_name = "' . CRM_Core_DAO::escapeString($group) . '"');
445 if ($path) {
446 $clauses[] = ('path = "' . CRM_Core_DAO::escapeString($path) . '"');
447 }
448 if ($componentID && is_numeric($componentID)) {
449 $clauses[] = ('component_id = ' . (int) $componentID);
450 }
451 return $clauses ? implode(' AND ', $clauses) : '(1)';
452 }
453
454 /**
455 * Normalize a cache key.
456 *
457 * This bridges an impedance mismatch between our traditional caching
458 * and PSR-16 -- PSR-16 accepts a narrower range of cache keys.
459 *
460 * @param string $key
461 * Ex: 'ab/cd:ef'
462 * @return string
463 * Ex: '_abcd1234abcd1234' or 'ab_xx/cd_xxef'.
464 * A similar key, but suitable for use with PSR-16-compliant cache providers.
465 */
466 public static function cleanKey($key) {
467 if (!is_string($key) && !is_int($key)) {
468 throw new \RuntimeException("Malformed cache key");
469 }
470
471 $maxLen = 64;
472 $escape = '-';
473
474 if (strlen($key) >= $maxLen) {
475 return $escape . md5($key);
476 }
477
478 $r = preg_replace_callback(';[^A-Za-z0-9_\.];', function($m) use ($escape) {
479 return $escape . dechex(ord($m[0]));
480 }, $key);
481
482 return strlen($r) >= $maxLen ? $escape . md5($key) : $r;
483 }
484
485 }