Skip to content

Commit

Permalink
Remove some duplication in CRM_Utils_Type::escape/validate
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Aug 23, 2019
1 parent 79f7016 commit ab2fa56
Showing 1 changed file with 13 additions and 60 deletions.
73 changes: 13 additions & 60 deletions CRM/Utils/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,22 @@ public static function validateAll($data, $type, $abort = TRUE) {
*
* @return mixed
* The data, escaped if necessary.
* @throws \Exception
*/
public static function escape($data, $type, $abort = TRUE) {
switch ($type) {
case 'Integer':
case 'Int':
if (CRM_Utils_Rule::integer($data)) {
return (int) $data;
}
break;

case 'Positive':
if (CRM_Utils_Rule::positiveInteger($data)) {
return (int) $data;
case 'Float':
case 'Money':
case 'Date':
case 'Timestamp':
case 'ContactReference':
case 'MysqlOrderByDirection':
$validatedData = self::validate($data, $type, $abort);
if (isset($validatedData)) {
return $validatedData;
}
break;

Expand Down Expand Up @@ -287,43 +290,10 @@ public static function escape($data, $type, $abort = TRUE) {
}
break;

case 'Float':
case 'Money':
if (CRM_Utils_Rule::numeric($data)) {
return $data;
}
break;

case 'String':
case 'Memo':
case 'Text':
return CRM_Core_DAO::escapeString($data);

case 'Date':
case 'Timestamp':
// a null date or timestamp is valid
if (strlen(trim($data)) == 0) {
return trim($data);
}

if ((preg_match('/^\d{8}$/', $data) ||
preg_match('/^\d{14}$/', $data)
) &&
CRM_Utils_Rule::mysqlDate($data)
) {
return $data;
}
break;

case 'ContactReference':
if (strlen(trim($data)) == 0) {
return trim($data);
}

if (CRM_Utils_Rule::validContact($data)) {
return (int) $data;
}
break;
return CRM_Core_DAO::escapeString(self::validate($data, $type, $abort));

case 'MysqlColumnNameOrAlias':
if (CRM_Utils_Rule::mysqlColumnNameOrAlias($data)) {
Expand All @@ -335,12 +305,6 @@ public static function escape($data, $type, $abort = TRUE) {
}
break;

case 'MysqlOrderByDirection':
if (CRM_Utils_Rule::mysqlOrderByDirection($data)) {
return strtolower($data);
}
break;

case 'MysqlOrderBy':
if (CRM_Utils_Rule::mysqlOrderBy($data)) {
$parts = explode(',', $data);
Expand Down Expand Up @@ -387,6 +351,7 @@ public static function escape($data, $type, $abort = TRUE) {
// @todo Use exceptions instead of CRM_Core_Error::fatal().
if ($abort) {
$data = htmlentities($data);

CRM_Core_Error::fatal("$data is not of the type $type");
}
return NULL;
Expand Down Expand Up @@ -470,18 +435,6 @@ public static function validate($data, $type, $abort = TRUE, $name = 'One of par
return $data;

case 'Date':
// a null date is valid
if (strlen(trim($data)) == 0) {
return trim($data);
}

if (preg_match('/^\d{8}$/', $data) &&
CRM_Utils_Rule::mysqlDate($data)
) {
return $data;
}
break;

case 'Timestamp':
// a null timestamp is valid
if (strlen(trim($data)) == 0) {
Expand All @@ -504,7 +457,7 @@ public static function validate($data, $type, $abort = TRUE, $name = 'One of par
}

if (CRM_Utils_Rule::validContact($data)) {
return $data;
return (int) $data;
}
break;

Expand Down

0 comments on commit ab2fa56

Please sign in to comment.