Example #1
0
 /**
  * 创建数据对象 但不保存到数据库
  * @access public
  * @param mixed $data 创建数据
  * @param string $type 状态
  * @return mixed
  * @throws \think\Exception
  */
 public function create($data = '', $type = '')
 {
     // 如果没有传值默认取POST数据
     if (empty($data)) {
         $data = \think\Input::post();
     } elseif (is_object($data)) {
         $data = get_object_vars($data);
     }
     // 验证数据
     if (empty($data) || !is_array($data)) {
         throw new Exception('invalid data type');
     }
     // 状态
     $type = $type ? $type : (!empty($data[$this->getPk()]) ? self::MODEL_UPDATE : self::MODEL_INSERT);
     // 检测提交字段的合法性
     if (isset($this->options['field'])) {
         // $this->field('field1,field2...')->create()
         $fields = $this->options['field'];
         unset($this->options['field']);
     } elseif (self::MODEL_INSERT == $type && isset($this->insertFields)) {
         $fields = $this->insertFields;
     } elseif (self::MODEL_UPDATE == $type && isset($this->updateFields)) {
         $fields = $this->updateFields;
     }
     if (isset($fields)) {
         if (is_string($fields)) {
             $fields = explode(',', $fields);
         }
         foreach ($data as $key => $val) {
             if (!in_array($key, $fields)) {
                 unset($data[$key]);
             }
         }
     }
     // 过滤创建的数据
     $this->_create_filter($data);
     // 赋值当前数据对象
     $this->data = $data;
     // 返回创建的数据以供其他调用
     return $data;
 }
Example #2
0
 /**
  * 获取上传的文件信息
  * @access public
  * @param string $name 名称
  * @return null|array|\think\File
  */
 public function file($name = '')
 {
     return Input::file($name, $this->file ?: $_FILES);
 }
Example #3
0
File: Model.php Project: klsf/kldns
 /**
  * 创建数据对象 但不保存到数据库
  * @access public
  * @param mixed $data 创建数据
  * @return mixed
  * @throws \think\Exception
  */
 public function create($data = '')
 {
     // 如果没有传值默认取POST数据
     if (empty($data)) {
         $data = \think\Input::post();
     } elseif (is_object($data)) {
         $data = get_object_vars($data);
     }
     // 验证数据
     if (empty($data) || !is_array($data)) {
         throw new Exception('invalid data type');
     }
     // 检测提交字段的合法性
     if (isset($this->options['field'])) {
         // $this->field('field1,field2...')->create()
         $fields = $this->options['field'];
         unset($this->options['field']);
         if (is_string($fields)) {
             $fields = explode(',', $fields);
         }
         foreach ($data as $key => $val) {
             if (!in_array($key, $fields)) {
                 unset($data[$key]);
             }
         }
     }
     // 数据自动验证
     if (!$this->dataValidate($data)) {
         return false;
     }
     // 数据自动填充
     $this->dataFill($data);
     // 过滤创建的数据
     $this->_create_filter($data);
     // 赋值当前数据对象
     $this->data = $data;
     // 返回创建的数据以供其他调用
     return $data;
 }
Example #4
0
 /**
  * 创建数据对象 但不保存到数据库
  * @access public
  * @param mixed $data 创建数据
  * @param string $type 状态
  * @return mixed
  */
 public function create($data = '', $type = '')
 {
     // 如果没有传值默认取POST数据
     if (empty($data)) {
         $data = \think\Input::post();
     } elseif (is_object($data)) {
         $data = get_object_vars($data);
     }
     // 验证数据
     if (empty($data) || !is_array($data)) {
         $this->error = Lang::get('_DATA_TYPE_INVALID_');
         return false;
     }
     $pk = $this->getPk();
     // 状态
     $type = $type ? $type : (is_string($pk) && !empty($data[$pk]) ? self::MODEL_UPDATE : self::MODEL_INSERT);
     $type = 1 << $type - 1;
     // 字段列表
     $keys = array_keys($data);
     // 检测提交字段的合法性
     if (isset($this->options['field'])) {
         // $this->field('field1,field2...')->create()
         $fields = $this->options['field'];
         unset($this->options['field']);
     } elseif (self::MODEL_INSERT == $type && isset($this->insertFields)) {
         $fields = $this->insertFields;
     } elseif (self::MODEL_UPDATE == $type && isset($this->updateFields)) {
         $fields = $this->updateFields;
     }
     if (isset($fields)) {
         if (is_string($fields)) {
             $fields = explode(',', $fields);
         }
         // 判断令牌验证字段
         if (Config::get('token_on')) {
             $fields[] = Config::get('token_name');
         }
         foreach ($keys as $i => $key) {
             if (!in_array($key, $fields)) {
                 unset($keys[$i]);
                 unset($data[$key]);
             }
         }
     }
     // 数据自动验证
     if (!$this->_autoValidation($data, $type)) {
         return false;
     }
     // 验证完成生成数据对象
     if (empty($this->options['link'])) {
         // 开启字段检测并且没有关联表 则过滤非法字段数据
         $fields = $this->getFields();
         foreach ($keys as $i => $key) {
             if (!in_array($key, $fields)) {
                 unset($data[$key]);
             }
         }
     }
     // 创建完成对数据进行自动处理
     $this->_autoOperation($data, $type);
     // 验证后的回调方法
     $this->_after_create($data, $this->options);
     // 赋值当前数据对象
     $this->data = $data;
     // 返回创建的数据以供其他调用
     return $data;
 }
