BxFilesModule.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. public function servicePruneDownloadingJobs()
  212. {
  213. $aFiles = $this->_oDb->deleteOldDownloadingJobs();
  214. if ($aFiles) foreach ($aFiles as $sFile) @unlink($sFile);
  215. }
  216. protected function _getFilesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  217. {
  218. $CNF = &$this->_oConfig->CNF;
  219. $aFile = $this->getContentFile($aContentInfo);
  220. if(!$aFile || $aFile['is_image'])
  221. return [];
  222. $sIconSrc = '';
  223. if(($oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE'])) !== false)
  224. $sIconSrc = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($aFile['file_name']));
  225. return [[
  226. 'id' => $aFile['id'],
  227. 'src' => $sIconSrc,
  228. 'src_medium' => $sIconSrc,
  229. 'src_orig' => '',
  230. 'url' => $sUrl
  231. ]];
  232. }
  233. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  234. {
  235. $aFile = $this->getContentFile($aContentInfo);
  236. if (!$aFile)
  237. return [];
  238. if (!$aFile['is_image'])
  239. return [];
  240. $sPhotoThumb = '';
  241. if ($oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($this->_oConfig->CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']))
  242. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aFile['id']);
  243. return [[
  244. 'id' => $aFile['id'],
  245. 'url' => $sUrl,
  246. 'src' => $sPhotoThumb
  247. ]];
  248. }
  249. public function actionBookmark($iContentId) {
  250. if (!$this->isLogged()) return;
  251. $aFilesToBookmark = [];
  252. if ($iContentId) $aFilesToBookmark = [$iContentId];
  253. elseif (isset($_POST['bulk'])) $aFilesToBookmark = $_POST['bulk'];
  254. if ($aFilesToBookmark)
  255. foreach ($aFilesToBookmark as $iContentId) {
  256. if ($iContentId <= 0) continue;
  257. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  258. if (!$aData) continue;
  259. $this->_oDb->bookmarkFile((int)$iContentId, bx_get_logged_profile_id());
  260. }
  261. }
  262. public function actionEntryPreview($iContentId) {
  263. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  264. $sJsCss = '';
  265. $sPreviewCode = $this->_serviceTemplateFunc ('entryFilePreview', $iContentId);
  266. // various init scripts could be there, which fail to work properly
  267. // because of a fact that it is inside a popup which is at the moment is hidden
  268. // so we need a way to postpone that initialization
  269. $aScripts = [];
  270. $sPreviewCode = preg_replace_callback('/<script>(.*)<\/script>/s', function($aMatch) use (&$aScripts) {
  271. $aScripts[] = ['code' => $aMatch[1]];
  272. return '';
  273. }, $sPreviewCode);
  274. // if there is a script then most likely it is a text + codemirror initialization, so include it here.
  275. if ($aScripts) {
  276. $sJsCss .= $this->_oTemplate->addJs('codemirror/codemirror-ext.min.js', true);
  277. $sJsCss .= $this->_oTemplate->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'codemirror/|codemirror.css', true);
  278. }
  279. $sJsCss .= $this->_oTemplate->addCss('file_handler.css', true);
  280. $sPopupCode = $this->_oTemplate->parseHtmlByName('entry-popup.html', [
  281. 'content' => $sPreviewCode,
  282. 'bx_repeat:init_scripts' => $aScripts,
  283. ]);
  284. echoJson([
  285. 'popup' => [
  286. 'html' => PopupBox('bx-files-popup', bx_process_output($aData['title']), $sJsCss.$sPopupCode),
  287. 'options' => [
  288. 'onShow' => 'bx_files_popup_init()',
  289. ],
  290. ],
  291. ]);
  292. }
  293. public function actionEntryInfo($iContentId) {
  294. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  295. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aData)) {
  296. echoJson([
  297. 'popup' => [
  298. 'html' => PopupBox('bx-files-popup', _t('_Access denied'), _t('_Access denied')),
  299. ],
  300. ]);
  301. return;
  302. }
  303. echoJson([
  304. 'popup' => [
  305. 'html' => PopupBox('bx-files-popup', bx_process_output($aData['title']), $this->_oTemplate->entryInfoPopup($aData)),
  306. ],
  307. ]);
  308. }
  309. public function actionEntryEditTitle($iContentId) {
  310. $iContentId = intval($iContentId);
  311. $aData = $this->_oDb->getContentInfoById($iContentId);
  312. $CNF = &$this->_oConfig->CNF;
  313. if (!$aData) {
  314. echoJson(['message' => _t('_bx_files_txt_error')]);
  315. exit;
  316. }
  317. if ($this->checkAllowedEdit($aData, false) !== CHECK_ACTION_RESULT_ALLOWED) {
  318. echoJson(['message' => _t('_bx_files_txt_permission_denied')]);
  319. exit;
  320. }
  321. setlocale(LC_ALL,'C.UTF-8'); //otherwise pathinfo returns empty string on a non-latin characters filenames
  322. if (isset($_POST['title'])) {
  323. if ($sNewTitle = bx_get('title')) {
  324. $sExt = pathinfo($aData[$CNF['FIELD_TITLE']], PATHINFO_EXTENSION);
  325. if ($sExt) $sExt = '.'.$sExt;
  326. $this->_oDb->updateEntryTitle($iContentId, $sNewTitle.$sExt);
  327. $aData = $this->_oDb->getContentInfoById($iContentId);
  328. echo bx_process_output($aData[$CNF['FIELD_TITLE']]);
  329. }
  330. exit;
  331. }
  332. $sActionUrl = BX_DOL_URL_ROOT.$this->_oConfig->getBaseUri();
  333. echoJson([
  334. 'eval' => "bx_prompt('"._t('_bx_files_form_entry_input_title')."', '".bx_js_string(pathinfo($aData[$CNF['FIELD_TITLE']], PATHINFO_FILENAME))."', function(val) {
  335. $.post('{$sActionUrl}entry_edit_title/{$iContentId}', {title: val.getValue()}, function(sTitle) {
  336. if (sTitle)
  337. $('.bx-file-entry[file_id=".$aData[$CNF['FIELD_ID']]."] .bx-files-filename a').html(sTitle);
  338. });
  339. })",
  340. ]);
  341. }
  342. public function actionEntryDelete($iContentId) {
  343. $aFilesToDelete = [];
  344. if ($iContentId) $aFilesToDelete = [$iContentId];
  345. elseif (isset($_POST['bulk'])) $aFilesToDelete = $_POST['bulk'];
  346. if ($aFilesToDelete)
  347. foreach ($aFilesToDelete as $iContentId) {
  348. $iContentId = intval($iContentId);
  349. if ($iContentId <= 0) continue;
  350. $aData = $this->_oDb->getContentInfoById($iContentId);
  351. $CNF = &$this->_oConfig->CNF;
  352. if (!$aData) {
  353. echoJson(['message' => _t('_bx_files_txt_error')]);
  354. exit;
  355. }
  356. if ($this->checkAllowedDelete($aData, false) !== CHECK_ACTION_RESULT_ALLOWED) {
  357. continue;
  358. }
  359. $this->serviceDeleteEntity($iContentId);
  360. }
  361. if (!isset($_POST['bulk'])) {
  362. echoJson([
  363. 'eval' => "
  364. $('.bx-file-entry[file_id={$iContentId}]').fadeOut();
  365. $(window).trigger('files_browser.update');
  366. ",
  367. ]);
  368. } else {
  369. echoJson([]);
  370. }
  371. }
  372. public function actionClearGhosts() {
  373. if (!isLogged()) die;
  374. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  375. if ($oStorage) {
  376. $aFiles = $oStorage->getGhosts(bx_get_logged_profile_id(), 0);
  377. if ($aFiles) foreach ($aFiles as $aFile) {
  378. $oStorage->deleteFile($aFile['id']);
  379. }
  380. }
  381. }
  382. public function actionUploadCompleted() {
  383. $iContext = intval(bx_get('context'));
  384. $iFolder = intval(bx_get('folder'));
  385. if (!isLogged()) {
  386. echoJson(['message' => _t('_Access denied')]);
  387. exit;
  388. }
  389. if ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || $iContext && !$this->serviceIsAllowedAddContentToContext($iContext)) {
  390. echoJson(['message' => _t('_Access denied')]);
  391. exit;
  392. }
  393. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  394. if ($oStorage) {
  395. $iAuthorProfile = bx_get_logged_profile_id();
  396. $aFiles = $oStorage->getGhosts($iAuthorProfile, 0);
  397. if ($aFiles) {
  398. $aFilesIDs = [];
  399. foreach($aFiles as $aFile) {
  400. $aFilesIDs[] = $aFile['id'];
  401. bx_set('title-'.$aFile['id'], $aFile['file_name'], 'post');
  402. }
  403. bx_set('attachments', $aFilesIDs, 'post');
  404. bx_set('profile_id', $iAuthorProfile, 'post');
  405. bx_set('allow_view_to', !$iContext ? BX_DOL_PG_ALL : -$iContext, 'post');
  406. bx_set('cf', 1, 'post');
  407. $oForm = BxDolForm::getObjectInstance($this->_oConfig->CNF['OBJECT_FORM_ENTRY_UPLOAD'], $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_UPLOAD']);
  408. $aFiles = $oForm->insert();
  409. if ($iFolder != 0 && !empty($aFiles))
  410. $this->_oDb->moveFilesToFolder($aFiles, $iFolder);
  411. if (!empty($aFiles))
  412. foreach ($aFiles as $iContentId) {
  413. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  414. $this->alertAfterAdd($aContentInfo);
  415. }
  416. }
  417. }
  418. echoJson(['success' => true]);
  419. }
  420. public function actionCreateFolder() {
  421. $iContext = intval(bx_get('context'));
  422. $iFolder = intval(bx_get('current_folder'));
  423. $sName = trim(bx_get('name'));
  424. if (!isLogged()) {
  425. echoJson(['message' => _t('_Access denied')]);
  426. exit;
  427. }
  428. if ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || $iContext && !$this->serviceIsAllowedAddContentToContext($iContext)) {
  429. echoJson(['message' => _t('_Access denied')]);
  430. exit;
  431. }
  432. if (!empty($sName)) {
  433. $iAuthor = bx_get_logged_profile_id();
  434. // in case it is posted to context make the context profile as an author of an entry
  435. // to leave folder in case creator is removed
  436. // and to delete folder when context profile is being deleted
  437. $this->_oDb->createFolder($iFolder, $iContext ? $iContext : $iAuthor, !$iContext || $iAuthor == $iContext ? BX_DOL_PG_ALL : -$iContext, $sName);
  438. }
  439. echoJson([]);
  440. exit;
  441. }
  442. public function actionMoveFiles() {
  443. $iContentId = intval(bx_get('file'));
  444. $aFilesToMove = [];
  445. if ($iContentId) $aFilesToMove = [$iContentId];
  446. elseif (isset($_POST['bulk'])) $aFilesToMove = $_POST['bulk'];
  447. $iContentId = reset($aFilesToMove);
  448. $aData = $this->_oDb->getContentInfoById($iContentId);
  449. if (!$aData) {
  450. echoJson(['message' => _t('_bx_files_txt_error')]);
  451. exit;
  452. }
  453. $CNF = &$this->_oConfig->CNF;
  454. $iContext = $aData[$CNF['FIELD_ALLOW_VIEW_TO']] < 0 ? -$aData[$CNF['FIELD_ALLOW_VIEW_TO']] : $aData[$CNF['FIELD_AUTHOR']];
  455. if (!isAdmin() && ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || !$this->serviceIsAllowedAddContentToContext($iContext))) {
  456. echoJson(['message' => _t('_Access denied')]);
  457. exit;
  458. }
  459. if (isset($_POST['move_to'])) {
  460. $iMoveTo = intval(bx_get('move_to'));
  461. if ($iMoveTo) {
  462. $aDataMoveTo = $this->_oDb->getContentInfoById($iMoveTo);
  463. if (!$aDataMoveTo) {
  464. echoJson(['message' => _t('_bx_files_txt_error')]);
  465. exit;
  466. }
  467. $iContextMoveTo = $aDataMoveTo[$CNF['FIELD_ALLOW_VIEW_TO']] < 0 ? -$aDataMoveTo[$CNF['FIELD_ALLOW_VIEW_TO']] : $aDataMoveTo[$CNF['FIELD_AUTHOR']];
  468. if (!isAdmin() && ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || !$this->serviceIsAllowedAddContentToContext($iContextMoveTo))) {
  469. echoJson(['message' => _t('_Access denied')]);
  470. exit;
  471. }
  472. }
  473. $this->_oDb->moveFilesToFolder($aFilesToMove, $iMoveTo);
  474. echoJson([
  475. 'eval' => "
  476. $(window).trigger('files_browser.update');
  477. ",
  478. ]);
  479. exit;
  480. }
  481. $aFolders = $this->_oDb->getFoldersInContext($iContext);
  482. if (!$aFolders) {
  483. echoJson(['message' => _t('_bx_files_txt_move_to_nowhere')]);
  484. exit;
  485. }
  486. echoJson([
  487. 'popup' => [
  488. 'html' => PopupBox('bx-files-popup', _t('_bx_files_txt_move_to'), $this->_oTemplate->getJsTree($aFilesToMove, $aFolders)),
  489. 'options' => [],
  490. ],
  491. ]);
  492. }
  493. public function serviceDeleteEntitiesByAuthor ($iProfileId) {
  494. $this->_oDb->deleteProfileBookmarks($iProfileId);
  495. return parent::serviceDeleteEntitiesByAuthor ($iProfileId);
  496. }
  497. public function serviceDeleteEntity ($iContentId, $sFuncDelete = 'deleteData') {
  498. $aData = $this->_oDb->getContentInfoById($iContentId);
  499. if ($aData['type'] == 'folder') {
  500. $aNestedFiles = $this->_oDb->getFolderFiles($aData[$this->_oConfig->CNF['FIELD_ID']]);
  501. if ($aNestedFiles)
  502. foreach ($aNestedFiles as $iFile) {
  503. $this->serviceDeleteEntity($iFile);
  504. }
  505. }
  506. parent::serviceDeleteEntity($iContentId, $sFuncDelete);
  507. }
  508. public function serviceBrowseContext ($iProfileId = 0, $aParams = array()) {
  509. if(!$iProfileId)
  510. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  511. if (!$iProfileId)
  512. return;
  513. // do not show files in context block for persons profiles
  514. $oProfile = BxDolProfile::getInstance($iProfileId);
  515. if ($oProfile && $oProfile->getModule() == 'bx_persons') return;
  516. return parent::serviceBrowseContext($iProfileId, $aParams);
  517. }
  518. public function serviceGetFile ($iContentId, $aParams = [])
  519. {
  520. $aResult = parent::serviceGetFile ($iContentId, $aParams);
  521. if(!$aResult)
  522. return $aResult;
  523. $aResult['url_download'] = $this->getDownloadLink($iContentId);
  524. return $aResult;
  525. }
  526. }
  527. /** @} */