BxStrmModule.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 Stream Stream
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Stream module
  13. */
  14. class BxStrmModule extends BxBaseModTextModule
  15. {
  16. protected $_oEngine;
  17. function __construct(&$aModule)
  18. {
  19. parent::__construct($aModule);
  20. $CNF = &$this->_oConfig->CNF;
  21. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  22. $CNF['FIELD_PUBLISHED'],
  23. $CNF['FIELD_ANONYMOUS'],
  24. $CNF['FIELD_DISABLE_COMMENTS']
  25. ));
  26. }
  27. public function getStreamEngine ()
  28. {
  29. if ($this->_oEngine)
  30. return $this->_oEngine;
  31. $sEngine = getParam('bx_stream_engine');
  32. bx_import('Engine' . $sEngine, $this->_aModule);
  33. $sClass = 'BxStrmEngine' . $sEngine;
  34. $this->_oEngine = new $sClass;
  35. return $this->_oEngine;
  36. }
  37. public function actionStreamViewers ($iContentId = 0)
  38. {
  39. header('Content-Type:text/javascript; charset=utf-8');
  40. $CNF = &$this->_oConfig->CNF;
  41. $mixedContent = $this->_getContent($iContentId, 'getContentInfoById');
  42. if ($mixedContent === false) {
  43. echo json_encode(['viewers' => _t('_sys_txt_error_occured')]);
  44. exit;
  45. }
  46. list($iContentId, $aContentInfo) = $mixedContent;
  47. $iNum = $this->getStreamEngine()->getViewersNum($aContentInfo[$CNF['FIELD_KEY']]);
  48. if (false === $iNum)
  49. $this->onStreamStopped($iContentId, $aContentInfo);
  50. else
  51. $this->onStreamStarted($iContentId, $aContentInfo);
  52. echo json_encode(['viewers' => $iNum !== false ? _t('_bx_stream_txt_viewers', (int)$iNum) : _t('_bx_stream_txt_wait_for_stream'), 'num' => $iNum]);
  53. }
  54. public function serviceStreamViewers ($iContentId = 0)
  55. {
  56. return $this->_serviceTemplateFunc ('entryStreamViewers', $iContentId);
  57. }
  58. public function serviceStreamPlayer ($iContentId = 0)
  59. {
  60. return $this->_serviceTemplateFunc ('entryStreamPlayer', $iContentId);
  61. }
  62. public function serviceStreamRtmpSettings ($iContentId = 0)
  63. {
  64. $CNF = &$this->_oConfig->CNF;
  65. $mixedContent = $this->_getContent($iContentId, 'getContentInfoById');
  66. if ($mixedContent === false)
  67. return false;
  68. list($iContentId, $aContentInfo) = $mixedContent;
  69. if ($aContentInfo[$CNF['FIELD_AUTHOR']] !== bx_get_logged_profile_id() && !isAdmin())
  70. return false;
  71. $a = $this->getStreamEngine()->getRtmpSettings($aContentInfo[$CNF['FIELD_KEY']]);
  72. if (!$a)
  73. return false;
  74. $aForm = array(
  75. 'form_attrs' => array(
  76. 'name' => 'bx-stream-stmp-settings',
  77. ),
  78. 'inputs' => array(
  79. 'url' => array(
  80. 'type' => 'text',
  81. 'name' => 'url',
  82. 'caption' => _t('_bx_stream_form_entry_input_server'),
  83. 'value' => $a['server'],
  84. ),
  85. 'key' => array(
  86. 'type' => 'password',
  87. 'name' => 'key',
  88. 'caption' => _t('_bx_stream_form_entry_input_stream_key'),
  89. 'value' => $a['key'],
  90. ),
  91. ),
  92. );
  93. $oForm = new BxTemplFormView ($aForm);
  94. return $oForm->getCode();
  95. }
  96. /**
  97. * Entry post for Timeline module
  98. */
  99. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  100. {
  101. $CNF = &$this->_oConfig->CNF;
  102. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  103. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  104. return $aResult;
  105. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  106. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  107. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  108. return $aResult;
  109. }
  110. public function serviceCheckAllowedCommentsPost($iContentId, $sObjectComments)
  111. {
  112. $CNF = &$this->_oConfig->CNF;
  113. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  114. if ($aContentInfo && $aContentInfo[$CNF['FIELD_DISABLE_COMMENTS']] == 1)
  115. return false;
  116. return parent::serviceCheckAllowedCommentsPost($iContentId, $sObjectComments);
  117. }
  118. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  119. {
  120. $CNF = &$this->_oConfig->CNF;
  121. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  122. if ($aContentInfo && $aContentInfo[$CNF['FIELD_DISABLE_COMMENTS']] == 1)
  123. return false;
  124. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  125. }
  126. public function checkAllowedSetThumb ($iContentId = 0)
  127. {
  128. return CHECK_ACTION_RESULT_ALLOWED;
  129. }
  130. public function serviceGetBadges($iContentId, $bIsSingle = false, $bIsCompact = false)
  131. {
  132. $CNF = &$this->_oConfig->CNF;
  133. $s = parent::serviceGetBadges($iContentId, $bIsSingle, $bIsCompact);
  134. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  135. if (!$aContentInfo)
  136. return $s;
  137. return $s . $this->_oTemplate->getLiveBadge($aContentInfo);
  138. }
  139. public function onStreamStarted($iContentId, $aContentInfo = array())
  140. {
  141. $CNF = &$this->_oConfig->CNF;
  142. if (!$aContentInfo)
  143. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  144. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'awaiting')
  145. return;
  146. if(!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'active'), array($CNF['FIELD_ID'] => $iContentId)))
  147. return;
  148. $this->onPublished($iContentId);
  149. bx_alert($this->getName(), 'publish_succeeded', $aContentInfo[$CNF['FIELD_ID']], $aContentInfo[$CNF['FIELD_AUTHOR']], array(
  150. 'object_author_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  151. 'privacy_view' => BX_DOL_PG_ALL,
  152. ));
  153. }
  154. public function onStreamStopped($iContentId, $aContentInfo = array())
  155. {
  156. $CNF = &$this->_oConfig->CNF;
  157. if (!$aContentInfo)
  158. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  159. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'active')
  160. return;
  161. if (!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'awaiting'), array($CNF['FIELD_ID'] => $iContentId)))
  162. return;
  163. if (BxDolRequest::serviceExists('bx_timeline', 'get_all')) {
  164. $a = BxDolService::call('bx_timeline', 'get_all', array(array(
  165. 'type' => 'conditions',
  166. 'conditions' => array(
  167. 'type' => 'bx_stream',
  168. 'action' => 'added',
  169. 'object_id' => $iContentId
  170. )
  171. )));
  172. if ($a) {
  173. $oTimeline = BxDolModule::getInstance('bx_timeline');
  174. if ($oTimeline) {
  175. foreach ($a as $r) {
  176. // $oTimeline->deleteEvent($r);
  177. // $oTimeline->_oDb->deleteCache(array('event_id' => $r['id']));
  178. BxDolService::call('bx_timeline', 'delete_entity', array($r['id']));
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. /** @} */