Exemplo n.º 1
0
 public function validate($checkMode = 'all', $requireMode = 'null')
 {
     //You do not need this if you extend DooModel or DooSmartModel
     //MODE: all, all_one, skip
     Doo::loadHelper('DooValidator');
     $v = new DooValidator();
     $v->checkMode = $checkMode;
     $v->requiredMode = $requireMode;
     return $v->validate(get_object_vars($this), $this->getVRules());
 }
Exemplo n.º 2
0
 /**
  * Validate form
  *
  * @var array Values for form, for example $_POST
  *
  * @return boolean True or false if form is not valid
  */
 public function isValid($values)
 {
     $valid = true;
     $errors = array();
     try {
         Doo::loadHelper('DooValidator');
         $v = new DooValidator();
     } catch (DooFormException $e) {
         echo 'Validator class coulndt be loaded ' . $e->getMessage() . '\\n';
     }
     $formElements = $this->_formElements;
     $elementValues = array();
     foreach ($this->_elements as $element => $e) {
         // handle values
         if (isset($values[$element])) {
             $elementValues[$element] = $values[$element];
         }
         // handle validators
         if (isset($e[1]['validators'])) {
             if (!isset($e[1]['required']) || $e[1]['required'] != false) {
                 $elementRules = array($element => $e[1]['validators']);
                 $errors[$element] = $v->validate($values, $elementRules);
                 if ($errors[$element]) {
                     unset($elementValues[$element]);
                 }
             } else {
                 if (isset($elementValues[$element]) && $elementValues[$element] != "") {
                     $elementRules = array($element => $e[1]['validators']);
                     $errors[$element] = $v->validate($values, $elementRules);
                 }
             }
         }
         // handle captcha
         if (isset($e[0]) && $e[0] == 'captcha') {
             $sessionData = isset($_SESSION['doo_captcha_' . $element]) ? md5($_SESSION['doo_captcha_' . $element]) : '';
             $msg = isset($e[1]['message']) ? $e[1]['message'] : null;
             $elementRules = array($element => array('equal', $sessionData, $msg));
             $values[$element] = md5($values[$element]);
             $errors[$element] = $v->validate($values, $elementRules);
             if ($errors[$element]) {
                 unset($elementValues[$element]);
             }
             // delete captcha if captcha is good
             if (isset($e[1]['url']) && file_exists($e[1]['directory'] . '/' . $sessionData . ".jpg")) {
                 unlink($e[1]['directory'] . '/' . $sessionData . ".jpg");
             }
         }
         // handle file
         if (isset($e[0]) && $e[0] == 'file') {
             // if there is file check if file exists
             if (isset($_FILES[$element])) {
                 // check file extension
                 if (isset($e[1]['extension'])) {
                     $extensions = array();
                     $extension = substr($_FILES[$element]['name'], strrpos($_FILES[$element]['name'], '.') + 1);
                     $extensions = explode(',', $e[1]['extension']);
                     if (!in_array($extension, $extensions)) {
                         $errors[$element] = array('File must have ' . $e[1]['extension'] . ' extension.');
                     }
                 }
                 // check file size
                 if (isset($e[1]['size'])) {
                     if ($e[1]['size'] < $_FILES[$element]['size']) {
                         $errors[$element] = array('File is too big!');
                     }
                 }
             } else {
                 if (isset($e[1]['required']) && $e[1]['required'] == 1) {
                     $errors[$element][] = array('File for upload is required.');
                 }
             }
         }
     }
     // set values
     $this->_elementValues = $elementValues;
     if (count($errors) > 0) {
         $this->_errors = $errors;
         foreach ($errors as $error => $e) {
             if (!empty($e)) {
                 $valid = false;
             }
         }
     }
     return $valid;
 }
Exemplo n.º 3
0
 /**
  * Validate the Model with the rules defined in getVRules()
  *
  * @param object $model Model object to be validated.
  * @param string $checkMode Validation mode. all, all_one, skip
  * @param string $requireMode Require Check Mode. null, nullempty
  * @return array Return array of errors if exists. Return null if data passes the validation rules.
  */
 public static function _validate($model, $checkMode = 'all', $requireMode = 'null')
 {
     //all, all_one, skip
     Doo::loadHelper('DooValidator');
     $v = new DooValidator();
     $v->checkMode = $checkMode;
     $v->requiredMode = $requireMode;
     return $v->validate(get_object_vars($model), $model->getVRules());
 }