Example #5
0
 public function testFilterMerge()
 {
     Input::setFilter('htmlspecialchars');
     $input = ['a' => ' test<> ', 'b' => '<b\\ar />'];
     $this->assertEquals(' test<> ', Input::data('a', '', '', false, $input));
     $filters = ['trim'];
     $this->assertEquals('test<>', Input::data('a', '', $filters, false, $input));
     $this->assertEquals('test&lt;&gt;', Input::data('a', '', $filters, true, $input));
     $filters = 'stripslashes';
     $this->assertEquals("&lt;bar /&gt;", Input::data('b', '', $filters, true, $input));
 }
Example #6
0
 public function testSuperglobals()
 {
     Input::$filter = 'trim';
     $_GET['get'] = 'get value ';
     $this->assertEquals('get value', Input::get('get'));
     $_POST['post'] = 'post value ';
     $this->assertEquals('post value', Input::post('post'));
     $_SERVER['REQUEST_METHOD'] = 'POST';
     $this->assertEquals('post value', Input::param('post'));
     $this->assertEquals(null, Input::param('get'));
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $this->assertEquals('get value', Input::param('get'));
     $this->assertEquals(null, Input::param('post'));
     session_start();
     $_SESSION['test'] = 'session value ';
     $this->assertEquals('session value', Input::session('test'));
     session_destroy();
     $_COOKIE['cookie'] = 'cookie value ';
     $this->assertEquals('cookie value', Input::cookie('cookie'));
     $_SERVER['REQUEST_METHOD'] = 'GET ';
     $this->assertEquals('GET', Input::server('REQUEST_METHOD'));
     $this->assertEquals('testing', Input::env('APP_ENV'));
 }
Example #7
0
 /**
  * 自动获取当前页码
  * @param string $varPage
  * @param int    $default
  * @return int
  */
 public static function getCurrentPage($varPage = 'page', $default = 1)
 {
     $page = Input::request($varPage);
     if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) {
         return $page;
     }
     return $default;
 }
Example #8
0
 public function testFilterCover()
 {
     Input::setFilter('htmlspecialchars');
     $input = ['a' => ' test<> ', 'b' => '<b\\ar />'];
     $filters = ['trim'];
     $this->assertEquals('test&lt;&gt;', Input::getData('a', $input, $filters));
     $filters = ['trim', false];
     $this->assertEquals('test<>', Input::getData('a', $input, $filters));
     $filters = 'stripslashes';
     $this->assertEquals("&lt;bar /&gt;", Input::getData('b', $input, $filters));
     $filters = 'stripslashes,0';
     $this->assertEquals("<bar />", Input::getData('b', $input, $filters));
 }
Example #9
0
 /**
  * 验证上传文件大小
  * @access protected
  * @param mixed $value  字段值
  * @param mixed $rule  验证规则
  * @return bool
  */
 protected function fileSize($value, $rule)
 {
     $file = Input::file($value);
     if (empty($file)) {
         return false;
     }
     if (is_string($rule)) {
         $rule = explode(',', $rule);
     }
     if (is_array($file)) {
         foreach ($file as $item) {
             if ($item->getSize() > $rule) {
                 return false;
             }
         }
         return true;
     } else {
         return $file->getSize() <= $rule;
     }
 }