Core.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace Upload\Src\Store;
  3. use Dever;
  4. use Dever\String\Helper as String;
  5. use Dever\Loader\Config;
  6. class Core
  7. {
  8. protected $data;
  9. protected $config;
  10. protected $handle;
  11. protected $output;
  12. protected $limit;
  13. protected $name;
  14. protected $path;
  15. protected $ext;
  16. protected $file;
  17. protected $size;
  18. protected $base = '';
  19. /**
  20. * __construct
  21. *
  22. * @return mixed
  23. */
  24. public function __construct($data)
  25. {
  26. $this->data = $data;
  27. }
  28. /**
  29. * 获取根目录
  30. *
  31. * @return mixed
  32. */
  33. protected function root()
  34. {
  35. if (!$this->base) {
  36. $path = Config::data();
  37. $this->base = $path . 'upload/';
  38. }
  39. return $this->base;
  40. }
  41. /**
  42. * 验证数据
  43. *
  44. * @return mixed
  45. */
  46. private function check($param)
  47. {
  48. foreach ($param as $k => $v) {
  49. $method = 'check_' . $v;
  50. $this->$method();
  51. if ($this->output['status'] == -1) {
  52. break;
  53. }
  54. }
  55. }
  56. /**
  57. * 验证key是否包含有后续处理的方法
  58. *
  59. * @return mixed
  60. */
  61. private function check_handle()
  62. {
  63. if (isset($this->data['key']) && strpos($this->data['key'], '_') !== false) {
  64. $temp = explode('_', $this->data['key']);
  65. $this->data['key'] = $temp[0];
  66. unset($temp[0]);
  67. foreach ($temp as $k => $v) {
  68. $this->handle[] = explode('=', $v);
  69. }
  70. }
  71. }
  72. /**
  73. * 验证文件是否存在
  74. *
  75. * @return mixed
  76. */
  77. private function check_file()
  78. {
  79. if ($this->data['file']['tmp_name'] == '') {
  80. $this->output['status'] = -1;
  81. $this->output['message'] = '没有选择文件';
  82. }
  83. }
  84. /**
  85. * 验证基本配置
  86. *
  87. * @return mixed
  88. */
  89. private function check_key()
  90. {
  91. if (trim($this->data['key']) == '') {
  92. $this->output['status'] = -1;
  93. $this->output['message'] = '请添加配置key';
  94. } else {
  95. $this->config = Dever::load('upload/upload-one', $this->data['key']);
  96. if (!$this->config) {
  97. $this->output['status'] = -1;
  98. $this->output['message'] = '此配置不存在';
  99. }
  100. }
  101. }
  102. /**
  103. * 验证文件类型
  104. *
  105. * @return mixed
  106. */
  107. private function check_type()
  108. {
  109. $ext = $this->getExt($this->data['file']['tmp_name']);
  110. if (strpos($this->config['type'], $ext) === false) {
  111. $this->output['status'] = -1;
  112. $this->output['message'] = '文件格式不符合要求';
  113. }
  114. $this->ext = '.' . $ext;
  115. }
  116. /**
  117. * 验证文件大小
  118. *
  119. * @return mixed
  120. */
  121. private function check_size()
  122. {
  123. if ($this->config['width'] > 0 || $this->config['height'] > 0) {
  124. $this->limit = getimagesize($this->data['file']['tmp_name']);
  125. }
  126. # 默认30M
  127. $size = $this->config['size'] > 0 ? 1024*$this->config['size'] : 30*1024*1024;
  128. $this->size = $this->data['file']['size'];
  129. if (($size < $this->data['file']['size']) && $size > 0) {
  130. $this->output['status'] = -1;
  131. $this->output['message'] = '文件不能超过'.$size.'k';
  132. } elseif ($this->config['width'] > 0 && $this->config['width'] < $this->limit[0]) {
  133. $this->output['status'] = -1;
  134. $this->output['message'] = '图片宽度不能超过' . $this->config['width'] . 'px';
  135. } elseif ($this->config['height'] > 0 && $this->config['height'] < $this->limit[1]) {
  136. $this->output['status'] = -1;
  137. $this->output['message'] = '图片高度不能超过' . $this->config['height'] . 'px';
  138. }
  139. }
  140. /**
  141. * 上传操作
  142. *
  143. * @return mixed
  144. */
  145. public function copy()
  146. {
  147. $this->output['status'] = 1;
  148. if (is_string($this->data['file'])) {
  149. if (strpos($this->data['file'], 'http') !== false) {
  150. $this->output['status'] = -1;
  151. $this->output['message'] = '暂时不支持复制网络文件';
  152. return $this->output;
  153. }
  154. $this->root();
  155. header('Content-type: text/json; charset=utf-8');
  156. $file = base64_decode($this->data['file']);
  157. $path = Dever::path($this->base . 'tmp/');
  158. $this->data['file'] = array();
  159. $this->data['file']['name'] = 'Tmp' . String::rand(8) . md5(microtime() . rand(0,1000)) . '.jpg';
  160. $this->data['file']['tmp_name'] = $path . $this->data['file']['name'];
  161. file_put_contents($this->data['file']['tmp_name'], $file);
  162. $this->data['file']['size'] = filesize($this->data['file']['tmp_name']);
  163. } else {
  164. header("Content-type: application/json; charset=utf-8");
  165. }
  166. $this->check(array('handle', 'file', 'key', 'type', 'size'));
  167. if ($this->output['status'] == -1) {
  168. unlink($this->data['file']['tmp_name']);
  169. return $this->output;
  170. }
  171. $this->save();
  172. if ($this->output['status'] == -1) {
  173. return $this->output;
  174. }
  175. if (isset($this->handle) && is_array($this->handle)) {
  176. foreach ($this->handle as $k => $v) {
  177. $method = 'handle_' . $v[0];
  178. $this->$method($v[1]);
  179. }
  180. }
  181. $this->output['status'] = 1;
  182. $this->output['name'] = $this->data['file']['name'];
  183. return $this->output;
  184. }
  185. protected function update($id)
  186. {
  187. $param['set_name'] = $this->name;
  188. $param['set_source_name'] = $this->data['file']['name'];
  189. $param['set_file'] = $this->file;
  190. $param['set_key'] = md5($this->output['url']);
  191. $param['set_ext'] = $this->ext;
  192. if ($this->limit) {
  193. $param['set_width'] = $this->limit[0];
  194. $param['set_height'] = $this->limit[1];
  195. }
  196. if ($this->size) {
  197. $param['set_size'] = $this->size;
  198. }
  199. $param['where_id'] = $id;
  200. Dever::load('upload/file-update', $param);
  201. }
  202. protected function insert()
  203. {
  204. $param['add_name'] = $this->name;
  205. $param['add_source_name'] = $this->data['file']['name'];
  206. $param['add_file'] = $this->file;
  207. $param['add_key'] = md5($this->output['url']);
  208. $param['add_ext'] = $this->ext;
  209. if ($this->limit) {
  210. $param['add_width'] = $this->limit[0];
  211. $param['add_height'] = $this->limit[1];
  212. }
  213. if ($this->size) {
  214. $param['add_size'] = $this->size;
  215. }
  216. $param['add_state'] = 1;
  217. //file_put_contents(DEVER_PATH . 'data/test', var_export($param,true));
  218. Dever::load('upload/file-insert', $param);
  219. }
  220. /**
  221. * getExt
  222. *
  223. * @return mixed
  224. */
  225. protected function getExt($filename)
  226. {
  227. $file = fopen($filename,"rb");
  228. $bin = fread($file,2);
  229. fclose($file);
  230. $strInfo = @unpack("c2chars",$bin);
  231. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  232. $fileType = '';
  233. switch ($typeCode) {
  234. case 7790:
  235. $fileType = 'exe';
  236. break;
  237. case 7784:
  238. $fileType = 'midi';
  239. break;
  240. case 8297:
  241. $fileType = 'rar';
  242. break;
  243. case 255216:
  244. $fileType = 'jpg';
  245. break;
  246. case 7173:
  247. $fileType = 'gif';
  248. break;
  249. case 13780:
  250. $fileType = 'png';
  251. break;
  252. case 6677:
  253. $fileType = 'bmp';
  254. break;
  255. case 6787:
  256. $fileType = 'swf';
  257. break;
  258. case 6063;
  259. $fileType = 'php|xml';
  260. break;
  261. case 6033:
  262. $fileType = 'html|htm|shtml';
  263. break;
  264. case 8075:
  265. $fileType = 'zip';
  266. break;
  267. case 6782:
  268. case 1310:
  269. $fileType = 'txt';
  270. break;
  271. case 4742:
  272. $fileType = 'js';
  273. break;
  274. case 8273:
  275. $fileType = 'wav';
  276. break;
  277. case 7368:
  278. $fileType = 'mp3';
  279. break;
  280. default:
  281. $fileType = 'unknown'.$typeCode;
  282. break;
  283. }
  284. if ($strInfo['chars1'] == '-1' && $strInfo['chars2'] == '-40') {
  285. return 'jpg';
  286. }
  287. if ($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80') {
  288. return 'png';
  289. }
  290. if ($strInfo['chars1'] == '-48' && $strInfo['chars2'] == '-49') {
  291. return 'msi';
  292. }
  293. return $fileType;
  294. }
  295. }