minor fix
[civicrm-core.git] / CRM / Utils / Request.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
32 */
33
34 /**
35 * Class for managing a http request
36 */
37 class CRM_Utils_Request {
38
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
67 /**
68 * Retrieve a value from the request (GET/POST/REQUEST)
69 *
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'.
82 *
83 * @return mixed
84 * The value of the variable
85 */
86 public static function retrieve($name, $type, &$store = NULL, $abort = FALSE, $default = NULL, $method = 'REQUEST') {
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 = self::getValue($name, $_GET);
98 break;
99
100 case 'POST':
101 $value = self::getValue($name, $_POST);
102 break;
103
104 default:
105 $value = self::getValue($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 }
138
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) {
149 if (isset($method[$name])) {
150 return $method[$name];
151 }
152 // CRM-18384 - decode incorrect keys generated when &amp; is present in url
153 foreach ($method as $key => $value) {
154 if (strpos($key, 'amp;') !== FALSE) {
155 $method[str_replace('amp;', '', $key)] = $method[$key];
156 if (isset($method[$name])) {
157 return $method[$name];
158 }
159 else {
160 continue;
161 }
162 }
163 }
164 return NULL;
165 }
166
167 /**
168 * This is a replacement for $_REQUEST which includes $_GET/$_POST
169 * but excludes $_COOKIE / $_ENV / $_SERVER.
170 *
171 * @return array
172 */
173 public static function exportValues() {
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 }
188
189 }