fileModel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. class fileClassModel extends Model
  3. {
  4. public function initModel()
  5. {
  6. $this->fileall = ',aac,ace,ai,ain,amr,app,arj,asf,asp,aspx,av,avi,bin,bmp,cab,cad,cat,cdr,chm,com,css,cur,dat,db,dll,dmv,doc,docx,dot,dps,dpt,dwg,dxf,emf,eps,et,ett,exe,fla,ftp,gif,hlp,htm,html,icl,ico,img,inf,ini,iso,jpeg,jpg,js,m3u,max,mdb,mde,mht,mid,midi,mov,mp3,mp4,mpeg,mpg,msi,nrg,ocx,ogg,ogm,pdf,php,png,pot,ppt,pptx,psd,pub,qt,ra,ram,rar,rm,rmvb,rtf,swf,tar,tif,tiff,txt,url,vbs,vsd,vss,vst,wav,wave,wm,wma,wmd,wmf,wmv,wps,wpt,wz,xls,xlsx,xlt,xml,zip,';
  7. $this->mimitype = c('file')->getAllMime();
  8. }
  9. /***
  10. * 添加预览和下载记录
  11. */
  12. public function addlogs($fileid, $type)
  13. {
  14. $uarr = array();
  15. $uarr['fileid'] = $fileid;
  16. $uarr['type'] = $type;
  17. $uarr['optname'] = $this->adminname;
  18. $uarr['optid'] = $this->adminid;
  19. $uarr['ip'] = $this->rock->ip;
  20. $uarr['web'] = $this->rock->web;
  21. $uarr['optdt'] = $this->rock->now;
  22. return m('files')->insert($uarr);
  23. }
  24. public function getmime($lx)
  25. {
  26. if(!isset($this->mimitype[$lx]))$lx = 'unkown';
  27. return $this->mimitype[$lx];
  28. }
  29. public function getfile($mtype, $mid, $where='')
  30. {
  31. if($where=='')$where = "`mtype`='$mtype' and `mid` in($mid)";
  32. $rows = $this->getall("$where order by `id`");
  33. return $rows;
  34. }
  35. public function addfile($fileid, $mtype, $mid, $mknum='')
  36. {
  37. if(!$this->isempt($fileid)){
  38. $this->update("`mtype`='$mtype',`mid`='$mid',`mknum`='$mknum'", "`id` in($fileid) and `mid`=0");
  39. }
  40. }
  41. public function getstr($mtype, $mid, $lx=0, $where='')
  42. {
  43. $filearr = $this->getfile($mtype, $mid, $where);
  44. $fstr = $this->getstrstr($filearr, $lx);
  45. return $fstr;
  46. }
  47. public function getstrstr($filearr, $lx)
  48. {
  49. $fstr = '';
  50. if($filearr)foreach($filearr as $k=>$rs){
  51. // if($k>0)$fstr.='<br>';
  52. $fstr .= $this->getfilestr($rs, $lx);
  53. }
  54. $fstr .= '<div style="clear:both;"></div>';
  55. return $fstr;
  56. }
  57. //获取聚合文件
  58. public function getallstr($filearr, $mid, $lx=0)
  59. {
  60. $farr = array();
  61. if($filearr)foreach($filearr as $k=>$rs){
  62. if($rs['mid']==$mid)$farr[] = $rs;
  63. }
  64. return $this->getstrstr($farr, $lx);
  65. }
  66. public function isimg($ext)
  67. {
  68. return $this->contain('|jpg|png|gif|bmp|jpeg|', '|'.$ext.'|');
  69. }
  70. public function isoffice($ext)
  71. {
  72. return contain('|doc|docx|xls|xlsx|ppt|pptx|pdf|', '|'.$ext.'|');
  73. }
  74. public function isbianju($ext)
  75. {
  76. return contain('|doc|docx|xls|xlsx|ppt|pptx|', '|'.$ext.'|');
  77. }
  78. public function isyulan($ext)
  79. {
  80. return contain(',txt,log,html,htm,js,php,php3,mp4,md,cs,sql,java,json,css,asp,aspx,shtml,cpp,c,vbs,jsp,xml,bat,ini,conf,sh,', ','.$ext.',');
  81. }
  82. //判断是否可预览
  83. public function isview($ext)
  84. {
  85. if($this->isimg($ext))return true;
  86. if($this->isoffice($ext))return true;
  87. if($this->isyulan($ext))return true;
  88. return contain(',mp3,ogg,mp4,', ','.$ext.',');
  89. }
  90. //获取缩略图的路径
  91. public function getthumbpath($rs)
  92. {
  93. $thumbpath = $this->rock->repempt(arrvalue($rs, 'thumbpath'));
  94. if(!isempt($thumbpath)){
  95. if(substr($thumbpath,0,4)=='http')return $thumbpath;
  96. if(!file_exists($thumbpath))$thumbpath='';
  97. }
  98. if(isempt($thumbpath))$thumbpath = arrvalue($rs, 'thumbplat');
  99. if(!isempt($thumbpath)){
  100. $thumbpath = str_replace('{FILEURL}', getconfig('rockfile_url'), $thumbpath);
  101. $thumbpath = $this->rock->gethttppath($thumbpath);
  102. }
  103. return $thumbpath;
  104. }
  105. //$lx=2详情,$lx=3是flow.php getdatalog下读取的
  106. public function getfilestr($rs, $lx=0)
  107. {
  108. $fstr= '';
  109. if(!$rs)return $fstr;
  110. $str = $this->rock->jm->strrocktoken(array('a'=>'down','id'=>$rs['id']));
  111. $url = ''.URL.'index.php?rocktoken='.$str.'';
  112. $str = 'href="'.$url.'"';
  113. $ext = $rs['fileext'];
  114. $id = $rs['id'];
  115. $isimg= $this->isimg($ext);
  116. $strd= $str;
  117. if($lx==1)$str='href="javascript:;" onclick="return js.downshow('.$rs['id'].')"';
  118. if($lx>=2){
  119. $paths = $rs['filepath'];
  120. if(arrvalue($rs,'filepathout'))$paths = $rs['filepathout'];
  121. if(!$isimg)$paths='';
  122. $str='href="javascript:;" onclick="return c.downshow('.$rs['id'].',\''.$ext.'\',\''.$paths.'\',\''.$rs['filenum'].'\')"';//详情上预览
  123. }
  124. $flx = $rs['fileext'];
  125. if(!$this->contain($this->fileall,','.$flx.','))$flx='wz';
  126. $str1 = '';
  127. $imurl = ''.URL.'web/images/fileicons/'.$flx.'.gif';
  128. $thumbpath = $this->getthumbpath($rs);
  129. if($isimg && !isempt($thumbpath))$imurl = $thumbpath;
  130. $isdel = file_exists($rs['filepath']);
  131. if(substr($rs['filepath'],0,4)=='http')$isdel=true;
  132. if(!isempt($rs['filenum']))$isdel=true;
  133. if(arrvalue($rs,'filepathout'))$isdel=true;
  134. $fstr .='<div style="float:left;width:30%;margin-right:5px;"><a temp="clo" '.$str.'><img src="'.$imurl.'" align="absmiddle" style="width:100%;height:100%;object-fit:cover;"></a>';
  135. // if($isdel){
  136. // $fstr .=' '.$rs['filename'].'';
  137. // }else{
  138. // $fstr .=' <s>'.$rs['filename'].'</s>';
  139. // }
  140. //
  141. // $fstr .=' <span style="color:#aaaaaa;font-size:12px">('.$rs['filesizecn'].')</span>';
  142. $filenum = arrvalue($rs,'filenum');
  143. //if(!isempt($filenum)){
  144. $strd = 'href="javascript:;" onclick="js.fileopt('.$id.', 1)"';//下载的链接
  145. //}
  146. // if($lx>=2){
  147. // if($isdel){
  148. // $fstr .= ' <a temp="clo" '.$strd.' class="blue">下载</a>';
  149. // if($isimg || $this->isoffice($ext) || $this->isyulan($ext))
  150. // $fstr .= '&nbsp; <a temp="clo" '.$str.' class="blue">预览</a>';
  151. // if($this->isbianju($ext) && $lx==3)$fstr .='`'.$rs['id'].'`'; //用于编辑
  152. // }else{
  153. // $fstr .= ' <span style="color:#aaaaaa;font-size:12px">已删除</span>';
  154. // }
  155. // }
  156. $fstr .= '</div>';
  157. return $fstr;
  158. }
  159. public function getfiles($mtype, $mid)
  160. {
  161. $rows = $this->getall("`mtype`='$mtype' and `mid`='$mid' order by `id`");
  162. foreach($rows as $k=>$rs){
  163. $rows[$k]['status'] = 4;
  164. }
  165. return $rows;
  166. }
  167. public function getfilepath($mtype, $mid)
  168. {
  169. $rows = $this->getfiles($mtype, $mid);
  170. $str = '';
  171. $nas = '';
  172. $st1 = '';
  173. foreach($rows as $k=>$rs){
  174. $path = $rs['filepath'];
  175. $outu = arrvalue($rs, 'filepathout');
  176. if(isempt($outu)){
  177. if(!isempt($path) && (file_exists($path) || substr($path,0,4)=='http') ){
  178. $str .= ','.$path.'';
  179. $nas .= ','.$rs['filename'].'';
  180. }
  181. }else{
  182. if($st1!='')$st1.='<br>';
  183. $st1.=''.$rs['filename'].'('.$rs['filesizecn'].')&nbsp;<a target="_blank" href="'.$outu.'">下载</a>';
  184. }
  185. }
  186. if($str!=''){
  187. $str = substr($str, 1);
  188. $nas = substr($nas, 1);
  189. }
  190. return array($str, $nas, $st1);
  191. }
  192. public function copyfile($mtype, $mid)
  193. {
  194. $rows = $this->getall("`mtype`='$mtype' and `mid`='$mid' order by `id`");
  195. $arr = array();
  196. foreach($rows as $k=>$rs){
  197. $inuar = $rs;
  198. if(isempt($rs['filepath']) || (substr($rs['filepath'],0,4)!='http' && !arrvalue($rs,'filepathout') && !file_exists($rs['filepath'])))continue;
  199. unset($inuar['id']);
  200. $oid = $rs['id'];
  201. $inuar['adddt'] = $this->rock->now;
  202. $inuar['optid'] = $this->adminid;
  203. $inuar['optname'] = $this->adminname;
  204. $inuar['downci'] = '0';
  205. $inuar['mtype'] = '';
  206. $inuar['mid'] = '0';
  207. $inuar['oid'] = $oid;
  208. $ids = (int)$this->getmou('id','oid='.$oid.' and `mid`=0');
  209. if($ids==0){
  210. $this->insert($inuar);
  211. $inuar['id'] = $this->db->insert_id();
  212. }else{
  213. $inuar['id'] = $ids;
  214. }
  215. $inuar['status'] = 4;
  216. $arr[] = $inuar;
  217. }
  218. return $arr;
  219. }
  220. public function delfiles($mtype, $mid)
  221. {
  222. $where = "`mtype`='$mtype' and `mid`='$mid'";
  223. $this->delfile('', $where);
  224. }
  225. public function delfile($sid='', $where='')
  226. {
  227. if($sid!='')$where = "`id` in ($sid)";
  228. if($where=='')return;
  229. $rows = $this->getall($where);
  230. foreach($rows as $k=>$rs){
  231. $path = $rs['filepath'];
  232. if(!$this->isempt($path) && substr($path,0,4)!='http' && file_exists($path))unlink($path);
  233. $path = $rs['thumbpath'];
  234. if(!$this->isempt($path) && substr($path,0,4)!='http' && file_exists($path))unlink($path);
  235. $path = $rs['pdfpath'];
  236. if(!$this->isempt($path) && substr($path,0,4)!='http' && file_exists($path))unlink($path);
  237. if(!isempt($rs['filenum']))c('rockqueue')->push('flow,uptodelete', array('filenum'=>$rs['filenum']));//发送同步删除
  238. }
  239. $this->delete($where);
  240. }
  241. public function fileheader($filename,$ext='xls', $size=0)
  242. {
  243. $mime = $this->getmime($ext);
  244. $filename = $this->iconvutf8(str_replace(' ','',$filename));
  245. header('Content-type:'.$mime.'');
  246. header('Accept-Ranges: bytes');
  247. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  248. header('Pragma: no-cache');
  249. if($size>0)header('Content-Length:'.$size.'');
  250. header('Expires: 0');
  251. header('Content-disposition:attachment;filename='.$filename.'');
  252. header('Content-Transfer-Encoding: binary');
  253. }
  254. //渣渣IE才需要转化,真是醉了
  255. public function iconvutf8($text) {
  256. if(contain($this->rock->web,'IE')){
  257. return iconv('utf-8','gb2312', $text);
  258. }else{
  259. return $text;
  260. }
  261. }
  262. public function show($id,$qx=false)
  263. {
  264. if($id==0)exit('Sorry!');
  265. $rs = $this->getone($id);
  266. if(!$rs)exit('504 Not find files');
  267. if(!$qx && !$this->isdownfile($rs))exit('404 No permission download');
  268. $this->update("`downci`=`downci`+1", $id);
  269. $this->addlogs($id, 1);
  270. $filepath = $rs['filepath'];
  271. $filename = $rs['filename'];
  272. $filesize = $rs['filesize'];
  273. $fileext = $rs['fileext'];
  274. $filepathout= $rs['filepathout'];
  275. if($this->rock->contain($filepath,'http')){
  276. header('location:'.$filepath.'');
  277. }else{
  278. //2018-07-18只能下载upload/images下的文件
  279. $ielx = substr($filepath,0,strlen(UPDIR));
  280. $ielx1 = substr($filepath,0,6);
  281. if($ielx!=UPDIR && $ielx1!='upload' && $ielx1!='images')exit('无效操作1');
  282. if(!file_exists($filepath)){
  283. if(!isempt($filepathout))header('location:'.$filepathout.'');
  284. exit('404 Not find files');
  285. }
  286. if(!contain($filename,'.'.$fileext.''))$filename .= '.'.$fileext.'';
  287. $filesize = filesize($filepath);
  288. $this->fileheader($filename, $fileext, $filesize);
  289. if(substr($filepath,-4)=='temp'){
  290. $content = file_get_contents($filepath);
  291. echo base64_decode($content);
  292. }else{
  293. if($this->rock->iswebbro(0) && $this->rock->iswebbro(5)){
  294. header('location:'.$filepath.'');
  295. return;
  296. }
  297. ob_clean();flush();readfile($filepath);return;
  298. if($filesize > 5*1024*1024){
  299. header('location:'.$filepath.'');
  300. }else{
  301. echo file_get_contents($filepath);
  302. }
  303. }
  304. }
  305. }
  306. //这个是下载temp文件的
  307. public function download($id)
  308. {
  309. if($id==0)exit('Sorry!');
  310. $rs = $this->getone($id);
  311. if(!$rs)exit('504 Not find files');
  312. if(!$this->isdownfile($rs))exit('No permission download');
  313. $filepath = $rs['filepath'];
  314. $ielx = substr($filepath,0,strlen(UPDIR));
  315. $ielx1 = substr($filepath,0,6);
  316. if($ielx!=UPDIR && $ielx1!='upload' && $ielx1!='images')exit('无效操作2');
  317. if(!file_exists($filepath))exit('404 Not find files');
  318. $this->update("`downci`=`downci`+1", $id);
  319. $this->addlogs($id, 1);
  320. $filename = $rs['filename'];
  321. $filesize = $rs['filesize'];
  322. if(substr($filepath,-4)=='temp'){
  323. Header("Content-type: application/octet-stream");
  324. header('Accept-Ranges: bytes');
  325. Header("Accept-Length: ".$filesize);
  326. Header("Content-Length: ".$filesize);
  327. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  328. header('Pragma: no-cache');
  329. header('Expires: 0');
  330. $content = file_get_contents($filepath);
  331. echo base64_decode($content);
  332. }else{
  333. }
  334. }
  335. //判断是否有下载文件的权限
  336. private function isdownfile($rs)
  337. {
  338. //return true; //如果不想权限判断就去掉注释直接返回true
  339. $uid = $this->adminid;
  340. if(arrvalue($rs,'optid')==$uid)return true;
  341. $table = arrvalue($rs,'mtype');
  342. $mid = (int)arrvalue($rs,'mid','0');
  343. if(!isempt($table) && $mid>0){
  344. $to = m('reads')->rows("`table`='$table' and `mid`='$mid' and `optid`='$uid'");
  345. if($to>0)return true;
  346. }
  347. $mknum = arrvalue($rs,'mknum');
  348. if(!isempt($mknum)){
  349. $mknuma = explode('|', $mknum);
  350. $num = $mknuma[0];
  351. $mid = (int)arrvalue($mknuma, 1, $mid);
  352. if($mid>0){
  353. $flow = m('flow')->initflow($num, $mid, false);
  354. if($flow->isreadqx(1))return true;
  355. }
  356. }
  357. if($table=='im_mess'){
  358. $ors = m($table)->getone($mid);
  359. if($ors){
  360. $receuid = $ors['receuid'];
  361. if(contain(','.$receuid.',',','.$uid.','))return true;
  362. }
  363. }
  364. if($table=='word'){
  365. $ors = m('word')->getone("`fileid`='".$rs['id']."'");
  366. if($ors){
  367. $cid = $ors['cid'];
  368. $flow = m('flow')->initflow('worc', $cid, false);
  369. if($flow->isreadqx(1))return true;
  370. $flow = m('flow')->initflow('word', $ors['id'], false);
  371. if($flow->isreadqx(1))return true;
  372. }
  373. }
  374. return false;
  375. }
  376. }