CRM-15598 - Allow relative URL's for Link custom fields
[civicrm-core.git] / CRM / Utils / Rule.php
index 89ee7718657ea599da2da4898d4e2ef3d8638c8e..d58c86a63725a6f44edfb117be75017e1ce6622d 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
 
 require_once 'HTML/QuickForm/Rule/Email.php';
 
+/**
+ * Class CRM_Utils_Rule
+ */
 class CRM_Utils_Rule {
 
+  /**
+   * @param $str
+   * @param int $maxLength
+   *
+   * @return bool
+   */
   static function title($str, $maxLength = 127) {
 
     // check length etc
@@ -52,10 +61,20 @@ class CRM_Utils_Rule {
     return TRUE;
   }
 
+  /**
+   * @param $str
+   *
+   * @return bool
+   */
   static function longTitle($str) {
     return self::title($str, 255);
   }
 
+  /**
+   * @param $str
+   *
+   * @return bool
+   */
   static function variable($str) {
     // check length etc
     if (empty($str) || strlen($str) > 31) {
@@ -70,6 +89,11 @@ class CRM_Utils_Rule {
     return TRUE;
   }
 
+  /**
+   * @param $str
+   *
+   * @return bool
+   */
   static function qfVariable($str) {
     // check length etc
     //if ( empty( $str ) || strlen( $str ) > 31 ) {
@@ -86,6 +110,11 @@ class CRM_Utils_Rule {
     return TRUE;
   }
 
+  /**
+   * @param $phone
+   *
+   * @return bool
+   */
   static function phone($phone) {
     // check length etc
     if (empty($phone) || strlen($phone) > 16) {
@@ -99,6 +128,11 @@ class CRM_Utils_Rule {
     return FALSE;
   }
 
+  /**
+   * @param $query
+   *
+   * @return bool
+   */
   static function query($query) {
     // check length etc
     if (empty($query) || strlen($query) < 3 || strlen($query) > 127) {
@@ -113,15 +147,34 @@ class CRM_Utils_Rule {
     return TRUE;
   }
 
+  /**
+   * @param $url
+   *
+   * @return bool
+   */
   static function url($url) {
+    if (preg_match('/^\//', $url)) {
+      // allow relative URL's (CRM-15598)
+      $url = 'http://' . $_SERVER['HTTP_HOST'] . $url;
+    }
     return (bool) filter_var($url, FILTER_VALIDATE_URL);
   }
 
+  /**
+   * @param $string
+   *
+   * @return bool
+   */
   static function wikiURL($string) {
     $items = explode(' ', trim($string), 2);
     return self::url($items[0]);
   }
 
+  /**
+   * @param $domain
+   *
+   * @return bool
+   */
   static function domain($domain) {
     // not perfect, but better than the previous one; see CRM-1502
     if (!preg_match('/^[A-Za-z0-9]([A-Za-z0-9\.\-]*[A-Za-z0-9])?$/', $domain)) {
@@ -130,6 +183,12 @@ class CRM_Utils_Rule {
     return TRUE;
   }
 
+  /**
+   * @param $value
+   * @param null $default
+   *
+   * @return null
+   */
   static function date($value, $default = NULL) {
     if (is_string($value) &&
       preg_match('/^\d\d\d\d-?\d\d-?\d\d$/', $value)
@@ -139,6 +198,12 @@ class CRM_Utils_Rule {
     return $default;
   }
 
+  /**
+   * @param $value
+   * @param null $default
+   *
+   * @return null|string
+   */
   static function dateTime($value, $default = NULL) {
     $result = $default;
     if (is_string($value) &&
@@ -151,7 +216,7 @@ class CRM_Utils_Rule {
   }
 
   /**
-   * check the validity of the date (in qf format)
+   * Check the validity of the date (in qf format)
    * note that only a year is valid, or a mon-year is
    * also valid in addition to day-mon-year. The date
    * specified has to be beyond today. (i.e today or later)
@@ -244,7 +309,7 @@ class CRM_Utils_Rule {
   }
 
   /**
-   * check the validity of a date or datetime (timestamp)
+   * Check the validity of a date or datetime (timestamp)
    * value which is in YYYYMMDD or YYYYMMDDHHMMSS format
    *
    * Uses PHP checkdate() - params are ( int $month, int $day, int $year )
@@ -268,6 +333,11 @@ class CRM_Utils_Rule {
     return FALSE;
   }
 
+  /**
+   * @param $value
+   *
+   * @return bool
+   */
   static function integer($value) {
     if (is_int($value)) {
       return TRUE;
@@ -296,6 +366,11 @@ class CRM_Utils_Rule {
     return FALSE;
   }
 
+  /**
+   * @param $value
+   *
+   * @return bool
+   */
   static function positiveInteger($value) {
     if (is_int($value)) {
       return ($value < 0) ? FALSE : TRUE;
@@ -314,6 +389,11 @@ class CRM_Utils_Rule {
     return FALSE;
   }
 
+  /**
+   * @param $value
+   *
+   * @return bool
+   */
   static function numeric($value) {
     // lets use a php gatekeeper to ensure this is numeric
     if (!is_numeric($value)) {
@@ -323,16 +403,36 @@ class CRM_Utils_Rule {
     return preg_match('/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/', $value) ? TRUE : FALSE;
   }
 
+  /**
+   * @param $value
+   * @param $noOfDigit
+   *
+   * @return bool
+   */
   static function numberOfDigit($value, $noOfDigit) {
     return preg_match('/^\d{' . $noOfDigit . '}$/', $value) ? TRUE : FALSE;
   }
 
+  /**
+   * @param $value
+   *
+   * @return mixed
+   */
   static function cleanMoney($value) {
     // first remove all white space
     $value = str_replace(array(' ', "\t", "\n"), '', $value);
 
     $config = CRM_Core_Config::singleton();
 
+    //CRM-14868 
+    $currencySymbols = CRM_Core_PseudoConstant::get(
+                          'CRM_Contribute_DAO_Contribution',
+                          'currency', array(
+                                           'keyColumn' => 'name',
+                                           'labelColumn' => 'symbol'
+                                            ));
+    $value = str_replace($currencySymbols,'',$value);
+
     if ($config->monetaryThousandSeparator) {
       $mon_thousands_sep = $config->monetaryThousandSeparator;
     }
@@ -358,6 +458,11 @@ class CRM_Utils_Rule {
     return $value;
   }
 
+  /**
+   * @param $value
+   *
+   * @return bool
+   */
   static function money($value) {
     $config = CRM_Core_Config::singleton();
 
@@ -381,6 +486,12 @@ class CRM_Utils_Rule {
     return preg_match('/(^-?\d+\.\d?\d?$)|(^-?\.\d\d?$)/', $value) ? TRUE : FALSE;
   }
 
+  /**
+   * @param $value
+   * @param int $maxLength
+   *
+   * @return bool
+   */
   static function string($value, $maxLength = 0) {
     if (is_string($value) &&
       ($maxLength === 0 || strlen($value) <= $maxLength)
@@ -390,16 +501,31 @@ class CRM_Utils_Rule {
     return FALSE;
   }
 
+  /**
+   * @param $value
+   *
+   * @return bool
+   */
   static function boolean($value) {
     return preg_match(
       '/(^(1|0)$)|(^(Y(es)?|N(o)?)$)|(^(T(rue)?|F(alse)?)$)/i', $value
     ) ? TRUE : FALSE;
   }
 
+  /**
+   * @param $value
+   *
+   * @return bool
+   */
   static function email($value) {
     return (bool) filter_var($value, FILTER_VALIDATE_EMAIL);
   }
 
+  /**
+   * @param $list
+   *
+   * @return bool
+   */
   static function emailList($list) {
     $emails = explode(',', $list);
     foreach ($emails as $email) {
@@ -414,6 +540,11 @@ class CRM_Utils_Rule {
   // allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or
   // if u disregard the first 0, 4 (thanx excel!)
   // FIXME: we need to figure out how to localize such rules
+  /**
+   * @param $value
+   *
+   * @return bool
+   */
   static function postalCode($value) {
     if (preg_match('/^\d{4,6}(-\d{4})?$/', $value)) {
       return TRUE;
@@ -422,7 +553,7 @@ class CRM_Utils_Rule {
   }
 
   /**
-   * see how file rules are written in HTML/QuickForm/file.php
+   * See how file rules are written in HTML/QuickForm/file.php
    * Checks to make sure the uploaded file is ascii
    *
    * @param     array     Uploaded file info (from $_FILES)
@@ -469,7 +600,7 @@ class CRM_Utils_Rule {
   }
 
   /**
-   * see how file rules are written in HTML/QuickForm/file.php
+   * See how file rules are written in HTML/QuickForm/file.php
    * Checks to make sure the uploaded file is html
    *
    * @param     array     Uploaded file info (from $_FILES)
@@ -505,21 +636,44 @@ class CRM_Utils_Rule {
     return CRM_Core_DAO::objectExists($value, CRM_Utils_Array::value(0, $options), CRM_Utils_Array::value(1, $options), CRM_Utils_Array::value(2, $options, $name));
   }
 
+  /**
+   * @param $value
+   * @param $options
+   *
+   * @return bool
+   */
   static function optionExists($value, $options) {
     return CRM_Core_OptionValue::optionExists($value, $options[0], $options[1], $options[2], CRM_Utils_Array::value(3, $options, 'name'));
   }
 
+  /**
+   * @param $value
+   * @param $type
+   *
+   * @return bool
+   */
   static function creditCardNumber($value, $type) {
     require_once 'Validate/Finance/CreditCard.php';
     return Validate_Finance_CreditCard::number($value, $type);
   }
 
+  /**
+   * @param $value
+   * @param $type
+   *
+   * @return bool
+   */
   static function cvv($value, $type) {
     require_once 'Validate/Finance/CreditCard.php';
 
     return Validate_Finance_CreditCard::cvv($value, $type);
   }
 
+  /**
+   * @param $value
+   *
+   * @return bool
+   */
   static function currencyCode($value) {
     static $currencyCodes = NULL;
     if (!$currencyCodes) {
@@ -531,6 +685,11 @@ class CRM_Utils_Rule {
     return FALSE;
   }
 
+  /**
+   * @param $value
+   *
+   * @return bool
+   */
   static function xssString($value) {
     if (is_string($value)) {
       return preg_match('!<(vb)?script[^>]*>.*</(vb)?script.*>!ims',
@@ -542,10 +701,21 @@ class CRM_Utils_Rule {
     }
   }
 
+  /**
+   * @param $path
+   *
+   * @return bool
+   */
   static function fileExists($path) {
     return file_exists($path);
   }
 
+  /**
+   * @param $value
+   * @param $options
+   *
+   * @return bool
+   */
   static function autocomplete($value, $options) {
     if ($value) {
       $selectOption = CRM_Core_BAO_CustomOption::valuesByID($options['fieldID'], $options['optionGroupID']);
@@ -557,6 +727,12 @@ class CRM_Utils_Rule {
     return TRUE;
   }
 
+  /**
+   * @param $value
+   * @param null $actualElementValue
+   *
+   * @return bool
+   */
   static function validContact($value, $actualElementValue = NULL) {
     if ($actualElementValue) {
       $value = $actualElementValue;
@@ -569,7 +745,7 @@ class CRM_Utils_Rule {
   }
 
   /**
-   * check the validity of the date (in qf format)
+   * Check the validity of the date (in qf format)
    * note that only a year is valid, or a mon-year is
    * also valid in addition to day-mon-year
    *
@@ -621,6 +797,11 @@ class CRM_Utils_Rule {
     return FALSE;
   }
 
+  /**
+   * @param $key
+   *
+   * @return bool
+   */
   static function qfKey($key) {
     return ($key) ? CRM_Core_Key::valid($key) : FALSE;
   }