Upload.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace app\common\model;
  3. use app\common\util\Ftp as ftpOper;
  4. class Upload extends Base {
  5. public function api($file_path,$config)
  6. {
  7. if(empty($config)){
  8. return $file_path;
  9. }
  10. if ($config['mode'] == '2') {
  11. $config['mode'] = 'upyun';
  12. }
  13. elseif ($config['mode'] == '3'){
  14. $config['mode'] = 'qiniu';
  15. }
  16. elseif ($config['mode'] == '4') {
  17. $config['mode'] = 'ftp';
  18. }
  19. elseif ($config['mode'] == '5') {
  20. $config['mode'] = 'weibo';
  21. }
  22. if(!in_array($config['mode'],['local','remote'])){
  23. $cp = 'app\\common\\extend\\upload\\' . ucfirst($config['mode']);
  24. if (class_exists($cp)) {
  25. $c = new $cp;
  26. $file_path = $c->submit($file_path);
  27. }
  28. }
  29. return str_replace(['http:','https:'],'mac:',$file_path);
  30. }
  31. public function upload($p=[])
  32. {
  33. $param = input();
  34. if(!empty($p)){
  35. $param = array_merge($param,$p);
  36. }
  37. $param['from'] = empty($param['from']) ? '' : $param['from'];
  38. $param['input'] = empty($param['input']) ? 'file' : $param['input'];
  39. $param['flag'] = empty($param['flag']) ? 'vod' : $param['flag'];
  40. $param['thumb'] = empty($param['thumb']) ? '0' : $param['thumb'];
  41. $param['thumb_class'] = empty($param['thumb_class']) ? '' : $param['thumb_class'];
  42. $param['user_id'] = empty($param['user_id']) ? '0' : $param['user_id'];
  43. $base64_img = $param['imgdata'];
  44. $data = [];
  45. $config = config('maccms.site');
  46. $pre= $config['install_dir'];
  47. $upload_image_ext = 'jpg,jpeg,png,gif,webp';
  48. $upload_file_ext = 'doc,docx,xls,xlsx,ppt,pptx,pdf,wps,txt,rar,zip,torrent';
  49. $upload_media_ext = 'rm,rmvb,avi,mkv,mp4,mp3';
  50. $add_rnd = false;
  51. $config = config('maccms.upload');
  52. if(!empty($param['from'])){
  53. $cp = 'app\\common\\extend\\editor\\' . ucfirst($param['from']);
  54. if (class_exists($cp)) {
  55. $c = new $cp;
  56. $c->front($param);
  57. }
  58. else{
  59. return self::upload_return(lang('admin/upload/not_find_extend'), '');
  60. }
  61. }
  62. else{
  63. $pre='';
  64. }
  65. // 上传附件路径
  66. $_upload_path = ROOT_PATH . 'upload' . '/' . $param['flag'] . '/' ;
  67. // 附件访问路径
  68. $_save_path = 'upload'. '/' . $param['flag'] . '/';
  69. if($param['flag']=='user'){
  70. $uniq = $param['user_id'] % 10;
  71. $_upload_path .= $uniq .'/';
  72. $_save_path .= $uniq .'/';
  73. $_save_name = $param['user_id'] . '.jpg';
  74. if(!file_exists($_save_path)){
  75. mac_mkdirss($_save_path);
  76. }
  77. }
  78. else{
  79. $ymd = date('Ymd');
  80. $n_dir = $ymd;
  81. for($i=1;$i<=100;$i++){
  82. $n_dir = $ymd .'-'.$i;
  83. $path1 = $_upload_path . $n_dir. '/';
  84. if(file_exists($path1)){
  85. $farr = glob($path1.'*.*');
  86. if($farr){
  87. $fcount = count($farr);
  88. if($fcount>999){
  89. continue;
  90. }
  91. else{
  92. break;
  93. }
  94. }
  95. else{
  96. break;
  97. }
  98. }
  99. else{
  100. break;
  101. }
  102. }
  103. $_save_name = $n_dir . '/' . md5(microtime(true));
  104. }
  105. if(!empty($base64_img)){
  106. if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
  107. $type = $result[2];
  108. if(in_array($type, explode(',', $upload_image_ext))){
  109. if(!file_put_contents($_save_path.$_save_name, base64_decode(str_replace($result[1], '', $base64_img)))){
  110. return self::upload_return(lang('admin/upload/upload_faild'), $param['from']);
  111. }
  112. $file_size = round(filesize('./'.$_save_path.$_save_name)/1024, 2);
  113. }
  114. else {
  115. return self::upload_return(lang('admin/upload/forbidden_ext'), $param['from']);
  116. }
  117. }
  118. else{
  119. return self::upload_return(lang('admin/upload/no_input_file'), $param['from']);
  120. }
  121. }
  122. else {
  123. $file = request()->file($param['input']);
  124. if (empty($file)) {
  125. return self::upload_return(lang('admin/upload/no_input_file'), $param['from']);
  126. }
  127. if ($file->getMime() == 'text/x-php') {
  128. return self::upload_return(lang('admin/upload/forbidden_ext'), $param['from']);
  129. }
  130. if ($file->checkExt($upload_image_ext)) {
  131. $type = 'image';
  132. } elseif ($file->checkExt($upload_file_ext)) {
  133. $type = 'file';
  134. } elseif ($file->checkExt($upload_media_ext)) {
  135. $type = 'media';
  136. } else {
  137. return self::upload_return(lang('admin/upload/forbidden_ext'), $param['from']);
  138. }
  139. $upfile = $file->move($_upload_path,$_save_name);
  140. if (!is_file($_upload_path.$upfile->getSaveName())) {
  141. return self::upload_return(lang('admin/upload/upload_faild'), $param['from']);
  142. }
  143. $file_size = round($upfile->getInfo('size')/1024, 2);
  144. $_save_name = str_replace('\\', '/', $upfile->getSaveName());
  145. }
  146. $resource = fopen($_save_path.$_save_name, 'rb');
  147. $fileSize = filesize($_save_path.$_save_name);
  148. fseek($resource, 0);
  149. if ($fileSize>512){
  150. $hexCode = bin2hex(fread($resource, 512));
  151. fseek($resource, $fileSize - 512);
  152. $hexCode .= bin2hex(fread($resource, 512));
  153. } else {
  154. $hexCode = bin2hex(fread($resource, $fileSize));
  155. }
  156. fclose($resource);
  157. if(preg_match("/(3c25.*?28.*?29.*?253e)|(3c3f.*?28.*?29.*?3f3e)|(3C534352495054)|(2F5343524950543E)|(3C736372697074)|(2F7363726970743E)/is", $hexCode)){
  158. return self::upload_return(lang('admin/upload/upload_safe'), $param['from']);
  159. }
  160. $file_count = 1;
  161. $data = [
  162. 'file' => $_save_path.$_save_name,
  163. 'type' => $type,
  164. 'size' => $file_size,
  165. 'flag' => $param['flag'],
  166. 'ctime' => request()->time(),
  167. 'thumb_class'=>$param['thumb_class'],
  168. ];
  169. $data['thumb'] = [];
  170. if($param['flag']=='user'){
  171. $add_rnd=true;
  172. $file = $_save_path.str_replace('\\', '/', $_save_name);
  173. $new_thumb = $param['user_id'] .'.jpg';
  174. $new_file = $_save_path . $new_thumb;
  175. try {
  176. $image = \think\Image::open('./' . $file);
  177. $t_size = explode('x', strtolower($GLOBALS['config']['user']['portrait_size']));
  178. if (!isset($t_size[1])) {
  179. $t_size[1] = $t_size[0];
  180. }
  181. $image->thumb($t_size[0], $t_size[1], 6)->save('./' . $new_file);
  182. $file_size = round(filesize('./' .$new_file)/1024, 2);
  183. }
  184. catch(\Exception $e){
  185. return self::upload_return(lang('admin/upload/make_thumb_faild'), $param['from']);
  186. }
  187. $update = [];
  188. $update['user_portrait'] = $new_file;
  189. $where = [];
  190. $where['user_id'] = $GLOBALS['user']['user_id'];
  191. $res = model('User')->where($where)->update($update);
  192. if ($res === false) {
  193. return self::upload_return(lang('index/portrait_err'), $param['from']);
  194. }
  195. }
  196. else {
  197. if ($type == 'image') {
  198. if ($config['watermark'] == 1) {
  199. model('Image')->watermark($data['file'], $config, $param['flag']);
  200. }
  201. if ($param['thumb'] == 1 && $config['thumb'] == 1) {
  202. $dd = model('Image')->makethumb($data['file'], $config, $param['flag']);
  203. if (is_array($dd)) {
  204. $data = array_merge($data, $dd);
  205. }
  206. }
  207. }
  208. }
  209. unset($upfile);
  210. if ($config['mode'] == 2) {
  211. $config['mode'] = 'upyun';
  212. }
  213. elseif ($config['mode'] == 3){
  214. $config['mode'] = 'qiniu';
  215. }
  216. elseif ($config['mode'] == 4) {
  217. $config['mode'] = 'ftp';
  218. }
  219. elseif ($config['mode'] == 5) {
  220. $config['mode'] = 'weibo';
  221. }
  222. $config['mode'] = strtolower($config['mode']);
  223. if(!in_array($config['mode'],['local','remote'])){
  224. $data['file'] = model('Upload')->api($data['file'],$config);
  225. if(!empty($data['thumb'])){
  226. $data['thumb'][0]['file'] = model('Upload')->api($data['thumb'][0]['file'],$config);
  227. }
  228. }
  229. if(!empty($param['from'])){
  230. if(substr($data['file'],0,4)!='http' && substr($data['file'],0,4)!='mac:'){
  231. $data['file'] = $pre. $data['file'];
  232. }
  233. else{
  234. $data['file'] = mac_url_content_img($data['file']);
  235. }
  236. }
  237. $tmp = $data['file'];
  238. if((substr($tmp,0,7) == "/upload")){
  239. $tmp = substr($tmp,1);
  240. }
  241. if((substr($tmp,0,6) == "upload")){
  242. $annex = [];
  243. $annex['annex_file'] = $tmp;
  244. $r = model('Annex')->infoData($annex);
  245. if($r['code']!==1){
  246. $annex['annex_type'] = $type;
  247. $annex['annex_size'] = $file_size;
  248. model('Annex')->saveData($annex);
  249. $tmp = $data['thumb'][0]['file'];
  250. if(!empty($tmp)){
  251. $file_size = filesize($tmp);
  252. $annex = [];
  253. $annex['annex_file'] = $tmp;
  254. $r = model('Annex')->infoData($annex);
  255. if($r['code']!==1){
  256. $annex['annex_type'] = $type;
  257. $annex['annex_size'] = $file_size;
  258. model('Annex')->saveData($annex);
  259. }
  260. }
  261. }
  262. }
  263. return self::upload_return(lang('admin/upload/upload_success'), $param['from'], 1, $data);
  264. }
  265. private function upload_return($info='',$from='',$status=0,$data=[])
  266. {
  267. $arr = [];
  268. if(!empty($from)){
  269. $cp = 'app\\common\\extend\\editor\\' . ucfirst($from);
  270. if (class_exists($cp)) {
  271. $c = new $cp;
  272. $arr = $c->back($info,$status,$data);
  273. }
  274. }
  275. elseif(ENTRANCE=='index'){
  276. $arr['msg'] = $info;
  277. $arr['code'] = $status;
  278. $arr['file'] = MAC_PATH . $data['file'] . '?'. mt_rand(1000, 9999);
  279. }
  280. else{
  281. $arr['msg'] = $info;
  282. $arr['code'] = $status;
  283. $arr['data'] = $data;
  284. }
  285. return $arr;
  286. }
  287. }