BxFilesModule.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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)
  46. {
  47. $CNF = $this->_oConfig->CNF;
  48. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  49. if (!$aData) {
  50. $this->_oTemplate->displayPageNotFound();
  51. return;
  52. }
  53. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aData)) {
  54. $this->_oTemplate->displayAccessDenied();
  55. return;
  56. }
  57. $aFile = $this->getContentFile($aData);
  58. if (!$aFile) {
  59. $this->_oTemplate->displayPageNotFound();
  60. return;
  61. }
  62. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  63. if (!$oStorage) {
  64. $this->_oTemplate->displayErrorOccured();
  65. return;
  66. }
  67. if (!$oStorage->download($aFile['remote_id'], $sToken)) {
  68. $this->_oTemplate->displayErrorOccured();
  69. return;
  70. }
  71. exit;
  72. }
  73. public function getContentFile($aData)
  74. {
  75. $CNF = $this->_oConfig->CNF;
  76. if (!isset($aData[$CNF['FIELD_AUTHOR']]) || !isset($aData[$CNF['FIELD_ID']]))
  77. return false;
  78. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  79. if (!$oStorage)
  80. return false;
  81. if (isset($aData[$CNF['FIELD_FILE_ID']]) && $aData[$CNF['FIELD_FILE_ID']])
  82. return $this->_addFileFields($oStorage->getFile($aData[$CNF['FIELD_FILE_ID']]));
  83. $aGhostFiles = $oStorage->getGhosts ($aData[$CNF['FIELD_AUTHOR']], $aData[$CNF['FIELD_ID']]);
  84. if (!$aGhostFiles)
  85. return false;
  86. return $this->_addFileFields(array_pop($aGhostFiles));
  87. }
  88. protected function _addFileFields($aFile)
  89. {
  90. if (is_array($aFile))
  91. $aFile['is_image'] = in_array($aFile['ext'], array('jpg', 'jpeg', 'png', 'gif', /* when ImageMagick is used - 'tif', 'tiff', 'bmp', 'ico', 'psd' */));
  92. return $aFile;
  93. }
  94. public function serviceEntityFilePreview($iContentId = 0)
  95. {
  96. return $this->_serviceTemplateFunc ('entryFilePreview', $iContentId);
  97. }
  98. public function serviceProcessFilesData($iNumberOfFilesToProcessAtOnce = 3)
  99. {
  100. $CNF = $this->_oConfig->CNF;
  101. if (!defined('BX_SYSTEM_JAVA') || !constant('BX_SYSTEM_JAVA'))
  102. return;
  103. $a = $this->_oDb->getNotProcessedFiles($iNumberOfFilesToProcessAtOnce);
  104. if (!$a)
  105. return;
  106. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  107. if (!$oStorage)
  108. return false;
  109. foreach ($a as $aContentInfo) {
  110. $aFile = $this->getContentFile($aContentInfo);
  111. if (!$aFile) {
  112. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  113. continue;
  114. }
  115. $sFileUrl = $oStorage->getFileUrlById($aFile['id']);
  116. $sFilePath = BX_DIRECTORY_PATH_TMP . $aFile['remote_id'] . '.' . $aFile['ext'];
  117. @file_put_contents($sFilePath, file_get_contents($sFileUrl));
  118. if (!file_exists($sFilePath)) {
  119. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  120. continue;
  121. }
  122. $sCommand = '"' . constant('BX_SYSTEM_JAVA') . '" -jar "' . $this->_oConfig->getHomePath() . 'data/tika-app.jar" --encoding=UTF-8 --text "' . $sFilePath . '"';
  123. $sData = `$sCommand`;
  124. @unlink($sFilePath);
  125. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], $sData);
  126. }
  127. }
  128. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  129. {
  130. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  131. $aFile = $this->getContentFile($aContentInfo);
  132. if (!$aFile['is_image']) {
  133. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  134. $sIcon = $oStorage ? $oStorage->getFontIconNameByFileName($aFile['file_name']) : 'far file';
  135. $aResult['raw'] = $this->_oTemplate->parseHtmlByName('timeline_post.html', array(
  136. 'title' => $aResult['title'],
  137. 'title_attr' => bx_html_attribute($aResult['title']),
  138. 'url' => $aResult['url'],
  139. 'icon' => $sIcon,
  140. ));
  141. $aResult['title'] = '';
  142. }
  143. return $aResult;
  144. }
  145. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  146. {
  147. $aFile = $this->getContentFile($aContentInfo);
  148. if (!$aFile)
  149. return array();
  150. if (!$aFile['is_image'])
  151. return array();
  152. $sPhotoThumb = '';
  153. if ($oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($this->_oConfig->CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']))
  154. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aFile['id']);
  155. return array(
  156. array('id' => $aFile['id'], 'url' => $sUrl, 'src' => $sPhotoThumb),
  157. );
  158. }
  159. public function actionBookmark($iContentId) {
  160. if (!$this->isLogged()) return;
  161. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  162. if (!$aData) return;
  163. $this->_oDb->bookmarkFile((int)$iContentId, bx_get_logged_profile_id());
  164. }
  165. public function actionEntryPreview($iContentId) {
  166. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  167. $sJsCss = '';
  168. $sPreviewCode = $this->_serviceTemplateFunc ('entryFilePreview', $iContentId);
  169. // various init scripts could be there, which fail to work properly
  170. // because of a fact that it is inside a popup which is at the moment is hidden
  171. // so we need a way to postpone that initialization
  172. $aScripts = [];
  173. $sPreviewCode = preg_replace_callback('/<script>(.*)<\/script>/s', function($aMatch) use (&$aScripts) {
  174. $aScripts[] = ['code' => $aMatch[1]];
  175. return '';
  176. }, $sPreviewCode);
  177. // if there is a script then most likely it is a text + codemirror initialization, so include it here.
  178. if ($aScripts) {
  179. $sJsCss .= $this->_oTemplate->addJs('codemirror/codemirror-ext.min.js', true);
  180. $sJsCss .= $this->_oTemplate->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'codemirror/|codemirror.css', true);
  181. }
  182. $sJsCss .= $this->_oTemplate->addCss('file_handler.css', true);
  183. $sPopupCode = $this->_oTemplate->parseHtmlByName('entry-popup.html', [
  184. 'content' => $sPreviewCode,
  185. 'bx_repeat:init_scripts' => $aScripts,
  186. ]);
  187. echoJson([
  188. 'popup' => [
  189. 'html' => PopupBox('bx-files-popup', bx_process_output($aData['title']), $sJsCss.$sPopupCode),
  190. 'options' => [
  191. 'onShow' => 'bx_files_popup_init()',
  192. ],
  193. ],
  194. ]);
  195. }
  196. public function actionEntryInfo($iContentId) {
  197. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  198. echoJson([
  199. 'popup' => [
  200. 'html' => PopupBox('bx-files-popup', bx_process_output($aData['title']), $this->_oTemplate->entryInfoPopup($aData)),
  201. ],
  202. ]);
  203. }
  204. public function actionEntryEditTitle($iContentId) {
  205. $iContentId = intval($iContentId);
  206. $aData = $this->_oDb->getContentInfoById($iContentId);
  207. $CNF = &$this->_oConfig->CNF;
  208. if (!$aData) {
  209. echoJson(['message' => _t('_bx_files_txt_error')]);
  210. exit;
  211. }
  212. if ($aData[$CNF['FIELD_AUTHOR']] != bx_get_logged_profile_id()) {
  213. echoJson(['message' => _t('_bx_files_txt_permission_denied')]);
  214. exit;
  215. }
  216. if (isset($_POST['title'])) {
  217. if ($sNewTitle = bx_get('title')) {
  218. $this->_oDb->updateEntryTitle($iContentId, $sNewTitle);
  219. $aData = $this->_oDb->getContentInfoById($iContentId);
  220. echo bx_process_output($aData[$CNF['FIELD_TITLE']]);
  221. }
  222. exit;
  223. }
  224. $sActionUrl = BX_DOL_URL_ROOT.$this->_oConfig->getBaseUri();
  225. echoJson([
  226. 'eval' => "bx_prompt('"._t('_bx_files_form_entry_input_title')."', '".bx_js_string($aData[$CNF['FIELD_TITLE']])."', function(val) {
  227. $.post('{$sActionUrl}entry_edit_title/{$iContentId}', {title: val.getValue()}, function(sTitle) {
  228. if (sTitle)
  229. $('.bx-file-entry[file_id=".$aData[$CNF['FIELD_ID']]."] .bx-files-filename a').html(sTitle);
  230. });
  231. })",
  232. ]);
  233. }
  234. public function actionEntryDelete($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. $this->serviceDeleteEntity($iContentId);
  247. echoJson([
  248. 'eval' => "
  249. $('.bx-file-entry[file_id={$iContentId}]').fadeOut();
  250. ",
  251. ]);
  252. }
  253. public function serviceDeleteEntitiesByAuthor ($iProfileId) {
  254. $this->_oDb->deleteProfileBookmarks($iProfileId);
  255. return parent::serviceDeleteEntitiesByAuthor ($iProfileId);
  256. }
  257. }
  258. /** @} */