3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
5 * This file is a part of Codendi.
7 * Codendi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Codendi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Codendi. If not, see <http://www.gnu.org/licenses/>.
21 /* abstract */ class Codendi_Request {
26 var $_validated_input;
32 var $_last_access_to_input;
42 function Codendi_Request($params) {
43 $this->params = $params;
44 $this->_validated_input = array();
45 $this->_last_access_to_input = array();
50 * Get the value of $variable in $this->params (user submitted values).
52 * @param string $variable Name of the parameter to get.
53 * @return mixed If the variable exist, the value is returned (string)
54 * otherwise return false;
56 function get($variable) {
58 return $this->_get($variable, $this->params);
62 * Add a param and/or set its value
65 function set($name, $value) {
66 $this->params[$name] = $value;
71 * Get the value of $variable in $array.
74 * @param string $variable Name of the parameter to get.
75 * @param array $array Name of the parameter to get.
77 function _get($variable, $array) {
78 if ($this->_exist($variable, $array)) {
79 return $array[$variable];
86 * Returns from where the variable is accessed.
90 function _getCallTrace() {
91 $backtrace = debug_backtrace();
92 $files = explode('/', $backtrace[1]['file']);
93 return $files[count($files) - 4] . '/'.
94 $files[count($files) - 3] . '/'.
95 $files[count($files) - 2] . '/'.
96 $files[count($files) - 1] . ' Line: '.
97 $backtrace[1]['line'];
102 * Check if $variable exists in user submitted parameters.
104 * @param string $variable Name of the parameter.
107 function exist($variable) {
108 return $this->_exist($variable, $this->params);
112 * Check if $variable exists in $array.
115 * @param string $variable Name of the parameter.
118 function _exist($variable, $array) {
119 return isset($array[$variable]);
122 * Apply validator on submitted user value.
124 * @param Valid Validator to apply
127 function valid(&$validator) {
128 $this->_validated_input[$validator->getKey()] = true;
129 return $validator->validate($this->get($validator->getKey()));
133 * Apply validator on all values of a submitted user array.
135 * @param Valid Validator to apply
138 function validArray(&$validator) {
139 $this->_validated_input[$validator->getKey()] = true;
141 $array = $this->get($validator->getKey());
142 if (is_array($array)) {
143 if (count($array)>0) {
144 foreach ($array as $key => $v) {
145 if (!$validator->validate($v)) {
150 $isValid = $validator->validate(null);
158 * Apply validator on submitted user value and return the value if valid
159 * Else return default value
160 * @param string $variable Name of the parameter to get.
161 * @param mixed $validator Name of the validator (string, uint, email) or an instance of a validator
162 * @param mixed $default_value Value return if the validator is not valid. Optional, default is null.
164 function getValidated($variable, $validator = 'string', $default_value = null) {
166 if ($v = ValidFactory::getInstance($validator, $variable)) {
167 $is_valid = $this->valid($v);
169 trigger_error('Validator '. $validator .' is not found', E_USER_ERROR);
171 return $is_valid ? $this->get($variable) : $default_value;*/
172 return $this->get($variable);
175 * Apply validator on submitted user array.
177 * @param string Index in the user submitted values where the array stands.
178 * @param Valid Validator to apply
181 function validInArray($index, &$validator) {
182 $this->_validated_input[$index][$validator->getKey()] = true;
183 return $validator->validate($this->getInArray($index, $validator->getKey()));
186 * Get value of $idx[$variable] in $this->params (user submitted values).
188 * @param string The index of the variable array in $this->params.
189 * @param string Name of the parameter to get.
190 * @return mixed If the variable exist, the value is returned (string)
191 * otherwise return false;
193 function getInArray($idx, $variable) {
194 $this->_last_access_to_input[$idx][$variable] = $this->_getCallTrace();
195 if(is_array($this->params[$idx])) {
196 return $this->_get($variable, $this->params[$idx]);
202 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtoupper($_SERVER['HTTP_X_REQUESTED_WITH']) == 'XMLHTTPREQUEST';