BxStrmModule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. if ($aContentInfo[$CNF['FIELD_AUTHOR']] !== bx_get_logged_profile_id() && !isAdmin())
  136. return false;
  137. $a = $this->getStreamEngine()->getRtmpSettings($aContentInfo[$CNF['FIELD_KEY']]);
  138. if (!$a)
  139. return false;
  140. $aForm = array(
  141. 'form_attrs' => array(
  142. 'name' => 'bx-stream-stmp-settings',
  143. ),
  144. 'inputs' => array(
  145. 'url' => array(
  146. 'type' => 'text',
  147. 'name' => 'url'.time(),
  148. 'caption' => _t('_bx_stream_form_entry_input_server'),
  149. 'value' => $a['server'],
  150. 'attrs' => array('readonly' => 'readonly'),
  151. ),
  152. 'key' => array(
  153. 'type' => 'password',
  154. 'name' => 'key'.time(),
  155. 'caption' => _t('_bx_stream_form_entry_input_stream_key'),
  156. 'value' => $a['key'],
  157. 'attrs' => array('readonly' => 'readonly'),
  158. ),
  159. ),
  160. );
  161. if ($this->getStreamEngine()->isSreamFromBrowser()) {
  162. $sUrl = 'page.php?i=broadcast-stream&id=' . (int)$iContentId;
  163. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sUrl);
  164. $aForm['inputs']['header_div'] = array (
  165. 'type' => 'custom',
  166. 'content' => _t('_bx_stream_manual_settings_or_stream_from_webcam'),
  167. );
  168. $aForm['inputs']['stream-from-webcam'] = array (
  169. 'type' => 'submit',
  170. 'name' => 'submit',
  171. 'value' => _t('_bx_stream_from_webcam'),
  172. 'attrs' => array('onclick' => 'document.location="' . bx_html_attribute($sUrl) . '"; return false;'),
  173. );
  174. }
  175. $oForm = new BxTemplFormView ($aForm);
  176. return $oForm->getCode();
  177. }
  178. /**
  179. * Entry post for Timeline module
  180. */
  181. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  182. {
  183. $CNF = &$this->_oConfig->CNF;
  184. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  185. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  186. return $aResult;
  187. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  188. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  189. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  190. return $aResult;
  191. }
  192. public function serviceCheckAllowedCommentsPost($iContentId, $sObjectComments)
  193. {
  194. $CNF = &$this->_oConfig->CNF;
  195. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  196. if ($aContentInfo && $aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  197. return false;
  198. return parent::serviceCheckAllowedCommentsPost($iContentId, $sObjectComments);
  199. }
  200. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  201. {
  202. $CNF = &$this->_oConfig->CNF;
  203. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  204. if ($aContentInfo && $aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  205. return false;
  206. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  207. }
  208. public function checkAllowedSetThumb ($iContentId = 0)
  209. {
  210. return CHECK_ACTION_RESULT_ALLOWED;
  211. }
  212. public function serviceGetBadges($iContentId, $bIsSingle = false, $bIsCompact = false)
  213. {
  214. $CNF = &$this->_oConfig->CNF;
  215. $s = parent::serviceGetBadges($iContentId, $bIsSingle, $bIsCompact);
  216. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  217. if (!$aContentInfo)
  218. return $s;
  219. return $s . $this->_oTemplate->getLiveBadge($aContentInfo);
  220. }
  221. public function onStreamStarted($iContentId, $aContentInfo = array())
  222. {
  223. $CNF = &$this->_oConfig->CNF;
  224. if (!$aContentInfo)
  225. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  226. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'awaiting')
  227. return;
  228. if(!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'active'), array($CNF['FIELD_ID'] => $iContentId)))
  229. return;
  230. if (CHECK_ACTION_RESULT_ALLOWED === $this->checkAllowedRecord(false, $aContentInfo[$CNF['FIELD_AUTHOR']]) && 'OvenMediaEngine' == getParam('bx_stream_engine')) {
  231. $iRecordingId = $this->_oDb->getNewRecordingId($iContentId);
  232. $this->getStreamEngine()->startRecording($iRecordingId, $aContentInfo[$CNF['FIELD_KEY']]);
  233. BxDolSession::getInstance()->setValue('bx-stream-rec-' . $iContentId, $iRecordingId);
  234. }
  235. $this->onPublished($iContentId);
  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)");
  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. }
  295. /** @} */