[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/VFS/ -> sql_file.php (source)

   1  <?php
   2  
   3  /**
   4   * File value for vfs_type column.
   5   */
   6  define('VFS_FILE', 1);
   7  
   8  /**
   9   * Folder value for vfs_type column.
  10   */
  11  define('VFS_FOLDER', 2);
  12  
  13  /**
  14   * VFS:: implementation using PHP's PEAR database abstraction
  15   * layer and local file system for file storage.
  16   *
  17   * Required values for $params:<pre>
  18   *      'phptype'       The database type (ie. 'pgsql', 'mysql', etc.).
  19   *      'vfsroot'       The root directory of where the files should be
  20   *                      actually stored.</pre>
  21   *
  22   * Optional values:<pre>
  23   *      'table'         The name of the vfs table in 'database'. Defaults to
  24   *                      'horde_vfs'.</pre>
  25   *
  26   * Required by some database implementations:<pre>
  27   *      'hostspec'      The hostname of the database server.
  28   *      'protocol'      The communication protocol ('tcp', 'unix', etc.).
  29   *      'database'      The name of the database.
  30   *      'username'      The username with which to connect to the database.
  31   *      'password'      The password associated with 'username'.
  32   *      'options'       Additional options to pass to the database.
  33   *      'tty'           The TTY on which to connect to the database.
  34   *      'port'          The port on which to connect to the database.</pre>
  35   *
  36   * The table structure for the VFS can be found in
  37   * data/vfs.sql.
  38   *
  39   * $Horde: framework/VFS/VFS/sql_file.php,v 1.44.4.22 2006/05/31 04:50:02 slusarz Exp $
  40   *
  41   * @author  Michael Varghese <mike.varghese@ascellatech.com>
  42   * @package VFS
  43   */
  44  class VFS_sql_file extends VFS {
  45  
  46      /**
  47       * Handle for the current database connection.
  48       *
  49       * @var DB
  50       */
  51      var $_db = false;
  52  
  53      /**
  54       * Returns the size of a file.
  55       *
  56       * @access public
  57       *
  58       * @param string $path  The path of the file.
  59       * @param string $name  The filename.
  60       *
  61       * @return integer  The size of the file in bytes or PEAR_Error on
  62       *                  failure.
  63       */
  64      function size($path, $name)
  65      {
  66          if (($size = @filesize($this->_getNativePath($path, $name))) === false){
  67              return PEAR::raiseError(sprintf(_("Unable to check file size of \"%s/%s\".", $path, $name)));
  68          }
  69  
  70          return $size;
  71      }
  72  
  73      /**
  74       * Retrieve a file from the VFS.
  75       *
  76       * @param string $path  The pathname to the file.
  77       * @param string $name  The filename to retrieve.
  78       *
  79       * @return string  The file data.
  80       */
  81      function read($path, $name)
  82      {
  83          $conn = $this->_connect();
  84          if (is_a($conn, 'PEAR_Error')) {
  85              return $conn;
  86          }
  87  
  88          $file = $this->_getNativePath($path, $name);
  89  
  90          if (function_exists('file_get_contents')) {
  91              $data = file_get_contents($file);
  92          } else {
  93              $fp = @fopen($file, 'rb');
  94              if (!$fp) {
  95                  return PEAR::raiseError(_("Unable to open VFS file."));
  96              }
  97              $data = fread($fp, filesize($file));
  98              fclose($fp);
  99          }
 100  
 101          return $data;
 102      }
 103  
 104      /**
 105       * Store a file in the VFS, with the data copied from a temporary
 106       * file.
 107       *
 108       * @param string $path         The path to store the file in.
 109       * @param string $name         The filename to use.
 110       * @param string $tmpFile      The temporary file containing the data to be
 111       *                             stored.
 112       * @param boolean $autocreate  Automatically create directories?
 113       *
 114       * @return mixed  True on success or a PEAR_Error object on failure.
 115       */
 116      function write($path, $name, $tmpFile, $autocreate = false)
 117      {
 118          /* No need to check quota here as we will check it when we call
 119           * writeData(). */
 120  
 121          if (function_exists('file_get_contents')) {
 122              $data = file_get_contents($tmpFile);
 123          } else {
 124              $dataFP = @fopen($tmpFile, 'rb');
 125              $data = @fread($dataFP, filesize($tmpFile));
 126              fclose($dataFP);
 127          }
 128          return $this->writeData($path, $name, $data, $autocreate);
 129      }
 130  
 131      /**
 132       * Store a file in the VFS from raw data.
 133       *
 134       * @param string $path         The path to store the file in.
 135       * @param string $name         The filename to use.
 136       * @param string $data         The file data.
 137       * @param boolean $autocreate  Automatically create directories?
 138       *
 139       * @return mixed  True on success or a PEAR_Error object on failure.
 140       */
 141      function writeData($path, $name, $data, $autocreate = false)
 142      {
 143          $res = $this->_checkQuotaWrite('string', $data);
 144          if (is_a($res, 'PEAR_Error')) {
 145              return $res;
 146          }
 147  
 148          $fp = @fopen($this->_getNativePath($path, $name), 'w');
 149          if (!$fp) {
 150              if ($autocreate) {
 151                  $result = $this->autocreatePath($path);
 152                  if (is_a($result, 'PEAR_Error')) {
 153                      return $result;
 154                  }
 155                  $fp = @fopen($this->_getNativePath($path, $name), 'w');
 156                  if (!$fp) {
 157                      return PEAR::raiseError(_("Unable to open VFS file for writing."));
 158                  }
 159              } else {
 160                  return PEAR::raiseError(_("Unable to open VFS file for writing."));
 161              }
 162          }
 163  
 164          if (!@fwrite($fp, $data)) {
 165              return PEAR::raiseError(_("Unable to write VFS file data."));
 166          }
 167  
 168          if (is_a($this->_writeSQLData($path, $name, $autocreate), 'PEAR_Error')) {
 169              @unlink($this->_getNativePath($path, $name));
 170              return PEAR::raiseError(_("Unable to write VFS file data."));
 171          }
 172      }
 173  
 174      /**
 175       * Moves a file in the database and the file system.
 176       *
 177       * @param string $path  The path to store the file in.
 178       * @param string $name  The old filename.
 179       * @param string $dest  The new filename.
 180       *
 181       * @return mixed  True on success or a PEAR_Error object on failure.
 182       */
 183      function move($path, $name, $dest)
 184      {
 185          $orig = $this->_getNativePath($path, $name);
 186          if (preg_match('|^' . preg_quote($orig) . '/?$|', $dest)) {
 187              return PEAR::raiseError(_("Cannot move file(s) - destination is within source."));
 188          }
 189  
 190          $conn = $this->_connect();
 191          if (is_a($conn, 'PEAR_Error')) {
 192              return $conn;
 193          }
 194  
 195          $fileCheck = $this->listFolder($dest, null, false);
 196          foreach ($fileCheck as $file) {
 197              if ($file['name'] == $name) {
 198                  return PEAR::raiseError(_("Unable to move VFS file."));
 199              }
 200          }
 201  
 202          if (strpos($dest, $this->_getSQLNativePath($path, $name)) !== false) {
 203              return PEAR::raiseError(_("Unable to move VFS file."));
 204          }
 205  
 206          return $this->rename($path, $name, $dest, $name);
 207      }
 208  
 209      /**
 210       * Copies a file through the backend.
 211       *
 212       * @param string $path  The path to store the file in.
 213       * @param string $name  The filename to use.
 214       * @param string $dest  The destination of the file.
 215       *
 216       * @return mixed  True on success or a PEAR_Error object on failure.
 217       */
 218      function copy($path, $name, $dest)
 219      {
 220          $orig = $this->_getNativePath($path, $name);
 221          if (preg_match('|^' . preg_quote($orig) . '/?$|', $dest)) {
 222              return PEAR::raiseError(_("Cannot copy file(s) - source and destination are the same."));
 223          }
 224  
 225          $conn = $this->_connect();
 226          if (is_a($conn, 'PEAR_Error')) {
 227              return $conn;
 228          }
 229  
 230          $fileCheck = $this->listFolder($dest, null, false);
 231          foreach ($fileCheck as $file) {
 232              if ($file['name'] == $name) {
 233                  return PEAR::raiseError(_("Unable to copy VFS file."));
 234              }
 235          }
 236  
 237          if (strpos($dest, $this->_getSQLNativePath($path, $name)) !== false) {
 238              return PEAR::raiseError(_("Unable to copy VFS file."));
 239          }
 240  
 241          if (is_dir($orig)) {
 242              return $this->_recursiveCopy($path, $name, $dest);
 243          }
 244  
 245          if (!@copy($orig, $this->_getNativePath($dest, $name))) {
 246              return PEAR::raiseError(_("Unable to copy VFS file."));
 247          }
 248  
 249          $id = $this->_db->nextId($this->_params['table']);
 250  
 251          $query = sprintf('INSERT INTO %s (vfs_id, vfs_type, vfs_path, vfs_name, vfs_modified, vfs_owner) VALUES (?, ?, ?, ?, ?, ?)',
 252                           $this->_params['table']);
 253          $values = array($id, VFS_FILE, $dest, $name, time(), $this->_params['user']);
 254  
 255          $result = $this->_db->query($query, $values);
 256  
 257          if (is_a($result, 'PEAR_Error')) {
 258              unlink($this->_getNativePath($dest, $name));
 259              return $result;
 260          }
 261  
 262          return true;
 263      }
 264  
 265      /**
 266       * Creates a folder on the VFS.
 267       *
 268       * @param string $path  Holds the path of directory to create folder.
 269       * @param string $name  Holds the name of the new folder.
 270       *
 271       * @return mixed  True on success or a PEAR_Error object on failure.
 272       */
 273      function createFolder($path, $name)
 274      {
 275          $conn = $this->_connect();
 276          if (is_a($conn, 'PEAR_Error')) {
 277              return $conn;
 278          }
 279  
 280          $id = $this->_db->nextId($this->_params['table']);
 281          $result = $this->_db->query(sprintf('INSERT INTO %s (vfs_id, vfs_type, vfs_path, vfs_name, vfs_modified, vfs_owner)
 282                                              VALUES (?, ?, ?, ?, ?, ?)',
 283                                              $this->_params['table']),
 284                                      array($id, VFS_FOLDER, $path, $name, time(), $this->_params['user']));
 285          if (is_a($result, 'PEAR_Error')) {
 286              return $result;
 287          }
 288  
 289          if (!@mkdir($this->_getNativePath($path, $name))) {
 290              $result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_id = ?',
 291                                                  $this->_params['table']),
 292                                          array($id));
 293              return PEAR::raiseError(_("Unable to create VFS directory."));
 294          }
 295  
 296          return true;
 297      }
 298  
 299      /**
 300       * Rename a file or folder in the VFS.
 301       *
 302       * @param string $oldpath  The old path to the file.
 303       * @param string $oldname  The old filename.
 304       * @param string $newpath  The new path of the file.
 305       * @param string $newname  The new filename.
 306       *
 307       * @return mixed  True on success or a PEAR_Error object on failure.
 308       */
 309      function rename($oldpath, $oldname, $newpath, $newname)
 310      {
 311          $conn = $this->_connect();
 312          if (is_a($conn, 'PEAR_Error')) {
 313              return $conn;
 314          }
 315  
 316          if (strpos($newpath, '/') === false) {
 317              $parent = '';
 318              $path = $newpath;
 319          } else {
 320              list($parent, $path) = explode('/', $newpath, 2);
 321          }
 322          if (!$this->isFolder($parent, $path)) {
 323              if (is_a($result = $this->autocreatePath($newpath), 'PEAR_Error')) {
 324                  return $result;
 325              }
 326          }
 327  
 328          $result = $this->_db->query(sprintf('UPDATE %s SET vfs_path = ?, vfs_name = ?, vfs_modified = ?
 329                                              WHERE vfs_path = ? AND vfs_name = ?',
 330                                              $this->_params['table']),
 331                                      array($newpath, $newname, time(), $oldpath, $oldname));
 332  
 333          if ($this->_db->affectedRows() == 0) {
 334              return PEAR::raiseError(_("Unable to rename VFS file."));
 335          }
 336  
 337          if (is_a($this->_recursiveSQLRename($oldpath, $oldname, $newpath, $newname), 'PEAR_Error')) {
 338              $result = $this->_db->query(sprintf('UPDATE %s SET vfs_path = ?, vfs_name = ?
 339                                                  WHERE vfs_path = ? AND vfs_name = ?',
 340                                                  $this->_params['table']),
 341                                          array($oldpath, $oldname, $newpath, $newname));
 342              return PEAR::raiseError(_("Unable to rename VFS directory."));
 343          }
 344  
 345          if (!@is_dir($this->_getNativePath($newpath))) {
 346              if (is_a($res = $this->autocreatePath($newpath), 'PEAR_Error')) {
 347                  return $res;
 348              }
 349          }
 350  
 351          if (!@rename($this->_getNativePath($oldpath, $oldname), $this->_getNativePath($newpath, $newname))) {
 352              $result = $this->_db->query(sprintf('UPDATE %s SET vfs_path = ?, vfs_name = ?
 353                                                  WHERE vfs_path = ? AND vfs_name = ?',
 354                                                  $this->_params['table']),
 355                                          array($oldpath, $oldname, $newpath, $newname));
 356              return PEAR::raiseError(_("Unable to rename VFS file."));
 357          }
 358  
 359          return true;
 360      }
 361  
 362      /**
 363       * Delete a folder from the VFS.
 364       *
 365       * @param string $path        The path to delete the folder from.
 366       * @param string $name        The foldername to use.
 367       * @param boolean $recursive  Force a recursive delete?
 368       *
 369       * @return mixed  True on success or a PEAR_Error object on failure.
 370       */
 371      function deleteFolder($path, $name, $recursive = false)
 372      {
 373          $conn = $this->_connect();
 374          if (is_a($conn, 'PEAR_Error')) {
 375              return $conn;
 376          }
 377  
 378          if ($recursive) {
 379              $result = $this->emptyFolder($path . '/' . $name);
 380              if (is_a($result, 'PEAR_Error')) {
 381                  return $result;
 382              }
 383          } else {
 384              $list = $this->listFolder($path . '/' . $name);
 385              if (is_a($list, 'PEAR_Error')) {
 386                  return $list;
 387              }
 388              if (count($list)) {
 389                  return PEAR::raiseError(sprintf(_("Unable to delete %s, the directory is not empty"),
 390                                                  $path . '/' . $name));
 391              }
 392          }
 393  
 394          $result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = ? AND vfs_path = ? AND vfs_name = ?',
 395                                              $this->_params['table']),
 396                                      array(VFS_FOLDER, $path, $name));
 397  
 398          if ($this->_db->affectedRows() == 0 || is_a($result, 'PEAR_Error')) {
 399              return PEAR::raiseError(_("Unable to delete VFS directory."));
 400          }
 401  
 402          if (is_a($this->_recursiveSQLDelete($path, $name), 'PEAR_Error')) {
 403              return PEAR::raiseError(_("Unable to delete VFS directory recursively."));
 404          }
 405  
 406          if (is_a($this->_recursiveLFSDelete($path, $name), 'PEAR_Error')) {
 407              return PEAR::raiseError(_("Unable to delete VFS directory recursively."));
 408          }
 409  
 410          return $result;
 411      }
 412  
 413      /**
 414       * Delete a file from the VFS.
 415       *
 416       * @param string $path  The path to store the file in.
 417       * @param string $name  The filename to use.
 418       *
 419       * @return mixed  True on success or a PEAR_Error object on failure.
 420       */
 421      function deleteFile($path, $name)
 422      {
 423          $res = $this->_checkQuotaDelete($path, $name);
 424          if (is_a($res, 'PEAR_Error')) {
 425              return $res;
 426          }
 427  
 428          $conn = $this->_connect();
 429          if (is_a($conn, 'PEAR_Error')) {
 430              return $conn;
 431          }
 432  
 433          $result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = ? AND vfs_path = ? AND vfs_name = ?',
 434                                              $this->_params['table']),
 435                                      array(VFS_FILE, $path, $name));
 436  
 437          if ($this->_db->affectedRows() == 0) {
 438              return PEAR::raiseError(_("Unable to delete VFS file."));
 439          }
 440  
 441          if (is_a($result, 'PEAR_Error')) {
 442              return $result;
 443          }
 444  
 445          if (!@unlink($this->_getNativePath($path, $name))) {
 446              return PEAR::raiseError(_("Unable to delete VFS file."));
 447          }
 448  
 449          return true;
 450      }
 451  
 452      /**
 453       * Return a list of the contents of a folder.
 454       *
 455       * @param string $path       The directory path.
 456       * @param mixed $filter      String/hash of items to filter based on
 457       *                           filename.
 458       * @param boolean $dotfiles  Show dotfiles?
 459       * @param boolean $dironly   Show directories only?
 460       *
 461       * @return mixed  File list on success or false on failure.
 462       */
 463      function _listFolder($path, $filter = null, $dotfiles = true,
 464                          $dironly = false)
 465      {
 466          $conn = $this->_connect();
 467          if (is_a($conn, 'PEAR_Error')) {
 468              return $conn;
 469          }
 470  
 471          $files = array();
 472          $fileList = array();
 473  
 474          $fileList = $this->_db->getAll(sprintf('SELECT vfs_name, vfs_type, vfs_modified, vfs_owner FROM %s
 475                                                 WHERE vfs_path = ?',
 476                                                 $this->_params['table']),
 477                                         array($path));
 478          if (is_a($fileList, 'PEAR_Error')) {
 479              return $fileList;
 480          }
 481  
 482          foreach ($fileList as $line) {
 483              // Filter out dotfiles if they aren't wanted.
 484              if (!$dotfiles && substr($line[0], 0, 1) == '.') {
 485                  continue;
 486              }
 487  
 488              $file['name'] = $line[0];
 489  
 490              if ($line[1] == VFS_FILE) {
 491                  $name = explode('.', $line[0]);
 492  
 493                  if (count($name) == 1) {
 494                      $file['type'] = '**none';
 495                  } else {
 496                      $file['type'] = VFS::strtolower($name[count($name) - 1]);
 497                  }
 498  
 499                  $file['size'] = filesize($this->_getNativePath($path, $line[0]));
 500              } elseif ($line[1] == VFS_FOLDER) {
 501                  $file['type'] = '**dir';
 502                  $file['size'] = -1;
 503              }
 504  
 505              $file['date'] = $line[2];
 506              $file['owner'] = $line[3];
 507              $file['perms'] = '';
 508              $file['group'] = '';
 509  
 510              // Filtering.
 511              if ($this->_filterMatch($filter, $file['name'])) {
 512                  unset($file);
 513                  continue;
 514              }
 515              if ($dironly && $file['type'] !== '**dir') {
 516                  unset($file);
 517                  continue;
 518              }
 519  
 520              $files[$file['name']] = $file;
 521              unset($file);
 522          }
 523  
 524          return $files;
 525      }
 526  
 527      /**
 528       * Returns a sorted list of folders in specified directory.
 529       *
 530       * @param string $path         The path of the directory to get the
 531       *                             directory list for.
 532       * @param mixed $filter        String/hash of items to filter based on
 533       *                             folderlist.
 534       * @param boolean $dotfolders  Include dotfolders?
 535       *
 536       * @return mixed  Folder list on success or a PEAR_Error object on failure.
 537       */
 538      function listFolders($path = '', $filter = array(), $dotfolders = true)
 539      {
 540          $conn = $this->_connect();
 541          if (is_a($conn, 'PEAR_Error')) {
 542              return $conn;
 543          }
 544  
 545          $sql = sprintf('SELECT vfs_name, vfs_path FROM %s WHERE vfs_path = ? AND vfs_type = ?',
 546                         $this->_params['table']);
 547  
 548          $folderList = $this->_db->getAll($sql, array($path, $VFS_FOLDER));
 549          if (is_a($folderList, 'PEAR_Error')) {
 550              return $folderList;
 551          }
 552  
 553          $folders = array();
 554          foreach ($folderList as $line) {
 555              $folder['val'] = $this->_getNativePath($line[1], $line[0]);
 556              $folder['abbrev'] = '';
 557              $folder['label'] = '';
 558  
 559              $count = substr_count($folder['val'], '/');
 560  
 561              $x = 0;
 562              while ($x < $count) {
 563                  $folder['abbrev'] .= '    ';
 564                  $folder['label'] .= '    ';
 565                  $x++;
 566              }
 567  
 568              $folder['abbrev'] .= $line[0];
 569              $folder['label'] .= $line[0];
 570  
 571              $strlen = VFS::strlen($folder['label']);
 572              if ($strlen > 26) {
 573                  $folder['abbrev'] = substr($folder['label'], 0, ($count * 4));
 574                  $length = (29 - ($count * 4)) / 2;
 575                  $folder['abbrev'] .= substr($folder['label'], ($count * 4), $length);
 576                  $folder['abbrev'] .= '...';
 577                  $folder['abbrev'] .= substr($folder['label'], -1 * $length, $length);
 578              }
 579  
 580              $found = false;
 581              foreach ($filter as $fltr) {
 582                  if ($folder['val'] == $fltr) {
 583                      $found = true;
 584                  }
 585              }
 586  
 587              if (!$found) {
 588                  $folders[$folder['val']] = $folder;
 589              }
 590          }
 591  
 592          ksort($folders);
 593          return $folders;
 594      }
 595  
 596      /**
 597       * Recursively copies the contents of a folder to a destination.
 598       *
 599       * @access private
 600       *
 601       * @param string $path  The path to store the directory in.
 602       * @param string $name  The name of the directory.
 603       * @param string $dest  The destination of the directory.
 604       *
 605       * @return mixed  True on success or a PEAR_Error object on failure.
 606       */
 607      function _recursiveCopy($path, $name, $dest)
 608      {
 609          $result = $this->createFolder($dest, $name);
 610  
 611          if (is_a($result, 'PEAR_Error')) {
 612              return $result;
 613          }
 614  
 615          $file_list = $this->listFolder($this->_getSQLNativePath($path, $name));
 616  
 617          foreach ($file_list as $file) {
 618              $result = $this->copy($this->_getSQLNativePath($path, $name), $file['name'], $this->_getSQLNativePath($dest, $name));
 619  
 620              if (is_a($result, 'PEAR_Error')) {
 621                  return $result;
 622              }
 623          }
 624          return true;
 625       }
 626  
 627      /**
 628       * Store a files information within the database.
 629       *
 630       * @access private
 631       *
 632       * @param string $path         The path to store the file in.
 633       * @param string $name         The filename to use.
 634       * @param boolean $autocreate  Automatically create directories?
 635       *
 636       * @return mixed  True on success or a PEAR_Error object on failure.
 637       */
 638      function _writeSQLData($path, $name, $autocreate = false)
 639      {
 640          $conn = $this->_connect();
 641          if (is_a($conn, 'PEAR_Error')) {
 642              return $conn;
 643          }
 644  
 645          // File already exists in database
 646          if ($this->exists($path, $name)) {
 647              $query = 'UPDATE ' . $this->_params['table'] .
 648                       ' SET vfs_modified = ?' .
 649                       ' WHERE vfs_path = ? AND vfs_name = ?';
 650              $values = array(time(), $path, $name);
 651          } else {
 652              $id = $this->_db->nextId($this->_params['table']);
 653  
 654              $query = 'INSERT INTO ' . $this->_params['table'] .
 655                       ' (vfs_id, vfs_type, vfs_path, vfs_name, vfs_modified,' .
 656                       ' vfs_owner) VALUES (?, ?, ?, ?, ?, ?)';
 657              $values = array($id, VFS_FILE, $path, $name, time(),
 658                              $this->_params['user']);
 659          }
 660          return $this->_db->query($query, $values);
 661      }
 662  
 663      /**
 664       * Renames all child paths.
 665       *
 666       * @access private
 667       *
 668       * @param string $oldpath  The old path of the folder to rename.
 669       * @param string $oldname  The old name.
 670       * @param string $newpath  The new path of the folder to rename.
 671       * @param string $newname  The new name.
 672       *
 673       * @return mixed  True on success or a PEAR_Error object on failure.
 674       */
 675      function _recursiveSQLRename($oldpath, $oldname, $newpath, $newname)
 676      {
 677          $folderList = $this->_db->getCol(sprintf('SELECT vfs_name FROM %s WHERE vfs_type = ? AND vfs_path = ?',
 678                                                   $this->_params['table']),
 679                                           0,
 680                                           array(VFS_FOLDER, $this->_getSQLNativePath($oldpath, $oldname)));
 681  
 682          foreach ($folderList as $folder) {
 683              $this->_recursiveSQLRename($this->_getSQLNativePath($oldpath, $oldname), $folder, $this->_getSQLNativePath($newpath, $newname), $folder);
 684          }
 685  
 686          $result = $this->_db->query(sprintf('UPDATE %s SET vfs_path = ? WHERE vfs_path = ?',
 687                                              $this->_params['table']),
 688                                      array($this->_getSQLNativePath($newpath, $newname),
 689                                            $this->_getSQLNativePath($oldpath, $oldname)));
 690  
 691          if (is_a($result, 'PEAR_Error')) {
 692              return $result;
 693          }
 694      }
 695  
 696      /**
 697       * Delete a folders contents from the VFS in the SQL database,
 698       * recursively.
 699       *
 700       * @access private
 701       *
 702       * @param string $path  The path of the folder.
 703       * @param string $name  The foldername to use.
 704       *
 705       * @return mixed  True on success or a PEAR_Error object on failure.
 706       */
 707      function _recursiveSQLDelete($path, $name)
 708      {
 709          $result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = ? AND vfs_path = ?',
 710                                              $this->_params['table']),
 711                                      array(VFS_FILE, $this->_getSQLNativePath($path, $name)));
 712          if (is_a($result, 'PEAR_Error')) {
 713              return $result;
 714          }
 715  
 716          $folderList = $this->_db->getCol(sprintf('SELECT vfs_name FROM %s WHERE vfs_type = ? AND vfs_path = ?',
 717                                                   $this->_params['table']),
 718                                           0,
 719                                           array(VFS_FOLDER, $this->_getSQLNativePath($path, $name)));
 720  
 721          foreach ($folderList as $folder) {
 722              $this->_recursiveSQLDelete($this->_getSQLNativePath($path, $name), $folder);
 723          }
 724  
 725          $result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = ? AND vfs_name = ? AND vfs_path = ?',
 726                                              $this->_params['table']),
 727                                      array(VFS_FOLDER, $name, $path));
 728  
 729          return $result;
 730      }
 731  
 732      /**
 733       * Delete a folders contents from the VFS, recursively.
 734       *
 735       * @access private
 736       *
 737       * @param string $path  The path of the folder.
 738       * @param string $name  The foldername to use.
 739       *
 740       * @return mixed  True on success or a PEAR_Error object on failure.
 741       */
 742      function _recursiveLFSDelete($path, $name)
 743      {
 744          $dir = $this->_getNativePath($path, $name);
 745          $dh = @opendir($dir);
 746  
 747          while (false !== ($file = readdir($dh))) {
 748              if ($file != '.' && $file != '..') {
 749                  if (is_dir($dir . '/' . $file)) {
 750                      $this->_recursiveLFSDelete(empty($path) ? $name : $path . '/' . $name, $file);
 751                  } else {
 752                      @unlink($dir . '/' . $file);
 753                  }
 754              }
 755          }
 756          @closedir($dh);
 757  
 758          return rmdir($dir);
 759      }
 760  
 761      /**
 762       * Attempts to open a persistent connection to the SQL server.
 763       *
 764       * @access private
 765       *
 766       * @return mixed  True on success or a PEAR_Error object on failure.
 767       */
 768      function _connect()
 769      {
 770          if ($this->_db === false) {
 771              if (!is_array($this->_params)) {
 772                  return PEAR::raiseError(_("No configuration information specified for SQL-File VFS."));
 773              }
 774  
 775              $required = array('phptype', 'vfsroot');
 776              foreach ($required as $val) {
 777                  if (!isset($this->_params[$val])) {
 778                      return PEAR::raiseError(sprintf(_("Required \"%s\" not specified in VFS configuration."), $val));
 779                  }
 780              }
 781  
 782              if (!isset($this->_params['database'])) {
 783                  $this->_params['database'] = '';
 784              }
 785  
 786              if (!isset($this->_params['username'])) {
 787                  $this->_params['username'] = '';
 788              }
 789  
 790              if (!isset($this->_params['hostspec'])) {
 791                  $this->_params['hostspec'] = '';
 792              }
 793  
 794              if (!isset($this->_params['table'])) {
 795                  $this->_params['table'] = 'horde_vfs';
 796              }
 797  
 798              /* Connect to the SQL server using the supplied parameters. */
 799              require_once 'DB.php';
 800              $this->_db = &DB::connect($this->_params,
 801                                        array('persistent' => !empty($this->_params['persistent'])));
 802              if (is_a($this->_db, 'PEAR_Error')) {
 803                  $error = $this->_db;
 804                  $this->_db = false;
 805                  return $error;
 806              }
 807  
 808              // Set DB portability options.
 809              switch ($this->_db->phptype) {
 810              case 'mssql':
 811                  $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
 812                  break;
 813              default:
 814                  $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
 815              }
 816  
 817          }
 818  
 819          return true;
 820      }
 821  
 822      /**
 823       * Disconnect from the SQL server and clean up the connection.
 824       *
 825       * @access private
 826       */
 827      function _disconnect()
 828      {
 829          if ($this->_db) {
 830              $this->_db->disconnect();
 831              $this->_db = false;
 832          }
 833      }
 834  
 835      /**
 836       * Return a full filename on the native filesystem, from a VFS
 837       * path and name.
 838       *
 839       * @access private
 840       *
 841       * @param string $path  The VFS file path.
 842       * @param string $name  The VFS filename.
 843       *
 844       * @return string  The full native filename.
 845       */
 846      function _getNativePath($path, $name)
 847      {
 848          if (!empty($name)) {
 849              $name = '/' . $name;
 850          }
 851          if (isset($path)) {
 852              if (isset($this->_params['home']) &&
 853                  preg_match('|^~/?(.*)$|', $path, $matches)) {
 854                  $path = $this->_params['home']  . '/' . $matches[1];
 855              }
 856  
 857              return $this->_params['vfsroot'] . '/' . $path . $name;
 858          } else {
 859              return $this->_params['vfsroot'] . $name;
 860          }
 861      }
 862  
 863      /**
 864       * Return a full SQL filename on the native filesystem, from a VFS
 865       * path and name.
 866       *
 867       * @access private
 868       *
 869       * @param string $path  The VFS file path.
 870       * @param string $name  The VFS filename.
 871       *
 872       * @return string  The full native filename.
 873       */
 874      function _getSQLNativePath($path, $name)
 875      {
 876          if (empty($path)) {
 877              return $name;
 878          }
 879  
 880          return $path . '/' . $name;
 881      }
 882  
 883  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7