BxFilesModule.php 7.2 KB

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