fileModel.php 13 KB

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