1: <?php
2: /**
3: * A wrapper for the VFS class to return objects, instead of arrays.
4: *
5: * Copyright 2002-2007 Jon Wood <jon@jellybob.co.uk>
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Jon Wood <jon@jellybob.co.uk>
11: * @package Vfs
12: */
13: class Horde_Vfs_Object
14: {
15: /**
16: * The actual vfs object that does the work.
17: *
18: * @var VFS
19: */
20: protected $_vfs;
21:
22: /**
23: * The current path that has been passed to listFolder(), if this
24: * changes, the list will be rebuilt.
25: *
26: * @var string
27: */
28: protected $_currentPath;
29:
30: /**
31: * The return value from a standard VFS listFolder() call, to
32: * be read with the Object listFolder().
33: *
34: * @var array
35: */
36: protected $_folderList;
37:
38: /**
39: * Constructor.
40: *
41: * If you pass in an existing VFS object, it will be used as the VFS
42: * object for this object.
43: *
44: * @param VFS $vfs The VFS object to wrap.
45: */
46: public function __construct($vfs)
47: {
48: if (isset($vfs)) {
49: $this->_vfs = $vfs;
50: }
51: }
52:
53: /**
54: * Check the credentials that we have to see if there is a valid login.
55: *
56: * @throws Horde_Vfs_Exception;
57: */
58: public function checkCredentials()
59: {
60: $this->_vfs->checkCredentials();
61: }
62:
63: /**
64: * Set configuration parameters.
65: *
66: * @param array $params An associative array of parameter name/value
67: * pairs.
68: */
69: public function setParams($params = array())
70: {
71: $this->_vfs->setParams($params);
72: }
73:
74: /**
75: * Retrieve a file from the VFS.
76: *
77: * @param string $path The pathname to the file.
78: *
79: * @return string The file data.
80: * @throws Horde_Vfs_Exception
81: */
82: public function read($path)
83: {
84: return $this->_vfs->read(dirname($path), basename($path));
85: }
86:
87: /**
88: * Store a file in the VFS.
89: *
90: * @param string $path The path to store the file in.
91: * @param string $tmpFile The temporary file containing the data to be
92: * stored.
93: * @param boolean $autocreate Automatically create directories?
94: *
95: * @throws Horde_Vfs_Exception
96: */
97: public function write($path, $tmpFile, $autocreate = false)
98: {
99: $this->_vfs->write(dirname($path), basename($path), $tmpFile, $autocreate);
100: }
101:
102: /**
103: * Store a file in the VFS from raw data.
104: *
105: * @param string $path The path to store the file in.
106: * @param string $data The file data.
107: * @param boolean $autocreate Automatically create directories?
108: *
109: * @throws Horde_Vfs_Exception
110: */
111: public function writeData($path, $data, $autocreate = false)
112: {
113: $this->_vfs->writeData(dirname($path), basename($path), $data, $autocreate);
114: }
115:
116: /**
117: * Delete a file from the VFS.
118: *
119: * @param string $path The path to store the file in.
120: * @param string $name The filename to use.
121: *
122: * @throws Horde_Vfs_Exception
123: */
124: public function deleteFile($path)
125: {
126: $this->_vfs->deleteFile(dirname($path), basename($path));
127: }
128:
129: /**
130: * Rename a file in the VFS.
131: *
132: * @param string $oldpath The old path to the file.
133: * @param string $oldname The old filename.
134: * @param string $newpath The new path of the file.
135: * @param string $newname The new filename.
136: *
137: * @throws Horde_Vfs_Exception
138: */
139: public function rename($oldpath, $newpath)
140: {
141: return $this->_vfs->rename(dirname($oldpath), basename($oldpath), dirname($newpath), basename($newpath));
142: }
143:
144: /**
145: * Create a folder in the VFS.
146: *
147: * @param string $path The path to the folder.
148: *
149: * @throws Horde_Vfs_Exception
150: */
151: public function createFolder($path)
152: {
153: $this->_vfs->createFolder(dirname($path));
154: }
155:
156: /**
157: * Deletes a folder from the VFS.
158: *
159: * @param string $path The path of the folder to delete.
160: *
161: * @throws Horde_Vfs_Exception
162: */
163: public function deleteFolder($path)
164: {
165: $this->_vfs->deleteFolder(dirname($path));
166: }
167:
168: /**
169: * Returns a Horde_Vfs_ListItem object if the folder can
170: * be read, or a PEAR_Error if it can't be. Returns false once
171: * the folder has been completely read.
172: *
173: * @param string $path The path of the diretory.
174: *
175: * @return mixed File list (array) on success or false if the folder is
176: * completely read.
177: * @throws Horde_Vfs_Exception
178: */
179: public function listFolder($path)
180: {
181: if (!($path === $this->_currentPath)) {
182: $folderList = $this->_vfs->listFolder($path);
183: if ($folderList) {
184: $this->_folderList = $folderList;
185: $this->_currentPath = $path;
186: } else {
187: throw new Horde_Vfs_Exception('Could not read ' . $path . '.');
188: }
189: }
190:
191: return ($file = array_shift($this->_folderList))
192: ? new Horde_Vfs_ListItem($path, $file)
193: : false;
194: }
195:
196: /**
197: * Changes permissions for an Item on the VFS.
198: *
199: * @param string $path Holds the path of directory of the Item.
200: * @param string $permission TODO
201: *
202: * @throws Horde_Vfs_Exception
203: */
204: public function changePermissions($path, $permission)
205: {
206: $this->_vfs->changePermissions(dirname($path), basename($path), $permission);
207: }
208:
209: /**
210: * Return the list of additional credentials required, if any.
211: *
212: * @return array Credential list.
213: */
214: public function getRequiredCredentials()
215: {
216: return $this->_vfs->getRequiredCredentials();
217: }
218:
219: /**
220: * Return the array specificying what permissions are changeable for this
221: * implementation.
222: *
223: * @return array Changeable permisions.
224: */
225: public function getModifiablePermissions()
226: {
227: return $this->_vfs->getModifiablePermissions();
228: }
229:
230: }
231: