Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
50bfb460 | 35 | * Class for managing a http request |
6a488035 TO |
36 | */ |
37 | class CRM_Utils_Request { | |
38 | ||
40101d37 | 39 | /** |
40 | * Get a unique ID for the request. | |
41 | * | |
42 | * This unique ID is assigned to mysql when the connection is opened and is | |
43 | * available in PHP. | |
44 | * | |
45 | * The intent is that it is available for logging purposes and for triggers. | |
46 | * | |
47 | * The resulting string is 17 characters long. This consists of 13 characters of uniqid | |
48 | * and 4 more random characters. | |
49 | * | |
50 | * Uniqid is unique to the microsecond - to make it more unique we add 4 more characters | |
51 | * but stop short of the full 23 character string that a prefix would generate. | |
52 | * | |
53 | * It is intended that this string will be saved to log tables so striking a balance between | |
54 | * uniqueness and length is important. Note that I did check & lining up with byte values | |
55 | * (e.g 16 characters) does not confer any benefits. Using a CHAR field rather than VARCHAR | |
56 | * may improve speed, if indexed. | |
57 | * | |
58 | * @return string | |
59 | */ | |
60 | public static function id() { | |
61 | if (!isset(\Civi::$statics[__CLASS__]['id'])) { | |
62 | \Civi::$statics[__CLASS__]['id'] = uniqid() . CRM_Utils_String::createRandom(CRM_Utils_String::ALPHANUMERIC, 4); | |
63 | } | |
64 | return \Civi::$statics[__CLASS__]['id']; | |
65 | } | |
66 | ||
6a488035 TO |
67 | /** |
68 | * Retrieve a value from the request (GET/POST/REQUEST) | |
69 | * | |
a3379cc1 AH |
70 | * @param string $name |
71 | * Name of the variable to be retrieved. | |
72 | * @param string $type | |
73 | * Type of the variable (see CRM_Utils_Type for details). | |
74 | * @param object $store | |
75 | * Session scope where variable is stored. | |
76 | * @param bool $abort | |
77 | * TRUE, if the variable is required. | |
78 | * @param mixed $default | |
79 | * Default value of the variable if not present. | |
80 | * @param string $method | |
81 | * Where to look for the variable - 'GET', 'POST' or 'REQUEST'. | |
6a488035 | 82 | * |
a3379cc1 AH |
83 | * @return mixed |
84 | * The value of the variable | |
6a488035 | 85 | */ |
00be9182 | 86 | public static function retrieve($name, $type, &$store = NULL, $abort = FALSE, $default = NULL, $method = 'REQUEST') { |
6a488035 TO |
87 | |
88 | // hack to detect stuff not yet converted to new style | |
89 | if (!is_string($type)) { | |
90 | CRM_Core_Error::backtrace(); | |
91 | CRM_Core_Error::fatal(ts("Please convert retrieve call to use new function signature")); | |
92 | } | |
93 | ||
94 | $value = NULL; | |
95 | switch ($method) { | |
96 | case 'GET': | |
97 | $value = CRM_Utils_Array::value($name, $_GET); | |
98 | break; | |
99 | ||
100 | case 'POST': | |
101 | $value = CRM_Utils_Array::value($name, $_POST); | |
102 | break; | |
103 | ||
104 | default: | |
105 | $value = CRM_Utils_Array::value($name, $_REQUEST); | |
106 | break; | |
107 | } | |
108 | ||
109 | if (isset($value) && | |
110 | (CRM_Utils_Type::validate($value, $type, $abort, $name) === NULL) | |
111 | ) { | |
112 | $value = NULL; | |
113 | } | |
114 | ||
115 | if (!isset($value) && $store) { | |
116 | $value = $store->get($name); | |
117 | } | |
118 | ||
119 | if (!isset($value) && $abort) { | |
120 | CRM_Core_Error::fatal(ts("Could not find valid value for %1", array(1 => $name))); | |
121 | } | |
122 | ||
123 | if (!isset($value) && $default) { | |
124 | $value = $default; | |
125 | } | |
126 | ||
127 | // minor hack for action | |
128 | if ($name == 'action' && is_string($value)) { | |
129 | $value = CRM_Core_Action::resolve($value); | |
130 | } | |
131 | ||
132 | if (isset($value) && $store) { | |
133 | $store->set($name, $value); | |
134 | } | |
135 | ||
136 | return $value; | |
137 | } | |
ba56a28f TO |
138 | |
139 | /** | |
140 | * This is a replacement for $_REQUEST which includes $_GET/$_POST | |
141 | * but excludes $_COOKIE / $_ENV / $_SERVER. | |
142 | * | |
ba56a28f | 143 | * @return array |
ba56a28f | 144 | */ |
00be9182 | 145 | public static function exportValues() { |
ba56a28f TO |
146 | // For more discussion of default $_REQUEST handling, see: |
147 | // http://www.php.net/manual/en/reserved.variables.request.php | |
148 | // http://www.php.net/manual/en/ini.core.php#ini.request-order | |
149 | // http://www.php.net/manual/en/ini.core.php#ini.variables-order | |
150 | ||
151 | $result = array(); | |
152 | if ($_GET) { | |
153 | $result = array_merge($result, $_GET); | |
154 | } | |
155 | if ($_POST) { | |
156 | $result = array_merge($result, $_POST); | |
157 | } | |
158 | return $result; | |
159 | } | |
96025800 | 160 | |
6a488035 | 161 | } |