Save.php 21 KB

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