downChajian.php 6.1 KB

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