downChajian.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. 下载文件类插件
  4. */
  5. class downChajian extends Chajian{
  6. private $upobj;
  7. private $messign;
  8. protected function initChajian()
  9. {
  10. $this->messign = '';
  11. $this->upobj = c('upfile');
  12. }
  13. /**
  14. * 获取随机文件名
  15. */
  16. public function getallfilename($ext)
  17. {
  18. if(!is_dir(UPDIR))mkdir(UPDIR);
  19. $mkdir = ''.UPDIR.'/'.date('Y-m').'';
  20. if(!is_dir($mkdir))mkdir($mkdir);
  21. $allfilename = ''.$mkdir.'/'.date('d_His').''.rand(10,99).'.'.$ext.'';
  22. return $allfilename;
  23. }
  24. /**
  25. * 根据扩展名保存文件(一般邮件附件下载)
  26. */
  27. public function savefilecont($ext, $cont)
  28. {
  29. $bo = $this->upobj->issavefile($ext);
  30. if(isempt($cont))return;
  31. $file= '';
  32. if(!$bo){
  33. $file = $this->getallfilename('uptemp');
  34. $bo = @file_put_contents($file, base64_encode($cont));
  35. }else{
  36. $file = $this->getallfilename($ext);
  37. $bo = @file_put_contents($file, $cont);
  38. }
  39. if(!$bo){
  40. $file = '';
  41. }else{
  42. if($this->upobj->isimg($ext)){
  43. $bo = $this->upobj->isimgsave($ext, $file);
  44. if(!$bo)$file = '';
  45. }
  46. }
  47. return $file;
  48. }
  49. private function reutnmsg($msg)
  50. {
  51. $this->messign = $msg;
  52. return false;
  53. }
  54. //获取提示内容
  55. public function gettishi($msg1='')
  56. {
  57. $msg = $this->messign;
  58. if(isempt($msg))$msg = $msg1;
  59. return $msg;
  60. }
  61. /**
  62. * 根据内容创建文件
  63. */
  64. public function createimage($cont, $ext, $filename, $thumbnail='')
  65. {
  66. if(isempt($cont))return $this->reutnmsg('创建内容为空');
  67. $allfilename = $this->getallfilename($ext);
  68. $upses['oldfilename'] = $filename.'.'.$ext;
  69. $upses['fileext'] = $ext;
  70. @file_put_contents($allfilename, $cont);
  71. if(!file_exists($allfilename))return $this->reutnmsg('无法写入:'.$allfilename.'');
  72. $fileobj = getimagesize($allfilename);
  73. $mime = strtolower($fileobj['mime']);
  74. $next = 'jpg';
  75. if(contain($mime,'bmp'))$next = 'bmp';
  76. if($mime=='image/gif')$next = 'gif';
  77. if($mime=='image/png')$next = 'png';
  78. if($ext != $next){
  79. @unlink($allfilename);
  80. $ext = $next;
  81. $allfilename = $this->getallfilename($ext);
  82. $upses['oldfilename'] = $filename.'.'.$ext;
  83. $upses['fileext'] = $ext;
  84. @file_put_contents($allfilename, $cont);
  85. if(!file_exists($allfilename))return $this->reutnmsg('无法写入:'.$allfilename.'');
  86. }
  87. $filesize = filesize($allfilename);
  88. $filesizecn = $this->upobj->formatsize($filesize);
  89. $picw = $fileobj[0];
  90. $pich = $fileobj[1];
  91. if($picw==0||$pich==0){
  92. @unlink($allfilename);
  93. return $this->reutnmsg('无效的图片');;
  94. }
  95. $upses['filesize'] = $filesize;
  96. $upses['filesizecn'] = $filesizecn;
  97. $upses['allfilename'] = $allfilename;
  98. $upses['picw'] = $picw;
  99. $upses['pich'] = $pich;
  100. $arr = $this->uploadback($upses, $thumbnail);
  101. return $arr;
  102. }
  103. public function uploadback($upses, $thumbnail='', $subo=true)
  104. {
  105. if($thumbnail=='')$thumbnail='150x150';
  106. $msg = '';
  107. $data = array();
  108. if(is_array($upses)){
  109. $noasyn = $this->rock->get('noasyn');$noasyn = ''; //=yes就不同步到文件平台
  110. $noyaso = $this->rock->get('noyaso'); //=yes就不压缩
  111. $fileext= substr($upses['fileext'],0,10);
  112. $arrs = array(
  113. 'adddt' => $this->rock->now,
  114. 'valid' => 1,
  115. 'filename' => $this->replacefile($upses['oldfilename']),
  116. 'web' => $this->rock->web,
  117. 'ip' => $this->rock->ip,
  118. 'mknum' => $this->rock->get('sysmodenum'),
  119. //'mid' => $this->rock->get('sysmid','0'),
  120. 'fileext' => $fileext,
  121. 'filesize' => (int)$this->rock->get('filesize', $upses['filesize']),
  122. 'filesizecn'=> $upses['filesizecn'],
  123. 'filepath' => str_replace('../','',$upses['allfilename']),
  124. 'optid' => $this->adminid,
  125. 'optname' => $this->adminname,
  126. 'comid' => m('admin')->getcompanyid(),
  127. );
  128. $arrs['filetype'] = m('file')->getmime($fileext);
  129. $thumbpath = $arrs['filepath'];
  130. $sttua = explode('x', $thumbnail);
  131. $lw = (int)$sttua[0];
  132. $lh = (int)$sttua[1];
  133. //判断是不是需要压缩jpg和jpeg
  134. $compress = getconfig('imgcompress');
  135. if($compress && $noyaso!='yes' && ($fileext=='jpg' || $fileext=='jpeg') && $upses['picw']>0 && $upses['pich']>0){
  136. $sttuc = explode('x', $compress);
  137. $yw = (int)$sttuc[0];
  138. $yh = (int)arrvalue($sttuc, 1, 0);
  139. if($upses['picw'] > $yw || $upses['pich'] > $yh){
  140. $imgac = c('image', true);
  141. $imgac->createimg($thumbpath);
  142. $yspaht = $imgac->compress($yw, $yh);
  143. if($yspaht){
  144. if($thumbpath != $yspaht)unlink($thumbpath);
  145. $thumbpath = $yspaht;
  146. $arrs['filepath'] = $yspaht;
  147. $arrs['filesize'] = filesize($yspaht);
  148. $arrs['filesizecn'] = $this->upobj->formatsize($arrs['filesize']);
  149. }
  150. }
  151. }
  152. $shuiyin = $this->rock->get('shuiyin');
  153. if($upses['picw']>$lw || $upses['pich']>$lh){
  154. $imgaa = c('image', true);
  155. $imgaa->createimg($thumbpath);
  156. $thumbpath = $imgaa->thumbnail($lw, $lh, 1);
  157. if(contain($thumbpath, 'reimchat'))$shuiyin = 'size';
  158. if($shuiyin=='size'){
  159. $imgaa->createimg($thumbpath);
  160. $imgaa->addwater($arrs['filesizecn']);
  161. }
  162. }
  163. if($upses['picw'] == 0 && $upses['pich']==0)$thumbpath = '';
  164. $arrs['thumbpath'] = $thumbpath;
  165. //有缩略图先上传到云里 && $this->rock->get('sysuptype')=='img'
  166. if($thumbpath){
  167. $tarr = $this->uploadBase($thumbpath);
  168. if($tarr['success'] && isset($tarr['url']))$arrs['thumbplat'] = $tarr['url'];
  169. }
  170. $bo = $this->db->record('[Q]file',$arrs);
  171. if(!$bo)$this->reutnmsg($this->db->error());
  172. $id = $this->db->insert_id();
  173. $arrs['id'] = $id;
  174. $arrs['picw'] = $upses['picw'];
  175. $arrs['pich'] = $upses['pich'];
  176. $data= $arrs;
  177. //上传到上传的文件管理2021-08-09
  178. if(getconfig('rockfile_autoup') && $noasyn != 'yes'){
  179. $stime = time()+rand(3,6);
  180. if($subo)$stime=0;
  181. c('rockqueue')->push('flow,uptofile', array('fileid'=>$id), $stime);
  182. }
  183. //自动上传到腾讯云存储/阿里云oss存储
  184. if((getconfig('qcloudCos_autoup') || getconfig('alioss_autoup')) && $noasyn != 'yes'){
  185. $stime = time()+rand(3,6);
  186. if($subo)$stime=0;
  187. c('rockqueue')->sendfile($id, $stime);
  188. }
  189. if(arrvalue($arrs, 'thumbplat')){
  190. $data['filepath'] = $arrs['thumbplat'];
  191. $data['thumbpath'] = $arrs['thumbplat'];
  192. }
  193. }else{
  194. $data['msg'] = $upses;
  195. }
  196. return $data;
  197. }
  198. /**
  199. * 简单上传要调用
  200. */
  201. private function uploadBase($path)
  202. {
  203. if(getconfig('qcloudCos_autoup')){
  204. return c('qcloudCos')->uploadFile($path);
  205. }else{
  206. if(getconfig('alioss_autoup')){
  207. $obj = c('alioss');
  208. if(method_exists($obj, 'uploadFile'))return $obj->uploadFile($path);
  209. }
  210. }
  211. return returnerror();
  212. }
  213. //过滤特殊文件名
  214. private function replacefile($str)
  215. {
  216. $s = strtolower($str);
  217. $s2 = $s.'';
  218. $lvlaraa = explode(',',' ,user(),found_rows,(),\',",select*from,select*,%20,<,>,\,');
  219. $s = str_replace($lvlaraa, '', $s);
  220. if($s!=$s2)$str = $s;
  221. return $str;
  222. }
  223. //获取扩展名
  224. public function getext($file)
  225. {
  226. return strtolower(substr($file,strrpos($file,'.')+1));
  227. }
  228. }
粤ICP备19079148号