home *** CD-ROM | disk | FTP | other *** search
- <?php
- /*
- DAV-Addings
- Copyright .tgm 2003
- Author: Michael Borko <michael.borko@tgm.ac.at>
-
- This program is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by the
- Free Software Foundation; either version 2 of the License, or (at your
- option) any later version.
- */
-
- class dav extends db
- {
- var $search_results = array();
-
- function dav()
- {
- $this->db();
- }
-
- function file_get_contents ($filename)
- {
- $opendf = fopen("$filename", "rb");
- $content = fread($opendf, filesize($filename));
- fclose($opendf);
- return $content;
- }
-
- function file_write_string ($filename, $input)
- {
- $opendf = fopen("$filename", "wb");
- if( is_writeable($filename) ) {
- fwrite($opendf,$input);
- fclose($opendf);
- }
- }
-
- function make_dirs($strPath, $mode)
- {
- if (is_dir($strPath)) return true;
- $pStrPath = dirname($strPath);
- if (!$this->make_dirs($pStrPath, $mode)) return false;
- return mkdir($strPath);
- }
-
- function delete_dirs($dir)
- {
- $current_dir = opendir($dir);
- while($entryname = readdir($current_dir)) {
- if(is_dir("$dir/$entryname") and ($entryname != "." and $entryname!="..")) {
- $this->delete_dirs("${dir}/${entryname}");
- }
- elseif($entryname != "." and $entryname!="..") {
- unlink("${dir}/${entryname}");
- }
- }
- closedir($current_dir);
- rmdir($dir);
- }
-
- function multi_strpos($pattern, $sequence)
- {
- $n = -1;
- while (ereg($pattern, $sequence)) {
- $n++;
- $fragment = split($pattern, $sequence);
- $trimsize = (strlen($fragment[0]))+1;
- $sequence = "*".substr($sequence, $trimsize);
- @$position[$n] = (strlen($fragment[0]) + $position[($n-1)]);
- }
- return $position;
- }
-
- function add_access($user, $path, $mode)
- {
- //If the Linkers Directory isn't created, end the action...
- if( !@is_link("/var/dav/$user") )
- $this->first_login($user);
-
- $string = $this->file_get_contents("$path/.htaccess");
-
- //If the User isn't present in the whole .htaccess file, add him to the choosen directive and to the linkers...
- if( !strstr($string, "$user ") ) {
- $string = str_replace ("##$mode-Access\nRequire user", "##$mode-Access\nRequire user $user", $string);
- $string = str_replace ("##Linkers:", "##Linkers: $user", $string);
- $this->file_write_string ("$path/.htaccess", "$string");
- return true;
- }
-
- //The User is present in the .htaccess! Checking the positions...
- $position = $this->multi_strpos($user, $string); $p = 0;
-
- while(@$position[$p] < strlen($string) && @$position[$p] != "") {
- $pos = $position[$p];
-
- //If the User is listed already in the choosen directive...
- if( strpos($string, "##$mode-Access") < $pos && $pos < strpos($string, "##END $mode-Access") ) {
- return true;
- }
-
- //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...
- $usersRW = strchr($string, "R/W-Access"); $usersRW = substr($usersRW,0,strpos($usersRW,"#")-1);
- if( strpos($usersRW, $user) ) { return true;
- } else {
- $string = str_replace ("##$mode-Access\nRequire user", "##$mode-Access\nRequire user $user", $string);
- $string = str_replace ("##R/W-Access\nRequire user", "##R/W-Access\nRequire user $user", $string);
- $this->file_write_string ("$path/.htaccess", "$string");
- return true;
- }
- $p++;
- }
- $this->file_write_string ("$path/.htaccess", "$string");
- return false;
- }
-
- function remove_access($user, $path, $mode)
- {
- $string = $this->file_get_contents("$path/.htaccess");
-
- $position = $this->multi_strpos("$user ", $string); $p = 0;
- $removed = false; $clear = true;
-
- while(@$position[$p] < strlen($string) && @$position[$p] != "") {
- $pos = $position[$p];
- if( strpos($string, "##$mode-Access") < $pos && $pos < strpos($string, "##END $mode-Access") ) {
- //...delete the user from the selected directive...
- $string = substr_replace($string, "", $pos, strlen($user)+1);
- //CAUTION: Stringposition changed!!!
- $position = $this->multi_strpos("$user ", $string); $p = -1;
- $removed = true;
- }
- if( $removed && strpos($string, "##R/W-Access") < $pos && $pos < strpos($string, "##END R/W-Access") ) {
- //...delete the user from the R/W-directive...
- $string = substr_replace($string, "", $pos, strlen($user)+1);
- //CAUTION: Stringposition changed!!!
- $position = $this->multi_strpos("$user ", $string); $p = -1;
- $clear = false;
- }
- if( $removed && $clear && strpos($string, "##Linkers:") < $pos ) {
- $string = substr_replace($string, "", $pos, strlen($user)+1);
- $this->file_write_string ("$path/.htaccess", "$string");
- return true;
- }
- $p++;
- }
- $this->file_write_string ("$path/.htaccess", "$string");
- }
-
- function first_login($username)
- {
- $dest_apache = "/etc/apache";
- $dest_dav = "/var/dav/$username";
- global $GO_CONFIG;
- $path = $GO_CONFIG->file_storage_path.$username;
- $path_home = $GO_CONFIG->go_storage_path.$username;
- if(!@is_dir($path_home))
- @mkdir($path_home, $GO_CONFIG->create_mode );
- //If DAV is enabled, create the user...
- if(@is_dir("$dest_apache/sites-enabled")) {
- if(!@is_file("$dest_apache/sites-enabled/$username")) {
- $string = $this->file_get_contents("$dest_apache/sites-available/UserDefault");
- $string = str_replace("USERNAME", $username, $string);
- $this->file_write_string("$dest_apache/sites-enabled/$username","$string");
- }
- //First User on the system -> the Directory /var/dav has to be create
- if(!@is_dir(dirname($dest_dav))) {
- $this->make_dirs(dirname($dest_dav),0755);
- symlink($path,$dest_dav);
- } elseif(!@is_link($dest_dav)) {
- symlink($path,$dest_dav);
- }
- }
- }
-
- function check_login($username)
- {
- $dest_apache = "/etc/apache";
- $dest_dav = "/var/dav/$username";
- global $GO_CONFIG;
- $path = $GO_CONFIG->file_storage_path.$username;
- $path_home = $GO_CONFIG->go_storage_path.$username;
- if(!@is_dir($path_home))
- @mkdir($path_home, $GO_CONFIG->create_mode );
- if(!@is_file("$dest_apache/sites-enabled/$username")) {
- $string = $this->file_get_contents("$dest_apache/sites-available/UserDefault");
- $string = str_replace("USERNAME", $username, $string);
- $this->file_write_string("$dest_apache/sites-enabled/$username","$string");
- }
- if(!@is_link($dest_dav)) {
- symlink($path,$dest_dav);
- }
- }
-
- function add_share($user_id, $path)
- {
- global $GO_CONFIG;
- require_once($GO_CONFIG->class_path.'users.class.inc');
- $users = new users();
- $temp = $users->get_user($user_id); $owner = $temp[1];
- //Load the .htaccess file into the new ShareDirectory...
- $string = $this->file_get_contents("/etc/apache/sites-available/ShareAccess");
- $string = str_replace("USER", $owner, $string);
- $this->file_write_string("$path/.htaccess","$string");
- }
-
- function delete_share($path)
- {
- global $GO_CONFIG;
- //deleting also the linked directories in the LINKER-SHARE-DIRECTORY
- $shareControl = "$path/.htaccess";
-
- $linkers = $this->file_get_contents($shareControl); $linkers = strchr($linkers, "##Linkers:");
- $list = explode (" ", $linkers); $i=1;
-
- while(@$list[$i] != "" && @$list[$i] != "#") {
- $user = $list[$i];
- $dest_dav = str_replace($GO_CONFIG->file_storage_path, "/var/dav/$user/freigaben/", $path);
- if(@is_link($dest_dav)) {
- unlink($dest_dav);
- @rmdir(dirname($dest_dav));
- }
- $i++;
- }
- //deleting the share-access and all granted permissions...
- if (is_file($shareControl)) { unlink($shareControl); }
- }
-
- function update_share($old_path, $new_path)
- {
- global $GO_CONFIG;
-
- //searching for the linkers...
- $shareControl = "$new_path/.htaccess";
- $linkers = $this->file_get_contents($shareControl);
- $linkers = strchr($linkers, "##Linkers:");
- $list = explode (" ", $linkers); $i=1;
-
- while(@$list[$i] != "" && !ereg("#",$list[$i]) ) {
- $user = $list[$i];
- $old_dest_dav = str_replace($GO_CONFIG->file_storage_path, "/var/dav/$user/freigaben/", $old_path);
- $new_dest_dav = str_replace($GO_CONFIG->file_storage_path, "/var/dav/$user/freigaben/", $new_path);
- //deleting the old link...
- if(is_link($old_dest_dav)) {
- unlink($old_dest_dav);
- symlink($new_path, $new_dest_dav);
- //creating the new link...
- } elseif(!is_link($new_dest_dav)) { symlink($new_path,$new_dest_dav); }
-
- $i++;
- }
- }
-
- function init_linkers($linker_id, $acl_id)
- {
- $db = new db();
- $sql_query = "SELECT * FROM fsShares WHERE fsShares.acl_read='$acl_id' OR fsShares.acl_write='$acl_id'";
- $db->query($sql_query);
- $db->next_record();
- $result = $db->Record;
-
- global $GO_CONFIG;
- require_once($GO_CONFIG->class_path.'users.class.inc');
- $users = new users();
- $temp = $users->get_user($result['user_id']);
- $owner = $temp[1];
- $temp = $users->get_user($linker_id);
- $linker = $temp[1];
- $array[0] = $linker;
-
- $path = $result['path'];
- $array[1] = $path;
- $share = strchr($path,"$owner/");
- //Linkers-Share-Folder
- $array[2] = "/var/dav/$linker/freigaben/$share"; //dest_dav
-
- //Check the acl_id permission with the current acl_id. If it is the acl_read, it should be incremented
- // for the checking of the permitted writers and vice versa...
- $array[3] = false; $array[4] = false; $array[5] = false;
- if ($result['acl_read'] == $acl_id) { $array[3] = true; $acl_id_new = $acl_id + 1 ; }
- if ($result['acl_write'] == $acl_id) { $array[4] = true; $acl_id_new = $acl_id - 1; }
- $sql_permission = "SELECT acl.user_id AS id FROM acl WHERE acl.acl_id = '$acl_id_new'";
- $db->query($sql_permission);
- $db->next_record();
- $user_array = $db->Record;
- if (@in_array($linker, $user_array)) $array[5] = true;
-
- return $array;
- }
-
- function add_user($linker_id, $acl_id)
- {
- $result = $this->init_linkers($linker_id, $acl_id);
- $linker = $result[0]; $path = $result[1]; $dest_dav = $result[2];
- $r_access = $result[3]; $w_access = $result[4]; $rw_access = $result[5];
-
- //If the share is activated...
- $return = false;
- if (is_file("$path/.htaccess")) {
- //Add granted permissions...
- if ( $r_access ) $return = $this->add_access($linker, $path, "R");
- if ( $w_access ) $return = $this->add_access($linker, $path, "W");
- if ( $rw_access ) $return = $this->add_access($linker, $path, "R/W");
- }
- //Generate the "Link" to the share into the Linkers-Share-Folder.
- //It's necessary to check if the parent directories exists.
- if( $return ) {
- if(!is_dir(dirname($dest_dav))) {
- $this->make_dirs(dirname($dest_dav),0755);
- symlink($path,$dest_dav);
- } elseif(@!is_link($dest_dav) && @!is_dir($dest_dav)) {
- symlink($path,$dest_dav);
- }
- }
-
- }
-
- function delete_user($linker_id, $acl_id)
- {
- $result = $this->init_linkers($linker_id, $acl_id);
- $notlinker = $result[0]; $path = $result[1]; $dest_dav = $result[2];
- $r_access = $result[3]; $w_access = $result[4]; $rw_access = $result[5];
-
- if ( $r_access ) {
- if( $this->remove_access($notlinker, $path, "R") ) {
- if(is_link($dest_dav)) {
- unlink($dest_dav);
- @rmdir(dirname($dest_dav));
- }
- }
- } elseif ( $w_access ) {
- if( $this->remove_access($notlinker, $path, "W") ) {
- if(is_link($dest_dav)) {
- unlink($dest_dav);
- @rmdir(dirname($dest_dav));
- }
- }
- }
- }
-
- function grouping($group_id, $acl_id, $switch)
- {
- $db = new db();
- $sql_query = "SELECT * FROM fsShares WHERE fsShares.acl_read='$acl_id' OR fsShares.acl_write='$acl_id'";
- $db->query($sql_query);
- $db->next_record();
- $result = $db->Record;
- $owner_id = $result['user_id'];
-
- $db = new db();
- $sql_query = "SELECT acl.user_id AS id FROM acl
- WHERE acl.acl_id = '$acl_id' AND acl.user_id != 0 AND acl.user_id != '$owner_id'
- UNION SELECT users_groups.user_id AS id FROM acl
- JOIN users_groups USING ( group_id )
- WHERE acl_id = '$acl_id' AND users_groups.user_id != 1 AND users_groups.user_id != '$owner_id'";
- $db->query($sql_query);
- $db->next_record();
- while($db->Record != "") {
- $result = $db->Record;
- if( $switch == "add" ) $this->add_user($result['id'], $acl_id);
- if( $switch == "delete" ) $this->delete_user($result['id'], $acl_id);
- $db->next_record();
- }
- }
-
- function write_permissions($user_id, $path, $acl_share_id)
- {
- $db = new db();
- $users = new users();
-
- //Username of Shareowner
- $temp = $users->get_user($user_id);
- $owner = $temp[1]; $owner_id = $user_id;
-
- $share = strchr($path,"$owner/");
-
- //Array with checked-users...
- $ar_checkedusers = array(); $c=0;
- $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'
- UNION SELECT users_groups.user_id AS id FROM acl JOIN users_groups USING (group_id)
- WHERE acl_id = '$acl_share_id' AND users_groups.user_id != 1 AND users_groups.user_id != '$owner_id'";
- $db->query($sql_checkedusers);
- $db->next_record();
- while($db->Record != "") {
- $result = $db->Record;
- $temp = $users->get_user($result['id']);
- $linker = $temp['username'];
- array_unshift ($ar_checkedusers, $linker);
- $db->next_record();
- }
- array_unique($ar_checkedusers);
-
- //Array with all read-users...
- $ar_readusers = array(); $acl_read = $acl_share_id - 1;
- $sql_readusers = "SELECT acl.user_id AS id FROM acl WHERE acl.acl_id = '$acl_read'";
- $db->query($sql_readusers);
- $db->next_record();
- while($db->Record != "") {
- $result = $db->Record;
- $temp = $users->get_user($result['id']);
- $reader = $temp['username'];
- array_unshift ($ar_readusers, $reader);
- $db->next_record();
- }
- array_unique($ar_readusers);
-
- while(@$ar_checkedusers[$c] != "") {
- $linker = $ar_checkedusers[$c];
- //Linkers-Share-Folder
- $dest_dav = "/var/dav/$linker/freigaben/$share";
- //If the share is activated...
- $return = false;
- if (is_file("$path/.htaccess")) {
- //Check if the user has also write-permissions...
- if ( in_array($linker,$ar_readusers) ) {
- $return = $this->add_access($linker, $path, "W");
- $return = $this->add_access($linker, $path, "R/W");
- }
- //else add him only to the read-access...
- else {
- $return = $this->add_access($linker, $path, "W");
- }
- }
- //Generate the "Link" to the share into the Linkers-Share-Folder.
- //It's necessary to check if the parent directories exists.
- if( $return ) {
- if(!is_dir(dirname($dest_dav))) {
- $this->make_dirs(dirname($dest_dav),0755);
- symlink($path,$dest_dav);
- } elseif(!is_link($dest_dav) && !is_dir($dest_dav)) {
- symlink($path,$dest_dav);
- }
- }
- $c++;
- }
-
- //REMOVING THE LINKER-SHARES IF THE CHECKBOX IS UNCHECKED...
- $shareControl = "$path/.htaccess";
- $notlinkers = $this->file_get_contents($shareControl);
- //Checking only the W-Users from .htaccess
- $notlinkers = strchr(strchr($notlinkers, "##W-Access"),"user");
- $notlinkers = substr($notlinkers,0,strpos($notlinkers,$owner)-1);
- $list = explode(" ", $notlinkers); $i=1;
-
- while(@$list[$i] != "" && @$list[$i] != "#") {
- $notlinker = chop($list[$i]);
- $dest_dav = "/var/dav/$notlinker/freigaben/$share";
-
- if( !in_array($notlinker,$ar_checkedusers) && strcmp("#",$notlinker) ) {
- //Read-permission for Not-Linker will be erased...
- if( $this->remove_access($notlinker, $path, "W") ) {
- if(is_link($dest_dav)) {
- unlink($dest_dav);
- @rmdir(dirname($dest_dav));
- }
- }
- }
- $i++;
- }
- }
-
- }
- ?>
-