BxStrmModule.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 actionEmbedStream($iContentId = 0)
  38. {
  39. $this->_serviceTemplateFunc ('embedStream', $iContentId);
  40. }
  41. public function actionStreamViewers ($iContentId = 0)
  42. {
  43. header('Content-Type:text/javascript; charset=utf-8');
  44. $CNF = &$this->_oConfig->CNF;
  45. $mixedContent = $this->_getContent($iContentId, 'getContentInfoById');
  46. if ($mixedContent === false) {
  47. echo json_encode(['viewers' => _t('_sys_txt_error_occured')]);
  48. exit;
  49. }
  50. list($iContentId, $aContentInfo) = $mixedContent;
  51. $iNum = $this->getStreamEngine()->getViewersNum($aContentInfo[$CNF['FIELD_KEY']]);
  52. if (false === $iNum)
  53. $this->onStreamStopped($iContentId, $aContentInfo);
  54. else
  55. $this->onStreamStarted($iContentId, $aContentInfo);
  56. echo json_encode(['viewers' => $iNum !== false ? _t('_bx_stream_txt_viewers', (int)$iNum) : _t('_bx_stream_txt_wait_for_stream'), 'num' => $iNum]);
  57. }
  58. public function serviceStreamBroadcast ($iContentId = 0)
  59. {
  60. return $this->_serviceTemplateFunc ('entryStreamBroadcast', $iContentId);
  61. }
  62. public function serviceStreamViewers ($iContentId = 0)
  63. {
  64. return $this->_serviceTemplateFunc ('entryStreamViewers', $iContentId);
  65. }
  66. public function serviceStreamPlayer ($iContentId = 0)
  67. {
  68. return $this->_serviceTemplateFunc ('entryStreamPlayer', $iContentId);
  69. }
  70. public function serviceStreamRtmpSettings ($iContentId = 0)
  71. {
  72. $CNF = &$this->_oConfig->CNF;
  73. $mixedContent = $this->_getContent($iContentId, 'getContentInfoById');
  74. if ($mixedContent === false)
  75. return false;
  76. list($iContentId, $aContentInfo) = $mixedContent;
  77. if ($aContentInfo[$CNF['FIELD_AUTHOR']] !== bx_get_logged_profile_id() && !isAdmin())
  78. return false;
  79. $a = $this->getStreamEngine()->getRtmpSettings($aContentInfo[$CNF['FIELD_KEY']]);
  80. if (!$a)
  81. return false;
  82. $aForm = array(
  83. 'form_attrs' => array(
  84. 'name' => 'bx-stream-stmp-settings',
  85. ),
  86. 'inputs' => array(
  87. 'url' => array(
  88. 'type' => 'text',
  89. 'name' => 'url'.time(),
  90. 'caption' => _t('_bx_stream_form_entry_input_server'),
  91. 'value' => $a['server'],
  92. 'attrs' => array('readonly' => 'readonly'),
  93. ),
  94. 'key' => array(
  95. 'type' => 'password',
  96. 'name' => 'key'.time(),
  97. 'caption' => _t('_bx_stream_form_entry_input_stream_key'),
  98. 'value' => $a['key'],
  99. 'attrs' => array('readonly' => 'readonly'),
  100. ),
  101. ),
  102. );
  103. if ($this->getStreamEngine()->isSreamFromBrowser()) {
  104. $sUrl = 'page.php?i=broadcast-stream&id=' . (int)$iContentId;
  105. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sUrl);
  106. $aForm['inputs']['header_div'] = array (
  107. 'type' => 'custom',
  108. 'content' => _t('_bx_stream_manual_settings_or_stream_from_webcam'),
  109. );
  110. $aForm['inputs']['stream-from-webcam'] = array (
  111. 'type' => 'submit',
  112. 'name' => 'submit',
  113. 'value' => _t('_bx_stream_from_webcam'),
  114. 'attrs' => array('onclick' => 'document.location="' . bx_html_attribute($sUrl) . '"; return false;'),
  115. );
  116. }
  117. $oForm = new BxTemplFormView ($aForm);
  118. return $oForm->getCode();
  119. }
  120. /**
  121. * Entry post for Timeline module
  122. */
  123. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  124. {
  125. $CNF = &$this->_oConfig->CNF;
  126. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  127. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  128. return $aResult;
  129. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  130. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  131. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  132. return $aResult;
  133. }
  134. public function serviceCheckAllowedCommentsPost($iContentId, $sObjectComments)
  135. {
  136. $CNF = &$this->_oConfig->CNF;
  137. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  138. if ($aContentInfo && $aContentInfo[$CNF['FIELD_DISABLE_COMMENTS']] == 1)
  139. return false;
  140. return parent::serviceCheckAllowedCommentsPost($iContentId, $sObjectComments);
  141. }
  142. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  143. {
  144. $CNF = &$this->_oConfig->CNF;
  145. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  146. if ($aContentInfo && $aContentInfo[$CNF['FIELD_DISABLE_COMMENTS']] == 1)
  147. return false;
  148. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  149. }
  150. public function checkAllowedSetThumb ($iContentId = 0)
  151. {
  152. return CHECK_ACTION_RESULT_ALLOWED;
  153. }
  154. public function serviceGetBadges($iContentId, $bIsSingle = false, $bIsCompact = false)
  155. {
  156. $CNF = &$this->_oConfig->CNF;
  157. $s = parent::serviceGetBadges($iContentId, $bIsSingle, $bIsCompact);
  158. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  159. if (!$aContentInfo)
  160. return $s;
  161. return $s . $this->_oTemplate->getLiveBadge($aContentInfo);
  162. }
  163. public function onStreamStarted($iContentId, $aContentInfo = array())
  164. {
  165. $CNF = &$this->_oConfig->CNF;
  166. if (!$aContentInfo)
  167. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  168. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'awaiting')
  169. return;
  170. if(!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'active'), array($CNF['FIELD_ID'] => $iContentId)))
  171. return;
  172. $this->onPublished($iContentId);
  173. bx_alert($this->getName(), 'publish_succeeded', $aContentInfo[$CNF['FIELD_ID']], $aContentInfo[$CNF['FIELD_AUTHOR']], array(
  174. 'object_author_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  175. 'privacy_view' => BX_DOL_PG_ALL,
  176. ));
  177. }
  178. public function onStreamStopped($iContentId, $aContentInfo = array())
  179. {
  180. $CNF = &$this->_oConfig->CNF;
  181. if (!$aContentInfo)
  182. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  183. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'active')
  184. return;
  185. if (!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'awaiting'), array($CNF['FIELD_ID'] => $iContentId)))
  186. return;
  187. if (BxDolRequest::serviceExists('bx_timeline', 'get_all')) {
  188. $a = BxDolService::call('bx_timeline', 'get_all', array(array(
  189. 'type' => 'conditions',
  190. 'conditions' => array(
  191. 'type' => 'bx_stream',
  192. 'action' => 'added',
  193. 'object_id' => $iContentId
  194. )
  195. )));
  196. if ($a) {
  197. $oTimeline = BxDolModule::getInstance('bx_timeline');
  198. if ($oTimeline) {
  199. foreach ($a as $r) {
  200. // $oTimeline->deleteEvent($r);
  201. // $oTimeline->_oDb->deleteCache(array('event_id' => $r['id']));
  202. BxDolService::call('bx_timeline', 'delete_entity', array($r['id']));
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. /** @} */