phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] phpgwapi/inc/class.vfs_shared.inc.php, 1.6


From: nomail
Subject: [Phpgroupware-cvs] phpgwapi/inc/class.vfs_shared.inc.php, 1.6
Date: Thu, 30 Dec 2004 10:00:31 +0100

Update of /phpgwapi/inc
Modified Files:
        Branch: 
          class.vfs_shared.inc.php

date: 2004/12/30 09:00:31;  author: skwashd;  state: Exp;  lines: +435 -5

Log Message:
vfs and dav fixes - patch #3525
=====================================================================
Index: phpgwapi/inc/class.vfs_shared.inc.php
diff -u phpgwapi/inc/class.vfs_shared.inc.php:1.5 
phpgwapi/inc/class.vfs_shared.inc.php:1.6
--- phpgwapi/inc/class.vfs_shared.inc.php:1.5   Thu Dec 30 06:47:31 2004
+++ phpgwapi/inc/class.vfs_shared.inc.php       Thu Dec 30 09:00:31 2004
@@ -163,6 +163,21 @@
                var $fakebase = '/home';
 
                /*
+               * working_id is the current user account_id under which we are 
working
+               */
+               var $working_id;
+
+               /*
+               * working_lid is the current acount_lid of the urrent user
+               */
+               var $working_lid;
+
+               /*
+               * now the creation time of this class ...
+               */
+               var $now;
+
+               /*
                 * All derived classes must store certain information about each
                 * location.  The attributes in the 'attributes' array represent
                 * the minimum attributes that must be stored.  Derived classes
@@ -197,6 +212,21 @@
                 */
                function vfs_shared ()
                {
+                       $this->basedir = 
$GLOBALS['phpgw_info']['server']['files_dir'];
+                       $this->working_id = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->working_lid = 
$GLOBALS['phpgw']->accounts->id2name($this->working_id);
+                       $this->now = date ('Y-m-d');
+                       /* These are stored in the MIME-type field and should 
normally 
+                       * be ignored.
+                       * Adding a type here will ensure it is normally 
ignored, 
+                       * but you will have to explicitly add it to acl_check 
(), and to 
+                       * any other SELECT's in this file
+                       */
+
+                       $this->meta_types = array ('journal', 
'journal-deleted');
+
+                       //Load the override_locks
+                       $this->locks_restore_session();
                }
 
                /*
@@ -254,6 +284,202 @@
                function get_journal ($data) { return array(array()); }
 
                /*
+                * Locking functions
+                *
+                * WARNING: THESE ARE PROPOSALS, and could be modified without 
advice.
+                *
+                * Overview:
+                * Each derived class should have some kind of
+                * Locking capability. This is not MANDATORY
+                * but very Usefull. At the time of writing, only
+                * DAV get such native capability.
+                * SQL locking could be the next LOCK enabled class
+                * with a dedicated table for example.
+                *
+                * PUBLIC Function:
+                * lock - put a WRITE lock on a file / directory
+                * unlock - remove a lock on a file / directory
+                * lock_token - get the lock token
+                *
+                * SHARED Functions: (they don't need to be implemented !)
+                * add_lock_override - Persistent (trought session) lock 
override for normal operation (mv, cp, rm, write, read, ...)
+                * remove_lock_override - stops overriding a lock
+                * and PRIVATE:
+                * locks_save_session - Save the ovveride_locks array into 
session
+                * locks_restore_session - Restore the override_locks array 
from session
+                */
+               /*!
+               * string str_error : contains an error about lock
+               */
+               var $str_lock_error = '';
+
+               /*!
+               * array override_locks
+               * @discussion : This array contains all our locks to override 
automatically the locks
+               * when we are writing/deleting etc... and we are the locker.
+               */
+               var $override_locks = array();
+
+               /*!
+                * @function lock
+                * @abstract put a WRITE lock on the file. The lock is EXCLUSIVE
+                * @param array data : 'string' => the filename / dirname
+                *                     'relatives'=> an array(0 => RETATIVITY) 
containing the relativity.
+                *                     'owner_lid' => the phpgw owner of the 
lock (lid)
+                *                     'timeout' => In second the time for the 
lock, or 'Infinite' for a notimeout lock
+                * @return True on Success False otherwise and 
$this->str_lock_error SHOULD contains an indication
+                */
+               function lock ($data) { 
+                       $this->str_lock_error = 'Method not implemented';
+                       return False;
+               }
+
+               /*!
+                * @function unlock
+                * @abstract remove the WRITE lock on the file.
+                * @param array data : 'string' => the filename / dirname
+                *                     'relatives' => the relativity array,
+                *                     'token' => the lock token to remove
+                * @param string token : the token string to remove.
+                * @return True on Success, False otherwise, in which case 
$this->str_lock_error SHOULD contains an indication
+                */
+               function unlock ($data) { 
+                       $this->str_lock_error = 'Method not implemented';
+                       return False; 
+               }
+               
+               /*!
+                * @function lock_token
+                * @abstract retrieve a lock token from a given file /dir
+                * @param array data : 'string' => the filename / dirname
+                *                     'relatives' => the relativity array
+                *                     'owner_lid' => (optional) the matching 
owner_lid, if this is SET and Empty 
+                *                                    then lock_token will only 
return a token for the current user, 
+                *                                    if it's not empty, only 
this owner_lid will be matched. Thus we permit
+                *                                    Group lock_token.
+                * @return string lock_token or boolean False if no token was 
mathed / or present.
+                * @discussion : If no owner_lid is specified nor set, it will 
return the last token of the file Only if the user have any right
+                * on this dir/file.
+                */
+               function lock_token($data) { return False; }
+
+               /*
+               * Lock SHARED Function, you don't need to overload these
+               */
+               /*!
+               * @function add_lock_override
+               * @abstract override a lock
+               * @param string filename
+               * @param relatives Relativity array
+               * @param token (optional) a token for the lock we want to 
override
+               * @result None
+               * @discussion locks are no good unless you can write to a file 
you yourself locked:
+               * to do this call add_lock_override with the lock token (or 
without it - it will
+               * find it itself, so long as there is only one).  lock_override 
info is stored in
+               * the groupware session, so it will persist between page loads, 
but will be lost 
+               * when the browser is closed
+               */      
+               function add_lock_override($data)
+               {
+                       $default_values = array
+                       (
+                               'relatives'     => array (RELATIVE_CURRENT),
+                               'token' => '',
+                               'owner_lid' => '',
+                               
+                       );
+
+                       $data = array_merge ($this->default_values ($data, 
$default_values), $data);
+                       
+                       if (!strlen($data['token']))
+                       {
+                                $token = $this->lock_token($data);
+                       }
+                       else
+                       {
+                               $token = $data['token'];
+                       }
+                        
+                       $p = $this->path_parts (array(
+                                       'string'        => $data['string'],
+                                       'relatives'     => array 
($data['relatives'][0])
+                               )
+                       );              
+                       $this->override_locks[$p->real_full_path] = $token;
+                       $this->locks_save_session();
+               }
+               
+               /*!
+               * @function remove_lock_override
+               * @abstract stops overriding a lock
+               * @param string filename
+               * @param relatives Relativity array
+               * @result None
+               */      
+               function remove_lock_override($data)
+               {
+                       $default_values = array
+                       (
+                               'relatives'     => array (RELATIVE_CURRENT),
+                               'owner_lid' => ''
+                               
+                       );
+
+                       $data = array_merge ($this->default_values ($data, 
$default_values), $data);
+                       
+                       if (!strlen($data['token']))
+                       {
+                                $token = $this->lock_token($data);
+                       }
+                       else
+                       {
+                               $token = $data['token'];
+                       }
+                        
+                       $p = $this->path_parts (array(
+                                       'string'        => $data['string'],
+                                       'relatives'     => array 
($data['relatives'][0])
+                               )
+                       );              
+                       unset($this->override_locks[$p->real_full_path]);
+                       $this->locks_save_session();
+               }
+               
+               /* Private */
+
+               /*
+               * @function locks_save_session
+               * @discussion : save the override_locks array in session file
+               */      
+               function locks_save_session()
+               {
+                       //Save the overrided locks in the session
+                       $app = $GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->session = $GLOBALS['phpgw']->session->appsession 
('vfs_shared',$app, base64_encode(serialize($this->override_locks)));
+               }       
+
+               /*
+               * @function locks_restore_session
+               * @discussion: restore the override_locks array from session, 
use only in vfs_shared
+               */
+               function locks_restore_session()
+               {
+                       //Reload the overriden_locks
+                       $app = $GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $session_data = 
base64_decode($GLOBALS['phpgw']->session->appsession ('vfs_shared',$app));
+                       if ($session_data)
+                       {
+                               $this->override_locks = 
unserialize($session_data);
+                       }
+                       else
+                       {
+                               $this->override_locks = array();
+                       }
+               }
+
+               /* End Private Lock function */
+
+               /*
                 * Access checking functions.
                 *
                 * Overview:
@@ -441,6 +667,8 @@
                 *
                 * set_attributes - Set attributes for a location
                 *
+                * correct_attributes - "correct" the owner id for a location 
(SHARED)
+                *
                 * file_exists - Check if a location (file or directory) exists
                 *
                 * get_size - Determine size of location
@@ -462,6 +690,69 @@
                 function set_attributes ($data) { return False; }
 
                /*!
+               @function correct_attributes
+               @abstract Set the correct attributes for 'string' (e.g. owner)
+               @param string File/directory to correct attributes of
+               @param relatives Relativity array
+               @result Boolean True/False
+               */
+               function correct_attributes ($data)
+               {
+                       if (!is_array ($data))
+                       {
+                               $data = array ();
+                       }
+
+                       $default_values = array
+                               (
+                                       'relatives'     => array 
(RELATIVE_CURRENT)
+                               );
+
+                       $data = array_merge ($this->default_values ($data, 
$default_values), $data);
+
+                       $p = $this->path_parts (array(
+                                       'string'        => $data['string'],
+                                       'relatives'     => array 
($data['relatives'][0])
+                               )
+                       );
+
+                       if ($p->fake_leading_dirs != $this->fakebase && 
$p->fake_leading_dirs != '/')
+                       {
+                               $ls_array = $this->ls (array(
+                                               'string'        => 
$p->fake_leading_dirs,
+                                               'relatives'     => array 
($p->mask),
+                                               'checksubdirs'  => False,
+                                               'nofiles'       => True
+                                       )
+                               );
+                               $set_attributes_array = Array(
+                                       'owner_id' => $ls_array[0]['owner_id']
+                               );
+                       }
+                       elseif (preg_match ("+^$this->fakebase\/(.*)$+U", 
$p->fake_full_path, $matches))
+                       {
+                               $set_attributes_array = Array(
+                                       'owner_id' => 
$GLOBALS['phpgw']->accounts->name2id ($matches[1])
+                               );
+                       }
+                       else
+                       {
+                               $set_attributes_array = Array(
+                                       'owner_id' => 0
+                               );
+                       }
+
+                       $this->set_attributes (array(
+                                       'string'        => $p->fake_full_path,
+                                       'relatives'     => array ($p->mask),
+                                       'attributes'    => $set_attributes_array
+                               )
+                       );
+
+                       return True;
+               }
+
+               /*!
                 * @function file_exists
                 * @abstract Check if a location (file or directory) exists
                 * @required string     Path to location
@@ -479,7 +770,80 @@
                 *                              all subdirectories recursively.
                 * @result Integer.  Size of location in bytes.
                 */
