BxStrmModule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. $sKey = bx_get('name');
  40. $sSecret = bx_get('s');
  41. $sPath = bx_get('path');
  42. $sBasePath = getParam('bx_stream_server_nginx_recording_base_path');
  43. if (empty($sBasePath) || empty($sPath) || empty($sKey) || empty($sSecret) || 'NGINX' != getParam('bx_stream_engine') || !($aContentInfo = $this->_oDb->getEntriesBy(['type' => 'conditions', 'conditions' => ['key' => $sKey]]))) {
  44. header('HTTP/1.0 404 Not Found');
  45. }
  46. elseif ($sSecret != base_convert(substr(md5(BX_DOL_SECRET . $sKey), -4), 16, 36)) {
  47. header('HTTP/1.0 403 Forbidden');
  48. }
  49. elseif (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedRecord(true)) {
  50. header('HTTP/1.0 403 Forbidden');
  51. }
  52. else {
  53. $sBaseUrl = rtrim(getParam('bx_stream_recordings_url'), '/');
  54. $sUrl = str_replace($sBasePath, $sBaseUrl, $sPath);
  55. if ($oStorage = BxDolStorage::getObjectInstance('bx_stream_recordings')) {
  56. $iFileId = $oStorage->storeFileFromUrl($sUrl, true, 0, $aContentInfo['id']);
  57. if (!$iFileId) {
  58. bx_log('bx_stream', "Store recording from URL(" . $sUrl . ") failed for content id({$aContentInfo['id']})");
  59. }
  60. }
  61. }
  62. }
  63. public function actionNginxOnPublish()
  64. {
  65. $sKey = bx_get('name');
  66. $sSecret = bx_get('s');
  67. if (empty($sKey) || empty($sSecret) || !($aContentInfo = $this->_oDb->getEntriesBy(['type' => 'conditions', 'conditions' => ['key' => $sKey]]))) {
  68. header('HTTP/1.0 404 Not Found');
  69. }
  70. elseif ($sSecret != base_convert(substr(md5(BX_DOL_SECRET . $sKey), -4), 16, 36)) {
  71. header('HTTP/1.0 403 Forbidden');
  72. }
  73. else {
  74. header('HTTP/1.0 201 Created');
  75. }
  76. exit;
  77. }
  78. public function actionEmbedStream($iContentId = 0)
  79. {
  80. $this->_serviceTemplateFunc ('embedStream', $iContentId);
  81. }
  82. public function actionStreamViewers ($iContentId = 0)
  83. {
  84. header('Content-Type:text/javascript; charset=utf-8');
  85. $CNF = &$this->_oConfig->CNF;
  86. $mixedContent = $this->_getContent($iContentId, 'getContentInfoById');
  87. if ($mixedContent === false) {
  88. echo json_encode(['viewers' => _t('_sys_txt_error_occured')]);
  89. exit;
  90. }
  91. list($iContentId, $aContentInfo) = $mixedContent;
  92. $iNum = $this->getStreamEngine()->getViewersNum($aContentInfo[$CNF['FIELD_KEY']]);
  93. if (false === $iNum)
  94. $this->onStreamStopped($iContentId, $aContentInfo);
  95. else
  96. $this->onStreamStarted($iContentId, $aContentInfo);
  97. echo json_encode(['viewers' => $iNum !== false ? _t('_bx_stream_txt_viewers', (int)$iNum) : _t('_bx_stream_txt_wait_for_stream'), 'num' => $iNum]);
  98. }
  99. public function serviceStreamBroadcast ($iContentId = 0)
  100. {
  101. return $this->_serviceTemplateFunc ('entryStreamBroadcast', $iContentId);
  102. }
  103. public function serviceStreamViewers ($iContentId = 0)
  104. {
  105. return $this->_serviceTemplateFunc ('entryStreamViewers', $iContentId);
  106. }
  107. public function serviceStreamPlayer ($iContentId = 0)
  108. {
  109. return $this->_serviceTemplateFunc ('entryStreamPlayer', $iContentId);
  110. }
  111. public function serviceStreamRecordings ($iContentId = 0)
  112. {
  113. $oGrid = BxDolGrid::getObjectInstance('bx_stream_recordings');
  114. return $oGrid ? $oGrid->getCode() : false;
  115. }
  116. public function serviceStreamRtmpSettings ($iContentId = 0)
  117. {
  118. $CNF = &$this->_oConfig->CNF;
  119. $mixedContent = $this->_getContent($iContentId, 'getContentInfoById');
  120. if ($mixedContent === false)
  121. return false;
  122. list($iContentId, $aContentInfo) = $mixedContent;
  123. if ($aContentInfo[$CNF['FIELD_AUTHOR']] !== bx_get_logged_profile_id() && !isAdmin())
  124. return false;
  125. $a = $this->getStreamEngine()->getRtmpSettings($aContentInfo[$CNF['FIELD_KEY']]);
  126. if (!$a)
  127. return false;
  128. $aForm = array(
  129. 'form_attrs' => array(
  130. 'name' => 'bx-stream-stmp-settings',
  131. ),
  132. 'inputs' => array(
  133. 'url' => array(
  134. 'type' => 'text',
  135. 'name' => 'url'.time(),
  136. 'caption' => _t('_bx_stream_form_entry_input_server'),
  137. 'value' => $a['server'],
  138. 'attrs' => array('readonly' => 'readonly'),
  139. ),
  140. 'key' => array(
  141. 'type' => 'password',
  142. 'name' => 'key'.time(),
  143. 'caption' => _t('_bx_stream_form_entry_input_stream_key'),
  144. 'value' => $a['key'],
  145. 'attrs' => array('readonly' => 'readonly'),
  146. ),
  147. ),
  148. );
  149. if ($this->getStreamEngine()->isSreamFromBrowser()) {
  150. $sUrl = 'page.php?i=broadcast-stream&id=' . (int)$iContentId;
  151. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sUrl);
  152. $aForm['inputs']['header_div'] = array (
  153. 'type' => 'custom',
  154. 'content' => _t('_bx_stream_manual_settings_or_stream_from_webcam'),
  155. );
  156. $aForm['inputs']['stream-from-webcam'] = array (
  157. 'type' => 'submit',
  158. 'name' => 'submit',
  159. 'value' => _t('_bx_stream_from_webcam'),
  160. 'attrs' => array('onclick' => 'document.location="' . bx_html_attribute($sUrl) . '"; return false;'),
  161. );
  162. }
  163. $oForm = new BxTemplFormView ($aForm);
  164. return $oForm->getCode();
  165. }
  166. /**
  167. * Entry post for Timeline module
  168. */
  169. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  170. {
  171. $CNF = &$this->_oConfig->CNF;
  172. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  173. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  174. return $aResult;
  175. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  176. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  177. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  178. return $aResult;
  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() && '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. bx_alert($this->getName(), 'publish_succeeded', $aContentInfo[$CNF['FIELD_ID']], $aContentInfo[$CNF['FIELD_AUTHOR']], array(
  225. 'object_author_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  226. 'privacy_view' => BX_DOL_PG_ALL,
  227. ));
  228. }
  229. public function onStreamStopped($iContentId, $aContentInfo = array())
  230. {
  231. $CNF = &$this->_oConfig->CNF;
  232. if (!$aContentInfo)
  233. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  234. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'active')
  235. return;
  236. if (!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'awaiting'), array($CNF['FIELD_ID'] => $iContentId)))
  237. return;
  238. if (CHECK_ACTION_RESULT_ALLOWED === $this->checkAllowedRecord(true) && 'OvenMediaEngine' == getParam('bx_stream_engine')) {
  239. $iRecordingId = BxDolSession::getInstance()->getValue('bx-stream-rec-' . $iContentId);
  240. if (!$iRecordingId)
  241. $iRecordingId = $this->_oDb->getRecordingId($iContentId);
  242. if ($iRecordingId) {
  243. $aRecordings = $this->getStreamEngine()->stopRecording($iRecordingId);
  244. foreach ($aRecordings as $a) {
  245. if ($a && !empty($a['filePath']) && $oStorage = BxDolStorage::getObjectInstance('bx_stream_recordings')) {
  246. $iFileId = $oStorage->storeFileFromUrl(getParam('bx_stream_recordings_url') . $a['filePath'], true, 0, $iContentId);
  247. if ($iFileId) {
  248. $this->_oDb->deleteRecording($iRecordingId);
  249. }
  250. else {
  251. bx_log('bx_stream', "Store recording from URL(" . getParam('bx_stream_recordings_url') . $a['filePath'] . ") failed for content id($iContentId)");
  252. }
  253. }
  254. }
  255. BxDolSession::getInstance()->unsetValue('bx-stream-rec-' . $iContentId);
  256. }
  257. else {
  258. bx_log('bx_stream', "No recording ID defined for content ID($iContentId)");
  259. }
  260. }
  261. if (BxDolRequest::serviceExists('bx_timeline', 'get_all')) {
  262. $a = BxDolService::call('bx_timeline', 'get_all', array(array(
  263. 'type' => 'conditions',
  264. 'conditions' => array(
  265. 'type' => 'bx_stream',
  266. 'action' => 'added',
  267. 'object_id' => $iContentId
  268. )
  269. )));
  270. if ($a) {
  271. $oTimeline = BxDolModule::getInstance('bx_timeline');
  272. if ($oTimeline) {
  273. foreach ($a as $r) {
  274. // $oTimeline->deleteEvent($r);
  275. // $oTimeline->_oDb->deleteCache(array('event_id' => $r['id']));
  276. BxDolService::call('bx_timeline', 'delete_entity', array($r['id']));
  277. }
  278. }
  279. }
  280. }
  281. }
  282. public function checkAllowedRecord ($isPerformAction = false)
  283. {
  284. if (!getParam('bx_stream_recordings_url'))
  285. return _t('_sys_txt_access_denied');
  286. // check ACL
  287. $aCheck = checkActionModule($this->_iProfileId, 'record', $this->getName(), $isPerformAction);
  288. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  289. return $aCheck[CHECK_ACTION_MESSAGE];
  290. return CHECK_ACTION_RESULT_ALLOWED;
  291. }
  292. }
  293. /** @} */