Save.php 21 KB

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