| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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 | * |
| 30 | * @package CiviCRM_Hook |
| 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
| 32 | */ |
| 33 | class CRM_Utils_Http { |
| 34 | |
| 35 | /** |
| 36 | * Parse the expiration time from a series of HTTP headers. |
| 37 | * |
| 38 | * @param array $headers |
| 39 | * @return int|NULL |
| 40 | * Expiration tme as seconds since epoch, or NULL if not cacheable. |
| 41 | */ |
| 42 | public static function parseExpiration($headers) { |
| 43 | $headers = CRM_Utils_Array::rekey($headers, function ($k, $v) { |
| 44 | return strtolower($k); |
| 45 | }); |
| 46 | |
| 47 | if (!empty($headers['cache-control'])) { |
| 48 | $cc = self::parseCacheControl($headers['cache-control']); |
| 49 | if ($cc['max-age'] && is_numeric($cc['max-age'])) { |
| 50 | return CRM_Utils_Time::getTimeRaw() + $cc['max-age']; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param string $value |
| 59 | * Ex: "max-age=86400, public". |
| 60 | * @return array |
| 61 | * Ex: Array("max-age"=>86400, "public"=>1). |
| 62 | */ |
| 63 | public static function parseCacheControl($value) { |
| 64 | $result = array(); |
| 65 | |
| 66 | $parts = preg_split('/, */', $value); |
| 67 | foreach ($parts as $part) { |
| 68 | if (strpos($part, '=') !== FALSE) { |
| 69 | list ($key, $value) = explode('=', $part, 2); |
| 70 | $result[$key] = $value; |
| 71 | } |
| 72 | else { |
| 73 | $result[$part] = TRUE; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return $result; |
| 78 | } |
| 79 | |
| 80 | } |