fileModel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. class fileClassModel extends Model
  3. {
  4. public $fileall,$mimitype;
  5. public $openmode = 0; //打开模式
  6. public function initModel()
  7. {
  8. $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,';
  9. $this->mimitype = c('file')->getAllMime();
  10. }
  11. /***
  12. * 添加预览和下载记录
  13. */
  14. public function addlogs($fileid, $type)
  15. {
  16. $uarr = array();
  17. $uarr['fileid'] = $fileid;
  18. $uarr['type'] = $type;
  19. $uarr['optname'] = $this->adminname;
  20. $uarr['optid'] = $this->adminid;
  21. $uarr['ip'] = $this->rock->ip;
  22. $uarr['web'] = $this->rock->web;
  23. $uarr['optdt'] = $this->rock->now;
  24. return m('files')->insert($uarr);
  25. }
  26. /**
  27. * 添加所选的文件提示
  28. */
  29. public function addxuan($fids,$xfids,$mknum)
  30. {
  31. if(!$xfids)return;
  32. $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);
  33. }
  34. public function getmime($lx)
  35. {
  36. if(!isset($this->mimitype[$lx]))$lx = 'unkown';
  37. return $this->mimitype[$lx];
  38. }
  39. public function getfile($mtype, $mid, $where='')
  40. {
  41. if($where=='')$where = "`mtype`='$mtype' and `mid` in($mid)";
  42. $rows = $this->getall("$where order by `id`");
  43. return $rows;
  44. }
  45. public function addfile($fileid, $mtype, $mid, $mknum='')
  46. {
  47. if(!$this->isempt($fileid)){
  48. $this->update("`mtype`='$mtype',`mid`='$mid',`mknum`='$mknum'", "`id` in($fileid) and `mid`=0");
  49. }
  50. }
  51. public function getstr($mtype, $mid, $lx=0, $where='')
  52. {
  53. $filearr = $this->getfile($mtype, $mid, $where);
  54. $fstr = $this->getstrstr($filearr, $lx);
  55. return $fstr;
  56. }
  57. public function getstrstr($filearr, $lx)
  58. {
  59. $fstr = '';
  60. if($filearr)foreach($filearr as $k=>$rs){
  61. if($k>0)$fstr.='<br>';
  62. $fstr .= $this->getfilestr($rs, $lx);
  63. }
  64. return $fstr;
  65. }
  66. //获取聚合文件
  67. public function getallstr($filearr, $mid, $lx=0)
  68. {
  69. $farr = array();
  70. if($filearr)foreach($filearr as $k=>$rs){
  71. if($rs['mid']==$mid)$farr[] = $rs;
  72. }
  73. return $this->getstrstr($farr, $lx);
  74. }
  75. public function isimg($ext)
  76. {
  77. return $this->contain('|jpg|png|gif|bmp|jpeg|', '|'.$ext.'|');
  78. }
  79. public function isoffice($ext)
  80. {
  81. return contain('|doc|docx|xls|xlsx|ppt|pptx|pdf|', '|'.$ext.'|');
  82. }
  83. public function isbianju($ext)
  84. {
  85. return contain('|doc|docx|xls|xlsx|ppt|pptx|', '|'.$ext.'|');
  86. }
  87. public function isyulan($ext)
  88. {
  89. 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.',');
  90. }
  91. //判断是否可预览
  92. public function isview($ext)
  93. {
  94. if($this->isimg($ext))return true;
  95. if($this->isoffice($ext))return true;
  96. if($this->isyulan($ext))return true;
  97. return contain(',mp3,ogg,mp4,amr,', ','.$ext.',');
  98. }
  99. //获取缩略图的路径
  100. public function getthumbpath($rs)
  101. {
  102. $thumbpath = $this->rock->repempt(arrvalue($rs, 'thumbpath'));
  103. if(!isempt($thumbpath)){
  104. if(substr($thumbpath,0,4)=='http')return $thumbpath;
  105. if(!file_exists($thumbpath))$thumbpath='';
  106. }
  107. $thumbplat = arrvalue($rs, 'thumbplat');
  108. if(!isempt($thumbplat)){
  109. $thumbpath = str_replace('{FILEURL}', getconfig('rockfile_url'), $thumbplat);
  110. }
  111. return $this->rock->gethttppath($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.'" onclick="js.imgview(this.src,false)" align="absmiddle" height=20 width=20>';
  143. if($isdel){
  144. $fstr .=' <span class="wrap">'.$rs['filename'].'</span>';
  145. }else{
  146. $fstr .=' <s class="wrap">'.$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($this->rock->get('openfrom')=='iosapp'){
  305. header('location:'.$filepath.'');
  306. return;
  307. }
  308. if($filesize > 10*1024*1024 && 1==1 && $fileext!='pdf'){
  309. header('location:'.$filepath.'');
  310. }else{
  311. ob_clean();flush();
  312. //$fileo = fopen($filepath,'rb');
  313. //while(!feof($fileo))echo fread($fileo, 1024*20);
  314. //fclose($fileo);
  315. readfile($filepath);
  316. }
  317. }
  318. }
  319. }
  320. //这个是下载temp文件的
  321. public function download($id)
  322. {
  323. if($id==0)exit('Sorry!');
  324. $rs = $this->getone($id);
  325. if(!$rs)exit('504 Not find files');
  326. if(!$this->isdownfile($rs))exit('No permission download');
  327. $filepath = $rs['filepath'];
  328. $ielx = substr($filepath,0,strlen(UPDIR));
  329. $ielx1 = substr($filepath,0,6);
  330. if($ielx!=UPDIR && $ielx1!='upload' && $ielx1!='images')exit('无效操作2');
  331. if(!file_exists($filepath))exit('404 Not find files');
  332. $this->update("`downci`=`downci`+1", $id);
  333. $this->addlogs($id, 1);
  334. $filename = $rs['filename'];
  335. $filesize = $rs['filesize'];
  336. if(substr($filepath,-4)=='temp'){
  337. Header("Content-type: application/octet-stream");
  338. header('Accept-Ranges: bytes');
  339. Header("Accept-Length: ".$filesize);
  340. Header("Content-Length: ".$filesize);
  341. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  342. header('Pragma: no-cache');
  343. header('Expires: 0');
  344. $content = file_get_contents($filepath);
  345. echo base64_decode($content);
  346. }else{
  347. }
  348. }
  349. //判断是否有下载文件的权限
  350. private function isdownfile($rs)
  351. {
  352. //return true; //如果不想权限判断就去掉注释直接返回true
  353. $uid = $this->adminid;
  354. if(arrvalue($rs,'optid')==$uid)return true;
  355. $table = arrvalue($rs,'mtype');
  356. $mid = (int)arrvalue($rs,'mid','0');
  357. if(!isempt($table) && $mid>0){
  358. $to = m('reads')->rows("`table`='$table' and `mid`='$mid' and `optid`='$uid'");
  359. if($to>0)return true;
  360. }
  361. $mknum = arrvalue($rs,'mknum');
  362. if(!isempt($mknum)){
  363. $mknuma = explode('|', $mknum);
  364. $num = $mknuma[0];
  365. $mid = (int)arrvalue($mknuma, 1, $mid);
  366. if($mid>0){
  367. $flow = m('flow')->initflow($num, $mid, false);
  368. if($flow->isreadqx(1))return true;
  369. }
  370. }
  371. if($table=='im_mess'){
  372. $ors = m($table)->getone($mid);
  373. if($ors){
  374. $receuid = $ors['receuid'];
  375. if(contain(','.$receuid.',',','.$uid.','))return true;
  376. }
  377. }
  378. if($table=='word'){
  379. $ors = m('word')->getone("`fileid`='".$rs['id']."'");
  380. if($ors){
  381. $cid = $ors['cid'];
  382. $flow = m('flow')->initflow('worc', $cid, false);
  383. if($flow->isreadqx(1))return true;
  384. $flow = m('flow')->initflow('word', $ors['id'], false);
  385. if($flow->isreadqx(1))return true;
  386. }
  387. }
  388. return false;
  389. }
  390. }
粤ICP备19079148号