Core.php 16 KB

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