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

/core/library/X/VlcShares/Plugins/FileSystem.php

http://vlc-shares.googlecode.com/
PHP | 304 lines | 167 code | 61 blank | 76 comment | 26 complexity | 0fb130885506a08966cba461a01046de MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. require_once 'X/VlcShares/Plugins/Abstract.php';
  3. require_once 'X/VlcShares/Plugins/BackuppableInterface.php';
  4. /**
  5. * VlcShares 0.4.2+ plugin:
  6. * provide filesystem based collection.
  7. * With this plugin, you can add directories
  8. * to vlc-shares's collection
  9. * @author ximarx
  10. *
  11. */
  12. class X_VlcShares_Plugins_FileSystem extends X_VlcShares_Plugins_Abstract implements X_VlcShares_Plugins_ResolverDisplayableInterface, X_VlcShares_Plugins_BackuppableInterface {
  13. public function __construct() {
  14. $this->setPriority('getCollectionsItems')
  15. ->setPriority('preRegisterVlcArgs')
  16. ->setPriority('getShareItems')
  17. ->setPriority('getIndexManageLinks')
  18. ->setPriority('getIndexStatistics')
  19. ->setPriority('getIndexActionLinks');
  20. }
  21. /**
  22. * Return the -shared-folders- link
  23. * for the collection index
  24. * @param Zend_Controller_Action $controller
  25. * @return X_Page_ItemList_PItem
  26. */
  27. public function getCollectionsItems(Zend_Controller_Action $controller) {
  28. X_Debug::i("Plugin triggered");
  29. $link = new X_Page_Item_PItem($this->getId(), X_Env::_('p_filesystem_collectionindex'));
  30. $link->setIcon('/images/filesystem/logo.png')
  31. ->setDescription(X_Env::_('p_filesystem_collectionindex_desc'))
  32. ->setType(X_Page_Item_PItem::TYPE_CONTAINER)
  33. ->setLink(
  34. array(
  35. 'controller' => 'browse',
  36. 'action' => 'share',
  37. 'p' => $this->getId(),
  38. ), 'default', true
  39. );
  40. return new X_Page_ItemList_PItem(array($link));
  41. }
  42. /**
  43. * Return a list of page items for the current $location
  44. * if the $provider is this
  45. */
  46. public function getShareItems($provider, $location, Zend_Controller_Action $controller) {
  47. // this plugin add items only if it is the provider
  48. if ( $provider != $this->getId() ) return;
  49. X_Debug::i("Plugin triggered");
  50. // disabling cache plugin
  51. try {
  52. $cachePlugin = X_VlcShares_Plugins::broker()->getPlugins('cache');
  53. if ( method_exists($cachePlugin, 'setDoNotCache') ) {
  54. $cachePlugin->setDoNotCache();
  55. }
  56. } catch (Exception $e) {
  57. // cache plugin not registered, no problem
  58. }
  59. $urlHelper = $controller->getHelper('url');
  60. $items = new X_Page_ItemList_PItem();
  61. if ( $location != '' ) {
  62. list($shareId, $path) = explode(':', $location, 2);
  63. $share = new Application_Model_FilesystemShare();
  64. Application_Model_FilesystemSharesMapper::i()->find($shareId, $share);
  65. // TODO prevent ../
  66. $browsePath = realpath($share->getPath().$path);
  67. if ( file_exists($browsePath)) {
  68. $dir = new DirectoryIterator($browsePath);
  69. foreach ($dir as $entry) {
  70. if ( $entry->isDot() )
  71. continue;
  72. if ( $entry->isDir() ) {
  73. $item = new X_Page_Item_PItem($this->getId().'-'.$entry->getFilename().'/', "{$entry->getFilename()}/");
  74. $item->setIcon('/images/icons/folder_32.png')
  75. ->setType(X_Page_Item_PItem::TYPE_CONTAINER)
  76. ->setCustom(__CLASS__.':location', "{$share->getId()}:{$path}{$entry->getFilename()}/")
  77. ->setLink(array(
  78. 'l' => X_Env::encode("{$share->getId()}:{$path}{$entry->getFilename()}/")
  79. ), 'default', false);
  80. $items->append($item);
  81. } else if ($entry->isFile() ) {
  82. $item = new X_Page_Item_PItem($this->getId().'-'.$entry->getFilename(), "{$entry->getFilename()}");
  83. $item->setIcon('/images/icons/file_32.png')
  84. ->setType(X_Page_Item_PItem::TYPE_ELEMENT)
  85. ->setCustom(__CLASS__.':location', "{$share->getId()}:{$path}{$entry->getFilename()}")
  86. ->setLink(array(
  87. 'action' => 'mode',
  88. 'l' => X_Env::encode("{$share->getId()}:{$path}{$entry->getFilename()}")
  89. ), 'default', false);
  90. $items->append($item);
  91. } else {
  92. // scarta i symlink
  93. continue;
  94. }
  95. }
  96. }
  97. } else {
  98. // if location is not specified,
  99. // show collections
  100. $shares = Application_Model_FilesystemSharesMapper::i()->fetchAll();
  101. foreach ( $shares as $share ) {
  102. /* @var $share Application_Model_FilesystemShare */
  103. $item = new X_Page_Item_PItem($this->getId().'-'.$share->getLabel(), $share->getLabel());
  104. $item->setIcon('/images/icons/folder_32.png')
  105. ->setDescription(APPLICATION_ENV == 'development' ? $share->getPath() : null)
  106. ->setType(X_Page_Item_PItem::TYPE_CONTAINER)
  107. ->setCustom(__CLASS__.':location', "{$share->getId()}:/")
  108. ->setLink(array(
  109. 'l' => X_Env::encode("{$share->getId()}:/")
  110. ), 'default', false);
  111. $items->append($item);
  112. }
  113. }
  114. return $items;
  115. }
  116. /**
  117. * This hook can be used to add low priority args in vlc stack
  118. *
  119. * @param X_Vlc $vlc vlc wrapper object
  120. * @param string $provider id of the plugin that should handle request
  121. * @param string $location to stream
  122. * @param Zend_Controller_Action $controller the controller who handle the request
  123. */
  124. public function preRegisterVlcArgs(X_Vlc $vlc, $provider, $location, Zend_Controller_Action $controller) {
  125. // this plugin inject params only if this is the provider
  126. if ( $provider != $this->getId() ) return;
  127. // i need to register source as first, because subtitles plugin use source
  128. // for create subfile
  129. X_Debug::i('Plugin triggered');
  130. $location = $this->resolveLocation($location);
  131. if ( $location !== null && file_exists($location) ) {
  132. // TODO adapt to newer api when ready
  133. $vlc->registerArg('source', "\"$location\"");
  134. } else {
  135. X_Debug::e("No source o_O");
  136. }
  137. }
  138. /**
  139. * @see X_VlcShares_Plugins_ResolverInterface::resolveLocation
  140. * @param string $location
  141. * @return string real address of a resource
  142. */
  143. function resolveLocation($location = null) {
  144. // prevent no-location-given error
  145. if ( $location === null || $location === '') return false;
  146. @list($shareId, $path) = explode(':', $location, 2);
  147. $share = new Application_Model_FilesystemShare();
  148. Application_Model_FilesystemSharesMapper::i()->find($shareId, $share);
  149. // TODO prevent ../
  150. return realpath($share->getPath().$path);
  151. }
  152. /**
  153. * @see X_VlcShares_Plugins_ResolverInterface::getParentLocation
  154. * @param $location
  155. */
  156. function getParentLocation($location = null) {
  157. if ($location == null || $location == '') return false;
  158. @list($shareId, $path) = explode(':', $location, 2);
  159. if (rtrim($path,'\\/') == '' ) return null;
  160. return $shareId.':'.rtrim(dirname($path),'\\/').'/';
  161. }
  162. /**
  163. * Add the link Add new shared folder link to actionLinks
  164. * @param Zend_Controller_Action $this
  165. * @return X_Page_ItemList_ActionLink
  166. */
  167. public function getIndexActionLinks(Zend_Controller_Action $controller) {
  168. $urlHelper = $controller->getHelper('url');
  169. $link = new X_Page_Item_ActionLink($this->getId(), X_Env::_('p_filesystem_actionadddirectory'));
  170. $link->setIcon('/images/plus.png')
  171. ->setLink(array(
  172. 'controller' => 'filesystem',
  173. 'action' => 'add'
  174. ), 'default', true);
  175. return new X_Page_ItemList_ActionLink(array($link));
  176. }
  177. /**
  178. * Add the link for -manage-sharef-folders-
  179. * @param Zend_Controller_Action $this
  180. * @return X_Page_ItemList_ManageLink
  181. */
  182. public function getIndexManageLinks(Zend_Controller_Action $controller) {
  183. $link = new X_Page_Item_ManageLink($this->getId(), X_Env::_('p_filesystem_mlink'));
  184. $link->setTitle(X_Env::_('p_filesystem_managetitle'))
  185. ->setIcon('/images/filesystem/logo.png')
  186. ->setLink(array(
  187. 'controller' => 'filesystem',
  188. 'action' => 'index',
  189. ), 'default', true);
  190. return new X_Page_ItemList_ManageLink(array($link));
  191. }
  192. /**
  193. * Show the number of shared folders in the dashboard
  194. * @param Zend_Controller_Action $this
  195. * @return X_Page_ItemList_Statistic
  196. */
  197. public function getIndexStatistics(Zend_Controller_Action $controller) {
  198. $collections = count(Application_Model_FilesystemSharesMapper::i()->fetchAll()); // FIXME create count functions
  199. $stat = new X_Page_Item_Statistic($this->getId(), X_Env::_('p_filesystem_statstitle'));
  200. $stat->setTitle(X_Env::_('p_filesystem_statstitle'))
  201. ->appendStat(X_Env::_('p_filesystem_statcollections').": $collections");
  202. return new X_Page_ItemList_Statistic(array($stat));
  203. }
  204. /**
  205. * Backup shared directories
  206. * This is not a trigger of plugin API. It's called by Backupper plugin
  207. */
  208. function getBackupItems() {
  209. $return = array();
  210. $models = Application_Model_FilesystemSharesMapper::i()->fetchAll();
  211. foreach ($models as $model) {
  212. /* @var $model Application_Model_FilesystemShare */
  213. $return['shares']['share-'.$model->getId()] = array(
  214. 'id' => $model->getId(),
  215. 'path' => $model->getPath(),
  216. 'image' => $model->getImage(),
  217. 'label' => $model->getLabel(),
  218. );
  219. }
  220. return $return;
  221. }
  222. /**
  223. * Restore backupped shared directories
  224. * This is not a trigger of plugin API. It's called by Backupper plugin
  225. */
  226. function restoreItems($items) {
  227. $shares = Application_Model_FilesystemSharesMapper::i()->fetchAll();
  228. // cleaning up all shares
  229. foreach ($shares as $share) {
  230. Application_Model_FilesystemSharesMapper::i()->delete($share);
  231. }
  232. foreach (@$items['shares'] as $shareInfo) {
  233. $share = new Application_Model_FilesystemShare();
  234. $share->setPath(@$shareInfo['path'])
  235. ->setImage(@$shareInfo['image'])
  236. ->setLabel(@$shareInfo['label']);
  237. // i don't set id, or db adapter will try to update old data that i cleaned
  238. Application_Model_FilesystemSharesMapper::i()->save($share);
  239. }
  240. return X_Env::_('p_filesystem_backupper_restoreditems'). ": " .count($items['shares']);
  241. //return parent::restoreItems($items);
  242. }
  243. }