Save.php 19 KB

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