1
0

BxStrmModule.php 8.4 KB

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