Save.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php namespace Upload\Lib;
  2. use Dever;
  3. class Save
  4. {
  5. private $config;
  6. private $ext = array
  7. (
  8. 1 => 'jpg,png,gif,webp,jpeg',
  9. 2 => 'mp3,m4a' ,
  10. 3 => 'video,flv,mp4,webm,mov',
  11. 4 => 'doc,xls,xlsx,docx',
  12. 5 => 'pdf',
  13. 6 => 'rar,zip',
  14. 7 => 'cer,pfx,pem',
  15. 8 => 'exe,msi',
  16. );
  17. private $type = 1;
  18. public function init($id)
  19. {
  20. $this->config = Dever::db('rule', 'upload')->find($id);
  21. if (!$this->config) {
  22. Dever::error('上传规则错误');
  23. }
  24. $this->config['save'] = Dever::db('save', 'upload')->find($this->config['save_id']);
  25. if (!$this->config['save']) {
  26. Dever::error('存储位置错误');
  27. }
  28. return $this;
  29. }
  30. public function get($id)
  31. {
  32. $this->init($id);
  33. $result = array();
  34. $result['id'] = $id;
  35. $result['chunkSize'] = $this->config['chunk'];
  36. $result['path'] = $id;
  37. $result['size'] = $this->config['size'];
  38. $result['accept'] = '';
  39. $result['mine'] = array();
  40. $mine = $this->getExtByMine(false, true);
  41. $type = explode(',', $this->config['type']);
  42. foreach ($type as $v) {
  43. if (isset($this->ext[$v])) {
  44. $result['accept'] .= '.' . str_replace(',', ',.', $this->ext[$v]);
  45. $temp = explode(',', $this->ext[$v]);
  46. foreach ($temp as $v2) {
  47. if (isset($mine[$v2])) {
  48. $result['mine'][] = $mine[$v2];
  49. }
  50. }
  51. }
  52. }
  53. $result['type'] = $this->config['save']['type'];
  54. if ($result['type'] > 1) {
  55. $result += Dever::load('tool', 'upload')->get($this->config['save'])->getInfo();
  56. }
  57. return $result;
  58. }
  59. public function act($id, $source, $default_ext = '', $uid = false, $dest_name = '', $cate = 1)
  60. {
  61. $this->init($id);
  62. $name = '';
  63. $ext = '';
  64. $size = 0;
  65. $method = '';
  66. $source_name = '';
  67. $chunk = array();
  68. if (is_array($source) && isset($source['tmp_name'])) {
  69. # 文件上传
  70. $type = 1;
  71. if ($source['name'] == 'blob' && $source['type'] == 'application/octet-stream') {
  72. $source['name'] = Dever::input('name');
  73. $source['type'] = Dever::input('type');
  74. }
  75. $total = Dever::input('chunks');
  76. if ($total > 1) {
  77. $chunk['size'] = $this->config['chunk'];
  78. $chunk['uid'] = Dever::input('uid');
  79. $chunk['timestamp'] = Dever::input('timestamp');
  80. $chunk['cur'] = Dever::input('chunk');
  81. $chunk['total'] = $total;
  82. if (!$chunk['uid'] || !$chunk['timestamp'] || !$chunk['cur'] || !$chunk['total']) {
  83. Dever::error('分片配置信息无效');
  84. }
  85. $name = $source_name = $source['name'] . $chunk['timestamp'] . $chunk['uid'];
  86. } else {
  87. $source_name = $source['name'];
  88. $name = $source_name . uniqid(date('YmdHis'), true) . mt_rand(10000, 99999);
  89. }
  90. $ext = $this->getExtByMine($source['type']);
  91. $size = $source['size'];
  92. $method = 'getimagesize';
  93. $source = $source['tmp_name'];
  94. } elseif (is_string($source)) {
  95. if (strstr($source, ';base64,')) {
  96. # base64编码
  97. $temp = explode(';base64,', $source);
  98. $ext = $this->getExtByMine(ltrim($temp[0], 'data:'));
  99. $source = str_replace(' ', '+', $temp[1]);
  100. $source = str_replace('=', '', $temp[1]);
  101. $source = base64_decode($source);
  102. $size = strlen($source);
  103. $size = round(($size - ($size/8)*2)/1024, 2);
  104. $method = 'getimagesizefromstring';
  105. $type = 2;
  106. } else {
  107. if (strstr($source, 'http')) {
  108. # http远程文件
  109. $name = $source;
  110. $type = 3;
  111. $ext = pathinfo($source, PATHINFO_EXTENSION);
  112. if ($this->config['save']['type'] == 1) {
  113. $content = Dever::curl($source)->log(false)->result();
  114. $source = Dever::file('tmp/' . sha1($name));
  115. file_put_contents($source, $content);
  116. }
  117. }
  118. if (is_file($source)) {
  119. # 本地文件
  120. $type = 1;
  121. $name = $source;
  122. $finfo = finfo_open(FILEINFO_MIME);
  123. $code = finfo_file($finfo, $source);
  124. finfo_close($finfo);
  125. $temp = explode(';', $code);
  126. $ext = $this->getExtByMine($temp[0]);
  127. if (!$ext) {
  128. $ext = $this->getExtByByte($source);
  129. }
  130. $size = filesize($source);
  131. $method = 'getimagesize';
  132. }
  133. }
  134. } else {
  135. $source = false;
  136. }
  137. if (!$source) {
  138. Dever::error('源文件不存在');
  139. }
  140. if (!$ext && $default_ext) {
  141. $ext = $default_ext;
  142. }
  143. $state = $this->check($ext, $size, $method, $source);
  144. if (is_string($state)) {
  145. if (isset($content)) {
  146. @unlink($source);
  147. }
  148. Dever::error($state);
  149. }
  150. if ($dest_name) {
  151. $name = $dest_name;
  152. }
  153. $dest = $this->config['id'] . '/' . $this->getDest($name, $ext, $uid);
  154. # type 1是文件复制 2是base64 3是远程文件复制
  155. $url = Dever::load('tool', 'upload')->get($this->config['save'])->upload($type, $source, $dest, $chunk, $this);
  156. $data = $this->up($source_name, $name, $dest, $this->config['size'], $this->config['width'] ?? 0, $this->config['height'] ?? 0, $cate);
  157. $data['url'] = $url;
  158. $data['type'] = $this->type;
  159. if (isset($content)) {
  160. @unlink($source);
  161. }
  162. return $data;
  163. }
  164. public function addFile($url, $source_name, $name, $dest, $size)
  165. {
  166. if ($this->config['save']['type'] > 1) {
  167. $url = Dever::load('tool', 'upload')->get($this->config['save'])->getUrl(3, $url, $this);
  168. if (isset($this->config['size']) && $this->config['size']) {
  169. $size = $this->config['size'];
  170. }
  171. }
  172. $data = $this->up($source_name, $name, $dest, $size, $this->config['width'] ?? 0, $this->config['height'] ?? 0);
  173. $data['url'] = $url;
  174. return $data;
  175. }
  176. private function up($source_name, $name, $dest, $size, $width = 0, $height = 0, $cate = 1)
  177. {
  178. $file['rule_id'] = $this->config['id'];
  179. $file['name'] = $name;
  180. $data = $file;
  181. $data['source_name'] = $source_name;
  182. $data['file'] = $dest;
  183. $data['save_id'] = $this->config['save_id'];
  184. $data['size'] = $size;
  185. if ($width) {
  186. $data['width'] = $width;
  187. }
  188. if ($height) {
  189. $data['height'] = $height;
  190. }
  191. $data['cate_id'] = $cate;
  192. $data['id'] = Dever::db('file', 'upload')->up($file, $data);
  193. return $data;
  194. }
  195. public function after()
  196. {
  197. $data = Dever::db('rule_after', 'upload')->select(array('rule_id' => $this->config['id']));
  198. if ($data) {
  199. foreach ($data as $k => $v) {
  200. $table = '';
  201. if ($v['type'] == 1) {
  202. $table = 'thumb';
  203. } elseif ($v['type'] == 2) {
  204. $table = 'crop';
  205. } elseif ($v['type'] == 3) {
  206. $table = 'water_pic';
  207. } elseif ($v['type'] == 4) {
  208. $table = 'water_txt';
  209. }
  210. if ($table && $v['type_id']) {
  211. $data[$k]['table'] = $table;
  212. $data[$k]['param'] = Dever::db($table, 'image')->find($v['type_id']);
  213. }
  214. }
  215. }
  216. return $data;
  217. }
  218. public function check($ext, $size, $info, $source = '')
  219. {
  220. $result = $this->checkExt($ext);
  221. if (is_string($result)) {
  222. return $result;
  223. }
  224. if ($size) {
  225. $result = $this->checkSize($size);
  226. if (is_string($result)) {
  227. return $result;
  228. }
  229. if ($this->type == 1 && $info) {
  230. if (is_string($info)) {
  231. $info = $info($source);
  232. }
  233. if ($info) {
  234. $result = $this->checkLimit($info);
  235. if (is_string($result)) {
  236. return $result;
  237. }
  238. }
  239. }
  240. }
  241. return true;
  242. }
  243. protected function checkExt($ext)
  244. {
  245. $this->type = 0;
  246. if (!$ext) {
  247. return '文件格式错误';
  248. }
  249. $state = false;
  250. $type = explode(',', $this->config['type']);
  251. foreach ($type as $v) {
  252. if (isset($this->ext[$v]) && strstr($this->ext[$v], $ext)) {
  253. $this->type = $v;
  254. $state = true;
  255. break;
  256. }
  257. }
  258. if (!$state) {
  259. return '文件格式不符合要求';
  260. }
  261. $this->config['ext'] = $ext;
  262. return true;
  263. }
  264. protected function checkSize($size)
  265. {
  266. $set = $this->config['size'];
  267. if (!$set) {
  268. $set = 2;
  269. }
  270. $set = $set * 1048576;
  271. if ($size > $set) {
  272. return '文件不能超过'.$set.'MB';
  273. }
  274. $this->config['size'] = $size;
  275. }
  276. protected function checkLimit($info)
  277. {
  278. if (isset($this->config['limit']) && $this->config['limit'] == 2 && $info[0] < $info[1]) {
  279. return '文件高度不能超过文件宽度';
  280. } elseif (isset($this->config['limit']) && $this->config['limit'] == 3 && $info[0] > $info[1]) {
  281. return '文件宽度不能超过文件高度';
  282. } elseif ($this->config['min_width'] > 0 && $this->config['min_width'] > $info[0]) {
  283. return '文件宽度不能小于' . $this->config['min_width'] . 'px';
  284. } elseif ($this->config['min_height'] > 0 && $this->config['min_height'] > $info[1]) {
  285. return '文件高度不能小于' . $this->config['min_height'] . 'px';
  286. }
  287. $this->config['width'] = $info[0];
  288. $this->config['height'] = $info[1];
  289. }
  290. protected function getDest(&$name, $ext, $uid)
  291. {
  292. if ($uid) {
  293. $id = abs(intval($uid));
  294. $id = sprintf("%09d", $id);
  295. $dest = DIRECTORY_SEPARATOR . substr($id, 0, 3) . DIRECTORY_SEPARATOR . substr($id, 3, 2) . DIRECTORY_SEPARATOR . substr($id, 5, 2) . DIRECTORY_SEPARATOR . $uid . '.' . $ext;
  296. $name = $uid;
  297. } else {
  298. if (!strpos($name, '_cr_')) {
  299. $name = md5($name);
  300. }
  301. $path = array_slice(str_split($name, 2), 0, 3);
  302. $dest = implode(DIRECTORY_SEPARATOR, $path) . DIRECTORY_SEPARATOR . $name . '.' . $ext;
  303. }
  304. return $dest;
  305. }
  306. protected function getExtByMine($mine, $flip = false, $result = false)
  307. {
  308. $config = array
  309. (
  310. 'application/envoy' => 'evy',
  311. 'application/fractals' => 'fif',
  312. 'application/futuresplash' => 'spl',
  313. 'application/hta' => 'hta',
  314. 'application/internet-property-stream' => 'acx',
  315. 'application/mac-binhex40' => 'hqx',
  316. 'application/msword' => 'doc',
  317. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
  318. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
  319. 'video/x-m4v' => 'mp4',
  320. 'video/mp4' => 'mp4',
  321. 'application/octet-stream' => 'exe',
  322. 'application/oda' => 'oda',
  323. 'application/olescript' => 'axs',
  324. 'application/pdf' => 'pdf',
  325. 'application/pics-rules' => 'prf',
  326. 'application/pkcs10' => 'p10',
  327. 'application/pkix-crl' => 'crl',
  328. 'application/postscript' => 'ai',
  329. 'application/postscript' => 'eps',
  330. 'application/postscript' => 'ps',
  331. 'application/rtf' => 'rtf',
  332. 'application/set-payment-initiation' => 'setpay',
  333. 'application/set-registration-initiation' => 'setreg',
  334. 'application/vnd.ms-excel' => 'xls',
  335. 'application/vnd.ms-outlook' => 'msg',
  336. 'application/vnd.ms-pkicertstore' => 'sst',
  337. 'application/vnd.ms-pkiseccat' => 'cat',
  338. 'application/vnd.ms-pkistl' => 'stl',
  339. 'application/vnd.ms-powerpoint' => 'ppt',
  340. 'application/vnd.ms-project' => 'mpp',
  341. 'application/vnd.ms-works' => 'wps',
  342. 'application/winhlp' => 'hlp',
  343. 'application/x-bcpio' => 'bcpio',
  344. 'application/x-cdf' => 'cdf',
  345. 'application/x-compress' => 'z',
  346. 'application/x-compressed' => 'tgz',
  347. 'application/x-cpio' => 'cpio',
  348. 'application/x-csh' => 'csh',
  349. 'application/x-director' => 'dir',
  350. 'application/x-dvi' => 'dvi',
  351. 'application/x-gtar' => 'gtar',
  352. 'application/x-gzip' => 'gz',
  353. 'application/x-hdf' => 'hdf',
  354. 'application/x-internet-signup' => 'isp',
  355. 'application/x-iphone' => 'iii',
  356. 'application/x-javascript' => 'js',
  357. 'application/x-latex' => 'latex',
  358. 'application/x-msaccess' => 'mdb',
  359. 'application/x-mscardfile' => 'crd',
  360. 'application/x-msclip' => 'clp',
  361. 'application/x-msdownload' => 'dll',
  362. 'application/x-msmediaview' => 'mvb',
  363. 'application/x-msmetafile' => 'wmf',
  364. 'application/x-msmoney' => 'mny',
  365. 'application/x-mspublisher' => 'pub',
  366. 'application/x-msschedule' => 'scd',
  367. 'application/x-msterminal' => 'trm',
  368. 'application/x-mswrite' => 'wri',
  369. 'application/x-netcdf' => 'cdf',
  370. 'application/x-netcdf' => 'nc',
  371. 'application/x-perfmon' => 'pma',
  372. 'application/x-pkcs12' => 'p12',
  373. 'application/x-pkcs12' => 'pfx',
  374. 'application/x-pkcs7-certificates' => 'p7b',
  375. 'application/x-pkcs7-certreqresp' => 'p7r',
  376. 'application/x-pkcs7-mime' => 'p7c',
  377. 'application/x-pkcs7-signature' => 'p7s',
  378. 'application/x-sh' => 'sh',
  379. 'application/x-shar' => 'shar',
  380. 'application/x-shockwave-flash' => 'swf',
  381. 'application/x-stuffit' => 'sit',
  382. 'application/x-sv4cpio' => 'sv4cpio',
  383. 'application/x-sv4crc' => 'sv4crc',
  384. 'application/x-tar' => 'tar',
  385. 'application/x-tcl' => 'tcl',
  386. 'application/x-tex' => 'tex',
  387. 'application/x-texinfo' => 'texi',
  388. 'application/x-texinfo' => 'texinfo',
  389. 'application/x-troff' => 'roff',
  390. 'application/x-troff' => 't',
  391. 'application/x-troff' => 'tr',
  392. 'application/x-troff-man' => 'man',
  393. 'application/x-troff-me' => 'me',
  394. 'application/x-troff-ms' => 'ms',
  395. 'application/x-ustar' => 'ustar',
  396. 'application/x-wais-source' => 'src',
  397. 'application/x-x509-ca-cert' => 'cer',
  398. 'application/ynd.ms-pkipko' => 'pko',
  399. 'application/zip' => 'zip',
  400. 'audio/basic' => 'au',
  401. 'audio/basic' => 'snd',
  402. 'audio/mid' => 'mid',
  403. 'audio/mid' => 'rmi',
  404. 'audio/mpeg' => 'mp3',
  405. 'audio/mp3' => 'mp3',
  406. 'audio/x-aiff' => 'aif',
  407. 'audio/x-aiff' => 'aifc',
  408. 'audio/x-aiff' => 'aiff',
  409. 'audio/x-mpegurl' => 'm3u',
  410. 'audio/x-pn-realaudio' => 'ram',
  411. 'audio/x-wav' => 'wav',
  412. 'image/png' => 'png',
  413. 'image/bmp' => 'bmp',
  414. 'image/cis-cod' => 'cod',
  415. 'image/gif' => 'gif',
  416. 'image/ief' => 'ief',
  417. 'image/jpeg' => 'jpg',
  418. 'image/pipeg' => 'jfif',
  419. 'image/svg+xml' => 'svg',
  420. 'image/tiff' => 'tif',
  421. 'image/tiff' => 'tiff',
  422. 'image/x-cmu-raster' => 'ras',
  423. 'image/x-cmx' => 'cmx',
  424. 'image/x-icon' => 'ico',
  425. 'image/x-portable-anymap' => 'pnm',
  426. 'image/x-portable-bitmap' => 'pbm',
  427. 'image/x-portable-graymap' => 'pgm',
  428. 'image/x-portable-pixmap' => 'ppm',
  429. 'image/x-rgb' => 'rgb',
  430. 'image/x-xbitmap' => 'xbm',
  431. 'image/x-xpixmap' => 'xpm',
  432. 'image/x-xwindowdump' => 'xwd',
  433. 'message/rfc822' => 'mht',
  434. 'message/rfc822' => 'mhtml',
  435. 'message/rfc822' => 'nws',
  436. 'text/css' => 'css',
  437. 'text/h323' => '323',
  438. 'text/html' => 'html',
  439. 'text/iuls' => 'uls',
  440. 'text/plain' => 'txt',
  441. 'text/richtext' => 'rtx',
  442. 'text/scriptlet' => 'sct',
  443. 'text/tab-separated-values' => 'tsv',
  444. 'text/webviewhtml' => 'htt',
  445. 'text/x-component' => 'htc',
  446. 'text/x-setext' => 'etx',
  447. 'text/x-vcard' => 'vcf',
  448. 'video/mpeg' => 'mpeg',
  449. 'video/quicktime' => 'mov',
  450. 'video/x-ms-asf' => 'asx',
  451. 'video/x-msvideo' => 'avi',
  452. 'video/x-sgi-movie' => 'movie',
  453. 'x-world/x-vrml' => 'flr',
  454. 'application/x-rar' => 'rar',
  455. 'application/vnd.android.package-archive' => 'apk',
  456. 'audio/webm' => 'webm',
  457. 'video/webm' => 'webm',
  458. 'audio/x-m4a' => 'm4a',
  459. 'image/webp' => 'webp',
  460. );
  461. if ($flip) {
  462. $config = array_flip($config);
  463. }
  464. if (!$mine) {
  465. return $config;
  466. }
  467. $mine = trim($mine);
  468. if (isset($config[$mine])) {
  469. return $config[$mine];
  470. } else {
  471. return false;
  472. }
  473. }
  474. protected function getExtByByte($file)
  475. {
  476. $file = fopen($file, "rb");
  477. $bin = fread($file, 2);
  478. fclose($file);
  479. $strInfo = @unpack("c2chars",$bin);
  480. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  481. $fileType = '';
  482. switch ($typeCode) {
  483. case 7790:
  484. $fileType = 'exe';
  485. break;
  486. case 7784:
  487. $fileType = 'midi';
  488. break;
  489. case 8297:
  490. $fileType = 'rar';
  491. break;
  492. case 255216:
  493. $fileType = 'jpg';
  494. break;
  495. case 7173:
  496. $fileType = 'gif';
  497. break;
  498. case 13780:
  499. $fileType = 'png';
  500. break;
  501. case 6677:
  502. $fileType = 'bmp';
  503. break;
  504. case 6787:
  505. $fileType = 'swf';
  506. break;
  507. case 6063;
  508. $fileType = 'php|xml';
  509. break;
  510. case 6033:
  511. $fileType = 'html|htm|shtml';
  512. break;
  513. case 8075:
  514. $fileType = 'zip';
  515. break;
  516. case 6782:
  517. case 1310:
  518. $fileType = 'txt';
  519. break;
  520. case 4742:
  521. $fileType = 'js';
  522. break;
  523. case 8273:
  524. $fileType = 'wav';
  525. break;
  526. case 7368:
  527. $fileType = 'mp3';
  528. break;
  529. case 3780:
  530. $fileType = 'pdf';
  531. break;
  532. case 4545:
  533. $fileType = 'pem';
  534. break;
  535. case 7597:
  536. $fileType = 'fbx';
  537. break;
  538. default:
  539. $fileType = 'unknown'.$typeCode;
  540. break;
  541. }
  542. if ($strInfo['chars1'] == '-1' && $strInfo['chars2'] == '-40') {
  543. return 'jpg';
  544. }
  545. if ($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80') {
  546. return 'png';
  547. }
  548. if ($strInfo['chars1'] == '-48' && $strInfo['chars2'] == '-49') {
  549. return 'msi';
  550. }
  551. return $fileType;
  552. }
  553. }