1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
<?php
/**
* Horde_VFS implementation for PHP's PEAR database abstraction layer.
*
* Required values for $params:
* 'phptype' The database type (ie. 'pgsql', 'mysql, etc.).
* 'hostspec' The hostname of the database server.
* 'protocol' The communication protocol ('tcp', 'unix', etc.).
* 'username' The username with which to connect to the database.
* 'password' The password associated with 'username'.
* 'database' The name of the database.
*
* Optional values:
* 'table' The name of the vfs table in 'database'. Defaults to 'horde_vfs'.
*
* Required by some database implementations:
* 'options' Additional options to pass to the database.
* 'tty' The TTY on which to connect to the database.
* 'port' The port on which to connect to the database.
*
* The table structure for the VFS can be found in
* horde/scripts/db/vfs.sql.
*
* Database specific notes:
*
* MSSQL:
* - The vfs_data field must be of type IMAGE.
* - You need the following php.ini settings:
* ; Valid range 0 - 2147483647. Default = 4096.
* mssql.textlimit = 0 ; zero to pass through
*
* ; Valid range 0 - 2147483647. Default = 4096.
* mssql.textsize = 0 ; zero to pass through
*
* $Horde: horde/lib/VFS/sql.php,v 1.32.2.4 2003/01/28 12:37:58 jan Exp $
*
* Copyright 2002-2003 Chuck Hagenbuch <chuck@horde.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @version $Revision: 1.32.2.4 $
* @since Horde 2.2
* @package horde.vfs
*/
// VFS File Types
/** @const HORDE_VFS_FILE File value for vfs_type column. */
define('HORDE_VFS_FILE', 1);
/** @const HORDE_VFS_FOLDER Folder value for vfs_type column. */
define('HORDE_VFS_FOLDER', 2);
class Horde_VFS_sql extends Horde_VFS {
/**
* Handle for the current database connection.
* @var $_db object DB
*/
var $_db;
/**
* Boolean indicating whether or not we're connected to the SQL
* server.
* @var $_connected boolean
*/
var $_connected = false;
/**
* Constructs a new SQL VFS object.
*
* @param array $params A hash containing connection parameters.
*/
function Horde_VFS_sql($params = array())
{
$this->_params = $params;
}
/**
* Retrieve a file from the VFS.
*
* @param string $path The pathname to the file.
* @param string $name The filename to retrieve.
*
* @return string The file data.
*/
function read($path, $name)
{
$conn = $this->_connect();
if (PEAR::isError($conn)) {
return $conn;
}
require_once dirname(__FILE__) . '/../SQL.php';
return Horde_SQL::readBlob($this->_db, $this->_params['table'], 'vfs_data',
array('vfs_path' => $path,
'vfs_name' => $name));
}
/**
* Store a file in the VFS.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $tmpFile The temporary file containing the data to be stored.
* @param boolean $autocreate (optional) Automatically create directories?
*
* @return mixed True on success or a PEAR_Error object on failure.
*/
function write($path, $name, $tmpFile, $autocreate = false)
{
$dataFP = @fopen($tmpFile, 'rb');
$data = @fread($dataFP, filesize($tmpFile));
fclose($dataFP);
return $this->writeData($path, $name, $data, $autocreate);
}
/**
* Store a file in the VFS from raw data.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $data The file data.
* @param boolean $autocreate (optional) Automatically create directories?
*
* @return mixed True on success or a PEAR_Error object on failure.
*/
function writeData($path, $name, $data, $autocreate = false)
{
$conn = $this->_connect();
if (PEAR::isError($conn)) {
return $conn;
}
/* Check to see if the data already exists. */
$query = sprintf('SELECT vfs_id FROM %s WHERE vfs_path %s AND vfs_name = %s',
$this->_params['table'],
(empty($path) && $this->_db->dbsyntax == 'oci8') ? ' IS NULL' : ' = ' . $this->_db->quote($path),
$this->_db->quote($name));
$id = $this->_db->getOne($query);
if (PEAR::isError($id)) {
return $id;
}
require_once dirname(__FILE__) . '/../SQL.php';
if ($id) {
return Horde_SQL::updateBlob($this->_db, $this->_params['table'], 'vfs_data',
$data, array('vfs_id' => $id),
array('vfs_modified' => time()));
} else {
$id = $this->_db->nextId($this->_params['table']);
if (PEAR::isError($id)) {
return $id;
}
return Horde_SQL::insertBlob($this->_db, $this->_params['table'], 'vfs_data',
$data, array('vfs_id' => $id,
'vfs_type' => HORDE_VFS_FILE,
'vfs_path' => $path,
'vfs_name' => $name,
'vfs_modified' => time(),
'vfs_owner' => Auth::getAuth()));
}
}
/**
* Delete a file from the VFS.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
*
* @return mixed True on success or a PEAR_Error object on failure.
*/
function deleteFile($path, $name)
{
$conn = $this->_connect();
if (PEAR::isError($conn)) {
return $conn;
}
$result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = %s AND vfs_path %s AND vfs_name = %s',
$this->_params['table'],
$this->_db->quote(HORDE_VFS_FILE),
(empty($path) && $this->_db->dbsyntax == 'oci8') ? ' IS NULL' : ' = ' . $this->_db->quote($path),
$this->_db->quote($name)));
if ($this->_db->affectedRows() == 0) {
return PEAR::raiseError('Unable to delete VFS file.');
}
return $result;
}
/**
* Rename a file or folder in the VFS.
*
* @param string $oldpath The old path to the file.
* @param string $oldname The old filename.
* @param string $newpath The new path of the file.
* @param string $newname The new filename.
*
* @return mixed True on success or a PEAR_Error object on failure.
*/
function rename($oldpath, $oldname, $newpath, $newname)
{
$conn = $this->_connect();
if (PEAR::isError($conn)) {
return $conn;
}
$result = $this->_db->query(sprintf('UPDATE %s SET vfs_path = %s, vfs_name = %s, vfs_modified = %s
WHERE vfs_path = %s AND vfs_name = %s',
$this->_params['table'],
$this->_db->quote($newpath),
$this->_db->quote($newname),
$this->_db->quote(time()),
$this->_db->quote($oldpath),
$this->_db->quote($oldname)));
if ($this->_db->affectedRows() == 0) {
return PEAR::raiseError('Unable to rename VFS file.');
}
$rename = $this->_recursiveRename($oldpath, $oldname, $newpath, $newname);
if (PEAR::isError($rename)) {
return PEAR::raiseError(sprintf('Unable to rename VFS directory: %s.', $rename->getMessage()));
}
return $result;
}
/**
* Renames all child paths.
*
* @param string $path The path of the folder to rename.
* @param string $name The foldername to use.
*
* @return mixed True on success or a PEAR_Error object on failure.
*/
function _recursiveRename($oldpath, $oldname, $newpath, $newname)
{
$folderList = $this->_db->getCol(sprintf('SELECT vfs_name FROM %s WHERE vfs_type = %s AND vfs_path = %s',
$this->_params['table'],
$this->_db->quote(HORDE_VFS_FOLDER),
$this->_db->quote($this->_getNativePath($oldpath, $oldname))));
foreach ($folderList as $folder) {
$this->_recursiveRename($this->_getNativePath($oldpath, $oldname), $folder, $this->_getNativePath($newpath, $newname), $folder);
}
$result = $this->_db->query(sprintf('UPDATE %s SET vfs_path = %s WHERE vfs_path = %s',
$this->_params['table'],
$this->_db->quote($this->_getNativePath($newpath, $newname)),
$this->_db->quote($this->_getNativePath($oldpath, $oldname))));
if (is_a($result)) {
return $result;
}
}
/**
* Creates a folder on the VFS.
*
* @param string $path Holds the path of directory to create folder.
* @param string $name Holds the name of the new folder.
*
* @return mixed True on success or a PEAR_Error object on failure.
*/
function createFolder($path, $name)
{
$conn = $this->_connect();
if (PEAR::isError($conn)) {
return $conn;
}
$id = $this->_db->nextId($this->_params['table']);
if (PEAR::isError($id)) {
return $id;
}
return $this->_db->query(sprintf('INSERT INTO %s (vfs_id, vfs_type, vfs_path, vfs_name, vfs_modified, vfs_owner)
VALUES (%s, %s, %s, %s, %s, %s)',
$this->_params['table'],
$this->_db->quote($id),
$this->_db->quote(HORDE_VFS_FOLDER),
$this->_db->quote($path),
$this->_db->quote($name),
$this->_db->quote(time()),
$this->_db->quote(Auth::getAuth())));
}
/**
* Delete a folder from the VFS.
*
* @param string $path The path of the folder.
* @param string $name The folername to use.
*
* @return mixed True on success or a PEAR_Error object on failure.
*/
function deleteFolder($path, $name)
{
$conn = $this->_connect();
if (PEAR::isError($conn)) {
return $conn;
}
$result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = %s AND vfs_path %s AND vfs_name = %s',
$this->_params['table'],
$this->_db->quote(HORDE_VFS_FOLDER),
(empty($path) && $this->_db->dbsyntax == 'oci8') ? ' IS NULL' : ' = ' . $this->_db->quote($path),
$this->_db->quote($name)));
if ($this->_db->affectedRows() == 0) {
return PEAR::raiseError('Unable to delete VFS directory.');
}
$delete = $this->_recursiveDelete($path, $name);
if (PEAR::isError($delete)) {
return PEAR::raiseError(sprintf('Unable to delete VFS recursively: %s.', $delete->getMessage()));
}
return $result;
}
/**
* Delete a folders contents from the VFS, recursively.
*
* @param string $path The path of the folder.
* @param string $name The foldername to use.
*
* @return mixed True on success or a PEAR_Error object on failure.
*/
function _recursiveDelete($path, $name)
{
$result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = %s AND vfs_path = %s',
$this->_params['table'],
$this->_db->quote(HORDE_VFS_FILE),
$this->_db->quote($this->_getNativePath($path, $name))));
if (PEAR::isError($result)) {
return $result;
}
$folderList = $this->_db->getCol(sprintf('SELECT vfs_name FROM %s WHERE vfs_type = %s AND vfs_path = %s',
$this->_params['table'],
$this->_db->quote(HORDE_VFS_FOLDER),
$this->_db->quote($this->_getNativePath($path, $name))));
foreach ($folderList as $folder) {
$this->_recursiveDelete($this->_getNativePath($path, $name), $folder);
}
$result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = %s AND vfs_name = %s AND vfs_path = %s',
$this->_params['table'],
$this->_db->quote(HORDE_VFS_FOLDER),
$this->_db->quote($name),
$this->_db->quote($path)));
return $result;
}
/**
* Return a full filename on the native filesystem, from a VFS
* path and name.
*
* @param string $path The VFS file path.
* @param string $name The VFS filename.
*
* @return string The full native filename.
*/
function _getNativePath($path, $name)
{
if (empty($path)) {
return $name;
}
return $path . DIRECTORY_SEPARATOR . $name;
}
/**
* Return a list of the contents of a folder.
*
* @param string $path Holds the path of the directory.
*
* @return mixed File list on success or false on failure.
*/
function listFolder($path)
{
$conn = $this->_connect();
if (PEAR::isError($conn)) {
return $conn;
}
$files = array();
$fileList = array();
// Fix for an ODD Oracle quirk.
if (empty($path) && $this->_db->dbsyntax == 'oci8') {
$where = 'vfs_path IS NULL';
} else {
$where = 'vfs_path = ' . $this->_db->quote($path);
}
$fileList = $this->_db->getAll(sprintf('SELECT vfs_name, vfs_type, vfs_data, vfs_modified, vfs_owner FROM %s
WHERE %s',
$this->_params['table'],
$where));
if (PEAR::isError($fileList)) {
return $fileList;
}
foreach ($fileList as $line) {
$file['name'] = $line[0];
if ($line[1] == HORDE_VFS_FILE) {
$name = explode('.', $line[0]);
if (count($name) == 1) {
$file['type'] = '**none';
} else {
$file['type'] = strtolower($name[count($name) - 1]);
}
$file['size'] = strlen($line[2]);
} else if ($line[1] == HORDE_VFS_FOLDER) {
$file['type'] = '**dir';
$file['size'] = -1;
}
$file['date'] = $line[3];
$file['owner'] = $line[4];
$file['perms'] = '-';
$file['group'] = '-';
$files[$file['name']] = $file;
}
return $files;
}
/**
* Attempts to open a persistent connection to the SQL server.
*
* @return mixed True on success or a PEAR_Error object on failure.
*/
function _connect()
{
if (!$this->_connected) {
if (!is_array($this->_params)) {
return PEAR::raiseError(_("No configuration information specified for SQL VFS."));
}
if (!isset($this->_params['phptype'])) {
return PEAR::raiseError(_("Required 'phptype' not specified in VFS configuration."));
}
if (!isset($this->_params['hostspec'])) {
return PEAR::raiseError(_("Required 'hostspec' not specified in VFS configuration."));
}
if (!isset($this->_params['username'])) {
return PEAR::raiseError(_("Required 'username' not specified in VFS configuration."));
}
if (!isset($this->_params['password'])) {
return PEAR::raiseError(_("Required 'password' not specified in VFS configuration."));
}
if (!isset($this->_params['database'])) {
return PEAR::raiseError(_("Required 'database' not specified in VFS configuration."));
}
if (!isset($this->_params['table'])) {
$this->_params['table'] = 'horde_vfs';
}
/* Connect to the SQL server using the supplied parameters. */
include_once 'DB.php';
$this->_db = &DB::connect($this->_params, true);
if (PEAR::isError($this->_db)) {
return $this->_db;
}
/* Enable the "portability" option. */
$this->_db->setOption('optimize', 'portability');
$this->_connected = true;
}
return true;
}
/**
* Disconnect from the SQL server and clean up the connection.
*
* @return boolean True on success, false on failure.
*/
function _disconnect()
{
if ($this->_connected) {
$this->_connected = false;
return $this->_db->disconnect();
}
return true;
}
}
|