Merge pull request #4809 from totten/master-cs2
[civicrm-core.git] / CRM / Utils / Request.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * class for managing a http request
38 *
39 */
40 class CRM_Utils_Request {
41
42 /**
43 * We only need one instance of this object. So we use the singleton
44 * pattern and cache the instance in this variable
45 *
46 * @var object
47 * @static
48 */
49 static private $_singleton = NULL;
50
51 /**
52 * Class constructor
53 */
54 public function __construct() {}
55
56 /**
57 * Retrieve a value from the request (GET/POST/REQUEST)
58 *
59 * @param string $name name of the variable to be retrieved
60 * @param string $type type of the variable (see CRM_Utils_Type for details)
61 * @param object $store session scope where variable is stored
62 * @param bool $abort is this variable required
63 * @param mixed $default default value of the variable if not present
64 * @param string $method where should we look for the variable
65 *
66 * @return mixed the value of the variable
67 * @static
68 */
69 public static function retrieve($name, $type, &$store = NULL, $abort = FALSE, $default = NULL, $method = 'REQUEST') {
70
71 // hack to detect stuff not yet converted to new style
72 if (!is_string($type)) {
73 CRM_Core_Error::backtrace();
74 CRM_Core_Error::fatal(ts("Please convert retrieve call to use new function signature"));
75 }
76
77 $value = NULL;
78 switch ($method) {
79 case 'GET':
80 $value = CRM_Utils_Array::value($name, $_GET);
81 break;
82
83 case 'POST':
84 $value = CRM_Utils_Array::value($name, $_POST);
85 break;
86
87 default:
88 $value = CRM_Utils_Array::value($name, $_REQUEST);
89 break;
90 }
91
92 if (isset($value) &&
93 (CRM_Utils_Type::validate($value, $type, $abort, $name) === NULL)
94 ) {
95 $value = NULL;
96 }
97
98 if (!isset($value) && $store) {
99 $value = $store->get($name);
100 }
101
102 if (!isset($value) && $abort) {
103 CRM_Core_Error::fatal(ts("Could not find valid value for %1", array(1 => $name)));
104 }
105
106 if (!isset($value) && $default) {
107 $value = $default;
108 }
109
110 // minor hack for action
111 if ($name == 'action' && is_string($value)) {
112 $value = CRM_Core_Action::resolve($value);
113 }
114
115 if (isset($value) && $store) {
116 $store->set($name, $value);
117 }
118
119 return $value;
120 }
121
122 /**
123 * This is a replacement for $_REQUEST which includes $_GET/$_POST
124 * but excludes $_COOKIE / $_ENV / $_SERVER.
125 *
126 * @return array
127 */
128 public static function exportValues() {
129 // For more discussion of default $_REQUEST handling, see:
130 // http://www.php.net/manual/en/reserved.variables.request.php
131 // http://www.php.net/manual/en/ini.core.php#ini.request-order
132 // http://www.php.net/manual/en/ini.core.php#ini.variables-order
133
134 $result = array();
135 if ($_GET) {
136 $result = array_merge($result, $_GET);
137 }
138 if ($_POST) {
139 $result = array_merge($result, $_POST);
140 }
141 return $result;
142 }
143 }