Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-06-25-23-42-42
[civicrm-core.git] / CRM / Utils / Type.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 class CRM_Utils_Type {
36 CONST
37 T_INT = 1,
38 T_STRING = 2,
39 T_ENUM = 2,
40 T_DATE = 4,
41 T_TIME = 8,
42 T_BOOLEAN = 16,
43 T_TEXT = 32,
44 T_LONGTEXT = 32,
45 T_BLOB = 64,
46 T_TIMESTAMP = 256,
47 T_FLOAT = 512,
48 T_MONEY = 1024,
49 T_EMAIL = 2048,
50 T_URL = 4096,
51 T_CCNUM = 8192,
52 T_MEDIUMBLOB = 16384;
53
54 CONST
55 TWO = 2,
56 FOUR = 4,
57 SIX = 6,
58 EIGHT = 8,
59 TWELVE = 12,
60 SIXTEEN = 16,
61 TWENTY = 20,
62 MEDIUM = 20,
63 THIRTY = 30,
64 BIG = 30,
65 FORTYFIVE = 45,
66 HUGE = 45;
67
68 /**
69 * Convert Constant Data type to String
70 *
71 * @param $type integer datatype
72 *
73 * @return string $string String datatype respective to integer datatype@access public
74 * @static
75 */
76 static function typeToString($type) {
77 switch ($type) {
78 case 1:
79 $string = 'Int';
80 break;
81
82 case 2:
83 $string = 'String';
84 break;
85
86 case 3:
87 $string = 'Enum';
88 break;
89
90 case 4:
91 $string = 'Date';
92 break;
93
94 case 8:
95 $string = 'Time';
96 break;
97
98 case 16:
99 $string = 'Boolean';
100 break;
101
102 case 32:
103 $string = 'Text';
104 break;
105
106 case 64:
107 $string = 'Blob';
108 break;
109
110 // CRM-10404
111 case 12:
112 case 256:
113 $string = 'Timestamp';
114 break;
115
116 case 512:
117 $string = 'Float';
118 break;
119
120 case 1024:
121 $string = 'Money';
122 break;
123
124 case 2048:
125 $string = 'Date';
126 break;
127
128 case 4096:
129 $string = 'Email';
130 break;
131
132 case 16384:
133 $string = 'Mediumblob';
134 break;
135 }
136
137 return (isset($string)) ? $string : "";
138 }
139
140 /**
141 * Verify that a variable is of a given type
142 *
143 * @param mixed $data The variable
144 * @param string $type The type
145 * @param boolean $abort Should we abort if invalid
146 *
147 * @return mixed The data, escaped if necessary
148 * @access public
149 * @static
150 */
151 public static function escape($data, $type, $abort = TRUE) {
152 switch ($type) {
153 case 'Integer':
154 case 'Int':
155 if (CRM_Utils_Rule::integer($data)) {
156 return $data;
157 }
158 break;
159
160 case 'Positive':
161 // the below 2 are for custom fields of this type
162 // CRM-8925
163 case 'Country':
164 case 'StateProvince':
165 // Checked for multi valued state/country value
166 if (is_array($data)) {
167 $returnData = TRUE;
168 foreach ($data as $data) {
169 if (CRM_Utils_Rule::positiveInteger($data) || CRM_Core_DAO::escapeString($data)) {
170 $returnData = TRUE;
171 }
172 else {
173 $returnData = FALSE;
174 }
175 }
176 if ($returnData) {
177 return $data;
178 }
179 }
180 elseif (!is_numeric($data) && CRM_Core_DAO::escapeString($data)) {
181 return $data;
182 }
183 elseif (CRM_Utils_Rule::positiveInteger($data)) {
184 return $data;
185 }
186 break;
187
188 case 'Link':
189 if (CRM_Utils_Rule::url($data = trim($data))) {
190 return $data;
191 }
192 break;
193
194 case 'Boolean':
195 if (CRM_Utils_Rule::boolean($data)) {
196 return $data;
197 }
198 break;
199
200 case 'Float':
201 case 'Money':
202 if (CRM_Utils_Rule::numeric($data)) {
203 return $data;
204 }
205 break;
206
207 case 'String':
208 case 'Memo':
209 case 'Text':
210 return CRM_Core_DAO::escapeString($data);
211
212 case 'Date':
213 case 'Timestamp':
214 // a null date or timestamp is valid
215 if (strlen(trim($data)) == 0) {
216 return trim($data);
217 }
218
219 if ((preg_match('/^\d{8}$/', $data) ||
220 preg_match('/^\d{14}$/', $data)
221 ) &&
222 CRM_Utils_Rule::mysqlDate($data)
223 ) {
224 return $data;
225 }
226 break;
227
228 case 'ContactReference':
229 if (strlen(trim($data)) == 0) {
230 return trim($data);
231 }
232
233 if (CRM_Utils_Rule::validContact($data)) {
234 return $data;
235 }
236 break;
237
238 default:
239 CRM_Core_Error::fatal("Cannot recognize $type for $data");
240 break;
241 }
242
243 if ($abort) {
244 $data = htmlentities($data);
245 CRM_Core_Error::fatal("$data is not of the type $type");
246 }
247 return NULL;
248 }
249
250 /**
251 * Verify that a variable is of a given type
252 *
253 * @param mixed $data The variable
254 * @param string $type The type
255 * @param boolean $abort Should we abort if invalid
256 * @name string $name The name of the attribute
257 *
258 * @return mixed The data, escaped if necessary
259 * @access public
260 * @static
261 */
262 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
263 switch ($type) {
264 case 'Integer':
265 case 'Int':
266 if (CRM_Utils_Rule::integer($data)) {
267 return $data;
268 }
269 break;
270
271 case 'Positive':
272 if (CRM_Utils_Rule::positiveInteger($data)) {
273 return $data;
274 }
275 break;
276
277 case 'Boolean':
278 if (CRM_Utils_Rule::boolean($data)) {
279 return $data;
280 }
281 break;
282
283 case 'Float':
284 case 'Money':
285 if (CRM_Utils_Rule::numeric($data)) {
286 return $data;
287 }
288 break;
289
290 case 'Text':
291 case 'String':
292 case 'Link':
293 case 'Memo':
294 return $data;
295
296 case 'Date':
297 // a null date is valid
298 if (strlen(trim($data)) == 0) {
299 return trim($data);
300 }
301
302 if (preg_match('/^\d{8}$/', $data) &&
303 CRM_Utils_Rule::mysqlDate($data)
304 ) {
305 return $data;
306 }
307 break;
308
309 case 'Timestamp':
310 // a null timestamp is valid
311 if (strlen(trim($data)) == 0) {
312 return trim($data);
313 }
314
315 if ((preg_match('/^\d{14}$/', $data) ||
316 preg_match('/^\d{8}$/', $data)
317 ) &&
318 CRM_Utils_Rule::mysqlDate($data)
319 ) {
320 return $data;
321 }
322 break;
323
324 case 'ContactReference':
325 // null is valid
326 if (strlen(trim($data)) == 0) {
327 return trim($data);
328 }
329
330 if (CRM_Utils_Rule::validContact($data)) {
331 return $data;
332 }
333 break;
334
335 default:
336 CRM_Core_Error::fatal("Cannot recognize $type for $data");
337 break;
338 }
339
340 if ($abort) {
341 $data = htmlentities($data);
342 CRM_Core_Error::fatal("$name (value: $data) is not of the type $type");
343 }
344
345 return NULL;
346 }
347 }
348