Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
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': | |
00479d26 | 97 | $value = self::getValue($name, $_GET); |
6a488035 TO |
98 | break; |
99 | ||
100 | case 'POST': | |
00479d26 | 101 | $value = self::getValue($name, $_POST); |
6a488035 TO |
102 | break; |
103 | ||
104 | default: | |
00479d26 | 105 | $value = self::getValue($name, $_REQUEST); |
6a488035 TO |
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 | ||
6ad0a621 | 123 | if (!isset($value) && $default) { |
6a488035 TO |
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 | 138 | |
00479d26 | 139 | /** |
140 | * @param string $name | |
141 | * Name of the variable to be retrieved. | |
142 | * | |
143 | * @param array $method - '$_GET', '$_POST' or '$_REQUEST'. | |
144 | * | |
145 | * @return mixed | |
146 | * The value of the variable | |
147 | */ | |
148 | public static function getValue($name, $method) { | |
131cc2eb | 149 | if (isset($method[$name])) { |
150 | return $method[$name]; | |
151 | } | |
00479d26 | 152 | // CRM-18384 - decode incorrect keys generated when & is present in url |
153 | foreach ($method as $key => $value) { | |
131cc2eb | 154 | if (strpos($key, 'amp;') !== FALSE) { |
00479d26 | 155 | $method[str_replace('amp;', '', $key)] = $method[$key]; |
131cc2eb | 156 | if (isset($method[$name])) { |
157 | return $method[$name]; | |
158 | } | |
159 | else { | |
160 | continue; | |
161 | } | |
00479d26 | 162 | } |
163 | } | |
131cc2eb | 164 | return NULL; |
00479d26 | 165 | } |
166 | ||
ba56a28f TO |
167 | /** |
168 | * This is a replacement for $_REQUEST which includes $_GET/$_POST | |
169 | * but excludes $_COOKIE / $_ENV / $_SERVER. | |
170 | * | |
ba56a28f | 171 | * @return array |
ba56a28f | 172 | */ |
00be9182 | 173 | public static function exportValues() { |
ba56a28f TO |
174 | // For more discussion of default $_REQUEST handling, see: |
175 | // http://www.php.net/manual/en/reserved.variables.request.php | |
176 | // http://www.php.net/manual/en/ini.core.php#ini.request-order | |
177 | // http://www.php.net/manual/en/ini.core.php#ini.variables-order | |
178 | ||
179 | $result = array(); | |
180 | if ($_GET) { | |
181 | $result = array_merge($result, $_GET); | |
182 | } | |
183 | if ($_POST) { | |
184 | $result = array_merge($result, $_POST); | |
185 | } | |
186 | return $result; | |
187 | } | |
96025800 | 188 | |
6a488035 | 189 | } |