| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Class for managing a http request |
| 36 | */ |
| 37 | class CRM_Utils_Request { |
| 38 | |
| 39 | /** |
| 40 | * Retrieve a value from the request (GET/POST/REQUEST) |
| 41 | * |
| 42 | * @param string $name |
| 43 | * Name of the variable to be retrieved. |
| 44 | * @param string $type |
| 45 | * Type of the variable (see CRM_Utils_Type for details). |
| 46 | * @param object $store |
| 47 | * Session scope where variable is stored. |
| 48 | * @param bool $abort |
| 49 | * TRUE, if the variable is required. |
| 50 | * @param mixed $default |
| 51 | * Default value of the variable if not present. |
| 52 | * @param string $method |
| 53 | * Where to look for the variable - 'GET', 'POST' or 'REQUEST'. |
| 54 | * |
| 55 | * @return mixed |
| 56 | * The value of the variable |
| 57 | */ |
| 58 | public static function retrieve($name, $type, &$store = NULL, $abort = FALSE, $default = NULL, $method = 'REQUEST') { |
| 59 | |
| 60 | // hack to detect stuff not yet converted to new style |
| 61 | if (!is_string($type)) { |
| 62 | CRM_Core_Error::backtrace(); |
| 63 | CRM_Core_Error::fatal(ts("Please convert retrieve call to use new function signature")); |
| 64 | } |
| 65 | |
| 66 | $value = NULL; |
| 67 | switch ($method) { |
| 68 | case 'GET': |
| 69 | $value = CRM_Utils_Array::value($name, $_GET); |
| 70 | break; |
| 71 | |
| 72 | case 'POST': |
| 73 | $value = CRM_Utils_Array::value($name, $_POST); |
| 74 | break; |
| 75 | |
| 76 | default: |
| 77 | $value = CRM_Utils_Array::value($name, $_REQUEST); |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | if (isset($value) && |
| 82 | (CRM_Utils_Type::validate($value, $type, $abort, $name) === NULL) |
| 83 | ) { |
| 84 | $value = NULL; |
| 85 | } |
| 86 | |
| 87 | if (!isset($value) && $store) { |
| 88 | $value = $store->get($name); |
| 89 | } |
| 90 | |
| 91 | if (!isset($value) && $abort) { |
| 92 | CRM_Core_Error::fatal(ts("Could not find valid value for %1", array(1 => $name))); |
| 93 | } |
| 94 | |
| 95 | if (!isset($value) && $default) { |
| 96 | $value = $default; |
| 97 | } |
| 98 | |
| 99 | // minor hack for action |
| 100 | if ($name == 'action' && is_string($value)) { |
| 101 | $value = CRM_Core_Action::resolve($value); |
| 102 | } |
| 103 | |
| 104 | if (isset($value) && $store) { |
| 105 | $store->set($name, $value); |
| 106 | } |
| 107 | |
| 108 | return $value; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * This is a replacement for $_REQUEST which includes $_GET/$_POST |
| 113 | * but excludes $_COOKIE / $_ENV / $_SERVER. |
| 114 | * |
| 115 | * @return array |
| 116 | */ |
| 117 | public static function exportValues() { |
| 118 | // For more discussion of default $_REQUEST handling, see: |
| 119 | // http://www.php.net/manual/en/reserved.variables.request.php |
| 120 | // http://www.php.net/manual/en/ini.core.php#ini.request-order |
| 121 | // http://www.php.net/manual/en/ini.core.php#ini.variables-order |
| 122 | |
| 123 | $result = array(); |
| 124 | if ($_GET) { |
| 125 | $result = array_merge($result, $_GET); |
| 126 | } |
| 127 | if ($_POST) { |
| 128 | $result = array_merge($result, $_POST); |
| 129 | } |
| 130 | return $result; |
| 131 | } |
| 132 | |
| 133 | } |