control.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. /**
  3. * The control file of file module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package file
  9. * @version $Id: control.php 4129 2013-01-18 01:58:14Z wwccss $
  10. * @link https://www.zentao.net
  11. */
  12. class file extends control
  13. {
  14. /**
  15. * Build the upload form.
  16. *
  17. * @param int $fileCount
  18. * @param float $percent
  19. * @param string $filesName
  20. * @param string $labelsName
  21. * @access public
  22. * @return void
  23. */
  24. public function buildForm($fileCount = 1, $percent = 0.9, $filesName = "files", $labelsName = "labels")
  25. {
  26. if(!file_exists($this->file->savePath)) return printf($this->lang->file->errorNotExists, $this->file->savePath);
  27. if(!is_writable($this->file->savePath)) return printf($this->lang->file->errorCanNotWrite, $this->file->savePath, $this->file->savePath);
  28. $this->view->filesName = $filesName;
  29. $this->view->labelsName = $labelsName;
  30. $this->display();
  31. }
  32. /**
  33. * Build the old upload form.
  34. *
  35. * @param int $fileCount
  36. * @param float $percent
  37. * @param string $filesName
  38. * @param string $labelsName
  39. * @access public
  40. * @return void
  41. */
  42. public function buildOldForm($fileCount = 1, $percent = 0.9, $filesName = "files", $labelsName = "labels")
  43. {
  44. if(!file_exists($this->file->savePath)) return printf($this->lang->file->errorNotExists, $this->file->savePath);
  45. if(!is_writable($this->file->savePath)) return printf($this->lang->file->errorCanNotWrite, $this->file->savePath, $this->file->savePath);
  46. $this->view->filesName = $filesName;
  47. $this->view->labelsName = $labelsName;
  48. $this->display();
  49. }
  50. /**
  51. * AJAX: get upload request from the web editor.
  52. *
  53. * @param string $uid
  54. * @param string $objectType
  55. * @param int $objectID
  56. * @param string $extra
  57. * @param string $field
  58. * @param bool $api
  59. * @access public
  60. * @return void
  61. * @param string $gid
  62. */
  63. public function ajaxUpload($uid = '', $objectType = '', $objectID = 0, $extra = '', $field = 'imgFile', $api = false, $gid = '')
  64. {
  65. $file = $this->file->getUpload($field);
  66. if(!isset($file[0]) or strpos(",{$this->config->file->allowed},", ",{$file[0]['extension']},") === false) return $this->send(array('result' => 'fail', 'message' => $this->lang->file->errorFileFormat));
  67. $file = $file[0];
  68. if($file)
  69. {
  70. if($file['size'] == 0)
  71. {
  72. if(defined('RUN_MODE') && RUN_MODE == 'api') return print(json_encode(array('status' => 'error', 'message' => $this->lang->file->errorFileUpload)));
  73. return $this->send(array('error' => 1, 'message' => $this->lang->file->errorFileUpload));
  74. }
  75. if(@move_uploaded_file($file['tmpname'], $this->file->savePath . $this->file->getSaveName($file['pathname'])))
  76. {
  77. /* Compress image for jpg and bmp. */
  78. $file = $this->file->compressImage($file);
  79. $file['addedBy'] = $this->app->user->account;
  80. $file['addedDate'] = helper::today();
  81. $file['objectType'] = $objectType;
  82. $file['objectID'] = $objectID;
  83. $file['extra'] = $extra;
  84. $file['gid'] = empty($gid) ? '' : base64_decode($gid);
  85. unset($file['tmpname']);
  86. $this->dao->insert(TABLE_FILE)->data($file)->exec();
  87. $fileID = $this->dao->lastInsertID();
  88. /* Show story file from zt_storyspec, not zt_file. */
  89. if($objectType == 'story') $this->file->updateStoryFiles($objectID);
  90. $url = $this->createLink('file', 'read', "fileID=$fileID", $file['extension']);
  91. if($uid) $_SESSION['album'][$uid][] = $fileID;
  92. if($api || (defined('RUN_MODE') && RUN_MODE == 'api'))
  93. {
  94. if($uid) $_SESSION['album']['used'][$uid][$fileID] = $fileID;
  95. $_SERVER['SCRIPT_NAME'] = 'index.php';
  96. return $this->send(array('result' => 'success', 'status' => 'success', 'id' => $fileID, 'url' => $url, 'data' => array('id' => $fileID, 'url' => $url))); // 兼容老的 API 形式。 Compatible with the old API.
  97. }
  98. return print(json_encode(array('error' => 0, 'url' => $url)));
  99. }
  100. else
  101. {
  102. $error = strip_tags(sprintf($this->lang->file->errorCanNotWrite, $this->file->savePath, $this->file->savePath));
  103. if(defined('RUN_MODE') && RUN_MODE == 'api') return $this->send(array('status' => 'error', 'message' => $error));
  104. return print(json_encode(array('error' => 1, 'message' => $error)));
  105. }
  106. }
  107. return $this->send(array('status' => 'error', 'message' => $this->lang->file->uploadImagesExplain));
  108. }
  109. /**
  110. * Preview a file.
  111. *
  112. * @param int $fileID
  113. * @param string $mouse
  114. * @access public
  115. * @return void
  116. */
  117. public function preview($fileID, $mouse = '')
  118. {
  119. return print($this->fetch('file', 'download', "fileID=$fileID&mouse=$mouse"));
  120. }
  121. /**
  122. * Down a file.
  123. *
  124. * @param int $fileID
  125. * @param string $mouse
  126. * @access public
  127. * @return void
  128. */
  129. public function download($fileID, $mouse = '')
  130. {
  131. $file = $this->file->getById($fileID);
  132. if(empty($file) || !$this->file->fileExists($file))
  133. {
  134. $this->view->error = $this->lang->file->fileNotFound;
  135. if(defined('RUN_MODE') && RUN_MODE == 'api') return $this->send(array('status' => 'fail', 'code' => 404, 'message' => $this->view->error));
  136. if(isInModal()) return $this->display();
  137. return print($this->view->error);
  138. }
  139. /* Unset the menu. */
  140. if(!empty($this->lang->{$this->app->tab}->menu)) $this->lang->{$this->app->tab}->menu = array();
  141. if(!$this->file->checkPriv($file))
  142. {
  143. if(isInModal()) return $this->send(array('result' => 'success', 'message' => $this->lang->file->accessDenied, 'closeModal' => true, 'load' => true));
  144. return $this->send(array('result' => 'success', 'message' => $this->lang->file->accessDenied, 'load' => helper::createLink('my', 'index')));
  145. }
  146. /* Judge the mode, down or open. */
  147. $mode = $this->fileZen->getDownloadMode($file, $mouse);
  148. /* Down the file. */
  149. if($mode != 'open')
  150. {
  151. $fileName = $file->title;
  152. if(!preg_match("/\.{$file->extension}$/", strtolower($fileName))) $fileName .= '.' . $file->extension;
  153. return $this->sendDownHeader($fileName, $file->extension, $file->realPath, 'file');
  154. }
  155. /* If the mode is open, locate directly. */
  156. $this->view->title = $this->lang->file->previewFile;
  157. $this->view->file = $file;
  158. $this->view->charset = $this->get->charset ? $this->get->charset : $this->config->charset;
  159. $this->view->fileType = ($file->extension == 'txt') ? 'txt' : ($file->extension == 'mp4' ? 'video' : 'image');
  160. $this->display();
  161. }
  162. /**
  163. * Export as csv format.
  164. *
  165. * @access public
  166. * @return void
  167. */
  168. public function export2CSV()
  169. {
  170. $fields = $this->post->fields;
  171. $rows = $this->post->rows;
  172. $domain = common::getSysURL();
  173. /* Build csv content. */
  174. $output = '';
  175. if($rows)
  176. {
  177. $output .= '"'. implode('","', $fields) . '"' . "\n";
  178. foreach($rows as $row)
  179. {
  180. $output .= '"';
  181. foreach($fields as $fieldName => $fieldLabel)
  182. {
  183. /* <img>标签替换成链接。 */
  184. if(in_array($fieldName, $this->config->file->img2href))
  185. {
  186. $pattern = '/<img src="\{([^"]*)\}" alt="([^"]*)"\s*\/>/';
  187. $replacement = '$1 (' . $domain . '/$2)';
  188. $row->$fieldName = preg_replace($pattern, $replacement, $row->$fieldName);
  189. }
  190. if(isset($row->$fieldName) && is_numeric($row->$fieldName)) $row->$fieldName = $row->$fieldName . "\t";
  191. if(isset($row->$fieldName)) $row->$fieldName = str_replace('&quot;', '“', $row->$fieldName);
  192. $output .= isset($row->$fieldName) ? str_replace(array('"', '&nbsp;', '&gt;'), array('“', ' ', '>'), htmlSpecialString(strip_tags((string)$row->$fieldName, '<img>'))) : '';
  193. $output .= '","';
  194. }
  195. $output .= '"' . "\n";
  196. }
  197. if($this->post->kind == 'task' && $this->config->vision != 'lite') $output .= $this->lang->file->childTaskTips;
  198. }
  199. $output = helper::replaceEmoji($output);
  200. $output = htmlspecialchars_decode(htmlspecialchars_decode($output, ENT_NOQUOTES), ENT_NOQUOTES);
  201. if(isset($_POST['encode']) && $this->post->encode != "utf-8") $output = helper::convertEncoding($output, 'utf-8', $this->post->encode . '//TRANSLIT');
  202. $this->sendDownHeader($this->post->fileName, 'csv', $output);
  203. }
  204. /**
  205. * export as xml format
  206. *
  207. * @access public
  208. * @return void
  209. */
  210. public function export2XML()
  211. {
  212. $fields = $this->post->fields;
  213. $rows = $this->post->rows;
  214. $output = "<?xml version='1.0' encoding='utf-8'?><xml>\n";
  215. $output .= "<fields>";
  216. foreach($fields as $fieldName => $fieldLabel) $output .= " <$fieldName>$fieldLabel</$fieldName>\n";
  217. $output .= "</fields>";
  218. $output .= "<rows>";
  219. foreach($rows as $row)
  220. {
  221. $output .= " <row>\n";
  222. foreach($fields as $fieldName => $fieldLabel)
  223. {
  224. $fieldValue = isset($row->$fieldName) ? htmlSpecialString(strip_tags((string)$row->$fieldName, '<img>')) : '';
  225. $output .= " <$fieldName>$fieldValue</$fieldName>\n";
  226. }
  227. $output .= " </row>\n";
  228. }
  229. $output .= "</rows></xml>";
  230. $this->sendDownHeader($this->post->fileName, 'xml', $output);
  231. }
  232. /**
  233. * export as html format
  234. *
  235. * @access public
  236. * @return void
  237. */
  238. public function export2HTML()
  239. {
  240. $fields = $this->post->fields;
  241. $rows = $this->post->rows;
  242. $rowspans = $this->post->rowspan ? $this->post->rowspan : array();
  243. $colspans = $this->post->colspan ? $this->post->colspan : array();
  244. $kind = $this->post->kind;
  245. $fileName = $this->post->fileName;
  246. $output = "<html xmlns='http://www.w3.org/1999/xhtml'>";
  247. $output .= "<head>";
  248. $output .= "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
  249. $output .= "<style>table, th, td {font-size: 12px; border: 1px solid gray; border-collapse: collapse;} table th, table td {padding: 5px;}</style>";
  250. $output .= "<title>{$fileName}</title>";
  251. $output .= "</head>";
  252. $output .= "<body>";
  253. if($kind == 'task') $output .= "<div style='color:red'>" . $this->lang->file->childTaskTips . '</div>';
  254. $output .= $this->fileZen->buildDownloadTable($fields, $rows, $kind, $rowspans, $colspans);
  255. $output .= "</body></html>";
  256. $this->sendDownHeader($this->post->fileName, 'html', $output);
  257. }
  258. /**
  259. * Send the download header to the client.
  260. *
  261. * @param string $fileName
  262. * @param string $extension
  263. * @access public
  264. * @return void
  265. * @param string $fileType
  266. * @param string $content
  267. * @param string $type
  268. */
  269. public function sendDownHeader($fileName, $fileType, $content, $type = 'content')
  270. {
  271. $this->file->sendDownHeader($fileName, $fileType, $content, $type);
  272. }
  273. /**
  274. * Delete a file.
  275. *
  276. * @param int $fileID
  277. * @param string $confirm yes|no
  278. * @access public
  279. * @return void
  280. */
  281. public function delete($fileID, $confirm = 'no')
  282. {
  283. if($confirm == 'no')
  284. {
  285. $formUrl = $this->createLink('file', 'delete', "fileID=$fileID&confirm=yes");
  286. if(!helper::isAjaxRequest()) return print(js::confirm($this->lang->file->confirmDelete, $formUrl));
  287. return $this->send(array('result' => 'fail', 'callback' => "zui.Modal.confirm('{$this->lang->file->confirmDelete}').then((res) => {if(res) $.ajaxSubmit({url: '$formUrl'});});"));
  288. }
  289. else
  290. {
  291. $file = $this->file->getById($fileID);
  292. if(empty($file) || !$this->file->fileExists($file)) return $this->send(array('result' => 'fail', 'message' => $this->lang->file->fileNotFound));
  293. $canDelete = $this->file->checkPriv($file);
  294. if($file->objectType == 'comment')
  295. {
  296. $action = $this->loadModel('action')->fetchBaseInfo($file->objectID);
  297. $canDelete = ($this->app->user->admin || empty($action) || $action->actor == $this->app->user->account);
  298. }
  299. if(!$canDelete) return $this->send(array('result' => 'fail', 'message' => $this->lang->file->accessDenied));
  300. $this->dao->delete()->from(TABLE_FILE)->where('id')->eq($fileID)->exec();
  301. $this->loadModel('action')->create($file->objectType, $file->objectID, 'deletedFile', '', $extra=$file->title);
  302. $this->fileZen->unlinkRealFile($file);
  303. /* Update test case version for test case synchronization. */
  304. if($file->objectType == 'testcase') $this->file->updateTestcaseVersion($file);
  305. if($file->objectType == 'charter') $this->loadModel('charter')->updateFileByDelete($file);
  306. if($file->objectType == 'story') $this->file->updateStoryFiles($file->objectID);
  307. if(!helper::isAjaxRequest() && $this->viewType != 'json') return print(js::reload('parent'));
  308. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true));
  309. }
  310. }
  311. /**
  312. * Print files.
  313. *
  314. * @param array $files
  315. * @param string $fieldset
  316. * @param object $object
  317. * @param string $method
  318. * @param bool $showDelete
  319. * @param bool $showEdit
  320. * @access public
  321. * @return void
  322. */
  323. public function printFiles($files, $fieldset, $object = null, $method = 'view', $showDelete = true, $showEdit = true)
  324. {
  325. $this->view->files = $files;
  326. $this->view->fieldset = $fieldset;
  327. $this->view->object = $object;
  328. $this->view->method = $method;
  329. $this->view->showDelete = $showDelete;
  330. $this->view->showEdit = $showEdit;
  331. if(empty($files)) return null;
  332. if(strpos('view,edit', $method) !== false and $this->app->clientDevice != 'mobile') return $this->display('file', 'viewfiles');
  333. $this->display();
  334. }
  335. /**
  336. * Edit file's name.
  337. *
  338. * @param int $fileID
  339. * @access public
  340. * @return void
  341. */
  342. public function edit($fileID)
  343. {
  344. if($_POST)
  345. {
  346. $result = $this->fileZen->updateFileName($fileID);
  347. if($result['result'] == 'fail') return $this->send($result);
  348. $newFile = $this->file->getByID($fileID);
  349. if($this->app->clientDevice == 'mobile') return $this->send(array('load' => true));
  350. return print(json_encode($newFile));
  351. }
  352. if($this->app->clientDevice == 'mobile')
  353. {
  354. $file = $this->file->getById($fileID);
  355. if(strrpos($file->title, '.') !== false)
  356. {
  357. /* Fix the file name exe.exe */
  358. $title = explode('.', $file->title);
  359. $extension = end($title);
  360. if($file->extension == 'txt' && $extension != $file->extension) $file->extension = $extension;
  361. array_pop($title);
  362. $file->title = implode('.', $title);
  363. }
  364. $this->view->file = $file;
  365. $this->display();
  366. }
  367. }
  368. /**
  369. * Paste image in kindeditor at firefox and chrome.
  370. *
  371. * @access public
  372. * @return void
  373. * @param string $uid
  374. */
  375. public function ajaxPasteImg($uid = '')
  376. {
  377. if($_POST) return print($this->file->pasteImage($this->post->editor, $uid, true));
  378. }
  379. /**
  380. * Upload Images.
  381. *
  382. * @param string $module
  383. * @param string $params
  384. * @access public
  385. * @return void
  386. * @param string $uid
  387. * @param string $locate
  388. */
  389. public function uploadImages($module, $params, $uid = '', $locate = '')
  390. {
  391. if($locate)
  392. {
  393. $sessionName = $uid . 'ImagesFile';
  394. $imageFiles = $this->session->$sessionName;
  395. $this->session->set($module . 'ImagesFile', $imageFiles);
  396. unset($_SESSION[$sessionName]);
  397. return $this->send(array('result' => 'success', 'load' => $this->createLink($module, 'batchCreate', helper::safe64Decode($params))));
  398. }
  399. if(strtolower($this->server->request_method) == 'post')
  400. {
  401. $file = $this->file->getChunkedFile();
  402. if(!$file) return print(json_encode(array('result' => 'fail', 'message' => $this->lang->error->noData)));
  403. if(empty($file['extension']) or !in_array($file['extension'], $this->config->file->imageExtensions)) return print(json_encode(array('result' => 'fail', 'message' => $this->lang->file->errorFileFormat)));
  404. $imageFile = $this->file->saveChunkedFile($file, $uid);
  405. if(!empty($imageFile))
  406. {
  407. $sessionName = $uid . 'ImagesFile';
  408. $imageFiles = $this->session->$sessionName;
  409. $fileName = basename($imageFile['pathname']);
  410. if(empty($imageFiles)) $imageFiles = array();
  411. $imageFiles[$fileName] = $imageFile;
  412. $this->session->set($sessionName, $imageFiles);
  413. }
  414. return print(json_encode(array('result' => 'success', 'file' => $file, 'message' => $this->lang->file->uploadSuccess)));
  415. }
  416. $this->view->uid = empty($uid) ? uniqid() : $uid;
  417. $this->view->module = $module;
  418. $this->view->params = $params;
  419. $this->display();
  420. }
  421. /**
  422. * Build export tpl.
  423. *
  424. * @param string $module
  425. * @param int $templateID
  426. * @access public
  427. * @return void
  428. */
  429. public function buildExportTPL($module, $templateID = 0)
  430. {
  431. $templates = $this->file->getExportTemplate($module);
  432. $templatePairs[] = $this->lang->file->defaultTPL;
  433. foreach($templates as $template) $templatePairs[$template->id] = ($template->public ? "[{$this->lang->public}] " : '') . $template->title;
  434. $this->view->templates = $templates;
  435. $this->view->templatePairs = $templatePairs;
  436. $this->view->templateID = $templateID;
  437. $this->display();
  438. }
  439. /**
  440. * Ajax save template.
  441. *
  442. * @param string $module
  443. * @access public
  444. * @return void
  445. */
  446. public function ajaxSaveTemplate($module)
  447. {
  448. $templateID = (int)$this->file->saveExportTemplate($module);
  449. if(dao::isError())
  450. {
  451. $alert = '';
  452. $errors = dao::getError();
  453. foreach($errors as $errorContent) $alert .= is_array($errorContent) ? implode("\n", $errorContent) : $errorContent;
  454. return $this->send(array('alert' => $alert));
  455. }
  456. return print($this->fetch('file', 'buildExportTPL', "module=$module&templateID=$templateID"));
  457. }
  458. /**
  459. * Ajax delete template.
  460. *
  461. * @param int $templateID
  462. * @access public
  463. * @return void
  464. */
  465. public function ajaxDeleteTemplate($templateID)
  466. {
  467. $this->dao->delete()->from(TABLE_USERTPL)->where('id')->eq($templateID)->andWhere('account')->eq($this->app->user->account)->exec();
  468. }
  469. /**
  470. * Read file.
  471. *
  472. * @param int $fileID
  473. * @access public
  474. * @return void
  475. * @param int $stream
  476. */
  477. public function read($fileID, $stream = 0)
  478. {
  479. if(!($this->app->company->guest and $this->app->user->account == 'guest') and !$this->loadModel('user')->isLogon()) return print(js::locate($this->createLink('user', 'login')));
  480. $file = $this->file->getById($fileID);
  481. if(empty($file) or !$this->file->fileExists($file)) return $this->send(array('result' => 'fail', 'message' => $this->lang->file->fileNotFound, 'load' => helper::createLink('my', 'index'), 'closeModal' => true));
  482. if(!$this->file->checkPriv($file)) return $this->send(array('result' => 'fail', 'load' => array('alert' => $this->lang->file->accessDenied, 'locate' => helper::createLink('my', 'index')), 'closeModal' => true));
  483. $obLevel = ob_get_level();
  484. for($i = 0; $i < $obLevel; $i++) ob_end_clean();
  485. $mime = (isset($file->extension) and in_array($file->extension, $this->config->file->imageExtensions)) ? "image/{$file->extension}" : $this->config->file->mimes['default'];
  486. if(!$stream) helper::header('Content-type', $mime);
  487. $cacheMaxAge = 10 * 365 * 24 * 3600;
  488. helper::header('Cache-Control', 'private');
  489. helper::header('Pragma', 'cache');
  490. helper::header('Expires', gmdate('D, d M Y H:i:s', time() + $cacheMaxAge) . ' GMT');
  491. helper::header('Cache-Control', "max-age=$cacheMaxAge");
  492. if($stream)
  493. {
  494. echo base64_encode(file_get_contents($file->realPath));
  495. }
  496. else
  497. {
  498. $handle = fopen($file->realPath, "r");
  499. if($handle)
  500. {
  501. while(!feof($handle)) echo fgets($handle);
  502. fclose($handle);
  503. }
  504. }
  505. }
  506. /**
  507. * Query the file by id or gid.
  508. *
  509. * @param int|string $fileID
  510. * @param string $objectType deprecated, only suggest to use id or gid
  511. * @param int $objectID deprecated, only suggest to use id or gid
  512. * @param string $title deprecated, only suggest to use id or gid
  513. * @param string $extra deprecated, only suggest to use id or gid
  514. * @param int $stream deprecated, only suggest to use id or gid
  515. * @access public
  516. * @return void
  517. */
  518. public function ajaxQuery($fileID, $objectType = '', $objectID = 0, $title = '', $extra = '', $stream = 0)
  519. {
  520. if(!empty($fileID))
  521. {
  522. if(is_string($fileID) && !is_numeric($fileID))
  523. {
  524. $gid = base64_decode($fileID);
  525. if(strpos($gid, 'g-') === 0) $gid = substr($gid, 2);
  526. $file = $this->file->getByGid($gid);
  527. }
  528. else
  529. {
  530. $file = $this->file->getById((int)$fileID);
  531. }
  532. if($file)
  533. {
  534. if($this->viewType === 'json')
  535. {
  536. $this->send(array('result' => 'success', 'file' => array('id' => $file->id, 'title' => $file->title, 'extension' => $file->extension, 'size' => $file->realPath, 'gid' => $file->gid, 'addedBy' => $file->addedBy, 'addedDate' => $file->addedDate, 'objectType' => $file->objectType, 'objectID' => $file->objectID)));
  537. return;
  538. }
  539. $fileID = $file->id;
  540. if($stream) return $this->fetch('file', 'read', "fileID=$fileID&stream=$stream");
  541. return $this->fetch('file', 'download', "fileID=$fileID");
  542. }
  543. http_response_code(404);
  544. $this->sendError(404, '404 Not found');
  545. }
  546. if(!empty($title)) $title = urldecode(base64_decode($title));
  547. $file = $this->file->query($objectType, $objectID, $title, $extra);
  548. if(empty($file)) return $this->send(array('result' => 'fail', 'message' => $this->lang->file->fileNotFound, 'load' => helper::createLink('my', 'index'), 'closeModal' => true));
  549. if($this->viewType === 'json')
  550. {
  551. echo json_encode(array('result' => 'success', 'data' => array('id' => $file->id, 'title' => $file->title, 'extension' => $file->extension, 'size' => $file->realPath, 'gid' => $file->gid, 'addedBy' => $file->addedBy, 'addedDate' => $file->addedDate, 'objectType' => $file->objectType, 'objectID' => $file->objectID)));
  552. return;
  553. }
  554. $fileID = $file->id;
  555. if($stream) return $this->fetch('file', 'read', "fileID=$fileID&stream=$stream");
  556. return $this->fetch('file', 'download', "fileID=$fileID");
  557. }
  558. /**
  559. * 关闭升级到企业版提示。
  560. * Close the biz guide.
  561. *
  562. * @param string $moduleName
  563. * @access public
  564. * @return void
  565. */
  566. public function ajaxCloseBizGuide($moduleName)
  567. {
  568. $path = "{$this->app->user->account}.{$moduleName}.closeBizGuide@rnd";
  569. $this->loadModel('setting')->setItem($path, 1);
  570. }
  571. }