Core.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php
  2. namespace Upload\Lib\Store;
  3. use Dever;
  4. use Dever\String\Helper as Helper;
  5. use Dever\Loader\Config;
  6. class Core
  7. {
  8. protected $data;
  9. protected $config;
  10. protected $handle;
  11. protected $img;
  12. protected $output;
  13. protected $limit;
  14. protected $name;
  15. protected $path;
  16. protected $ext = '';
  17. protected $file;
  18. protected $size;
  19. protected $base = '';
  20. /**
  21. * __construct
  22. *
  23. * @return mixed
  24. */
  25. public function __construct($data = array())
  26. {
  27. $this->data = $data;
  28. }
  29. /**
  30. * 获取根目录
  31. *
  32. * @return mixed
  33. */
  34. protected function root()
  35. {
  36. if (!$this->base) {
  37. $path = Config::data();
  38. $this->base = Dever::path($path . 'upload/');
  39. }
  40. return $this->base;
  41. }
  42. /**
  43. * 验证数据
  44. *
  45. * @return mixed
  46. */
  47. private function check($param)
  48. {
  49. foreach ($param as $k => $v) {
  50. $method = 'check_' . $v;
  51. $this->$method();
  52. if ($this->output['status'] == -1) {
  53. break;
  54. }
  55. }
  56. }
  57. /**
  58. * 验证key是否包含有后续处理的方法
  59. *
  60. * @return mixed
  61. */
  62. private function check_handle()
  63. {
  64. if (isset($this->data['key']) && strpos($this->data['key'], '_') !== false) {
  65. $temp = explode('_', $this->data['key']);
  66. $this->data['key'] = $temp[0];
  67. unset($temp[0]);
  68. foreach ($temp as $k => $v) {
  69. $this->handle[] = explode('=', $v);
  70. }
  71. }
  72. }
  73. /**
  74. * 验证文件是否存在
  75. *
  76. * @return mixed
  77. */
  78. private function check_file()
  79. {
  80. if ($this->data['file']['tmp_name'] == '') {
  81. $this->output['status'] = -1;
  82. $this->output['message'] = '没有选择文件';
  83. }
  84. }
  85. /**
  86. * 验证基本配置
  87. *
  88. * @return mixed
  89. */
  90. private function check_key()
  91. {
  92. if (trim($this->data['key']) == '') {
  93. $this->output['status'] = -1;
  94. $this->output['message'] = '请添加配置key';
  95. } else {
  96. $this->config = Dever::load('upload/upload-one', $this->data['key']);
  97. if (!$this->config) {
  98. $this->output['status'] = -1;
  99. $this->output['message'] = '此配置不存在';
  100. }
  101. }
  102. }
  103. /**
  104. * 验证文件类型
  105. *
  106. * @return mixed
  107. */
  108. private function check_type()
  109. {
  110. if (!$this->ext) {
  111. $ext = $this->getExt($this->data['file']['tmp_name']);
  112. if (strpos($this->config['type'], $ext) === false) {
  113. $this->output['status'] = -1;
  114. $this->output['message'] = '文件格式不符合要求';
  115. }
  116. $this->ext = '.' . $ext;
  117. }
  118. }
  119. /**
  120. * 验证文件大小
  121. *
  122. * @return mixed
  123. */
  124. private function check_size()
  125. {
  126. if ($this->config['width'] > 0 || $this->config['height'] > 0) {
  127. $this->limit = getimagesize($this->data['file']['tmp_name']);
  128. }
  129. # 默认30M
  130. $size = $this->config['size'] > 0 ? 1024*$this->config['size'] : 30*1024*1024;
  131. $this->size = $this->data['file']['size'];
  132. if (($size < $this->data['file']['size']) && $size > 0) {
  133. $this->output['status'] = -1;
  134. $this->output['message'] = '文件不能超过'.$size.'k';
  135. } elseif ($this->config['width'] > 0 && $this->config['width'] < $this->limit[0]) {
  136. $this->output['status'] = -1;
  137. $this->output['message'] = '图片宽度不能超过' . $this->config['width'] . 'px';
  138. } elseif ($this->config['height'] > 0 && $this->config['height'] < $this->limit[1]) {
  139. $this->output['status'] = -1;
  140. $this->output['message'] = '图片高度不能超过' . $this->config['height'] . 'px';
  141. }
  142. }
  143. /**
  144. * 上传操作
  145. *
  146. * @return mixed
  147. */
  148. public function copy()
  149. {
  150. $this->output['status'] = 1;
  151. if (is_string($this->data['file'])) {
  152. /*
  153. if (strpos($this->data['file'], 'http') !== false) {
  154. $this->output['status'] = -1;
  155. $this->output['message'] = '暂时不支持复制网络文件';
  156. return $this->output;
  157. }
  158. */
  159. $this->root();
  160. header('Content-type: text/json; charset=utf-8');
  161. $path = Dever::path($this->base, 'tmp/');
  162. $name = urldecode($this->data['file']);
  163. $this->data['file'] = array();
  164. $this->data['file']['name'] = 'Tmp' . sha1($name);
  165. //$this->data['file']['name'] = 'Tmp' . Helper::rand(8) . md5(microtime() . rand(0,1000)) . '.jpg';
  166. $this->data['file']['tmp_name'] = $path . $this->data['file']['name'];
  167. if (!is_file($this->data['file']['tmp_name'])) {
  168. if (strstr($name, 'tp=webp')) {
  169. $name = str_replace('tp=webp', 'tp=jpeg', $name);
  170. } elseif (strstr($name, '.webp')) {
  171. $name = str_replace('.webp', '.jpg', $name);
  172. }
  173. $file = Dever::curl($name);
  174. if (stristr($file, 'webp')) {
  175. # 将webp图片转成jpg
  176. $this->ext = '.jpg';
  177. }
  178. file_put_contents($this->data['file']['tmp_name'], $file);
  179. }
  180. $this->data['file']['size'] = filesize($this->data['file']['tmp_name']);
  181. } else {
  182. header("Content-type: application/json; charset=utf-8");
  183. }
  184. $this->check(array('handle', 'file', 'key', 'type', 'size'));
  185. if ($this->output['status'] == -1) {
  186. $this->delete();
  187. return $this->output;
  188. }
  189. $this->save();
  190. if ($this->output['status'] == -1) {
  191. return $this->output;
  192. }
  193. if (isset($this->handle) && is_array($this->handle)) {
  194. foreach ($this->handle as $k => $v) {
  195. $method = 'handle_' . $v[0];
  196. if (method_exists($this, $method)) {
  197. $this->$method($v);
  198. }
  199. $this->$method($v[1]);
  200. }
  201. } elseif (isset($this->config['alter']) && $this->config['alter']) {
  202. parse_str($this->config['alter'], $handle);
  203. if ($handle) {
  204. foreach ($handle as $k => $v) {
  205. $method = 'handle_' . $k;
  206. if (method_exists($this, $method)) {
  207. $this->$method($v);
  208. }
  209. }
  210. }
  211. }
  212. $this->output['status'] = 1;
  213. $this->output['name'] = $this->data['file']['name'];
  214. return $this->output;
  215. }
  216. protected function update($id)
  217. {
  218. $param['set_name'] = $this->name;
  219. $param['set_source_name'] = $this->data['file']['name'];
  220. $param['set_file'] = $this->file;
  221. $param['set_key'] = md5($this->output['url']);
  222. $param['set_ext'] = $this->ext;
  223. if ($this->limit) {
  224. $param['set_width'] = $this->limit[0];
  225. $param['set_height'] = $this->limit[1];
  226. }
  227. if ($this->size) {
  228. $param['set_size'] = $this->size;
  229. }
  230. $param['where_id'] = $id;
  231. Dever::load('upload/file-update', $param);
  232. }
  233. protected function insert()
  234. {
  235. $param['add_name'] = $this->name;
  236. $param['add_source_name'] = $this->data['file']['name'];
  237. $param['add_file'] = $this->file;
  238. $param['add_key'] = md5($this->output['url']);
  239. $param['add_ext'] = $this->ext;
  240. $param['add_upload'] = $this->data['key'];
  241. if ($this->limit) {
  242. $param['add_width'] = $this->limit[0];
  243. $param['add_height'] = $this->limit[1];
  244. }
  245. if ($this->size) {
  246. $param['add_size'] = $this->size;
  247. }
  248. $param['add_state'] = 1;
  249. //file_put_contents(DEVER_PATH . 'data/test', var_export($param,true));
  250. Dever::load('upload/file-insert', $param);
  251. }
  252. /**
  253. * getExt
  254. *
  255. * @return mixed
  256. */
  257. protected function getExt($filename)
  258. {
  259. if (isset($this->data['file']['type'])) {
  260. $ext = $this->getExtByMine($this->data['file']['type']);
  261. } elseif (function_exists('finfo_open')) {
  262. $finfo = finfo_open(FILEINFO_MIME); // 返回 mime 类型
  263. $code = finfo_file($finfo, $filename);
  264. finfo_close($finfo);
  265. $temp = explode(';', $code);
  266. $ext = $this->getExtByMine($temp[0]);
  267. } else {
  268. $ext = $this->getExtByByte($filename);
  269. }
  270. if (!$ext || $ext == 'txt' || $ext == 'exe') {
  271. if (isset($this->data['file']['type'])) {
  272. $ext = $this->getExtByMine($this->data['file']['type']);
  273. }
  274. if (!$ext || $ext == 'txt' || $ext == 'exe') {
  275. $ext = $this->getExtByByte($filename);
  276. }
  277. }
  278. return $ext;
  279. }
  280. public function delete()
  281. {
  282. @unlink($this->data['file']['tmp_name']);
  283. }
  284. /**
  285. * 根据mime类型获取文件扩展名
  286. *
  287. * @return mixed
  288. */
  289. protected function getExtByMine($mine)
  290. {
  291. $mine = trim($mine);
  292. $config = array
  293. (
  294. 'application/envoy' => 'evy',
  295. 'application/fractals' => 'fif',
  296. 'application/futuresplash' => 'spl',
  297. 'application/hta' => 'hta',
  298. 'application/internet-property-stream' => 'acx',
  299. 'application/mac-binhex40' => 'hqx',
  300. 'application/msword' => 'doc',
  301. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
  302. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
  303. 'video/x-m4v' => 'mp4',
  304. 'video/mp4' => 'mp4',
  305. 'application/octet-stream' => 'exe',
  306. 'application/oda' => 'oda',
  307. 'application/olescript' => 'axs',
  308. 'application/pdf' => 'pdf',
  309. 'application/pics-rules' => 'prf',
  310. 'application/pkcs10' => 'p10',
  311. 'application/pkix-crl' => 'crl',
  312. 'application/postscript' => 'ai',
  313. 'application/postscript' => 'eps',
  314. 'application/postscript' => 'ps',
  315. 'application/rtf' => 'rtf',
  316. 'application/set-payment-initiation' => 'setpay',
  317. 'application/set-registration-initiation' => 'setreg',
  318. 'application/vnd.ms-excel' => 'xls',
  319. 'application/vnd.ms-outlook' => 'msg',
  320. 'application/vnd.ms-pkicertstore' => 'sst',
  321. 'application/vnd.ms-pkiseccat' => 'cat',
  322. 'application/vnd.ms-pkistl' => 'stl',
  323. 'application/vnd.ms-powerpoint' => 'ppt',
  324. 'application/vnd.ms-project' => 'mpp',
  325. 'application/vnd.ms-works' => 'wps',
  326. 'application/winhlp' => 'hlp',
  327. 'application/x-bcpio' => 'bcpio',
  328. 'application/x-cdf' => 'cdf',
  329. 'application/x-compress' => 'z',
  330. 'application/x-compressed' => 'tgz',
  331. 'application/x-cpio' => 'cpio',
  332. 'application/x-csh' => 'csh',
  333. 'application/x-director' => 'dir',
  334. 'application/x-dvi' => 'dvi',
  335. 'application/x-gtar' => 'gtar',
  336. 'application/x-gzip' => 'gz',
  337. 'application/x-hdf' => 'hdf',
  338. 'application/x-internet-signup' => 'isp',
  339. 'application/x-iphone' => 'iii',
  340. 'application/x-javascript' => 'js',
  341. 'application/x-latex' => 'latex',
  342. 'application/x-msaccess' => 'mdb',
  343. 'application/x-mscardfile' => 'crd',
  344. 'application/x-msclip' => 'clp',
  345. 'application/x-msdownload' => 'dll',
  346. 'application/x-msmediaview' => 'mvb',
  347. 'application/x-msmetafile' => 'wmf',
  348. 'application/x-msmoney' => 'mny',
  349. 'application/x-mspublisher' => 'pub',
  350. 'application/x-msschedule' => 'scd',
  351. 'application/x-msterminal' => 'trm',
  352. 'application/x-mswrite' => 'wri',
  353. 'application/x-netcdf' => 'cdf',
  354. 'application/x-netcdf' => 'nc',
  355. 'application/x-perfmon' => 'pma',
  356. 'application/x-pkcs12' => 'p12',
  357. 'application/x-pkcs12' => 'pfx',
  358. 'application/x-pkcs7-certificates' => 'p7b',
  359. 'application/x-pkcs7-certreqresp' => 'p7r',
  360. 'application/x-pkcs7-mime' => 'p7c',
  361. 'application/x-pkcs7-signature' => 'p7s',
  362. 'application/x-sh' => 'sh',
  363. 'application/x-shar' => 'shar',
  364. 'application/x-shockwave-flash' => 'swf',
  365. 'application/x-stuffit' => 'sit',
  366. 'application/x-sv4cpio' => 'sv4cpio',
  367. 'application/x-sv4crc' => 'sv4crc',
  368. 'application/x-tar' => 'tar',
  369. 'application/x-tcl' => 'tcl',
  370. 'application/x-tex' => 'tex',
  371. 'application/x-texinfo' => 'texi',
  372. 'application/x-texinfo' => 'texinfo',
  373. 'application/x-troff' => 'roff',
  374. 'application/x-troff' => 't',
  375. 'application/x-troff' => 'tr',
  376. 'application/x-troff-man' => 'man',
  377. 'application/x-troff-me' => 'me',
  378. 'application/x-troff-ms' => 'ms',
  379. 'application/x-ustar' => 'ustar',
  380. 'application/x-wais-source' => 'src',
  381. 'application/x-x509-ca-cert' => 'cer',
  382. 'application/ynd.ms-pkipko' => 'pko',
  383. 'application/zip' => 'zip',
  384. 'audio/basic' => 'au',
  385. 'audio/basic' => 'snd',
  386. 'audio/mid' => 'mid',
  387. 'audio/mid' => 'rmi',
  388. 'audio/mpeg' => 'mp3',
  389. 'audio/mp3' => 'mp3',
  390. 'audio/x-aiff' => 'aif',
  391. 'audio/x-aiff' => 'aifc',
  392. 'audio/x-aiff' => 'aiff',
  393. 'audio/x-mpegurl' => 'm3u',
  394. 'audio/x-pn-realaudio' => 'ram',
  395. 'audio/x-wav' => 'wav',
  396. 'image/bmp' => 'bmp',
  397. 'image/cis-cod' => 'cod',
  398. 'image/gif' => 'gif',
  399. 'image/ief' => 'ief',
  400. 'image/jpeg' => 'jpg',
  401. 'image/pipeg' => 'jfif',
  402. 'image/svg+xml' => 'svg',
  403. 'image/tiff' => 'tif',
  404. 'image/tiff' => 'tiff',
  405. 'image/x-cmu-raster' => 'ras',
  406. 'image/x-cmx' => 'cmx',
  407. 'image/x-icon' => 'ico',
  408. 'image/x-portable-anymap' => 'pnm',
  409. 'image/x-portable-bitmap' => 'pbm',
  410. 'image/x-portable-graymap' => 'pgm',
  411. 'image/x-portable-pixmap' => 'ppm',
  412. 'image/x-rgb' => 'rgb',
  413. 'image/x-xbitmap' => 'xbm',
  414. 'image/x-xpixmap' => 'xpm',
  415. 'image/x-xwindowdump' => 'xwd',
  416. 'message/rfc822' => 'mht',
  417. 'message/rfc822' => 'mhtml',
  418. 'message/rfc822' => 'nws',
  419. 'text/css' => 'css',
  420. 'text/h323' => '323',
  421. 'text/html' => 'html',
  422. 'text/iuls' => 'uls',
  423. 'text/plain' => 'txt',
  424. 'text/richtext' => 'rtx',
  425. 'text/scriptlet' => 'sct',
  426. 'text/tab-separated-values' => 'tsv',
  427. 'text/webviewhtml' => 'htt',
  428. 'text/x-component' => 'htc',
  429. 'text/x-setext' => 'etx',
  430. 'text/x-vcard' => 'vcf',
  431. 'video/mpeg' => 'mpeg',
  432. 'video/quicktime' => 'mov',
  433. 'video/x-ms-asf' => 'asx',
  434. 'video/x-msvideo' => 'avi',
  435. 'video/x-sgi-movie' => 'movie',
  436. 'x-world/x-vrml' => 'flr',
  437. 'application/x-rar' => 'rar',
  438. );
  439. if (isset($config[$mine])) {
  440. return $config[$mine];
  441. } else {
  442. return false;
  443. }
  444. }
  445. /**
  446. * getExt
  447. *
  448. * @return mixed
  449. */
  450. protected function getExtByByte($filename)
  451. {
  452. $file = fopen($filename,"rb");
  453. $bin = fread($file,2);
  454. fclose($file);
  455. $strInfo = @unpack("c2chars",$bin);
  456. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  457. $fileType = '';
  458. switch ($typeCode) {
  459. case 7790:
  460. $fileType = 'exe';
  461. break;
  462. case 7784:
  463. $fileType = 'midi';
  464. break;
  465. case 8297:
  466. $fileType = 'rar';
  467. break;
  468. case 255216:
  469. $fileType = 'jpg';
  470. break;
  471. case 7173:
  472. $fileType = 'gif';
  473. break;
  474. case 13780:
  475. $fileType = 'png';
  476. break;
  477. case 6677:
  478. $fileType = 'bmp';
  479. break;
  480. case 6787:
  481. $fileType = 'swf';
  482. break;
  483. case 6063;
  484. $fileType = 'php|xml';
  485. break;
  486. case 6033:
  487. $fileType = 'html|htm|shtml';
  488. break;
  489. case 8075:
  490. $fileType = 'zip';
  491. break;
  492. case 6782:
  493. case 1310:
  494. $fileType = 'txt';
  495. break;
  496. case 4742:
  497. $fileType = 'js';
  498. break;
  499. case 8273:
  500. $fileType = 'wav';
  501. break;
  502. case 7368:
  503. $fileType = 'mp3';
  504. break;
  505. case 3780:
  506. $fileType = 'pdf';
  507. break;
  508. default:
  509. $fileType = 'unknown'.$typeCode;
  510. break;
  511. }
  512. if ($strInfo['chars1'] == '-1' && $strInfo['chars2'] == '-40') {
  513. return 'jpg';
  514. }
  515. if ($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80') {
  516. return 'png';
  517. }
  518. if ($strInfo['chars1'] == '-48' && $strInfo['chars2'] == '-49') {
  519. return 'msi';
  520. }
  521. return $fileType;
  522. }
  523. }