Core.php 17 KB

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