3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class for managing a http request
21 class CRM_Utils_Request
{
24 * Get a unique ID for the request.
26 * This unique ID is assigned to mysql when the connection is opened and is
29 * The intent is that it is available for logging purposes and for triggers.
31 * The resulting string is 17 characters long. This consists of 13 characters of uniqid
32 * and 4 more random characters.
34 * Uniqid is unique to the microsecond - to make it more unique we add 4 more characters
35 * but stop short of the full 23 character string that a prefix would generate.
37 * It is intended that this string will be saved to log tables so striking a balance between
38 * uniqueness and length is important. Note that I did check & lining up with byte values
39 * (e.g 16 characters) does not confer any benefits. Using a CHAR field rather than VARCHAR
40 * may improve speed, if indexed.
44 public static function id() {
45 if (!isset(\Civi
::$statics[__CLASS__
]['id'])) {
46 \Civi
::$statics[__CLASS__
]['id'] = uniqid() . CRM_Utils_String
::createRandom(CRM_Utils_String
::ALPHANUMERIC
, 4);
48 return \Civi
::$statics[__CLASS__
]['id'];
52 * Retrieve a value from the request (GET/POST/REQUEST)
55 * Name of the variable to be retrieved.
57 * Type of the variable (see CRM_Utils_Type for details).
58 * @param object $store
59 * Session scope where variable is stored.
61 * TRUE, if the variable is required.
62 * @param mixed $default
63 * Default value of the variable if not present.
64 * @param string $method
65 * Where to look for the variable - 'GET', 'POST' or 'REQUEST'.
66 * @param bool $isThrowException
67 * Should a an exception be thrown rather than a fatal.
70 * The value of the variable
72 * @throws \CRM_Core_Exception
74 public static function retrieve($name, $type, &$store = NULL, $abort = FALSE, $default = NULL, $method = 'REQUEST', $isThrowException = TRUE) {
79 $value = self
::getValue($name, $_GET);
83 $value = self
::getValue($name, $_POST);
87 $value = self
::getValue($name, $_REQUEST);
92 (CRM_Utils_Type
::validate($value, $type, $abort, $name) === NULL)
97 if (!isset($value) && $store) {
98 $value = $store->get($name);
101 if (!isset($value) && $abort) {
102 if ($isThrowException) {
103 throw new CRM_Core_Exception(ts("Could not find valid value for %1", [1 => $name]));
105 CRM_Core_Error
::fatal(ts("Could not find valid value for %1", [1 => $name]));
108 if (!isset($value) && $default) {
112 // minor hack for action
113 if ($name == 'action') {
114 if (!is_numeric($value) && is_string($value)) {
115 $value = CRM_Core_Action
::resolve($value);
119 if (isset($value) && $store) {
120 $store->set($name, $value);
127 * @param string $name
128 * Name of the variable to be retrieved.
130 * @param array $method - '$_GET', '$_POST' or '$_REQUEST'.
133 * The value of the variable
135 protected static function getValue($name, $method) {
136 if (isset($method[$name])) {
137 return $method[$name];
139 // CRM-18384 - decode incorrect keys generated when & is present in url
140 foreach ($method as $key => $value) {
141 if (strpos($key, 'amp;') !== FALSE) {
142 $method[str_replace('amp;', '', $key)] = $method[$key];
143 if (isset($method[$name])) {
144 return $method[$name];
157 * We should use a function that checks url values.
159 * This is a replacement for $_REQUEST which includes $_GET/$_POST
160 * but excludes $_COOKIE / $_ENV / $_SERVER.
164 public static function exportValues() {
165 // For more discussion of default $_REQUEST handling, see:
166 // http://www.php.net/manual/en/reserved.variables.request.php
167 // http://www.php.net/manual/en/ini.core.php#ini.request-order
168 // http://www.php.net/manual/en/ini.core.php#ini.variables-order
172 $result = array_merge($result, $_GET);
175 $result = array_merge($result, $_POST);
181 * Retrieve a variable from the http request.
183 * @param string $name
184 * Name of the variable to be retrieved.
185 * @param string $type
186 * Type of the variable (see CRM_Utils_Type for details).
187 * Most common options are:
190 * - 'CommaSeparatedIntegers'
194 * @param mixed $defaultValue
195 * Default value of the variable if not present.
196 * @param bool $isRequired
197 * Is the variable required for this function to proceed without an exception.
198 * @param string $method
199 * Where to look for the value - GET|POST|REQUEST
202 * @throws \CRM_Core_Exception
204 public static function retrieveValue($name, $type, $defaultValue = NULL, $isRequired = FALSE, $method = 'REQUEST') {
206 return CRM_Utils_Request
::retrieve((string) $name, (string) $type, $null, (bool) $isRequired, $defaultValue, $method, TRUE);
210 * Retrieve the component from the action attribute of a form.
212 * Contribution Page forms and Event Management forms detect the value of a
213 * component (and therefore the desired tab key) by reaching into the "action"
214 * attribute of a form and reading the final item of the path. In WordPress,
215 * however, the URL may be urlencoded, and so the URL may need to be decoded
218 * @see https://lab.civicrm.org/dev/wordpress/issues/12#note_10699
220 * @param array $attributes
221 * The form attributes array.
226 public static function retrieveComponent($attributes) {
227 $url = CRM_Utils_Array
::value('action', $attributes);
228 // Whilst the following is a fallible universal test for urlencoded URLs,
229 // thankfully the "action" URL has a limited and predictable form and
230 // therefore this comparison is sufficient for our purposes.
231 if (rawurlencode(rawurldecode($url)) !== $url) {
232 $value = strtolower(basename(rawurldecode($url)));
235 $value = strtolower(basename($url));