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

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