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

Source for file musql.php

Documentation is available at musql.php

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/sql.php';
  4.  
  5. /** @constant integer VFS_FLAG_READ  Permission for read access. */
  6. define('VFS_FLAG_READ'1);
  7.  
  8. /** @constant integer VFS_FLAG_WRITE  Permission for read access. */
  9. define('VFS_FLAG_WRITE'2);
  10.  
  11. /**
  12.  * Multi User VFS implementation for PHP's PEAR database
  13.  * abstraction layer.
  14.  *
  15.  * <pre>
  16.  * Required values for $params:
  17.  *      'phptype'       The database type (ie. 'pgsql', 'mysql, etc.).
  18.  *      'hostspec'      The hostname of the database server.
  19.  *      'protocol'      The communication protocol ('tcp', 'unix', etc.).
  20.  *      'username'      The username with which to connect to the database.
  21.  *      'password'      The password associated with 'username'.
  22.  *      'database'      The name of the database.
  23.  *
  24.  * Optional values:
  25.  *      'table'         The name of the vfs table in 'database'. Defaults to
  26.  *                      'horde_muvfs'.
  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.  * Known Issues:
  35.  * Delete is not recusive, so file and folders that used to be in a folder that
  36.  * gets deleted life forever in the database, or re-appear when the folder is
  37.  * recreated.
  38.  * Rename has the same issue, if files are lost if a folder is renamed.
  39.  *
  40.  * The table structure for the VFS can be found in
  41.  * data/muvfs.sql.
  42.  *
  43.  * Database specific notes:
  44.  *
  45.  * MSSQL:
  46.  * - The vfs_data field must be of type IMAGE.
  47.  * - You need the following php.ini settings:
  48.  * <code>
  49.  *    ; Valid range 0 - 2147483647. Default = 4096.
  50.  *    mssql.textlimit = 0 ; zero to pass through
  51.  *
  52.  *    ; Valid range 0 - 2147483647. Default = 4096.
  53.  *    mssql.textsize = 0 ; zero to pass through
  54.  * </code>
  55.  *
  56.  * $Horde: framework/VFS/VFS/musql.php,v 1.34 2005/04/07 13:24:23 jan Exp $
  57.  *
  58.  * Copyright 2002-2005 Chuck Hagenbuch <chuck@horde.org>
  59.  *
  60.  * See the enclosed file COPYING for license information (LGPL). If you
  61.  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  62.  *
  63.  * @author  Chuck Hagenbuch <chuck@horde.org>
  64.  * @author  Mike Cochrane <mike@graftonhall.co.nz>
  65.  * @since   Horde 2.2
  66.  * @package VFS
  67.  */
  68. class VFS_musql extends VFS_sql {
  69.  
  70.     /**
  71.      * List of permissions and if they can be changed in this VFS
  72.      *
  73.      * @var array $_permissions 
  74.      */
  75.     var $_permissions = array(
  76.         'owner' => array('read' => false'write' => false'execute' => false),
  77.         'group' => array('read' => false'write' => false'execute' => false),
  78.         'all'   => array('read' => true,  'write' => true,  'execute' => false)
  79.     );
  80.  
  81.     /**
  82.      * Store a file in the VFS from raw data.
  83.      *
  84.      * @access public
  85.      *
  86.      * @param string $path                  The path to store the file in.
  87.      * @param string $name                  The filename to use.
  88.      * @param string $data                  The file data.
  89.      * @param optional boolean $autocreate  Automatically create directories?
  90.      *
  91.      * @return mixed  True on success or a PEAR_Error object on failure.
  92.      */
  93.     function writeData($path$name$data$autocreate = false)
  94.     {
  95.         $conn $this->_connect();
  96.         if (PEAR::isError($conn)) {
  97.             return $conn;
  98.         }
  99.  
  100.         /* Make sure we have write access to this and all parent
  101.          * paths. */
  102.         if ($path != ''{
  103.             $paths explode('/'$path);
  104.             $path_name array_pop($paths);
  105.             if (!$this->isFolder(implode('/'$paths)$path_name)) {
  106.                 if (!$autocreate{
  107.                     return PEAR::raiseError(sprintf(_("Folder %s does not exist")$path)'horde.error');
  108.                 else {
  109.                     $result $this->autocreatePath($path);
  110.                     if (is_a($result'PEAR_Error')) {
  111.                         return $result;
  112.                     }
  113.                 }
  114.             }
  115.             $paths[$path_name;
  116.             $previous '';
  117.  
  118.             foreach ($paths as $thispath{
  119.                 $results $this->_db->getAll(sprintf('SELECT vfs_owner, vfs_perms FROM %s
  120.                                                        WHERE vfs_path = ? AND vfs_name= ?',
  121.                                                       $this->_params['table']),
  122.                                               array($previous$thispath));
  123.                 if (!is_array($results|| count($results< 1{
  124.                     return PEAR::raiseError(_("Unable to create VFS file."));
  125.                 }
  126.  
  127.                 $allowed = false;
  128.                 foreach ($results as $result{
  129.                     if ($result[0== $this->_params['user'|| $result[1VFS_FLAG_WRITE{
  130.                         $allowed = true;
  131.                         break;
  132.                     }
  133.                 }
  134.  
  135.                 if (!$allowed{
  136.                     return PEAR::raiseError(_("Access denied creating VFS file."));
  137.                 }
  138.  
  139.                 $previous $thispath;
  140.             }
  141.         }
  142.  
  143.         return parent::writeData($path$name$data$autocreate);
  144.     }
  145.  
  146.     /**
  147.      * Delete a file from the VFS.
  148.      *
  149.      * @access public
  150.      *
  151.      * @param string $path  The path to store the file in.
  152.      * @param string $name  The filename to use.
  153.      *
  154.      * @return mixed  True on success or a PEAR_Error object on failure.
  155.      */
  156.     function deleteFile($path$name)
  157.     {
  158.         $conn $this->_connect();
  159.         if (PEAR::isError($conn)) {
  160.             return $conn;
  161.         }
  162.  
  163.         $fileList $this->_db->getAll(sprintf('SELECT vfs_id, vfs_owner, vfs_perms FROM %s
  164.                                                 WHERE vfs_path = ? AND vfs_name= ? AND vfs_type = ?',
  165.                                                $this->_params['table']),
  166.                                        array($path$nameVFS_FILE));
  167.  
  168.         if (!is_array($fileList|| count($fileList< 1{
  169.             return PEAR::raiseError(_("Unable to delete VFS file."));
  170.         }
  171.  
  172.         /* There may be one or more files with the same name but the
  173.            user may not have read access to them, so doesn't see
  174.            them. So we have to delete the one they have access to. */
  175.         foreach ($fileList as $file{
  176.             if ($file[1== $this->_params['user'|| $file[2VFS_FLAG_WRITE{
  177.                 $result $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_id = ?',
  178.                                                     $this->_params['table']),
  179.                                             array($file[0]));
  180.  
  181.                 if ($this->_db->affectedRows(== 0{
  182.                     return PEAR::raiseError(_("Unable to delete VFS file."));
  183.                 }
  184.                 return $result;
  185.             }
  186.         }
  187.  
  188.         // FIXME: 'Access Denied deleting file %s/%s'
  189.         return PEAR::raiseError(_("Unable to delete VFS file."));
  190.     }
  191.  
  192.     /**
  193.      * Rename a file or folder in the VFS.
  194.      *
  195.      * @access public
  196.      *
  197.      * @param string $oldpath  The old path to the file.
  198.      * @param string $oldname  The old filename.
  199.      * @param string $newpath  The new path of the file.
  200.      * @param string $newname  The new filename.
  201.      *
  202.      * @return mixed  True on success or a PEAR_Error object on failure.
  203.      */
  204.     function rename($oldpath$oldname$newpath$newname)
  205.     {
  206.         $conn $this->_connect();
  207.         if (PEAR::isError($conn)) {
  208.             return $conn;
  209.         }
  210.  
  211.         $fileList $this->_db->getAll(sprintf('SELECT vfs_id, vfs_owner, vfs_perms FROM %s
  212.                                                        WHERE vfs_path = ? AND vfs_name= ?',
  213.                                                        $this->_params['table']),
  214.                                        array($oldpath$oldname));
  215.  
  216.         if (!is_array($fileList|| count($fileList< 1{
  217.             return PEAR::raiseError(_("Unable to rename VFS file."));
  218.         }
  219.  
  220.         /* There may be one or more files with the same name but the user may
  221.      * not have read access to them, so doesn't see them. So we have to
  222.      * rename the one they have access to. */
  223.         foreach ($fileList as $file{
  224.             if ($file[1== $this->_params['user'|| $file[2VFS_FLAG_WRITE{
  225.                 $result $this->_db->query(sprintf('UPDATE %s SET vfs_path = ?, vfs_name = ?, vfs_modified = ?
  226.                                                     WHERE vfs_id = ?',
  227.                                                     $this->_params['table']),
  228.                                             array($newpath$newnametime()$file[0]));
  229.                 return $result;
  230.             }
  231.         }
  232.  
  233.         return PEAR::raiseError(sprintf(_("Unable to rename VFS file %s/%s.")$oldpath$oldname));
  234.     }
  235.  
  236.     /**
  237.      * Creates a folder on the VFS.
  238.      *
  239.      * @access public
  240.      *
  241.      * @param string $path  Holds the path of directory to create folder.
  242.      * @param string $name  Holds the name of the new folder.
  243.      *
  244.      * @return mixed  True on success or a PEAR_Error object on failure.
  245.      */
  246.     function createFolder($path$name)
  247.     {
  248.         $conn $this->_connect();
  249.         if (PEAR::isError($conn)) {
  250.             return $conn;
  251.         }
  252.  
  253.         /* make sure we have write access to this and all parent paths */
  254.         if ($path != ""{
  255.             $paths explode('/'$path);
  256.             $previous '';
  257.  
  258.             foreach ($paths as $thispath{
  259.                 $results $this->_db->getAll(sprintf('SELECT vfs_owner, vfs_perms FROM %s
  260.                                                                WHERE vfs_path = ? AND vfs_name= ?',
  261.                                                                $this->_params['table']),
  262.                                               array($previous$thispath));
  263.                 if (!is_array($results|| count($results< 1{
  264.                     return PEAR::raiseError(_("Unable to create VFS directory."));
  265.                 }
  266.  
  267.                 $allowed = false;
  268.                 foreach ($results as $result{
  269.                     if ($result[0== $this->_params['user'|| $result[1VFS_FLAG_WRITE{
  270.                         $allowed = true;
  271.                         break;
  272.                     }
  273.                 }
  274.  
  275.                 if (!$allowed{
  276.                     return PEAR::raiseError(_("Access denied creating VFS directory."));
  277.                 }
  278.  
  279.                 $previous $thispath;
  280.             }
  281.         }
  282.  
  283.         $id $this->_db->nextId($this->_params['table']);
  284.         return $this->_db->query(sprintf('INSERT INTO %s (vfs_id, vfs_type, vfs_path, vfs_name, vfs_modified, vfs_owner, vfs_perms)
  285.                                          VALUES (?, ?, ?, ?, ?, ?, ?)',
  286.                                          $this->_params['table']),
  287.                                  array($idVFS_FOLDER$path$nametime()$this->_params['user']0));
  288.     }
  289.  
  290.     /**
  291.      * Delete a file from the VFS.
  292.      *
  293.      * @access public
  294.      *
  295.      * @param string $path                 The path to delete the folder from.
  296.      * @param string $name                 The foldername to use.
  297.      * @param optional boolean $recursive  Force a recursive delete?
  298.      *
  299.      * @return mixed  True on success or a PEAR_Error object on failure.
  300.      */
  301.     function deleteFolder($path$name$recursive = false)
  302.     {
  303.         $conn $this->_connect();
  304.         if (PEAR::isError($conn)) {
  305.             return $conn;
  306.         }
  307.  
  308.         if ($recursive{
  309.             $result $this->emptyFolder($path '/' $name);
  310.             if (is_a($result'PEAR_Error')) {
  311.                 return $result;
  312.             }
  313.         else {
  314.             $list $this->listFolder($path '/' $name);
  315.             if (is_a($list'PEAR_Error')) {
  316.                 return $list;
  317.             }
  318.             if (count($list)) {
  319.                 return PEAR::raiseError(sprintf(_("Unable to delete %s, the directory is not empty"),
  320.                                                 $path '/' $name));
  321.             }
  322.         }
  323.  
  324.         $fileList $this->_db->getAll(sprintf('SELECT vfs_id, vfs_owner, vfs_perms FROM %s
  325.                                                        WHERE vfs_path = ? AND vfs_name= ? AND vfs_type = ?',
  326.                                                        $this->_params['table']),
  327.                                        array($path$nameVFS_FOLDER));
  328.  
  329.         if (!is_array($fileList|| count($fileList< 1{
  330.             return PEAR::raiseError(_("Unable to delete VFS directory."));
  331.         }
  332.  
  333.         /* There may be one or more folders with the same name but as the user
  334.      * may not have read access to them, they don't see them. So we have to
  335.      * delete the one they have access to */
  336.         foreach ($fileList as $file{
  337.             if ($file[1== $this->_params['user'|| $file[2VFS_FLAG_WRITE{
  338.                 $result $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_id = ?',
  339.                                                     $this->_params['table']),
  340.                                             array($file[0]));
  341.  
  342.                 if ($this->_db->affectedRows(== 0{
  343.                     return PEAR::raiseError(_("Unable to delete VFS directory."));
  344.                 }
  345.  
  346.                 return $result;
  347.             }
  348.         }
  349.  
  350.         // FIXME: 'Access Denied deleting folder %s/%s'
  351.         return PEAR::raiseError(_("Unable to delete VFS directory."));
  352.     }
  353.  
  354.     /**
  355.      * Return a list of the contents of a folder.
  356.      *
  357.      * @param string $path                The path of the directory.
  358.      * @param optional mixed $filter      String/hash to filter file/dirname
  359.      *                                     on.
  360.      * @param optional boolean $dotfiles  Show dotfiles?
  361.      * @param optional boolean $dironly   Show only directories?
  362.      *
  363.      * @return mixed  File list on success or false on failure.
  364.      */
  365.     function _listFolder($path$filter = null$dotfiles = true,
  366.                         $dironly = false)
  367.     {
  368.         $conn $this->_connect();
  369.         if (is_a($conn'PEAR_Error')) {
  370.             return $conn;
  371.         }
  372.  
  373.         $files = array();
  374.         $fileList = array();
  375.  
  376.         $fileList $this->_db->getAll(sprintf('SELECT vfs_name, vfs_type, vfs_modified, vfs_owner, vfs_perms, vfs_data FROM %s
  377.                                                WHERE vfs_path = ? AND (vfs_owner = ? or vfs_perms && ?)',
  378.                                                $this->_params['table']),
  379.                                        array($path$this->_params['user']VFS_FLAG_READ));
  380.         if (is_a($fileList'PEAR_Error')) {
  381.             return $fileList;
  382.         }
  383.  
  384.         foreach ($fileList as $line{
  385.             // Filter out dotfiles if they aren't wanted.
  386.             if (!$dotfiles && substr($line[0]01== '.'{
  387.                 continue;
  388.             }
  389.  
  390.             $file['name'stripslashes($line[0]);
  391.  
  392.             if ($line[1== VFS_FILE{
  393.                 $name explode('.'$line[0]);
  394.  
  395.                 if (count($name== 1{
  396.                     $file['type''**none';
  397.                 else {
  398.                     $file['type'VFS::strtolower($name[count($name- 1]);
  399.                 }
  400.  
  401.                 $file['size'VFS::strlen($line[5]);
  402.             elseif ($line[1== VFS_FOLDER{
  403.                 $file['type''**dir';
  404.                 $file['size'= -1;
  405.             }
  406.  
  407.             $file['date'$line[2];
  408.             $file['owner'$line[3];
  409.  
  410.             $line[4intval($line[4]);
  411.             $file['perms']  ($line[1== VFS_FOLDER'd' '-';
  412.             $file['perms'.= 'rw-';
  413.             $file['perms'.= ($line[4VFS_FLAG_READ'r' '-';
  414.             $file['perms'.= ($line[4VFS_FLAG_WRITE'w' '-';
  415.             $file['perms'.= '-';
  416.             $file['group''-';
  417.  
  418.             // filtering
  419.             if ($this->_filterMatch($filter$file['name'])) {
  420.                 unset($file);
  421.                 continue;
  422.             }
  423.             if ($dironly && $file['type'!== '**dir'{
  424.                 unset($file);
  425.                 continue;
  426.             }
  427.  
  428.             $files[$file['name']] $file;
  429.             unset($file);
  430.         }
  431.  
  432.         return $files;
  433.     }
  434.  
  435.     /**
  436.      * Changes permissions for an Item on the VFS.
  437.      *
  438.      * @access public
  439.      *
  440.      * @param string $path  Holds the path of directory of the Item.
  441.      * @param string $name  Holds the name of the Item.
  442.      *
  443.      * @return mixed  True on success or a PEAR_Error object on failure.
  444.      */
  445.     function changePermissions($path$name$permission)
  446.     {
  447.         $conn $this->_connect();
  448.         if (PEAR::isError($conn)) {
  449.             return $conn;
  450.         }
  451.  
  452.         $val intval(substr($permission-1));
  453.         $perm = 0;
  454.         $perm |= ($val 4VFS_FLAG_READ : 0;
  455.         $perm |= ($val 2VFS_FLAG_WRITE : 0;
  456.  
  457.         $fileList $this->_db->getAll(sprintf('SELECT vfs_id, vfs_owner, vfs_perms FROM %s
  458.                                                        WHERE vfs_path = ? AND vfs_name= ?',
  459.                                                        $this->_params['table']),
  460.                                        array($path$name));
  461.  
  462.         if (!is_array($fileList|| count($fileList< 1{
  463.             return PEAR::raiseError(_("Unable to rename VFS file."));
  464.         }
  465.  
  466.         /* There may be one or more files with the same name but the user may
  467.      * not have read access to them, so doesn't see them. So we have to
  468.      * chmod the one they have access to. */
  469.         foreach ($fileList as $file{
  470.             if ($file[1== $this->_params['user'|| $file[2VFS_FLAG_WRITE{
  471.                 $result $this->_db->query(sprintf('UPDATE %s SET vfs_perms = ?
  472.                                                     WHERE vfs_id = ?',
  473.                                                     $this->_params['table']),
  474.                                             array($perm$file[0]));
  475.                 return $result;
  476.             }
  477.         }
  478.  
  479.         return PEAR::raiseError(sprintf(_("Unable to change permission for VFS file %s/%s.")$path$name));
  480.     }
  481.  
  482. }

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