[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/inc/ -> class.vfs_sharing.inc.php (source)

   1  <?php
   2    /***************************************************************************\
   3    * eGroupWare - File Manager                                                 *
   4    * http://www.egroupware.org                                                 *
   5    * Written by:                                                               *
   6    *  - Vinicius Cubas Brand <viniciuscb@users.sourceforge.net>                *
   7    *  sponsored by Thyamad - http://www.thyamad.com                            *
   8    * ------------------------------------------------------------------------- *
   9    * Description: File Sharing class handler for VFS (SQL implementation v2)   *
  10    * ------------------------------------------------------------------------- *
  11    *  This program is free software; you can redistribute it and/or modify it  *
  12    *  under the terms of the GNU General Public License as published by the    *
  13    *  Free Software Foundation; either version 2 of the License, or (at your   *
  14    *  option) any later version.                                               *
  15    \***************************************************************************/
  16  
  17      // This class had to be created because file sharing work very differently
  18      // In fm2 than in fm.
  19  
  20      #FIXME this class is completely vfs_sql2 oriented. Must exist implementation
  21      # to other types
  22  
  23      class vfs_sharing
  24      {
  25          var $accounts;
  26          var $db;
  27          
  28          /*!
  29           * function vfs_sharing
  30           * @description Class constructor
  31           */
  32  		function vfs_sharing()
  33          {
  34              $this->accounts =& $GLOBALS['phpgw']->accounts;
  35              $this->db = clone($GLOBALS['phpgw']->db);
  36          }
  37  
  38          /*!
  39           * function set_permissions
  40           * @description Add specified permissions that do not exist, remove
  41           *    unspecified permissions that exist. Easier to call than 
  42           *    add_permissions and then remove_permissions
  43           * @param array $data in the following format:
  44           *         array(
  45           *           file_id => array(
  46           *              account_id  => acl_rights,
  47           *              account_id2 => acl_rights2,...
  48           *             ),
  49           *           file_id2 ... );
  50           */
  51  		function set_permissions($data)
  52          {
  53              //TODO see if a user have permissions in a file. Only if he have 
  54              //(or if is inside his homedir) he can change permissions
  55              if (!$data || !is_array($data))
  56              {
  57                  return false;
  58              }
  59              
  60              //search for permissions on files, to know which ones must be
  61              //updated/inserted
  62              reset($data);
  63              while(list($file_id,$account_ids) = each($data))
  64              {
  65                  $file_ids[] = $file_id;
  66              }
  67              
  68              $sql = 'SELECT * from phpgw_vfs2_shares 
  69                      WHERE file_id IN ('.implode(',',$file_ids).')';
  70  
  71              $this->db->query($sql,__LINE__,__FILE__);
  72  
  73              while ($this->db->next_record())
  74              {
  75                  $current_shares[$this->db->Record['file_id']][$this->db->Record['account_id']] = $this->db->Record['acl_rights']; 
  76              }
  77  
  78              //now that we have the current permissions, must know which ones to
  79              //insert/update and which ones to delete
  80              reset($data);
  81              while(list($file_id,$account_ids) = each($data))
  82              {
  83                  reset($account_ids);
  84                  while(list($account_id,$acl_rights) = each($account_ids))
  85                  {
  86                      //exists
  87                      if (array_key_exists($account_id,$current_shares[$file_id]))
  88                      {
  89                          if ($current_shares[$file_id][$account_id] != $acl_rights)
  90                          {
  91                              $insert[$file_id][$account_id] = $acl_rights;
  92                          }
  93  
  94                          unset($current_shares[$file_id][$account_id]);
  95                          
  96                      }
  97                      else
  98                      {
  99                          $insert[$file_id][$account_id] = $acl_rights;
 100                      }
 101                  }
 102              }
 103  
 104              //get which ones to delete
 105              reset($current_shares);
 106              while(list($file_id,$account_ids) = each($current_shares))
 107              {
 108                  if (is_array($account_ids))
 109                  {
 110                      reset($account_ids);
 111                      while(list($account_id,$acl_rights) = each($account_ids))
 112                      {
 113                          $delete[$file_id][$account_id] = $acl_rights;
 114                      }
 115                  }
 116              }
 117  
 118              foreach($insert as $file_id => $account_ids)
 119              {
 120                  $this->store_permissions($file_id,$account_ids);
 121              }
 122              
 123              foreach($delete as $file_id => $account_ids)
 124              {
 125                  $this->remove_permissions($file_id,$account_ids);
 126              }
 127  
 128              return true;
 129          }
 130  
 131          /*!
 132           * function store_qpermissions
 133           * @description Add/update new permissions to a file id
 134           * @param account_ids: array('account_id'=> acl_rights,acc_id2=>acl_r2,)
 135           */
 136  		function store_permissions($file_id,$account_ids)
 137          {
 138              if (!is_array($account_ids) || !is_numeric($file_id))
 139              {
 140                  return false;
 141              }
 142  
 143              foreach($account_ids as $account_id => $acl_rights)
 144              {
 145                  $this->db->insert('phpgw_vfs2_shares',
 146                      array('acl_rights'=>$acl_rights),
 147                      array('account_id'=>$account_id,'file_id'=>$file_id),
 148                      __LINE__,__FILE__);
 149              }
 150              return true;
 151          }
 152  
 153          /*!
 154           * function remove_permissions
 155           * @description Remove some permissions of a file id
 156           */
 157  		function remove_permissions($file_id,$account_ids)
 158          {
 159              if (!is_array($account_ids) || !is_numeric($file_id))
 160              {
 161                  return false;
 162              }
 163  
 164              //gets an array will all accounts as key
 165              $accounts = array_keys($account_ids);
 166              
 167              $this->db->delete('phpgw_vfs2_shares',
 168                  array('account_id'=>$accounts,'file_id'=>$file_id),
 169                  __LINE__,__FILE__);
 170  
 171              return true;
 172          }
 173  
 174          /**
 175           * Function: remove_all_permissions
 176           *
 177           *        Removes all permissions of a file 
 178           */
 179  		function remove_all_permissions($file_id)
 180          {
 181              $this->db->delete('phpgw_vfs2_shares',
 182                  array('file_id'=>$file_id),
 183                  __LINE__,__FILE__);
 184          }
 185  
 186          /*!
 187           * function get_permissions
 188           * @description This function will get the permissions set for a given
 189           *   file, makeing a simple query in the file repository. Does not
 190           *   search for permissions in parent dirs. If you want to know which
 191           *   is the permission for a user in a given file TAKING IN COUNT the
 192           *   parent dirs, use $this->get_file_permissions instead.
 193           * @param int file_id The id of the file
 194           * @result array with account_id as index, acl_rights as value
 195           * @author Vinicius Cubas Brand
 196           */
 197  		function get_permissions($file_id)
 198          {
 199              $this->db->select('phpgw_vfs2_shares','acl_rights,account_id',
 200                  array('file_id'=>$file_id),__LINE__,__FILE__);
 201  
 202              $result = array();
 203  
 204              while ($this->db->next_record())
 205              {
 206                  $result[$this->db->Record['account_id']] = $this->db->Record['acl_rights'];
 207              }
 208  
 209              return ($result);
 210  
 211          }
 212  
 213          /*!
 214           * function get_shares
 215           * @description Get all shares in which the user have $permission
 216           * @param $account_id The id of the user that can read the shared folder
 217           * @param $is_owner If true, will get only the shared folders that
 218           *   $account_id owns. Useful to get the shares that account_id owns
 219           *   and have configured himself (true), or instead the shares of the 
 220           *   others that he have $permission (false)
 221           * @result array with the list of the file_id's of all shares
 222           */
 223  		function get_shares($account_id,$is_owner=false,$permission=PHPGW_ACL_READ,$exclude_dir='')
 224          {
 225              $default_values = array(
 226                  'is_owner' => false,
 227                  'permission' => PHPGW_ACL_READ
 228                  );
 229          
 230              if (is_array($account_id))
 231              {
 232                  $account_id = array_merge($default_values,$account_id);
 233                  $is_owner = $account_id['is_owner'];
 234                  $permission = $account_id['permission'];
 235                  $exclude_dir = $account_id['exclude_dir'];
 236                  $only_dir = $account_id['only_dir'];
 237                  $account_id = $account_id['account_id'];
 238              }
 239          
 240              if ($exclude_dir)
 241              {
 242                  if (is_array($exclude_dir))
 243                  {
 244                      foreach ($exclude_dir as $dir)
 245                      {
 246                          $append .= " AND fls.directory NOT LIKE '".$dir."%' ";
 247                      }
 248                  }
 249                  else
 250                  {
 251                      $append .= " AND fls.directory NOT LIKE '".$exclude_dir."%' ";
 252                  }
 253              }
 254              elseif ($only_dir)
 255              {
 256                  $append .= " AND fls.directory LIKE '".$only_dir."%' ";
 257              }
 258          
 259              if ($is_owner)
 260              {
 261                  $sql = "SELECT DISTINCT sh.file_id     as file_id,
 262                                 sh.acl_rights  as acl_rights,
 263                                 fls.directory  as directory,
 264                                 fls.name       as name,
 265                                 fls.owner_id   as owner_id
 266                          FROM   phpgw_vfs2_shares as sh,
 267                                 phpgw_vfs2_files  as fls
 268                          INNER JOIN phpgw_vfs2_mimetypes mime on fls.mime_id = mime.mime_id
 269                          WHERE  sh.file_id = fls.file_id
 270                            AND  mime.mime = 'Directory'
 271                            AND  fls.shared = 'Y' 
 272                            $append
 273                            AND  fls.owner_id = $account_id";
 274              }
 275              else
 276              {
 277                  //gets the id of all groups $account_id belongs to
 278                  $groups = $GLOBALS['phpgw']->accounts->membership($account_id);
 279  
 280                  foreach($groups as $group)
 281                  {
 282                      $accounts[] = $group['account_id'];
 283                  }
 284  
 285                  $accounts[] = $account_id;
 286              
 287                  $sql = "SELECT DISTINCT sh.file_id     as file_id,
 288                                 sh.acl_rights  as acl_rights,
 289                                 fls.directory  as directory,
 290                                 fls.name       as name,
 291                                 fls.owner_id   as owner_id
 292                          FROM   phpgw_vfs2_shares as sh,
 293                                 phpgw_vfs2_files  as fls
 294                          INNER JOIN phpgw_vfs2_mimetypes mime on fls.mime_id = mime.mime_id
 295                          WHERE  sh.file_id = fls.file_id
 296                            AND  mime.mime = 'Directory'
 297                            AND  sh.account_id IN (".implode(',',$accounts).")
 298                            AND  fls.shared = 'Y' 
 299                            $append
 300                            AND  fls.owner_id != $account_id";
 301              }
 302  
 303              $this->db->query($sql,__LINE__,__FILE__);
 304  
 305              $res = array();
 306              while($this->db->next_record())
 307              {
 308                  if($this->db->Record['acl_rights'] & $permission)
 309                  {
 310                      $res[] = $this->db->Record;
 311                  }
 312              }
 313              
 314              //should be returned the array with complete file description
 315              return $res; 
 316          }
 317  
 318          /*!
 319           * function search_shares
 320           * @description Search for a shared folder which the user have
 321           *   $permission and have $keyword related (in directory or filename)
 322           * @result array with the list of all shares
 323           */
 324          //TODO search by file owner's name
 325  		function search_shares($account_id,$keyword,$permission=PHPGW_ACL_READ)
 326          {
 327              if ($account_id != ((int)$account_id))
 328              {
 329                  return false;
 330              }
 331  
 332              //gets the id of all groups $account_id belongs to
 333              $groups = $GLOBALS['phpgw']->accounts->membership($account_id);
 334  
 335              foreach($groups as $group)
 336              {
 337                  $accounts[] = $group['account_id'];
 338              }
 339  
 340              $accounts[] = $account_id;
 341          
 342              $sql = "SELECT DISTINCT sh.file_id     as file_id,
 343                             sh.acl_rights  as acl_rights,
 344                             fls.directory  as directory,
 345                             fls.name       as name,
 346                             fls.owner_id   as owner_id
 347                      FROM   phpgw_vfs2_shares as sh,
 348                             phpgw_vfs2_files  as fls
 349                      WHERE  sh.file_id = fls.file_id
 350                        AND  sh.account_id IN (".implode(',',$accounts).")
 351                        AND  (  fls.directory LIKE '%$keyword%' 
 352                             OR fls.name LIKE '%$keyword%')
 353                        AND  fls.shared = 'Y' 
 354                        AND  fls.owner_id != $account_id";
 355  
 356              $this->db->query($sql,__LINE__,__FILE__);
 357  
 358              while($this->db->next_record())
 359              {
 360                  if ($this->db->Record['acl_rights'] & $permission)
 361                  {
 362                      $res[] = $this->db->Record;
 363                  }
 364              }
 365              return $res;
 366          }
 367  
 368          /**
 369           * Function: get_file_permissions
 370           *
 371           *        Gets the permissions for a user in a given file For files in a
 372           *        shared dir, will get the acl rights of the parent dir, and if
 373           *        not specified, of the parent of the parent, and so on.  NOTE:
 374           *        this consider that files CANNOT have permissions set, only
 375           *        their parent dir, and the file inherits the nearer parent with
 376           *        permissions defined (even if these permissions are NONE)
 377           *
 378           * @result int some mask of various PHPGW_ACL_*
 379           */
 380  		function get_file_permissions($account_id,$file_id)
 381          {
 382              //get directory/file names
 383              $this->db->select('phpgw_vfs2_files','directory,name',
 384                  array('file_id' => $file_id),__LINE__,__FILE__);
 385  
 386              $this->db->next_record();
 387  
 388              $directory = $this->db->Record['directory'];
 389              $name = $this->db->Record['name'];
 390  
 391              $fullname = $directory.'/'.$name;
 392  
 393              $parent_dirs = array();
 394      
 395              $dirs_expl = explode('/',$fullname);
 396  
 397              //put all parents hierarchy in an array
 398              $parent_dirs_array[]=$fullname;
 399              while(1)
 400              {
 401                  array_pop($dirs_expl);
 402                  if($dirs_expl[1])
 403                  {
 404                      $parent_dirs_array[]=implode('/',$dirs_expl);
 405                  }
 406                  else
 407                  {
 408                      $parent_dirs_array[]='/';
 409                      break;
 410                  }
 411              }
 412  
 413              //gets the id of all groups $account_id belongs to
 414              $groups = $GLOBALS['phpgw']->accounts->membership($account_id);
 415  
 416              foreach($groups as $group)
 417              {
 418                  $accounts[] = $group['account_id'];
 419              }
 420  
 421              $accounts[] = $account_id;
 422              $accounts[] = 0; //default permission for all users
 423  
 424              $accounts = implode(',',$accounts);
 425  
 426              //searches for information in the parent dirs
 427              for($i=0; $i<count($parent_dirs_array);$i++)
 428              {
 429                  $dir_name = array_pop(explode('/',$parent_dirs_array[$i]));
 430                  if ($dir_name)
 431                  {
 432  
 433                      //if file have a reg in table, will try to see if it is
 434                      //with permissions to the current user
 435                      $sql = "SELECT sh.acl_rights  as acl_rights,
 436                                     fls.directory  as directory,
 437                                     fls.name       as name,
 438                                     fls.owner_id   as owner_id,
 439                                     fls.shared     as shared
 440                              FROM    phpgw_vfs2_files  as fls,
 441                                      phpgw_vfs2_shares as sh
 442                              WHERE  fls.file_id = sh.file_id
 443                                AND  fls.directory = '".$parent_dirs_array[$i+1]."' 
 444                                AND  fls.name      = '".$dir_name."'
 445                                AND  fls.shared    = 'Y'";
 446  
 447                      $this->db->query($sql,__LINE__,__FILE__);
 448  
 449                      if ($this->db->next_record())
 450                      {
 451  
 452                          $sql = "SELECT sh.acl_rights  as acl_rights,
 453                                         fls.directory  as directory,
 454                                         fls.name       as name,
 455                                         fls.owner_id   as owner_id,
 456                                         fls.shared     as shared
 457                                  FROM    phpgw_vfs2_files  as fls,
 458                                          phpgw_vfs2_shares as sh
 459                                  WHERE  (sh.account_id IN ($accounts)
 460                                         OR fls.owner_id  = $account_id)
 461                                    AND  fls.file_id = sh.file_id
 462                                    AND  fls.directory = '".$parent_dirs_array[$i+1]."' 
 463                                    AND  fls.name      = '".$dir_name."'
 464                                    AND  fls.shared    = 'Y'";
 465  
 466                          $this->db->query($sql,__LINE__,__FILE__);
 467  
 468                          
 469      /*
 470                          $this->db->select('phpgw_vfs2_files','file_id',
 471                              array('directory'=>$parent_dirs_array[$i+1],
 472                                    'name'=>$dir_name), __LINE__,__FILE__);
 473  
 474                          $this->db->next_record();
 475                          
 476                          $this->db->select('phpgw_vfs2_shares','acl_rights',
 477                              array('file_id'=>$this->db->Record['file_id'],
 478                                    'account_id'=>$account_id),__LINE__,__FILE__);*/
 479  
 480  
 481                                    
 482  
 483  //                        echo "tested file: ".$dir_name." \n<br>";
 484  //                        echo $sql."<br><br>\n\n";
 485                          while ($this->db->next_record())
 486                          {
 487  //                            echo "results for file: ".$dir_name." \n<br>";
 488                              if ($this->db->Record['owner_id'] == $account_id)
 489                              {
 490                                  //the user can do anything with any dir or file
 491                                  //inside a dir that belongs to him.
 492                                  return PHPGW_ACL_READ|PHPGW_ACL_EDIT|PHPGW_ACL_ADD;
 493                              }
 494                              else
 495                              {
 496                                  $entered = true;
 497                                  $result |= $this->db->Record['acl_rights'];
 498                              }
 499                          }
 500                          if($entered)
 501                          {
 502                              return $result;
 503                          }
 504                          else
 505                          {
 506                              return 0;
 507                          }
 508                      }
 509                  }
 510              }
 511              return false;
 512          }
 513  
 514      }
 515  
 516  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7