-               function get_size ($data) { return 0; }
+               function get_size ($data)
+               {
+                       if (!is_array ($data))
+                       {
+                               $data = array ();
+                       }
+
+                       $default_values = array
+                               (
+                                       'relatives'     => array 
(RELATIVE_CURRENT),
+                                       'checksubdirs'  => True,
+                                       'nofiles' => False
+                               );
+
+                       $data = array_merge ($this->default_values ($data, 
$default_values), $data);
+
+                       $p = $this->path_parts (array(
+                                       'string'        => $data['string'],
+                                       'relatives'     => array 
($data['relatives'][0])
+                               )
+                       );
+
+                       if (!$this->acl_check (array(
+                                       'string'        => $p->fake_full_path,
+                                       'relatives'     => array ($p->mask),
+                                       'operation'     => PHPGW_ACL_READ,
+                                       'must_exist'    => True
+                               ))
+                       )
+                       {
+                               return False;
+                       }
+
+                       /*
+                          WIP - this should run through all of the 
subfiles/directories in the directory and tally up
+                          their sizes.  Should modify ls () to be able to 
return a list for files outside the virtual root
+                       */
+                       if ($p->outside){
+                         return filesize($p->real_full_path);
+                       }
+
+                       $ls_array = $this->ls (array(
+                                       'string'        => $p->fake_full_path,
+                                       'relatives'     => array ($p->mask),
+                                       'checksubdirs'  => 
$data['checksubdirs'],
+                                       'nofiles'       => 
!$data['checksubdirs']
+                               )
+                       );
+
+                       //The virtual size of the current directory
+                       if ( $this->file_type($data) == 'Directory' )
+                       {
+                               $size = 4096;
+                       }
+                       else
+                       {
+                               $size = 0;
+                       }
+                       while (list ($num, $file_array) = each ($ls_array))
+                       {
+                               /*
+                                  Make sure the file is in the directory we 
want, and not
+                                  some deeper nested directory with a similar 
name
+                               */
+/*
+                               if (@!ereg ('^' . $file_array['directory'], 
$p->fake_full_path))
+                               {
+                                       continue;
+                               }
+*/
+                               $size += $file_array['size'];
+                       }
+                       return $size;
+               }
 
                /*!
                 * @function ls
@@ -545,7 +909,7 @@
                 * update_real - Ensure that information about a location is
                 *               up-to-date
                 */
