00001 <?php
00017 include_once (OPENBIZ_HOME."/messages/validateService.msg");
00018
00029 class validateService
00030 {
00031 protected $m_ErrorMessage = null;
00032 protected $m_FieldNameMask = "%%FIELDNAME%%";
00033
00040 function __construct(&$xmlArr)
00041 {
00042 $this->readMetadata($xmlArr);
00043 }
00044
00051 protected function readMetadata(&$xmlArr)
00052 {
00053 }
00054
00062 public function shorterThan($value, $max)
00063 {
00064 $this->m_ErrorMessage = null;
00065 $result = false;
00066 if(function_exists("mb_strlen"))
00067 {
00068 if (mb_strlen($value,'UTF-8') < $max)
00069 {
00070 $result = true;
00071 }
00072 }else
00073 {
00074 if (strlen($value) < $max)
00075 {
00076 $result = true;
00077 }
00078 }
00079 if (!$result)
00080 {
00081 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_SHORTER_THAN",array($this->m_FieldNameMask,$max));
00082 }
00083 return $result;
00084 }
00085
00094 public function betweenLength($value, $min, $max)
00095 {
00096 if((int)$min > (int)$max)
00097 {
00098 $tmp = $min;
00099 $min = $max;
00100 $max = $tmp;
00101 }
00102 $this->m_ErrorMessage = null;
00103 $result = false;
00104 if(function_exists("mb_strlen"))
00105 {
00106 if (mb_strlen($value,'UTF-8') <= $max && mb_strlen($value,'UTF-8') >= $min)
00107 {
00108 $result = true;
00109 }
00110 }
00111 else
00112 {
00113 if (strlen($value) <= $max && strlen($value) >= $min)
00114 {
00115 $result = true;
00116 }
00117 }
00118 if (!$result)
00119 {
00120 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_BETWEEN_LENGTH",array($this->m_FieldNameMask,$min,$max));
00121 }
00122 return $result;
00123 }
00124
00132 public function longerThan($value, $min)
00133 {
00134 $this->m_ErrorMessage = null;
00135 $result = false;
00136 if(function_exists("mb_strlen"))
00137 {
00138 if (mb_strlen($value,'UTF-8') > $min)
00139 {
00140 $result = true;
00141 }
00142 }
00143 else
00144 {
00145 if (strlen($value) > $min)
00146 {
00147 $result = true;
00148 }
00149 }
00150 if (!$result)
00151 {
00152 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_LONGER_THAN",array($this->m_FieldNameMask,$min));
00153 }
00154 return $result;
00155 }
00156
00157
00158
00166 public function lessThan($value, $max)
00167 {
00168 $this->m_ErrorMessage = null;
00169 require_once 'Zend/Validate/LessThan.php';
00170 $validator = new Zend_Validate_LessThan($max);
00171 $result = $validator->isValid($value);
00172 if(!$result)
00173 {
00174 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_LESS_THAN",array($this->m_FieldNameMask,$max));
00175 }
00176 return $result;
00177 }
00178
00186 public function strongPassword($value)
00187 {
00188 $this->m_ErrorMessage = null;
00189 require_once 'Zend/Validate/Regex.php';
00190 $validator = new Zend_Validate_Regex("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/");
00191 $result = $validator->isValid($value);
00192 if(!$result)
00193 {
00194 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_PASSWORD_NOT_STRONG",array($this->m_FieldNameMask));
00195 }
00196 return $result;
00197 }
00198
00199
00200
00208 public function greaterThan($value, $min)
00209 {
00210 $this->m_ErrorMessage = null;
00211 require_once 'Zend/Validate/GreaterThan.php';
00212 $validator = new Zend_Validate_GreaterThan($min);
00213 $result = $validator->isValid($value);
00214 if(!$result)
00215 {
00216 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_GREATER_THAN",array($this->m_FieldNameMask, $min));
00217 }
00218 return $result;
00219 }
00220
00231 public function between($value, $min, $max, $inclusive=true)
00232 {
00233 $this->m_ErrorMessage = null;
00234 require_once 'Zend/Validate/Between.php';
00235 $validator = new Zend_Validate_Between($min, $max, $inclusive);
00236 $result = $validator->isValid($value);
00237 if(!$result)
00238 {
00239 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_BETWEEN",array($this->m_FieldNameMask ,$min, $max));
00240 }
00241 return $result;
00242 }
00243
00244
00251 public function email($email)
00252 {
00253 $this->m_ErrorMessage = null;
00254 require_once 'Zend/Validate/EmailAddress.php';
00255
00256
00257
00258
00259
00260
00261 if(preg_match("/^[A-Z0-9_-][A-Z0-9._-]*@([A-Z0-9][A-Z0-9-]*\.)+[A-Z\.]{2,6}$/i",$email)){
00262 $result = true;
00263 }else{
00264 $result = false;
00265 };
00266 if(!$result)
00267 {
00268 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_EMAIL_INVALID",array($this->m_FieldNameMask));
00269 }
00270 return $result;
00271 }
00272
00279 public function date($date)
00280 {
00281 $this->m_ErrorMessage=null;
00282 require_once 'Zend/Validate/Date.php';
00283 $validator = new Zend_Validate_Date();
00284 $result = $validator->isValid($date);
00285 if(!$result)
00286 {
00287 $this->m_ErrorMessage = BizSystem::getMessage("VALIDATESVC_DATE_INVALID",array($this->m_FieldNameMask));
00288 }
00289 return $result;
00290 }
00291
00298 public function phone($phone)
00299 {
00300 $this->m_ErrorMessage = null;
00301 require_once 'Zend/Validate/Regex.php';
00302 $validator = new Zend_Validate_Regex("/^[0-9]{3}-[0-9]{3}-[0-9]{4}/");
00303 $result = $validator->isValid($phone);
00304 if(!$result)
00305 {
00306 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_PHONE_INVALID",array($this->m_FieldNameMask));
00307 }
00308 return $result;
00309 }
00310
00317 public function zip($zip)
00318 {
00319 $this->m_ErrorMessage = null;
00320 require_once 'Zend/Validate/Regex.php';
00321 $validator = new Zend_Validate_Regex("/^[0-9]{5,5}([- ]?[0-9]{4,4})?$/");
00322 $result = $validator->isValid($zip);
00323 if(!$result)
00324 {
00325 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_ZIP_INVALID",array($this->m_FieldNameMask));
00326 }
00327 return $result;
00328 }
00329
00336 public function social($social)
00337 {
00338 $this->m_ErrorMessage = null;
00339 require_once 'Zend/Validate/Regex.php';
00340 $validator = new Zend_Validate_Regex("\b[0-9]{3}-[0-9]{2}-[0-9]{4}\b");
00341 $result = $validator->isValid($social);
00342 if(!$result)
00343 {
00344 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_SOCIAL_INVALID",array($this->m_FieldNameMask));
00345 }
00346 return $result;
00347 }
00348
00355 public function credit($credit)
00356 {
00357 $this->m_ErrorMessage = null;
00358 require_once 'Zend/Validate/Regex.php';
00359 $validator = new Zend_Validate_Regex("^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|");
00360 $result = $validator->isValid($credit);
00361 if(!$result)
00362 {
00363 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_CREDIT_INVALID",array($this->m_FieldNameMask));
00364 }
00365 return $result;
00366 }
00367
00374 public function street($street)
00375 {
00376 $this->m_ErrorMessage = null;
00377 require_once 'Zend/Validate/Regex.php';
00378 $validator = new Zend_Validate_Regex("");
00379 $result = $validator->isValid($street);
00380 if(!$result)
00381 {
00382 $this->m_ErrorMessage= BizSystem::getMessage("VALIDATESVC_STREET_INVALID",array($this->m_FieldNameMask));
00383 }
00384 return $result;
00385 }
00386
00394 public function getErrorMessage($validator=null, $fieldName=null)
00395 {
00396 if($this->m_ErrorMessage != "")
00397 {
00398 if($fieldName != "")
00399 {
00400 $this->m_ErrorMessage = str_replace($this->m_FieldNameMask,
00401 $fieldName,
00402 $this->m_ErrorMessage);
00403 }
00404 return $this->m_ErrorMessage;
00405 }
00406 else
00407 {
00408 $validator = str_replace('{@validate:', '', $validator);
00409 $pos1 = strpos($validator, '(');
00410 $type = substr($validator, 0, $pos1);
00411
00412 switch ($type)
00413 {
00414 case "date":
00415 return BizSystem::getMessage("VALIDATESVC_DATE_INVALID",array($fieldName));
00416 break;
00417 case "email":
00418 return BizSystem::getMessage("VALIDATESVC_EMAIL_INVALID",array($fieldName));
00419 break;
00420 case "phone":
00421 return BizSystem::getMessage("VALIDATESVC_PHONE_INVALID",array($fieldName));
00422 break;
00423 case "zip":
00424 return BizSystem::getMessage("VALIDATESVC_ZIP_INVALID",array($fieldName));
00425 break;
00426 case "social":
00427 return BizSystem::getMessage("VALIDATESVC_SOCIAL_INVALID",array($fieldName));
00428 break;
00429 case "credit":
00430 return BizSystem::getMessage("VALIDATESVC_CREDIT_INVALID",array($fieldName));
00431 break;
00432 case "street":
00433 return BizSystem::getMessage("VALIDATESVC_STREET_INVALID",array($fieldName));
00434 break;
00435 case "strongPassword":
00436 return BizSystem::getMessage("VALIDATESVC_PASSWORD_NOT_STRONG",array($fieldName));
00437 break;
00438 }
00439 return BizSystem::getMessage("VALIDATESVC_INVALID",array($fieldName));
00440 }
00441 }
00442 }
00443
00444 ?>