BxFilesModule.php 5.6 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. if (BxDolService::call($sProfileModule, 'is_group_profile') && BxDolService::call($sProfileModule, 'is_fan', array($iGroupProfileId)))
  44. return true;
  45. return false;
  46. }
  47. public function serviceMyEntriesActions ($iProfileId = 0)
  48. {
  49. if (!$iProfileId)
  50. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  51. if (!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  52. return false;
  53. $sProfileModule = $oProfile->getModule();
  54. if ($iProfileId != $this->_iProfileId && !(BxDolService::call($sProfileModule, 'is_group_profile') && BxDolService::call($sProfileModule, 'is_fan', array($iProfileId))))
  55. return false;
  56. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_MY_ENTRIES']);
  57. return $oMenu ? $oMenu->getCode() : false;
  58. }
  59. public function checkAllowedSetThumb ()
  60. {
  61. return _t('_sys_txt_access_denied');
  62. }
  63. public function actionDownload($iContentId, $sToken = '')
  64. {
  65. $CNF = $this->_oConfig->CNF;
  66. $aData = $this->_oDb->getContentInfoById((int)$iContentId);
  67. if (!$aData) {
  68. $this->_oTemplate->displayPageNotFound();
  69. return;
  70. }
  71. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aData)) {
  72. $this->_oTemplate->displayAccessDenied();
  73. return;
  74. }
  75. $aFile = $this->getContentFile($aData);
  76. if (!$aFile) {
  77. $this->_oTemplate->displayPageNotFound();
  78. return;
  79. }
  80. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  81. if (!$oStorage) {
  82. $this->_oTemplate->displayErrorOccured();
  83. return;
  84. }
  85. if (!$oStorage->download($aFile['remote_id'], $sToken)) {
  86. $this->_oTemplate->displayErrorOccured();
  87. return;
  88. }
  89. exit;
  90. }
  91. public function getContentFile($aData)
  92. {
  93. $CNF = $this->_oConfig->CNF;
  94. if (!isset($aData[$CNF['FIELD_AUTHOR']]) || !isset($aData[$CNF['FIELD_ID']]))
  95. return false;
  96. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  97. if (!$oStorage)
  98. return false;
  99. if (isset($aData[$CNF['FIELD_FILE_ID']]) && $aData[$CNF['FIELD_FILE_ID']])
  100. return $oStorage->getFile($aData[$CNF['FIELD_FILE_ID']]);
  101. $aGhostFiles = $oStorage->getGhosts ($aData[$CNF['FIELD_AUTHOR']], $aData[$CNF['FIELD_ID']]);
  102. if (!$aGhostFiles)
  103. return false;
  104. return array_pop($aGhostFiles);
  105. }
  106. public function serviceEntityFilePreview($iContentId = 0)
  107. {
  108. return $this->_serviceTemplateFunc ('entryFilePreview', $iContentId);
  109. }
  110. public function serviceProcessFilesData($iNumberOfFilesToProcessAtOnce = 3)
  111. {
  112. $CNF = $this->_oConfig->CNF;
  113. if (!defined('BX_SYSTEM_JAVA') || !constant('BX_SYSTEM_JAVA'))
  114. return;
  115. $a = $this->_oDb->getNotProcessedFiles($iNumberOfFilesToProcessAtOnce);
  116. if (!$a)
  117. return;
  118. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  119. if (!$oStorage)
  120. return false;
  121. foreach ($a as $aContentInfo) {
  122. $aFile = $this->getContentFile($aContentInfo);
  123. if (!$aFile) {
  124. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  125. continue;
  126. }
  127. $sFileUrl = $oStorage->getFileUrlById($aFile['id']);
  128. $sFilePath = BX_DIRECTORY_PATH_TMP . $aFile['remote_id'] . '.' . $aFile['ext'];
  129. @file_put_contents($sFilePath, file_get_contents($sFileUrl));
  130. if (!file_exists($sFilePath)) {
  131. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], '');
  132. continue;
  133. }
  134. $sCommand = '"' . constant('BX_SYSTEM_JAVA') . '" -jar "' . $this->_oConfig->getHomePath() . 'data/tika-app.jar" --encoding=UTF-8 --text "' . $sFilePath . '"';
  135. $sData = `$sCommand`;
  136. @unlink($sFilePath);
  137. $this->_oDb->updateFileData ($aContentInfo[$CNF['FIELD_ID']], $sData);
  138. }
  139. }
  140. }
  141. /** @} */