-
+                
                /*!
                 * @function update_real
                 * @abstract Ensure that information about a location is 
up-to-date
@@ -592,6 +956,8 @@
                 *
                 * PUBLIC functions (mandatory):
                 *
+                * file_type - return the file type of a given file/dir (need 
to be overloaded !)
+                *
                 * set_relative - Sets the current relativity, the relativity 
used
                 *                when RELATIVE_CURRENT is passed to a function
                 *
@@ -619,6 +985,49 @@
                /* PRIVATE functions */
 
                /*!
+               @function checkperms
+               @abstract Check if $this->working_id has write access to create 
files in $dir
+               @discussion Simple call to acl_check
+               @param string Directory to check access of
+               @param relatives Relativity array
+               @result Boolean True/False
+               */
+               function checkperms ($data)
+               {
+                       if (!is_array ($data))
+                       {
+                               $data = array ();
+                       }
+
+                       $default_values = array
+                               (
+                                       'relatives'     => array 
(RELATIVE_CURRENT)
+                               );
+
+                       $data = array_merge ($this->default_values ($data, 
$default_values), $data);
+
+                       $p = $this->path_parts (array(
+                                       'string'        => $data['string'],
+                                       'relatives'     => array 
($data['relatives'][0])
+                               )
+                       );
+
+                       if (!$this->acl_check (array(
+                                       'string'        => $p->fake_full_path,
+                                       'relatives'     => array ($p->mask),
+                                       'operation'     => PHPGW_ACL_ADD
+                               ))
+                       )
+                       {
+                               return False;
+                       }
+                       else
+                       {
+                               return True;
+                       }
+               }
+
+               /*!
                 * @function securitycheck
                 * @abstract Check if location string is ok to use in VFS 
functions
                 * @discussion Checks for basic violations such as ..
@@ -803,10 +1212,13 @@
                        }
 
                        /* Let's not return // */
