BxFilesModule.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Files Files
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Files module
  13. */
  14. class BxFilesModule extends BxBaseModFilesModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. }
  20. public function serviceMyEntriesActions ($iProfileId = 0)
  21. {
  22. if (!$iProfileId)
  23. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  24. if (!$this->serviceIsAllowedAddContentToContext($iProfileId))
  25. return false;
  26. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_MY_ENTRIES']);
  27. return $oMenu ? $oMenu->getCode() : false;
  28. }
  29. public function checkAllowedSetThumb ($iContentId = 0)
  30. {
  31. return _t('_sys_txt_access_denied');
  32. }
  33. public function actionDownload($sToken = '', $iContentId = 0, $sBulk = '')
  34. {
  35. $CNF = $this->_oConfig->CNF;
  36. $aFilesToDownload = [$iContentId];
  37. if (!empty($sBulk)) {
  38. $aFilesToDownload = json_decode($sBulk);
  39. }
  40. if (!is_array($aFilesToDownload)) {
  41. $this->_oTemplate->displayPageNotFound();
  42. return;
  43. }
  44. $iContentId = reset($aFilesToDownload);
  45. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  46. if (!$aData) {
  47. $this->_oTemplate->displayPageNotFound();
  48. return;
  49. }
  50. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aData)) {
  51. $this->_oTemplate->displayAccessDenied();
  52. return;
  53. }
  54. if ($aData['type'] == 'folder' || $aFilesToDownload && count($aFilesToDownload) > 1) {
  55. bx_import('BulkDownloader', $this->_aModule);
  56. $oDownloader = new BxFilesBulkDownloader($this);
  57. $aResult = $oDownloader->createDownloadingJob($aFilesToDownload, $aData[$CNF['FIELD_TITLE']]);
  58. switch ($aResult['status']) {
  59. case BX_FILES_DOWNLOADER_STATUS_TOO_LARGE:
  60. $this->_oTemplate->displayMsg(_t('_bx_files_txt_max_size_reached', _t_format_size(intval(getParam('bx_files_max_bulk_download_size')) * 1024 * 1024)));
  61. break;
  62. case BX_FILES_DOWNLOADER_STATUS_DOWNLOADING:
  63. $this->_oTemplate->addJsTranslation('_bx_files_txt_status_downloading_ready_msg');
  64. $this->_oTemplate->displayMsg(['title' => _t('_bx_files_txt_status_downloading'), 'content' => $this->_oTemplate->parseHtmlByName('bulk_downloader_process.html', [
  65. 'timeout' => BX_FILES_DOWNLOADER_TIMEOUT_REQUESTS,
  66. 'action_url' => BX_DOL_URL_ROOT.$this->_oConfig->getBaseUri().'process_download/'.$aResult['job'],
  67. ])]);
  68. break;
  69. case BX_FILES_DOWNLOADER_STATUS_READY:
  70. bx_smart_readfile($aResult['file'], $aResult['filename'], 'application/zip');
  71. break;
  72. case BX_FILES_DOWNLOADER_STATUS_EMPTY:
  73. default:
  74. $this->_oTemplate->displayPageNotFound();
  75. break;
  76. }
  77. exit;
  78. }
  79. $aFile = $this->getContentFile($aData);
  80. if (!$aFile) {
  81. $this->_oTemplate->displayPageNotFound();
  82. return;
  83. }
  84. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  85. if (!$oStorage) {
  86. $this->_oTemplate->displayErrorOccured();
  87. return;
  88. }
  89. if (!$oStorage->download($aFile['remote_id'], $sToken, true)) {
  90. $this->_oTemplate->displayErrorOccured();
  91. return;
  92. }
  93. exit;
  94. }
  95. public function actionProcessDownload($iDownloadingJobId) {
  96. if (!$iDownloadingJobId) die;
  97. $aJob = $this->_oDb->getDownloadingJob($iDownloadingJobId);
  98. if (!$aJob || $aJob['owner'] != bx_get_logged_profile_id()) die;
  99. if ($aJob['result']) {
  100. bx_smart_readfile($aJob['result'], $aJob['name'], 'application/zip');
  101. exit;
  102. }
  103. bx_import('BulkDownloader', $this->_aModule);
  104. $oDownloader = new BxFilesBulkDownloader($this);
  105. $mRes = $oDownloader->processDownloading($aJob['files']);
  106. if ($mRes === BX_FILES_DOWNLOADER_STATUS_DOWNLOADING) {
  107. $this->_oDb->updateDownloadingJob($iDownloadingJobId, $aJob['files'], '');
  108. echoJson([
  109. 'status' => 'downloading',
  110. ]);
  111. } else {
  112. $this->_oDb->updateDownloadingJob($iDownloadingJobId, $aJob['files'], $mRes);
  113. echoJson([
  114. 'status' => 'ready',
  115. ]);
  116. }
  117. }
  118. public function getContentFile($aData)
  119. {
  120. $CNF = $this->_oConfig->CNF;
  121. if (!isset($aData[$CNF['FIELD_AUTHOR']]) || !isset($aData[$CNF['FIELD_ID']]))
  122. return false;
  123. if (isset($aData['type']) && $aData['type'] == 'folder') {
  124. return [
  125. 'id' => $aData[$CNF['FIELD_ID']],
  126. 'profile_id' => $aData[$CNF['FIELD_AUTHOR']],
  127. 'remote_id' => 0,
  128. 'path' => '',
  129. 'file_name' => $aData[$CNF['FIELD_TITLE']],
  130. 'mime_type' => '',
  131. 'ext' => '',
  132. 'size' => 0,
  133. 'added' => $aData[$CNF['FIELD_ADDED']],
  134. 'modified' => 0,
  135. 'private' => false,
  136. 'is_image' => false,
  137. ];
  138. }
  139. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  140. if (!$oStorage)
  141. return false;
  142. if (isset($aData[$CNF['FIELD_FILE_ID']]) && $aData[$CNF['FIELD_FILE_ID']])
  143. return $this->_addFileFields($oStorage->getFile($aData[$CNF['FIELD_FILE_ID']]));
  144. $aGhostFiles = $oStorage->getGhosts ($aData[$CNF['FIELD_AUTHOR']], $aData[$CNF['FIELD_ID']]);
  145. if (!$aGhostFiles)
  146. return false;
  147. return $this->_addFileFields(array_pop($aGhostFiles));
  148. }
  149. public function getDownloadLink($mixedData)
  150. {
  151. $CNF = &$this->_oConfig->CNF;
  152. if(!is_array($mixedData))
  153. $mixedData = $this->_oDb->getContentInfoById((int)$mixedData);
  154. if(empty($mixedData) || !is_array($mixedData))
  155. return false;
  156. $aFile = $this->getContentFile($mixedData);
  157. if($aFile === false)
  158. return false;
  159. $sFileExt = !empty($aFile['ext']) ? $aFile['ext'] : '';
  160. $aMarkers = array_merge([
  161. 'content_id' => $mixedData[$CNF['FIELD_ID']]
  162. ], ($aFile['private'] && ($oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE'])) ? [
  163. 'file_download_token' => $oStorage->genToken($aFile['id']),
  164. 'file_ext' => $sFileExt
  165. ] : [
  166. 'file_download_token' => '0',
  167. 'file_ext' => $sFileExt,
  168. ]));
  169. return bx_replace_markers(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_DOWNLOAD']), $aMarkers);
  170. }
  171. protected function _addFileFields($aFile)
  172. {
  173. if (is_array($aFile))
  174. $aFile['is_image'] = in_array($aFile['ext'], array('jpg', 'jpeg', 'png', 'gif', /* when ImageMagick is used - 'tif', 'tiff', 'bmp', 'ico', 'psd' */));
  175. return $aFile;
  176. }
  177. public function serviceEntityFilePreview($iContentId = 0)
  178. {
  179. return $this->_serviceTemplateFunc ('entryFilePreview', $iContentId);
  180. }
  181. public function serviceProcessFilesData($iNumberOfFilesToProcessAtOnce = 3)
  182. {
  183. $CNF = $this->_oConfig->CNF;
  184. if (!defined('BX_SYSTEM_JAVA') || !constant('BX_SYSTEM_JAVA'))
  185. return;
  186. $a = $this->_oDb->getNotProcessedFiles($iNumberOfFilesToProcessAtOnce);
  187. if (!$a)
  188. return;
  189. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  190. if (!$oStorage)
  191. return false;
  192. foreach ($a as $aContentInfo) {
  193. $aFile = $this->getContentFile($aContentInfo);
  194. if (!$aFile) {
  195. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  196. continue;
  197. }
  198. $sFileUrl = $oStorage->getFileUrlById($aFile['id']);
  199. $sFilePath = BX_DIRECTORY_PATH_TMP . $aFile['remote_id'] . '.' . $aFile['ext'];
  200. @file_put_contents($sFilePath, file_get_contents($sFileUrl));
  201. if (!file_exists($sFilePath)) {
  202. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  203. continue;
  204. }
  205. $sCommand = '"' . constant('BX_SYSTEM_JAVA') . '" -Djava.awt.headless=true -jar "' . $this->_oConfig->getHomePath() . 'data/tika-app.jar" --encoding=UTF-8 --text "' . $sFilePath . '"';
  206. $sData = shell_exec($sCommand);
  207. @unlink($sFilePath);
  208. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], $sData);
  209. }
  210. }
  211. /**
  212. * The Studio Dashboard's Queues block: archive download jobs. Clearing removes the jobs older than an hour (finished or
  213. * abandoned), the module's own cron prunes them after a day.
  214. */
  215. public function serviceGetDashboardQueues()
  216. {
  217. return array(
  218. 'downloads' => array(
  219. 'name' => '_bx_files_queue_downloads',
  220. 'icon' => 'download',
  221. 'all' => "SELECT COUNT(*) FROM `bx_files_downloading_jobs`",
  222. 'action' => "DELETE FROM `bx_files_downloading_jobs` WHERE UNIX_TIMESTAMP() - `started` > 3600"
  223. )
  224. );
  225. }
  226. public function servicePruneDownloadingJobs()
  227. {
  228. $aFiles = $this->_oDb->deleteOldDownloadingJobs();
  229. if ($aFiles) foreach ($aFiles as $sFile) @unlink($sFile);
  230. }
  231. protected function _getFilesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  232. {
  233. $CNF = &$this->_oConfig->CNF;
  234. $aFile = $this->getContentFile($aContentInfo);
  235. if(!$aFile || $aFile['is_image'])
  236. return [];
  237. $sIconSrc = '';
  238. if(($oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE'])) !== false)
  239. $sIconSrc = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  240. return [[
  241. 'id' => $aFile['id'],
  242. 'src' => $sIconSrc,
  243. 'src_medium' => $sIconSrc,
  244. 'src_orig' => '',
  245. 'url' => $sUrl
  246. ]];
  247. }
  248. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  249. {
  250. $aFile = $this->getContentFile($aContentInfo);
  251. if (!$aFile)
  252. return [];
  253. if (!$aFile['is_image'])
  254. return [];
  255. $sPhotoThumb = '';
  256. if ($oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($this->_oConfig->CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']))
  257. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aFile['id']);
  258. return [[
  259. 'id' => $aFile['id'],
  260. 'url' => $sUrl,
  261. 'src' => $sPhotoThumb
  262. ]];
  263. }
  264. public function actionBookmark($iContentId) {
  265. if (!$this->isLogged()) return;
  266. $aFilesToBookmark = [];
  267. if ($iContentId) $aFilesToBookmark = [$iContentId];
  268. elseif (isset($_POST['bulk'])) $aFilesToBookmark = $_POST['bulk'];
  269. if ($aFilesToBookmark)
  270. foreach ($aFilesToBookmark as $iContentId) {
  271. if ($iContentId <= 0) continue;
  272. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  273. if (!$aData) continue;
  274. $this->_oDb->bookmarkFile((int)$iContentId, bx_get_logged_profile_id());
  275. }
  276. }
  277. public function actionEntryPreview($iContentId) {
  278. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  279. $sJsCss = '';
  280. $sPreviewCode = $this->_serviceTemplateFunc ('entryFilePreview', $iContentId);
  281. // various init scripts could be there, which fail to work properly
  282. // because of a fact that it is inside a popup which is at the moment is hidden
  283. // so we need a way to postpone that initialization
  284. $aScripts = [];
  285. $sPreviewCode = preg_replace_callback('/<script>(.*)<\/script>/s', function($aMatch) use (&$aScripts) {
  286. $aScripts[] = ['code' => $aMatch[1]];
  287. return '';
  288. }, $sPreviewCode);
  289. // if there is a script then most likely it is a text + codemirror initialization, so include it here.
  290. if ($aScripts) {
  291. $sJsCss .= $this->_oTemplate->addJs('codemirror/codemirror-ext.min.js', true);
  292. $sJsCss .= $this->_oTemplate->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'codemirror/|codemirror.css', true);
  293. }
  294. $sJsCss .= $this->_oTemplate->addCss('file_handler.css', true);
  295. $sPopupCode = $this->_oTemplate->parseHtmlByName('entry-popup.html', [
  296. 'content' => $sPreviewCode,
  297. 'bx_repeat:init_scripts' => $aScripts,
  298. ]);
  299. echoJson([
  300. 'popup' => [
  301. 'html' => PopupBox('bx-files-popup', bx_process_output($aData['title']), $sJsCss.$sPopupCode),
  302. 'options' => [
  303. 'onShow' => 'bx_files_popup_init()',
  304. ],
  305. ],
  306. ]);
  307. }
  308. public function actionEntryInfo($iContentId) {
  309. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  310. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aData)) {
  311. echoJson([
  312. 'popup' => [
  313. 'html' => PopupBox('bx-files-popup', _t('_Access denied'), _t('_Access denied')),
  314. ],
  315. ]);
  316. return;
  317. }
  318. echoJson([
  319. 'popup' => [
  320. 'html' => PopupBox('bx-files-popup', bx_process_output($aData['title']), $this->_oTemplate->entryInfoPopup($aData)),
  321. ],
  322. ]);
  323. }
  324. public function actionEntryEditTitle($iContentId) {
  325. $iContentId = intval($iContentId);
  326. $aData = $this->_oDb->getContentInfoById($iContentId);
  327. $CNF = &$this->_oConfig->CNF;
  328. if (!$aData) {
  329. echoJson(['message' => _t('_bx_files_txt_error')]);
  330. exit;
  331. }
  332. if ($this->checkAllowedEdit($aData, false) !== CHECK_ACTION_RESULT_ALLOWED) {
  333. echoJson(['message' => _t('_bx_files_txt_permission_denied')]);
  334. exit;
  335. }
  336. setlocale(LC_ALL,'C.UTF-8'); //otherwise pathinfo returns empty string on a non-latin characters filenames
  337. if (isset($_POST['title'])) {
  338. if ($sNewTitle = bx_get('title')) {
  339. $sExt = pathinfo($aData[$CNF['FIELD_TITLE']], PATHINFO_EXTENSION);
  340. if ($sExt) $sExt = '.'.$sExt;
  341. $this->_oDb->updateEntryTitle($iContentId, $sNewTitle.$sExt);
  342. $aData = $this->_oDb->getContentInfoById($iContentId);
  343. echo bx_process_output($aData[$CNF['FIELD_TITLE']]);
  344. }
  345. exit;
  346. }
  347. $sActionUrl = BX_DOL_URL_ROOT.$this->_oConfig->getBaseUri();
  348. echoJson([
  349. 'eval' => "bx_prompt('"._t('_bx_files_form_entry_input_title')."', '".bx_js_string(pathinfo($aData[$CNF['FIELD_TITLE']], PATHINFO_FILENAME))."', function(val) {
  350. $.post('{$sActionUrl}entry_edit_title/{$iContentId}', {title: val.getValue()}, function(sTitle) {
  351. if (sTitle)
  352. $('.bx-file-entry[file_id=".$aData[$CNF['FIELD_ID']]."] .bx-files-filename a').html(sTitle);
  353. });
  354. })",
  355. ]);
  356. }
  357. public function actionEntryDelete($iContentId) {
  358. $aFilesToDelete = [];
  359. if ($iContentId) $aFilesToDelete = [$iContentId];
  360. elseif (isset($_POST['bulk'])) $aFilesToDelete = $_POST['bulk'];
  361. if ($aFilesToDelete)
  362. foreach ($aFilesToDelete as $iContentId) {
  363. $iContentId = intval($iContentId);
  364. if ($iContentId <= 0) continue;
  365. $aData = $this->_oDb->getContentInfoById($iContentId);
  366. $CNF = &$this->_oConfig->CNF;
  367. if (!$aData) {
  368. echoJson(['message' => _t('_bx_files_txt_error')]);
  369. exit;
  370. }
  371. if ($this->checkAllowedDelete($aData, false) !== CHECK_ACTION_RESULT_ALLOWED) {
  372. continue;
  373. }
  374. $this->serviceDeleteEntity($iContentId);
  375. }
  376. if (!isset($_POST['bulk'])) {
  377. echoJson([
  378. 'eval' => "
  379. $('.bx-file-entry[file_id={$iContentId}]').fadeOut();
  380. $(window).trigger('files_browser.update');
  381. ",
  382. ]);
  383. } else {
  384. echoJson([]);
  385. }
  386. }
  387. public function actionClearGhosts() {
  388. if (!isLogged()) die;
  389. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  390. if ($oStorage) {
  391. $aFiles = $oStorage->getGhosts(bx_get_logged_profile_id(), 0);
  392. if ($aFiles) foreach ($aFiles as $aFile) {
  393. $oStorage->deleteFile($aFile['id']);
  394. }
  395. }
  396. }
  397. public function actionUploadCompleted() {
  398. $iContext = intval(bx_get('context'));
  399. $iFolder = intval(bx_get('folder'));
  400. if (!isLogged()) {
  401. echoJson(['message' => _t('_Access denied')]);
  402. exit;
  403. }
  404. if ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || $iContext && !$this->serviceIsAllowedAddContentToContext($iContext)) {
  405. echoJson(['message' => _t('_Access denied')]);
  406. exit;
  407. }
  408. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  409. if ($oStorage) {
  410. $iAuthorProfile = bx_get_logged_profile_id();
  411. $aFiles = $oStorage->getGhosts($iAuthorProfile, 0);
  412. if ($aFiles) {
  413. $aFilesIDs = [];
  414. foreach($aFiles as $aFile) {
  415. $aFilesIDs[] = $aFile['id'];
  416. bx_set('title-'.$aFile['id'], $aFile['file_name'], 'post');
  417. }
  418. bx_set('attachments', $aFilesIDs, 'post');
  419. bx_set('profile_id', $iAuthorProfile, 'post');
  420. bx_set('allow_view_to', !$iContext ? BX_DOL_PG_ALL : -$iContext, 'post');
  421. bx_set('cf', 1, 'post');
  422. $oForm = BxDolForm::getObjectInstance($this->_oConfig->CNF['OBJECT_FORM_ENTRY_UPLOAD'], $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_UPLOAD']);
  423. $aFiles = $oForm->insert();
  424. if ($iFolder != 0 && !empty($aFiles))
  425. $this->_oDb->moveFilesToFolder($aFiles, $iFolder);
  426. if (!empty($aFiles))
  427. foreach ($aFiles as $iContentId) {
  428. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  429. $this->alertAfterAdd($aContentInfo);
  430. }
  431. }
  432. }
  433. echoJson(['success' => true]);
  434. }
  435. public function actionCreateFolder() {
  436. $iContext = intval(bx_get('context'));
  437. $iFolder = intval(bx_get('current_folder'));
  438. $sName = trim(bx_get('name'));
  439. if (!isLogged()) {
  440. echoJson(['message' => _t('_Access denied')]);
  441. exit;
  442. }
  443. if ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || $iContext && !$this->serviceIsAllowedAddContentToContext($iContext)) {
  444. echoJson(['message' => _t('_Access denied')]);
  445. exit;
  446. }
  447. if (!empty($sName)) {
  448. $iAuthor = bx_get_logged_profile_id();
  449. // in case it is posted to context make the context profile as an author of an entry
  450. // to leave folder in case creator is removed
  451. // and to delete folder when context profile is being deleted
  452. $this->_oDb->createFolder($iFolder, $iContext ? $iContext : $iAuthor, !$iContext || $iAuthor == $iContext ? BX_DOL_PG_ALL : -$iContext, $sName);
  453. }
  454. echoJson([]);
  455. exit;
  456. }
  457. public function actionMoveFiles() {
  458. $iContentId = intval(bx_get('file'));
  459. $aFilesToMove = [];
  460. if ($iContentId) $aFilesToMove = [$iContentId];
  461. elseif (isset($_POST['bulk'])) $aFilesToMove = $_POST['bulk'];
  462. $iContentId = reset($aFilesToMove);
  463. $aData = $this->_oDb->getContentInfoById($iContentId);
  464. if (!$aData) {
  465. echoJson(['message' => _t('_bx_files_txt_error')]);
  466. exit;
  467. }
  468. $CNF = &$this->_oConfig->CNF;
  469. $iContext = $aData[$CNF['FIELD_ALLOW_VIEW_TO']] < 0 ? -$aData[$CNF['FIELD_ALLOW_VIEW_TO']] : $aData[$CNF['FIELD_AUTHOR']];
  470. if (!isAdmin() && ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || !$this->serviceIsAllowedAddContentToContext($iContext))) {
  471. echoJson(['message' => _t('_Access denied')]);
  472. exit;
  473. }
  474. if (isset($_POST['move_to'])) {
  475. $iMoveTo = intval(bx_get('move_to'));
  476. if ($iMoveTo) {
  477. $aDataMoveTo = $this->_oDb->getContentInfoById($iMoveTo);
  478. if (!$aDataMoveTo) {
  479. echoJson(['message' => _t('_bx_files_txt_error')]);
  480. exit;
  481. }
  482. $iContextMoveTo = $aDataMoveTo[$CNF['FIELD_ALLOW_VIEW_TO']] < 0 ? -$aDataMoveTo[$CNF['FIELD_ALLOW_VIEW_TO']] : $aDataMoveTo[$CNF['FIELD_AUTHOR']];
  483. if (!isAdmin() && ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || !$this->serviceIsAllowedAddContentToContext($iContextMoveTo))) {
  484. echoJson(['message' => _t('_Access denied')]);
  485. exit;
  486. }
  487. }
  488. $this->_oDb->moveFilesToFolder($aFilesToMove, $iMoveTo);
  489. echoJson([
  490. 'eval' => "
  491. $(window).trigger('files_browser.update');
  492. ",
  493. ]);
  494. exit;
  495. }
  496. $aFolders = $this->_oDb->getFoldersInContext($iContext);
  497. if (!$aFolders) {
  498. echoJson(['message' => _t('_bx_files_txt_move_to_nowhere')]);
  499. exit;
  500. }
  501. echoJson([
  502. 'popup' => [
  503. 'html' => PopupBox('bx-files-popup', _t('_bx_files_txt_move_to'), $this->_oTemplate->getJsTree($aFilesToMove, $aFolders)),
  504. 'options' => [],
  505. ],
  506. ]);
  507. }
  508. public function serviceDeleteEntitiesByAuthor ($iProfileId) {
  509. $this->_oDb->deleteProfileBookmarks($iProfileId);
  510. return parent::serviceDeleteEntitiesByAuthor ($iProfileId);
  511. }
  512. public function serviceDeleteEntity ($iContentId, $sFuncDelete = 'deleteData') {
  513. $aData = $this->_oDb->getContentInfoById($iContentId);
  514. if ($aData['type'] == 'folder') {
  515. $aNestedFiles = $this->_oDb->getFolderFiles($aData[$this->_oConfig->CNF['FIELD_ID']]);
  516. if ($aNestedFiles)
  517. foreach ($aNestedFiles as $iFile) {
  518. $this->serviceDeleteEntity($iFile);
  519. }
  520. }
  521. parent::serviceDeleteEntity($iContentId, $sFuncDelete);
  522. }
  523. public function serviceBrowseContext ($iProfileId = 0, $aParams = array()) {
  524. if(!$iProfileId)
  525. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  526. if (!$iProfileId)
  527. return;
  528. // do not show files in context block for persons profiles
  529. $oProfile = BxDolProfile::getInstance($iProfileId);
  530. if ($oProfile && $oProfile->getModule() == 'bx_persons') return;
  531. return parent::serviceBrowseContext($iProfileId, $aParams);
  532. }
  533. public function serviceGetFile ($iContentId, $aParams = [])
  534. {
  535. $aResult = parent::serviceGetFile ($iContentId, $aParams);
  536. if(!$aResult)
  537. return $aResult;
  538. $aResult['url_download'] = $this->getDownloadLink($iContentId);
  539. return $aResult;
  540. }
  541. }
  542. /** @} */