BxFilesModule.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. /**
  21. * Display entries of the author
  22. * @return HTML string
  23. */
  24. public function serviceBrowseGroupAuthor ($iProfileId = 0, $aParams = array())
  25. {
  26. if (!$iProfileId)
  27. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  28. if (!$iProfileId)
  29. return '';
  30. if (!($oGroupProfile = BxDolProfile::getInstance($iProfileId)))
  31. return '';
  32. if (!BxDolService::call($oGroupProfile->getModule(), 'is_group_profile'))
  33. return '';
  34. $bDisplayEmptyMsg = true;
  35. if(isset($aParams['empty_message'])) {
  36. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  37. unset($aParams['empty_message']);
  38. }
  39. return $this->_serviceBrowse ('group_author', array_merge(array('author' => $iProfileId), $aParams), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  40. }
  41. public function serviceIsAllowedAddContentToProfile($iGroupProfileId)
  42. {
  43. if (!$iGroupProfileId || !($oProfile = BxDolProfile::getInstance((int)$iGroupProfileId)))
  44. return false;
  45. if ($iGroupProfileId == bx_get_logged_profile_id())
  46. return true;
  47. $sProfileModule = $oProfile->getModule();
  48. $aContentInfo = BxDolService::call($sProfileModule, 'get_content_info_by_id', array($oProfile->getContentId()));
  49. if (BxDolService::call($sProfileModule, 'is_group_profile') && (BxDolService::call($sProfileModule, 'is_fan', array($iGroupProfileId)) || bx_get_logged_profile_id() == $aContentInfo['author']))
  50. return true;
  51. return false;
  52. }
  53. public function serviceMyEntriesActions ($iProfileId = 0)
  54. {
  55. if (!$iProfileId)
  56. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  57. if (!$this->serviceIsAllowedAddContentToProfile($iProfileId))
  58. return false;
  59. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_MY_ENTRIES']);
  60. return $oMenu ? $oMenu->getCode() : false;
  61. }
  62. public function checkAllowedSetThumb ($iContentId = 0)
  63. {
  64. return _t('_sys_txt_access_denied');
  65. }
  66. public function actionDownload($sToken, $iContentId)
  67. {
  68. $CNF = $this->_oConfig->CNF;
  69. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  70. if (!$aData) {
  71. $this->_oTemplate->displayPageNotFound();
  72. return;
  73. }
  74. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aData)) {
  75. $this->_oTemplate->displayAccessDenied();
  76. return;
  77. }
  78. $aFile = $this->getContentFile($aData);
  79. if (!$aFile) {
  80. $this->_oTemplate->displayPageNotFound();
  81. return;
  82. }
  83. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  84. if (!$oStorage) {
  85. $this->_oTemplate->displayErrorOccured();
  86. return;
  87. }
  88. if (!$oStorage->download($aFile['remote_id'], $sToken)) {
  89. $this->_oTemplate->displayErrorOccured();
  90. return;
  91. }
  92. exit;
  93. }
  94. public function getContentFile($aData)
  95. {
  96. $CNF = $this->_oConfig->CNF;
  97. if (!isset($aData[$CNF['FIELD_AUTHOR']]) || !isset($aData[$CNF['FIELD_ID']]))
  98. return false;
  99. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  100. if (!$oStorage)
  101. return false;
  102. if (isset($aData[$CNF['FIELD_FILE_ID']]) && $aData[$CNF['FIELD_FILE_ID']])
  103. return $this->_addFileFields($oStorage->getFile($aData[$CNF['FIELD_FILE_ID']]));
  104. $aGhostFiles = $oStorage->getGhosts ($aData[$CNF['FIELD_AUTHOR']], $aData[$CNF['FIELD_ID']]);
  105. if (!$aGhostFiles)
  106. return false;
  107. return $this->_addFileFields(array_pop($aGhostFiles));
  108. }
  109. protected function _addFileFields($aFile)
  110. {
  111. if (is_array($aFile))
  112. $aFile['is_image'] = in_array($aFile['ext'], array('jpg', 'jpeg', 'png', 'gif', /* when ImageMagick is used - 'tif', 'tiff', 'bmp', 'ico', 'psd' */));
  113. return $aFile;
  114. }
  115. public function serviceEntityFilePreview($iContentId = 0)
  116. {
  117. return $this->_serviceTemplateFunc ('entryFilePreview', $iContentId);
  118. }
  119. public function serviceProcessFilesData($iNumberOfFilesToProcessAtOnce = 3)
  120. {
  121. $CNF = $this->_oConfig->CNF;
  122. if (!defined('BX_SYSTEM_JAVA') || !constant('BX_SYSTEM_JAVA'))
  123. return;
  124. $a = $this->_oDb->getNotProcessedFiles($iNumberOfFilesToProcessAtOnce);
  125. if (!$a)
  126. return;
  127. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  128. if (!$oStorage)
  129. return false;
  130. foreach ($a as $aContentInfo) {
  131. $aFile = $this->getContentFile($aContentInfo);
  132. if (!$aFile) {
  133. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  134. continue;
  135. }
  136. $sFileUrl = $oStorage->getFileUrlById($aFile['id']);
  137. $sFilePath = BX_DIRECTORY_PATH_TMP . $aFile['remote_id'] . '.' . $aFile['ext'];
  138. @file_put_contents($sFilePath, file_get_contents($sFileUrl));
  139. if (!file_exists($sFilePath)) {
  140. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  141. continue;
  142. }
  143. $sCommand = '"' . constant('BX_SYSTEM_JAVA') . '" -jar "' . $this->_oConfig->getHomePath() . 'data/tika-app.jar" --encoding=UTF-8 --text "' . $sFilePath . '"';
  144. $sData = `$sCommand`;
  145. @unlink($sFilePath);
  146. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], $sData);
  147. }
  148. }
  149. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  150. {
  151. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  152. $aFile = $this->getContentFile($aContentInfo);
  153. if (!$aFile['is_image']) {
  154. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  155. $sIcon = $oStorage ? $oStorage->getFontIconNameByFileName($aFile['file_name']) : 'file-o';
  156. $aResult['raw'] = $this->_oTemplate->parseHtmlByName('timeline_post.html', array(
  157. 'title' => $aResult['title'],
  158. 'title_attr' => bx_html_attribute($aResult['title']),
  159. 'url' => $aResult['url'],
  160. 'icon' => $sIcon,
  161. ));
  162. $aResult['title'] = '';
  163. }
  164. return $aResult;
  165. }
  166. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  167. {
  168. $aFile = $this->getContentFile($aContentInfo);
  169. if (!$aFile)
  170. return array();
  171. if (!$aFile['is_image'])
  172. return array();
  173. $sPhotoThumb = '';
  174. if ($oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($this->_oConfig->CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']))
  175. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aFile['id']);
  176. return array(
  177. array('url' => $sUrl, 'src' => $sPhotoThumb),
  178. );
  179. }
  180. }
  181. /** @} */