Merge pull request #19455 from colemanw/afformMeta
[civicrm-core.git] / CRM / Utils / Request.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
50bfb460 19 * Class for managing a http request
6a488035
TO
20 */
21class CRM_Utils_Request {
22
40101d37 23 /**
24 * Get a unique ID for the request.
25 *
26 * This unique ID is assigned to mysql when the connection is opened and is
27 * available in PHP.
28 *
29 * The intent is that it is available for logging purposes and for triggers.
30 *
31 * The resulting string is 17 characters long. This consists of 13 characters of uniqid
32 * and 4 more random characters.
33 *
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.
36 *
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.
41 *
42 * @return string
43 */
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);
47 }
48 return \Civi::$statics[__CLASS__]['id'];
49 }
50
6a488035
TO
51 /**
52 * Retrieve a value from the request (GET/POST/REQUEST)
53 *
a3379cc1
AH
54 * @param string $name
55 * Name of the variable to be retrieved.
56 * @param string $type
57 * Type of the variable (see CRM_Utils_Type for details).
58 * @param object $store
59 * Session scope where variable is stored.
60 * @param bool $abort
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'.
6a488035 66 *
a3379cc1
AH
67 * @return mixed
68 * The value of the variable
5e4ccea5 69 *
70 * @throws \CRM_Core_Exception
6a488035 71 */
c6ae94bd 72 public static function retrieve($name, $type, &$store = NULL, $abort = FALSE, $default = NULL, $method = 'REQUEST') {
6a488035
TO
73
74 $value = NULL;
75 switch ($method) {
76 case 'GET':
00479d26 77 $value = self::getValue($name, $_GET);
6a488035
TO
78 break;
79
80 case 'POST':
00479d26 81 $value = self::getValue($name, $_POST);
6a488035
TO
82 break;
83
84 default:
00479d26 85 $value = self::getValue($name, $_REQUEST);
6a488035
TO
86 break;
87 }
88
8ae17a2d 89 if (isset($value)) {
90 $value = CRM_Utils_Type::validate($value, $type, $abort, $name);
6a488035
TO
91 }
92
93 if (!isset($value) && $store) {
94 $value = $store->get($name);
95 }
96
97 if (!isset($value) && $abort) {
c6ae94bd 98 throw new CRM_Core_Exception(ts('Could not find valid value for %1', [1 => $name]));
6a488035
TO
99 }
100
6ad0a621 101 if (!isset($value) && $default) {
6a488035
TO
102 $value = $default;
103 }
104
105 // minor hack for action
c6ae94bd 106 if ($name === 'action') {
25cdc89e
MWMC
107 if (!is_numeric($value) && is_string($value)) {
108 $value = CRM_Core_Action::resolve($value);
109 }
6a488035
TO
110 }
111
112 if (isset($value) && $store) {
113 $store->set($name, $value);
114 }
115
116 return $value;
117 }
ba56a28f 118
00479d26 119 /**
120 * @param string $name
121 * Name of the variable to be retrieved.
122 *
123 * @param array $method - '$_GET', '$_POST' or '$_REQUEST'.
124 *
125 * @return mixed
6714d8d2 126 * The value of the variable
00479d26 127 */
5e4ccea5 128 protected static function getValue($name, $method) {
131cc2eb 129 if (isset($method[$name])) {
130 return $method[$name];
131 }
00479d26 132 // CRM-18384 - decode incorrect keys generated when &amp; is present in url
133 foreach ($method as $key => $value) {
131cc2eb 134 if (strpos($key, 'amp;') !== FALSE) {
00479d26 135 $method[str_replace('amp;', '', $key)] = $method[$key];
131cc2eb 136 if (isset($method[$name])) {
137 return $method[$name];
138 }
139 else {
140 continue;
141 }
00479d26 142 }
143 }
131cc2eb 144 return NULL;
00479d26 145 }
146
ba56a28f 147 /**
5e4ccea5 148 * @deprecated
149 *
150 * We should use a function that checks url values.
151 *
ba56a28f
TO
152 * This is a replacement for $_REQUEST which includes $_GET/$_POST
153 * but excludes $_COOKIE / $_ENV / $_SERVER.
154 *
ba56a28f 155 * @return array
ba56a28f 156 */
00be9182 157 public static function exportValues() {
ba56a28f
TO
158 // For more discussion of default $_REQUEST handling, see:
159 // http://www.php.net/manual/en/reserved.variables.request.php
160 // http://www.php.net/manual/en/ini.core.php#ini.request-order
161 // http://www.php.net/manual/en/ini.core.php#ini.variables-order
162
be2fb01f 163 $result = [];
ba56a28f
TO
164 if ($_GET) {
165 $result = array_merge($result, $_GET);
166 }
167 if ($_POST) {
168 $result = array_merge($result, $_POST);
169 }
170 return $result;
171 }
96025800 172
5e4ccea5 173 /**
174 * Retrieve a variable from the http request.
175 *
176 * @param string $name
177 * Name of the variable to be retrieved.
178 * @param string $type
179 * Type of the variable (see CRM_Utils_Type for details).
180 * Most common options are:
181 * - 'Integer'
182 * - 'Positive'
183 * - 'CommaSeparatedIntegers'
184 * - 'Boolean'
185 * - 'String'
186 *
187 * @param mixed $defaultValue
188 * Default value of the variable if not present.
189 * @param bool $isRequired
190 * Is the variable required for this function to proceed without an exception.
191 * @param string $method
192 * Where to look for the value - GET|POST|REQUEST
193 *
194 * @return mixed
54c32137 195 * @throws \CRM_Core_Exception
5e4ccea5 196 */
197 public static function retrieveValue($name, $type, $defaultValue = NULL, $isRequired = FALSE, $method = 'REQUEST') {
198 $null = NULL;
199 return CRM_Utils_Request::retrieve((string) $name, (string) $type, $null, (bool) $isRequired, $defaultValue, $method, TRUE);
200 }
201
b90552b7
CW
202 /**
203 * Retrieve the component from the action attribute of a form.
204 *
205 * Contribution Page forms and Event Management forms detect the value of a
206 * component (and therefore the desired tab key) by reaching into the "action"
207 * attribute of a form and reading the final item of the path. In WordPress,
208 * however, the URL may be urlencoded, and so the URL may need to be decoded
209 * before parsing it.
210 *
211 * @see https://lab.civicrm.org/dev/wordpress/issues/12#note_10699
212 *
213 * @param array $attributes
214 * The form attributes array.
215 *
6714d8d2 216 * @return string
b90552b7
CW
217 * The desired value.
218 */
219 public static function retrieveComponent($attributes) {
9c1bc317 220 $url = $attributes['action'] ?? NULL;
b90552b7
CW
221 // Whilst the following is a fallible universal test for urlencoded URLs,
222 // thankfully the "action" URL has a limited and predictable form and
223 // therefore this comparison is sufficient for our purposes.
224 if (rawurlencode(rawurldecode($url)) !== $url) {
225 $value = strtolower(basename(rawurldecode($url)));
226 }
227 else {
228 $value = strtolower(basename($url));
229 }
230 return $value;
231 }
232
6a488035 233}