home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / filesystem.class.inc < prev    next >
Text File  |  2004-03-08  |  15KB  |  668 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12.  
  13. class filesystem extends db
  14. {
  15.     var $search_results = array();
  16.     var $disable_go_permissions = false;
  17.  
  18.     function is_sub_dir($sub_path, $parent_path)
  19.     {
  20.          if (strpos($sub_path, $parent_path) === false)
  21.          {
  22.              return false;
  23.          }else
  24.          {
  25.              if (substr($sub_path, strlen($parent_path), 1) != '/')
  26.              {
  27.                  return false;
  28.              }
  29.          }
  30.          return true;
  31.     }
  32.  
  33.     function filesystem($disable_go_permissions=false)
  34.     {
  35.         $this->db();
  36.         $this->disable_go_permissions = $disable_go_permissions;
  37.     }
  38.  
  39.     function is_home_path($user_id, $path)
  40.     {
  41.         global $GO_CONFIG;
  42.         require_once($GO_CONFIG->class_path.'users.class.inc');
  43.         $users = new users();
  44.         if ($users->get_user($user_id))
  45.         {
  46.             $home_path = $GO_CONFIG->file_storage_path.$users->f('username');
  47.  
  48.             if (dirname($path).basename($path) == dirname($home_path).basename($home_path))
  49.             {
  50.                 return true;
  51.             }
  52.         }
  53.         return false;
  54.     }
  55.  
  56.     function is_owner($user_id, $path)
  57.     {
  58.         global $GO_CONFIG;
  59.         require_once($GO_CONFIG->class_path.'users.class.inc');
  60.         $users = new users();
  61.         if ($users->get_user($user_id))
  62.         {
  63.             $home_path = $GO_CONFIG->file_storage_path.$users->f('username');
  64.  
  65.             if (strpos($path, $home_path) === 0)
  66.             {
  67.                 return true;
  68.             }
  69.         }
  70.         return false;
  71.     }
  72.  
  73.     function get_shares($user_id)
  74.     {
  75.         //ORDER BY PATH important so higher order shares come first
  76.         $sql = "SELECT * FROM fsShares WHERE user_id='$user_id' ORDER BY path ASC";
  77.         $this->query($sql);
  78.         return $this->num_rows();
  79.     }
  80.  
  81.     function get_my_shares()
  82.     {
  83.         global $GO_SECURITY;
  84.         $sql = "SELECT DISTINCT fsShares.user_id FROM fsShares, acl, users_groups WHERE (".
  85.             "fsShares.acl_read = acl.acl_id OR fsShares.acl_write = acl.acl_id AND fsShares.user_id != ".$GO_SECURITY->user_id.
  86.             ") AND ( ( acl.group_id = users_groups.group_id AND users_groups.user_id = ".$GO_SECURITY->user_id." AND acl.user_id = 0 ) OR (".
  87.             "acl.group_id = 0 AND acl.user_id = ".$GO_SECURITY->user_id." ) )";
  88.         $this->query($sql);
  89.         $list = array();
  90.         while ( $this->next_record() ) {
  91.             $list[] = $this->f('user_id');
  92.         }
  93.         return $list;
  94.     }
  95.  
  96.     function get_authorized_sharers($user_id)
  97.     {
  98.         global $GO_SECURITY;
  99.                 $sql = "SELECT DISTINCT fsShares.user_id FROM fsShares, acl, users_groups WHERE (".
  100.                     "fsShares.acl_read = acl.acl_id OR fsShares.acl_write = acl.acl_id AND fsShares.user_id != ".$user_id.
  101.                     ") AND ( ( acl.group_id = users_groups.group_id AND users_groups.user_id = ".$user_id." AND acl.user_id = 0 ) OR (".
  102.                     "acl.group_id = 0 AND acl.user_id = ".$user_id." ) )";
  103.         $this->query($sql);
  104.         return $this->num_rows();
  105.     }
  106.  
  107.     function add_share($user_id, $path)
  108.     {
  109.         $path = addslashes($path);
  110.  
  111.         global $GO_SECURITY;
  112.         $acl_read = $GO_SECURITY->get_new_acl('read: '.$path);
  113.         $acl_write = $GO_SECURITY->get_new_acl('write: '.$path);
  114.         if($acl_read && $acl_write)
  115.         {
  116.             $sql = "INSERT INTO fsShares (user_id, path, acl_read, acl_write) VALUES ('$user_id', '$path', '$acl_read', '$acl_write')";
  117.             return $this->query($sql);
  118.         }else
  119.         {
  120.             $GO_SECURITY->delete_acl($acl_read);
  121.             $GO_SECURITY->delete_acl($acl_write);
  122.         }
  123.  
  124.         return false;
  125.     }
  126.  
  127.     function delete_share($path)
  128.     {
  129.         if ($share = $this->get_share($path))
  130.         {
  131.             $path = addslashes($path);
  132.             global $GO_SECURITY;
  133.             $GO_SECURITY->delete_acl($share['acl_read']);
  134.             $GO_SECURITY->delete_acl($share['acl_write']);
  135.  
  136.             $sql = "DELETE FROM fsShares WHERE path='$path'";
  137.             return $this->query($sql);
  138.         }
  139.  
  140.         return false;
  141.     }
  142.  
  143.     function update_share($old_path, $new_path)
  144.     {
  145.         $new_path = addslashes($new_path);
  146.         $old_path = addslashes($old_path);
  147.  
  148.         $sql = "UPDATE fsShares SET path='$new_path' WHERE path='$old_path'";
  149.         return $this->query($sql);
  150.     }
  151.  
  152.     function get_share($path)
  153.     {
  154.         $path = addslashes($path);
  155.         $sql = "SELECT * FROM fsShares WHERE path='$path'";
  156.         $this->query($sql);
  157.         if($this->next_record())
  158.         {
  159.             return $this->Record;
  160.         }
  161.         return false;
  162.     }
  163.  
  164.     function find_share($path)
  165.     {
  166.         if ($share = $this->get_share($path))
  167.         {
  168.             return $share;
  169.         }else
  170.         {
  171.             global $GO_CONFIG;
  172.             $parent = dirname($path);
  173.             if ($parent == $GO_CONFIG->file_storage_path || $parent == '' || $parent == $GO_CONFIG->slash)
  174.             {
  175.                 return false;
  176.             }else
  177.             {
  178.                 return $this->find_share($parent);
  179.             }
  180.         }
  181.     }
  182.  
  183.     function has_read_permission($user_id, $path)
  184.     {
  185.         if ($this->disable_go_permissions || $this->is_owner($user_id, $path))
  186.         {
  187.             return is_readable($path);
  188.         }else
  189.         {
  190.             if ($share = $this->find_share($path))
  191.             {
  192.                 global $GO_SECURITY;
  193.                 if($GO_SECURITY->has_permission($user_id, $share['acl_read']))
  194.                 {
  195.                     return is_readable($path);
  196.                 }
  197.             }
  198.             global $GO_CONFIG;
  199.             if (strpos($path, $GO_CONFIG->tmpdir) === 0)
  200.             {
  201.                 return is_readable($path);
  202.             }
  203.         }
  204.         return $this->has_write_permission($user_id, $path);
  205.     }
  206.  
  207.     function has_write_permission($user_id, $path)
  208.     {
  209.         if ($this->disable_go_permissions || $this->is_owner($user_id, $path))
  210.         {
  211.             return is_writable($path);
  212.         }else
  213.         {
  214.             global $GO_SECURITY;
  215.             if ($share = $this->find_share($path))
  216.             {
  217.                 if($GO_SECURITY->has_permission($user_id, $share['acl_write']))
  218.                 {
  219.                     return is_writable($path);
  220.                 }
  221.             }
  222.             global $GO_CONFIG;
  223.             if (strpos($path, $GO_CONFIG->tmpdir) === 0)
  224.             {
  225.                 return is_writable($path);
  226.             }
  227.         }
  228.         return false;
  229.     }
  230.  
  231.     function size($path)
  232.     {
  233.         if (is_dir($path))
  234.         {
  235.             $size = 0;
  236.             $children = $this->get_folders($path);
  237.             while ($child = array_shift($children))
  238.             {
  239.                 $size += $this->size($child['path']);
  240.             }
  241.  
  242.             $files = $this->get_files($path);
  243.             while ($file = array_shift($files))
  244.             {
  245.                 $size += $file['size'];
  246.             }
  247.             return $size;
  248.         }else
  249.         {
  250.             return filesize($path);
  251.         }
  252.     }
  253.  
  254.     function move($source_path, $destination_path)
  255.     {
  256.         global $GO_CONFIG;
  257.         //do not move into own path
  258.         $source_dir_count = count(explode('/',$source_path));
  259.         $destination_dir_count = count(explode('/',$destination_path));
  260.  
  261.         if ((strpos($destination_path, $source_path) === 0) && ($destination_dir_count > $source_dir_count))
  262.         {
  263.             return false;
  264.         }elseif($source_path == $destination_path)
  265.         {
  266.             return true;
  267.         }else
  268.         {
  269.             if (is_dir($source_path))
  270.             {
  271.                 if (!file_exists($destination_path))
  272.                 {
  273.                     if (!mkdir($destination_path, $GO_CONFIG->create_mode))
  274.                     {
  275.                         return false;
  276.                     }else
  277.                     {
  278.                         if ($this->get_share($source_path))
  279.                         {
  280.                             $this->update_share($source_path, $destination_path);
  281.                         }
  282.                     }
  283.                 }
  284.                 $files = $this->get_files($source_path);
  285.                 while ($file = array_shift($files))
  286.                 {
  287.                     if(!$this->move($file['path'], $destination_path.'/'.$file['name']))
  288.                     {
  289.                         return false;
  290.                     }
  291.                 }
  292.                 $children = $this->get_folders($source_path);
  293.                 while ($child = array_shift($children))
  294.                 {
  295.  
  296.                     if (!$this->move($child['path'], $destination_path.'/'.$child['name']))
  297.                     {
  298.                         return false;
  299.                     }else
  300.                     {
  301.                         if ($this->get_share($child['path']))
  302.                         {
  303.                             $this->update_share($child['path'], $destination_path.'/'.$child['name']);
  304.                         }
  305.                     }
  306.                 }
  307.                 return rmdir($source_path);
  308.             }else
  309.             {
  310.                 #rename fails when moving accross partitions
  311.                 #return rename($source_path, $destination_path);
  312.                 if (copy($source_path, $destination_path))
  313.                 {
  314.  
  315.                     return unlink($source_path);
  316.                 }
  317.             }
  318.         }
  319.     }
  320.  
  321.     function copy($source_path, $destination_path)
  322.     {
  323.         global $GO_CONFIG;
  324.         //do not copy into own path
  325.         if (strpos($destination_path, $source_path) === 0)
  326.         {
  327.             return false;
  328.         }else
  329.         {
  330.             if (is_dir($source_path))
  331.             {
  332.                 if (!file_exists($destination_path))
  333.                 {
  334.                     if (!mkdir($destination_path, $GO_CONFIG->create_mode))
  335.                     {
  336.                         return false;
  337.                     }
  338.                 }
  339.                 $files = $this->get_files($source_path);
  340.                 while ($file = array_shift($files))
  341.                 {
  342.                     if(!$this->copy($file['path'], $destination_path.'/'.$file['name']))
  343.                     {
  344.                         return false;
  345.                     }
  346.                 }
  347.                 $children = $this->get_folders($source_path);
  348.                 while ($child = array_shift($children))
  349.                 {
  350.                     if (!$this->copy($child['path'], $destination_path.'/'.$child['name']))
  351.                     {
  352.                         return false;
  353.                     }
  354.                 }
  355.                 return true;
  356.             }else
  357.             {
  358.                 return copy($source_path, $destination_path);
  359.             }
  360.         }
  361.     }
  362.  
  363.     function delete($path)
  364.     {
  365.         global $GO_SECURITY;
  366.         if (is_dir($path))
  367.         {
  368.             $children = $this->get_folders($path);
  369.             while ($child = array_shift($children))
  370.             {
  371.                 if (!$this->delete($child['path']))
  372.                 {
  373.                     return false;
  374.                 }
  375.             }
  376.  
  377.             $files = $this->get_files($path);
  378.             while ($file = array_shift($files))
  379.             {
  380.                 if (!$this->delete($file['path']))
  381.                 {
  382.                     return false;
  383.                 }
  384.             }
  385.             if ($this->has_write_permission($GO_SECURITY->user_id, $path) || $this->disable_go_permissions)
  386.             {
  387.                 if ($this->get_share($path))
  388.                 {
  389.                     $this->delete_share($path);
  390.                 }
  391.                 return @rmdir($path);
  392.             }else
  393.             {
  394.                 return false;
  395.             }
  396.         }else
  397.         {
  398.             if ($this->has_write_permission($GO_SECURITY->user_id, $path) || $this->disable_go_permissions)
  399.             {
  400.                 return @unlink($path);
  401.             }else
  402.             {
  403.                 return false;
  404.             }
  405.         }
  406.     }
  407.  
  408.     function get_parent_path($path)
  409.     {
  410.         $last_folder_pos = strrpos($path, '/');
  411.         if (is_integer($last_folder_pos))
  412.         {
  413.             if ($last_folder_pos === 0)
  414.             {
  415.                 return '/';
  416.             }else
  417.             {
  418.                 return substr($path, 0, $last_folder_pos);
  419.             }
  420.         }else
  421.         {
  422.             return false;
  423.         }
  424.     }
  425.  
  426.     //faster then get_folders_sorted
  427.     function get_folders($path)
  428.     {
  429.         $slash = stristr(PHP_OS, 'Windows') ? '\\' : '/';
  430.         if (substr($path, -1) != $slash) $path .= $slash;
  431.  
  432.         $folders = array();
  433.         if($dir = opendir($path))
  434.         {
  435.             while($item=readdir($dir))
  436.             {
  437.                 $folder_path = $path.$item;
  438.                 if (is_dir($folder_path) && $item != "." && $item != "..")
  439.                 {
  440.                     $folder['path'] = $folder_path;
  441.                     $folder['name'] = basename($folder_path);
  442.                     $folder['mtime'] = filemtime($folder_path);
  443.                     $folder['size'] = filesize($folder_path);
  444.                     $folder['type'] = filetype($folder_path);
  445.                     $folders[] = $folder;
  446.                 }
  447.             }
  448.             closedir($dir);
  449.         }
  450.         return $folders;
  451.     }
  452.  
  453.     #returns all subfolders of a folder sorted based on the result of a function
  454.     #passed that is performed on the pathname. (For example filesize();)
  455.     function get_folders_sorted($path,$sort_field='basename',$sort_direction='ASC')
  456.     {
  457.         $folders = array();
  458.         $slash = stristr(PHP_OS, 'Windows') ? '\\' : '/';
  459.         if (substr($path, -1) != $slash) $path .= $slash;
  460.         if(strstr($_SERVER['HTTP_HOST'], $path))
  461.         {
  462.             $url = str_replace($_SERVER['DOCUMENT_ROOT'], 'http://'.$_SERVER['HTTP_HOST'], $path);
  463.             if ($slash == '\\')
  464.             {
  465.                 $url = str_replace('\\','/',$url);
  466.             }
  467.         }
  468.         $sort_field = function_exists($sort_field) ? $sort_field : 'basename';
  469.         if (is_dir($path))
  470.         {
  471.             $sorted_list = array();
  472.  
  473.             if(@$dir = opendir($path))
  474.             {
  475.                 while($item=readdir($dir))
  476.                 {
  477.                     $folder_path = $path.$item;
  478.  
  479.                     if (is_dir($folder_path) && $item != "." && $item != "..")
  480.                     {
  481.                         $key_id = 0;
  482.                         $first_key = strtolower($sort_field($folder_path));
  483.                         $key = $first_key;
  484.                         while (array_key_exists($key, $sorted_list))
  485.                         {
  486.                             $key = $first_key.'_'.$key_id;
  487.                             $key_id++;
  488.                         }
  489.                         $sorted_list[$key] = $folder_path;
  490.                     }
  491.                 }
  492.                 closedir($dir);
  493.  
  494.                 if ($sort_direction == 'ASC')
  495.                 {
  496.                     ksort($sorted_list);
  497.                 }else
  498.                 {
  499.                     krsort($sorted_list);
  500.                 }
  501.  
  502.                 while ($item=array_shift($sorted_list))
  503.                 {
  504.                     $folder = array();
  505.                     $folder['path'] = $item;
  506.                     $folder['name'] = basename($item);
  507.                     $folder['mtime'] = filemtime($item);
  508.                     $folder['size'] = filesize($item);
  509.                     $folder['type'] = filetype($item);
  510.                     if(isset($url))
  511.                     {
  512.                         $folder['url'] = $url.$folder['name'];
  513.                     }
  514.                     $folders[] = $folder;
  515.                 }
  516.             }
  517.         }
  518.         return $folders;
  519.     }
  520.  
  521.     //faster then get_files_sorted
  522.     function get_files($path)
  523.     {
  524.         $slash = stristr(PHP_OS, 'Windows') ? '\\' : '/';
  525.         if (substr($path, -1) != $slash) $path .= $slash;
  526.         $files = array();
  527.         if($dir = @opendir($path))
  528.         {
  529.             while($item=readdir($dir))
  530.             {
  531.                 $file_path = $path.$item;
  532.                 if (!is_dir($file_path))
  533.                 {
  534.                     $file['path'] = $file_path;
  535.                     $file['name'] = basename($file_path);
  536.                     $file['size'] = filesize($file_path);
  537.                     $file['mtime'] = filemtime($file_path);
  538.                     $file['type'] = filemimefriendly($file_path);
  539.  
  540.                     $files[] = $file;
  541.                 }
  542.             }
  543.             closedir($dir);
  544.         }
  545.         return $files;
  546.     }
  547.  
  548.     #returns all subfolders of a folder sorted based on the result of a function
  549.     #passed that is performed on the pathname. (For example filesize();)
  550.     function get_files_sorted($path,$sort_field='basename',$sort_direction='ASC')
  551.     {
  552.         $files = array();
  553.  
  554.         $slash = stristr(PHP_OS, 'Windows') ? '\\' : '/';
  555.         if (substr($path, -1) != $slash) $path .= $slash;
  556.  
  557.         if(strstr($_SERVER['HTTP_HOST'], $path))
  558.         {
  559.             $url = str_replace($_SERVER['DOCUMENT_ROOT'], 'http://'.$_SERVER['HTTP_HOST'], $path);
  560.             if ($slash == '\\')
  561.             {
  562.                 $url = str_replace('\\','/',$url);
  563.             }
  564.         }
  565.         $sort_field = function_exists($sort_field) ? $sort_field : 'basename';
  566.         if (is_dir($path))
  567.         {
  568.             $sorted_list = array();
  569.  
  570.             if($dir = @opendir($path))
  571.             {
  572.                 while($item=readdir($dir))
  573.                 {
  574.                     $file = $path.$item;
  575.                     if (!is_dir($file))
  576.                     {
  577.                         $key_id = 0;
  578.                         $first_key = strtolower($sort_field($file));
  579.                         $key = $first_key;
  580.                         while (array_key_exists($key, $sorted_list))
  581.                         {
  582.                             $key = $first_key.'_'.$key_id;
  583.                             $key_id++;
  584.                         }
  585.                         $sorted_list[$key] = $file;
  586.                     }
  587.                 }
  588.                 closedir($dir);
  589.  
  590.                 if ($sort_direction == 'ASC')
  591.                 {
  592.                      ksort($sorted_list);
  593.                 }else
  594.                 {
  595.                     krsort($sorted_list);
  596.                 }
  597.  
  598.                 while ($item=array_shift($sorted_list))
  599.                 {
  600.                     $file = array();
  601.                     $file['path'] = $item;
  602.                     $file['name'] = basename($item);
  603.                     $file['mtime'] = filemtime($item);
  604.                     $file['size'] = filesize($item);
  605.                     $file['type'] = filemimefriendly($item);
  606.  
  607.                     if(isset($url))
  608.                     {
  609.                         $file['url'] = $url.$file['name'];
  610.                     }
  611.                     $files[] = $file;
  612.                 }
  613.             }
  614.  
  615.         }
  616.         return $files;
  617.     }
  618.  
  619.     function search($path, $keyword, $modified_later_then=0, $modified_earlier_then=0)
  620.     {
  621.         global $GO_SECURITY;
  622.  
  623.         if ($modified_earlier_then == 0)
  624.         {
  625.             $modified_earlier_then = time();
  626.         }
  627.  
  628.         if($this->has_read_permission($GO_SECURITY->user_id, $path))
  629.         {
  630.             $folders = $this->get_folders($path);
  631.             while ($folder = array_shift($folders))
  632.             {
  633.                 $this->search($folder['path'], $keyword, $modified_later_then, $modified_earlier_then);
  634.             }
  635.         }
  636.         $folder['path'] = $path;
  637.         $folder['name'] = basename($path);
  638.         $folder['mtime'] = filemtime($path);
  639.         $folder['size'] = filesize($path);
  640.         $folder['type'] = filetype($path);
  641.  
  642.         if (stristr(basename($path), $keyword) && $modified_later_then < $folder['mtime'] && $modified_earlier_then > $folder['mtime'])
  643.         {
  644.             $this->search_results[] = $folder;
  645.         }
  646.  
  647.         $files = $this->get_files($path);
  648.         while ($file = array_shift($files))
  649.         {
  650.             if (stristr($file['name'], $keyword) && $modified_later_then < $file['mtime'] && $modified_earlier_then > $file['mtime'])
  651.             {
  652.                 $this->search_results[] = $file;
  653.             }
  654.         }
  655.  
  656.         return $this->search_results;
  657.     }
  658.  
  659.     function delete_user($user_id)
  660.     {
  661.         $fs = new filesystem();
  662.         $this->get_shares($user_id);
  663.         while($this->next_record())
  664.         {
  665.             $fs->delete_share($this->f('path'));
  666.         }
  667.     }
  668. }