+                       //XXX If $basedir contains http(s):// what are we doing 
??? Caeies
+                       $basedir = ereg_replace('://','DOTSLASHSLASH',$basedir);
                        while (ereg ($sep . $sep, $basedir))
                        {
                                $basedir = ereg_replace ($sep . $sep, $sep, 
$basedir);
                        }
+                       $basedir = ereg_replace('DOTSLASHSLASH','://',$basedir);
 
                        $basedir = ereg_replace ($sep . '$', '', $basedir);
 
@@ -858,7 +1270,22 @@
                        return '';
                }
 
-               /* PUBLIC functions (mandatory) */
+               /* PUBLIC functions (mandatory) they don't need to be 
implemented
+               until you want to overload them ! (except file_type)
+               */
+
+               /*!
+                * @function file_type
+                * @abstract Return a string contianing the mime-type of the 
given data
+                * @param array $data : contains 'string' => path to what we 
want to get the mimetype
+                *                               'relatives' (optional) => 
array of relatives
+                * @return string $mime-type, could be empty if we don't know 
the file type, 
+                * or perhaps a default mime-type (what about 
'application/octet-stream' ?)
+                */
+               function file_type($data)
+               {
+                       return 'application/octet-stream';
+               }
 
                /*!
                 * @function set_relative
@@ -949,7 +1376,7 @@
                        {
                                $data = array ();
                        }
-
+//                     $data['string'] = 
preg_replace('#[/]+#','/',$data['string']);
                        $default_values = array
                                (
                                        'relatives'     => array 
(RELATIVE_CURRENT),
@@ -1006,7 +1433,6 @@
 
                                $rarray['real_full_path'] = $string;
                        }
-
                        /* This is needed because of substr's handling of 
negative lengths */
                        $baselen = strlen ($base);
                        $lastslashpos = strrpos ($string, $base_sep);
@@ -1407,6 +1833,10 @@
 
                function default_values ($data, $default_values)
                {
+                       if(!is_array($data)) 
+                       {
+                               $data=array();
+                       }
                        for ($i = 0; list ($key, $value) = each 
($default_values); $i++)
                        {
                                if (!isset ($data[$key]))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]