Merge pull request #16263 from eileenmcnaughton/ids_3
[civicrm-core.git] / CRM / Utils / Http.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CiviCRM_Hook
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Utils_Http {
18
19 /**
20 * Parse the expiration time from a series of HTTP headers.
21 *
22 * @param array $headers
23 * @return int|null
24 * Expiration tme as seconds since epoch, or NULL if not cacheable.
25 */
26 public static function parseExpiration($headers) {
27 $headers = CRM_Utils_Array::rekey($headers, function ($k, $v) {
28 return strtolower($k);
29 });
30
31 if (!empty($headers['cache-control'])) {
32 $cc = self::parseCacheControl($headers['cache-control']);
33 if ($cc['max-age'] && is_numeric($cc['max-age'])) {
34 return CRM_Utils_Time::getTimeRaw() + $cc['max-age'];
35 }
36 }
37
38 return NULL;
39 }
40
41 /**
42 * @param string $value
43 * Ex: "max-age=86400, public".
44 * @return array
45 * Ex: Array("max-age"=>86400, "public"=>1).
46 */
47 public static function parseCacheControl($value) {
48 $result = [];
49
50 $parts = preg_split('/, */', $value);
51 foreach ($parts as $part) {
52 if (strpos($part, '=') !== FALSE) {
53 list ($key, $value) = explode('=', $part, 2);
54 $result[$key] = $value;
55 }
56 else {
57 $result[$part] = TRUE;
58 }
59 }
60
61 return $result;
62 }
63
64 }