PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/realdebrid/library/X/VlcShares/Plugins/RealDebrid.php

http://vlc-shares.googlecode.com/
PHP | 401 lines | 277 code | 58 blank | 66 comment | 23 complexity | 9a9bfb53c147ba425fe89abc8f0f8421 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. class X_VlcShares_Plugins_RealDebrid extends X_VlcShares_Plugins_Abstract {
  3. const VERSION_CLEAN = '0.1.5';
  4. const VERSION = '0.1.5';
  5. private $hosters = array(
  6. // replace-id => with class
  7. 'megavideo' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebridMegavideo',
  8. 'megaupload' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebridMegaupload',
  9. 'videobb' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebridVideoBB',
  10. '4shared' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebrid4Shared',
  11. 'megaporn' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebridMegaporn',
  12. 'rapidshare' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebridRapidShare',
  13. 'videozer' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebridVideozer',
  14. 'hulu' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebridHulu',
  15. '2shared' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebrid2Shared',
  16. 'cwtv' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebridCwtv',
  17. 'cbs' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebridCbs',
  18. 'videoweed' => 'X_VlcShares_Plugins_Helper_Hoster_RealDebridVideoweed',
  19. // generics
  20. 'novamov' => array(
  21. '/https?:\/\/(www\.)?nova(mov|up)\.com\/(video|download)\/(?P<ID>[A-Za-z0-9]+)/i',
  22. 'http://www.novamov.com/video/%s'
  23. ),
  24. 'bitshare' => array(
  25. '/https?:\/\/(www\.)?bitshare\.com\/files\/(?P<ID>.+)/i',
  26. 'http://www.bitshare.com/files/%s'
  27. ),
  28. 'filefactory' => array(
  29. '/https?:\/\/(www\.)?filefactory\.com\/file\/(?P<ID>.+)/i',
  30. 'http://www.filefactory.com/file/%s'
  31. ),
  32. 'hotfile' => array(
  33. '/https?:\/\/(www\.)?hotfile\.com\/dl\/(?P<ID>.+)/i',
  34. 'https?://www.hotfile.com/dl/%s'
  35. ),
  36. 'justintv' => array(
  37. '/https?:\/\/(www\.|([a-z]+)\.)?justin\.tv\/(?P<ID>.+)/i',
  38. 'http://www.justin.tv/dl/%s'
  39. ),
  40. 'loadto' => array(
  41. '/https?:\/\/(www\.)?load\.to\/(?P<ID>.+)/i',
  42. 'http://www.load.to/%s'
  43. ),
  44. 'mediafire' => array(
  45. '/https?:\/\/(www\.)?mediafire\.com\/\?(?P<ID>.+)/i',
  46. 'http://www.mediafire.com/?%s'
  47. ),
  48. 'megashares' => array(
  49. '/https?:\/\/(www\.|([a-z]+)\.)?megashares\.com\/(index\.php)?\?(?P<ID>.+)/i',
  50. 'http://www.megashares.com/?%s'
  51. ),
  52. 'netloadin' => array(
  53. '/https?:\/\/(www\.)?netload\.in\/(?P<ID>.+)/i',
  54. 'http://www.netload.in/%s'
  55. ),
  56. 'putlocker' => array(
  57. '/https?:\/\/(www\.)?putlocker\.com\/file\/(?P<ID>.+)/i',
  58. 'http://www.putlocker.com/file/%s'
  59. ),
  60. 'uploadedto' => array(
  61. '/https?:\/\/(www\.)?u((ploaded)|l)?\.to\/(file\/)?(?P<ID>.+)/i',
  62. 'http://uploaded.to/file/%s'
  63. ),
  64. 'wupload' => array(
  65. '/https?:\/\/(www\.)?wupload\.com\/file\/(?P<ID>.+)/i',
  66. 'http://www.wupload.com/file/%s'
  67. ),
  68. 'wattv' => array(
  69. '/https?:\/\/(www\.)?wat\.tv\/video\/(?P<ID>.+)/i',
  70. 'http://www.wat.tv/video/%s'
  71. ),
  72. 'abc' => array(
  73. '/https?:\/\/abc\.go\.com\/watch\/(?P<ID>.+)/i',
  74. 'http://abc.go.com/watch/%s'
  75. ),
  76. 'abc' => array(
  77. '/https?:\/\/abc\.go\.com\/watch\/(?P<ID>.+)/i',
  78. 'http://abc.go.com/watch/%s'
  79. ),
  80. 'sockshare' => array(
  81. '/https\:\/\/((www\.)?)sockshare\.com\/(file|embed)\/(?P<ID>[A-Za-z0-9]+)/i',
  82. 'http://www.sockshare.com/file/%s'
  83. ),
  84. );
  85. public function __construct() {
  86. $this
  87. // gen_beforeInit must be loaded with low priority, other hosters must be setted first
  88. ->setPriority('gen_beforeInit', 99)
  89. ->setPriority('getIndexManageLinks')
  90. ->setPriority('getModeItems')
  91. ->setPriority('preGetSelectionItems')
  92. ->setPriority('getSelectionItems')
  93. ->setPriority('prepareConfigElement');
  94. }
  95. public function gen_beforeInit(Zend_Controller_Action $controller) {
  96. // translation
  97. $this->helpers()->language()->addTranslation(__CLASS__);
  98. $this->helpers()->registerHelper('realdebrid', new X_VlcShares_Plugins_Helper_RealDebrid(array(
  99. 'username' => $this->config('auth.username', ''),
  100. 'password' => $this->config('auth.password', ''),
  101. 'cache' => $this->config('cache.validity', 24 * 60),
  102. )));
  103. foreach ( $this->hosters as $hId => $hClass ) {
  104. // only if hosters is enabled
  105. if ( $this->config("hosters.$hId.enabled", true) ) {
  106. $pHoster = null;
  107. try {
  108. $pHoster = $this->helpers()->hoster()->getHoster($hId);
  109. $this->helpers()->hoster()->unregisterHoster($hId);
  110. } catch (Exception $e) {
  111. // no problem, wasn't registered
  112. }
  113. $hObj = null;
  114. if ( is_array($hClass) ) {
  115. $hObj = new X_VlcShares_Plugins_Helper_Hoster_RealDebridGeneric("$hId-realdebrid", $hClass[0], $hClass[1] );
  116. } else {
  117. $hObj = new $hClass();
  118. }
  119. // allow to use a standard hoster (if available) for fetch informations
  120. // because it should work better than realdebrid hoster
  121. if ( method_exists($hObj, 'setParentHoster') ) {
  122. $hObj->setParentHoster($pHoster);
  123. }
  124. // register the new one:
  125. // - repleace the old hoster id if there is one (so old links work with new hoster)
  126. // - otherwise register it as a new one (with the right icon), so new links are locked to this hoster
  127. $this->helpers()->hoster()->registerHoster($hObj, ($pHoster === null ? null : $hId ) );
  128. }
  129. }
  130. }
  131. /**
  132. * Allow to choose the stream type (if more than one is available)
  133. *
  134. * @param string $provider
  135. * @param string $location
  136. * @param Zend_Controller_Action $controller
  137. * @return X_Page_ItemList_PItem
  138. */
  139. public function getModeItems($provider, $location, Zend_Controller_Action $controller) {
  140. // if provider is FileSystem, This isn't needed for sure
  141. if ( X_VlcShares_Plugins::broker()->getPluginClass($provider) == 'X_VlcShares_Plugins_FileSystem' ) {
  142. return;
  143. }
  144. try {
  145. /* @var $realdebridHelper X_VlcShares_Plugins_Helper_RealDebrid */
  146. $realdebridHelper = $this->helpers('realdebrid');
  147. // check if a valid location has been setted
  148. if ( !$realdebridHelper->isValid() ) {
  149. X_Debug::i("Location is not provided by RealDebrid");
  150. return;
  151. }
  152. $lists = new X_Page_ItemList_PItem();
  153. X_Debug::i('Plugin triggered. Location could be provided by RealDebrid');
  154. $urlHelper = $controller->getHelper('url');
  155. $links = $realdebridHelper->getUrls();
  156. if ( count($links) <= 1 ) {
  157. X_Debug::i('Links count is <= 1, no choose needed');
  158. } else {
  159. $selectedIndex = $controller->getRequest()->getParam('realdebrid', 0);
  160. if ( count($links) < $selectedIndex ) {
  161. $selectedIndex = 0;
  162. }
  163. $linkPart = explode('/', $links[$selectedIndex]);
  164. $selectedLink = urldecode(array_pop($linkPart));
  165. $link = new X_Page_Item_PItem($this->getId(), X_Env::_('p_realdebrid_selected', $selectedLink, $links[$selectedIndex]));
  166. $link->setIcon('/images/realdebrid/logo.png')
  167. ->setType(X_Page_Item_PItem::TYPE_ELEMENT)
  168. ->setLink(array(
  169. 'action' => 'selection',
  170. 'pid' => $this->getId()
  171. ), 'default', false);
  172. $lists->append($link);
  173. }
  174. $link = new X_Page_Item_PItem($this->getId(), X_Env::_('p_realdebrid_regenerate'));
  175. $link->setIcon('/images/realdebrid/logo.png')
  176. ->setType(X_Page_Item_PItem::TYPE_ELEMENT)
  177. ->setLink(array(
  178. 'action' => 'selection',
  179. 'pid' => "{$this->getId()}",
  180. "{$this->getId()}:refresh" => 1
  181. ), 'default', false);
  182. $lists->append($link);
  183. return $lists;
  184. } catch (Exception $e) {
  185. X_Debug::i("Location is not provided by RealDebrid");
  186. }
  187. }
  188. /**
  189. * Set the header of stream type selection
  190. * @param string $provider
  191. * @param string $location
  192. * @param string $pid
  193. * @param Zend_Controller_Action $controller
  194. * @return X_Page_ItemList_PItem
  195. */
  196. public function preGetSelectionItems($provider, $location, $pid, Zend_Controller_Action $controller) {
  197. // we want to expose items only if pid is this plugin
  198. if ( $this->getId() != $pid ) return;
  199. X_Debug::i('Plugin triggered');
  200. $urlHelper = $controller->getHelper('url');
  201. $link = new X_Page_Item_PItem($this->getId().'-header', X_Env::_('p_realdebrid_streamselection_title'));
  202. $link->setType(X_Page_Item_PItem::TYPE_ELEMENT)
  203. ->setLink(X_Env::completeUrl($urlHelper->url()));
  204. return new X_Page_ItemList_PItem(array($link));
  205. }
  206. /**
  207. * Show the list of valid streams
  208. * @param string $provider
  209. * @param string $location
  210. * @param string $pid
  211. * @param Zend_Controller_Action $controller
  212. * @return X_Page_ItemList_PItem
  213. */
  214. public function getSelectionItems($provider, $location, $pid, Zend_Controller_Action $controller) {
  215. // we want to expose items only if pid is this plugin
  216. if ( $this->getId() != $pid ) return;
  217. X_Debug::i('Plugin triggered');
  218. $urlHelper = $controller->getHelper('url');
  219. // i try to mark current selected sub based on $this->getId() param
  220. // in $currentSub i get the name of the current profile
  221. $currentStream = $controller->getRequest()->getParam($this->getId(), false);
  222. $return = new X_Page_ItemList_PItem();
  223. try {
  224. /* @var $realdebridHelper X_VlcShares_Plugins_Helper_RealDebrid */
  225. $realdebridHelper = $this->helpers('realdebrid');
  226. // check if a valid location has been setted
  227. if ( !$realdebridHelper->isValid() ) {
  228. X_Debug::i("Try to force location retrieval");
  229. $providerObj = X_VlcShares_Plugins::broker()->getPlugins($provider);
  230. if ( $providerObj instanceof X_VlcShares_Plugins_ResolverInterface ) {
  231. $providerObj->resolveLocation($location);
  232. } elseif (method_exists($providerObj, 'resolveLocation') ) {
  233. // try to check if resolveLocation is there, even if not the interface
  234. $providerObj->resolveLocation($location);
  235. } else {
  236. X_Debug::e("Provider can't be called for location resolving");
  237. return;
  238. }
  239. }
  240. $links = $realdebridHelper->getUrls();
  241. X_Debug::i('Plugin triggered. Location could be provided by RealDebrid');
  242. // if $pid == ID:refresh, only force refresh the link
  243. if ( $controller->getRequest()->getParam("{$this->getId()}:refresh", false) != false ) {
  244. $realdebridHelper->cleanCurrentCacheEntry();
  245. $item = new X_Page_Item_PItem($this->getId(), X_Env::_('p_realdebrid_refreshdone'));
  246. $item->setType(X_Page_Item_PItem::TYPE_ELEMENT)
  247. ->setLink(array(
  248. 'action' => 'mode',
  249. "{$this->getId()}:refresh" => null,
  250. 'pid' => null
  251. ), 'default', false);
  252. $return->append($item);
  253. } else {
  254. foreach ($links as $i => $streamlink) {
  255. $linkPart = explode('/', $streamlink);
  256. $label = urldecode(array_pop($linkPart));
  257. $item = new X_Page_Item_PItem($this->getId()."-$i", X_Env::_('p_realdebrid_streamoption', $label, $streamlink));
  258. $item->setType(X_Page_Item_PItem::TYPE_ELEMENT)
  259. ->setLink(array(
  260. 'action' => 'mode',
  261. $this->getId() => $i == 0 ? null : $i, // unset this plugin selection
  262. 'pid' => null
  263. ), 'default', false)
  264. ->setHighlight(($currentStream == $i));
  265. $return->append($item);
  266. }
  267. }
  268. } catch ( Exception $e) {
  269. X_Debug::f("Location is not provided by RealDebrid, but i'm inside the stream type selection O_o");
  270. }
  271. return $return;
  272. }
  273. /**
  274. * Add the link for -manage-megavideo-
  275. * @param Zend_Controller_Action $this
  276. * @return X_Page_ItemList_ManageLink
  277. */
  278. public function getIndexManageLinks(Zend_Controller_Action $controller) {
  279. $link = new X_Page_Item_ManageLink($this->getId(), X_Env::_('p_realdebrid_mlink'));
  280. $link->setTitle(X_Env::_('p_realdebrid_managetitle'))
  281. ->setIcon('/images/realdebrid/logo.png')
  282. ->setLink(array(
  283. 'controller' => 'config',
  284. 'action' => 'index',
  285. 'key' => 'realdebrid'
  286. ), 'default', true);
  287. return new X_Page_ItemList_ManageLink(array($link));
  288. }
  289. /**
  290. * Remove cookie.jar if configs change and convert form password to password element
  291. * @param string $section
  292. * @param string $namespace
  293. * @param unknown_type $key
  294. * @param Zend_Form_Element $element
  295. * @param Zend_Form $form
  296. * @param Zend_Controller_Action $controller
  297. */
  298. public function prepareConfigElement($section, $namespace, $key, Zend_Form_Element $element, Zend_Form $form, Zend_Controller_Action $controller) {
  299. // nothing to do if this isn't the right section
  300. if ( $namespace != $this->getId() ) return;
  301. switch ($key) {
  302. // i have to convert it to a password element
  303. case 'plugins_realdebrid_auth_password':
  304. $password = $form->createElement('password', 'plugins_realdebrid_auth_password', array(
  305. 'label' => $element->getLabel(),
  306. 'description' => $element->getDescription(),
  307. 'renderPassword' => true,
  308. ));
  309. $form->plugins_realdebrid_auth_password = $password;
  310. break;
  311. }
  312. // remove cookie.jar if somethings has value
  313. if ( !$form->isErrors() && !is_null($element->getValue()) && $key = 'plugins_realdebrid_auth_password' ) {
  314. // clean cookies
  315. try {
  316. X_Debug::i("Cleaning up cookies in cache");
  317. /* @var $cacheHelper X_VlcShares_Plugins_Helper_Cache */
  318. $cacheHelper = X_VlcShares_Plugins::helpers()->helper('cache');
  319. try {
  320. $cacheHelper->retrieveItem("realdebrid::cookies");
  321. // set expire date to now!
  322. $cacheHelper->storeItem("realdebrid::cookies", '', 0);
  323. } catch (Exception $e) {
  324. // nothing to do
  325. }
  326. } catch (Exception $e) {
  327. X_Debug::w("Cache plugin disabled? O_o");
  328. }
  329. }
  330. }
  331. }