Exemplo n.º 4
0
Arquivo: User.php Projeto: aising/ding
 /**
  * 用户登录
  * @param string $account
  * @param string $passwd
  * @param string $safecode
  * @return int
  */
 public function login($account, $passwd, $safecode = '', $lang = 'zh', $remember = 0)
 {
     Doo::loadHelper('DooValidator');
     $v = new DooValidator();
     $success = TRUE;
     $userinfo = $errors = array();
     $postData = array('username' => $account, 'password' => $passwd, 'captcha' => $safecode, 'lang' => $lang);
     //D($postData);
     $rules = array('username' => array(array('maxlength', 20), array('notnull')), 'password' => array(array('maxlength', 20), array('notnull')), 'captcha' => array(array('notnull'), array('custom', 'User::vSafecode')), 'lang' => array(array('notnull'), array('inList', array_keys(Doo::conf()->langList))));
     if ($this->_checkSafecode == FALSE) {
         unset($rules['captcha']);
     }
     if ($errors = $v->validate($postData, $rules)) {
         $success = FALSE;
     }
     // 无论登录是否正确都删除验证码
     unset($_SESSION['safe_code']);
     if ($success) {
         //根据用户账号查数据库获取用户
         $param = array('username' => $account);
         $result = DBproxy::getProcedure('Manage')->setDimension(2)->spSysUserSSign($param);
         if (!empty($result)) {
             $userinfo = $result;
             $userinfo['uname'] = $userinfo['username'] = $account;
         } else {
             $success = FALSE;
             $errors['username'] = '******';
         }
     }
     if ($success) {
         if ($userinfo['passwd'] != $this->password($passwd)) {
             $success = FALSE;
             $errors['password'] = '******';
         }
     }
     //用户锁定
     if ($success) {
         if ($userinfo['is_locked'] == 1) {
             $success = FALSE;
             $errors['username'] = '******';
         }
     }
     //用户验证
     if ($success) {
         if ($userinfo['is_check'] == 0) {
             $success = FALSE;
             $errors['username'] = '******';
         }
     }
     if ($success) {
         //取所属角色(s)
         $userinfo['roleids'] = $this->get_user_roles($userinfo['uid']);
         $this->userinfo = $userinfo;
         //最后做是否验证的检查
         if ($userinfo['is_check'] == 1) {
             $_SESSION['userinfo'] = $this->userinfo;
             //写权限到$_SESSION['authory']
             $this->authorityInSession();
             $this->insert_login_log($userinfo['uid']);
         } else {
             $_SESSION['userinfo_tmp'] = $this->userinfo;
         }
     }
     if ($success && $remember) {
         @setcookie($this->_rememberKey, encrypt(getIp() . '|' . $account . '|' . $passwd . '|' . $lang, Doo::conf()->KEY_PASSWORD), time() + 30 * 86400, '/');
     }
     return array('success' => $success, 'errors' => $errors);
 }
 function saveNewPost()
 {
     Doo::loadHelper('DooValidator');
     $_POST['content'] = trim($_POST['content']);
     //get defined rules and add show some error messages
     $validator = new DooValidator();
     $validator->checkMode = DooValidator::CHECK_SKIP;
     if ($error = $validator->validate($_POST, 'post_create.rules')) {
         $data['rootUrl'] = Doo::conf()->APP_URL;
         $data['title'] = 'Error Occured!';
         $data['content'] = '<p style="color:#ff0000;">' . $error . '</p>';
         $data['content'] .= '<p>Go <a href="javascript:history.back();">back</a> to edit.</p>';
         $this->render('admin_msg', $data);
     } else {
         Doo::loadModel('Post');
         Doo::loadModel('Tag');
         Doo::autoload('DooDbExpression');
         $p = new Post($_POST);
         $p->createtime = new DooDbExpression('NOW()');
         //insert the post along with the tags
         if (self::$tags != Null) {
             $tags = array();
             foreach (self::$tags as $t) {
                 $tg = new Tag();
                 $tg->name = $t;
                 $tags[] = $tg;
             }
             $id = $p->relatedInsert($tags);
         } else {
             $id = $p->insert();
         }
         //clear the sidebar cache
         Doo::cache('front')->flushAllParts();
         $data['rootUrl'] = Doo::conf()->APP_URL;
         $data['title'] = 'Post Created!';
         $data['content'] = '<p>Your post is created successfully!</p>';
         if ($p->status == 1) {
             $data['content'] .= '<p>Click  <a href="' . $data['rootUrl'] . 'article/' . $id . '">here</a> to view the published post.</p>';
         }
         $this->render('admin_msg', $data);
     }
 }