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

/plugins/youporn/library/X/VlcShares/Plugins/YouPorn.php

http://vlc-shares.googlecode.com/
PHP | 377 lines | 209 code | 80 blank | 88 comment | 42 complexity | 1772609409b090235baee211dcd42611 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. class X_VlcShares_Plugins_YouPorn extends X_VlcShares_Plugins_Abstract implements X_VlcShares_Plugins_ResolverInterface {
  3. const VERSION = '0.1';
  4. const VERSION_CLEAN = '0.1';
  5. const URL_INDEX_VIDEOS = 'http://www.youporn.com/browse/time?page=%s';
  6. const LOCATION = '/^(?P<page>[0-9]+)\/(?P<id>[0-9]+)$';
  7. //{{{NODE DEFINITION
  8. private $nodes = array(
  9. 'exact:' => array(
  10. 'function' => 'menuVideos',
  11. 'params' => array(1)
  12. ),
  13. 'regex:/^(?P<page>[0-9]+?)$/' => array(
  14. 'function' => 'menuVideos',
  15. 'params' => array('$page')
  16. ),
  17. );
  18. //}}}
  19. protected $cachedLocation = array();
  20. function __construct() {
  21. // this plugin requires:
  22. // VlcShares > 0.5.4
  23. // OR
  24. // VlcShares 0.5.4 + PageParserLib >= 0.1alpha2
  25. if ( X_VlcShares::VERSION_CLEAN != '0.5.4' ||
  26. (class_exists('X_VlcShares_Plugins_Utils')
  27. && method_exists('X_VlcShares_Plugins_Utils', 'menuProxy'))
  28. ) {
  29. $this->setPriority('getCollectionsItems');
  30. $this->setPriority('getShareItems');
  31. $this->setPriority('preGetModeItems');
  32. $this->setPriority('preRegisterVlcArgs');
  33. $this->setPriority('getIndexManageLinks');
  34. $this->setPriority('prepareConfigElement');
  35. }
  36. $this->setPriority('gen_beforeInit');
  37. $this->setPriority('getIndexMessages');
  38. }
  39. /**
  40. * Inizialize translator for this plugin
  41. * @param Zend_Controller_Action $controller
  42. */
  43. function gen_beforeInit(Zend_Controller_Action $controller) {
  44. $this->helpers()->language()->addTranslation(__CLASS__);
  45. $this->helpers()->hoster()->registerHoster(
  46. new X_VlcShares_Plugins_Helper_Hoster_YouPorn(
  47. $this->config('hide.useragent', false),
  48. $this->config('video.quality', 1)
  49. )
  50. );
  51. }
  52. /**
  53. * @see X_VlcShares_Plugins_ResolverInterface::getLocation()
  54. */
  55. function resolveLocation($location = null) {
  56. if ( $location == '' || $location == null ) return false;
  57. if ( array_key_exists($location, $this->cachedLocation) ) {
  58. return $this->cachedLocation[$location];
  59. }
  60. X_Debug::i("Requested location: $location");
  61. $split = $location != '' ? @explode('/', $location, 2) : array();
  62. @list($page, $id) = $split;
  63. X_Debug::i("Page: $page, Id: $id");
  64. if ( $id == null ) {
  65. // location isn't a valid video id, so we return fals
  66. // and insert the query result in the cache
  67. $this->cachedLocation[$location] = false;
  68. return false;
  69. }
  70. try {
  71. // find an hoster which can handle the url type and revolve the real url
  72. $return = $this->getLinkHosterUrl($id);
  73. } catch (Exception $e) {
  74. $return = false;
  75. }
  76. $this->cachedLocation[$location] = $return;
  77. return $return;
  78. }
  79. /**
  80. * @see X_VlcShares_Plugins_ResolverInterface::getParentLocation()
  81. */
  82. function getParentLocation($location = null) {
  83. if ( $location == '' || $location == null ) return false;
  84. $exploded = explode('/', $location);
  85. array_pop($exploded);
  86. if ( count($exploded) >= 1 ) {
  87. return implode('/', $exploded);
  88. } else {
  89. return null;
  90. }
  91. }
  92. /**
  93. * Add the TvLinks link inside collection index
  94. * @param Zend_Controller_Action $controller
  95. */
  96. public function getCollectionsItems(Zend_Controller_Action $controller) {
  97. X_Debug::i("Plugin triggered");
  98. return X_VlcShares_Plugins_Utils::getCollectionsEntryList($this->getId());
  99. }
  100. /**
  101. * Fetch resources from filmstream site
  102. * @param string $provider the plugin key of the one who should handle the request
  103. * @param string $location the current $location
  104. * @param Zend_Controller_Action $controller the controller who handle the request
  105. * @return X_Page_ItemList_PItem
  106. */
  107. public function getShareItems($provider, $location, Zend_Controller_Action $controller) {
  108. // this plugin fetch resources only if it's the provider
  109. if ( $provider != $this->getId() ) return;
  110. // add an info inside the debug log so we can trace this call
  111. X_Debug::i('Plugin triggered');
  112. // disable automatic sorting, items will be already sorted in the target site
  113. X_VlcShares_Plugins::broker()->unregisterPluginClass('X_VlcShares_Plugins_SortItems');
  114. // let's create the itemlist
  115. $items = new X_Page_ItemList_PItem();
  116. // show the requested location in the debug log
  117. // $location has been already decoded
  118. X_Debug::i("Requested node: $location");
  119. X_VlcShares_Plugins_Utils::menuProxy($items, $location, $this->nodes, $this );
  120. return $items;
  121. }
  122. /**
  123. * Add button -watch stream directly-
  124. *
  125. * @param string $provider
  126. * @param string $location
  127. * @param Zend_Controller_Action $controller
  128. */
  129. public function preGetModeItems($provider, $location, Zend_Controller_Action $controller) {
  130. if ( $provider != $this->getId()) return;
  131. X_Debug::i("Plugin triggered");
  132. return X_VlcShares_Plugins_Utils::getWatchDirectlyOrFilter($this->getId(), $this, $location);
  133. }
  134. /**
  135. * Remove vlc-play button if location is invalid
  136. * @param X_Page_Item_PItem $item,
  137. * @param string $provider
  138. * @param Zend_Controller_Action $controller
  139. */
  140. public function filterModeItems(X_Page_Item_PItem $item, $provider,Zend_Controller_Action $controller) {
  141. if ( $item->getKey() == 'core-play') {
  142. X_Debug::i('plugin triggered');
  143. X_Debug::w('core-play flagged as invalid because the link is invalid');
  144. return false;
  145. }
  146. }
  147. /**
  148. * This hook can be used to add low priority args in vlc stack
  149. *
  150. * @param X_Vlc $vlc vlc wrapper object
  151. * @param string $provider id of the plugin that should handle request
  152. * @param string $location to stream
  153. * @param Zend_Controller_Action $controller the controller who handle the request
  154. */
  155. public function preRegisterVlcArgs(X_Vlc $vlc, $provider, $location, Zend_Controller_Action $controller) {
  156. // this plugin inject params only if this is the provider
  157. if ( $provider != $this->getId() ) return;
  158. X_Debug::i('Plugin triggered');
  159. X_VlcShares_Plugins_Utils::registerVlcLocation($this, $vlc, $location);
  160. }
  161. /**
  162. * Add the link for -manage-streamingonline-
  163. * @param Zend_Controller_Action $this
  164. * @return X_Page_ItemList_ManageLink
  165. */
  166. public function getIndexManageLinks(Zend_Controller_Action $controller) {
  167. return X_VlcShares_Plugins_Utils::getIndexManageEntryList($this->getId());
  168. }
  169. /**
  170. * Show an error message if one of the plugin dependencies is missing
  171. * @param Zend_Controller_Action $this
  172. * @return X_Page_ItemList_Message
  173. */
  174. public function getIndexMessages(Zend_Controller_Action $controller) {
  175. $messages = new X_Page_ItemList_Message();
  176. if ( class_exists("X_VlcShares_Plugins_Utils", true) ) {
  177. if ( !method_exists('X_VlcShares_Plugins_Utils', 'menuProxy') ) {
  178. // old version of PageParserLib
  179. $message = new X_Page_Item_Message($this->getId(),"PageParserLib plugin version is old. Please, update it (0.1alpha2 or later required)");
  180. $message->setType(X_Page_Item_Message::TYPE_FATAL);
  181. $messages->append($message);
  182. }
  183. } else {
  184. $message = new X_Page_Item_Message($this->getId(),"PageParser API is required from TvLinks. Please, install PageParserLib plugin (0.1alpha2 or later required)");
  185. $message->setType(X_Page_Item_Message::TYPE_FATAL);
  186. $messages->append($message);
  187. }
  188. return $messages;
  189. }
  190. public function menuVideos(X_Page_ItemList_PItem $items, $pageN = 1) {
  191. $pageN = X_VlcShares_Plugins_Utils::isset_or($pageN, 1);
  192. $page = X_PageParser_Page::getPage(
  193. sprintf(self::URL_INDEX_VIDEOS, $pageN),
  194. new X_PageParser_Parser_YouPorn(X_PageParser_Parser_YouPorn::MODE_VIDEOS)
  195. );
  196. $this->preparePageLoader($page);
  197. $parsed = $page->getParsed();
  198. if ($pageN > 1 ) {
  199. // add -previous-page-
  200. $previousPage = $pageN - 1;
  201. $items->append(X_VlcShares_Plugins_Utils::getPreviousPage("$previousPage", $previousPage));
  202. }
  203. foreach ($parsed as $match) {
  204. $id = $match['id'];
  205. $label = $match['label'];
  206. $thumbnail = array_key_exists('thumbnail', $match) ? $match['thumbnail'] : null;
  207. $item = new X_Page_Item_PItem($this->getId()."-{$pageN}-{$id}", $label );
  208. $item->setType(X_Page_Item_PItem::TYPE_ELEMENT)
  209. ->setCustom(__CLASS__.':location', "$pageN/$id")
  210. ->setDescription(APPLICATION_ENV == 'development' ? "$pageN/$id" : null)
  211. ->setIcon("/images/icons/hosters/youporn.png")
  212. ->setLink(array(
  213. 'action' => 'mode',
  214. 'l' => X_Env::encode("$pageN/$id")
  215. ), 'default', false);
  216. if ( $thumbnail ) $item->setThumbnail($thumbnail);
  217. $items->append($item);
  218. }
  219. if ( $page->getParsed(new X_PageParser_Parser_YouPorn(X_PageParser_Parser_YouPorn::MODE_NEXTPAGE)) ) {
  220. $nextPage = $pageN + 1;
  221. $items->append(X_VlcShares_Plugins_Utils::getNextPage("$nextPage", $nextPage));
  222. }
  223. }
  224. /**
  225. * Add multioptions for video quality
  226. * @param string $section
  227. * @param string $namespace
  228. * @param unknown_type $key
  229. * @param Zend_Form_Element $element
  230. * @param Zend_Form $form
  231. * @param Zend_Controller_Action $controller
  232. */
  233. public function prepareConfigElement($section, $namespace, $key, Zend_Form_Element $element, Zend_Form $form, Zend_Controller_Action $controller) {
  234. // nothing to do if this isn't the right section
  235. if ( $namespace != $this->getId() ) return;
  236. switch ($key) {
  237. // add multioptions for veetle server ip selection
  238. case 'plugins_youporn_video_quality':
  239. if ( $element instanceof Zend_Form_Element_Select ) {
  240. $element->setMultiOptions(array(
  241. '0' => 'MPG (MPG1V/MP2 - 640x480 - 1.2kbps)',
  242. '1' => 'MP4 (H264/AAC - 640x480 - 400kbps)',
  243. '2' => 'MP4 (H264/AAC - 320x240 - 200kbps)'
  244. ));
  245. }
  246. break;
  247. }
  248. }
  249. private function getLinkHosterUrl($linkId) {
  250. try {
  251. /* @var $cacheHelper X_VlcShares_Plugins_Helper_Cache */
  252. $cacheHelper = X_VlcShares_Plugins::helpers()->helper('cache');
  253. $response = $cacheHelper->retrieveItem("youporn::$linkId");
  254. X_Debug::i("Valid cache entry found: $response");
  255. return $response;
  256. } catch (Exception $e) {
  257. // no cache plugin or no entry in cache, it's the same
  258. X_Debug::i("Cache disabled or no valid entry found");
  259. }
  260. $linkUrl = $this->helpers()->hoster()->getHoster('youporn')->getPlayable($linkId, true);
  261. X_Debug::i("Hoster location resolved: $linkUrl");
  262. try {
  263. /* @var $cacheHelper X_VlcShares_Plugins_Helper_Cache */
  264. $cacheHelper = X_VlcShares_Plugins::helpers()->helper('cache');
  265. $cacheHelper->storeItem("youporn::$linkId", $linkUrl, 15); // store for the next 15 min
  266. X_Debug::i("Value stored in cache for 15 min: {key = youporn::$linkId, value = $linkUrl}");
  267. } catch (Exception $e) {
  268. // no cache plugin, next time i have to repeat the request
  269. }
  270. return $linkUrl;
  271. }
  272. private function preparePageLoader(X_PageParser_Page $page) {
  273. $loader = $page->getLoader();
  274. if ( $loader instanceof X_PageParser_Loader_Http || $loader instanceof X_PageParser_Loader_HttpAuthRequired ) {
  275. $http = $loader->getHttpClient()->setConfig(array(
  276. 'maxredirects' => $this->config('request.maxredirects', 10),
  277. 'timeout' => $this->config('request.timeout', 25)
  278. ));
  279. $http->setMethod(Zend_Http_Client::POST);
  280. $http->setParameterPost('user_choice', 'Enter');
  281. $http->setHeaders(array(
  282. $this->config('hide.useragent', false) ? 'User-Agent: vlc-shares/'.X_VlcShares::VERSION .' youporn/'.self::VERSION : 'User-Agent: Mozilla/5.0 (X11; Linux i686; rv:2.0.1) Gecko/20101019 Firefox/4.0.1',
  283. ));
  284. }
  285. }
  286. /**
  287. * Disable cache plugin is registered and enabled
  288. */
  289. private function disableCache() {
  290. if ( X_VlcShares_Plugins::broker()->isRegistered('cache') ) {
  291. $cache = X_VlcShares_Plugins::broker()->getPlugins('cache');
  292. if ( method_exists($cache, 'setDoNotCache') ) {
  293. $cache->setDoNotCache();
  294. }
  295. }
  296. }
  297. }