3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
35 class CRM_Utils_Type
{
69 * Convert Constant Data type to String
71 * @param $type integer datatype
73 * @return $string String datatype respective to integer datatype
78 static function typeToString($type) {
115 $string = 'Timestamp';
135 $string = 'Mediumblob';
139 return (isset($string)) ?
$string : "";
143 * Verify that a variable is of a given type
145 * @param mixed $data The variable
146 * @param string $type The type
147 * @param boolean $abort Should we abort if invalid
149 * @return mixed The data, escaped if necessary
153 public static function escape($data, $type, $abort = TRUE) {
157 if (CRM_Utils_Rule
::integer($data)) {
163 // the below 2 are for custom fields of this type
166 case 'StateProvince':
167 if (CRM_Utils_Rule
::positiveInteger($data)) {
173 if (CRM_Utils_Rule
::url($data = trim($data))) {
179 if (CRM_Utils_Rule
::boolean($data)) {
186 if (CRM_Utils_Rule
::numeric($data)) {
194 return CRM_Core_DAO
::escapeString($data);
198 // a null date or timestamp is valid
199 if (strlen(trim($data)) == 0) {
203 if ((preg_match('/^\d{8}$/', $data) ||
204 preg_match('/^\d{14}$/', $data)
206 CRM_Utils_Rule
::mysqlDate($data)
212 case 'ContactReference':
213 if (strlen(trim($data)) == 0) {
217 if (CRM_Utils_Rule
::validContact($data)) {
223 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
228 $data = htmlentities($data);
229 CRM_Core_Error
::fatal("$data is not of the type $type");
235 * Verify that a variable is of a given type
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
242 * @return mixed The data, escaped if necessary
246 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
250 if (CRM_Utils_Rule
::integer($data)) {
256 if (CRM_Utils_Rule
::positiveInteger($data)) {
262 if (CRM_Utils_Rule
::boolean($data)) {
269 if (CRM_Utils_Rule
::numeric($data)) {
281 // a null date is valid
282 if (strlen(trim($data)) == 0) {
286 if (preg_match('/^\d{8}$/', $data) &&
287 CRM_Utils_Rule
::mysqlDate($data)
294 // a null timestamp is valid
295 if (strlen(trim($data)) == 0) {
299 if ((preg_match('/^\d{14}$/', $data) ||
300 preg_match('/^\d{8}$/', $data)
302 CRM_Utils_Rule
::mysqlDate($data)
308 case 'ContactReference':
310 if (strlen(trim($data)) == 0) {
314 if (CRM_Utils_Rule
::validContact($data)) {
320 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
325 $data = htmlentities($data);
326 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");