<?php
/*
This file is part of Thomas Bley's moregroupware webmail2/calendar2/admin2 module. (tb-mgw)

More information:
-----------------

Simple Groupware Solutions Thomas Bley
Fichtenstr. 23
Germany, 85640 Putzbrunn

Contact:
--------

info@simple-groupware.de

http://www.simple-groupware.de


Copyright 2002-2003 Thomas Bley
tb-mgw is distributed under the terms of the GNU General Public License 

tb-mgw 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.

tb-mgw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with tb-mgw; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Tree
// Moregroupware.org

$myEnv["module"] = "admin2";
$myEnv['stdsmenu'] = false;
$myEnv["output"] = "html";

include("../../config.inc.php");
include(INCLUDEPATH . "container.inc.php");

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$myvars = array("id","return","highlight_user","showonly");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}
$id_string = $id;

if ($highlight_user=="") $highlight_user=$_SESSION["MGW"]->username;

$users = array();
$users2 = array();
$users_sys = array();
$groups = array();
$groups2 = array();
$groups_sys = array();
$group_members = array();


$sql = "select username,id from mgw_users order by username";
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
while ($row = $res->FetchRow()) {
  $users[$row["username"]] = $row["id"];
  $users2[] = array($row["id"],$row["username"]);
  $users_sys[$row["id"]] = array($row["id"],$row["username"]);
  $users4[$row["id"]] = $row["id"];
}

$sql = "select name,id from mgw_groups order by name";
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
while ($row = $res->FetchRow()) {
  $groups[$row["name"]] = $row["id"];
  $groups2[] = array($row["id"],$row["name"]);
  $groups_sys[$row["id"]] = array($row["id"],$row["name"]);

  $sql = "select u.id from mgw_groups_members m, mgw_users u where m.groupid=".(int)$row["id"]." and m.userid=u.id order by u.username";
  if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  while ($row2 = $res2->FetchRow()) {
    $group_members[$row["id"]][] = $row2["id"];
  }
}

$users_all = $users_sys;

$admin2_rights = array();
$admin2_rights["root"] = array();
$fields = array();

$valid_user_nodes_all = array();
foreach ($_SESSION["MGW"]->modules as $module) {
  if ($module["active"]=="1" and file_exists("../".$module["modulename"]."/inc/rights.inc.php")) {
    $admin2_rights["root"][$module["modulename"]] = array();
    $rights = array();
	$valid_user_nodes = array();
    include "../".$module["modulename"]."/inc/rights.inc.php";
	if (is_array($valid_user_nodes) and count($valid_user_nodes)>0) {
	  foreach ($valid_user_nodes as $key=>$key2) {
	    $valid_user_nodes[$key] = "root_modules_calendar2_".$valid_user_nodes[$key];
	  }
	  $valid_user_nodes_all = array_merge($valid_user_nodes,$valid_user_nodes_all);
	}
    $admin2_rights["root"][$module["modulename"]] = $rights;

	if (isset($rights) and is_array($rights) and count($rights)>0) {
	  $parent = 'root_modules_'.$module["modulename"];
	  process_rights2($rights,$parent);
	}
  }
}

if (isset($fields[$id_string])) {
  $default = $fields[$id_string];
  if ($_SESSION["MGW"]->username == "admin") {
    $access_to_node = true;
  } else {
    $access_to_node = false;
    foreach ($valid_user_nodes_all as $key3=>$key4) {
      if (strpos("@".$id_string,$key4)==1) {
	    $access_to_node = true;
	  }
    }
  }
  if ($access_to_node) {
	$smarty->assign("access_to_node",$access_to_node);
	
	$allow_users = array();
	$deny_users = array();
	$allow_groups = array();
	$deny_groups = array();

	if (isset($_POST["save"])) {
  	  if (isset($_POST["allow_users"]) and is_array($_POST["allow_users"]) and count($_POST["allow_users"])>0) $allow_users = $_POST["allow_users"];
	  if (isset($_POST["deny_users"]) and is_array($_POST["deny_users"]) and count($_POST["deny_users"])>0) $deny_users = $_POST["deny_users"];

	  if (isset($_POST["allow_groups"]) and is_array($_POST["allow_groups"]) and count($_POST["allow_groups"])>0) $allow_groups = $_POST["allow_groups"];
	  if (isset($_POST["deny_groups"]) and is_array($_POST["deny_groups"]) and count($_POST["deny_groups"])>0) $deny_groups = $_POST["deny_groups"];
	
	  sort($allow_users);
	  sort($deny_users);
	  sort($allow_groups);
	  sort($deny_groups);

      $sql = "select * from mgw_rights2 where id_string=".$conn->Quote($id_string);
	  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  if ($res->RecordCount()==0) {
	    $sql = "insert into mgw_rights2 (default_value,id, id_string, allow_users, deny_users, allow_groups, deny_groups, cuser, cdate) values (
		".(int)$fields[$id_string].",
		".mgw_genID('mgw__seq_rights2').",
		".$conn->Quote($id_string).",
		".$conn->Quote(",".implode($allow_users,",").",").",
		".$conn->Quote(",".implode($deny_users,",").",").",
		".$conn->Quote(",".implode($allow_groups,",").",").",
		".$conn->Quote(",".implode($deny_groups,",").",").",
		".$_SESSION["MGW"]->userid.",".time().")";
		if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  } else {
	    $sql = "update mgw_rights2 set default_value=".(int)$fields[$id_string].",allow_users=".$conn->Quote(",".implode($allow_users,",").",").", deny_users=".$conn->Quote(",".implode($deny_users,",").",").", allow_groups=".$conn->Quote(",".implode($allow_groups,",").",").", deny_groups=".$conn->Quote(",".implode($deny_groups,",").",").", cuser=".$_SESSION["MGW"]->userid.", cdate=".time()." where id_string=".$conn->Quote($id_string);
		if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  }
	  if (isset($_POST["return"]) and $_POST["return"]!="") {
	    header("Location: main.php?id=".$_POST["return"]."&".SID);
		exit;
	  }
	}
	
	$allow_users = array();
	$deny_users = array();
	$allow_groups = array();
	$deny_groups = array();
	
    $sql = "select * from mgw_rights2 where id_string=".$conn->Quote($id_string);
	if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

	if ($fields[$id_string]==0) {
	  $deny_users = $users_sys;
	  $deny_groups = $groups_sys;
    } else {
	  $allow_users = $users_sys;
	  $allow_groups = $groups_sys;
	}

	if ($res->RecordCount()!=0) {
	  $row = $res->FetchRow();
	  
	  if (isset($users_sys[$row["cuser"]])) {
	    $smarty->assign("cuser",$users_sys[$row["cuser"]][1]);
	    $smarty->assign("cdate",date("r",$row["cdate"]));
	  }
	  
	  $allow_users_db = explode(",",$row["allow_users"]);
	  $deny_users_db = explode(",",$row["deny_users"]);
	  $allow_groups_db = explode(",",$row["allow_groups"]);
	  $deny_groups_db = explode(",",$row["deny_groups"]);

	  if (is_array($allow_users_db) and count($allow_users_db)>0) {
 	    foreach ($allow_users_db as $allow_user) {
		  if (isset($users_sys[$allow_user])) {
		    $allow_users[$users_sys[$allow_user][0]] = array($users_sys[$allow_user][0],$users_sys[$allow_user][1]);
			unset($deny_users[$users_sys[$allow_user][0]]);
		  }
	    }
      }
	  
	  if (is_array($deny_users_db) and count($deny_users_db)>0) {
 	    foreach ($deny_users_db as $deny_user) {
		  if (isset($users_sys[$deny_user])) {
		    $deny_users[$users_sys[$deny_user][0]] = array($users_sys[$deny_user][0],$users_sys[$deny_user][1]);
			unset($allow_users[$users_sys[$deny_user][0]]);
		  }
	    }
      }
	  
	  if (is_array($allow_groups_db) and count($allow_groups_db)>0) {
 	    foreach ($allow_groups_db as $allow_group) {
		  if (isset($groups_sys[$allow_group])) {
		    $allow_groups[$groups_sys[$allow_group][0]] = array($groups_sys[$allow_group][0],$groups_sys[$allow_group][1]);
			unset($deny_groups[$groups_sys[$allow_group][0]]);
		  }
	    }
      }

	  if (is_array($deny_groups_db) and count($deny_groups_db)>0) {
 	    foreach ($deny_groups_db as $deny_group) {
		  if (isset($groups_sys[$deny_group])) {
		    $deny_groups[$groups_sys[$deny_group][0]] = array($groups_sys[$deny_group][0],$groups_sys[$deny_group][1]);
			unset($allow_groups[$groups_sys[$deny_group][0]]);
		  }
	    }
      }

	}	
	
	$allow_users2 = array();
	foreach ($allow_users as $allow_user) {
	  $allow_users2[] = $allow_user;
	}
	$deny_users2 = array();
	foreach ($deny_users as $deny_user) {
	  $deny_users2[] = $deny_user;
	}

	$allow_groups2 = array();
	foreach ($allow_groups as $allow_group) {
	  $allow_groups2[] = $allow_group;
	}
	$deny_groups2 = array();
	foreach ($deny_groups as $deny_group) {
	  $deny_groups2[] = $deny_group;
	}

	usort ($allow_users2, "my_cmp");
	usort ($deny_users2, "my_cmp");

	usort ($allow_groups2, "my_cmp");
	usort ($deny_groups2, "my_cmp");

    $smarty->assign("allow_users",$allow_users2);
    $smarty->assign("deny_users",$deny_users2);
    $smarty->assign("allow_groups",$allow_groups2);
    $smarty->assign("deny_groups",$deny_groups2);
  }  
} elseif ($id_string!="") {
  $queries = array();
  foreach ($fields as $key=>$key2) {
	if (strpos("@".$key,$id_string)==1) {
	  if ($_SESSION["MGW"]->username=="admin") {
	    $queries[] = $key;
      } else {
	    if (isset($valid_user_nodes_all) and is_array($valid_user_nodes_all) and count($valid_user_nodes_all)>0) {
	      foreach ($valid_user_nodes_all as $valid_user_node_all) {
		    if (strpos("@".$key,$valid_user_node_all)==1) {
		      $queries[] = $key;
			}
		  }
		}
	  }
	}	  
  }
  
  if (count($queries)>0) {
    if ($showonly) {
	  foreach ($users_sys as $sys_key=>$sys_key2) {
	    if ($sys_key2[1]==$highlight_user) {
		  $users_sys = array($sys_key2);
		  break;
		}
	  }
	}
    $objects = array();
    foreach ($queries as $query) {
      $sql = "select * from mgw_rights2 where id_string=".$conn->Quote($query);
	  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

	  $allow_all_users = array();
	  $deny_all_users = array();

	  if ($fields[$query]==0) {
	    $deny_all_users = $users_sys;
	  } else {
	    $allow_all_users = $users_sys;
	  }
	
	  $cuser = "";
	  $cdate = "";
	  if ($res->RecordCount()!=0) {
	    $row = $res->FetchRow();
		$cuser = $users_all[$row["cuser"]][1];
		$cdate = date("r",$row["cdate"]);

	    $allow_users_db = explode(",",substr($row["allow_users"],1,-1));
	    $deny_users_db = explode(",",substr($row["deny_users"],1,-1));

	    $allow_groups_db = explode(",",substr($row["allow_groups"],1,-1));
	    $deny_groups_db = explode(",",substr($row["deny_groups"],1,-1));

	    if ($fields[$query]==0) {
		  if (count($allow_users_db)>0) {
		    foreach ($allow_users_db as $allow_user) {
			  if (isset($users_sys[$allow_user])) {
		        $allow_all_users[$allow_user] = array($allow_user,$users_sys[$allow_user][1]);
			  }
		      unset($deny_all_users[$allow_user]);
		    }
		  }
		  if (count($allow_groups_db)>0) {
		    foreach ($allow_groups_db as $allow_group) {
		      if (isset($group_members[$allow_group])) {
			    foreach ($group_members[$allow_group] as $group_member) {
			      if (isset($users_sys[$group_member])) {
			        $allow_all_users[$group_member] = array($group_member,$users_sys[$group_member][1]);
				  }
			      unset($deny_all_users[$group_member]);
			    }
			  }
		    }		
		  }
	    } else {
		  if (count($deny_users_db)>0) {
		    foreach ($deny_users_db as $deny_user) {
			  if (isset($users_sys[$deny_user])) {
		        $deny_all_users[$deny_user] = array($deny_user,$users_sys[$deny_user][1]);
			  }
		      unset($allow_all_users[$deny_user]);
		    }
		  }
		  if (count($deny_groups_db)>0) {
		    foreach ($deny_groups_db as $deny_group) {
		      if (isset($group_members[$deny_group])) {
			    foreach ($group_members[$deny_group] as $group_member) {
			      if (isset($users_sys[$group_member])) {
			        $deny_all_users[$group_member] = array($group_member,$users_sys[$group_member][1]);
				  }
			      unset($allow_all_users[$group_member]);
			    }
			  }
		    }		
		  }
		}
	  }

	  usort ($allow_all_users, "my_cmp");
	  usort ($deny_all_users, "my_cmp");

	  $allow_users2 = array();
	  foreach ($allow_all_users as $allow_user) {
		$allow_users2[] = $allow_user;
	  }
	  $deny_users2 = array();
	  foreach ($deny_all_users as $deny_user) {
		$deny_users2[] = $deny_user;
	  }

	  $query_id = $query;
	  
	  $query = explode("_",$query);
	  array_shift($query); array_shift($query);
	  $item = array_shift($query);
	  $query_str = isset($hashmap[$item])?$hashmap[$item]:$item;
	  
	  foreach ($query as $item) {
		if (is_numeric($item)) {
		  $query_str .= " / ".get_calendar_name($item);
	    } elseif (isset($hashmap[$item])) {
		  $query_str .= " / ".$hashmap[$item];
	    } else {
	  	  $query_str .= " / ".$item;
		}
	  }

	  $objects[] = array($query_id,$allow_users2,$deny_users2,$cuser,$cdate,$query_str);
	}
	
	$smarty->assign("display_objects",true);
	$smarty->assign("objects",$objects);
  }
}


function process_rights2($rights,$parent) {
  global $fields;
  
  foreach ($rights as $key=>$key2) {
	if (is_array($key2) and count($key2)>0) {
	  process_rights2($key2,$parent."_".$key);
	} else if (!is_array($key2) and ($key2==1 or $key2==0)) {
      $fields[$parent.'_'.$key] = $key2;
	}
  }
}

$users = array();
foreach ($users_all as $user) {
  $users[] = $user[1];
}


$query = explode("_",$id_string);
array_shift($query); array_shift($query);
$item = array_shift($query);
$query_str = isset($hashmap[$item])?$hashmap[$item]:$item;
	  
foreach ($query as $item) {
  if (isset($hashmap[$item])) {
	$query_str .= " / ".$hashmap[$item];
  } else {
	$query_str .= " / ".$item;
  }
}


$smarty->assign("users_all",$users);
$smarty->assign("return",$return);
$smarty->assign("id_string",$id_string);
$smarty->assign("query_str",$query_str);
$smarty->assign("username2",ucfirst($_SESSION["MGW"]->username));
$smarty->assign("username3",$highlight_user);
$smarty->display("admin2_main.tpl");
?>