<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://www.gekkoware.org
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	require_once GEKKO_ROOT."modules/users/main.lib.php";

	# /users/login
	$action = _ext("action", 1);
	$user = _ext("user", 2);
	$edit = _ext("edit", 3);

	$edit = ($edit && gauth("user"));

	switch ($action) {
		case "login":

			use_ssl();

			$tpl = new ContentBlock();
			$tpl->insert ("BLOCK_CONTENT", "users/userLogin.tpl");
			$tpl->set_arr (
				Array (
					"BLOCK_TITLE" => _l("L_LOGIN"),
					"BLOCK_ICON" => mkIcon("login.png", 16),
					"USERNAME" => isset($_GET["user"]) ? htmlspecialchars($_GET["user"]) : "",
					"RETURN" => "../../index.php"
				)
			);
			$mcBuff = $tpl->make();

			title_attach(_l("L_LOGIN"));

		break;
		case "logout":
			if (gauth("$")) {
				if (!conf::getKey("users", "login.multi_session"))
					// dropping auth key
					$db->query("UPDATE ".db_table("user")." SET AuthKey = '".genkey(32)."' WHERE ID = '".$User["ID"]."'", __FILE__, __LINE__);
				// cleaning cookie
				gcookie("auth", "", 1);
			}
			abort(header("Location: ../../index.php"));
		break;
		case "confirm":

    		use_ssl();

			if (conf::getKey("users", "account.require_confirmation")) {

				$tpl = new ContentBlock();
				$tpl->insert ("BLOCK_CONTENT", "users/confirm.tpl");
				$tpl->set_arr (
					Array (
						"BLOCK_TITLE" => _l("L_ACCOUNT_CONFIRMATION"),
						"BLOCK_ICON" => mkIcon("users.png", 16),
						"RETURN" => "../../index.php"
					)
				);

				$mcBuff = $tpl->make();
				title_attach(_l("L_ACCOUNT_CONFIRMATION"));

			} else {
				$mcBuff = _l("L_DISABLED_FEATURE");
			}
		break;
		case "resetpasswd":

    		use_ssl();

			if (conf::getKey("users", "account.enable_password_reset")) {
				$tpl = new ContentBlock();
				$tpl->insert ("BLOCK_CONTENT", "users/resetpasswd.tpl");
				$tpl->set_arr (
					Array (
						"BLOCK_TITLE" => _l("L_RESET_PASSWORD"),
						"BLOCK_ICON" => mkIcon("password.png", 16),
						"RETURN" => "../../index.php",
						"SENT" => isset($_GET["sent"]) ? intval($_GET["sent"]) : 0,
						"USERNAME" => isset($_GET["user"]) ? htmlspecialchars($_GET["user"]) : "",
						"CONFIRMATION" => isset($_GET["code"]) ? htmlspecialchars($_GET["code"]) : "",
						"EMAIL" => isset($_GET["email"]) ? htmlspecialchars($_GET["email"]) : ""
					)
				);
				$mcBuff = $tpl->make();

				title_attach(_l("L_RESET_PASSWORD"));
			} else {
				$mcBuff = _l("L_DISABLED_FEATURE");
			}
		break;
		case "register":

   			use_ssl();

			if (conf::getKey("users", "account.enable_self_registration")) {
				$tpl = new ContentBlock();
				$tpl->insert("BLOCK_CONTENT", "users/selfRegister.form.tpl");
				$tpl->set_arr(
					Array (
						"BLOCK_TITLE" => _l("L_USER_REGISTRATION"),
						"BLOCK_ICON" => mkIcon("users.png", 16),
						"RETURN" => "../../index.php/users/login",
					)
				);
				$mcBuff = $tpl->make();

				title_attach(_l("L_USER_REGISTRATION"));
			} else {
				$mcBuff = _l("L_DISABLED_FEATURE");
			}
		break;
		case "profile" :

			$gender = Array (
				_l("L_UNKNOWN"),
				_l("L_MALE"),
				_l("L_FEMALE")
			);

			$id = intval(_ext("id"));

			if (($user && (!$edit || !gauth("$"))) || ($id > 0)) {
				// viewing user's profile

				$db->query ("
					SELECT ID, Username, Realname FROM ".db_table("user")."
					WHERE ".($id ? "ID = '{$id}'" : "Username = '{$user}'")."
				", __FILE__, __LINE__);

				if ($db->num_rows()) {
					// saving first query info into buffer
					$buff = $db->fetch_row();

					saneHTML($buff);

					$user_id = $buff["ID"];

					$db->query("
						SELECT
							Nickname,
							PublicEmail,
							BirthDate,
							WebSite,
							Avatar,
							AboutMe,
							Signature,
							Location,
							Gender,
							imMSN,
							imYIM,
							imICQ
						FROM ".db_table("user_profile")."
						WHERE ID = '".$buff["ID"]."'
					", __FILE__, __LINE__);

					if ($db->num_rows()) {

						$buff2 = $db->fetch_row();
						// this avoid AboutMe and Signature from being "html escaped"
						saneHTML($buff2, "AboutMe,Signature");

						$buff = array_merge($buff, $buff2);
					} else {
						$mcBuff = _l("L_NO_PROFILE");
						break;
					}

					$buff["AGE"] = getAge($buff["BirthDate"]);
					$buff["AVATAR.URL"] = getAvatar($buff["Avatar"]);
					$buff["WEBSITE.URL"] = safeLink($buff["WebSite"]);
					$buff["Gender"] = $gender[$buff["Gender"]];
					Format::Style($buff["AboutMe"], $user_id);
					Format::Style($buff["Signature"], $user_id);

					$tpl = new ContentBlock();
					$tpl->insert("BLOCK_CONTENT", "users/viewProfile.tpl");
					$tpl->set_arr($buff);

					$tpl->set_arr(
						Array (
							"BLOCK_TITLE" => _l("L_USER_PROFILE"),
							"BLOCK_ICON" => mkIcon("profile.png", 16)
						)
					);

					$mcBuff = $tpl->make();

					title_attach(_l("L_USER_PROFILE").": {$buff["Username"]}");
				}

			} else {
				// profile editing
				if ($user && $edit) {
					$db->query("SELECT ID, Username, Email FROM ".db_table("user")." WHERE Username = '".$user."'", __FILE__, __LINE__);
					$u = $db->fetch_row();
				} else {
					$u = $User;
				}

				$avatars = getAvatars();

				$db->query ("
					SELECT
						Nickname, PublicEmail, BirthDate, WebSite, Avatar, AboutMe,
						Signature, Location, Gender, imMSN, imYIM, imICQ
					FROM ".db_table("user_profile")."
					WHERE ID = '".$u["ID"]."'
				", __FILE__, __LINE__);

				if (!$db->num_rows()) {
					$row = Array (
						"Nickname" => $u["Username"],
						"PublicEmail" => $u["Email"],
						"WebSite" => "",
						"Avatar" => "default.png",
						"AboutMe" => "",
						"Signature" => "",
						"Location" => "",
						"Gender" => "0",
						"imMSN" => "",
						"imICQ" => "",
						"imYIM" => "",
						"BirthDate" => date("Y")."01010000"
					);
				} else {
					$row = $db->fetch_row();
					saneHTML($row);
				}

				$tpl = new ContentBlock();
				$tpl->insert("BLOCK_CONTENT", "users/editProfile.tpl");
				$tpl->insert("PROFILE.FORM", "users/profile.form.tpl");

				$tpl->set_arr($row);
				$tpl->set_arr(
					Array (
						"BLOCK_TITLE" => _l("L_MY_PROFILE"),
						"BLOCK_ICON" => mkIcon("profile.png", 16),
						"ID" => $u["ID"],
						"ACTION" => "profile",
						"RETURN" => "",
						"AVATAR.SEL" => mkSelect($avatars, $row["Avatar"]),
						"AVATAR.URL" => getAvatar($row["Avatar"]),
						"BIRTHDATE.SEL" => mkDateSelect("birthdate", $row["BirthDate"]),
						"GENDER.SEL" =>  mkSelect($gender, $row["Gender"], "gender"),
					)
				);

				$mcBuff = $tpl->make();

				title_attach(_l("L_MY_PROFILE"));
			}
		break;
		case "account":
			if (gauth("$")) {

   				use_ssl();

				$tpl = new ContentBlock();
				$tpl->insert("BLOCK_CONTENT", "users/myAccount.tpl");
				$tpl->insert("ACCOUNT.FORM", "users/account.form.tpl");

				$db->query("
					SELECT ID, Username, Realname, Email, SesLen, Preferences
						FROM ".db_table("user")."
					WHERE ID = ".$User["ID"]."
					",__FILE__, __LINE__);

				$row = $db->fetch_row();
				saneHTML($row);

				$tpl->set_arr($row);
				$tpl->set_arr(
					Array (
						"BLOCK_TITLE" => _l("L_MY_ACCOUNT"),
						"BLOCK_ICON" => mkIcon("account.png", 16),
						"ID" => $User["ID"],
						"ACTION" => "account",
						"RETURN" => "",
					)
				);

				$mcBuff = $tpl->make();

				title_attach(_l("L_MY_ACCOUNT"));
			} else {
				$mcBuff = accessDenied(0);
			}

		break;
		default:
			if (gauth("$")) {

				$Panel = new Panel(_l("L_USER_PANEL"), "account.png");

				$basedir = GEKKO_ROOT."modules/";
				$moddir = opendir($basedir);
				while (($file = readdir($moddir)) !== false) {
					if (($file != "." && $file != "..")) {
						// check out: ./modules/some_module/user.panel.php
						$icon = $basedir."$file/user.panel.php";
						if (file_exists($icon)) include $icon;
					}
				}
				closedir($moddir);

				$mcBuff .= $Panel->make();

				title_attach(_l("L_USER_PANEL"));
			} else {
				abort(header("Location: users/register"));
			}

		break;
	}

	$mc->save($mcBuff);
?>