BxStrmModule.php 9.2 KB

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