PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/horde-3.3.13/lib/VFS/horde.php

#
PHP | 333 lines | 158 code | 25 blank | 150 comment | 28 complexity | 155a58c283132b7d0174aa7bbd45e67f MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /**
  3. * VFS implementation for the Horde Application Framework.
  4. *
  5. * Required parameters:<pre>
  6. * 'horde_base' Filesystem location of a local Horde installation.</pre>
  7. *
  8. * Optional parameters:<pre>
  9. * 'user' A valid Horde user name.
  10. * 'password' The user's password.</pre>
  11. *
  12. * $Horde: framework/VFS/lib/VFS/horde.php,v 1.1.2.3 2009/01/06 15:23:47 jan Exp $
  13. *
  14. * Copyright 2006-2009 The Horde Project (http://www.horde.org/)
  15. *
  16. * See the enclosed file COPYING for license information (LGPL). If you
  17. * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  18. *
  19. * @author Jan Schneider <jan@horde.org>
  20. * @since Horde 3.2
  21. * @package VFS
  22. */
  23. class VFS_horde extends VFS {
  24. /**
  25. * Reference to a Horde Registry instance.
  26. *
  27. * @var Registry
  28. */
  29. var $_registry;
  30. /**
  31. * Constructor.
  32. *
  33. * @param array $params A hash containing connection parameters.
  34. */
  35. function VFS_horde($params = array())
  36. {
  37. parent::VFS($params);
  38. if (!isset($this->_params['horde_base'])) {
  39. $this->_registry = PEAR::raiseError(sprintf(_("Required \"%s\" not specified in VFS configuration."), 'horde_base'));
  40. return;
  41. }
  42. // Define path to Horde.
  43. @define('HORDE_BASE', $this->_params['horde_base']);
  44. // Load the Horde Framework core, and set up inclusion paths.
  45. require_once HORDE_BASE . '/lib/core.php';
  46. // Create the Registry object.
  47. $this->_registry = &Registry::singleton();
  48. }
  49. function _connect()
  50. {
  51. if (!empty($this->_params['user']) &&
  52. !empty($this->_params['password'])) {
  53. include HORDE_BASE . '/config/conf.php';
  54. $auth_driver = empty($conf['auth']['driver']) ? 'none' : $conf['auth']['driver'];
  55. $auth = &Auth::singleton($auth_driver);
  56. $auth->setAuth($this->_params['user'],
  57. array('password' => $this->_params['password']));
  58. }
  59. }
  60. /**
  61. * Retrieves the size of a file from the VFS.
  62. *
  63. * @abstract
  64. *
  65. * @param string $path The pathname to the file.
  66. * @param string $name The filename to retrieve.
  67. *
  68. * @return integer The file size.
  69. */
  70. function size($path, $name)
  71. {
  72. if (is_a($this->_registry, 'PEAR_Error')) {
  73. return $this->_registry;
  74. }
  75. return PEAR::raiseError(_("Not supported."));
  76. }
  77. /**
  78. * Retrieves a file from the VFS.
  79. *
  80. * @abstract
  81. *
  82. * @param string $path The pathname to the file.
  83. * @param string $name The filename to retrieve.
  84. *
  85. * @return string The file data.
  86. */
  87. function read($path, $name)
  88. {
  89. if (is_a($this->_registry, 'PEAR_Error')) {
  90. return $this->_registry;
  91. }
  92. if (substr($path, 0, 1) == '/') {
  93. $path = substr($path, 1);
  94. }
  95. $pieces = explode('/', $path);
  96. $data = $this->_registry->callByPackage($pieces[0], 'browse', array('path' => $path . '/' . $name));
  97. if (is_object($data)) {
  98. return $data;
  99. }
  100. return $data['data'];
  101. }
  102. /**
  103. * Stores a file in the VFS.
  104. *
  105. * @abstract
  106. *
  107. * @param string $path The path to store the file in.
  108. * @param string $name The filename to use.
  109. * @param string $tmpFile The temporary file containing the data to
  110. * be stored.
  111. * @param boolean $autocreate Automatically create directories?
  112. *
  113. * @return mixed True on success or a PEAR_Error object on failure.
  114. */
  115. function write($path, $name, $tmpFile, $autocreate = false)
  116. {
  117. if (is_a($this->_registry, 'PEAR_Error')) {
  118. return $this->_registry;
  119. }
  120. return PEAR::raiseError(_("Not supported."));
  121. }
  122. /**
  123. * Stores a file in the VFS from raw data.
  124. *
  125. * @abstract
  126. *
  127. * @param string $path The path to store the file in.
  128. * @param string $name The filename to use.
  129. * @param string $data The file data.
  130. * @param boolean $autocreate Automatically create directories?
  131. *
  132. * @return mixed True on success or a PEAR_Error object on failure.
  133. */
  134. function writeData($path, $name, $data, $autocreate = false)
  135. {
  136. if (is_a($this->_registry, 'PEAR_Error')) {
  137. return $this->_registry;
  138. }
  139. return PEAR::raiseError(_("Not supported."));
  140. }
  141. /**
  142. * Moves a file through the backend.
  143. *
  144. * @abstract
  145. *
  146. * @param string $path The path of the original file.
  147. * @param string $name The name of the original file.
  148. * @param string $dest The destination file name.
  149. *
  150. * @return mixed True on success or a PEAR_Error object on failure.
  151. */
  152. function move($path, $name, $dest)
  153. {
  154. if (is_a($this->_registry, 'PEAR_Error')) {
  155. return $this->_registry;
  156. }
  157. return PEAR::raiseError(_("Not supported."));
  158. }
  159. /**
  160. * Copies a file through the backend.
  161. *
  162. * @abstract
  163. *
  164. * @param string $path The path of the original file.
  165. * @param string $name The name of the original file.
  166. * @param string $dest The name of the destination directory.
  167. *
  168. * @return mixed True on success or a PEAR_Error object on failure.
  169. */
  170. function copy($path, $name, $dest)
  171. {
  172. if (is_a($this->_registry, 'PEAR_Error')) {
  173. return $this->_registry;
  174. }
  175. return PEAR::raiseError(_("Not supported."));
  176. }
  177. /**
  178. * Deletes a file from the VFS.
  179. *
  180. * @abstract
  181. *
  182. * @param string $path The path to delete the file from.
  183. * @param string $name The filename to delete.
  184. *
  185. * @return mixed True on success or a PEAR_Error object on failure.
  186. */
  187. function deleteFile($path, $name)
  188. {
  189. if (is_a($this->_registry, 'PEAR_Error')) {
  190. return $this->_registry;
  191. }
  192. return PEAR::raiseError(_("Not supported."));
  193. }
  194. /**
  195. * Renames a file in the VFS.
  196. *
  197. * @abstract
  198. *
  199. * @param string $oldpath The old path to the file.
  200. * @param string $oldname The old filename.
  201. * @param string $newpath The new path of the file.
  202. * @param string $newname The new filename.
  203. *
  204. * @return mixed True on success or a PEAR_Error object on failure.
  205. */
  206. function rename($oldpath, $oldname, $newpath, $newname)
  207. {
  208. if (is_a($this->_registry, 'PEAR_Error')) {
  209. return $this->_registry;
  210. }
  211. return PEAR::raiseError(_("Not supported."));
  212. }
  213. /**
  214. * Returns an an unsorted file list of the specified directory.
  215. *
  216. * @abstract
  217. *
  218. * @param string $path The path of the directory.
  219. * @param mixed $filter String/hash to filter file/dirname on.
  220. * @param boolean $dotfiles Show dotfiles?
  221. * @param boolean $dironly Show only directories?
  222. *
  223. * @return array File list on success or PEAR_Error on failure.
  224. */
  225. function _listFolder($path, $filter = null, $dotfiles = true,
  226. $dironly = false)
  227. {
  228. if (is_a($this->_registry, 'PEAR_Error')) {
  229. return $this->_registry;
  230. }
  231. $list = array();
  232. if ($path == '/') {
  233. $apps = $this->_registry->listApps(null, false, PERMS_READ);
  234. if (is_a($apps, 'PEAR_Error')) {
  235. return $apps;
  236. }
  237. foreach ($apps as $app) {
  238. if ($this->_registry->hasMethod('browse', $app)) {
  239. $file = array(
  240. //'name' => $this->_registry->get('name', $app),
  241. 'name' => $app,
  242. 'date' => time(),
  243. 'type' => '**dir',
  244. 'size' => -1
  245. );
  246. $list[] = $file;
  247. }
  248. }
  249. return $list;
  250. }
  251. if (substr($path, 0, 1) == '/') {
  252. $path = substr($path, 1);
  253. }
  254. $pieces = explode('/', $path);
  255. $items = $this->_registry->callByPackage($pieces[0], 'browse', array('path' => $path, 'properties' => array('name', 'browseable', 'contenttype', 'contentlength', 'modified')));
  256. if (is_a($items, 'PEAR_Error')) {
  257. return $items;
  258. }
  259. if (!is_array(reset($items))) {
  260. /* We return an object's content. */
  261. return PEAR::raiseError(_("unknown error"));
  262. }
  263. include_once 'Horde/MIME/Magic.php';
  264. foreach ($items as $sub_path => $i) {
  265. if ($dironly && !$i['browseable']) {
  266. continue;
  267. }
  268. $name = basename($sub_path);
  269. if ($this->_filterMatch($filter, $name)) {
  270. continue;
  271. }
  272. if (class_exists('MIME_Magic')) {
  273. $type = empty($i['contenttype']) ? 'application/octet-stream' : $i['contenttype'];
  274. $type = MIME_Magic::MIMEToExt($type);
  275. } else {
  276. $type = '**none';
  277. }
  278. $file = array(
  279. //'name' => $i['name'],
  280. 'name' => $name,
  281. 'date' => empty($i['modified']) ? 0 : $i['modified'],
  282. 'type' => $i['browseable'] ? '**dir' : $type,
  283. 'size' => empty($i['contentlength']) ? 0 : $i['contentlength']
  284. );
  285. $list[] = $file;
  286. }
  287. return $list;
  288. }
  289. /**
  290. * Returns a sorted list of folders in the specified directory.
  291. *
  292. * @abstract
  293. *
  294. * @param string $path The path of the directory to get the
  295. * directory list for.
  296. * @param mixed $filter Hash of items to filter based on folderlist.
  297. * @param boolean $dotfolders Include dotfolders?
  298. *
  299. * @return mixed Folder list on success or a PEAR_Error object on failure.
  300. */
  301. function listFolders($path = '', $filter = null, $dotfolders = true)
  302. {
  303. if (is_a($this->_registry, 'PEAR_Error')) {
  304. return $this->_registry;
  305. }
  306. return PEAR::raiseError(_("Not supported."));
  307. }
  308. }