BxStrmModule.php 13 KB

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