BxFilesModule.php 22 KB

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