VFS
[ class tree: VFS ] [ index: VFS ] [ all elements ]

Source for file sql_file.php

Documentation is available at sql_file.php

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

Documentation generated on Mon, 11 Mar 2019 14:31:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.