Merge pull request #7893 from eileenmcnaughton/CRM-18128
[civicrm-core.git] / CRM / Utils / Type.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33 class CRM_Utils_Type {
34 const
35 T_INT = 1,
36 T_STRING = 2,
37 T_ENUM = 2,
38 T_DATE = 4,
39 T_TIME = 8,
40 T_BOOLEAN = 16,
41 T_TEXT = 32,
42 T_LONGTEXT = 32,
43 T_BLOB = 64,
44 T_TIMESTAMP = 256,
45 T_FLOAT = 512,
46 T_MONEY = 1024,
47 T_EMAIL = 2048,
48 T_URL = 4096,
49 T_CCNUM = 8192,
50 T_MEDIUMBLOB = 16384;
51
52 // @todo What's the point of these constants? Backwards compatibility?
53 const
54 TWO = 2,
55 FOUR = 4,
56 SIX = 6,
57 EIGHT = 8,
58 TWELVE = 12,
59 SIXTEEN = 16,
60 TWENTY = 20,
61 MEDIUM = 20,
62 THIRTY = 30,
63 BIG = 30,
64 FORTYFIVE = 45,
65 HUGE = 45;
66
67 /**
68 * Gets the string representation for a data type.
69 *
70 * @param int $type
71 * Integer number identifying the data type.
72 *
73 * @return string
74 * String identifying the data type, e.g. 'Int' or 'String'.
75 */
76 public static function typeToString($type) {
77 // @todo Use constants in the case statements, e.g. "case T_INT:".
78 // @todo return directly, instead of assigning a value.
79 // @todo Use a lookup array, as a property or as a local variable.
80 switch ($type) {
81 case 1:
82 $string = 'Int';
83 break;
84
85 case 2:
86 $string = 'String';
87 break;
88
89 case 3:
90 $string = 'Enum';
91 break;
92
93 case 4:
94 $string = 'Date';
95 break;
96
97 case 8:
98 $string = 'Time';
99 break;
100
101 case 16:
102 $string = 'Boolean';
103 break;
104
105 case 32:
106 $string = 'Text';
107 break;
108
109 case 64:
110 $string = 'Blob';
111 break;
112
113 // CRM-10404
114 case 12:
115 case 256:
116 $string = 'Timestamp';
117 break;
118
119 case 512:
120 $string = 'Float';
121 break;
122
123 case 1024:
124 $string = 'Money';
125 break;
126
127 case 2048:
128 $string = 'Date';
129 break;
130
131 case 4096:
132 $string = 'Email';
133 break;
134
135 case 16384:
136 $string = 'Mediumblob';
137 break;
138 }
139
140 return (isset($string)) ? $string : "";
141 }
142
143 /**
144 * Helper function to call escape on arrays
145 *
146 * @see escape
147 */
148 public static function escapeAll($data, $type, $abort = TRUE) {
149 foreach ($data as $key => $value) {
150 $data[$key] = CRM_Utils_Type::escape($value, $type, $abort);
151 }
152 return $data;
153 }
154
155 /**
156 * Verify that a variable is of a given type, and apply a bit of processing.
157 *
158 * @param mixed $data
159 * The value to be verified/escaped.
160 * @param string $type
161 * The type to verify against.
162 * @param bool $abort
163 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
164 *
165 * @return mixed
166 * The data, escaped if necessary.
167 */
168 public static function escape($data, $type, $abort = TRUE) {
169 switch ($type) {
170 case 'Integer':
171 case 'Int':
172 if (CRM_Utils_Rule::integer($data)) {
173 return (int) $data;
174 }
175 break;
176
177 case 'Positive':
178 if (CRM_Utils_Rule::positiveInteger($data)) {
179 return (int) $data;
180 }
181 break;
182
183 // CRM-8925 for custom fields of this type
184 case 'Country':
185 case 'StateProvince':
186 // Handle multivalued data in delimited or array format
187 if (is_array($data) || (strpos($data, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE)) {
188 $valid = TRUE;
189 foreach (CRM_Utils_Array::explodePadded($data) as $item) {
190 if (!CRM_Utils_Rule::positiveInteger($item)) {
191 $valid = FALSE;
192 }
193 }
194 if ($valid) {
195 return $data;
196 }
197 }
198 elseif (CRM_Utils_Rule::positiveInteger($data)) {
199 return (int) $data;
200 }
201 break;
202
203 case 'File':
204 if (CRM_Utils_Rule::positiveInteger($data)) {
205 return (int) $data;
206 }
207 break;
208
209 case 'Link':
210 if (CRM_Utils_Rule::url($data = trim($data))) {
211 return $data;
212 }
213 break;
214
215 case 'Boolean':
216 if (CRM_Utils_Rule::boolean($data)) {
217 return $data;
218 }
219 break;
220
221 case 'Float':
222 case 'Money':
223 if (CRM_Utils_Rule::numeric($data)) {
224 return $data;
225 }
226 break;
227
228 case 'String':
229 case 'Memo':
230 case 'Text':
231 return CRM_Core_DAO::escapeString($data);
232
233 case 'Date':
234 case 'Timestamp':
235 // a null date or timestamp is valid
236 if (strlen(trim($data)) == 0) {
237 return trim($data);
238 }
239
240 if ((preg_match('/^\d{8}$/', $data) ||
241 preg_match('/^\d{14}$/', $data)
242 ) &&
243 CRM_Utils_Rule::mysqlDate($data)
244 ) {
245 return $data;
246 }
247 break;
248
249 case 'ContactReference':
250 if (strlen(trim($data)) == 0) {
251 return trim($data);
252 }
253
254 if (CRM_Utils_Rule::validContact($data)) {
255 return (int) $data;
256 }
257 break;
258
259 default:
260 CRM_Core_Error::fatal(
261 $type . " is not a recognised (camel cased) data type."
262 );
263 break;
264 }
265
266 // @todo Use exceptions instead of CRM_Core_Error::fatal().
267 if ($abort) {
268 $data = htmlentities($data);
269 CRM_Core_Error::fatal("$data is not of the type $type");
270 }
271 return NULL;
272 }
273
274 /**
275 * Verify that a variable is of a given type.
276 *
277 * @param mixed $data
278 * The value to validate.
279 * @param string $type
280 * The type to validate against.
281 * @param bool $abort
282 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
283 * @name string $name
284 * The name of the attribute
285 *
286 * @return mixed
287 * The data, escaped if necessary
288 */
289 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
290 switch ($type) {
291 case 'Integer':
292 case 'Int':
293 if (CRM_Utils_Rule::integer($data)) {
294 return (int) $data;
295 }
296 break;
297
298 case 'Positive':
299 if (CRM_Utils_Rule::positiveInteger($data)) {
300 return (int) $data;
301 }
302 break;
303
304 case 'Boolean':
305 if (CRM_Utils_Rule::boolean($data)) {
306 return $data;
307 }
308 break;
309
310 case 'Float':
311 case 'Money':
312 if (CRM_Utils_Rule::numeric($data)) {
313 return $data;
314 }
315 break;
316
317 case 'Text':
318 case 'String':
319 case 'Link':
320 case 'Memo':
321 return $data;
322
323 case 'Date':
324 // a null date is valid
325 if (strlen(trim($data)) == 0) {
326 return trim($data);
327 }
328
329 if (preg_match('/^\d{8}$/', $data) &&
330 CRM_Utils_Rule::mysqlDate($data)
331 ) {
332 return $data;
333 }
334 break;
335
336 case 'Timestamp':
337 // a null timestamp is valid
338 if (strlen(trim($data)) == 0) {
339 return trim($data);
340 }
341
342 if ((preg_match('/^\d{14}$/', $data) ||
343 preg_match('/^\d{8}$/', $data)
344 ) &&
345 CRM_Utils_Rule::mysqlDate($data)
346 ) {
347 return $data;
348 }
349 break;
350
351 case 'ContactReference':
352 // null is valid
353 if (strlen(trim($data)) == 0) {
354 return trim($data);
355 }
356
357 if (CRM_Utils_Rule::validContact($data)) {
358 return $data;
359 }
360 break;
361
362 default:
363 CRM_Core_Error::fatal("Cannot recognize $type for $data");
364 break;
365 }
366
367 if ($abort) {
368 $data = htmlentities($data);
369 CRM_Core_Error::fatal("$name (value: $data) is not of the type $type");
370 }
371
372 return NULL;
373 }
374
375 }