BxFilesModule.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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($iContentId)
  62. {
  63. $sToken = bx_get('token');
  64. $CNF = $this->_oConfig->CNF;
  65. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  66. if (!$aData) {
  67. $this->_oTemplate->displayPageNotFound();
  68. return;
  69. }
  70. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aData)) {
  71. $this->_oTemplate->displayAccessDenied();
  72. return;
  73. }
  74. $aFile = $this->getContentFile($aData);
  75. if (!$aFile) {
  76. $this->_oTemplate->displayPageNotFound();
  77. return;
  78. }
  79. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  80. if (!$oStorage) {
  81. $this->_oTemplate->displayErrorOccured();
  82. return;
  83. }
  84. if (!$oStorage->download($aFile['remote_id'], $sToken)) {
  85. $this->_oTemplate->displayErrorOccured();
  86. return;
  87. }
  88. exit;
  89. }
  90. public function getContentFile($aData)
  91. {
  92. $CNF = $this->_oConfig->CNF;
  93. if (!isset($aData[$CNF['FIELD_AUTHOR']]) || !isset($aData[$CNF['FIELD_ID']]))
  94. return false;
  95. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  96. if (!$oStorage)
  97. return false;
  98. if (isset($aData[$CNF['FIELD_FILE_ID']]) && $aData[$CNF['FIELD_FILE_ID']])
  99. return $oStorage->getFile($aData[$CNF['FIELD_FILE_ID']]);
  100. $aGhostFiles = $oStorage->getGhosts ($aData[$CNF['FIELD_AUTHOR']], $aData[$CNF['FIELD_ID']]);
  101. if (!$aGhostFiles)
  102. return false;
  103. return array_pop($aGhostFiles);
  104. }
  105. public function serviceEntityFilePreview($iContentId = 0)
  106. {
  107. return $this->_serviceTemplateFunc ('entryFilePreview', $iContentId);
  108. }
  109. public function serviceProcessFilesData($iNumberOfFilesToProcessAtOnce = 3)
  110. {
  111. $CNF = $this->_oConfig->CNF;
  112. if (!defined('BX_SYSTEM_JAVA') || !constant('BX_SYSTEM_JAVA'))
  113. return;
  114. $a = $this->_oDb->getNotProcessedFiles($iNumberOfFilesToProcessAtOnce);
  115. if (!$a)
  116. return;
  117. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  118. if (!$oStorage)
  119. return false;
  120. foreach ($a as $aContentInfo) {
  121. $aFile = $this->getContentFile($aContentInfo);
  122. if (!$aFile) {
  123. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  124. continue;
  125. }
  126. $sFileUrl = $oStorage->getFileUrlById($aFile['id']);
  127. $sFilePath = BX_DIRECTORY_PATH_TMP . $aFile['remote_id'] . '.' . $aFile['ext'];
  128. @file_put_contents($sFilePath, file_get_contents($sFileUrl));
  129. if (!file_exists($sFilePath)) {
  130. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  131. continue;
  132. }
  133. $sCommand = '"' . constant('BX_SYSTEM_JAVA') . '" -jar "' . $this->_oConfig->getHomePath() . 'data/tika-app.jar" --encoding=UTF-8 --text "' . $sFilePath . '"';
  134. $sData = `$sCommand`;
  135. @unlink($sFilePath);
  136. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], $sData);
  137. }
  138. }
  139. }
  140. /** @} */