home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / files.class.inc < prev    next >
Text File  |  2004-03-08  |  24KB  |  823 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. class files extends db
  13. {
  14.     var $folders = array();
  15.  
  16.     function files()
  17.     {
  18.         $this->db();
  19.     }
  20.  
  21.     function get_all_sub_folders($folder_id)
  22.     {
  23.  
  24.         $sql = "SELECT * FROM fbFolders WHERE parent_id='$folder_id' ORDER BY name DESC";
  25.         $this->query($sql);
  26.         return $this->num_rows();
  27.     }
  28.  
  29.     function get_authorized_sub_folders($user_id, $folder_id, $sort = "name", $direction = "ASC")
  30.     {
  31.                 //get the users groups
  32.                 $sql = "SELECT group_id FROM users_groups WHERE user_id='$user_id'";
  33.  
  34.                 $this->query($sql);
  35.  
  36.  
  37.                 if ($this->next_record())
  38.                 {
  39.                         $groups = $this->Record["group_id"];
  40.                 }
  41.  
  42.                 //After the first group has been added put a ',' after each group.
  43.                 while ($this->next_record())
  44.                 {
  45.                         $groups .= ",".$this->Record["group_id"];
  46.                 }
  47.  
  48.                 //if user is not in groups don't check if the acl is permitted to one of his groups.
  49.                 if ($groups == "")
  50.                 {
  51.                     $sql  = "SELECT DISTINCT fbFolders.* FROM fbFolders LEFT JOIN acl ON (fbFolders.acl_read=acl.acl_id OR fbFolders.acl_write=acl.acl_id) ";
  52.                     $sql .= "WHERE fbFolders.parent_id='$folder_id' AND ";
  53.                     $sql .= "((acl.user_id='$user_id') OR fbFolders.user_id='$user_id') ";
  54.                     $sql .= "ORDER BY ".$sort." ".$direction;
  55.  
  56.                 }else
  57.                 {
  58.                     $sql  = "SELECT DISTINCT fbFolders.* FROM fbFolders LEFT JOIN acl ON (fbFolders.acl_read=acl.acl_id OR fbFolders.acl_write=acl.acl_id) ";
  59.                     $sql .= "WHERE fbFolders.parent_id='$folder_id' AND ";
  60.                     $sql .= "((acl.user_id='$user_id' OR (acl.group_id IN($groups))) OR fbFolders.user_id='$user_id') ";
  61.                     $sql .= "ORDER BY ".$sort." ".$direction;
  62.                 }
  63.                 $this->query($sql);
  64.                 return $this->num_rows();
  65.     }
  66.  
  67.     function get_parent_id($folder_id)
  68.     {
  69.  
  70.         $sql = "SELECT parent_id FROM fbFolders WHERE id='$folder_id'";
  71.         $this->query($sql);
  72.         if ($this->num_rows() > 0)
  73.         {
  74.             $this->next_record();
  75.             return $this->f("parent_id");
  76.         }else
  77.         {
  78.             return -1;
  79.         }
  80.     }
  81.  
  82.     function get_path($folder_id)
  83.     {
  84.         $path = '';
  85.         if ($folder_id == 0)
  86.         {
  87.             $path = '/';
  88.         }
  89.         while ($folder_id != 0)
  90.         {
  91.             $folder = $this->get_folder_properties($folder_id, false);
  92.             $path = '/'.$folder["name"].$path;
  93.             $folder_id = $folder["parent_id"];
  94.         }
  95.         return $path;
  96.     }
  97.  
  98.     function get_folder_properties($folder_id, $include_username=false)
  99.     {
  100.         if ($include_username)
  101.         {
  102.             $sql = "SELECT fbFolders.*,users.username FROM fbFolders,users WHERE fbFolders.id='$folder_id' AND fbFolders.user_id=users.id";
  103.         }else
  104.         {
  105.             $sql = "SELECT * FROM fbFolders WHERE id='$folder_id'";
  106.         }
  107.         $this->query($sql);
  108.         if ($this->num_rows() > 0)
  109.         {
  110.             $this->next_record();
  111.             return $this->Record;
  112.         }else
  113.         {
  114.             return false;
  115.         }
  116.     }
  117.  
  118.     function add_folder($parent_id, $user_id, $name, $acl_read, $acl_write)
  119.     {
  120.         $id = $this->nextid("fbFolders");
  121.         if ($id > 0)
  122.         {
  123.             $sql = "INSERT INTO fbFolders (id, parent_id, user_id, name, acl_read, acl_write, time)";
  124.             $sql .= " VALUES ('$id','$parent_id', '$user_id', '$name', '$acl_read', '$acl_write', '".time()."')";
  125.             $this->query($sql);
  126.             return $id;
  127.         }else
  128.         {
  129.             return -1;
  130.         }
  131.     }
  132.  
  133.     function folder_exists($parent_id, $name)
  134.     {
  135.  
  136.         $sql = "SELECT id FROM fbFolders WHERE name='$name' AND parent_id='$parent_id'";
  137.         $this->query($sql);
  138.         if ($this->next_record())
  139.         {
  140.             return $this->f("id");
  141.         }else
  142.         {
  143.             return false;
  144.         }
  145.     }
  146.  
  147.     function has_write_permission($user_id, $id, $type)
  148.     {
  149.         if ($type == "folder")
  150.         {
  151.             $item = $this->get_folder_properties($id, false);
  152.         }else
  153.         {
  154.             $item = $this->get_file_properties($id, false);
  155.         }
  156.  
  157.         global $GO_SECURITY;
  158.         return $GO_SECURITY->has_permission($user_id, $item["acl_write"]);
  159.  
  160.     }
  161.  
  162.     function has_read_permission($user_id, $id, $type)
  163.     {
  164.         if ($type == "folder")
  165.         {
  166.             $item = $this->get_folder_properties($id, false);
  167.         }else
  168.         {
  169.             $item = $this->get_file_properties($id, false);
  170.         }
  171.  
  172.                 global $GO_SECURITY;
  173.                 if ($GO_SECURITY->has_permission($user_id, $item["acl_read"]) == true)
  174.                 {
  175.                         return true;
  176.                 }else
  177.                 {
  178.                         return $GO_SECURITY->has_permission($user_id, $item["acl_write"]);
  179.                 }
  180.     }
  181.  
  182.  
  183.     function delete_folder($user_id, $folder_id)
  184.     {
  185.         $children = $this->get_all_sub_folders($folder_id);
  186.         while($this->next_record())
  187.         {
  188.             $folder_query = new files;
  189.             if ($folder_query->delete_folder($user_id, $this->Record["id"]) == -1)
  190.             {
  191.                 return -1;
  192.             }
  193.         }
  194.         if ($this->has_write_permission($user_id, $folder_id, "folder"))
  195.         {
  196.                         $this->get_all_files($folder_id);
  197.             while ($this->next_record())
  198.             {
  199.                 $files_query = new files;
  200.                 if ($files_query->has_write_permission($user_id, $this->f("id"), "file"))
  201.                 {
  202.                     $files_query->delete_file($this->f("id"));
  203.                 }else
  204.                 {
  205.                         return -1;
  206.                 }
  207.             }
  208.             unset($files_query);
  209.  
  210.             $sql = "SELECT acl_write, acl_read FROM fbFolders WHERE id='$folder_id'";
  211.             $this->query($sql);
  212.             $this->next_record();
  213.  
  214.             global $GO_SECURITY;
  215.             $GO_SECURITY->delete_acl($this->f("acl_read"));
  216.             $GO_SECURITY->delete_acl($this->f("acl_write"));
  217.  
  218.             $sql = "DELETE FROM fbFolders WHERE id='$folder_id'";
  219.             $this->query($sql);
  220.         }else
  221.         {
  222.             return -1;
  223.         }
  224.     }
  225.  
  226.     function recurse_permissions($user_id, $folder_id, $source_acl, $acl_name)
  227.     {
  228.         $GO_SECURITY = new GO_SECURITY;
  229.         $folder = $this->get_folder_properties($folder_id, false);
  230.         if ($folder["user_id"] == $user_id)
  231.         {
  232.  
  233.  
  234.                 if ($source_acl != $folder[$acl_name])
  235.                         $GO_SECURITY->copy_acl($source_acl, $folder[$acl_name]);
  236.         }
  237.  
  238.         $this->get_all_files($folder_id);
  239.         while ($this->next_record())
  240.         {
  241.                 if ($this->f("user_id") == $user_id)
  242.                 {
  243.                         $GO_SECURITY->copy_acl($source_acl, $this->f($acl_name));
  244.                 }
  245.         }
  246.  
  247.         $children = $this->get_all_sub_folders($folder_id);
  248.         while($this->next_record())
  249.         {
  250.                 $folder_query = new files;
  251.                 $folder_query->recurse_permissions($user_id, $this->Record["id"], $source_acl, $acl_name);
  252.         }
  253.  
  254.     }
  255.  
  256.     function save_folder_name($folder_id,$name)
  257.     {
  258.         $sql = "UPDATE fbFolders SET name='$name' WHERE id='$folder_id'";
  259.         $this->query($sql);
  260.     }
  261.  
  262.     function add_file($file, $filename, $filesize, $filetype, $folder_id, $user_id, $acl_read, $acl_write, $versions)
  263.     {
  264.         $filetype = trim($filetype);
  265.         $extension = get_extension($filename);
  266.  
  267.         global $GO_CONFIG;
  268.  
  269.         require($GO_CONFIG->class_path."filetypes.class.inc");
  270.         $filetypes = new filetypes;
  271.  
  272.         $filetype_id= $filetypes->get_filetype_id($extension, $filetype);
  273.         if ($filetype_id == -1)
  274.         {
  275.             return -1;
  276.         }
  277.         $file_id = $this->nextid("fbFiles");
  278.         $version_id = $this->nextid("fbVersions");
  279.  
  280.         if ($file_id > 0 && $version_id > 0)
  281.         {
  282.             $sql  = "INSERT INTO fbFiles (id, folder_id, name,filetype_id,user_id,acl_read,acl_write,versions, latest_version_id) ";
  283.             $sql .= "VALUES ('$file_id','$folder_id', '$filename','$filetype_id','$user_id','$acl_read','$acl_write','$versions','$version_id')";
  284.             if ($this->query($sql))
  285.             {
  286.                 $sql  = "INSERT INTO fbVersions (id, file_id, size, time, user_id) ";
  287.                 $sql .= "VALUES ('$version_id','$file_id' , '$filesize', '".time()."', '$user_id')";
  288.                 $this->query($sql);
  289.  
  290.                 global $GO_CONFIG;
  291.                 if (@mkdir($GO_CONFIG->file_storage_path.$user_id."/".$file_id,0700) && @copy($file,$GO_CONFIG->file_storage_path.$user_id."/".$file_id."/".$version_id))
  292.                 {
  293.                     return file_id;
  294.                 }else
  295.                 {
  296.                     $sql = "DELETE FROM fbFiles WHERE id = '$file_id'";
  297.                     $this->query($sql);
  298.                     $sql = "DELETE FROM fbVersions WHERE file_id = '$file_id'";
  299.                     $this->query($sql);
  300.                     return -1;
  301.                 }
  302.             }
  303.  
  304.         }else
  305.         {
  306.             return -1;
  307.         }
  308.     }
  309.  
  310.     function add_version($file, $filesize, $file_id, $user_id)
  311.     {
  312.  
  313.         $sql = "SELECT user_id FROM fbFiles WHERE id='$file_id'";
  314.         $this->query($sql);
  315.         $this->next_record();
  316.         $owner_id = $this->f("user_id");
  317.  
  318.         if ($owner_id != "")
  319.         {
  320.             global $GO_CONFIG;
  321.  
  322.             $version_id = $this->nextid("fbVersions");
  323.             if ($version_id > 0)
  324.             {
  325.                 $sql  = "INSERT INTO fbVersions (id, file_id, size, time, user_id) ";
  326.                 $sql .= "VALUES ('$version_id','$file_id', '$filesize','".time()."', '$user_id')";
  327.                 if ($this->query($sql))
  328.                 {
  329.                         $sql = "UPDATE fbFiles SET latest_version_id='$version_id' WHERE id='$file_id'";
  330.                         if ($this->query($sql))
  331.                         {
  332.                                 if(@copy($file,$GO_CONFIG->file_storage_path.$owner_id."/".$file_id."/".$version_id))
  333.                                 {
  334.                                         return $version_id;
  335.                                 }else
  336.                                 {
  337.                                         $sql = "DELETE FROM fbVersions WHERE id='$version_id'";
  338.                                         $this->query($sql);
  339.                                         return -1;
  340.                                 }
  341.                         }else
  342.                         {
  343.                                 $sql = "DELETE FROM fbVersions WHERE id='$version_id'";
  344.                                 $this->query($sql);
  345.                                 return -1;
  346.                         }
  347.                 }
  348.             }else
  349.             {
  350.                 return -1;
  351.             }
  352.         }else
  353.         {
  354.             return -1;
  355.         }
  356.     }
  357.  
  358.     function file_exists($folder_id, $filename)
  359.     {
  360.  
  361.         $sql = "SELECT id FROM fbFiles WHERE name='$filename' AND folder_id='$folder_id'";
  362.         $this->query($sql);
  363.         if ($this->num_rows() > 0)
  364.         {
  365.             $this->next_record();
  366.             return $this->f("id");
  367.         }else
  368.         {
  369.             return false;
  370.         }
  371.     }
  372.  
  373.  
  374.  
  375.     function get_all_files($folder_id)
  376.     {
  377.  
  378.         $sql  = "SELECT fbFiles.*, filetypes.friendly AS type, fbVersions.size, fbVersions.time ";
  379.         $sql .= "FROM fbFiles LEFT JOIN fbVersions ON (fbFiles.latest_version_id = fbVersions.id) ";
  380.         $sql .= "LEFT JOIN filetypes ON (fbFiles.filetype_id = filetypes.id) WHERE fbFiles.folder_id = '$folder_id'";
  381.         $this->query($sql);
  382.     }
  383.  
  384.     function get_authorized_files($user_id, $folder_id, $sort = "name", $direction = "ASC")
  385.     {
  386.         //get the users groups
  387.         $sql = "SELECT group_id FROM users_groups WHERE user_id='$user_id'";
  388.  
  389.         $this->query($sql);
  390.  
  391.  
  392.         if ($this->next_record())
  393.         {
  394.                 $groups = $this->Record["group_id"];
  395.         }
  396.  
  397.         //After the first group has been added put a ',' after each group.
  398.         while ($this->next_record())
  399.         {
  400.                 $groups .= ",".$this->Record["group_id"];
  401.         }
  402.  
  403.         //if user is not in groups don't check if the acl is permitted to one of his groups.
  404.         if ($groups == "")
  405.         {
  406.             $sql  = "SELECT DISTINCT fbFiles.*, filetypes.friendly AS type, filetypes.type AS content, fbVersions.size, fbVersions.time, fbVersions.id as version_id ";
  407.             $sql .= "FROM fbFiles LEFT JOIN fbVersions ON (fbFiles.latest_version_id = fbVersions.id) ";
  408.             $sql .= "LEFT JOIN filetypes ON (fbFiles.filetype_id = filetypes.id) ";
  409.             $sql .= "LEFT JOIN acl ON (fbFiles.acl_read=acl.acl_id OR fbFiles.acl_write=acl.acl_id) ";
  410.             $sql .= "WHERE fbFiles.folder_id = '$folder_id' AND ((acl.user_id='$user_id') OR fbFiles.user_id='$user_id') ";
  411.             $sql .= "GROUP BY fbFiles.name ORDER BY ".$sort." ".$direction;
  412.                 }else
  413.                 {
  414.             $sql  = "SELECT DISTINCT fbFiles.*, filetypes.friendly AS type, filetypes.type AS content, fbVersions.size, fbVersions.time, fbVersions.id as version_id ";
  415.             $sql .= "FROM fbFiles LEFT JOIN fbVersions ON (fbFiles.latest_version_id = fbVersions.id) ";
  416.             $sql .= "LEFT JOIN filetypes ON (fbFiles.filetype_id = filetypes.id) ";
  417.             $sql .= "LEFT JOIN acl ON (fbFiles.acl_read=acl.acl_id OR fbFiles.acl_write=acl.acl_id) ";
  418.             $sql .= "WHERE fbFiles.folder_id = '$folder_id' AND ((acl.user_id='$user_id') OR (acl.group_id IN($groups)) ";
  419.             $sql .= "OR fbFiles.user_id='$user_id') GROUP BY fbFiles.name ORDER BY ".$sort." ".$direction;
  420.         }
  421.  
  422.         $this->query($sql);
  423.         return $this->num_rows();
  424.     }
  425.  
  426.     function get_file_properties($file_id, $include_username = false)
  427.     {
  428.  
  429.         if ($include_username)
  430.         {
  431.             $sql  = "SELECT fbFiles.*,users.username,filetypes.friendly FROM fbFiles,users,filetypes ";
  432.             $sql .= "WHERE fbFiles.id='$file_id' AND fbFiles.user_id=users.id AND filetypes.id=fbFiles.filetype_id";
  433.         }else
  434.         {
  435.             $sql = "SELECT fbFiles.*,filetypes.friendly FROM fbFiles,filetypes WHERE fbFiles.id='$file_id' AND fbFiles.filetype_id=filetypes.id";
  436.         }
  437.         $this->query($sql);
  438.         if ($this->num_rows() > 0)
  439.         {
  440.             $this->next_record();
  441.             return $this->Record;
  442.         }else
  443.         {
  444.             return false;
  445.         }
  446.     }
  447.  
  448.     function update_file($file_id,$name, $versions)
  449.     {
  450.  
  451.         $sql = "UPDATE fbFiles SET name='$name', versions='$versions' WHERE id='$file_id'";
  452.         $this->query($sql);
  453.     }
  454.  
  455.     function get_file($version_id)
  456.     {
  457.  
  458.         $sql  = "SELECT fbVersions.*, filetypes.type, fbFiles.user_id AS file_user_id, fbFiles.name, fbFiles.latest_version_id FROM fbVersions LEFT JOIN fbFiles ON fbVersions.file_id=fbFiles.id ";
  459.         $sql .= "LEFT JOIN filetypes ON fbFiles.filetype_id=filetypes.id WHERE fbVersions.id='$version_id'";
  460.         $this->query($sql);
  461.         $this->query($sql);
  462.         if ($this->num_rows() > 0)
  463.         {
  464.             $this->next_record();
  465.             return $this->Record;
  466.         }else
  467.         {
  468.             return false;
  469.         }
  470.     }
  471.  
  472.     function delete_file($file_id)
  473.     {
  474.  
  475.         $sql = "SELECT acl_write, acl_read, user_id FROM fbFiles WHERE id='$file_id'";
  476.         $this->query($sql);
  477.         $this->next_record();
  478.  
  479.         $user_id = $this->f("user_id");
  480.  
  481.         global $GO_SECURITY;
  482.  
  483.         $GO_SECURITY->delete_acl($this->f("acl_read"));
  484.         $GO_SECURITY->delete_acl($this->f("acl_write"));
  485.  
  486.         $sql = "DELETE FROM fbVersions WHERE file_id='$file_id'";
  487.         $this->query($sql);
  488.  
  489.         $sql = "DELETE FROM fbFiles WHERE id='$file_id'";
  490.         $this->query($sql);
  491.  
  492.         global $GO_CONFIG;
  493.  
  494.         $location = $GO_CONFIG->file_storage_path.$user_id."/".$file_id."/";
  495.         $file_dir = opendir($location);
  496.         while ($file = readdir($file_dir))
  497.         {
  498.             if (!is_dir($location.$file))
  499.             {
  500.                 unlink($location.$file);
  501.                 unset($file);
  502.             }
  503.         }
  504.         rmdir($location);
  505.     }
  506.  
  507.     function delete_version($version_id)
  508.     {
  509.         $file = $this->get_file($version_id);
  510.  
  511.         $sql = "DELETE FROM fbVersions WHERE id='$version_id'";
  512.         $this->query($sql);
  513.         global $GO_CONFIG;
  514.         unlink($GO_CONFIG->file_storage_path.$file["file_user_id"]."/".$file["file_id"]."/".$version_id);
  515.         if ($file["latest_version_id"] == $version_id)
  516.         {
  517.                 $sql = "SELECT max(id) AS version_id FROM fbVersions WHERE file_id='".$file["file_id"]."'";
  518.                 $this->query($sql);
  519.                 $this->next_record();
  520.                 $sql = "UPDATE fbFiles SET latest_version_id='".$this->f("version_id")."' WHERE id='".$file["file_id"]."'";
  521.                 $this->query($sql);
  522.         }
  523.     }
  524.  
  525.     function delete_latest_version($file_id)
  526.     {
  527.  
  528.         $sql = "SELECT user_id FROM fbFiles WHERE id='$file_id'";
  529.         $this->query($sql);
  530.         $this->next_record();
  531.         $user_id = $this->f("user_id");
  532.  
  533.         $sql  = "SELECT max(id) FROM fbVersions WHERE file_id='$file_id'";
  534.         $this->query($sql);
  535.         $this->next_record();
  536.         $version_id = $this->f(0);
  537.  
  538.         if ($version_id != "" && $user_id != "")
  539.         {
  540.             $sql = "DELETE FROM fbVersions WHERE id='$version_id'";
  541.             $this->query($sql);
  542.  
  543.             global $GO_CONFIG;
  544.             unlink($GO_CONFIG->file_storage_path.$user_id."/".$file_id."/".$version_id);
  545.             $sql = "SELECT max(id) AS version_id FROM fbVersions WHERE file_id='".$file_id."'";
  546.             $this->query($sql);
  547.             $this->next_record();
  548.             $sql = "UPDATE fbFiles SET latest_version_id='".$this->f("version_id")."' WHERE id='".$file_id."'";
  549.             $this->query($sql);
  550.         }else
  551.         {
  552.             $this->halt("Could not get latest version");
  553.         }
  554.     }
  555.  
  556.     function delete_oldest_version($file_id)
  557.     {
  558.  
  559.         $sql = "SELECT user_id FROM fbFiles WHERE id='$file_id'";
  560.         $this->query($sql);
  561.         $this->next_record();
  562.         $user_id = $this->f("user_id");
  563.  
  564.         $sql  = "SELECT min(id) FROM fbVersions WHERE file_id='$file_id'";
  565.         $this->query($sql);
  566.         $this->next_record();
  567.         $version_id = $this->f(0);
  568.  
  569.         if ($version_id != "" && $user_id != "")
  570.         {
  571.             $sql = "DELETE FROM fbVersions WHERE id='$version_id'";
  572.             $this->query($sql);
  573.  
  574.             global $GO_CONFIG;
  575.             unlink($GO_CONFIG->file_storage_path.$user_id."/".$file_id."/".$version_id);
  576.         }else
  577.         {
  578.             $this->halt("Could not get oldest version");
  579.         }
  580.     }
  581.  
  582.     function max_versions_reached($file_id)
  583.     {
  584.  
  585.         $sql  = "SELECT fbVersions.id, fbFiles.versions FROM fbVersions LEFT JOIN fbFiles ON fbFiles.id=fbVersions.file_id ";
  586.         $sql .= "WHERE file_id='$file_id'";
  587.         $this->query($sql);
  588.         $this->next_record();
  589.         if ($this->f("versions") < $this->num_rows())
  590.         {
  591.             return true;
  592.         }else
  593.         {
  594.             return false;
  595.         }
  596.     }
  597.  
  598.     function get_versions($file_id)
  599.     {
  600.  
  601.         $sql  = "SELECT fbVersions.*, users.username FROM fbVersions, users WHERE file_id='$file_id' ";
  602.         $sql .=" AND fbVersions.user_id=users.id ORDER BY time DESC";
  603.         $this->query($sql);
  604.         return $this->num_rows();
  605.     }
  606.  
  607.     function paste($id, $new_folder_id, $type)
  608.     {
  609.         if ($type == "folder")
  610.         {
  611.             $sql = "UPDATE fbFolders SET parent_id='$new_folder_id' WHERE id='$id'";
  612.         }else
  613.         {
  614.             $sql = "UPDATE fbFiles SET folder_id='$new_folder_id' WHERE id='$id'";
  615.         }
  616.  
  617.         $this->query($sql);
  618.     }
  619.  
  620.  
  621.     function get_used_diskspace($user_id)
  622.     {
  623.             $sql = "SELECT SUM(size) AS size FROM fbVersions WHERE user_id='$user_id'";
  624.             $this->query($sql);
  625.             $this->next_record();
  626.             return $this->f("size");
  627.     }
  628.  
  629.     function search_files($user_id, $filename, $sort="name", $direction="ASC")
  630.     {
  631.         //get the users groups
  632.         $sql = "SELECT group_id FROM users_groups WHERE user_id='$user_id'";
  633.  
  634.         $this->query($sql);
  635.  
  636.  
  637.         if ($this->next_record())
  638.         {
  639.                 $groups = $this->Record["group_id"];
  640.         }
  641.  
  642.         //After the first group has been added put a ',' after each group.
  643.         while ($this->next_record())
  644.         {
  645.                 $groups .= ",".$this->Record["group_id"];
  646.         }
  647.  
  648.         //if user is not in groups don't check if the acl is permitted to one of his groups.
  649.         if ($groups == "")
  650.         {
  651.             $sql  = "SELECT DISTINCT fbFiles.*, filetypes.friendly AS type, filetypes.type AS content, fbVersions.size, fbVersions.time, versions.id as version_id ";
  652.             $sql .= "FROM fbFiles LEFT JOIN fbVersions ON (fbFiles.latest_version_id = fbVersions.id) ";
  653.             $sql .= "LEFT JOIN filetypes ON (fbFiles.filetype_id = filetypes.id) ";
  654.             $sql .= "LEFT JOIN acl ON (fbFiles.acl_read=acl.acl_id OR fbFiles.acl_write=acl.acl_id) ";
  655.             $sql .= "WHERE fbFiles.name LIKE '%".$filename."%' AND ((acl.user_id='$user_id') OR fbFiles.user_id='$user_id') ";
  656.             $sql .= "GROUP BY fbFiles.name ORDER BY fbFiles.name ASC";
  657.                 }else
  658.                 {
  659.             $sql  = "SELECT DISTINCT fbFiles.*, filetypes.friendly AS type, filetypes.type AS content, fbVersions.size, fbVersions.time, fbVersions.id as version_id ";
  660.             $sql .= "FROM fbFiles LEFT JOIN fbVersions ON (fbFiles.latest_version_id = fbVersions.id) ";
  661.             $sql .= "LEFT JOIN filetypes ON (fbFiles.filetype_id = filetypes.id) ";
  662.             $sql .= "LEFT JOIN acl ON (fbFiles.acl_read=acl.acl_id OR fbFiles.acl_write=acl.acl_id) ";
  663.             $sql .= "WHERE fbFiles.name LIKE '%".$filename."%' AND ((acl.user_id='$user_id') OR (acl.group_id IN($groups)) ";
  664.             $sql .= "OR fbFiles.user_id='$user_id') GROUP BY fbFiles.id ORDER BY fbFiles.name ASC";
  665.         }
  666.  
  667.         $this->query($sql);
  668.         return $this->num_rows();
  669.     }
  670.  
  671.     function search_folders($user_id, $foldername)
  672.     {
  673.         //get the users groups
  674.  
  675.         $sql = "SELECT group_id FROM users_groups WHERE user_id='$user_id'";
  676.  
  677.         $this->query($sql);
  678.  
  679.  
  680.         if ($this->next_record())
  681.         {
  682.                 $groups = $this->Record["group_id"];
  683.         }
  684.  
  685.         //After the first group has been added put a ',' after each group.
  686.         while ($this->next_record())
  687.         {
  688.                 $groups .= ",".$this->Record["group_id"];
  689.         }
  690.  
  691.         //if user is not in groups don't check if the acl is permitted to one of his groups.
  692.         if ($groups == "")
  693.         {
  694.             $sql  = "SELECT DISTINCT fbFolders.* FROM fbFolders LEFT JOIN acl ON (fbFolders.acl_read=acl.acl_id OR fbFolders.acl_write=acl.acl_id) ";
  695.             $sql .= "WHERE fbFolders.name LIKE '%".$foldername."%' AND ";
  696.             $sql .= "((acl.user_id='$user_id') OR fbFolders.user_id='$user_id') ";
  697.             $sql .= "ORDER BY fbFolders.name ASC";
  698.  
  699.         }else
  700.         {
  701.             $sql  = "SELECT DISTINCT fbFolders.* FROM fbFolders LEFT JOIN acl ON (fbFolders.acl_read=acl.acl_id OR fbFolders.acl_write=acl.acl_id) ";
  702.                         $sql .= "WHERE fbFolders.name LIKE '%".$foldername."%' AND ";
  703.             $sql .= "((acl.user_id='$user_id' OR (acl.group_id IN($groups))) OR fbFolders.user_id='$user_id') ";
  704.             $sql .= "ORDER BY fbFolders.name ASC";
  705.         }
  706.  
  707.         //$sql = "SELECT * from fbFolders WHere name like '%".$foldername."%' ORDER BY ".$sort." ".$direction;
  708.         $this->query($sql);
  709.         return $this->num_rows();
  710.     }
  711.  
  712.     function save_description($id, $type, $description)
  713.     {
  714.         if ($type == "file")
  715.                 $sql = "UPDATE fbFiles SET description='$description' WHERE id='$id'";
  716.         else
  717.                 $sql = "UPDATE fbFolders SET description='$description' WHERE id='$id'";
  718.  
  719.         $this->query($sql);
  720.     }
  721.  
  722.     function delete_user($user_id)
  723.     {
  724.         global $GO_SECURITY;
  725.         global $GO_CONFIG;
  726.  
  727.         //change ownership of users files to the current user
  728.  
  729.         $sql = "UPDATE fbFiles SET user_id='".$GO_SECURITY->user_id."' WHERE user_id='$user_id'";
  730.         $this->query($sql);
  731.         $sql = "UPDATE fbVersions SET user_id='".$GO_SECURITY->user_id."' WHERE user_id='$user_id'";
  732.         $this->query($sql);
  733.  
  734.         //move the root folder to the current users folder
  735.         $sql="SELECT id FROM fbFolders WHERE parent_id='0' AND user_id='".$user_id."'";
  736.         $this->query($sql);
  737.         $this->next_record();
  738.         $old_id = $this->f("id");
  739.         $old_filename = $this->f("name");
  740.         $new_filename = $this->f("name");
  741.  
  742.  
  743.         $sql = "UPDATE fbFolders SET user_id='".$GO_SECURITY->user_id."' WHERE user_id='$user_id'";
  744.         $this->query($sql);
  745.  
  746.  
  747.         $sql="SELECT id FROM fbFolders WHERE parent_id='0' AND user_id='".$GO_SECURITY->user_id."'";
  748.         $this->query($sql);
  749.         $this->next_record();
  750.         $new_id = $this->f("id");
  751.  
  752.         if ($new_id > 0 && $old_id > 0)
  753.         {
  754.             $x=0;
  755.             while ($this->folder_exists($new_id, $new_filename))
  756.             {
  757.                 $x++;
  758.                 $new_filename = $old_filename."(".$x.")";
  759.             }
  760.             if ($x>0)
  761.                 $this->save_folder_name($old_id,$new_filename);
  762.  
  763.             $this->paste($old_id, $new_id, "folder");
  764.         }
  765.  
  766.         $old_path = $GO_CONFIG->file_storage_path.$user_id.'/';
  767.         $new_path = $GO_CONFIG->file_storage_path.$GO_SECURITY->user_id.'/';
  768.         $file_dir = opendir($old_path);
  769.         while ($file = readdir($file_dir))
  770.         {
  771.             if (is_dir($old_path.$file) && $file != "." && $file != "..")
  772.             {
  773.                 $this->move_dir($old_path.$file.'/', $new_path.$file.'/');
  774.             }
  775.         }
  776.         rmdir($old_path);
  777.     }
  778.  
  779.     function move_dir ($from_path, $to_path)
  780.     {
  781.         mkdir($to_path, 0700);
  782.         $this_path = getcwd();
  783.         if (is_dir($from_path))
  784.         {
  785.             chdir($from_path);
  786.             $handle=opendir('.');
  787.             while (($file = readdir($handle))!==false)
  788.             {
  789.                 if (($file != ".") && ($file != ".."))
  790.                 {
  791.                     if (is_dir($file))
  792.                     {
  793.                         $this->move_dir($from_path.$file."/",$to_path.$file."/");
  794.                         chdir($from_path);
  795.                     }
  796.                     if (is_file($file)){
  797.                         copy($from_path.$file, $to_path.$file);
  798.                         unlink($from_path.$file);
  799.                     }
  800.                 }
  801.             }
  802.             closedir($handle);
  803.             rmdir($from_path);
  804.         }
  805.     }
  806.  
  807.     function get_family($folder_id)
  808.     {
  809.         $this->folders[] = $folder_id;
  810.         $sql = "SELECT id FROM fbFolders WHERE parent_id='$folder_id'";
  811.         $files = new files;
  812.         $files->query($sql);
  813.         while ($files->next_record())
  814.         {
  815.             $this->get_family($files->f("id"));
  816.         }
  817.         return $this->folders;
  818.     }
  819.  
  820. }
  821.  
  822. ?>
  823.