BxVideosModule.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 Videos Videos
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Videos module
  13. */
  14. class BxVideosModule extends BxBaseModFilesModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. }
  20. public function actionParseEmbedLink() {
  21. $sCode = bx_get('code');
  22. $aRes = $this->parseEmbedLink($sCode);
  23. if ($aRes) echo $aRes['embed'];
  24. }
  25. public function actionEmbed($iContentId, $sUnitTemplate = '', $sAddCode = '')
  26. {
  27. return parent::actionEmbed($iContentId, 'unit_gallery_embed.html');
  28. }
  29. public function parseEmbedLink($sLink) {
  30. $aEmbedProviders = $this->_oDb->getEmbedProviders();
  31. if ($aEmbedProviders) foreach ($aEmbedProviders as $aEmbedProvider) {
  32. if (!$aEmbedProvider['class_name']) continue;
  33. if ($aEmbedProvider['class_file'])
  34. require_once(BX_DIRECTORY_PATH_ROOT . $aEmbedProvider['class_file']);
  35. $oLinkParser = new $aEmbedProvider['class_name']($this);
  36. if ($mResult = $oLinkParser->parseLink($sLink))
  37. return $mResult;
  38. }
  39. return false;
  40. }
  41. /**
  42. * @page service Service Calls
  43. * @section bx_videos Videos
  44. * @subsection bx_videos-page_blocks Page Blocks
  45. * @subsubsection bx_videos-entity_video_block entity_video_block
  46. *
  47. * @code bx_srv('bx_videos', 'entity_video_block', [...]); @endcode
  48. *
  49. * Get page block with video player.
  50. *
  51. * @param $iContentId (optional) video ID. If empty value is provided, an attempt to get it from GET/POST arrays will be performed.
  52. * @return HTML string with block content to display on the site or false if there is no enough input data. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  53. *
  54. * @see BxVideosModule::serviceEntityVideoBlock
  55. */
  56. /**
  57. * @ref bx_videos-entity_video_block "entity_video_block"
  58. */
  59. public function serviceEntityVideoBlock ($iContentId = 0)
  60. {
  61. $mixedContent = $this->_getContent($iContentId);
  62. if($mixedContent === false)
  63. return false;
  64. list($iContentId, $aContentInfo) = $mixedContent;
  65. $s = $this->_oTemplate->entryVideo($aContentInfo);
  66. return $this->_bIsApi ? [bx_api_get_block('entity_text', $s)] : $s;
  67. }
  68. /**
  69. * @page service Service Calls
  70. * @section bx_videos Videos
  71. * @subsection bx_videos-page_blocks Page Blocks
  72. * @subsubsection bx_videos-entity_rating entity_rating
  73. *
  74. * @code bx_srv('bx_videos', 'entity_rating', [...]); @endcode
  75. *
  76. * Get page block with Stars based video's rating.
  77. *
  78. * @param $iContentId (optional) video ID. If empty value is provided, an attempt to get it from GET/POST arrays will be performed.
  79. * @return HTML string with block content to display on the site or false if there is no enough input data. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  80. *
  81. * @see BxVideosModule::serviceEntityRating
  82. */
  83. /**
  84. * @ref bx_videos-entity_rating "entity_rating"
  85. */
  86. public function serviceEntityRating($iContentId = 0)
  87. {
  88. return $this->_serviceTemplateFunc ('entryRating', $iContentId);
  89. }
  90. public function serviceGetThumb ($iContentId, $sTranscoder = '')
  91. {
  92. $CNF = &$this->_oConfig->CNF;
  93. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  94. if($this->_bIsApi) {
  95. $aImage = bx_api_get_image($CNF['OBJECT_STORAGE'], (int)$aContentInfo[$CNF['FIELD_THUMB']]);
  96. if(!$aImage)
  97. $aImage = bx_api_get_image([$CNF['OBJECT_STORAGE_VIDEOS'], $CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']], (int)$aContentInfo[$CNF['FIELD_VIDEO']]);
  98. return $aImage;
  99. }
  100. list($iImageId, $sImageThumb, $sImageGallery, $sImageCover) = $this->_oTemplate->getUnitImages($aContentInfo);
  101. return $sImageCover ? $sImageCover : $sImageGallery;
  102. }
  103. public function serviceGetVideo ($iContentId)
  104. {
  105. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  106. return $this->getVideoData($aContentInfo);
  107. }
  108. public function serviceGetFile ($iContentId, $aParams = [])
  109. {
  110. $CNF = &$this->_oConfig->CNF;
  111. if(!isset($aParams['storage']))
  112. $aParams['storage'] = $CNF['OBJECT_STORAGE_VIDEOS'];
  113. if(!isset($aParams['field']))
  114. $aParams['field'] = $CNF['FIELD_VIDEO'];
  115. return parent::serviceGetFile($iContentId, $aParams);
  116. }
  117. public function getVideoData($aContentInfo)
  118. {
  119. $CNF = &$this->_oConfig->CNF;
  120. if(empty($CNF['OBJECT_STORAGE_VIDEOS']) || empty($CNF['OBJECT_VIDEOS_TRANSCODERS']))
  121. return [];
  122. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_VIDEOS']);
  123. $iFile = (int)$aContentInfo[$CNF['FIELD_VIDEO']];
  124. $aFile = $oStorage->getFile($iFile);
  125. if(empty($aFile) || !is_array($aFile) || strncmp('video/', $aFile['mime_type'], 6) !== 0)
  126. return [];
  127. $oTcvPoster = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']);
  128. $oTciPoster = BxDolTranscoder::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_POSTER']);
  129. $oTcvMp4 = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  130. $oTcvMp4Hd = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4_hd']);
  131. if(!($oTcvPoster || $oTciPoster) || !$oTcvMp4 || !$oTcvMp4Hd)
  132. return [];
  133. $sPosterSrc = !empty($CNF['FIELD_POSTER']) ? $CNF['FIELD_POSTER'] : $CNF['FIELD_THUMB'];
  134. if(!empty($sPosterSrc) && !empty($aContentInfo[$sPosterSrc]) && $oTciPoster)
  135. $sPoster = $oTciPoster->getFileUrl($aContentInfo[$sPosterSrc]);
  136. else
  137. $sPoster = $oTcvPoster->getFileUrl($iFile);
  138. $sVideoUrl = $oStorage->getFileUrlById($iFile);
  139. $aVideoFile = $oStorage->getFile($iFile);
  140. $sVideoUrlHd = '';
  141. if (!empty($aVideoFile['dimensions']) && $oTcvMp4Hd->isProcessHD($aVideoFile['dimensions']))
  142. $sVideoUrlHd = $oTcvMp4Hd->getFileUrl($iFile);
  143. return [
  144. 'id' => $iFile,
  145. 'src_poster' => $sPoster,
  146. 'src_mp4' => $oTcvMp4->getFileUrl($iFile),
  147. 'src_mp4_hd' => $sVideoUrlHd
  148. ];
  149. }
  150. public function serviceGetTimelinePost($aEvent, $aBrowseParams = [])
  151. {
  152. $mixedResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  153. if($this->_bIsApi && $mixedResult && is_array($mixedResult)) {
  154. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  155. switch($aContentInfo['video_source']) {
  156. case 'embed':
  157. if(($sKey = 'video_embed') && !empty($aContentInfo[$sKey])) {
  158. bx_import('BxDolEmbed');
  159. $aEmbed = ($oEmbed = BxDolEmbed::getObjectInstance('sys_system')) !== false ? $oEmbed->getLinkHTML($aContentInfo[$sKey]) : ['url' => $aContentInfo[$sKey]];
  160. $mixedResult['content'] = array_merge($mixedResult['content'], [
  161. 'embed' => $aEmbed,
  162. 'images' => []
  163. ]);
  164. }
  165. break;
  166. case 'upload':
  167. $aCnt = $mixedResult['content'];
  168. if($aCnt['videos'] && is_array($aCnt['videos']))
  169. $mixedResult['content']['images'] = [];
  170. break;
  171. }
  172. }
  173. return $mixedResult;
  174. }
  175. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  176. {
  177. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  178. if($this->_bIsApi)
  179. return $aResult;
  180. if ($aContentInfo['video_source'] == 'embed' && !empty($aContentInfo['video_embed_data'])) {
  181. if (!is_array($aContentInfo['video_embed_data'])) $aContentInfo['video_embed_data'] = unserialize($aContentInfo['video_embed_data']);
  182. if (isset($aContentInfo['video_embed_data']['embed']) && !empty($aContentInfo['video_embed_data']['embed'])) {
  183. $aResult['images'] = array();
  184. $aResult['top_raw'] = $this->_oTemplate->getResponsiveEmbed($aContentInfo['video_embed_data']['embed'], isset($aBrowseParams['dynamic_mode']) ? $aBrowseParams['dynamic_mode'] : false);
  185. return $aResult;
  186. }
  187. }
  188. if(!empty($aResult['videos']) && is_array($aResult['videos']))
  189. $aResult['images'] = array();
  190. return $aResult;
  191. }
  192. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  193. {
  194. $CNF = &$this->_oConfig->CNF;
  195. if($this->_bIsApi) {
  196. $aImage = bx_api_get_image($CNF['OBJECT_STORAGE'], (int)$aContentInfo[$CNF['FIELD_THUMB']]);
  197. if(!$aImage)
  198. $aImage = bx_api_get_image([$CNF['OBJECT_STORAGE_VIDEOS'], $CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']], (int)$aContentInfo[$CNF['FIELD_VIDEO']]);
  199. return $aImage ? [$aImage] : [];
  200. }
  201. list($iImageId, $sImageThumb, $sImageGallery, $sImageCover) = $this->_oTemplate->getUnitImages($aContentInfo);
  202. if(empty($sImageGallery) && !empty($sImageThumb))
  203. $sImageGallery = $sImageThumb;
  204. if(empty($sImageGallery))
  205. return array();
  206. return array(
  207. array('id' => $iImageId, 'url' => $sUrl, 'src' => $sImageGallery, 'src_orig' => $sImageCover),
  208. );
  209. }
  210. protected function _getVideosForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = [])
  211. {
  212. $aResult = $this->getVideoData($aContentInfo);
  213. if(!$aResult)
  214. return $aResult;
  215. return $this->_bIsApi ? [$aResult] : [
  216. $aResult['id'] => $aResult
  217. ];
  218. }
  219. }
  220. /** @} */