fileModel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. $thumbplat = arrvalue($rs, 'thumbplat');
  107. if(!isempt($thumbplat)){
  108. $thumbpath = str_replace('{FILEURL}', getconfig('rockfile_url'), $thumbplat);
  109. }
  110. return $this->rock->gethttppath($thumbpath);
  111. }
  112. //$lx=2详情,$lx=3是flow.php getdatalog下读取的
  113. public function getfilestr($rs, $lx=0)
  114. {
  115. $fstr= '';
  116. if(!$rs)return $fstr;
  117. $str = $this->rock->jm->strrocktoken(array('a'=>'down','id'=>$rs['id']));
  118. $url = ''.URL.'index.php?rocktoken='.$str.'';
  119. $str = 'href="'.$url.'"';
  120. $ext = $rs['fileext'];
  121. $id = $rs['id'];
  122. $isimg= $this->isimg($ext);
  123. $strd= $str;
  124. if($lx==1)$str='href="javascript:;" onclick="return js.downshow('.$rs['id'].')"';
  125. if($lx>=2){
  126. $paths = $rs['filepath'];
  127. if(arrvalue($rs,'filepathout'))$paths = $rs['filepathout'];
  128. if(!$isimg)$paths='';
  129. $str='href="javascript:;" onclick="return c.downshow('.$rs['id'].',\''.$ext.'\',\''.$paths.'\',\''.$rs['filenum'].'\')"';//详情上预览
  130. }
  131. $flx = $rs['fileext'];
  132. if(!$this->contain($this->fileall,','.$flx.','))$flx='wz';
  133. $str1 = '';
  134. $imurl = ''.URL.'web/images/fileicons/'.$flx.'.gif';
  135. $thumbpath = $this->getthumbpath($rs);
  136. if($isimg && !isempt($thumbpath))$imurl = $thumbpath;
  137. $isdel = file_exists($rs['filepath']);
  138. if(substr($rs['filepath'],0,4)=='http')$isdel=true;
  139. if(!isempt($rs['filenum']))$isdel=true;
  140. if(arrvalue($rs,'filepathout'))$isdel=true;
  141. $fstr .='<img src="'.$imurl.'" align="absmiddle" height=20 width=20>';
  142. if($isdel){
  143. $fstr .=' '.$rs['filename'].'';
  144. }else{
  145. $fstr .=' <s>'.$rs['filename'].'</s>';
  146. }
  147. $fstr .=' <span style="color:#aaaaaa;font-size:12px">('.$rs['filesizecn'].')</span>';
  148. $filenum = arrvalue($rs,'filenum');
  149. //if(!isempt($filenum)){
  150. $strd = 'href="javascript:;" onclick="js.fileopt('.$id.', 1)"';//下载的链接
  151. //}
  152. if($lx>=2){
  153. if($isdel){
  154. $fstr .= ' <a temp="clo" '.$strd.' class="blue">下载</a>';
  155. if($isimg || $this->isoffice($ext) || $this->isyulan($ext))
  156. $fstr .= '&nbsp; <a temp="clo" '.$str.' class="blue">预览</a>';
  157. if($this->isbianju($ext) && $lx==3)$fstr .='`'.$rs['id'].'`'; //用于编辑
  158. }else{
  159. $fstr .= ' <span style="color:#aaaaaa;font-size:12px">已删除</span>';
  160. }
  161. }
  162. return $fstr;
  163. }
  164. public function getfiles($mtype, $mid)
  165. {
  166. $rows = $this->getall("`mtype`='$mtype' and `mid`='$mid' order by `id`");
  167. foreach($rows as $k=>$rs){
  168. $rows[$k]['status'] = 4;
  169. }
  170. return $rows;
  171. }
  172. public function getfilepath($mtype, $mid)
  173. {
  174. $rows = $this->getfiles($mtype, $mid);
  175. $str = '';
  176. $nas = '';
  177. $st1 = '';
  178. foreach($rows as $k=>$rs){
  179. $path = $rs['filepath'];
  180. $outu = arrvalue($rs, 'filepathout');
  181. if(isempt($outu)){
  182. if(!isempt($path) && (file_exists($path) || substr($path,0,4)=='http') ){
  183. $str .= ','.$path.'';
  184. $nas .= ','.$rs['filename'].'';
  185. }
  186. }else{
  187. if($st1!='')$st1.='<br>';
  188. $st1.=''.$rs['filename'].'('.$rs['filesizecn'].')&nbsp;<a target="_blank" href="'.$outu.'">下载</a>';
  189. }
  190. }
  191. if($str!=''){
  192. $str = substr($str, 1);
  193. $nas = substr($nas, 1);
  194. }
  195. return array($str, $nas, $st1);
  196. }
  197. public function copyfile($mtype, $mid)
  198. {
  199. $rows = $this->getall("`mtype`='$mtype' and `mid`='$mid' order by `id`");
  200. $arr = array();
  201. foreach($rows as $k=>$rs){
  202. $inuar = $rs;
  203. if(isempt($rs['filepath']) || (substr($rs['filepath'],0,4)!='http' && !arrvalue($rs,'filepathout') && !file_exists($rs['filepath'])))continue;
  204. unset($inuar['id']);
  205. $oid = $rs['id'];
  206. $inuar['adddt'] = $this->rock->now;
  207. $inuar['optid'] = $this->adminid;
  208. $inuar['optname'] = $this->adminname;
  209. $inuar['downci'] = '0';
  210. $inuar['mtype'] = '';
  211. $inuar['mid'] = '0';
  212. $inuar['oid'] = $oid;
  213. $ids = (int)$this->getmou('id','oid='.$oid.' and `mid`=0');
  214. if($ids==0){
  215. $this->insert($inuar);
  216. $inuar['id'] = $this->db->insert_id();
  217. }else{
  218. $inuar['id'] = $ids;
  219. }
  220. $inuar['status'] = 4;
  221. $arr[] = $inuar;
  222. }
  223. return $arr;
  224. }
  225. public function delfiles($mtype, $mid)
  226. {
  227. $where = "`mtype`='$mtype' and `mid`='$mid'";
  228. $this->delfile('', $where);
  229. }
  230. public function delfile($sid='', $where='')
  231. {
  232. if($sid!='')$where = "`id` in ($sid)";
  233. if($where=='')return;
  234. $rows = $this->getall($where);
  235. foreach($rows as $k=>$rs){
  236. $path = $rs['filepath'];
  237. if(!$this->isempt($path) && substr($path,0,4)!='http' && file_exists($path))unlink($path);
  238. $path = $rs['thumbpath'];
  239. if(!$this->isempt($path) && substr($path,0,4)!='http' && file_exists($path))unlink($path);
  240. $path = $rs['pdfpath'];
  241. if(!$this->isempt($path) && substr($path,0,4)!='http' && file_exists($path))unlink($path);
  242. if(!isempt($rs['filenum']))c('rockqueue')->push('flow,uptodelete', array('filenum'=>$rs['filenum']));//发送同步删除
  243. m('files')->delete('`fileid`='.$rs['id'].'');
  244. }
  245. $this->delete($where);
  246. }
  247. public function fileheader($filename,$ext='xls', $size=0)
  248. {
  249. $mime = $this->getmime($ext);
  250. $filename = $this->iconvutf8(str_replace(' ','',$filename));
  251. header('Content-type:'.$mime.'');
  252. header('Accept-Ranges: bytes');
  253. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  254. header('Pragma: no-cache');
  255. if($size>0)header('Content-Length:'.$size.'');
  256. header('Expires: 0');
  257. header('Content-disposition:attachment;filename='.$filename.'');
  258. header('Content-Transfer-Encoding: binary');
  259. }
  260. //渣渣IE才需要转化,真是醉了
  261. public function iconvutf8($text) {
  262. if(contain($this->rock->web,'IE')){
  263. return iconv('utf-8','gb2312', $text);
  264. }else{
  265. return $text;
  266. }
  267. }
  268. public function show($id,$qx=false)
  269. {
  270. if($id==0)exit('Sorry!');
  271. $rs = $this->getone($id);
  272. if(!$rs)exit('504 Not find files');
  273. if(!$qx && !$this->isdownfile($rs))exit('404 No permission download');
  274. $this->update("`downci`=`downci`+1", $id);
  275. $this->addlogs($id, 1);
  276. $filepath = $rs['filepath'];
  277. $filename = $rs['filename'];
  278. $filesize = $rs['filesize'];
  279. $fileext = $rs['fileext'];
  280. $filepathout= $rs['filepathout'];
  281. if($this->rock->contain($filepath,'http')){
  282. header('location:'.$filepath.'');
  283. }else{
  284. //2018-07-18只能下载upload/images下的文件
  285. $ielx = substr($filepath,0,strlen(UPDIR));
  286. $ielx1 = substr($filepath,0,6);
  287. if($ielx!=UPDIR && $ielx1!='upload' && $ielx1!='images')exit('无效操作1');
  288. if(!file_exists($filepath)){
  289. if(!isempt($filepathout))header('location:'.$filepathout.'');
  290. exit('404 Not find files');
  291. }
  292. if(!contain($filename,'.'.$fileext.''))$filename .= '.'.$fileext.'';
  293. $filesize = filesize($filepath);
  294. $this->fileheader($filename, $fileext, $filesize);
  295. if(substr($filepath,-4)=='temp'){
  296. $content = file_get_contents($filepath);
  297. echo base64_decode($content);
  298. }else{
  299. if($this->rock->iswebbro(0) && $this->rock->iswebbro(5)){
  300. header('location:'.$filepath.'');
  301. return;
  302. }
  303. if($this->rock->get('openfrom')=='iosapp'){
  304. header('location:'.$filepath.'');
  305. return;
  306. }
  307. if($filesize > 10*1024*1024 && 1==1 && $fileext!='pdf'){
  308. header('location:'.$filepath.'');
  309. }else{
  310. ob_clean();flush();
  311. //$fileo = fopen($filepath,'rb');
  312. //while(!feof($fileo))echo fread($fileo, 1024*20);
  313. //fclose($fileo);
  314. readfile($filepath);
  315. }
  316. }
  317. }
  318. }
  319. //这个是下载temp文件的
  320. public function download($id)
  321. {
  322. if($id==0)exit('Sorry!');
  323. $rs = $this->getone($id);
  324. if(!$rs)exit('504 Not find files');
  325. if(!$this->isdownfile($rs))exit('No permission download');
  326. $filepath = $rs['filepath'];
  327. $ielx = substr($filepath,0,strlen(UPDIR));
  328. $ielx1 = substr($filepath,0,6);
  329. if($ielx!=UPDIR && $ielx1!='upload' && $ielx1!='images')exit('无效操作2');
  330. if(!file_exists($filepath))exit('404 Not find files');
  331. $this->update("`downci`=`downci`+1", $id);
  332. $this->addlogs($id, 1);
  333. $filename = $rs['filename'];
  334. $filesize = $rs['filesize'];
  335. if(substr($filepath,-4)=='temp'){
  336. Header("Content-type: application/octet-stream");
  337. header('Accept-Ranges: bytes');
  338. Header("Accept-Length: ".$filesize);
  339. Header("Content-Length: ".$filesize);
  340. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  341. header('Pragma: no-cache');
  342. header('Expires: 0');
  343. $content = file_get_contents($filepath);
  344. echo base64_decode($content);
  345. }else{
  346. }
  347. }
  348. //判断是否有下载文件的权限
  349. private function isdownfile($rs)
  350. {
  351. //return true; //如果不想权限判断就去掉注释直接返回true
  352. $uid = $this->adminid;
  353. if(arrvalue($rs,'optid')==$uid)return true;
  354. $table = arrvalue($rs,'mtype');
  355. $mid = (int)arrvalue($rs,'mid','0');
  356. if(!isempt($table) && $mid>0){
  357. $to = m('reads')->rows("`table`='$table' and `mid`='$mid' and `optid`='$uid'");
  358. if($to>0)return true;
  359. }
  360. $mknum = arrvalue($rs,'mknum');
  361. if(!isempt($mknum)){
  362. $mknuma = explode('|', $mknum);
  363. $num = $mknuma[0];
  364. $mid = (int)arrvalue($mknuma, 1, $mid);
  365. if($mid>0){
  366. $flow = m('flow')->initflow($num, $mid, false);
  367. if($flow->isreadqx(1))return true;
  368. }
  369. }
  370. if($table=='im_mess'){
  371. $ors = m($table)->getone($mid);
  372. if($ors){
  373. $receuid = $ors['receuid'];
  374. if(contain(','.$receuid.',',','.$uid.','))return true;
  375. }
  376. }
  377. if($table=='word'){
  378. $ors = m('word')->getone("`fileid`='".$rs['id']."'");
  379. if($ors){
  380. $cid = $ors['cid'];
  381. $flow = m('flow')->initflow('worc', $cid, false);
  382. if($flow->isreadqx(1))return true;
  383. $flow = m('flow')->initflow('word', $ors['id'], false);
  384. if($flow->isreadqx(1))return true;
  385. }
  386. }
  387. return false;
  388. }
  389. }
粤ICP备19079148号