CRM-14499: Update FourFour upgrade script and changes image urls to improve security...
[civicrm-core.git] / CRM / Utils / Type.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 datatype respective to integer datatype
74 *
75 * @access public
76 * @static
77 */
78 static function typeToString($type) {
79 switch ($type) {
80 case 1:
81 $string = 'Int';
82 break;
83
84 case 2:
85 $string = 'String';
86 break;
87
88 case 3:
89 $string = 'Enum';
90 break;
91
92 case 4:
93 $string = 'Date';
94 break;
95
96 case 8:
97 $string = 'Time';
98 break;
99
100 case 16:
101 $string = 'Boolean';
102 break;
103
104 case 32:
105 $string = 'Text';
106 break;
107
108 case 64:
109 $string = 'Blob';
110 break;
111
112 // CRM-10404
113 case 12:
114 case 256:
115 $string = 'Timestamp';
116 break;
117
118 case 512:
119 $string = 'Float';
120 break;
121
122 case 1024:
123 $string = 'Money';
124 break;
125
126 case 2048:
127 $string = 'Date';
128 break;
129
130 case 4096:
131 $string = 'Email';
132 break;
133
134 case 16384:
135 $string = 'Mediumblob';
136 break;
137 }
138
139 return (isset($string)) ? $string : "";
140 }
141
142 /**
143 * Verify that a variable is of a given type
144 *
145 * @param mixed $data The variable
146 * @param string $type The type
147 * @param boolean $abort Should we abort if invalid
148 *
149 * @return mixed The data, escaped if necessary
150 * @access public
151 * @static
152 */
153 public static function escape($data, $type, $abort = TRUE) {
154 switch ($type) {
155 case 'Integer':
156 case 'Int':
157 if (CRM_Utils_Rule::integer($data)) {
158 return $data;
159 }
160 break;
161
162 case 'Positive':
163 // the below 2 are for custom fields of this type
164 // CRM-8925
165 case 'Country':
166 case 'StateProvince':
167 if (CRM_Utils_Rule::positiveInteger($data)) {
168 return $data;
169 }
170 break;
171
172 case 'Link':
173 if (CRM_Utils_Rule::url($data = trim($data))) {
174 return $data;
175 }
176 break;
177
178 case 'Boolean':
179 if (CRM_Utils_Rule::boolean($data)) {
180 return $data;
181 }
182 break;
183
184 case 'Float':
185 case 'Money':
186 if (CRM_Utils_Rule::numeric($data)) {
187 return $data;
188 }
189 break;
190
191 case 'String':
192 case 'Memo':
193 case 'Text':
194 return CRM_Core_DAO::escapeString($data);
195
196 case 'Date':
197 case 'Timestamp':
198 // a null date or timestamp is valid
199 if (strlen(trim($data)) == 0) {
200 return trim($data);
201 }
202
203 if ((preg_match('/^\d{8}$/', $data) ||
204 preg_match('/^\d{14}$/', $data)
205 ) &&
206 CRM_Utils_Rule::mysqlDate($data)
207 ) {
208 return $data;
209 }
210 break;
211
212 case 'ContactReference':
213 if (strlen(trim($data)) == 0) {
214 return trim($data);
215 }
216
217 if (CRM_Utils_Rule::validContact($data)) {
218 return $data;
219 }
220 break;
221
222 default:
223 CRM_Core_Error::fatal("Cannot recognize $type for $data");
224 break;
225 }
226
227 if ($abort) {
228 $data = htmlentities($data);
229 CRM_Core_Error::fatal("$data is not of the type $type");
230 }
231 return NULL;
232 }
233
234 /**
235 * Verify that a variable is of a given type
236 *
237 * @param mixed $data The variable
238 * @param string $type The type
239 * @param boolean $abort Should we abort if invalid
240 * @name string $name The name of the attribute
241 *
242 * @return mixed The data, escaped if necessary
243 * @access public
244 * @static
245 */
246 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
247 switch ($type) {
248 case 'Integer':
249 case 'Int':
250 if (CRM_Utils_Rule::integer($data)) {
251 return $data;
252 }
253 break;
254
255 case 'Positive':
256 if (CRM_Utils_Rule::positiveInteger($data)) {
257 return $data;
258 }
259 break;
260
261 case 'Boolean':
262 if (CRM_Utils_Rule::boolean($data)) {
263 return $data;
264 }
265 break;
266
267 case 'Float':
268 case 'Money':
269 if (CRM_Utils_Rule::numeric($data)) {
270 return $data;
271 }
272 break;
273
274 case 'Text':
275 case 'String':
276 case 'Link':
277 case 'Memo':
278 return $data;
279
280 case 'Date':
281 // a null date is valid
282 if (strlen(trim($data)) == 0) {
283 return trim($data);
284 }
285
286 if (preg_match('/^\d{8}$/', $data) &&
287 CRM_Utils_Rule::mysqlDate($data)
288 ) {
289 return $data;
290 }
291 break;
292
293 case 'Timestamp':
294 // a null timestamp is valid
295 if (strlen(trim($data)) == 0) {
296 return trim($data);
297 }
298
299 if ((preg_match('/^\d{14}$/', $data) ||
300 preg_match('/^\d{8}$/', $data)
301 ) &&
302 CRM_Utils_Rule::mysqlDate($data)
303 ) {
304 return $data;
305 }
306 break;
307
308 case 'ContactReference':
309 // null is valid
310 if (strlen(trim($data)) == 0) {
311 return trim($data);
312 }
313
314 if (CRM_Utils_Rule::validContact($data)) {
315 return $data;
316 }
317 break;
318
319 default:
320 CRM_Core_Error::fatal("Cannot recognize $type for $data");
321 break;
322 }
323
324 if ($abort) {
325 $data = htmlentities($data);
326 CRM_Core_Error::fatal("$name (value: $data) is not of the type $type");
327 }
328
329 return NULL;
330 }
331 }
332