Example #1
0
 public static function _doValidate($data = '')
 {
     //get $_POST then do validate and auto then return an array
     //status 0 success   1 error  3  redirect  4   //redirect do by js
     $return = array('data' => '', 'info' => '', 'status' => '0', 'type' => 'json');
     if (empty($data)) {
         $data = $_POST;
     } elseif (is_object($data)) {
         $data = get_object_vars($data);
     } elseif (!is_array($data)) {
         $return['info'] = 'DATA_TYPE_INVAILD';
         $return['status'] = 1;
         return $return;
     }
     //logTrace(__CLASS__.'/'.__FUNCTION__.':show post data         '.implode(',', $data));
     //map the filed ,get the target fields
     $check = C(APP_NAME . 'FORMS');
     $checkform = $check[$_POST['form']];
     //get form validate filed configuration
     //logDebug($_POST['form'].":".json_encode($checkform));
     $validateField = $checkform['check'];
     $r = true;
     //return the result data
     $vo = array();
     if (!empty($checkform['fields'])) {
         foreach ($checkform['fields'] as $key => $val) {
             if (isset($data[$key])) {
                 $vo[$val] = $data[$key];
             } else {
                 //merge the form value
                 if (isset($checkform['value'][$val])) {
                     $vo[$val] = $checkform['value'][$val];
                 } else {
                     $vo[$val] = '';
                 }
             }
         }
     }
     //logTrace(__CLASS__.'/'.__FUNCTION__.':do check field ok'.implode(',', $vo));
     //do validate
     if (!empty($validateField) && !empty($vo)) {
         foreach ($validateField as $key => $val) {
             switch ($val[0]) {
                 case 1:
                     if (isset($vo[$key])) {
                         $do = self::regex($vo[$key], $val[1]);
                     } else {
                         //logNotice(__CLASS__.'/'.__FUNCTION__.'%s NOT EXIST IN FORM PLEASE CHECK FORMS.CONFIG',$key);
                     }
                     if ($do !== $r) {
                         $return['info'] = $val[2];
                         $return['status'] = 1;
                         //if get error then what will do ?
                         if ($checkform['return'] == 'json') {
                             header("Content-Type:text/html; charset=utf-8");
                             exit(json_encode($return));
                         } else {
                             header("Content-Type:text/xml; charset=utf-8");
                             exit(xml_encode($return));
                         }
                     }
                     break;
                 case 2:
                     if (!empty($vo[$key])) {
                         $do = self::regex($vo[$key], $val[1]);
                         if ($do !== $r) {
                             $return['info'] = $val[2];
                             $return['status'] = 1;
                             if ($checkform['return'] == 'json') {
                                 header("Content-Type:text/html; charset=utf-8");
                                 exit(json_encode($return));
                             } elseif ($checkform['return'] == 'xml') {
                                 header("Content-Type:text/xml; charset=utf-8");
                                 exit(xml_encode($return));
                             }
                         }
                     }
             }
         }
     }
     //logTrace(__CLASS__.'/'.__FUNCTION__.': do validate ok '.implode(',', $vo));
     //do auto
     $auto = $checkform['auto'];
     if (!empty($auto)) {
         foreach ($auto as $key => $val) {
             $str = '';
             switch ($val) {
                 case 'objid':
                     $str = objid();
                     break;
                 case 'md5':
                     if (!empty($_POST[$key])) {
                         $str = md5($_POST[$key]);
                     }
                     break;
                 case 'time':
                     $str = time();
                     break;
                 case 'getip':
                     $str = getip();
                     break;
                 case 'userid':
                     $str = userID();
                     break;
                 case 'module':
                     $str = MODULE_NAME;
                     break;
                 case 'action':
                     $str = ACTION_NAME;
             }
             $vo[$key] = $str;
         }
     }
     //logTrace(__CLASS__.'/'.__FUNCTION__.': do auto ok  '.implode(',', $vo));
     //var_dump($vo,$checkform['api']);die;
     //得到数据调用方法进行处理
     $r = call_user_func_array($checkform['api'], array($vo));
     //var_dump($r);die;
     //请求失败返回false  成功则返回结果$r
     if ($r === false) {
         $return['status'] = '1';
         $return['info'] = 'sys error,please waite';
         $return['data'] = '';
         $return['type'] = 'json';
         if (!$checkform['ajax']) {
             jump(U('Public/404'));
         } else {
             header("Content-Type:text/html; charset=utf-8");
             exit(json_encode($return));
         }
     } else {
         //set session
         //logTrace(__CLASS__.'/'.__FUNCTION__.':'.implode(',', $vo));
         //@TODO : NEED CHANGE
         if (!empty($checkform['callback'])) {
             //set session
             $method = $checkform['callback'];
             $method($r);
         }
         //redirect to the page
         if (!$checkform['ajax'] && isset($checkform['next'])) {
             if (is_int(strpos($checkform['next'], ':code')) && isset($r['code'])) {
                 //根据返回来拼接URL跳转到目标地址
                 $url = !empty($r['uri']) ? U($r['uri']) . '?code=' . $r['code'] : '';
                 $checkform['next'] = empty($url) ? str_replace(':code', $r['code'], $checkform['next']) : $url;
             }
             if (is_int(strpos($checkform['next'], ':id')) && isset($r['id'])) {
                 $checkform['next'] = str_replace(':id', $r['id'], $checkform['next']);
             }
             //先判断当前参数中是否含有地址链接
             jump(U($checkform['next']));
         } elseif ($checkform['ajax']) {
             //输出
             header("Content-Type:text/html; charset=utf-8");
             //定义这里包含的状态status,info,data
             if (isset($r['code']) && isset($r['data'])) {
                 //@TODO 这里报错需要提示验证码
                 //$msg = Error::getMsg($r['code']);
                 $info = !empty($msg) ? $msg : '';
                 $arr = array('status' => $r['code'], 'info' => $info, 'data' => $r['data']);
                 exit(json_encode($arr));
             }
         }
     }
     return $return;
 }
Example #2
0
 /**
  * save file or image to upaiyun.com then update db
  * 先保存数据到又拍云存储,然后在更新数据库
  * @param string $model
  * @param string $filename   保存文件名
  * @param mixed $streamFile  流文件或者是图片路径
  * @param bigint $id ID
  * @param bigint $user_id userid
  */
 public function save($account_id, $filename = '', $streamFile = '', $id = '')
 {
     $result = $this->put('image', $filename, $streamFile);
     logDebug('UPLOAD FILE TO UPAI ' . json_encode($result));
     $id = empty($id) ? objid() : $id;
     $filename = empty($filename) ? $id . '.jpg' : $filename;
     if ($result['code'] == 200) {
         return ImageLogic::setImage($id, $account_id, $filename, 'web');
     }
     logNotice(__CLASS__ . '/' . __FUNCTION__ . ':UPLOAD IMAGE ERROR PATH:%s', $filename);
     return false;
 }