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

  1. <?php
  2. /*
  3. DAV-Addings
  4. Copyright .tgm 2003
  5. Author: Michael Borko <michael.borko@tgm.ac.at>
  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 dav extends db
  14. {
  15.     var $search_results = array();
  16.     
  17.     function dav()
  18.     {
  19.         $this->db();
  20.     }
  21.  
  22.     function file_get_contents ($filename)
  23.     {
  24.         $opendf = fopen("$filename", "rb");
  25.         $content = fread($opendf, filesize($filename));
  26.         fclose($opendf);
  27.         return $content;
  28.     }
  29.  
  30.     function file_write_string ($filename, $input)
  31.     {
  32.         $opendf = fopen("$filename", "wb");
  33.         if( is_writeable($filename) ) {
  34.             fwrite($opendf,$input);
  35.             fclose($opendf);
  36.         }
  37.     }
  38.     
  39.     function make_dirs($strPath, $mode)
  40.     {
  41.         if (is_dir($strPath)) return true;
  42.         $pStrPath = dirname($strPath);
  43.         if (!$this->make_dirs($pStrPath, $mode)) return false;
  44.         return mkdir($strPath);
  45.     }
  46.     
  47.     function delete_dirs($dir)
  48.     {
  49.         $current_dir = opendir($dir);
  50.         while($entryname = readdir($current_dir)) {
  51.             if(is_dir("$dir/$entryname") and ($entryname != "." and $entryname!="..")) {
  52.                 $this->delete_dirs("${dir}/${entryname}");
  53.             }
  54.             elseif($entryname != "." and $entryname!="..") {
  55.                 unlink("${dir}/${entryname}");
  56.             }
  57.         }
  58.         closedir($current_dir);
  59.         rmdir($dir);
  60.     }
  61.     
  62.     function multi_strpos($pattern, $sequence)
  63.     {
  64.         $n = -1;
  65.         while (ereg($pattern, $sequence)) {
  66.             $n++;
  67.             $fragment = split($pattern, $sequence);
  68.             $trimsize = (strlen($fragment[0]))+1;
  69.             $sequence = "*".substr($sequence, $trimsize);
  70.             @$position[$n] = (strlen($fragment[0]) + $position[($n-1)]);
  71.         }
  72.         return $position;
  73.     }
  74.     
  75.     function add_access($user, $path, $mode)
  76.     {
  77.         //If the Linkers Directory isn't created, end the action...
  78.         if( !@is_link("/var/dav/$user") )
  79.             $this->first_login($user);
  80.         
  81.         $string = $this->file_get_contents("$path/.htaccess");
  82.  
  83.         //If the User isn't present in the whole .htaccess file, add him to the choosen directive and to the linkers...
  84.         if( !strstr($string, "$user ") ) {
  85.             $string = str_replace ("##$mode-Access\nRequire user", "##$mode-Access\nRequire user $user", $string);
  86.             $string = str_replace ("##Linkers:", "##Linkers: $user", $string);
  87.             $this->file_write_string ("$path/.htaccess", "$string");
  88.             return true;
  89.         }
  90.  
  91.         //The User is present in the .htaccess! Checking the positions...
  92.         $position = $this->multi_strpos($user, $string); $p = 0;
  93.         
  94.         while(@$position[$p] < strlen($string) && @$position[$p] != "") {
  95.             $pos = $position[$p];
  96.             
  97.             //If the User is listed already in the choosen directive...
  98.             if( strpos($string, "##$mode-Access") < $pos && $pos < strpos($string, "##END $mode-Access") ) {
  99.                 return true;
  100.             }
  101.             
  102.             //If the User is not listed in the R/W-directive, so it isn't listed in the choosen directive, if it is present in .htaccess...
  103.             $usersRW = strchr($string, "R/W-Access"); $usersRW = substr($usersRW,0,strpos($usersRW,"#")-1);
  104.             if( strpos($usersRW, $user) ) { return true;
  105.             } else {
  106.                 $string = str_replace ("##$mode-Access\nRequire user", "##$mode-Access\nRequire user $user", $string);
  107.                 $string = str_replace ("##R/W-Access\nRequire user", "##R/W-Access\nRequire user $user", $string);
  108.                 $this->file_write_string ("$path/.htaccess", "$string");
  109.                 return true;
  110.             }
  111.             $p++;
  112.         }
  113.         $this->file_write_string ("$path/.htaccess", "$string");
  114.         return false;
  115.     }
  116.     
  117.     function remove_access($user, $path, $mode)
  118.     {
  119.         $string = $this->file_get_contents("$path/.htaccess");
  120.  
  121.         $position = $this->multi_strpos("$user ", $string); $p = 0;
  122.         $removed = false; $clear = true;
  123.  
  124.         while(@$position[$p] < strlen($string) && @$position[$p] != "") {
  125.             $pos = $position[$p];
  126.             if( strpos($string, "##$mode-Access") < $pos && $pos < strpos($string, "##END $mode-Access") ) {
  127.                 //...delete the user from the selected directive...
  128.                 $string = substr_replace($string, "", $pos, strlen($user)+1);
  129.                 //CAUTION: Stringposition changed!!!
  130.                 $position = $this->multi_strpos("$user ", $string); $p = -1;
  131.                 $removed = true;
  132.             }
  133.             if( $removed && strpos($string, "##R/W-Access") < $pos && $pos < strpos($string, "##END R/W-Access") ) {
  134.                 //...delete the user from the R/W-directive...
  135.                 $string = substr_replace($string, "", $pos, strlen($user)+1);
  136.                 //CAUTION: Stringposition changed!!!
  137.                 $position = $this->multi_strpos("$user ", $string); $p = -1;
  138.                 $clear = false;
  139.             }
  140.             if( $removed && $clear && strpos($string, "##Linkers:") < $pos ) {
  141.                 $string = substr_replace($string, "", $pos, strlen($user)+1);
  142.                 $this->file_write_string ("$path/.htaccess", "$string");
  143.                 return true;
  144.             }
  145.             $p++;
  146.         }
  147.         $this->file_write_string ("$path/.htaccess", "$string");
  148.     }
  149.  
  150.     function first_login($username)
  151.     {
  152.         $dest_apache = "/etc/apache";
  153.         $dest_dav = "/var/dav/$username";
  154.         global $GO_CONFIG;
  155.         $path = $GO_CONFIG->file_storage_path.$username;
  156.         $path_home = $GO_CONFIG->go_storage_path.$username;
  157.         if(!@is_dir($path_home))
  158.             @mkdir($path_home, $GO_CONFIG->create_mode );
  159.         //If DAV is enabled, create the user...
  160.         if(@is_dir("$dest_apache/sites-enabled")) {
  161.             if(!@is_file("$dest_apache/sites-enabled/$username")) {
  162.                 $string = $this->file_get_contents("$dest_apache/sites-available/UserDefault");
  163.                 $string = str_replace("USERNAME", $username, $string);
  164.                 $this->file_write_string("$dest_apache/sites-enabled/$username","$string");
  165.             }
  166.             //First User on the system -> the Directory /var/dav has to be create
  167.             if(!@is_dir(dirname($dest_dav))) {
  168.                 $this->make_dirs(dirname($dest_dav),0755);
  169.                 symlink($path,$dest_dav);
  170.             } elseif(!@is_link($dest_dav)) {
  171.                 symlink($path,$dest_dav);
  172.             }
  173.         }
  174.     }
  175.  
  176.     function check_login($username)
  177.     {
  178.         $dest_apache = "/etc/apache";
  179.         $dest_dav = "/var/dav/$username";
  180.         global $GO_CONFIG;
  181.         $path = $GO_CONFIG->file_storage_path.$username;
  182.         $path_home = $GO_CONFIG->go_storage_path.$username;
  183.         if(!@is_dir($path_home))
  184.             @mkdir($path_home, $GO_CONFIG->create_mode );
  185.         if(!@is_file("$dest_apache/sites-enabled/$username")) {
  186.             $string = $this->file_get_contents("$dest_apache/sites-available/UserDefault");
  187.             $string = str_replace("USERNAME", $username, $string);
  188.             $this->file_write_string("$dest_apache/sites-enabled/$username","$string");
  189.         }
  190.         if(!@is_link($dest_dav)) {
  191.             symlink($path,$dest_dav);
  192.         }
  193.     }
  194.     
  195.     function add_share($user_id, $path)
  196.     {
  197.         global $GO_CONFIG;
  198.         require_once($GO_CONFIG->class_path.'users.class.inc');
  199.         $users = new users();
  200.         $temp = $users->get_user($user_id); $owner = $temp[1];
  201.         //Load the .htaccess file into the new ShareDirectory...
  202.         $string = $this->file_get_contents("/etc/apache/sites-available/ShareAccess");
  203.         $string = str_replace("USER", $owner, $string);
  204.         $this->file_write_string("$path/.htaccess","$string");
  205.     }
  206.  
  207.     function delete_share($path)
  208.     {
  209.         global $GO_CONFIG;
  210.         //deleting also the linked directories in the LINKER-SHARE-DIRECTORY
  211.         $shareControl = "$path/.htaccess";
  212.             
  213.         $linkers = $this->file_get_contents($shareControl); $linkers = strchr($linkers, "##Linkers:");
  214.         $list = explode (" ", $linkers); $i=1;
  215.             
  216.         while(@$list[$i] != "" && @$list[$i] != "#") {
  217.             $user = $list[$i];
  218.             $dest_dav = str_replace($GO_CONFIG->file_storage_path, "/var/dav/$user/freigaben/", $path);
  219.             if(@is_link($dest_dav)) {
  220.                         unlink($dest_dav);
  221.                         @rmdir(dirname($dest_dav));
  222.                 }
  223.             $i++;
  224.         }
  225.         //deleting the share-access and all granted permissions...
  226.         if (is_file($shareControl)) { unlink($shareControl); }
  227.     }
  228.  
  229.     function update_share($old_path, $new_path)
  230.     {
  231.         global $GO_CONFIG;
  232.  
  233.         //searching for the linkers...
  234.         $shareControl = "$new_path/.htaccess";
  235.         $linkers = $this->file_get_contents($shareControl);
  236.         $linkers = strchr($linkers, "##Linkers:");
  237.         $list = explode (" ", $linkers); $i=1;
  238.             
  239.         while(@$list[$i] != "" && !ereg("#",$list[$i]) ) {
  240.             $user = $list[$i];
  241.             $old_dest_dav = str_replace($GO_CONFIG->file_storage_path, "/var/dav/$user/freigaben/", $old_path);
  242.             $new_dest_dav = str_replace($GO_CONFIG->file_storage_path, "/var/dav/$user/freigaben/", $new_path);
  243.             //deleting the old link...
  244.             if(is_link($old_dest_dav)) {
  245.                 unlink($old_dest_dav); 
  246.                 symlink($new_path, $new_dest_dav);
  247.             //creating the new link...
  248.             } elseif(!is_link($new_dest_dav)) { symlink($new_path,$new_dest_dav); }
  249.  
  250.             $i++;
  251.         }
  252.     }
  253.  
  254.     function init_linkers($linker_id, $acl_id)
  255.     {
  256.         $db = new db();
  257.         $sql_query = "SELECT * FROM fsShares WHERE fsShares.acl_read='$acl_id' OR fsShares.acl_write='$acl_id'";
  258.         $db->query($sql_query);
  259.         $db->next_record();
  260.         $result = $db->Record;
  261.         
  262.         global $GO_CONFIG;
  263.         require_once($GO_CONFIG->class_path.'users.class.inc');
  264.         $users = new users();
  265.         $temp = $users->get_user($result['user_id']);
  266.         $owner = $temp[1];
  267.         $temp = $users->get_user($linker_id);
  268.         $linker = $temp[1];
  269.         $array[0] = $linker;
  270.  
  271.         $path = $result['path'];
  272.         $array[1] = $path;
  273.         $share = strchr($path,"$owner/");
  274.         //Linkers-Share-Folder
  275.         $array[2] = "/var/dav/$linker/freigaben/$share"; //dest_dav
  276.  
  277.         //Check the acl_id permission with the current acl_id. If it is the acl_read, it should be incremented
  278.         // for the checking of the permitted writers and vice versa...
  279.         $array[3] = false; $array[4] = false; $array[5] = false;
  280.         if ($result['acl_read'] == $acl_id) { $array[3] = true; $acl_id_new = $acl_id + 1 ; }
  281.         if ($result['acl_write'] == $acl_id) { $array[4] = true; $acl_id_new = $acl_id - 1; }
  282.         $sql_permission = "SELECT acl.user_id AS id FROM acl WHERE acl.acl_id = '$acl_id_new'";
  283.         $db->query($sql_permission);
  284.         $db->next_record();
  285.         $user_array = $db->Record;
  286.         if (@in_array($linker, $user_array)) $array[5] = true;
  287.  
  288.         return $array;
  289.     }
  290.  
  291.     function add_user($linker_id, $acl_id)
  292.     {
  293.         $result = $this->init_linkers($linker_id, $acl_id);
  294.         $linker = $result[0]; $path = $result[1]; $dest_dav = $result[2];
  295.         $r_access = $result[3]; $w_access = $result[4]; $rw_access = $result[5];
  296.  
  297.         //If the share is activated...
  298.         $return = false;
  299.         if (is_file("$path/.htaccess")) {
  300.         //Add granted permissions...
  301.             if ( $r_access ) $return = $this->add_access($linker, $path, "R");
  302.             if ( $w_access ) $return = $this->add_access($linker, $path, "W");
  303.             if ( $rw_access ) $return = $this->add_access($linker, $path, "R/W");
  304.         }
  305.         //Generate the "Link" to the share into the Linkers-Share-Folder.
  306.         //It's necessary to check if the parent directories exists.
  307.         if( $return ) {
  308.             if(!is_dir(dirname($dest_dav))) {
  309.                 $this->make_dirs(dirname($dest_dav),0755);
  310.                 symlink($path,$dest_dav);
  311.             } elseif(@!is_link($dest_dav) && @!is_dir($dest_dav)) {
  312.                 symlink($path,$dest_dav);
  313.             }
  314.         }
  315.         
  316.     }
  317.  
  318.     function delete_user($linker_id, $acl_id)
  319.     {
  320.         $result = $this->init_linkers($linker_id, $acl_id);
  321.         $notlinker = $result[0]; $path = $result[1]; $dest_dav = $result[2];
  322.         $r_access = $result[3]; $w_access = $result[4]; $rw_access = $result[5];
  323.  
  324.         if ( $r_access ) {
  325.             if( $this->remove_access($notlinker, $path, "R") ) {
  326.                         if(is_link($dest_dav)) {
  327.                                 unlink($dest_dav);
  328.                                 @rmdir(dirname($dest_dav));
  329.                 }
  330.             }
  331.         } elseif ( $w_access ) {
  332.             if( $this->remove_access($notlinker, $path, "W") ) {
  333.                 if(is_link($dest_dav)) {
  334.                     unlink($dest_dav);
  335.                     @rmdir(dirname($dest_dav));
  336.                 }
  337.             }
  338.         }
  339.     }
  340.  
  341.     function grouping($group_id, $acl_id, $switch)
  342.     {
  343.         $db = new db();
  344.         $sql_query = "SELECT * FROM fsShares WHERE fsShares.acl_read='$acl_id' OR fsShares.acl_write='$acl_id'";
  345.         $db->query($sql_query);
  346.         $db->next_record();
  347.         $result = $db->Record;
  348.         $owner_id = $result['user_id'];
  349.         
  350.         $db = new db();
  351.         $sql_query = "SELECT acl.user_id AS id FROM acl
  352.             WHERE acl.acl_id = '$acl_id' AND acl.user_id != 0 AND acl.user_id != '$owner_id'
  353.             UNION SELECT users_groups.user_id AS id FROM acl
  354.             JOIN users_groups USING ( group_id )
  355.             WHERE acl_id = '$acl_id' AND users_groups.user_id != 1 AND users_groups.user_id != '$owner_id'";
  356.         $db->query($sql_query);
  357.         $db->next_record();
  358.         while($db->Record != "") {
  359.             $result = $db->Record;
  360.             if( $switch == "add" ) $this->add_user($result['id'], $acl_id);
  361.             if( $switch == "delete" ) $this->delete_user($result['id'], $acl_id);
  362.             $db->next_record();
  363.         }
  364.     }
  365.  
  366.     function write_permissions($user_id, $path, $acl_share_id)
  367.     {
  368.         $db = new db();
  369.         $users = new users();
  370.     
  371.         //Username of Shareowner
  372.         $temp = $users->get_user($user_id);
  373.         $owner = $temp[1]; $owner_id = $user_id;
  374.  
  375.         $share = strchr($path,"$owner/");
  376.  
  377.         //Array with checked-users...
  378.         $ar_checkedusers = array(); $c=0;
  379.         $sql_checkedusers = "SELECT acl.user_id AS id FROM acl WHERE acl.acl_id = '$acl_share_id' AND acl.user_id != 0 AND acl.user_id != '$owner_id'
  380.                     UNION SELECT users_groups.user_id AS id FROM acl JOIN users_groups USING (group_id)
  381.                         WHERE acl_id = '$acl_share_id' AND users_groups.user_id != 1 AND users_groups.user_id != '$owner_id'";
  382.         $db->query($sql_checkedusers);
  383.         $db->next_record();
  384.         while($db->Record != "") {
  385.             $result = $db->Record;
  386.             $temp = $users->get_user($result['id']);
  387.             $linker = $temp['username'];
  388.             array_unshift ($ar_checkedusers, $linker);
  389.             $db->next_record();
  390.         }
  391.         array_unique($ar_checkedusers);
  392.  
  393.         //Array with all read-users...
  394.         $ar_readusers = array(); $acl_read = $acl_share_id - 1;
  395.         $sql_readusers = "SELECT acl.user_id AS id FROM acl WHERE acl.acl_id = '$acl_read'";
  396.         $db->query($sql_readusers);
  397.         $db->next_record();
  398.         while($db->Record != "") {
  399.             $result = $db->Record;
  400.             $temp = $users->get_user($result['id']);
  401.             $reader = $temp['username'];
  402.             array_unshift ($ar_readusers, $reader);
  403.             $db->next_record();
  404.         }
  405.         array_unique($ar_readusers);
  406.     
  407.         while(@$ar_checkedusers[$c] != "") {
  408.             $linker = $ar_checkedusers[$c];
  409.             //Linkers-Share-Folder
  410.             $dest_dav = "/var/dav/$linker/freigaben/$share";
  411.             //If the share is activated...
  412.             $return = false;
  413.             if (is_file("$path/.htaccess")) {
  414.                 //Check if the user has also write-permissions...
  415.                 if ( in_array($linker,$ar_readusers) ) {
  416.                     $return = $this->add_access($linker, $path, "W");
  417.                     $return = $this->add_access($linker, $path, "R/W");
  418.                 }
  419.                 //else add him only to the read-access...
  420.                 else {
  421.                     $return = $this->add_access($linker, $path, "W");
  422.                 }
  423.             }
  424.             //Generate the "Link" to the share into the Linkers-Share-Folder.
  425.             //It's necessary to check if the parent directories exists.
  426.             if( $return ) {
  427.                 if(!is_dir(dirname($dest_dav))) {
  428.                     $this->make_dirs(dirname($dest_dav),0755);
  429.                     symlink($path,$dest_dav);
  430.                 } elseif(!is_link($dest_dav) && !is_dir($dest_dav)) {
  431.                     symlink($path,$dest_dav);
  432.                 }
  433.             }
  434.             $c++;
  435.         }
  436.  
  437.         //REMOVING THE LINKER-SHARES IF THE CHECKBOX IS UNCHECKED...
  438.         $shareControl = "$path/.htaccess";
  439.         $notlinkers = $this->file_get_contents($shareControl);
  440.         //Checking only the W-Users from .htaccess
  441.         $notlinkers = strchr(strchr($notlinkers, "##W-Access"),"user");
  442.         $notlinkers = substr($notlinkers,0,strpos($notlinkers,$owner)-1);
  443.         $list = explode(" ", $notlinkers); $i=1;
  444.         
  445.         while(@$list[$i] != "" && @$list[$i] != "#") {
  446.             $notlinker = chop($list[$i]);
  447.             $dest_dav = "/var/dav/$notlinker/freigaben/$share";
  448.     
  449.             if( !in_array($notlinker,$ar_checkedusers) && strcmp("#",$notlinker) ) {
  450.             //Read-permission for Not-Linker will be erased...
  451.                 if( $this->remove_access($notlinker, $path, "W") ) {
  452.                     if(is_link($dest_dav)) {
  453.                         unlink($dest_dav);
  454.                         @rmdir(dirname($dest_dav));
  455.                     }
  456.                 }
  457.             }
  458.             $i++;
  459.         }
  460.     }
  461.  
  462. }
  463. ?>
  464.