PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/videogals/kalturalib.php

https://gitlab.com/ElvisAns/tiki
PHP | 381 lines | 314 code | 58 blank | 9 comment | 46 complexity | 01a47a91aff9950a6b49061c9b91e4cc MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. use Kaltura\Client\Client;
  8. use Kaltura\Client\Configuration;
  9. use Kaltura\Client\Enum\UiConfCreationMode;
  10. use Kaltura\Client\Enum\UiConfObjType;
  11. use Kaltura\Client\Type\FilterPager;
  12. use Kaltura\Client\Type\MediaEntry;
  13. use Kaltura\Client\Type\MediaEntryFilter;
  14. use Kaltura\Client\Type\MixEntry;
  15. use Kaltura\Client\Type\MixEntryFilter;
  16. use Kaltura\Client\Type\UiConf;
  17. use Kaltura\Client\Type\UiConfFilter;
  18. class KalturaLib
  19. {
  20. const CONFIGURATION_LIST = 'kaltura_configuration_list';
  21. const SESSION_ADMIN = 2;
  22. const SESSION_USER = 0;
  23. private $kconfig;
  24. private $client;
  25. private $sessionType;
  26. private $initialized = false;
  27. public function __construct($session_type)
  28. {
  29. $this->sessionType = $session_type;
  30. }
  31. public function getSessionKey()
  32. {
  33. if ($session = $this->storedKey()) {
  34. return $session;
  35. }
  36. if ($this->getClient()) {
  37. return $this->storedKey();
  38. }
  39. return '';
  40. }
  41. private function storedKey($key = null)
  42. {
  43. global $user;
  44. $tikilib = TikiLib::lib('tiki');
  45. $session = "kaltura_session_{$this->sessionType}_$user";
  46. if (is_null($key)) {
  47. if (isset($_SESSION[$session]) && $_SESSION[$session]['expiry'] > $tikilib->now) {
  48. return $_SESSION[$session]['key'];
  49. }
  50. } else {
  51. $_SESSION[$session] = [
  52. 'key' => $key,
  53. 'expiry' => $tikilib->now + 1800, // Keep for half an hour
  54. ];
  55. }
  56. return $key;
  57. }
  58. private function getConfig()
  59. {
  60. if (! $this->kconfig) {
  61. global $prefs;
  62. $this->kconfig = new Configuration($prefs['kaltura_partnerId']);
  63. $this->kconfig->setServiceUrl($prefs['kaltura_kServiceUrl']);
  64. }
  65. return $this->kconfig;
  66. }
  67. private function getClient()
  68. {
  69. if (! $this->initialized && ! $this->client) {
  70. $this->initialized = true;
  71. try {
  72. $client = new Client($this->getConfig());
  73. if ($session = $this->storedKey()) {
  74. $client->setKs($session);
  75. $this->client = $client;
  76. } elseif ($session = $this->initializeClient($client)) {
  77. $client->setKs($session);
  78. $this->client = $client;
  79. $this->storedKey($session);
  80. }
  81. } catch (Exception $e) {
  82. Feedback::error($e->getMessage());
  83. }
  84. }
  85. return $this->client;
  86. }
  87. public function getMediaUrl($entryId, $playerId)
  88. {
  89. global $prefs;
  90. $config = $this->getConfig();
  91. return $config->getServiceUrl() . "kwidget/wid/_{$prefs['kaltura_partnerId']}/uiconf_id/$playerId/entry_id/$entryId";
  92. }
  93. public function getPlaylist($entryId)
  94. {
  95. return $this->getClient()->playlist->get($entryId);
  96. }
  97. public function testSetup()
  98. {
  99. global $prefs;
  100. if (empty($prefs['kaltura_partnerId']) || ! is_numeric($prefs['kaltura_partnerId']) || empty($prefs['kaltura_secret']) || empty($prefs['kaltura_adminSecret'])) {
  101. return false;
  102. } else {
  103. return true;
  104. }
  105. }
  106. private function initializeClient($client)
  107. {
  108. global $prefs, $user;
  109. if (! $this->testSetup()) {
  110. return false;
  111. }
  112. if ($user) {
  113. $kuser = $user;
  114. } else {
  115. $kuser = 'Anonymous';
  116. }
  117. if ($this->sessionType == self::SESSION_ADMIN) {
  118. $session = $client->session->start($prefs['kaltura_adminSecret'], $kuser, self::SESSION_ADMIN, $prefs['kaltura_partnerId'], 86400, 'edit:*');
  119. } else {
  120. $session = $client->session->start($prefs['kaltura_secret'], $kuser, self::SESSION_USER, $prefs['kaltura_partnerId'], 86400, 'edit:*');
  121. }
  122. return $session;
  123. }
  124. private function _getPlayersUiConfs()
  125. {
  126. if ($client = $this->getClient()) {
  127. $filter = new UiConfFilter();
  128. $filter->objTypeEqual = 1; // 1 denotes Players
  129. $filter->orderBy = '-createdAt';
  130. $uiConfs = $client->uiConf->listAction($filter);
  131. if (is_null($client->error)) {
  132. return $uiConfs;
  133. }
  134. }
  135. $uiConfs = new stdClass();
  136. $uiConfs->objects = [];
  137. return $uiConfs;
  138. }
  139. public function getPlayersUiConfs()
  140. {
  141. $cachelib = TikiLib::lib('cache');
  142. if (! $configurations = $cachelib->getSerialized(self::CONFIGURATION_LIST)) {
  143. try {
  144. $obj = $this->_getPlayersUiConfs()->objects;
  145. } catch (Exception $e) {
  146. Feedback::error($e->getMessage());
  147. return [];
  148. }
  149. $configurations = [];
  150. foreach ($obj as $o) {
  151. $configurations[] = get_object_vars($o);
  152. }
  153. $cachelib->cacheItem(self::CONFIGURATION_LIST, serialize($configurations));
  154. }
  155. return $configurations;
  156. }
  157. public function getPlayersUiConf($playerId)
  158. {
  159. // Ontaining full list, because it is cached
  160. $confs = $this->getPlayersUiConfs();
  161. foreach ($confs as $config) {
  162. if ($config['id'] == $playerId) {
  163. return $config;
  164. }
  165. }
  166. }
  167. public function updateStandardTikiKcw()
  168. {
  169. if ($client = $this->getClient()) {
  170. // first check if there is an existing one
  171. $pager = null;
  172. $filter = new UiConfFilter();
  173. $filter->nameLike = 'Tiki.org Standard 2013';
  174. $filter->objTypeEqual = UiConfObjType::CONTRIBUTION_WIZARD;
  175. $existing = $client->uiConf->listAction($filter, $pager);
  176. if (count($existing->objects) > 0) {
  177. $current_obj = array_pop($existing->objects);
  178. $current = $current_obj->id;
  179. } else {
  180. $current = '';
  181. }
  182. global $tikipath;
  183. $uiConf = new UiConf();
  184. $uiConf->name = 'Tiki.org Standard 2013';
  185. $uiConf->objType = UiConfObjType::CONTRIBUTION_WIZARD;
  186. $filename = $tikipath . "lib/videogals/standardTikiKcw.xml";
  187. $fh = fopen($filename, 'r');
  188. $confXML = fread($fh, filesize($filename));
  189. $uiConf->confFile = $confXML;
  190. $uiConf->useCdn = 1;
  191. $uiConf->swfUrl = '/flash/kcw/v2.1.4/ContributionWizard.swf';
  192. $uiConf->tags = 'autodeploy, content_v3.2.5, content_upload';
  193. // first try to update
  194. if ($current) {
  195. try {
  196. $results = $client->uiConf->update($current, $uiConf);
  197. if (isset($results->id)) {
  198. return $results->id;
  199. }
  200. } catch (Exception $e) {
  201. Feedback::error($e->getMessage());
  202. }
  203. } else {
  204. try {
  205. // create if updating failed or not updating
  206. $uiConf->creationMode = UiConfCreationMode::ADVANCED;
  207. $results = $client->uiConf->add($uiConf);
  208. if (isset($results->id)) {
  209. return $results->id;
  210. } else {
  211. return '';
  212. }
  213. } catch (Exception $e) {
  214. Feedback::error($e->getMessage());
  215. }
  216. }
  217. }
  218. return '';
  219. }
  220. public function cloneMix($entryId)
  221. {
  222. if ($client = $this->getClient()) {
  223. return $client->mixing->cloneAction($entryId);
  224. }
  225. }
  226. public function deleteMedia($entryId)
  227. {
  228. if ($client = $this->getClient()) {
  229. return $client->media->delete($entryId);
  230. }
  231. }
  232. public function deleteMix($entryId)
  233. {
  234. if ($client = $this->getClient()) {
  235. return $client->mixing->delete($entryId);
  236. }
  237. }
  238. public function flattenVideo($entryId)
  239. {
  240. if ($client = $this->getClient()) {
  241. return $client->mixing->requestFlattening($entryId, 'flv'); // FIXME this method is no longer supported
  242. }
  243. }
  244. public function getMix($entryId)
  245. {
  246. if ($client = $this->getClient()) {
  247. return $client->mixing->get($entryId);
  248. }
  249. }
  250. public function updateMix($entryId, array $data)
  251. {
  252. if ($client = $this->getClient()) {
  253. $kentry = new MixEntry();
  254. $kentry->name = $data['name'];
  255. $kentry->description = $data['description'];
  256. $kentry->tags = $data['tags'];
  257. $kentry->editorType = $data['editorType'];
  258. $kentry->adminTags = $data['adminTags'];
  259. return $client->mixing->update($entryId, $kentry);
  260. }
  261. }
  262. public function getMedia($entryId)
  263. {
  264. if ($client = $this->getClient()) {
  265. return $client->media->get($entryId);
  266. }
  267. }
  268. public function updateMedia($entryId, array $data)
  269. {
  270. if ($client = $this->getClient()) {
  271. $kentry = new MediaEntry();
  272. $kentry->name = $data['name'];
  273. $kentry->description = $data['description'];
  274. $kentry->tags = $data['tags'];
  275. $kentry->adminTags = $data['adminTags'];
  276. return $client->media->update($entryId, $kentry);
  277. }
  278. }
  279. public function listMix($sort_mode, $page, $page_size, $find)
  280. {
  281. if ($client = $this->getClient()) {
  282. $kpager = new FilterPager();
  283. $kpager->pageIndex = $page;
  284. $kpager->pageSize = $page_size;
  285. $kfilter = new MixEntryFilter();
  286. $kfilter->orderBy = $sort_mode;
  287. $kfilter->nameMultiLikeOr = $find;
  288. return $client->mixing->listAction($kfilter, $kpager);
  289. }
  290. }
  291. public function listMedia($sort_mode, $page, $page_size, $find)
  292. {
  293. if ($client = $this->getClient()) {
  294. $kpager = new FilterPager();
  295. $kpager->pageIndex = $page;
  296. $kpager->pageSize = $page_size;
  297. $kfilter = new MediaEntryFilter();
  298. $kfilter->orderBy = $sort_mode;
  299. $kfilter->nameMultiLikeOr = $find;
  300. $kfilter->statusIn = '-1,-2,0,1,2';
  301. return $client->media->listAction($kfilter, $kpager);
  302. }
  303. }
  304. public function getMovieList(array $movies)
  305. {
  306. if (count($movies) && $client = $this->getClient()) {
  307. $kpager = new FilterPager();
  308. $kpager->pageIndex = 0;
  309. $kpager->pageSize = count($movies);
  310. $kfilter = new MediaEntryFilter();
  311. $kfilter->idIn = implode(',', $movies);
  312. $mediaList = [];
  313. foreach ($client->media->listAction($kfilter, $kpager)->objects as $media) {
  314. $mediaList[] = [
  315. 'id' => $media->id,
  316. 'name' => $media->name,
  317. ];
  318. }
  319. return $mediaList;
  320. }
  321. return [];
  322. }
  323. }