<?php 

/*
 *	W-AGORA 4.1
 *	-----------
 *	$Id: load_users.php,v 1.2 2003/06/23 15:21:48 mdruilhe Exp $
 *	Usage:			Initialize the user database from a text file
 *	Author:			Marc Druilhe <mdruilhe@w-agora.net>
 */

	$agora_dir = "..";

# Format of the CSV file:
# userid|password|userpriv|username|useraddress|details|mailok|forum1[;forum2;...]
#
# example:
# marc|xxxx|user|Marc Druilhe|mdruilhe@w-agora.net|A user|Y|site1_forum1;site1_forum2

	chdir ("$agora_dir");

	$site= (empty($site) ) ? "agora" : $site;

	include "init_admin.inc";

	function reg_user($user_line) {
		global $db, $site;

		if (! ereg("\\|", $user_line)) {
			echo "Warning, Invalid line: [<font color=red>".htmlspecialchars($user_line)."</font>]<br>";
			return false;
		}

		$user = explode ("|", $user_line);
		if (!is_array($user)) {
			echo "Warning, Invalid line: [<font color=red>".htmlspecialchars($user_line)."</font>]<br>";
			return false;
		}

		$userid = $user[0];
		$userid = ereg_replace("[[:space:]]+", "", $userid);
		if (empty($userid)) {
			echo "Warning, Invalid line: [<font color=red>".htmlspecialchars($user_line)."</font>]<br>";
			return false;
		}

		echo "User: <b>$userid</b><br>";

		$password = md5($user[1]);

		$userpriv = $user[2];
		if ( ($userpriv != "user") && ($userpriv != "moder") && ($userpriv != "admin") ) {
			$userpriv = "user";
		}

		$username = $user[3];
		if (empty($username)) {
			$username = $userid;
		}

		$useraddress = $user[4];
		if (empty($useraddress)) {
			echo "Line: ".htmlspecialchars($user_line)." invalid -> email is missing<br>";
			return false;
		}

		$details = $user[5];
		$mailok = (empty($user[6])) ? "Y" : $user[6];

		$u_fields["userid"] = $userid;
		$u_fields["password"] = $password;
		$u_fields["userpriv"] = $userpriv;
		$u_fields["username"] = $username;
		$u_fields["useraddress"] = $useraddress;
		$u_fields["details"] = $details;
		$u_fields["mailok"] = $mailok;
		$u_fields["lastlogin"] = 0;
		$u_fields["totallogins"] = 0;

        $tmp = $db->getUser ($site, $userid);
		if (is_array($tmp) ) {
			$ret = $db->updateUser ($site, $userid, $u_fields);
			if ($ret >= 0) {
				echo " already exists => update OK<br>\n";
			}
		} else {
	        $ret = $db->insertUser ($site, $u_fields);
			if ($ret >= 0) {
				echo " => successfully added<br>\n";
			}
		}

		if (!empty ($user[7]) ) {
			$forum = $user[7];
			$key = $now;

			$uf_fields["listpriv"] = 1;
			$uf_fields["readpriv"] = 1;
			$uf_fields["writepriv"] = 1;
			$uf_fields["modpriv"] = 0;
			$uf_fields["state"] = 1;

			$forums = explode (";", $forum);
			print "<ul>";
			while ( list(,$f) = each($forums) ) {
				$uf_fields["unixdate"] = time();
				$uf_fields["bn_name"]=$f;
				$ret = $db->insertPrivs ($site, $f, $userid, $uf_fields);
				if ($ret == 0) {
					echo "<li><B>$userid</B> has been registered in the forum <B>$f</B>\n";
				}
			}
			print "</ul>";
		}

		return true;
	}

	if (empty($site) || ($site=='agora') ) {
		echo "<form method=get action='$PHP_SELF'>";
		echo "Please, select a site : ";
		$db->query ("select bn_name, bn_title from agora");
		while ($db->next_record() ) {
			$s = $db->Record['bn_name'];
			$sites[$s] = $db->Record['bn_title'];
		}
		listBox ("site", $sites);
		submitButton ("go", "select");
		endForm();
		exit;

	}

	echo "<h2>Adding users to the site [$site]</h2>";

	if (is_uploaded_file($HTTP_POST_FILES['csvfile']['tmp_name'])) {
		$reg_users = file($HTTP_POST_FILES['csvfile']['tmp_name']);
		if (!is_array ($reg_users) ) {
			die ("Could not read file " . $HTTP_POST_FILES['csvfile']['name']);
		}
	}

	if (count($reg_users)>=1) {
		$num = 0;
		print "<ul>";
		while ( list (, $user_line) = each ($reg_users)) {
			echo "<li> ";
			if (reg_user ($user_line)) {
				$num++;
			}
		}
		print "</ul>";
		print "$num users recorded";
	}
?>

<form method="post" action="<?echo $PHP_SELF?>" ENCTYPE="multipart/form-data">
<input type=hidden name="site" value="<?echo $site?>">
<input type=hidden name="MAX_FILE_SIZE" value="1000000">
 <TABLE BORDER=0 WIDTH="70%" align=center>
  <TR>
	<TD bgcolor=#e0eaf6 valign=top><b>user (CSV) file to send:</b> </TD>
	<TD bgcolor=#e0eaf6><INPUT TYPE="file" NAME="csvfile" width="260" SIZE="30" /></TD>
  </TR>
  <TR>
	<TD bgcolor=#e0eaf6 colspan=2 align=center ><INPUT TYPE="submit" VALUE="send"></TD>
  </tr>
 </TABLE>
</FORM>
</BODY>
</HTML>
