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