1
0

BxFilesModule.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 BxBaseModTextModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. }
  20. public function serviceIsAllowedAddContentToProfile($iGroupProfileId)
  21. {
  22. if (!$iGroupProfileId || !($oProfile = BxDolProfile::getInstance((int)$iGroupProfileId)))
  23. return false;
  24. if ($iGroupProfileId == bx_get_logged_profile_id())
  25. return true;
  26. $sProfileModule = $oProfile->getModule();
  27. $aContentInfo = BxDolService::call($sProfileModule, 'get_content_info_by_id', array($oProfile->getContentId()));
  28. if (BxDolService::call($sProfileModule, 'is_group_profile') && (BxDolService::call($sProfileModule, 'is_fan', array($iGroupProfileId)) || bx_get_logged_profile_id() == $aContentInfo['author']))
  29. return true;
  30. return false;
  31. }
  32. public function serviceMyEntriesActions ($iProfileId = 0)
  33. {
  34. if (!$iProfileId)
  35. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  36. if (!$this->serviceIsAllowedAddContentToProfile($iProfileId))
  37. return false;
  38. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_MY_ENTRIES']);
  39. return $oMenu ? $oMenu->getCode() : false;
  40. }
  41. public function checkAllowedSetThumb ($iContentId = 0)
  42. {
  43. return _t('_sys_txt_access_denied');
  44. }
  45. public function actionDownload($sToken, $iContentId, $sBulk = '')
  46. {
  47. $CNF = $this->_oConfig->CNF;
  48. $aFilesToDownload = [$iContentId];
  49. if (!empty($sBulk)) {
  50. $aFilesToDownload = json_decode($sBulk);
  51. }
  52. $iContentId = reset($aFilesToDownload);
  53. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  54. if (!$aData) {
  55. $this->_oTemplate->displayPageNotFound();
  56. return;
  57. }
  58. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aData)) {
  59. $this->_oTemplate->displayAccessDenied();
  60. return;
  61. }
  62. if ($aData['type'] == 'folder' || $aFilesToDownload) {
  63. $sZipFilePath = $this->prepareFolderForDownloading($aFilesToDownload);
  64. if (!$sZipFilePath) {
  65. $this->_oTemplate->displayPageNotFound();
  66. return;
  67. }
  68. $sFolderName = $this->sanitizeFileName($aData[$CNF['FIELD_TITLE']]).'.zip';
  69. bx_smart_readfile($sZipFilePath, $sFolderName, 'application/zip');
  70. exit;
  71. }
  72. $aFile = $this->getContentFile($aData);
  73. if (!$aFile) {
  74. $this->_oTemplate->displayPageNotFound();
  75. return;
  76. }
  77. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  78. if (!$oStorage) {
  79. $this->_oTemplate->displayErrorOccured();
  80. return;
  81. }
  82. if (!$oStorage->download($aFile['remote_id'], $sToken)) {
  83. $this->_oTemplate->displayErrorOccured();
  84. return;
  85. }
  86. exit;
  87. }
  88. public function getContentFile($aData)
  89. {
  90. $CNF = $this->_oConfig->CNF;
  91. if (!isset($aData[$CNF['FIELD_AUTHOR']]) || !isset($aData[$CNF['FIELD_ID']]))
  92. return false;
  93. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  94. if (!$oStorage)
  95. return false;
  96. if (isset($aData[$CNF['FIELD_FILE_ID']]) && $aData[$CNF['FIELD_FILE_ID']])
  97. return $this->_addFileFields($oStorage->getFile($aData[$CNF['FIELD_FILE_ID']]));
  98. $aGhostFiles = $oStorage->getGhosts ($aData[$CNF['FIELD_AUTHOR']], $aData[$CNF['FIELD_ID']]);
  99. if (!$aGhostFiles)
  100. return false;
  101. return $this->_addFileFields(array_pop($aGhostFiles));
  102. }
  103. protected function _addFileFields($aFile)
  104. {
  105. if (is_array($aFile))
  106. $aFile['is_image'] = in_array($aFile['ext'], array('jpg', 'jpeg', 'png', 'gif', /* when ImageMagick is used - 'tif', 'tiff', 'bmp', 'ico', 'psd' */));
  107. return $aFile;
  108. }
  109. public function serviceEntityFilePreview($iContentId = 0)
  110. {
  111. return $this->_serviceTemplateFunc ('entryFilePreview', $iContentId);
  112. }
  113. public function serviceProcessFilesData($iNumberOfFilesToProcessAtOnce = 3)
  114. {
  115. $CNF = $this->_oConfig->CNF;
  116. if (!defined('BX_SYSTEM_JAVA') || !constant('BX_SYSTEM_JAVA'))
  117. return;
  118. $a = $this->_oDb->getNotProcessedFiles($iNumberOfFilesToProcessAtOnce);
  119. if (!$a)
  120. return;
  121. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  122. if (!$oStorage)
  123. return false;
  124. foreach ($a as $aContentInfo) {
  125. $aFile = $this->getContentFile($aContentInfo);
  126. if (!$aFile) {
  127. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  128. continue;
  129. }
  130. $sFileUrl = $oStorage->getFileUrlById($aFile['id']);
  131. $sFilePath = BX_DIRECTORY_PATH_TMP . $aFile['remote_id'] . '.' . $aFile['ext'];
  132. @file_put_contents($sFilePath, file_get_contents($sFileUrl));
  133. if (!file_exists($sFilePath)) {
  134. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  135. continue;
  136. }
  137. $sCommand = '"' . constant('BX_SYSTEM_JAVA') . '" -jar "' . $this->_oConfig->getHomePath() . 'data/tika-app.jar" --encoding=UTF-8 --text "' . $sFilePath . '"';
  138. $sData = `$sCommand`;
  139. @unlink($sFilePath);
  140. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], $sData);
  141. }
  142. }
  143. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  144. {
  145. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  146. $aFile = $this->getContentFile($aContentInfo);
  147. if (!$aFile['is_image']) {
  148. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  149. $sIcon = $oStorage ? $oStorage->getFontIconNameByFileName($aFile['file_name']) : 'far file';
  150. $aResult['raw'] = $this->_oTemplate->parseHtmlByName('timeline_post.html', array(
  151. 'title' => $aResult['title'],
  152. 'title_attr' => bx_html_attribute($aResult['title']),
  153. 'url' => $aResult['url'],
  154. 'icon' => $sIcon,
  155. ));
  156. $aResult['title'] = '';
  157. }
  158. return $aResult;
  159. }
  160. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  161. {
  162. $aFile = $this->getContentFile($aContentInfo);
  163. if (!$aFile)
  164. return array();
  165. if (!$aFile['is_image'])
  166. return array();
  167. $sPhotoThumb = '';
  168. if ($oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($this->_oConfig->CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']))
  169. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aFile['id']);
  170. return array(
  171. array('id' => $aFile['id'], 'url' => $sUrl, 'src' => $sPhotoThumb),
  172. );
  173. }
  174. public function actionBookmark($iContentId) {
  175. if (!$this->isLogged()) return;
  176. $aFilesToBookmark = [];
  177. if ($iContentId) $aFilesToBookmark = [$iContentId];
  178. elseif (isset($_POST['bulk'])) $aFilesToBookmark = $_POST['bulk'];
  179. if ($aFilesToBookmark)
  180. foreach ($aFilesToBookmark as $iContentId) {
  181. if ($iContentId <= 0) continue;
  182. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  183. if (!$aData) continue;
  184. $this->_oDb->bookmarkFile((int)$iContentId, bx_get_logged_profile_id());
  185. }
  186. }
  187. public function actionEntryPreview($iContentId) {
  188. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  189. $sJsCss = '';
  190. $sPreviewCode = $this->_serviceTemplateFunc ('entryFilePreview', $iContentId);
  191. // various init scripts could be there, which fail to work properly
  192. // because of a fact that it is inside a popup which is at the moment is hidden
  193. // so we need a way to postpone that initialization
  194. $aScripts = [];
  195. $sPreviewCode = preg_replace_callback('/<script>(.*)<\/script>/s', function($aMatch) use (&$aScripts) {
  196. $aScripts[] = ['code' => $aMatch[1]];
  197. return '';
  198. }, $sPreviewCode);
  199. // if there is a script then most likely it is a text + codemirror initialization, so include it here.
  200. if ($aScripts) {
  201. $sJsCss .= $this->_oTemplate->addJs('codemirror/codemirror-ext.min.js', true);
  202. $sJsCss .= $this->_oTemplate->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'codemirror/|codemirror.css', true);
  203. }
  204. $sJsCss .= $this->_oTemplate->addCss('file_handler.css', true);
  205. $sPopupCode = $this->_oTemplate->parseHtmlByName('entry-popup.html', [
  206. 'content' => $sPreviewCode,
  207. 'bx_repeat:init_scripts' => $aScripts,
  208. ]);
  209. echoJson([
  210. 'popup' => [
  211. 'html' => PopupBox('bx-files-popup', bx_process_output($aData['title']), $sJsCss.$sPopupCode),
  212. 'options' => [
  213. 'onShow' => 'bx_files_popup_init()',
  214. ],
  215. ],
  216. ]);
  217. }
  218. public function actionEntryInfo($iContentId) {
  219. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  220. echoJson([
  221. 'popup' => [
  222. 'html' => PopupBox('bx-files-popup', bx_process_output($aData['title']), $this->_oTemplate->entryInfoPopup($aData)),
  223. ],
  224. ]);
  225. }
  226. public function actionEntryEditTitle($iContentId) {
  227. $iContentId = intval($iContentId);
  228. $aData = $this->_oDb->getContentInfoById($iContentId);
  229. $CNF = &$this->_oConfig->CNF;
  230. if (!$aData) {
  231. echoJson(['message' => _t('_bx_files_txt_error')]);
  232. exit;
  233. }
  234. if ($aData[$CNF['FIELD_AUTHOR']] != bx_get_logged_profile_id()) {
  235. echoJson(['message' => _t('_bx_files_txt_permission_denied')]);
  236. exit;
  237. }
  238. if (isset($_POST['title'])) {
  239. if ($sNewTitle = bx_get('title')) {
  240. $this->_oDb->updateEntryTitle($iContentId, $sNewTitle);
  241. $aData = $this->_oDb->getContentInfoById($iContentId);
  242. echo bx_process_output($aData[$CNF['FIELD_TITLE']]);
  243. }
  244. exit;
  245. }
  246. $sActionUrl = BX_DOL_URL_ROOT.$this->_oConfig->getBaseUri();
  247. echoJson([
  248. 'eval' => "bx_prompt('"._t('_bx_files_form_entry_input_title')."', '".bx_js_string($aData[$CNF['FIELD_TITLE']])."', function(val) {
  249. $.post('{$sActionUrl}entry_edit_title/{$iContentId}', {title: val.getValue()}, function(sTitle) {
  250. if (sTitle)
  251. $('.bx-file-entry[file_id=".$aData[$CNF['FIELD_ID']]."] .bx-files-filename a').html(sTitle);
  252. });
  253. })",
  254. ]);
  255. }
  256. public function actionEntryDelete($iContentId) {
  257. $aFilesToDelete = [];
  258. if ($iContentId) $aFilesToDelete = [$iContentId];
  259. elseif (isset($_POST['bulk'])) $aFilesToDelete = $_POST['bulk'];
  260. if ($aFilesToDelete)
  261. foreach ($aFilesToDelete as $iContentId) {
  262. $iContentId = intval($iContentId);
  263. if ($iContentId <= 0) continue;
  264. $aData = $this->_oDb->getContentInfoById($iContentId);
  265. $CNF = &$this->_oConfig->CNF;
  266. if (!$aData) {
  267. echoJson(['message' => _t('_bx_files_txt_error')]);
  268. exit;
  269. }
  270. if ($aData[$CNF['FIELD_AUTHOR']] != bx_get_logged_profile_id() && !($aData[$CNF['FIELD_ALLOW_VIEW_TO']] < 0 && $this->serviceIsAllowedAddContentToProfile(-$aData[$CNF['FIELD_ALLOW_VIEW_TO']]))) {
  271. echoJson(['message' => _t('_bx_files_txt_permission_denied')]);
  272. exit;
  273. }
  274. $this->serviceDeleteEntity($iContentId);
  275. }
  276. if (!isset($_POST['bulk'])) {
  277. echoJson([
  278. 'eval' => "
  279. $('.bx-file-entry[file_id={$iContentId}]').fadeOut();
  280. $(window).trigger('files_browser.update');
  281. ",
  282. ]);
  283. } else {
  284. echoJson([]);
  285. }
  286. }
  287. public function actionClearGhosts() {
  288. if (!isLogged()) die;
  289. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  290. if ($oStorage) {
  291. $aFiles = $oStorage->getGhosts(bx_get_logged_profile_id(), 0);
  292. if ($aFiles) foreach ($aFiles as $aFile) {
  293. $oStorage->deleteFile($aFile['id']);
  294. }
  295. }
  296. }
  297. public function actionUploadCompleted() {
  298. $iContext = intval(bx_get('context'));
  299. $iFolder = intval(bx_get('folder'));
  300. if (!isLogged()) {
  301. echoJson(['message' => _t('_Access denied')]);
  302. exit;
  303. }
  304. if ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || !$this->serviceIsAllowedAddContentToProfile($iContext)) {
  305. echoJson(['message' => _t('_Access denied')]);
  306. exit;
  307. }
  308. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  309. if ($oStorage) {
  310. $iAuthorProfile = bx_get_logged_profile_id();
  311. $aFiles = $oStorage->getGhosts($iAuthorProfile, 0);
  312. if ($aFiles) {
  313. $aFilesIDs = [];
  314. foreach($aFiles as $aFile) {
  315. $aFilesIDs[] = $aFile['id'];
  316. bx_set('title-'.$aFile['id'], $aFile['file_name'], 'post');
  317. }
  318. bx_set('attachments', $aFilesIDs, 'post');
  319. $oForm = BxDolForm::getObjectInstance($this->_oConfig->CNF['OBJECT_FORM_ENTRY_UPLOAD'], $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_UPLOAD']);
  320. $oForm->aInputs['profile_id']['value'] = $iAuthorProfile;
  321. $oForm->aInputs['allow_view_to']['value'] = $iContext == $iAuthorProfile ? 3 : -$iContext;
  322. $aFiles = $oForm->insert();
  323. if ($iFolder != 0 && !empty($aFiles))
  324. $this->_oDb->moveFilesToFolder($aFiles, $iFolder);
  325. }
  326. }
  327. }
  328. public function actionCreateFolder() {
  329. $iContext = intval(bx_get('context'));
  330. $iFolder = intval(bx_get('current_folder'));
  331. $sName = trim(bx_get('name'));
  332. if (!isLogged()) {
  333. echoJson(['message' => _t('_Access denied')]);
  334. exit;
  335. }
  336. if ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || !$this->serviceIsAllowedAddContentToProfile($iContext)) {
  337. echoJson(['message' => _t('_Access denied')]);
  338. exit;
  339. }
  340. if (!empty($sName)) {
  341. $iAuthor = bx_get_logged_profile_id();
  342. //in case it is posted to context make the context profile as an author of an entry
  343. $this->_oDb->createFolder($iFolder, $iAuthor == $iContext ? $iAuthor : $iContext, $iAuthor == $iContext ? BX_DOL_PG_ALL : -$iContext, $sName);
  344. }
  345. echoJson([]);
  346. exit;
  347. }
  348. public function actionMoveFiles() {
  349. $iContentId = intval(bx_get('file'));
  350. $aFilesToMove = [];
  351. if ($iContentId) $aFilesToMove = [$iContentId];
  352. elseif (isset($_POST['bulk'])) $aFilesToMove = $_POST['bulk'];
  353. $iContentId = reset($aFilesToMove);
  354. $aData = $this->_oDb->getContentInfoById($iContentId);
  355. if (!$aData) {
  356. echoJson(['message' => _t('_bx_files_txt_error')]);
  357. exit;
  358. }
  359. $CNF = &$this->_oConfig->CNF;
  360. $iContext = $aData[$CNF['FIELD_ALLOW_VIEW_TO']] < 0 ? -$aData[$CNF['FIELD_ALLOW_VIEW_TO']] : $aData[$CNF['FIELD_AUTHOR']];
  361. if ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || !$this->serviceIsAllowedAddContentToProfile($iContext)) {
  362. echoJson(['message' => _t('_Access denied')]);
  363. exit;
  364. }
  365. if (isset($_POST['move_to'])) {
  366. $iMoveTo = intval(bx_get('move_to'));
  367. if ($iMoveTo) {
  368. $aDataMoveTo = $this->_oDb->getContentInfoById($iMoveTo);
  369. if (!$aDataMoveTo) {
  370. echoJson(['message' => _t('_bx_files_txt_error')]);
  371. exit;
  372. }
  373. $iContextMoveTo = $aDataMoveTo[$CNF['FIELD_ALLOW_VIEW_TO']] < 0 ? -$aDataMoveTo[$CNF['FIELD_ALLOW_VIEW_TO']] : $aDataMoveTo[$CNF['FIELD_AUTHOR']];
  374. if ($this->checkAllowedAdd() != CHECK_ACTION_RESULT_ALLOWED || !$this->serviceIsAllowedAddContentToProfile($iContextMoveTo)) {
  375. echoJson(['message' => _t('_Access denied')]);
  376. exit;
  377. }
  378. }
  379. $this->_oDb->moveFilesToFolder($aFilesToMove, $iMoveTo);
  380. echoJson([
  381. 'eval' => "
  382. $(window).trigger('files_browser.update');
  383. ",
  384. ]);
  385. exit;
  386. }
  387. $aFolders = $this->_oDb->getFoldersInContext($iContext);
  388. if (!$aFolders) {
  389. echoJson(['message' => _t('_bx_files_txt_move_to_nowhere')]);
  390. exit;
  391. }
  392. echoJson([
  393. 'popup' => [
  394. 'html' => PopupBox('bx-files-popup', _t('_bx_files_txt_move_to'), $this->_oTemplate->getJsTree($aFilesToMove, $aFolders)),
  395. 'options' => [],
  396. ],
  397. ]);
  398. }
  399. public function serviceDeleteEntitiesByAuthor ($iProfileId) {
  400. $this->_oDb->deleteProfileBookmarks($iProfileId);
  401. return parent::serviceDeleteEntitiesByAuthor ($iProfileId);
  402. }
  403. public function serviceDeleteEntity ($iContentId, $sFuncDelete = 'deleteData') {
  404. $aData = $this->_oDb->getContentInfoById($iContentId);
  405. if ($aData['type'] == 'folder') {
  406. $aNestedFiles = $this->_oDb->getFolderFiles($aData[$this->_oConfig->CNF['FIELD_ID']]);
  407. if ($aNestedFiles)
  408. foreach ($aNestedFiles as $iFile) {
  409. $this->serviceDeleteEntity($iFile);
  410. }
  411. }
  412. parent::serviceDeleteEntity($iContentId, $sFuncDelete);
  413. }
  414. private function prepareFolderForDownloading($aFiles) {
  415. $CNF = &$this->_oConfig->CNF;
  416. $aFilesList = [];
  417. foreach ($aFiles as $iFile) {
  418. $aData = $this->_oDb->getContentInfoById(intval($iFile));
  419. $aFilesList = array_merge($aFilesList, $this->getFolderFiles(intval($iFile), $aData['type'] == 'folder' ? $this->sanitizeFileName($aData['title']).'/' : '/', $aData['type']));
  420. }
  421. if(!$aFilesList) return false;
  422. $sZipFile = BX_DIRECTORY_PATH_TMP.'files-folder-'.mt_rand().'.zip';
  423. if (file_exists($sZipFile)) @unlink($sZipFile);
  424. $oZipFile = new ZipArchive();
  425. if ($oZipFile->open($sZipFile, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {
  426. $oZipFile->setCompressionIndex(0, ZipArchive::CM_STORE);
  427. foreach ($aFilesList as $aFile) {
  428. $sFilePath = BX_DIRECTORY_STORAGE . $CNF['OBJECT_STORAGE'].'/'.$aFile['path'];
  429. if (file_exists($sFilePath) && is_readable($sFilePath))
  430. $oZipFile->addFile($sFilePath, $aFile['filename']);
  431. }
  432. $oZipFile->close();
  433. return $sZipFile;
  434. } else {
  435. return false;
  436. }
  437. }
  438. private function getFolderFiles($iFile, $sRelPath, $sType) {
  439. $aFiles = $this->_oDb->getFolderFilesEx($iFile, $sType);
  440. if (!$aFiles) return [];
  441. $aResult = [];
  442. $aUniqueNames = [];
  443. foreach ($aFiles as $aFile) {
  444. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aFile)) continue;
  445. //in case title has been changed we might have to add extensions manually
  446. $aFileInfo = pathinfo($aFile['title']);
  447. $sFileBaseName = isset($aFileInfo['filename']) ? $aFileInfo['filename'] : '_';
  448. $sFileExt = isset($aFileInfo['extension']) ? $aFileInfo['extension'] : '';
  449. if (!$sFileExt || $sFileExt != $aFile['ext']) $sFileExt = $aFile['ext'];
  450. //handle multiple files with the same name
  451. $sFilename = $this->sanitizeFileName($sFileBaseName);
  452. $iIndex = 1;
  453. while (isset($aUniqueNames[$sFilename])) {
  454. $sFilename = $this->sanitizeFileName($sFileBaseName.'_'.$iIndex++);
  455. }
  456. $aUniqueNames[$sFilename] = 1;
  457. if ($aFile['type'] == 'folder') {
  458. $aResult = array_merge($aResult, $this->getFolderFiles($aFile['id'], $sRelPath . $sFilename . '/', 'folder'));
  459. } else {
  460. $aResult[] = [
  461. 'filename' => trim($sRelPath.$sFilename.'.'.$sFileExt, '/'),
  462. 'size' => $aFile['size'],
  463. 'path' => $aFile['path'],
  464. ];
  465. }
  466. }
  467. return $aResult;
  468. }
  469. private function sanitizeFileName($sDesiredFilename) {
  470. $sDesiredFilename = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '_', $sDesiredFilename);
  471. $sDesiredFilename = mb_ereg_replace("([\.]{2,})", '_', $sDesiredFilename);
  472. return $sDesiredFilename;
  473. }
  474. }
  475. /** @} */