BxStrmTemplate.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * Module representation.
  13. */
  14. class BxStrmTemplate extends BxBaseModTextTemplate
  15. {
  16. function __construct(&$oConfig, &$oDb)
  17. {
  18. $this->MODULE = 'bx_stream';
  19. parent::__construct($oConfig, $oDb);
  20. }
  21. public function getLiveBadge ($aContentInfo)
  22. {
  23. $CNF = &$this->getModule()->_oConfig->CNF;
  24. if (!$aContentInfo || $aContentInfo[$CNF['FIELD_STATUS']] != 'active')
  25. return '';
  26. return $this->parseHtmlByName('stream_badge.html', array(
  27. 'label' => _t('_bx_stream_txt_live_now'),
  28. ));
  29. }
  30. public function entryStreamBroadcast ($aContentInfo)
  31. {
  32. $oEngine = $this->getModule()->getStreamEngine();
  33. if (!$oEngine->isSreamFromBrowser())
  34. return MsgBox(_t('_bx_stream_from_webcam_not_supported'));
  35. $mixedMsg = $this->getModule()->checkAllowedAdd (false);
  36. if ($mixedMsg !== CHECK_ACTION_RESULT_ALLOWED)
  37. return MsgBox($mixedMsg);
  38. $CNF = &$this->getModule()->_oConfig->CNF;
  39. $aAspect = preg_split("/[\/:]+/", trim(getParam('bx_stream_aspect_ratio')), 2, PREG_SPLIT_NO_EMPTY);
  40. return $this->parseHtmlByName('stream_broadcast_ome.html', array(
  41. 'suffix' => md5($aContentInfo[$CNF['FIELD_KEY']]),
  42. 'broadcast_url' => $oEngine->getWebrtcIngestUrl($aContentInfo[$CNF['FIELD_KEY']]),
  43. 'aspect_ratio' => $aAspect && is_array($aAspect) && $aAspect[0] && $aAspect[1] ? $aAspect[0] / $aAspect[1] : 0,
  44. 'popup_share' => $this->popupShare($aContentInfo),
  45. 'popup_settings' => $this->popupSettings($aContentInfo),
  46. ));
  47. }
  48. public function popupShare ($aContentInfo)
  49. {
  50. $CNF = &$this->getModule()->_oConfig->CNF;
  51. $sUrlViewStream = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . (int)$aContentInfo['id'];
  52. $sUrlViewStream = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($sUrlViewStream));
  53. $sUrlEmbed = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'embed/' . (int)$aContentInfo['id'];
  54. $aForm = array(
  55. 'form_attrs' => array(
  56. 'name' => 'bx-stream-form-share',
  57. 'class' => 'bx-def-margin',
  58. ),
  59. 'inputs' => array(
  60. 'url' => array(
  61. 'type' => 'text',
  62. 'name' => 'url'.time(),
  63. 'caption' => _t('_bx_stream_field_link'),
  64. 'value' => $sUrlViewStream,
  65. 'attrs' => array('readonly' => 'readonly'),
  66. ),
  67. 'embed' => array(
  68. 'type' => 'text',
  69. 'name' => 'embed'.time(),
  70. 'caption' => _t('_bx_stream_field_embed'),
  71. 'value' => '<iframe width="560" height="315" frameborder="0" allow="autoplay; picture-in-picture" allowfullscreen src="' . $sUrlEmbed . '"></iframe>',
  72. 'attrs' => array('readonly' => 'readonly'),
  73. ),
  74. ),
  75. );
  76. $oForm = new BxTemplFormView ($aForm);
  77. return BxTemplFunctions::getInstance()->popupBox('bx-stream-popup-share', _t('_bx_stream_popup_share'), $oForm->getCode(), true);
  78. }
  79. public function popupSettings ($aContentInfo)
  80. {
  81. $CNF = &$this->getModule()->_oConfig->CNF;
  82. $aForm = array(
  83. 'form_attrs' => array(
  84. 'name' => 'bx-stream-form-settings',
  85. 'onsubmit' => 'return false',
  86. 'onreset' => 'return false',
  87. ),
  88. 'inputs' => array(
  89. 'audio' => array(
  90. 'type' => 'select',
  91. 'name' => 'audio',
  92. 'caption' => _t('_bx_stream_field_audio_source'),
  93. 'value' => '',
  94. ),
  95. 'video' => array(
  96. 'type' => 'select',
  97. 'name' => 'video',
  98. 'caption' => _t('_bx_stream_field_video_source'),
  99. 'value' => '',
  100. ),
  101. ),
  102. );
  103. $oForm = new BxTemplFormView ($aForm);
  104. return BxTemplFunctions::getInstance()->popupBox('bx-stream-popup-settings', _t('_bx_stream_popup_settings'), $oForm->getCode(), true);
  105. }
  106. public function entryStreamViewers ($aContentInfo)
  107. {
  108. $CNF = &$this->getModule()->_oConfig->CNF;
  109. return $this->parseHtmlByName('stream_viewers.html', array(
  110. 'viewers' => _t('_bx_stream_txt_wait_for_stream'),
  111. 'suffix' => md5($aContentInfo[$CNF['FIELD_KEY']]),
  112. 'url' => BX_DOL_URL_MODULES . '?r=' . $this->getModule()->_oConfig->getUri() . '/stream_viewers/' . $aContentInfo[$CNF['FIELD_ID']]
  113. ));
  114. }
  115. public function entryStreamPlayer ($aContentInfo, $aOptions = [])
  116. {
  117. $CNF = &$this->getModule()->_oConfig->CNF;
  118. $sSources = getParam('bx_stream_sources_pattern');
  119. $sHost = getParam('bx_stream_server_host');
  120. $sApp = getParam('bx_stream_app');
  121. if (!$sSources || !$sHost)
  122. return MsgBox(_t('_bx_stream_txt_no_config'));
  123. $sSources = str_replace(
  124. ['{host}', '{app}', '{key}', '{params}'],
  125. [$sHost, $sApp, $aContentInfo[$CNF['FIELD_KEY']], ''],
  126. $sSources
  127. );
  128. $this->addJs('ovenplayer/ovenplayer.js');
  129. if (getParam('bx_stream_dash_enabled'))
  130. $this->addJs('ovenplayer/dash.all.min.js');
  131. if (getParam('bx_stream_hls_enabled'))
  132. $this->addJs('ovenplayer/hls.min.js');
  133. $this->addJsTranslation('_bx_stream_txt_wait_for_stream');
  134. $sImage = '';
  135. $mixedImage = $this->_getHeaderImage($aContentInfo);
  136. if($mixedImage !== false) {
  137. if($o = BxDolTranscoder::getObjectInstance($mixedImage['transcoder']))
  138. $sImage = $o->getFileUrlById($mixedImage['id']);
  139. }
  140. if (!isset($aOptions['mute']))
  141. $aOptions['mute'] = getParam('bx_stream_mute') ? 1 : 0;
  142. if (isset($aOptions['autostart']))
  143. $aOptions['autostart'] = $aOptions['autostart'] ? 1 : 0;
  144. else
  145. $aOptions['autostart'] = 1;
  146. return $this->parseHtmlByName('stream_player.html', array(
  147. 'suffix' => md5($aContentInfo[$CNF['FIELD_KEY']]),
  148. 'sources' => $sSources,
  149. 'mute' => $aOptions['mute'],
  150. 'image' => $sImage,
  151. 'autostart' => $aOptions['autostart'],
  152. ));
  153. }
  154. public function embedStream($aContentInfo)
  155. {
  156. $CNF = &$this->getModule()->_oConfig->CNF;
  157. // TODO: visibility and other checks
  158. $sCode = $this->parseHtmlByName('stream_embed.html', array(
  159. 'player' => $this->entryStreamPlayer ($aContentInfo, [
  160. 'mute' => getParam('bx_stream_mute_embed') ? 1 : 0,
  161. 'autostart' => 0,
  162. ]),
  163. 'viewers' => $this->entryStreamViewers ($aContentInfo),
  164. ));
  165. $this->addCss('main.css');
  166. // display page
  167. $oTemplate = BxDolTemplate::getInstance();
  168. $oTemplate->setPageNameIndex (BX_PAGE_EMBED);
  169. $oTemplate->setPageHeader ($aContentInfo[$CNF['FIELD_TITLE']]);
  170. $oTemplate->setPageContent ('page_main_code', $sCode);
  171. $oTemplate->getPageCode();
  172. exit;
  173. }
  174. /**
  175. * Use Gallery image for both because currently there is no Unit types with small thumbnails.
  176. */
  177. protected function getUnitThumbAndGallery ($aData)
  178. {
  179. list($sPhotoThumb, $sPhotoGallery) = parent::getUnitThumbAndGallery($aData);
  180. return array($sPhotoGallery, $sPhotoGallery);
  181. }
  182. public function getTmplVarsText($aData)
  183. {
  184. $a = parent::getTmplVarsText($aData);
  185. $a['bx_if:show_image']['condition'] = false;
  186. return $a;
  187. }
  188. }
  189. /** @} */