<?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("startnode");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}

$fields = array();
$users = array();
$groups = array();

$fields[] = 'root_users = insFld(root, gFld("'.addslashes($hashmap["users"]).'", "root_users", "#",0));';
$lastfield = "root_users";

$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()) {
  $fields[] = 'root_users_'.$row["id"].' = insFld('.$lastfield.', gFld("'.$row["username"].'", "root_users_'.$row["id"].'", "#",0));';
  $lastfield2 = 'root_users_'.$row["id"];
  
  $users[$row["username"]] = $row["id"];

  $sql = "select g.name, g.id from mgw_groups_members m, mgw_groups g where m.userid=".(int)$row["id"]." and m.groupid=g.id order by g.name";
  if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  while ($row2 = $res2->FetchRow()) {
    $fields[] = 'root_users_'.$row["id"].'_'.$row2["id"].' = insFld('.$lastfield2.', gFld("'.$row2["name"].'", "root_groups_'.$row2["id"].'", "#",1));';
  }
}

$fields[] = 'root_groups = insFld(root, gFld("'.addslashes($hashmap["groups"]).'", "root_groups", "#",0));';
$lastfield = "root_groups";

$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()) {
  $fields[] = 'root_groups_'.$row["id"].' = insFld('.$lastfield.', gFld("'.$row["name"].'", "root_groups_'.$row["id"].'", "#",0));';
  $lastfield2 = 'root_groups_'.$row["id"];

  $groups[$row["name"]] = $row["id"];

  $sql = "select u.username, 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()) {
    // $fields[] = 'insDoc(root_groups_'.$row["id"].', gLnk("R","'.$row2["username"].'", "#","root_users_'.$row2["id"].'"))';
    $fields[] = 'root_groups_'.$row["id"].'_'.$row2["id"].' = insFld('.$lastfield2.', gFld("'.$row2["username"].'", "root_users_'.$row2["id"].'", "#",1));';
  }
}

// if ($_SESSION["MGW"]->username!="admin") {
  $fields = array();
// }

$fields[] = 'root_modules = insFld(root, gFld("'.addslashes($hashmap["modules"]).'", "root_modules", "#",0));';
$lastfield = "root_modules";

$valid_user_nodes_all = array();
foreach ($_SESSION["MGW"]->modules as $module) {
  if ($module["active"]=="1" and file_exists("../".$module["modulename"]."/inc/rights.inc.php")) {
    $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);
	}
	
	if ($_SESSION["MGW"]->username=="admin" or count($valid_user_nodes)>0) {
  	$fields[] = 'root_modules_'.$module["modulename"].' = insFld('.$lastfield.', gFld("'.addslashes((isset($hashmap[$module["modulename"]])?$hashmap[$module["modulename"]]:$module["modulename"])).'", "root_modules_'.$module["modulename"].'", "#",0));';
	$lastfield2 = 'root_modules_'.$module["modulename"];
        }

	if (isset($rights) and is_array($rights) and count($rights)>0) {
	  process_rights($rights,$lastfield2);
	}
  }
}

function process_rights($rights,$parent) {
  global $fields,$valid_user_nodes_all,$hashmap;

  foreach ($rights as $key=>$key2) {
	$valid_node = true;
	$bigvalid_node = true;
	if ($_SESSION["MGW"]->username!="admin" and isset($valid_user_nodes_all) and is_array($valid_user_nodes_all) and count($valid_user_nodes_all)>0) {
	  $valid_node = false;
	  $bigvalid_node = false;
	  foreach ($valid_user_nodes_all as $key3=>$key4) {
	    if (strpos("@".$parent.'_'.$key,$key4)==1) {
  	      $valid_node = true;
		}
	    if (strpos("@".$key4,$parent.'_'.$key)==1) {
  	      $bigvalid_node = true;
		}
	  }
    } elseif ($_SESSION["MGW"]->username!="admin") {
	  $valid_node = false;
	  $bigvalid_node = false;
	}
    
	if (is_array($key2) and count($key2)>0) {
	  $count_fields = count($fields);
	  
      if ($bigvalid_node) {
	    if (is_numeric($key)) {
		  $key_lang = get_calendar_name($key);
		} elseif (isset($hashmap[$key])) {
		  $key_lang = addslashes($hashmap[$key]);
		} else {
		  $key_lang = $key;
		}
	    $fields[] = $parent.'_'.$key.' = insFld('.$parent.', gFld("'.$key_lang.'", "'.$parent.'_'.$key.'", "#",0));';

	    process_rights($key2,$parent."_".$key);
	    if (count($fields) == $count_fields+1) {
		  $fields[count($fields)-1] = array();	    
	    }
      }
	} else if (!is_array($key2) and ($key2==1 or $key2==0)) {
      if ($valid_node) {
	    if (is_numeric($key)) {
		  $key_lang = $key;
		} elseif (isset($hashmap[$key])) {
		  $key_lang = addslashes($hashmap[$key]);
		} else {
		  $key_lang = $key;
		}
        $fields[] = $parent.'_'.$key.' = insFld('.$parent.', gFld("'.$key_lang.'", "'.$parent.'_'.$key.'", "#",1));';
	  }
	}
  }
}

if ($startnode!="") {
  $first_item = false;
  foreach ($fields as $key=>$key2) {
    if (strpos("@".$key2,$startnode)==1) {
	  if (!$first_item) {
	    $first_item = true;
		$fields[$key] = preg_replace("/insFld\((.*?),/si","insFld(root,",$fields[$key]);
	  }
	} else {
	  $fields[$key] = "";
	}
  }
}
	

$smarty->assign("fields",$fields);
$smarty->assign("username",ucfirst($_SESSION["MGW"]->username));
$smarty->display("admin2_tree.tpl");

?>