<?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
*/

  if (!defined('ROOTPATH')) die;

  $rights = array();
  require_once(ROOTPATH."/modules/admin/inc/rights.inc.php");
  if (count($rights)>0) set_default_rights_admin("root_modules_admin_",$rights);

  $rights = array();
  require_once(ROOTPATH."/modules/settings/inc/rights.inc.php");
  if (count($rights)>0) set_default_rights("root_modules_settings_",$rights);

  $rights = array();
  require_once(ROOTPATH."/modules/overview/inc/rights.inc.php");
  if (count($rights)>0) set_default_rights("root_modules_overview_",$rights);

  $rights = array();
  require_once(ROOTPATH."/modules/admin2/inc/rights.inc.php");
  if (count($rights)>0) set_default_rights_admin("root_modules_admin2_",$rights);

  if (count($rights)>0) set_default_rights("root_modules_admin2_",array("accessmodule"=>0));


  $sql = "SELECT id FROM mgw_groups WHERE id=50000";
  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  $row = $res->FetchRow();

  if (!isset($row["id"])) {
    $sql = "INSERT INTO mgw_groups (id,name,description,lu_user,lu_date) VALUES (50000,'Admin','Admin Group',1,NOW())";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  }


  $sql = "SELECT id FROM mgw_users WHERE username=".$conn->QMagic("admin");
  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  $row = $res->FetchRow();

  if (!isset($row["id"])) {
    $sql = "INSERT INTO mgw_users (id,username,lastname,firstname,email,mobil,skintheme,pw,lang,level,lu_user,lu_date,lastin,is_ldap) VALUES (1,'admin','AdminLastName','AdminFirstName','admin@mgw.invalid','','Morelogs_Silver.css','21232f297a57a5a743894a0e4a801fc3','login',9,1,NOW(),NOW(),0)";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  }


  $sql = "SELECT groupid FROM mgw_groups_members WHERE groupid=50000 and userid=1";
  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  $row = $res->FetchRow();

  if (!isset($row["groupid"])) {
    $sql = "INSERT INTO mgw_groups_members (groupid,userid,lu_user,lu_date) VALUES (50000,1,1,NOW())";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  }
  

  /*
  // Script for exporting db to xml
  $sql = "SELECT * FROM mgw_countries ORDER BY code";
  if(!$res = $conn->Execute($sql)) exit(showSQLerror2($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  while ($row = $res->FetchRow()) {
    echo "\t".'<mgw_countries code="'.$row["code"].'" name="'.$row["name"].'" />'."\n";
  }
  */
  
  $countries2 = array();
  $countries = get_definition_countries_from_xml(ROOTPATH."/modules/general/definition2.xml");
  foreach ($countries as $country) {
    if (!isset($country["region"])) $country["region"] = "";
	if (isset($country["code"]) and isset($country["name"])) {
      $countries2[$country["code"]] = $country;
	  
	  $sql = "SELECT code FROM mgw_countries WHERE code=".$conn->QMagic($country["code"]);
	  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	  $row = $res->FetchRow();

	  if (!isset($row["code"])) {
	    $sql = "INSERT INTO mgw_countries (name, code, region, lu_date, lu_user) VALUES (".$conn->QMagic($country["name"]).",".$conn->QMagic($country["code"]).",".$conn->QMagic($country["region"]).",NOW(),1)";
	    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	  }
	}
  }

  $sql = "SELECT * FROM mgw_countries ORDER BY code";
  if(!$res = $conn->Execute($sql)) exit(showSQLerror2($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  while ($row = $res->FetchRow()) {
    if (!isset($countries2[$row["code"]])) {
	  $sql = "DELETE FROM mgw_countries WHERE code=".$conn->QMagic($row["code"]);
	  if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	}
  }

?>