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

Source for file VFS.php

Documentation is available at VFS.php

  1. <?php
  2.  
  3. require_once 'PEAR.php';
  4. require_once 'Log.php';
  5.  
  6. /**
  7.  * VFS API for abstracted file storage and access.
  8.  *
  9.  * $Horde: framework/VFS/VFS.php,v 1.72 2005/01/30 23:53:51 jan Exp $
  10.  *
  11.  * Copyright 2002-2005 Chuck Hagenbuch <chuck@horde.org>
  12.  *
  13.  * See the enclosed file COPYING for license information (LGPL). If you did
  14.  * not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  15.  *
  16.  * @author  Chuck Hagenbuch <chuck@horde.org>
  17.  * @package VFS
  18.  * @since   Horde 2.2
  19.  */
  20. class VFS {
  21.  
  22.     /**
  23.      * Hash containing connection parameters.
  24.      *
  25.      * @var array $_params 
  26.      */
  27.     var $_params = array();
  28.  
  29.     /**
  30.      * List of additional credentials required for this VFS backend (example:
  31.      * For FTP, we need a username and password to log in to the server with).
  32.      *
  33.      * @var array $_credentials 
  34.      */
  35.     var $_credentials = array();
  36.  
  37.     /**
  38.      * List of permissions and if they can be changed in this VFS backend.
  39.      *
  40.      * @var array $_permissions 
  41.      */
  42.     var $_permissions = array(
  43.         'owner' => array('read' => false'write' => false'execute' => false),
  44.         'group' => array('read' => false'write' => false'execute' => false),
  45.         'all'   => array('read' => false'write' => false'execute' => false));
  46.  
  47.     /**
  48.      * A PEAR Log object. If present, will be used to log errors and
  49.      * informational messages about VFS activity.
  50.      *
  51.      * @var Log $_logger 
  52.      */
  53.     var $_logger = null;
  54.  
  55.     /**
  56.      * The log level to use - messages with a higher log level than configured
  57.      * here will not be logged. Defaults to only logging errors or higher.
  58.      *
  59.      * @var integer $_logLevel 
  60.      */
  61.     var $_logLevel = PEAR_LOG_ERR;
  62.  
  63.     /**
  64.      * Constructor.
  65.      *
  66.      * @access public
  67.      *
  68.      * @param array $params  A hash containing connection parameters.
  69.      */
  70.     function VFS($params = array())
  71.     {
  72.         if (empty($params['user'])) {
  73.             $params['user''';
  74.         }
  75.         $this->_params $params;
  76.     }
  77.  
  78.     /**
  79.      * Checks the credentials that we have by calling _connect(), to see if
  80.      * there is a valid login.
  81.      *
  82.      * @access public
  83.      *
  84.      * @return mixed  True on success, PEAR_Error describing the problem if the
  85.      *                 credentials are invalid.
  86.      */
  87.     function checkCredentials()
  88.     {
  89.         return $this->_connect();
  90.     }
  91.  
  92.     /**
  93.      * Sets configuration parameters.
  94.      *
  95.      * @access public
  96.      *
  97.      * @param array $params  An associative array with parameter names as keys.
  98.      */
  99.     function setParams($params = array())
  100.     {
  101.         foreach ($params as $name => $value{
  102.             $this->_params[$name$value;
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * Returns configuration parameters.
  108.      *
  109.      * @access public
  110.      *
  111.      * @param string $name  The parameter to return.
  112.      *
  113.      * @return mixed  The parameter value or null if it doesn't exist.
  114.      */
  115.     function getParam($name)
  116.     {
  117.         return isset($this->_params[$name]$this->_params[$name: null;
  118.     }
  119.  
  120.     /**
  121.      * Logs a message if a PEAR Log object is available, and the message's
  122.      * priority is lower than or equal to the configured log level.
  123.      *
  124.      * @param mixed   $message   The message to be logged.
  125.      * @param integer $priority  The message's priority.
  126.      */
  127.     function log($message$priority = PEAR_LOG_ERR)
  128.     {
  129.         if (!isset($this->_logger|| $priority $this->_logLevel{
  130.             return;
  131.         }
  132.  
  133.         if (is_a($message'PEAR_Error')) {
  134.             $userinfo $message->getUserInfo();
  135.             $message $message->getMessage();
  136.             if ($userinfo{
  137.                 if (is_array($userinfo)) {
  138.                     $userinfo implode(', '$userinfo);
  139.                 }
  140.                 $message .= ': ' $userinfo;
  141.             }
  142.         }
  143.  
  144.         /* Make sure to log in the system's locale. */
  145.         $locale setlocale(LC_TIME0);
  146.         setlocale(LC_TIME'C');
  147.  
  148.         $this->_logger->log($message$priority);
  149.  
  150.         /* Restore original locale. */
  151.         setlocale(LC_TIME$locale);
  152.     }
  153.  
  154.     /**
  155.      * Sets the PEAR Log object used to log informational or error messages.
  156.      *
  157.      * @param Log &$logger  The Log object to use.
  158.      */
  159.     function setLogger(&$logger$logLevel = null)
  160.     {
  161.         if (!is_a($logger'Log')) {
  162.             return false;
  163.         }
  164.  
  165.         $this->_logger &$logger;
  166.         if (!is_null($logLevel)) {
  167.             $this->_logLevel $logLevel;
  168.         }
  169.     }
  170.  
  171.     /**
  172.      * Retrieves a file from the VFS.
  173.      *
  174.      * @access public
  175.      * @abstract
  176.      *
  177.      * @param string $path  The pathname to the file.
  178.      * @param string $name  The filename to retrieve.
  179.      *
  180.      * @return string The file data.
  181.      */
  182.     function read($path$name)
  183.     {
  184.         return PEAR::raiseError(_("Not supported."));
  185.     }
  186.  
  187.     /**
  188.      * Stores a file in the VFS.
  189.      *
  190.      * @access public
  191.      * @abstract
  192.      *
  193.      * @param string $path         The path to store the file in.
  194.      * @param string $name         The filename to use.
  195.      * @param string $tmpFile      The temporary file containing the data to
  196.      *                              be stored.
  197.      * @param boolean $autocreate  Automatically create directories?
  198.      *
  199.      * @return mixed  True on success or a PEAR_Error object on failure.
  200.      */
  201.     function write($path$name$tmpFile$autocreate = false)
  202.     {
  203.         return PEAR::raiseError(_("Not supported."));
  204.     }
  205.  
  206.     /**
  207.      * Stores a file in the VFS from raw data.
  208.      *
  209.      * @access public
  210.      * @abstract
  211.      *
  212.      * @param string $path         The path to store the file in.
  213.      * @param string $name         The filename to use.
  214.      * @param string $data         The file data.
  215.      * @param boolean $autocreate  Automatically create directories?
  216.      *
  217.      * @return mixed  True on success or a PEAR_Error object on failure.
  218.      */
  219.     function writeData($path$name$data$autocreate = false)
  220.     {
  221.         return PEAR::raiseError(_("Not supported."));
  222.     }
  223.  
  224.     /**
  225.      * Moves a file through the backend.
  226.      *
  227.      * @access public
  228.      * @abstract
  229.      *
  230.      * @param string $path  The path of the original file.
  231.      * @param string $name  The name of the original file.
  232.      * @param string $dest  The destination file name.
  233.      *
  234.      * @return mixed  True on success or a PEAR_Error object on failure.
  235.      */
  236.     function move($path$name$dest)
  237.     {
  238.         return PEAR::raiseError(_("Not supported."));
  239.     }
  240.  
  241.     /**
  242.      * Copies a file through the backend.
  243.      *
  244.      * @access public
  245.      * @abstract
  246.      *
  247.      * @param string $path  The path of the original file.
  248.      * @param string $name  The name of the original file.
  249.      * @param string $dest  The name of the destination directory.
  250.      *
  251.      * @return mixed  True on success or a PEAR_Error object on failure.
  252.      */
  253.     function copy($path$name$dest)
  254.     {
  255.         return PEAR::raiseError(_("Not supported."));
  256.     }
  257.  
  258.     /**
  259.      * Deletes a file from the VFS.
  260.      *
  261.      * @access public
  262.      * @abstract
  263.      *
  264.      * @param string $path  The path to delete the file from.
  265.      * @param string $name  The filename to delete.
  266.      *
  267.      * @return mixed  True on success or a PEAR_Error object on failure.
  268.      */
  269.     function deleteFile($path$name)
  270.     {
  271.         return PEAR::raiseError(_("Not supported."));
  272.     }
  273.  
  274.     /**
  275.      * Renames a file in the VFS.
  276.      *
  277.      * @access public
  278.      * @abstract
  279.      *
  280.      * @param string $oldpath  The old path to the file.
  281.      * @param string $oldname  The old filename.
  282.      * @param string $newpath  The new path of the file.
  283.      * @param string $newname  The new filename.
  284.      *
  285.      * @return mixed  True on success or a PEAR_Error object on failure.
  286.      */
  287.     function rename($oldpath$oldname$newpath$newname)
  288.     {
  289.         return PEAR::raiseError(_("Not supported."));
  290.     }
  291.  
  292.     /**
  293.      * Returns if a given file or folder exists in a folder.
  294.      *
  295.      * @access public
  296.      *
  297.      * @param string $path  The path to the folder.
  298.      * @param string $name  The file or folder name.
  299.      *
  300.      * @return boolean  True if it exists, false otherwise.
  301.      */
  302.     function exists($path$name)
  303.     {
  304.         $list $this->listFolder($path);
  305.         if (is_a($list'PEAR_Error')) {
  306.             return false;
  307.         else {
  308.             return isset($list[$name]);
  309.         }
  310.     }
  311.  
  312.     /**
  313.      * Creates a folder in the VFS.
  314.      *
  315.      * @access public
  316.      * @abstract
  317.      *
  318.      * @param string $path  The parent folder.
  319.      * @param string $name  The name of the new folder.
  320.      *
  321.      * @return mixed  True on success or a PEAR_Error object on failure.
  322.      */
  323.     function createFolder($path$name)
  324.     {
  325.         return PEAR::raiseError(_("Not supported."));
  326.     }
  327.  
  328.     /**
  329.      * Automatically creates any necessary parent directories in the specified
  330.      * $path.
  331.      *
  332.      * @access public
  333.      *
  334.      * @param string $path  The VFS path to autocreate.
  335.      */
  336.     function autocreatePath($path)
  337.     {
  338.         $dirs explode('/'$path);
  339.         if (is_array($dirs)) {
  340.             $cur '';
  341.             foreach ($dirs as $dir{
  342.                 if (!$this->isFolder($cur$dir)) {
  343.                     $result $this->createFolder($cur$dir);
  344.                     if (is_a($result'PEAR_Error')) {
  345.                         return $result;
  346.                     }
  347.                 }
  348.                 if (!empty($cur)) {
  349.                     $cur .= '/';
  350.                 }
  351.                 $cur .= $dir;
  352.             }
  353.         }
  354.  
  355.         return true;
  356.     }
  357.  
  358.     /**
  359.      * Checks if a given item is a folder.
  360.      *
  361.      * @access public
  362.      *
  363.      * @param string $path  The parent folder.
  364.      * @param string $name  The item name.
  365.      *
  366.      * @return boolean  True if it is a folder, false otherwise.
  367.      */
  368.     function isFolder($path$name)
  369.     {
  370.         $folderList $this->listFolder($pathnulltruetrue);
  371.         return isset($folderList[$name]);
  372.     }
  373.  
  374.     /**
  375.      * Deletes a folder from the VFS.
  376.      *
  377.      * @access public
  378.      * @abstract
  379.      *
  380.      * @param string $path        The parent folder.
  381.      * @param string $name        The name of the folder to delete.
  382.      * @param boolean $recursive  Force a recursive delete?
  383.      *
  384.      * @return mixed  True on success or a PEAR_Error object on failure.
  385.      */
  386.     function deleteFolder($path$name$recursive = false)
  387.     {
  388.         return PEAR::raiseError(_("Not supported."));
  389.     }
  390.  
  391.     /**
  392.      * Removes recursively all files and subfolders from the given folder.
  393.      *
  394.      * @access public
  395.      *
  396.      * @param string $path  The path of the folder to empty.
  397.      *
  398.      * @return mixed  True on success or a PEAR_Error object on failure.
  399.      */
  400.     function emptyFolder($path)
  401.     {
  402.         // Get and delete the subfolders.
  403.         $list $this->listFolder($pathnulltruetrue);
  404.         if (is_a($list'PEAR_Error')) {
  405.             return $list;
  406.         }
  407.         foreach ($list as $folder{
  408.             $result $this->deleteFolder($path$folder['name']true);
  409.             if (is_a($result'PEAR_Error')) {
  410.                 return $result;
  411.             }
  412.         }
  413.         // Only files are left, get and delete them.
  414.         $list $this->listFolder($path);
  415.         if (is_a($list'PEAR_Error')) {
  416.             return $list;
  417.         }
  418.         foreach ($list as $file{
  419.             $result $this->deleteFile($path$file['name']);
  420.             if (is_a($result'PEAR_Error')) {
  421.                 return $result;
  422.             }
  423.         }
  424.     }
  425.  
  426.     /**
  427.      * Returns a file list of the directory passed in.
  428.      *
  429.      * @access public
  430.      *
  431.      * @param string $path        The path of the directory.
  432.      * @param mixed $filter       String/hash to filter file/dirname on.
  433.      * @param boolean $dotfiles   Show dotfiles?
  434.      * @param boolean $dironly    Show only directories?
  435.      * @param boolean $recursive  Return all directory levels recursively?
  436.      *
  437.      * @return array  File list on success or PEAR_Error on failure.
  438.      */
  439.     function listFolder($path$filter = null$dotfiles = true,
  440.                         $dironly = false$recursive = false)
  441.     {
  442.         $list $this->_listFolder($path$filter$dotfiles$dironly);
  443.         if (!$recursive || is_a($list'PEAR_Error')) {
  444.             return $list;
  445.         }
  446.  
  447.         foreach ($list as $name => $values{
  448.             if ($values['type'== '**dir'{
  449.                 $list[$name]['subdirs'$this->listFolder($path '/' $name$filter$dotfiles$dironly$recursive);
  450.             }
  451.         }
  452.  
  453.         return $list;
  454.     }
  455.  
  456.     /**
  457.      * Returns an an unsorted file list of the specified directory.
  458.      *
  459.      * @access public
  460.      * @abstract
  461.      *
  462.      * @param string $path       The path of the directory.
  463.      * @param mixed $filter      String/hash to filter file/dirname on.
  464.      * @param boolean $dotfiles  Show dotfiles?
  465.      * @param boolean $dironly   Show only directories?
  466.      *
  467.      * @return array  File list on success or PEAR_Error on failure.
  468.      */
  469.     function _listFolder($path$filter = null$dotfiles = true,
  470.                          $dironly = false)
  471.     {
  472.         return PEAR::raiseError(_("Not supported."));
  473.     }
  474.  
  475.     /**
  476.      * Returns the current working directory of the VFS backend.
  477.      *
  478.      * @access public
  479.      *
  480.      * @return string  The current working directory.
  481.      */
  482.     function getCurrentDirectory()
  483.     {
  484.         return '';
  485.     }
  486.  
  487.     /**
  488.      * Returns whether or not a filename matches any filter element.
  489.      *
  490.      * @access private
  491.      *
  492.      * @param mixed $filter     String/hash to build the regular expression
  493.      *                           from.
  494.      * @param string $filename  String containing the filename to match.
  495.      *
  496.      * @return boolean  True on match, false on no match.
  497.      */
  498.     function _filterMatch($filter$filename)
  499.     {
  500.         $namefilter = null;
  501.  
  502.         // Build a regexp based on $filter.
  503.         if ($filter !== null{
  504.             $namefilter '/';
  505.             if (is_array($filter)) {
  506.                 $once = false;
  507.                 foreach ($filter as $item{
  508.                     if ($once !== true{
  509.                         $namefilter .= '(';
  510.                         $once = true;
  511.                     else {
  512.                         $namefilter .= '|(';
  513.                     }
  514.                     $namefilter .= $item ')';
  515.                 }
  516.             else {
  517.                 $namefilter .= '(' $filter ')';
  518.             }
  519.             $namefilter .= '/';
  520.         }
  521.  
  522.         $match = false;
  523.         if ($namefilter !== null{
  524.             $match preg_match($namefilter$filename);
  525.         }
  526.  
  527.         return $match;
  528.     }
  529.  
  530.     /**
  531.      * Changes permissions for an item on the VFS.
  532.      *
  533.      * @access public
  534.      * @abstract
  535.      *
  536.      * @param string $path        The parent folder of the item.
  537.      * @param string $name        The name of the item.
  538.      * @param string $permission  The permission to set.
  539.      *
  540.      * @return mixed  True on success or a PEAR_Error object on failure.
  541.      */
  542.     function changePermissions($path$name$permission)
  543.     {
  544.         return PEAR::raiseError(_("Not supported."));
  545.     }
  546.  
  547.     /**
  548.      * Returns a sorted list of folders in the specified directory.
  549.      *
  550.      * @access public
  551.      * @abstract
  552.      *
  553.      * @param string $path         The path of the directory to get the
  554.      *                              directory list for.
  555.      * @param mixed $filter        Hash of items to filter based on folderlist.
  556.      * @param boolean $dotfolders  Include dotfolders?
  557.      *
  558.      * @return mixed  Folder list on success or a PEAR_Error object on failure.
  559.      */
  560.     function listFolders($path ''$filter = null$dotfolders = true)
  561.     {
  562.         return PEAR::raiseError(_("Not supported."));
  563.     }
  564.  
  565.     /**
  566.      * Returns the list of additional credentials required, if any.
  567.      *
  568.      * @access public
  569.      *
  570.      * @return array  Credential list.
  571.      */
  572.     function getRequiredCredentials()
  573.     {
  574.         return array_diff($this->_credentialsarray_keys($this->_params));
  575.     }
  576.  
  577.     /**
  578.      * Returns an array specifying what permissions are changeable for this
  579.      * VFS implementation.
  580.      *
  581.      * @access public
  582.      *
  583.      * @return array  Changeable permisions.
  584.      */
  585.     function getModifiablePermissions()
  586.     {
  587.         return $this->_permissions;
  588.     }
  589.  
  590.     /**
  591.      * Converts a string to all lowercase characters ignoring the current
  592.      * locale.
  593.      *
  594.      * @access public
  595.      *
  596.      * @param string $string  The string to be lowercased
  597.      *
  598.      * @return string  The string with lowercase characters
  599.      */
  600.     function strtolower($string)
  601.     {
  602.         $language setlocale(LC_CTYPE0);
  603.         setlocale(LC_CTYPE'en');
  604.         $string strtolower($string);
  605.         setlocale(LC_CTYPE$language);
  606.         return $string;
  607.     }
  608.  
  609.     /**
  610.      * Returns the character (not byte) length of a string.
  611.      *
  612.      * @access public
  613.      *
  614.      * @param string $string   The string to return the length of.
  615.      * @param string $charset  The charset to use when calculating the
  616.      *                          string's length.
  617.      *
  618.      * @return string  The string's length.
  619.      */
  620.     function strlen($string$charset = null)
  621.     {
  622.         if (extension_loaded('mbstring')) {
  623.             if (is_null($charset)) {
  624.                 $charset 'ISO-8859-1';
  625.             }
  626.             $ret @mb_strlen($string$charset);
  627.             if (!empty($ret)) {
  628.                 return $ret;
  629.             }
  630.         }
  631.         return strlen($string);
  632.     }
  633.  
  634.     /**
  635.      * Attempts to return a concrete VFS instance based on $driver.
  636.      *
  637.      * @access public
  638.      *
  639.      * @param mixed $driver  The type of concrete VFS subclass to return. This
  640.      *                        is based on the storage driver ($driver). The
  641.      *                        code is dynamically included.
  642.      * @param array $params  A hash containing any additional configuration or
  643.      *                        connection parameters a subclass might need.
  644.      *
  645.      * @return VFS  The newly created concrete VFS instance, or a PEAR_Error
  646.      *               on failure.
  647.      */
  648.     function &factory($driver$params = array())
  649.     {
  650.         include_once 'VFS/' $driver '.php';
  651.         $class 'VFS_' $driver;
  652.         if (class_exists($class)) {
  653.             return $ret &new $class($params);
  654.         else {
  655.             return PEAR::raiseError(sprintf(_("Class definition of %s not found.")$class));
  656.         }
  657.     }
  658.  
  659.     /**
  660.      * Attempts to return a reference to a concrete VFS instance based on
  661.      * $driver. It will only create a new instance if no VFS instance with the
  662.      * same parameters currently exists.
  663.      *
  664.      * This should be used if multiple types of file backends (and, thus,
  665.      * multiple VFS instances) are required.
  666.      *
  667.      * This method must be invoked as: $var = &VFS::singleton()
  668.      *
  669.      * @access public
  670.      *
  671.      * @param mixed $driver  The type of concrete VFS subclass to return. This
  672.      *                        is based on the storage driver ($driver). The
  673.      *                        code is dynamically included.
  674.      * @param array $params  A hash containing any additional configuration or
  675.      *                        connection parameters a subclass might need.
  676.      *
  677.      * @return VFS  The concrete VFS reference, or a PEAR_Error on failure.
  678.      */
  679.     function &singleton($driver$params = array())
  680.     {
  681.         static $instances;
  682.         if (!isset($instances)) {
  683.             $instances = array();
  684.         }
  685.  
  686.         $signature serialize(array($driver$params));
  687.         if (!isset($instances[$signature])) {
  688.             $instances[$signature&VFS::factory($driver$params);
  689.         }
  690.  
  691.         return $instances[$signature];
  692.     }
  693.  
  694. }

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