uploadAction.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <?php
  2. class uploadClassAction extends apiAction
  3. {
  4. /**
  5. * 上传文件
  6. */
  7. public function upfileAction()
  8. {
  9. if(!$_FILES)exit('sorry!');
  10. $upimg = c('upfile');
  11. $maxsize= (int)$this->get('maxsize', $upimg->getmaxzhao());//上传最大M
  12. $uptypes= '*';
  13. $updir = $this->get('updir');
  14. if(isempt($updir)){
  15. $updir=date('Y-m');
  16. }else{
  17. $updir=str_replace(array(' ','.'),'', trim($updir));
  18. $updir=str_replace('{month}',date('Y-m'), $updir);
  19. $updir=str_replace('{Year}',date('Y'), $updir);
  20. $updir=str_replace(array('{','}'),'', $updir);
  21. $updir=str_replace(',','|', $updir);
  22. $bobg = preg_replace("/[a-zA-Z0-9_]/",'', $updir);
  23. $bobg = str_replace(array('-','|'),'', $bobg);
  24. if($bobg)exit('stop:'.$bobg.'');
  25. }
  26. $upimg->initupfile($uptypes, ''.UPDIR.'|'.$updir.'', $maxsize);
  27. $upses = $upimg->up('file');
  28. if(!is_array($upses))exit($upses);
  29. $arr = c('down')->uploadback($upses);
  30. $arr['autoup'] = (getconfig('qcloudCos_autoup') || getconfig('alioss_autoup')) ? 1 : 0; //是否上传其他平台
  31. $this->returnjson($arr);
  32. }
  33. /**
  34. * 这个是用来在线编辑文档上传的
  35. */
  36. public function upfilevbAction()
  37. {
  38. $fileid = (int)$this->get('fileid','0');
  39. if($fileid==0)exit('fileid=0');
  40. $data = $this->getpostdata();
  41. if(isempt($data))return '没有数据';
  42. $fileext= $this->get('fileext');
  43. $uptype = '|doc|docx|xls|xlsx|ppt|pptx|';
  44. if(!contain($uptype,'|'.$fileext.'|'))$fileext='doc';
  45. $fileobj = m('file');
  46. $frs = $fileobj->getone($fileid); //记录
  47. if(!$frs)exit('文件记录不存在了');
  48. $frs['oldfilepath'] = $frs['filepath'];
  49. $filename = $frs['filename'];
  50. if(!contain($filename, '.'.$fileext.'')){
  51. $filename = str_replace(array('.doc','.xls','.ppt'), '.'.$fileext.'', $filename);
  52. }
  53. $filepath = ''.UPDIR.'/'.date('Y-m').'/'.date('d_His').''.rand(10,99).'.'.$fileext.'';
  54. $this->rock->createtxt($filepath, base64_decode($data));
  55. $filesize = filesize($filepath);
  56. $filesizecn = $this->rock->formatsize($filesize);
  57. //更新文件
  58. $fileobj->update(array(
  59. 'filename' => $filename,
  60. 'filepath' => $filepath,
  61. 'filenum' => '',
  62. 'filesize' => $filesize,
  63. 'filesizecn' => $filesizecn,
  64. 'fileext' => $fileext,
  65. 'pdfpath' => '',
  66. ),$fileid);
  67. c('cache')->del('filetopdf'.$fileid.'');
  68. //【弃用】发队列自动上传到信呼文件平台
  69. if(getconfig('autoup_toxinhudoc')){
  70. //c('rockqueue')->sendfile($fileid);
  71. }
  72. //上传到腾讯存储
  73. if(getconfig('qcloudCos_autoup')){
  74. c('rockqueue')->sendfile($fileid);
  75. }
  76. //告诉上传人说编辑了他的附件
  77. $mknums = arrvalue($frs,'mknum');
  78. if(!isempt($mknums) && $frs['mid']>0){
  79. $mid = $frs['mid'];
  80. $mknumsa = explode('|', $mknums);
  81. $modenum = $mknumsa[0];
  82. if(isset($mknumsa[1]))$mid = $mknumsa[1];
  83. $flow = m('flow')->initflow($modenum, $mid, false);
  84. $ssid = $flow->addlog(array(
  85. 'name' => '在线编辑'
  86. ));
  87. $ufrs = $frs;
  88. $ufrs['filepath'] = $frs['oldfilepath'];
  89. unset($ufrs['oldfilepath']);
  90. unset($ufrs['id']);
  91. $ufrs['mtype'] = 'flow_log';
  92. $ufrs['mid'] = $ssid;
  93. $ufrs['mknum'] = ''.$modenum.'|'.$mid.'';
  94. $ufrs['filename'] = str_replace('.'.$ufrs['fileext'].'','(备份).'.$ufrs['fileext'].'', $ufrs['filename']);
  95. $fileobj->insert($ufrs); //记录原来的文件
  96. //不是我创建就告诉创建人
  97. if($this->adminid<>$frs['optid'])
  98. $flow->push($frs['optid'],'', ''.$this->adminname.'在线编辑文件“'.$frs['filename'].'”', '文件在线编辑');
  99. $flow->floweditoffice($frs, $ufrs);
  100. }else if($this->adminid<>$frs['optid']){ //不知道关联哪个模块
  101. $flow = m('flow')->initflow('word');
  102. $flow->push($frs['optid'],'文档', ''.$this->adminname.'在线编辑文件“'.$frs['filename'].'”', '文件在线编辑',0, array(
  103. 'wxurl' => ''
  104. ));
  105. }
  106. $frs['filesize'] = $filesize;
  107. $fkey = $this->createtempurl($frs);
  108. return 'ok,'.$fkey.'';
  109. }
  110. /**
  111. * 上传时初始化看是不是存在文件
  112. */
  113. public function initfileAction()
  114. {
  115. $filesize = c('check')->onlynumber($this->post('filesize'));
  116. $fileext = $this->post('fileext');
  117. $filename = $this->getvals('filename');
  118. $where = "`fileext`='$fileext' and `filesize`='$filesize'";
  119. if(!isempt($filename))$where.=" and `filename`='$filename'";
  120. $frs = m('file')->getone($where,'*','`id` desc');
  121. $bo = false;
  122. if($frs){
  123. $filepath = $frs['filepath'];
  124. if(!isempt($filepath) && file_exists($filepath))$bo=true;
  125. }
  126. if($bo){
  127. $this->showreturn(json_encode($frs));
  128. }else{
  129. $this->showreturn('','not found', 201);
  130. }
  131. }
  132. public function upfileappAction()
  133. {
  134. if(!$_FILES)$this->showreturn('', '禁止访问', 201);
  135. $upimg = c('upfile');
  136. $maxsize= (int)$this->get('maxsize', $upimg->getmaxzhao());//上传最大M
  137. $uptypes= '*';
  138. $upimg->initupfile($uptypes, ''.UPDIR.'|reimchat|'.date('Y-m').'', $maxsize);
  139. $upses = $upimg->up('file');
  140. if(!is_array($upses))$this->showreturn('', $upses, 202);
  141. $arr = c('down')->uploadback($upses);
  142. $this->showreturn($arr);
  143. }
  144. public function upcontAction()
  145. {
  146. $cont = $this->post('content');
  147. if(isempt($cont))exit('sorry not cont');
  148. $cont = str_replace(' ','', $cont);
  149. $cont = $this->rock->jm->base64decode($cont);
  150. $arr = c('down')->createimage($cont,'png','截图');
  151. $this->returnjson($arr);
  152. }
  153. public function getfileAjax()
  154. {
  155. $cont = '';
  156. $path = ''.UPDIR.'/uptxt'.$this->adminid.'.txt';
  157. if(file_exists($path)){
  158. @$cont = file_get_contents($path);
  159. }
  160. $data = array();
  161. if($cont!=''){
  162. $arr = json_decode($cont, true);
  163. $msg = $arr['msg'];
  164. $data = $arr['data'];
  165. @unlink($path);
  166. }else{
  167. $msg = 'sorry,not infor!';
  168. }
  169. $this->showreturn($data, $msg);
  170. }
  171. public function getfileAction()
  172. {
  173. $fileid = (int)$this->post('fileid',0);
  174. $rs = m('file')->getone($fileid);
  175. $this->showreturn($rs);
  176. }
  177. public function downAction()
  178. {
  179. $id = (int)$this->jm->gettoken('id');
  180. m('file')->show($id);
  181. }
  182. //记录预览记录
  183. public function logsAction()
  184. {
  185. $fileid = (int)$this->post('fileid',0);
  186. $type = (int)$this->post('type',0);
  187. m('file')->addlogs($fileid, $type);
  188. }
  189. /**
  190. * 发送编辑权限
  191. */
  192. public function rockofficeeditAction()
  193. {
  194. $fileid = (int)$this->get('id');
  195. $lx = (int)$this->get('lx');
  196. $frs = m('file')->getone($fileid);
  197. if(!$frs)return returnerror('文件不存在了');
  198. $filepath = $frs['filepath'];
  199. $filepathout = $frs['filepathout'];
  200. if(substr($filepath,0,4)!='http' && !file_exists($filepath)){
  201. if(isempt($filepathout)){
  202. return returnerror('文件不存在了1');
  203. }else{
  204. $filepath = $filepathout;
  205. }
  206. }
  207. $uptype = '|doc|docx|xls|xlsx|ppt|pptx|';
  208. if(!contain($uptype,'|'.$frs['fileext'].'|'))return returnerror('不是文档类型无法在线编辑');
  209. $filename = $frs['filename'];
  210. $utes = 'edit';
  211. if($lx==1){
  212. $filename = '(只读)'.$filename.'';
  213. $utes = 'yulan';
  214. }
  215. $arr = array();
  216. $arr[0] = URL;
  217. $arr[1] = $filename;
  218. $arr[2] = $this->createtempurl($frs);
  219. $arr[3] = $this->rock->gethttppath($filepath); //下载地址
  220. $arr[4] = $fileid;
  221. $arr[5] = $this->adminid;
  222. $arr[6] = $this->token;
  223. $arr[7] = $utes;
  224. $arr[8] = $frs['fileext'];
  225. $str = '';
  226. foreach($arr as $s1)$str.=','.$s1.'';
  227. return returnsuccess(substr($str,1));
  228. }
  229. /**
  230. * 获取预览和下载地址
  231. */
  232. public function fileinfoAction()
  233. {
  234. $fileid = (int)$this->get('id');
  235. $type = (int)$this->get('type'); //0预览,1下载,2编辑
  236. $ismobile = (int)$this->get('ismobile'); //是否手机端
  237. return $this->fileinfoShow($fileid, $type, $ismobile);
  238. }
  239. private $frs,$loadyuan;
  240. public function fileinfoShow($fileid, $type, $ismobile)
  241. {
  242. $fobj = m('file');
  243. $frs = $fobj->getone($fileid);
  244. $this->frs = $frs;
  245. if(!$frs)return returnerror('文件不存在了');
  246. $filenum= $frs['filenum'];
  247. $fileext = $frs['fileext'];
  248. $filename = $frs['filename'];
  249. $filepath = $frs['filepath'];
  250. $filepathout = arrvalue($frs, 'filepathout');
  251. $data = array();
  252. $loadyuan = false;
  253. $data['url'] = '';
  254. $data['fileext']= $fileext;
  255. //预览
  256. if($type==0){
  257. if(!$fobj->isview($fileext))
  258. return returnerror('此'.$fileext.'类型文件不支持在线预览');
  259. }
  260. //从文件上传中心最新
  261. if(!isempt($filenum)){
  262. $dbs = m('admin');
  263. $barr = c('rockfile')->getdata('upload','fileinfo', array(
  264. 'fnum' => $filenum,
  265. 'lx' => $type,
  266. 'sysuid' => $this->adminid,
  267. 'ismobile' => $ismobile,
  268. 'sysname' => $this->rock->jm->base64encode($this->adminname),
  269. 'sysface' => $this->rock->jm->base64encode($dbs->getface($dbs->getmou('face',$this->adminid))),
  270. ));
  271. if(!$barr['success']){
  272. return $barr;
  273. }else{
  274. $loadyuan = true;
  275. $da = $barr['data'];
  276. $data['url'] = $da['url'];
  277. if($da['isimg'] && $type==0)$data['url'] = $da['imgurl'];
  278. }
  279. }
  280. $this->loadyuan = $loadyuan;
  281. //存自己服务器的
  282. if(!$loadyuan){
  283. if(substr($filepath,0,4)!='http' && isempt($filepathout) && !file_exists($filepath))return returnerror('文件不存在了1');
  284. if(c('upfile')->isimg($fileext)){
  285. $data['url'] = m('admin')->getface($filepath);
  286. if(!isempt($filepathout))$data['url'] = $filepathout;
  287. }
  288. //下载
  289. if($type==1){
  290. $url = 'api.php?m=upload&id='.$fileid.'&a=down';
  291. if($this->rock->web=='wxbro')$url.= '&adminid='.$this->adminid.'&token='.$this->admintoken.'';
  292. $data['url'] = $url;
  293. }
  294. //编辑
  295. if($type==2){
  296. if(getconfig('officebj')=='1'){
  297. $data['fileext']='rockedit';
  298. $data['url'] = 'index.php?m=public&a=fileedit&id='.$fileid.'';
  299. }else{
  300. if($ismobile==1)return returnerror('移动端不支持在线编辑');
  301. $data['fileext']='rockoffice';
  302. $data['url'] = $this->rock->gethttppath($filepath);
  303. }
  304. }
  305. }
  306. $data['filename'] = $filename;
  307. $url = arrvalue($data, 'url');
  308. if($url==''){
  309. $url = 'index.php?m=public&a=fileviewer&id='.$fileid.'';
  310. }
  311. //用本地插件编辑和预览
  312. if($data['fileext']=='rockoffice'){
  313. $utes = 'edit';
  314. if($type==0){
  315. $filename = '(只读)'.$filename.'';
  316. $utes = 'yulan';
  317. }
  318. $arr = array();
  319. $arr[0] = URL;
  320. $arr[1] = $filename;
  321. $arr[2] = $this->createtempurl($frs);
  322. $arr[3] = $data['url']; //下载地址
  323. $arr[4] = $fileid;
  324. $arr[5] = $this->adminid;
  325. $arr[6] = $this->token;
  326. $arr[7] = $utes;
  327. $arr[8] = $fileext;
  328. $str = '';
  329. foreach($arr as $s1)$str.=','.$s1.'';
  330. $url = substr($str, 1);
  331. }
  332. $data['url'] = $url;
  333. $data['type'] = $type;
  334. $data['id'] = $fileid;
  335. $data['isview'] = $fobj->isview($fileext); //是否可直接预览
  336. return returnsuccess($data);
  337. }
  338. //生成唯一文件名键值
  339. private function createtempurl($frs)
  340. {
  341. $str = ''.md5(URL).'_'.$frs['filesize'].'_'.$frs['id'].'.'.$frs['fileext'].'';
  342. return $str;
  343. }
  344. /**
  345. * app上获取下载地址
  346. */
  347. public function appgetfileAction()
  348. {
  349. $id = (int)$this->post('id',0);
  350. $barr = $this->fileinfoShow($id, 1, 1);
  351. if(!$barr['success'])return $barr;
  352. $frs = $this->frs;
  353. $frs['filetype'] = m('file')->getmime($frs['fileext']);
  354. $frs['downurl'] = $barr['data']['url'].'&cfrom=app';
  355. if(!$this->loadyuan){
  356. $frs['downurl']= '';
  357. if(substr($frs['filepath'],0,4)=='http'){
  358. $frs['downurl'] = $frs['filepath'];
  359. }else{
  360. if(!file_exists($frs['filepath']) && arrvalue($frs,'filepathout'))$frs['downurl'] = $frs['filepathout'];
  361. }
  362. }
  363. return returnsuccess($frs);
  364. }
  365. /**
  366. * 编辑时验证
  367. */
  368. public function sendeditAction()
  369. {
  370. $id = (int)$this->get('id',0);
  371. $otype = (int)$this->get('otype',0);
  372. return c('rockedit')->sendedit($id, $this->admintoken, $otype);
  373. }
  374. /**
  375. * 获取文件信息
  376. */
  377. public function afileinfoAction()
  378. {
  379. $allfid = c('check')->onlynumber($this->get('allfid'));
  380. $filearr= array();
  381. if($allfid){
  382. $fobj = m('file');
  383. $frows = $fobj->getall('`id` in('.$allfid.')','filename,id,filesizecn,fileext,optname,thumbpath,thumbplat');
  384. foreach($frows as $k1=>$rs1){
  385. $rs1['thumbpath'] = $fobj->getthumbpath($rs1);
  386. $filearr['f'.$rs1['id'].''] = $rs1;
  387. }
  388. }
  389. return $filearr;
  390. }
  391. /**
  392. * 获取文件(写入到内容里)
  393. */
  394. public function filedaoAction()
  395. {
  396. $allfid = c('check')->onlynumber($this->get('fileid'));
  397. $filearr= array();
  398. $str = '';
  399. if($allfid){
  400. $fobj = m('file');
  401. $frows = $fobj->getall('`id` in('.$allfid.')');
  402. $urla = getconfig('outurl', URL);
  403. foreach($frows as $k1=>$rs1){
  404. $str.='<br>';
  405. $url = ''.$urla.''.$rs1['filepath'].'';
  406. if($sst = arrvalue($rs1,'filepathout'))$url = $sst;
  407. $flx = $rs1['fileext'];
  408. if(!contain($fobj->fileall,','.$flx.','))$flx='wz';
  409. $str1 = '';
  410. $imurl = ''.URL.'web/images/fileicons/'.$flx.'.gif';
  411. $thumbpath = $fobj->getthumbpath($rs1);
  412. if($fobj->isimg($flx) && !isempt($thumbpath))$imurl = $thumbpath;
  413. $str.='<img src="'.$imurl.'" align="absmiddle" height=20 width=20> <a target="_blank" href="'.$url.'">'.$rs1['filename'].'</a>('.$rs1['filesizecn'].')';
  414. }
  415. }
  416. return $str;
  417. }
  418. /**
  419. * 获取模版文件
  420. */
  421. public function getmfileAction()
  422. {
  423. $data = array();
  424. $fenlei = $this->jm->base64decode($this->get('fenlei'));
  425. $fenlei = $this->rock->xssrepstr($this->rock->iconvsql($fenlei));
  426. $where = m('admin')->getjoinstr('a.`receid`', $this->adminid, 1);
  427. $sql = 'select a.`name`,a.`wtype`,b.`filepath`,b.`id` from `[Q]wordxie` a left join `[Q]file` b on a.`fileid`=b.`id` where a.`fenlei`=\''.$fenlei.'\' and a.`isgk`=1 and ('.$where.')';
  428. $rows = $this->db->getall($sql);
  429. foreach($rows as $k=>$rs){
  430. $data[] = array(
  431. 'value' => $rs['id'],
  432. 'name' => $rs['name'],
  433. 'subname' => $rs['wtype'],
  434. );
  435. }
  436. return $data;
  437. }
  438. public function getmfilvAction()
  439. {
  440. $fileid = (int)$this->get('fileid','0');
  441. $frs = m('file')->getone($fileid);
  442. if(!$frs)return returnerror('不存在');
  443. $lujing = $frs['filepathout'];
  444. if(isempt($lujing)){
  445. $lujing = $frs['filepath'];
  446. if(substr($lujing,0,4)!='http' && !file_exists($lujing))return returnerror('文件不存在了');
  447. }
  448. $fileext = $frs['fileext'];
  449. $fname = $this->jm->base64decode($this->get('fname'));
  450. $fname = (isempt($fname)) ? $frs['filename'] : ''.$fname.'.'.$fileext.'';
  451. $filepath = ''.UPDIR.'/'.date('Y-m').'/'.date('d').'_rocktpl'.rand(1000,9999).'_'.$fileid.'.'.$fileext.'';
  452. $this->rock->createtxt($filepath, file_get_contents($lujing));
  453. $uarr = array(
  454. 'filename' => $fname,
  455. 'fileext' => $fileext,
  456. 'filepath' => $filepath,
  457. 'filesize' => filesize($filepath),
  458. 'filesizecn' => $this->rock->formatsize(filesize($filepath)),
  459. 'optid' => $this->adminid,
  460. 'optname' => $this->adminname,
  461. 'adddt' => $this->rock->now,
  462. 'ip' => $this->rock->ip,
  463. 'web' => $this->rock->web,
  464. );
  465. $uarr['id'] = m('file')->insert($uarr);
  466. return returnsuccess($uarr);
  467. }
  468. /**
  469. * 文库选择文件
  470. */
  471. public function changedataAction()
  472. {
  473. $rows = array();
  474. $uptp = $this->get('uptp');
  475. $tsye = $this->get('tsye');
  476. $key = $this->get('key');
  477. $selv = (int)$this->get('selvalue','0');
  478. $where = '`optid`='.$this->adminid.'';
  479. if($selv==1){
  480. $str = m('admin')->getjoinstrs('`shateid`', $this->adminid, 1);
  481. $where = '`id` in(select `fileid` from `[Q]word` where `type`=0 '.$str.' )';
  482. }
  483. if($key){
  484. $key = $this->rock->xssrepstr($this->jm->base64decode($key));
  485. $where.=" and `filename` like '%".$key."%'";
  486. }
  487. if($uptp && $uptp!='*'){
  488. if($uptp=='image')$uptp='jpg,png,gif,jpeg';
  489. $uptp = str_replace(',', "','", $uptp);
  490. $where.=" and `fileext` in('".$uptp."')";
  491. }
  492. $db = m('file');
  493. $rows = $db->getall($where,'id,filename,filesizecn,fileext,thumbpath,filepath,filepathout','`id` desc limit 10');
  494. foreach($rows as $k=>$rs){
  495. $rows[$k]['value'] = $rs['id'];
  496. $rows[$k]['name'] = $rs['filename'];
  497. $rows[$k]['subname']= $rs['filesizecn'];
  498. $rows[$k]['xuanbool']= true;
  499. if($tsye=='img'){
  500. if(!isempt($rs['filepathout']))$rows[$k]['filepath'] = $rs['filepathout'];
  501. }else{
  502. unset($rows[$k]['filepath']);
  503. }
  504. unset($rows[$k]['filepathout']);
  505. if(!isempt($rs['thumbpath'])){
  506. $rows[$k]['iconsimg'] = $rs['thumbpath'];
  507. }else{
  508. $flx = $rs['fileext'];
  509. if(!contain($db->fileall,','.$flx.','))$flx='wz';
  510. $rows[$k]['iconsimg'] = 'web/images/fileicons/'.$flx.'.gif';
  511. }
  512. }
  513. $count = $db->rows($where);
  514. $selectdata[] = array('value'=>'','name'=>'选自己上传');
  515. $selectdata[] = array('value'=>'1','name'=>'共享给我的');
  516. return array(
  517. 'rows' => $rows,
  518. 'totalCount' => $count,
  519. 'selectdata' => $selectdata
  520. );
  521. }
  522. }
粤ICP备19079148号