BxStrmModule.php 14 KB

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