BxStrmModule.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 serviceStreamRecordings ($iContentId = 0)
  86. {
  87. $oGrid = BxDolGrid::getObjectInstance('bx_stream_recordings');
  88. return $oGrid ? $oGrid->getCode() : false;
  89. }
  90. public function serviceStreamRtmpSettings ($iContentId = 0)
  91. {
  92. $CNF = &$this->_oConfig->CNF;
  93. $mixedContent = $this->_getContent($iContentId, 'getContentInfoById');
  94. if ($mixedContent === false)
  95. return false;
  96. list($iContentId, $aContentInfo) = $mixedContent;
  97. if ($aContentInfo[$CNF['FIELD_AUTHOR']] !== bx_get_logged_profile_id() && !isAdmin())
  98. return false;
  99. $a = $this->getStreamEngine()->getRtmpSettings($aContentInfo[$CNF['FIELD_KEY']]);
  100. if (!$a)
  101. return false;
  102. $aForm = array(
  103. 'form_attrs' => array(
  104. 'name' => 'bx-stream-stmp-settings',
  105. ),
  106. 'inputs' => array(
  107. 'url' => array(
  108. 'type' => 'text',
  109. 'name' => 'url'.time(),
  110. 'caption' => _t('_bx_stream_form_entry_input_server'),
  111. 'value' => $a['server'],
  112. 'attrs' => array('readonly' => 'readonly'),
  113. ),
  114. 'key' => array(
  115. 'type' => 'password',
  116. 'name' => 'key'.time(),
  117. 'caption' => _t('_bx_stream_form_entry_input_stream_key'),
  118. 'value' => $a['key'],
  119. 'attrs' => array('readonly' => 'readonly'),
  120. ),
  121. ),
  122. );
  123. if ($this->getStreamEngine()->isSreamFromBrowser()) {
  124. $sUrl = 'page.php?i=broadcast-stream&id=' . (int)$iContentId;
  125. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sUrl);
  126. $aForm['inputs']['header_div'] = array (
  127. 'type' => 'custom',
  128. 'content' => _t('_bx_stream_manual_settings_or_stream_from_webcam'),
  129. );
  130. $aForm['inputs']['stream-from-webcam'] = array (
  131. 'type' => 'submit',
  132. 'name' => 'submit',
  133. 'value' => _t('_bx_stream_from_webcam'),
  134. 'attrs' => array('onclick' => 'document.location="' . bx_html_attribute($sUrl) . '"; return false;'),
  135. );
  136. }
  137. $oForm = new BxTemplFormView ($aForm);
  138. return $oForm->getCode();
  139. }
  140. /**
  141. * Entry post for Timeline module
  142. */
  143. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  144. {
  145. $CNF = &$this->_oConfig->CNF;
  146. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  147. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  148. return $aResult;
  149. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  150. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  151. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  152. return $aResult;
  153. }
  154. public function serviceCheckAllowedCommentsPost($iContentId, $sObjectComments)
  155. {
  156. $CNF = &$this->_oConfig->CNF;
  157. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  158. if ($aContentInfo && $aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  159. return false;
  160. return parent::serviceCheckAllowedCommentsPost($iContentId, $sObjectComments);
  161. }
  162. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  163. {
  164. $CNF = &$this->_oConfig->CNF;
  165. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  166. if ($aContentInfo && $aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  167. return false;
  168. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  169. }
  170. public function checkAllowedSetThumb ($iContentId = 0)
  171. {
  172. return CHECK_ACTION_RESULT_ALLOWED;
  173. }
  174. public function serviceGetBadges($iContentId, $bIsSingle = false, $bIsCompact = false)
  175. {
  176. $CNF = &$this->_oConfig->CNF;
  177. $s = parent::serviceGetBadges($iContentId, $bIsSingle, $bIsCompact);
  178. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  179. if (!$aContentInfo)
  180. return $s;
  181. return $s . $this->_oTemplate->getLiveBadge($aContentInfo);
  182. }
  183. public function onStreamStarted($iContentId, $aContentInfo = array())
  184. {
  185. $CNF = &$this->_oConfig->CNF;
  186. if (!$aContentInfo)
  187. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  188. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'awaiting')
  189. return;
  190. if(!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'active'), array($CNF['FIELD_ID'] => $iContentId)))
  191. return;
  192. if (CHECK_ACTION_RESULT_ALLOWED === $this->checkAllowedRecord()) {
  193. $iRecordingId = $this->_oDb->getNewRecordingId($iContentId);
  194. $this->getStreamEngine()->startRecording($iRecordingId, $aContentInfo[$CNF['FIELD_KEY']]);
  195. BxDolSession::getInstance()->setValue('bx-stream-rec-' . $iContentId, $iRecordingId);
  196. }
  197. $this->onPublished($iContentId);
  198. bx_alert($this->getName(), 'publish_succeeded', $aContentInfo[$CNF['FIELD_ID']], $aContentInfo[$CNF['FIELD_AUTHOR']], array(
  199. 'object_author_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  200. 'privacy_view' => BX_DOL_PG_ALL,
  201. ));
  202. }
  203. public function onStreamStopped($iContentId, $aContentInfo = array())
  204. {
  205. $CNF = &$this->_oConfig->CNF;
  206. if (!$aContentInfo)
  207. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  208. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'active')
  209. return;
  210. if (!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'awaiting'), array($CNF['FIELD_ID'] => $iContentId)))
  211. return;
  212. if (CHECK_ACTION_RESULT_ALLOWED === $this->checkAllowedRecord(true)) {
  213. $iRecordingId = BxDolSession::getInstance()->getValue('bx-stream-rec-' . $iContentId);
  214. if (!$iRecordingId)
  215. $iRecordingId = $this->_oDb->getRecordingId($iContentId);
  216. if ($iRecordingId) {
  217. $aRecordings = $this->getStreamEngine()->stopRecording($iRecordingId);
  218. foreach ($aRecordings as $a) {
  219. if ($a && !empty($a['filePath']) && $oStorage = BxDolStorage::getObjectInstance('bx_stream_recordings')) {
  220. $iFileId = $oStorage->storeFileFromUrl(getParam('bx_stream_recordings_url') . $a['filePath'], true, 0, $iContentId);
  221. if ($iFileId) {
  222. $this->_oDb->deleteRecording($iRecordingId);
  223. }
  224. else {
  225. bx_log('bx_stream', "Store recording from URL(" . getParam('bx_stream_recordings_url') . $a['filePath'] . ") failed");
  226. }
  227. }
  228. }
  229. BxDolSession::getInstance()->unsetValue('bx-stream-rec-' . $iContentId);
  230. }
  231. else {
  232. bx_log('bx_stream', "No recording ID defined for content ID($iContentId)");
  233. }
  234. }
  235. if (BxDolRequest::serviceExists('bx_timeline', 'get_all')) {
  236. $a = BxDolService::call('bx_timeline', 'get_all', array(array(
  237. 'type' => 'conditions',
  238. 'conditions' => array(
  239. 'type' => 'bx_stream',
  240. 'action' => 'added',
  241. 'object_id' => $iContentId
  242. )
  243. )));
  244. if ($a) {
  245. $oTimeline = BxDolModule::getInstance('bx_timeline');
  246. if ($oTimeline) {
  247. foreach ($a as $r) {
  248. // $oTimeline->deleteEvent($r);
  249. // $oTimeline->_oDb->deleteCache(array('event_id' => $r['id']));
  250. BxDolService::call('bx_timeline', 'delete_entity', array($r['id']));
  251. }
  252. }
  253. }
  254. }
  255. }
  256. public function checkAllowedRecord ($isPerformAction = false)
  257. {
  258. if (!getParam('bx_stream_recordings_url'))
  259. return _t('_sys_txt_access_denied');
  260. // check ACL
  261. $aCheck = checkActionModule($this->_iProfileId, 'record', $this->getName(), $isPerformAction);
  262. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  263. return $aCheck[CHECK_ACTION_MESSAGE];
  264. return CHECK_ACTION_RESULT_ALLOWED;
  265. }
  266. }
  267. /** @} */