BxFilesModule.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. }
  160. /** @} */