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. define('VFS_QUOTA_METRIC_BYTE'1);
  7. define('VFS_QUOTA_METRIC_KB'2);
  8. define('VFS_QUOTA_METRIC_MB'3);
  9. define('VFS_QUOTA_METRIC_GB'4);
  10.  
  11. /**
  12.  * VFS API for abstracted file storage and access.
  13.  *
  14.  * $Horde: framework/VFS/VFS.php,v 1.96 2006/03/30 08:03:09 selsky Exp $
  15.  *
  16.  * Copyright 2002-2006 Chuck Hagenbuch <chuck@horde.org>
  17.  *
  18.  * See the enclosed file COPYING for license information (LGPL). If you did
  19.  * not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  20.  *
  21.  * @author  Chuck Hagenbuch <chuck@horde.org>
  22.  * @package VFS
  23.  * @since   Horde 2.2
  24.  */
  25. class VFS {
  26.  
  27.     /**
  28.      * Hash containing connection parameters.
  29.      *
  30.      * @var array 
  31.      */
  32.     var $_params = array();
  33.  
  34.     /**
  35.      * List of additional credentials required for this VFS backend (example:
  36.      * For FTP, we need a username and password to log in to the server with).
  37.      *
  38.      * @var array 
  39.      */
  40.     var $_credentials = array();
  41.  
  42.     /**
  43.      * List of permissions and if they can be changed in this VFS backend.
  44.      *
  45.      * @var array 
  46.      */
  47.     var $_permissions = array(
  48.         'owner' => array('read' => false'write' => false'execute' => false),
  49.         'group' => array('read' => false'write' => false'execute' => false),
  50.         'all'   => array('read' => false'write' => false'execute' => false));
  51.  
  52.     /**
  53.      * A PEAR Log object. If present, will be used to log errors and
  54.      * informational messages about VFS activity.
  55.      *
  56.      * @var Log 
  57.      */
  58.     var $_logger = null;
  59.  
  60.     /**
  61.      * The log level to use - messages with a higher log level than configured
  62.      * here will not be logged. Defaults to only logging errors or higher.
  63.      *
  64.      * @var integer 
  65.      */
  66.     var $_logLevel = PEAR_LOG_ERR;
  67.  
  68.     /**
  69.      * The current size, in bytes, of the VFS item.
  70.      *
  71.      * @var integer 
  72.      */
  73.     var $_vfsSize = null;
  74.  
  75.     /**
  76.      * Constructor.
  77.      *
  78.      * @param array $params  A hash containing connection parameters.
  79.      */
  80.     function VFS($params = array())
  81.     {
  82.         if (empty($params['user'])) {
  83.             $params['user''';
  84.         }
  85.         if (empty($params['vfs_quotalimit'])) {
  86.             $params['vfs_quotalimit'= -1;
  87.         }
  88.         if (empty($params['vfs_quotaroot'])) {
  89.             $params['vfs_quotaroot''/';
  90.         }
  91.         $this->_params $params;
  92.     }
  93.  
  94.     /**
  95.      * Checks the credentials that we have by calling _connect(), to see if
  96.      * there is a valid login.
  97.      *
  98.      * @return mixed  True on success, PEAR_Error describing the problem if the
  99.      *                 credentials are invalid.
  100.      */
  101.     function checkCredentials()
  102.     {
  103.         return $this->_connect();
  104.     }
  105.  
  106.     /**
  107.      * Sets configuration parameters.
  108.      *
  109.      * @param array $params  An associative array with parameter names as keys.
  110.      */
  111.     function setParams($params = array())
  112.     {
  113.         foreach ($params as $name => $value{
  114.             $this->_params[$name$value;
  115.         }
  116.     }
  117.  
  118.     /**
  119.      * Returns configuration parameters.
  120.      *
  121.      * @param string $name  The parameter to return.
  122.      *
  123.      * @return mixed  The parameter value or null if it doesn't exist.
  124.      */
  125.     function getParam($name)
  126.     {
  127.         return isset($this->_params[$name]$this->_params[$name: null;
  128.     }
  129.  
  130.     /**
  131.      * Logs a message if a PEAR Log object is available, and the message's
  132.      * priority is lower than or equal to the configured log level.
  133.      *
  134.      * @param mixed   $message   The message to be logged.
  135.      * @param integer $priority  The message's priority.
  136.      */
  137.     function log($message$priority = PEAR_LOG_ERR)
  138.     {
  139.         if (!isset($this->_logger|| $priority $this->_logLevel{
  140.             return;
  141.         }
  142.  
  143.         if (is_a($message'PEAR_Error')) {
  144.             $userinfo $message->getUserInfo();
  145.             $message $message->getMessage();
  146.             if ($userinfo{
  147.                 if (is_array($userinfo)) {
  148.                     $userinfo implode(', '$userinfo);
  149.                 }
  150.                 $message .= ': ' $userinfo;
  151.             }
  152.         }
  153.  
  154.         /* Make sure to log in the system's locale. */
  155.         $locale setlocale(LC_TIME0);
  156.         setlocale(LC_TIME'C');
  157.  
  158.         $this->_logger->log($message$priority);
  159.  
  160.         /* Restore original locale. */
  161.         setlocale(LC_TIME$locale);
  162.     }
  163.  
  164.     /**
  165.      * Sets the PEAR Log object used to log informational or error messages.
  166.      *
  167.      * @param Log &$logger  The Log object to use.
  168.      */
  169.     function setLogger(&$logger$logLevel = null)
  170.     {
  171.         if (!is_a($logger'Log')) {
  172.             return false;
  173.         }
  174.  
  175.         $this->_logger &$logger;
  176.         if (!is_null($logLevel)) {
  177.             $this->_logLevel $logLevel;
  178.         }
  179.     }
  180.  
  181.     /**
  182.      * Retrieves the size of a file from the VFS.
  183.      *
  184.      * @abstract
  185.      *
  186.      * @param string $path  The pathname to the file.
  187.      * @param string $name  The filename to retrieve.
  188.      *
  189.      * @return integer The file size.
  190.      */
  191.     function size($path$name)
  192.     {
  193.         return PEAR::raiseError(_("Not supported."));
  194.     }
  195.  
  196.     /**
  197.      * Retrieves a file from the VFS.
  198.      *
  199.      * @abstract
  200.      *
  201.      * @param string $path  The pathname to the file.
  202.      * @param string $name  The filename to retrieve.
  203.      *
  204.      * @return string The file data.
  205.      */
  206.     function read($path$name)
  207.     {
  208.         return PEAR::raiseError(_("Not supported."));
  209.     }
  210.  
  211.     /**
  212.      * Retrieves a part of a file from the VFS. Particularly useful when
  213.      * reading large files which would exceed the PHP memory limits if they
  214.      * were stored in a string.
  215.      *
  216.      * @abstract
  217.      *
  218.      * @param string  $path       The pathname to the file.
  219.      * @param string  $name       The filename to retrieve.
  220.      * @param integer $offset     The offset of the part. (The new offset will
  221.      *                             be stored in here).
  222.      * @param integer $length     The length of the part. If the length = -1,
  223.      *                             the whole part after the offset is retrieved.
  224.      *                             If more bytes are given as exists after the
  225.      *                             given offset. Only the available bytes are
  226.      *                             read.
  227.      * @param integer $remaining  The bytes that are left, after the part that
  228.      *                             is retrieved.
  229.      *
  230.      * @return string The file data.
  231.      */
  232.     function readByteRange($path$name&$offset$length = -1&$remaining)
  233.     {
  234.         return PEAR::raiseError(_("Not supported."));
  235.     }
  236.  
  237.     /**
  238.      * Stores a file in the VFS.
  239.      *
  240.      * @abstract
  241.      *
  242.      * @param string $path         The path to store the file in.
  243.      * @param string $name         The filename to use.
  244.      * @param string $tmpFile      The temporary file containing the data to
  245.      *                              be stored.
  246.      * @param boolean $autocreate  Automatically create directories?
  247.      *
  248.      * @return mixed  True on success or a PEAR_Error object on failure.
  249.      */
  250.     function write($path$name$tmpFile$autocreate = false)
  251.     {
  252.         return PEAR::raiseError(_("Not supported."));
  253.     }
  254.  
  255.     /**
  256.      * Stores a file in the VFS from raw data.
  257.      *
  258.      * @abstract
  259.      *
  260.      * @param string $path         The path to store the file in.
  261.      * @param string $name         The filename to use.
  262.      * @param string $data         The file data.
  263.      * @param boolean $autocreate  Automatically create directories?
  264.      *
  265.      * @return mixed  True on success or a PEAR_Error object on failure.
  266.      */
  267.     function writeData($path$name$data$autocreate = false)
  268.     {
  269.         return PEAR::raiseError(_("Not supported."));
  270.     }
  271.  
  272.     /**
  273.      * Moves a file through the backend.
  274.      *
  275.      * @param string $path         The path of the original file.
  276.      * @param string $name         The name of the original file.
  277.      * @param string $dest         The destination file name.
  278.      * @param boolean $autocreate  Automatically create directories?
  279.      *
  280.      * @return mixed  True on success or a PEAR_Error object on failure.
  281.      */
  282.     function move($path$name$dest$autocreate = false)
  283.     {
  284.         if (is_a($result $this->copy($path$name$dest$autocreate)'PEAR_Error')) {
  285.             return $result;
  286.         }
  287.         return $this->deleteFile($path$name);
  288.     }
  289.  
  290.     /**
  291.      * Copies a file through the backend.
  292.      *
  293.      * @param string $path         The path of the original file.
  294.      * @param string $name         The name of the original file.
  295.      * @param string $dest         The name of the destination directory.
  296.      * @param boolean $autocreate  Automatically create directories?
  297.      *
  298.      * @return mixed  True on success or a PEAR_Error object on failure.
  299.      */
  300.     function copy($path$name$dest$autocreate = false)
  301.     {
  302.         $orig $this->_getPath($path$name);
  303.         if (preg_match('|^' preg_quote($orig'[$/]|'$dest)) {
  304.             return PEAR::raiseError(_("Cannot copy file(s): source and destination are the same."));
  305.         }
  306.  
  307.         if ($autocreate{
  308.             $result $this->autocreatePath($dest);
  309.             if (is_a($result'PEAR_Error')) {
  310.                 return $result;
  311.             }
  312.         }
  313.         if ($this->isFolder($path$name)) {
  314.             if (is_a($result $this->_copyRecursive($path$name$dest)'PEAR_Error')) {
  315.                 return $result;
  316.             }
  317.         else {
  318.             $data $this->read($path$name);
  319.             if (is_a($data'PEAR_Error')) {
  320.                 return $data;
  321.             }
  322.             return $this->writeData($dest$name$data$autocreate);
  323.         }
  324.         return true;
  325.     }
  326.  
  327.     /**
  328.      * Recursively copies a directory through the backend.
  329.      *
  330.      * @access protected
  331.      *
  332.      * @param string $path         The path of the original file.
  333.      * @param string $name         The name of the original file.
  334.      * @param string $dest         The name of the destination directory.
  335.      */
  336.     function _copyRecursive($path$name$dest)
  337.     {
  338.         if (is_a($result $this->createFolder($dest$name)'PEAR_Error')) {
  339.             return $result;
  340.         }
  341.  
  342.         if (is_a($file_list $this->listFolder($this->_getPath($path$name))'PEAR_Error')) {
  343.             return $file_list;
  344.         }
  345.  
  346.         foreach ($file_list as $file{
  347.             $result $this->copy($this->_getPath($path$name),
  348.                                   $file['name'],
  349.                                   $this->_getPath($dest$name));
  350.             if (is_a($result'PEAR_Error')) {
  351.                 return $result;
  352.             }
  353.         }
  354.     }
  355.  
  356.     /**
  357.      * Deletes a file from the VFS.
  358.      *
  359.      * @abstract
  360.      *
  361.      * @param string $path  The path to delete the file from.
  362.      * @param string $name  The filename to delete.
  363.      *
  364.      * @return mixed  True on success or a PEAR_Error object on failure.
  365.      */
  366.     function deleteFile($path$name)
  367.     {
  368.         return PEAR::raiseError(_("Not supported."));
  369.     }
  370.  
  371.     /**
  372.      * Renames a file in the VFS.
  373.      *
  374.      * @abstract
  375.      *
  376.      * @param string $oldpath  The old path to the file.
  377.      * @param string $oldname  The old filename.
  378.      * @param string $newpath  The new path of the file.
  379.      * @param string $newname  The new filename.
  380.      *
  381.      * @return mixed  True on success or a PEAR_Error object on failure.
  382.      */
  383.     function rename($oldpath$oldname$newpath$newname)
  384.     {
  385.         return PEAR::raiseError(_("Not supported."));
  386.     }
  387.  
  388.     /**
  389.      * Returns if a given file or folder exists in a folder.
  390.      *
  391.      * @param string $path  The path to the folder.
  392.      * @param string $name  The file or folder name.
  393.      *
  394.      * @return boolean  True if it exists, false otherwise.
  395.      */
  396.     function exists($path$name)
  397.     {
  398.         $list $this->listFolder($path);
  399.         if (is_a($list'PEAR_Error')) {
  400.             return false;
  401.         else {
  402.             return isset($list[$name]);
  403.         }
  404.     }
  405.  
  406.     /**
  407.      * Creates a folder in the VFS.
  408.      *
  409.      * @abstract
  410.      *
  411.      * @param string $path  The parent folder.
  412.      * @param string $name  The name of the new folder.
  413.      *
  414.      * @return mixed  True on success or a PEAR_Error object on failure.
  415.      */
  416.     function createFolder($path$name)
  417.     {
  418.         return PEAR::raiseError(_("Not supported."));
  419.     }
  420.  
  421.     /**
  422.      * Automatically creates any necessary parent directories in the specified
  423.      * $path.
  424.      *
  425.      * @param string $path  The VFS path to autocreate.
  426.      */
  427.     function autocreatePath($path)
  428.     {
  429.         $dirs explode('/'$path);
  430.         if (is_array($dirs)) {
  431.             $cur '/';
  432.             foreach ($dirs as $dir{
  433.                 if (!empty($dir)) {
  434.                     if (!$this->isFolder($cur$dir)) {
  435.                         $result $this->createFolder($cur$dir);
  436.                         if (is_a($result'PEAR_Error')) {
  437.                             return $result;
  438.                         }
  439.                     }
  440.                     if ($cur != '/'{
  441.                         $cur .= '/';
  442.                     }
  443.                     $cur .= $dir;
  444.                 }
  445.             }
  446.         }
  447.  
  448.         return true;
  449.     }
  450.  
  451.     /**
  452.      * Checks if a given item is a folder.
  453.      *
  454.      * @param string $path  The parent folder.
  455.      * @param string $name  The item name.
  456.      *
  457.      * @return boolean  True if it is a folder, false otherwise.
  458.      */
  459.     function isFolder($path$name)
  460.     {
  461.         $folderList $this->listFolder($pathnulltruetrue);
  462.         return isset($folderList[$name]);
  463.     }
  464.  
  465.     /**
  466.      * Deletes a folder from the VFS.
  467.      *
  468.      * @abstract
  469.      *
  470.      * @param string $path        The parent folder.
  471.      * @param string $name        The name of the folder to delete.
  472.      * @param boolean $recursive  Force a recursive delete?
  473.      *
  474.      * @return mixed  True on success or a PEAR_Error object on failure.
  475.      */
  476.     function deleteFolder($path$name$recursive = false)
  477.     {
  478.         return PEAR::raiseError(_("Not supported."));
  479.     }
  480.  
  481.     /**
  482.      * Removes recursively all files and subfolders from the given folder.
  483.      *
  484.      * @param string $path  The path of the folder to empty.
  485.      *
  486.      * @return mixed  True on success or a PEAR_Error object on failure.
  487.      */
  488.     function emptyFolder($path)
  489.     {
  490.         // Get and delete the subfolders.
  491.         $list $this->listFolder($pathnulltruetrue);
  492.         if (is_a($list'PEAR_Error')) {
  493.             return $list;
  494.         }
  495.         foreach ($list as $folder{
  496.             $result $this->deleteFolder($path$folder['name']true);
  497.             if (is_a($result'PEAR_Error')) {
  498.                 return $result;
  499.             }
  500.         }
  501.         // Only files are left, get and delete them.
  502.         $list $this->listFolder($path);
  503.         if (is_a($list'PEAR_Error')) {
  504.             return $list;
  505.         }
  506.         foreach ($list as $file{
  507.             $result $this->deleteFile($path$file['name']);
  508.             if (is_a($result'PEAR_Error')) {
  509.                 return $result;
  510.             }
  511.         }
  512.     }
  513.  
  514.     /**
  515.      * Returns a file list of the directory passed in.
  516.      *
  517.      * @param string $path        The path of the directory.
  518.      * @param mixed $filter       String/hash to filter file/dirname on.
  519.      * @param boolean $dotfiles   Show dotfiles?
  520.      * @param boolean $dironly    Show only directories?
  521.      * @param boolean $recursive  Return all directory levels recursively?
  522.      *
  523.      * @return array  File list on success or PEAR_Error on failure.
  524.      */
  525.     function listFolder($path$filter = null$dotfiles = true,
  526.                         $dironly = false$recursive = false)
  527.     {
  528.         $list $this->_listFolder($path$filter$dotfiles$dironly);
  529.         if (!$recursive || is_a($list'PEAR_Error')) {
  530.             return $list;
  531.         }
  532.  
  533.         foreach ($list as $name => $values{
  534.             if ($values['type'== '**dir'{
  535.                 $list[$name]['subdirs'$this->listFolder($path '/' $name$filter$dotfiles$dironly$recursive);
  536.             }
  537.         }
  538.  
  539.         return $list;
  540.     }
  541.  
  542.     /**
  543.      * Returns an an unsorted file list of the specified directory.
  544.      *
  545.      * @abstract
  546.      *
  547.      * @param string $path       The path of the directory.
  548.      * @param mixed $filter      String/hash to filter file/dirname on.
  549.      * @param boolean $dotfiles  Show dotfiles?
  550.      * @param boolean $dironly   Show only directories?
  551.      *
  552.      * @return array  File list on success or PEAR_Error on failure.
  553.      */
  554.     function _listFolder($path$filter = null$dotfiles = true,
  555.                          $dironly = false)
  556.     {
  557.         return PEAR::raiseError(_("Not supported."));
  558.     }
  559.  
  560.     /**
  561.      * Returns the current working directory of the VFS backend.
  562.      *
  563.      * @return string  The current working directory.
  564.      */
  565.     function getCurrentDirectory()
  566.     {
  567.         return '';
  568.     }
  569.  
  570.     /**
  571.      * Returns whether or not a filename matches any filter element.
  572.      *
  573.      * @access private
  574.      *
  575.      * @param mixed $filter     String/hash to build the regular expression
  576.      *                           from.
  577.      * @param string $filename  String containing the filename to match.
  578.      *
  579.      * @return boolean  True on match, false on no match.
  580.      */
  581.     function _filterMatch($filter$filename)
  582.     {
  583.         $namefilter = null;
  584.  
  585.         // Build a regexp based on $filter.
  586.         if ($filter !== null{
  587.             $namefilter '/';
  588.             if (is_array($filter)) {
  589.                 $once = false;
  590.                 foreach ($filter as $item{
  591.                     if ($once !== true{
  592.                         $namefilter .= '(';
  593.                         $once = true;
  594.                     else {
  595.                         $namefilter .= '|(';
  596.                     }
  597.                     $namefilter .= $item ')';
  598.                 }
  599.             else {
  600.                 $namefilter .= '(' $filter ')';
  601.             }
  602.             $namefilter .= '/';
  603.         }
  604.  
  605.         $match = false;
  606.         if ($namefilter !== null{
  607.             $match preg_match($namefilter$filename);
  608.         }
  609.  
  610.         return $match;
  611.     }
  612.  
  613.     /**
  614.      * Changes permissions for an item on the VFS.
  615.      *
  616.      * @abstract
  617.      *
  618.      * @param string $path        The parent folder of the item.
  619.      * @param string $name        The name of the item.
  620.      * @param string $permission  The permission to set.
  621.      *
  622.      * @return mixed  True on success or a PEAR_Error object on failure.
  623.      */
  624.     function changePermissions($path$name$permission)
  625.     {
  626.         return PEAR::raiseError(_("Not supported."));
  627.     }
  628.  
  629.     /**
  630.      * Returns a sorted list of folders in the specified directory.
  631.      *
  632.      * @abstract
  633.      *
  634.      * @param string $path         The path of the directory to get the
  635.      *                              directory list for.
  636.      * @param mixed $filter        Hash of items to filter based on folderlist.
  637.      * @param boolean $dotfolders  Include dotfolders?
  638.      *
  639.      * @return mixed  Folder list on success or a PEAR_Error object on failure.
  640.      */
  641.     function listFolders($path ''$filter = null$dotfolders = true)
  642.     {
  643.         return PEAR::raiseError(_("Not supported."));
  644.     }
  645.  
  646.     /**
  647.      * Returns the list of additional credentials required, if any.
  648.      *
  649.      * @return array  Credential list.
  650.      */
  651.     function getRequiredCredentials()
  652.     {
  653.         return array_diff($this->_credentialsarray_keys($this->_params));
  654.     }
  655.  
  656.     /**
  657.      * Returns an array specifying what permissions are changeable for this
  658.      * VFS implementation.
  659.      *
  660.      * @return array  Changeable permisions.
  661.      */
  662.     function getModifiablePermissions()
  663.     {
  664.         return $this->_permissions;
  665.     }
  666.  
  667.     /**
  668.      * Converts a string to all lowercase characters ignoring the current
  669.      * locale.
  670.      *
  671.      * @param string $string  The string to be lowercased
  672.      *
  673.      * @return string  The string with lowercase characters
  674.      */
  675.     function strtolower($string)
  676.     {
  677.         $language setlocale(LC_CTYPE0);
  678.         setlocale(LC_CTYPE'en');
  679.         $string strtolower($string);
  680.         setlocale(LC_CTYPE$language);
  681.         return $string;
  682.     }
  683.  
  684.     /**
  685.      * Returns the character (not byte) length of a string.
  686.      *
  687.      * @param string $string   The string to return the length of.
  688.      * @param string $charset  The charset to use when calculating the
  689.      *                          string's length.
  690.      *
  691.      * @return string  The string's length.
  692.      */
  693.     function strlen($string$charset = null)
  694.     {
  695.         if (extension_loaded('mbstring')) {
  696.             if (is_null($charset)) {
  697.                 $charset 'ISO-8859-1';
  698.             }
  699.             $ret @mb_strlen($string$charset);
  700.             if (!empty($ret)) {
  701.                 return $ret;
  702.             }
  703.         }
  704.         return strlen($string);
  705.     }
  706.  
  707.     /**
  708.      * Returns the size of a file.
  709.      *
  710.      * @abstract
  711.      * @since Horde 3.1
  712.      *
  713.      * @param string $path  The path to the file.
  714.      * @param string $name  The name of the file.
  715.      *
  716.      * @return integer  The size of the file, in bytes, or PEAR_Error on
  717.      *                   failure.
  718.      */
  719.     function getFileSize($path$name)
  720.     {
  721.         return PEAR::raiseError(_("Not supported."));
  722.     }
  723.  
  724.     /**
  725.      * Returns the size of a folder
  726.      *
  727.      * @since Horde 3.1
  728.      *
  729.      * @param string $path  The path to the folder.
  730.      * @param string $name  The name of the folder.
  731.      *
  732.      * @return integer  The size of the folder, in bytes, or PEAR_Error on
  733.      *                   failure.
  734.      */
  735.     function getFolderSize($path = null$name = null)
  736.     {
  737.         $size = 0;
  738.         $root ((!is_null($path)) $path '/' ''$name;
  739.         $object_list $this->listFolder($rootnulltruefalsetrue);
  740.         foreach ($object_list as $key => $val{
  741.             if (isset($val['subdirs'])) {
  742.                 $size += $this->getFolderSize($root$key);
  743.             else {
  744.                 $size += @$this->getFileSize($root$key);
  745.             }
  746.         }
  747.  
  748.         return $size;
  749.     }
  750.  
  751.     /**
  752.      * Returns the size of the VFS item.
  753.      *
  754.      * @since Horde 3.1
  755.      *
  756.      * @return integer  The size, in bytes, of the VFS item.
  757.      */
  758.     function getVFSSize()
  759.     {
  760.         if (is_null($this->_vfsSize)) {
  761.             $this->_vfsSize $this->getFolderSize($this->_params['vfs_quotaroot']);
  762.         }
  763.         return $this->_vfsSize;
  764.     }
  765.  
  766.     /**
  767.      * Sets the VFS quota limit.
  768.      *
  769.      * @since Horde 3.1
  770.      *
  771.      * @param integer $quota   The limit to apply.
  772.      * @param integer $metric  The metric to multiply the quota into.
  773.      */
  774.     function setQuota($quota$metric = VFS_QUOTA_METRIC_BYTE)
  775.     {
  776.         switch ($metric{
  777.         case VFS_QUOTA_METRIC_KB:
  778.             $quota *= pow(210);
  779.             break;
  780.  
  781.         case VFS_QUOTA_METRIC_MB:
  782.             $quota *= pow(220);
  783.             break;
  784.  
  785.         case VFS_QUOTA_METRIC_GB:
  786.             $quota *= pow(230);
  787.             break;
  788.         }
  789.  
  790.         $this->_params['vfs_quotalimit'$quota;
  791.     }
  792.  
  793.     /**
  794.      * Sets the VFS quota root.
  795.      *
  796.      * @since Horde 3.1
  797.      *
  798.      * @param string $dir  The root directory for the quota determination.
  799.      */
  800.     function setQuotaRoot($dir)
  801.     {
  802.         $this->_params['vfs_quotaroot'$dir;
  803.     }
  804.  
  805.     /**
  806.      * Get quota information (used/allocated), in bytes.
  807.      *
  808.      * @since Horde 3.1
  809.      *
  810.      * @return mixed  An associative array.
  811.      *                 'limit' = Maximum quota allowed
  812.      *                 'usage' = Currently used portion of quota (in bytes)
  813.      *                 Returns PEAR_Error on failure.
  814.      */
  815.     function getQuota()
  816.     {
  817.         if (empty($this->_params['vfs_quotalimit'])) {
  818.             return PEAR::raiseError(_("No quota set."));
  819.         else {
  820.             $usage $this->getVFSSize();
  821.             if (is_a($usage'PEAR_Error')) {
  822.                  return $usage;
  823.             else {
  824.                 return array('usage' => $this->getVFSSize()'limit' => $this->_params['vfs_quotalimit']);
  825.             }
  826.         }
  827.     }
  828.  
  829.     /**
  830.      * Determines the location of the system temporary directory.
  831.      *
  832.      * @access protected
  833.      *
  834.      * @return string  A directory name which can be used for temp files.
  835.      *                  Returns false if one could not be found.
  836.      */
  837.     function _getTempDir()
  838.     {
  839.         $tmp_locations = array('/tmp''/var/tmp''c:\WUTemp''c:\temp',
  840.                                'c:\windows\temp''c:\winnt\temp');
  841.  
  842.         /* Try PHP's upload_tmp_dir directive. */
  843.         $tmp ini_get('upload_tmp_dir');
  844.  
  845.         /* Otherwise, try to determine the TMPDIR environment variable. */
  846.         if (empty($tmp)) {
  847.             $tmp getenv('TMPDIR');
  848.         }
  849.  
  850.         /* If we still cannot determine a value, then cycle through a list of
  851.          * preset possibilities. */
  852.         while (empty($tmp&& sizeof($tmp_locations)) {
  853.             $tmp_check array_shift($tmp_locations);
  854.             if (@is_dir($tmp_check)) {
  855.                 $tmp $tmp_check;
  856.             }
  857.         }
  858.  
  859.         /* If it is still empty, we have failed, so return false; otherwise
  860.          * return the directory determined. */
  861.         return empty($tmp? false : $tmp;
  862.     }
  863.  
  864.     /**
  865.      * Creates a temporary file.
  866.      *
  867.      * @access protected
  868.      *
  869.      * @return string   Returns the full path-name to the temporary file or
  870.      *                   false if a temporary file could not be created.
  871.      */
  872.     function _getTempFile()
  873.     {
  874.         $tmp_dir $this->_getTempDir();
  875.         if (empty($tmp_dir)) {
  876.             return false;
  877.         }
  878.  
  879.         $tmp_file tempnam($tmp_dir'vfs');
  880.  
  881.         /* If the file was created, then register it for deletion and
  882.          * return. */
  883.         if (empty($tmp_file)) {
  884.             return false;
  885.         else {
  886.             return $tmp_file;
  887.         }
  888.     }
  889.  
  890.     /**
  891.      * Checks the quota when preparing to write data.
  892.      *
  893.      * @access private
  894.      *
  895.      * @param string $mode   Either 'string' or 'file'.  If 'string', $data is
  896.      *                        the data to be written.  If 'file', $data is the
  897.      *                        filename containing the data to be written.
  898.      * @param string &$data  Either the data or the filename to the data.
  899.      *
  900.      * @return mixed  PEAR_Error on error, true on success.
  901.      */
  902.     function _checkQuotaWrite($mode&$data)
  903.     {
  904.         if ($this->_params['vfs_quotalimit'!= -1{
  905.             if ($mode == 'file'{
  906.                 $filesize @filesize($data);
  907.                 if ($filesize === false{
  908.                     return PEAR::raiseError(_("Unable to read VFS file (filesize() failed)."));
  909.                }
  910.             else {
  911.                 $filesize strlen($data);
  912.             }
  913.             if (($this->getVFSSize($filesize$this->_params['vfs_quotalimit']{
  914.                 return PEAR::raiseError(_("Unable to write VFS file, quota will be exceeded."));
  915.             elseif ($this->_vfsSize !== 0{
  916.                 $this->_vfsSize += $filesize;
  917.             }
  918.         }
  919.  
  920.         return true;
  921.     }
  922.  
  923.     /**
  924.      * Checks the quota when preparing to delete data.
  925.      *
  926.      * @access private
  927.      *
  928.      * @param string $path  The path the file is located in.
  929.      * @param string $name  The filename.
  930.      *
  931.      * @return mixed  PEAR_Error on error, true on success.
  932.      */
  933.     function _checkQuotaDelete($path$name)
  934.     {
  935.         if (($this->_params['vfs_quotalimit'!= -1&&
  936.             !empty($this->_vfsSize)) {
  937.             $filesize $this->getFileSize($path$name);
  938.             if (is_a($filesize'PEAR_Error')) {
  939.                 return PEAR::raiseError(_("Unable to read VFS file (getFileSize() failed)."));
  940.             }
  941.             $this->_vfsSize -= $filesize;
  942.         }
  943.  
  944.         return true;
  945.     }
  946.  
  947.     /**
  948.      * Returns the full path of an item.
  949.      *
  950.      * @access protected
  951.      *
  952.      * @param string $path  The path of directory of the item.
  953.      * @param string $name  The name of the item.
  954.      *
  955.      * @return mixed  Full path when $path isset and just $name when not set.
  956.      */
  957.     function _getPath($path$name)
  958.     {
  959.         if ($path !== ''{
  960.              return ($path '/' $name);
  961.         }
  962.         return $name;
  963.     }
  964.  
  965.     /**
  966.      * Attempts to return a concrete VFS instance based on $driver.
  967.      *
  968.      * @param mixed $driver  The type of concrete VFS subclass to return. This
  969.      *                        is based on the storage driver ($driver). The
  970.      *                        code is dynamically included.
  971.      * @param array $params  A hash containing any additional configuration or
  972.      *                        connection parameters a subclass might need.
  973.      *
  974.      * @return VFS  The newly created concrete VFS instance, or a PEAR_Error
  975.      *               on failure.
  976.      */
  977.     function &factory($driver$params = array())
  978.     {
  979.         include_once 'VFS/' $driver '.php';
  980.         $class 'VFS_' $driver;
  981.         if (class_exists($class)) {
  982.             $vfs &new $class($params);
  983.         else {
  984.             $vfs = PEAR::raiseError(sprintf(_("Class definition of %s not found.")$class));
  985.         }
  986.  
  987.         return $vfs;
  988.     }
  989.  
  990.     /**
  991.      * Attempts to return a reference to a concrete VFS instance based on
  992.      * $driver. It will only create a new instance if no VFS instance with the
  993.      * same parameters currently exists.
  994.      *
  995.      * This should be used if multiple types of file backends (and, thus,
  996.      * multiple VFS instances) are required.
  997.      *
  998.      * This method must be invoked as: $var = &VFS::singleton()
  999.      *
  1000.      * @param mixed $driver  The type of concrete VFS subclass to return. This
  1001.      *                        is based on the storage driver ($driver). The
  1002.      *                        code is dynamically included.
  1003.      * @param array $params  A hash containing any additional configuration or
  1004.      *                        connection parameters a subclass might need.
  1005.      *
  1006.      * @return VFS  The concrete VFS reference, or a PEAR_Error on failure.
  1007.      */
  1008.     function &singleton($driver$params = array())
  1009.     {
  1010.         static $instances;
  1011.         if (!isset($instances)) {
  1012.             $instances = array();
  1013.         }
  1014.  
  1015.         $signature serialize(array($driver$params));
  1016.         if (!isset($instances[$signature])) {
  1017.             $instances[$signature&VFS::factory($driver$params);
  1018.         }
  1019.  
  1020.         return $instances[$signature];
  1021.     }
  1022.  
  1023. }

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