BxStrmModule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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, [
  22. $CNF['FIELD_ANONYMOUS'],
  23. $CNF['FIELD_ALLOW_COMMENTS']
  24. ]);
  25. }
  26. public function getStreamEngine ()
  27. {
  28. if ($this->_oEngine)
  29. return $this->_oEngine;
  30. $sEngine = getParam('bx_stream_engine');
  31. bx_import('Engine' . $sEngine, $this->_aModule);
  32. $sClass = 'BxStrmEngine' . $sEngine;
  33. $this->_oEngine = new $sClass;
  34. return $this->_oEngine;
  35. }
  36. public function actionNginxOnRecordDone()
  37. {
  38. $CNF = &$this->_oConfig->CNF;
  39. $sKey = bx_get('name');
  40. $sSecret = bx_get('s');
  41. $sPath = bx_get('path');
  42. $sBasePath = rtrim(getParam('bx_stream_server_nginx_recording_base_path'), '/');
  43. if (empty($sBasePath) || empty($sPath) || empty($sKey) || empty($sSecret) || 'NGINX' != getParam('bx_stream_engine')) {
  44. header('HTTP/1.0 400 Bad Request');
  45. return;
  46. }
  47. if ($sSecret != base_convert(substr(md5(BX_DOL_SECRET . $sKey), -4), 16, 36)) {
  48. header('HTTP/1.0 401 Unauthorized');
  49. return;
  50. }
  51. $aContentInfo = $this->_oDb->getEntriesBy(['type' => 'conditions', 'conditions' => ['key' => $sKey]]);
  52. if (!$aContentInfo) {
  53. header('HTTP/1.0 404 Not Found');
  54. return;
  55. }
  56. $aContentInfo = array_pop($aContentInfo);
  57. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedRecord(true, $aContentInfo[$CNF['FIELD_AUTHOR']])) {
  58. header('HTTP/1.0 403 Forbidden');
  59. return;
  60. }
  61. $sBaseUrl = rtrim(getParam('bx_stream_recordings_url'), '/');
  62. $sUrl = str_replace($sBasePath, $sBaseUrl, $sPath);
  63. if ($oStorage = BxDolStorage::getObjectInstance('bx_stream_recordings')) {
  64. $iFileId = $oStorage->storeFileFromUrl($sUrl, true, $aContentInfo[$CNF['FIELD_AUTHOR']], $aContentInfo['id']);
  65. if (!$iFileId)
  66. bx_log('bx_stream', "Store recording from URL(" . $sUrl . ") failed for content id({$aContentInfo['id']})", BX_LOG_ERR);
  67. }
  68. }
  69. public function actionNginxOnPublish()
  70. {
  71. $sKey = bx_get('name');
  72. $sSecret = bx_get('s');
  73. if (empty($sKey) || empty($sSecret) || !($aContentInfo = $this->_oDb->getEntriesBy(['type' => 'conditions', 'conditions' => ['key' => $sKey]]))) {
  74. header('HTTP/1.0 404 Not Found');
  75. }
  76. elseif ($sSecret != base_convert(substr(md5(BX_DOL_SECRET . $sKey), -4), 16, 36)) {
  77. header('HTTP/1.0 403 Forbidden');
  78. }
  79. else {
  80. header('HTTP/1.0 201 Created');
  81. }
  82. exit;
  83. }
  84. public function actionEmbed($iContentId, $sUnitTemplate = '', $sAddCode = '')
  85. {
  86. return $this->_serviceTemplateFunc ('embedStream', $iContentId);
  87. }
  88. public function actionEmbedStream($iContentId = 0)
  89. {
  90. header("Location:" . BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'embed/' . $iContentId . '/', true, 301);
  91. exit;
  92. }
  93. public function actionStreamViewers ($iContentId = 0)
  94. {
  95. header('Content-Type:text/javascript; charset=utf-8');
  96. $CNF = &$this->_oConfig->CNF;
  97. $mixedContent = $this->_getContent($iContentId, 'getContentInfoById');
  98. if ($mixedContent === false) {
  99. echo json_encode(['viewers' => _t('_sys_txt_error_occured')]);
  100. exit;
  101. }
  102. list($iContentId, $aContentInfo) = $mixedContent;
  103. $iNum = $this->getStreamEngine()->getViewersNum($aContentInfo[$CNF['FIELD_KEY']]);
  104. if (false === $iNum)
  105. $this->onStreamStopped($iContentId, $aContentInfo);
  106. else
  107. $this->onStreamStarted($iContentId, $aContentInfo);
  108. echo json_encode(['viewers' => $iNum !== false ? _t('_bx_stream_txt_viewers', (int)$iNum) : _t('_bx_stream_txt_wait_for_stream'), 'num' => $iNum]);
  109. }
  110. public function serviceStreamBroadcast ($iContentId = 0)
  111. {
  112. return $this->_serviceTemplateFunc ('entryStreamBroadcast', $iContentId);
  113. }
  114. public function serviceStreamViewers ($iContentId = 0)
  115. {
  116. return $this->_serviceTemplateFunc ('entryStreamViewers', $iContentId);
  117. }
  118. public function serviceStreamPlayer ($iContentId = 0)
  119. {
  120. return $this->_serviceTemplateFunc ('entryStreamPlayer', $iContentId);
  121. }
  122. public function serviceStreamRecordings ($iContentId = 0)
  123. {
  124. $oGrid = BxDolGrid::getObjectInstance('bx_stream_recordings');
  125. return $oGrid ? $oGrid->getCode() : false;
  126. }
  127. public function serviceStreamRtmpSettings ($iContentId = 0)
  128. {
  129. $CNF = &$this->_oConfig->CNF;
  130. $mixedContent = $this->_getContent($iContentId, 'getContentInfoById');
  131. if ($mixedContent === false)
  132. return false;
  133. list($iContentId, $aContentInfo) = $mixedContent;
  134. $mixedMsg = $this->checkAllowedAdd (false);
  135. if ($mixedMsg !== CHECK_ACTION_RESULT_ALLOWED)
  136. return MsgBox($mixedMsg);
  137. if ($aContentInfo[$CNF['FIELD_AUTHOR']] !== bx_get_logged_profile_id() && !isAdmin())
  138. return false;
  139. $a = $this->getStreamEngine()->getRtmpSettings($aContentInfo[$CNF['FIELD_KEY']]);
  140. if (!$a)
  141. return false;
  142. $aForm = array(
  143. 'form_attrs' => array(
  144. 'name' => 'bx-stream-stmp-settings',
  145. ),
  146. 'inputs' => array(
  147. 'url' => array(
  148. 'type' => 'text',
  149. 'name' => 'url'.time(),
  150. 'caption' => _t('_bx_stream_form_entry_input_server'),
  151. 'value' => $a['server'],
  152. 'attrs' => array('readonly' => 'readonly'),
  153. ),
  154. 'key' => array(
  155. 'type' => 'password',
  156. 'name' => 'key'.time(),
  157. 'caption' => _t('_bx_stream_form_entry_input_stream_key'),
  158. 'value' => $a['key'],
  159. 'attrs' => array('readonly' => 'readonly'),
  160. ),
  161. ),
  162. );
  163. if ($this->getStreamEngine()->isSreamFromBrowser()) {
  164. $sUrl = 'page.php?i=broadcast-stream&id=' . (int)$iContentId;
  165. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($sUrl));
  166. $aForm['inputs']['header_div'] = array (
  167. 'type' => 'custom',
  168. 'content' => _t('_bx_stream_manual_settings_or_stream_from_webcam'),
  169. );
  170. $aForm['inputs']['stream-from-webcam'] = array (
  171. 'type' => 'submit',
  172. 'name' => 'submit',
  173. 'value' => _t('_bx_stream_from_webcam'),
  174. 'attrs' => array('onclick' => 'document.location="' . bx_html_attribute($sUrl) . '"; return false;'),
  175. );
  176. }
  177. $oForm = new BxTemplFormView ($aForm);
  178. return $oForm->getCode();
  179. }
  180. public function serviceCheckAllowedCommentsPost($iContentId, $sObjectComments)
  181. {
  182. $CNF = &$this->_oConfig->CNF;
  183. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  184. if ($aContentInfo && $aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  185. return false;
  186. return parent::serviceCheckAllowedCommentsPost($iContentId, $sObjectComments);
  187. }
  188. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  189. {
  190. $CNF = &$this->_oConfig->CNF;
  191. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  192. if ($aContentInfo && $aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  193. return false;
  194. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  195. }
  196. public function checkAllowedSetThumb ($iContentId = 0)
  197. {
  198. return CHECK_ACTION_RESULT_ALLOWED;
  199. }
  200. public function serviceGetBadges($iContentId, $bIsSingle = false, $bIsCompact = false)
  201. {
  202. $CNF = &$this->_oConfig->CNF;
  203. $s = parent::serviceGetBadges($iContentId, $bIsSingle, $bIsCompact);
  204. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  205. if (!$aContentInfo)
  206. return $s;
  207. return $s . $this->_oTemplate->getLiveBadge($aContentInfo);
  208. }
  209. public function onStreamStarted($iContentId, $aContentInfo = array())
  210. {
  211. $CNF = &$this->_oConfig->CNF;
  212. if (!$aContentInfo)
  213. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  214. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'awaiting')
  215. return;
  216. if(!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'active'), array($CNF['FIELD_ID'] => $iContentId)))
  217. return;
  218. if (CHECK_ACTION_RESULT_ALLOWED === $this->checkAllowedRecord(false, $aContentInfo[$CNF['FIELD_AUTHOR']]) && 'OvenMediaEngine' == getParam('bx_stream_engine')) {
  219. $iRecordingId = $this->_oDb->getNewRecordingId($iContentId);
  220. $this->getStreamEngine()->startRecording($iRecordingId, $aContentInfo[$CNF['FIELD_KEY']]);
  221. BxDolSession::getInstance()->setValue('bx-stream-rec-' . $iContentId, $iRecordingId);
  222. }
  223. $this->onPublished($iContentId);
  224. /**
  225. * @hooks
  226. * @hookdef hook-bx_stream-publish_succeeded 'bx_stream', 'publish_succeeded' - hook on stream published succeeded
  227. * - $unit_name - equals `bx_stream`
  228. * - $action - equals `publish_succeeded`
  229. * - $object_id - stream id
  230. * - $sender_id - profile_id of stream's author
  231. * - $extra_params - array of additional params with the following array keys:
  232. * - `object_author_id` - [int] stream's author
  233. * - `privacy_view` - [int] BX_DOL_PG_ALL
  234. * @hook @ref hook-bx_stream-publish_succeeded
  235. */
  236. bx_alert($this->getName(), 'publish_succeeded', $aContentInfo[$CNF['FIELD_ID']], $aContentInfo[$CNF['FIELD_AUTHOR']], array(
  237. 'object_author_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  238. 'privacy_view' => BX_DOL_PG_ALL,
  239. ));
  240. }
  241. public function onStreamStopped($iContentId, $aContentInfo = array())
  242. {
  243. $CNF = &$this->_oConfig->CNF;
  244. if (!$aContentInfo)
  245. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  246. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'active')
  247. return;
  248. if (!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'awaiting'), array($CNF['FIELD_ID'] => $iContentId)))
  249. return;
  250. if (CHECK_ACTION_RESULT_ALLOWED === $this->checkAllowedRecord(true, $aContentInfo[$CNF['FIELD_AUTHOR']]) && 'OvenMediaEngine' == getParam('bx_stream_engine')) {
  251. $iRecordingId = BxDolSession::getInstance()->getValue('bx-stream-rec-' . $iContentId);
  252. if (!$iRecordingId)
  253. $iRecordingId = $this->_oDb->getRecordingId($iContentId);
  254. if ($iRecordingId) {
  255. $aRecordings = $this->getStreamEngine()->stopRecording($iRecordingId);
  256. // $this->getStreamEngine()->processRecordings($iRecordingId, $aContentInfo, $this); // this line is commented to process recordings later upon cron run to make sure long recording are processed properly
  257. BxDolSession::getInstance()->unsetValue('bx-stream-rec-' . $iContentId);
  258. }
  259. else {
  260. bx_log('bx_stream', "No recording ID defined for content ID($iContentId)", BX_LOG_ERR);
  261. }
  262. }
  263. if (BxDolRequest::serviceExists('bx_timeline', 'get_all')) {
  264. $a = BxDolService::call('bx_timeline', 'get_all', array(array(
  265. 'type' => 'conditions',
  266. 'conditions' => array(
  267. 'type' => 'bx_stream',
  268. 'action' => 'added',
  269. 'object_id' => $iContentId
  270. )
  271. )));
  272. if ($a) {
  273. $oTimeline = BxDolModule::getInstance('bx_timeline');
  274. if ($oTimeline) {
  275. foreach ($a as $r) {
  276. // $oTimeline->deleteEvent($r);
  277. // $oTimeline->_oDb->deleteCache(array('event_id' => $r['id']));
  278. BxDolService::call('bx_timeline', 'delete_entity', array($r['id']));
  279. }
  280. }
  281. }
  282. }
  283. }
  284. public function checkAllowedRecord ($isPerformAction = false, $iProfileId = false)
  285. {
  286. if (!getParam('bx_stream_recordings_url'))
  287. return _t('_sys_txt_access_denied');
  288. // check ACL
  289. $aCheck = checkActionModule(false === $iProfileId ? $this->_iProfileId : $iProfileId, 'record', $this->getName(), $isPerformAction);
  290. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  291. return $aCheck[CHECK_ACTION_MESSAGE];
  292. return CHECK_ACTION_RESULT_ALLOWED;
  293. }
  294. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = [])
  295. {
  296. $aImages = parent::_getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams);
  297. if(!empty($aImages) && is_array($aImages))
  298. foreach($aImages as $iIndex => $aImage)
  299. $aImages[$iIndex]['onclick'] = '';
  300. return $aImages;
  301. }
  302. }
  303. /** @} */