123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- namespace Upload\Src\Store;
- use Dever;
- use Dever\String\Helper as String;
- use Dever\Loader\Config;
- class Core
- {
- protected $data;
- protected $config;
- protected $handle;
- protected $output;
- protected $limit;
- protected $name;
- protected $path;
- protected $ext;
- protected $file;
- protected $size;
- protected $base = '';
-
- /**
- * __construct
- *
- * @return mixed
- */
- public function __construct($data)
- {
- $this->data = $data;
- }
- /**
- * 获取根目录
- *
- * @return mixed
- */
- protected function root()
- {
- if (!$this->base) {
- $path = Config::data();
- $this->base = $path . 'upload/';
- }
- return $this->base;
- }
-
- /**
- * 验证数据
- *
- * @return mixed
- */
- private function check($param)
- {
- foreach ($param as $k => $v) {
- $method = 'check_' . $v;
- $this->$method();
-
- if ($this->output['status'] == -1) {
- break;
- }
- }
- }
-
- /**
- * 验证key是否包含有后续处理的方法
- *
- * @return mixed
- */
- private function check_handle()
- {
- if (isset($this->data['key']) && strpos($this->data['key'], '_') !== false) {
- $temp = explode('_', $this->data['key']);
- $this->data['key'] = $temp[0];
- unset($temp[0]);
- foreach ($temp as $k => $v) {
- $this->handle[] = explode('=', $v);
- }
- }
- }
-
- /**
- * 验证文件是否存在
- *
- * @return mixed
- */
- private function check_file()
- {
- if ($this->data['file']['tmp_name'] == '') {
- $this->output['status'] = -1;
- $this->output['message'] = '没有选择文件';
- }
- }
-
- /**
- * 验证基本配置
- *
- * @return mixed
- */
- private function check_key()
- {
- if (trim($this->data['key']) == '') {
- $this->output['status'] = -1;
- $this->output['message'] = '请添加配置key';
- } else {
- $this->config = Dever::load('upload/upload-one', $this->data['key']);
-
- if (!$this->config) {
- $this->output['status'] = -1;
- $this->output['message'] = '此配置不存在';
- }
- }
- }
-
- /**
- * 验证文件类型
- *
- * @return mixed
- */
- private function check_type()
- {
- $ext = $this->getExt($this->data['file']['tmp_name']);
- if (strpos($this->config['type'], $ext) === false) {
- $this->output['status'] = -1;
- $this->output['message'] = '文件格式不符合要求';
- }
- $this->ext = '.' . $ext;
- }
-
- /**
- * 验证文件大小
- *
- * @return mixed
- */
- private function check_size()
- {
- if ($this->config['width'] > 0 || $this->config['height'] > 0) {
- $this->limit = getimagesize($this->data['file']['tmp_name']);
- }
- # 默认30M
- $size = $this->config['size'] > 0 ? 1024*$this->config['size'] : 30*1024*1024;
- $this->size = $this->data['file']['size'];
-
- if (($size < $this->data['file']['size']) && $size > 0) {
- $this->output['status'] = -1;
- $this->output['message'] = '文件不能超过'.$size.'k';
- } elseif ($this->config['width'] > 0 && $this->config['width'] < $this->limit[0]) {
- $this->output['status'] = -1;
- $this->output['message'] = '图片宽度不能超过' . $this->config['width'] . 'px';
- } elseif ($this->config['height'] > 0 && $this->config['height'] < $this->limit[1]) {
- $this->output['status'] = -1;
- $this->output['message'] = '图片高度不能超过' . $this->config['height'] . 'px';
- }
- }
-
- /**
- * 上传操作
- *
- * @return mixed
- */
- public function copy()
- {
- $this->output['status'] = 1;
- if (is_string($this->data['file'])) {
- if (strpos($this->data['file'], 'http') !== false) {
- $this->output['status'] = -1;
- $this->output['message'] = '暂时不支持复制网络文件';
- return $this->output;
- }
- $this->root();
- header('Content-type: text/json; charset=utf-8');
- $file = base64_decode($this->data['file']);
- $path = Dever::path($this->base . 'tmp/');
- $this->data['file'] = array();
- $this->data['file']['name'] = 'Tmp' . String::rand(8) . md5(microtime() . rand(0,1000)) . '.jpg';
- $this->data['file']['tmp_name'] = $path . $this->data['file']['name'];
- file_put_contents($this->data['file']['tmp_name'], $file);
- $this->data['file']['size'] = filesize($this->data['file']['tmp_name']);
- } else {
- header("Content-type: application/json; charset=utf-8");
- }
- $this->check(array('handle', 'file', 'key', 'type', 'size'));
- if ($this->output['status'] == -1) {
- unlink($this->data['file']['tmp_name']);
- return $this->output;
- }
-
- $this->save();
-
- if ($this->output['status'] == -1) {
- return $this->output;
- }
-
- if (isset($this->handle) && is_array($this->handle)) {
- foreach ($this->handle as $k => $v) {
- $method = 'handle_' . $v[0];
- $this->$method($v[1]);
- }
- }
- $this->output['status'] = 1;
- $this->output['name'] = $this->data['file']['name'];
- return $this->output;
- }
- protected function update($id)
- {
- $param['set_name'] = $this->name;
- $param['set_source_name'] = $this->data['file']['name'];
- $param['set_file'] = $this->file;
- $param['set_key'] = md5($this->output['url']);
- $param['set_ext'] = $this->ext;
- if ($this->limit) {
- $param['set_width'] = $this->limit[0];
- $param['set_height'] = $this->limit[1];
- }
-
- if ($this->size) {
- $param['set_size'] = $this->size;
- }
-
- $param['where_id'] = $id;
- Dever::load('upload/file-update', $param);
- }
- protected function insert()
- {
- $param['add_name'] = $this->name;
- $param['add_source_name'] = $this->data['file']['name'];
- $param['add_file'] = $this->file;
- $param['add_key'] = md5($this->output['url']);
- $param['add_ext'] = $this->ext;
- if ($this->limit) {
- $param['add_width'] = $this->limit[0];
- $param['add_height'] = $this->limit[1];
- }
-
- if ($this->size) {
- $param['add_size'] = $this->size;
- }
-
- $param['add_state'] = 1;
- //file_put_contents(DEVER_PATH . 'data/test', var_export($param,true));
- Dever::load('upload/file-insert', $param);
- }
-
- /**
- * getExt
- *
- * @return mixed
- */
- protected function getExt($filename)
- {
- $file = fopen($filename,"rb");
- $bin = fread($file,2);
- fclose($file);
- $strInfo = @unpack("c2chars",$bin);
- $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
- $fileType = '';
- switch ($typeCode) {
- case 7790:
- $fileType = 'exe';
- break;
- case 7784:
- $fileType = 'midi';
- break;
- case 8297:
- $fileType = 'rar';
- break;
- case 255216:
- $fileType = 'jpg';
- break;
- case 7173:
- $fileType = 'gif';
- break;
- case 13780:
- $fileType = 'png';
- break;
- case 6677:
- $fileType = 'bmp';
- break;
- case 6787:
- $fileType = 'swf';
- break;
- case 6063;
- $fileType = 'php|xml';
- break;
- case 6033:
- $fileType = 'html|htm|shtml';
- break;
- case 8075:
- $fileType = 'zip';
- break;
- case 6782:
- case 1310:
- $fileType = 'txt';
- break;
- case 4742:
- $fileType = 'js';
- break;
- case 8273:
- $fileType = 'wav';
- break;
- case 7368:
- $fileType = 'mp3';
- break;
- default:
- $fileType = 'unknown'.$typeCode;
- break;
- }
- if ($strInfo['chars1'] == '-1' && $strInfo['chars2'] == '-40') {
- return 'jpg';
- }
- if ($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80') {
- return 'png';
- }
- if ($strInfo['chars1'] == '-48' && $strInfo['chars2'] == '-49') {
- return 'msi';
- }
- return $fileType;
- }
- }
|