<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2006, 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.sourceforge.net>
	*	@link		http://www.gekkoware.org
	*/

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

	define("GEKKO_AVATAR_DIR", GEKKO_SOURCE_DIR."media/avatars/");

	Class Users {
		function mkAvatarChooser($selected = "default.png") {
			return '<div><img class="avatar" src="'.users::getAvatarURL($selected).'" width="50" height="50" /><input readonly="readonly" onclick="avatarChooser(this)" class="text" type="text" name="avatar" size="14" value="'.$selected.'" /></div>';
		}
		/**
		* listGenders(gender_id);
		* @returns: human readable gender given its gender ID
		*/
		function listGenders($id) {
			$gender = Array (
				_l("L_UNKNOWN"),
				_l("L_MALE"),
				_l("L_FEMALE")
			);
			return ($id < 0) ? $gender : $gender[$id];
		}

		function listSessionTimes($id = 0) {
			$session = Array(
				_l("L_DEFAULT"),
				_l("L_A_HOUR"),
				_l("L_A_DAY"),
				_l("L_A_WEEK"),
				_l("L_A_MONTH")
			);

			return $id ? $session[$id] : $session;
		}

		/**
		* fetchInfo(user_id, [field list]);
		* @returns: an array containing requested user fields of false if the user doesn't exists
		*/
		function fetchInfo($id, $field = "*") {

			$q = $GLOBALS["db"]->query("SELECT $field FROM
			".db_table("user")." WHERE id = '{$id}'", __FILE__, __LINE__, true);

			if ($GLOBALS["db"]->numRows($q)) {
				$row = $GLOBALS["db"]->fetchRow($q);
				return ($field == "*" || preg_match("/,/", $field)) ? $row : $row[$field];
			} else {
				return false;
			}

		}

		/**
		* mkAvatar(user_id, [avatar size]);
		* @returns: HTML formatted avatar for the given user_id
		*/
		function mkAvatar($id, $email = false, $size = 50, $avatar = false) {
			if ($id) {
				$avatar = cacheFunc("db_get_field", "user_profile", $id, "avatar");
				$email = cacheFunc("db_get_field", "user_profile", $id, "public_email");
			}
			return "<img src=\"".($url = users::getAvatarURL($avatar, $email))."\" width=\"{$size}\" height=\"{$size}\" alt=\"\" />";
		}

		/**
		* listAvatars();
		* @returns: a list of avatars found in GEKKO_AVATAR_DIR
		*/
		function listAvatars() {
			$avdir = listdir(GEKKO_AVATAR_DIR);
			$info = @parse_ini_file(GEKKO_AVATAR_DIR."avatars.ini");
			$avatars = Array();
			natsort($avdir);
			foreach ($avdir as $avatar) {
				if (ereg("(png|gif|jpg)", file_extension($avatar))) {
					$avatars[$avatar] = isset($info[$avatar]) ? $info[$avatar] : $avatar;
				}
			}
			return $avatars;
		}


		/**
		* getAvatarURL(avatar, [user.email]);
		* @returns: URL for specified avatar or, if avatar is null, its gravatar URL (this is a nice
		* feature for guest's comments that I saw in jaws cms)
		*/
		function getAvatarURL($avatar = null, $email = false) {

			// avatar is null, checking for gravatar
			if (!$avatar)
				$avatar = (conf::getKey("users", "gravatar", 0, "b") && $email) ? "http://gravatar.com/avatar.php?gravatar_id=".md5(strtolower($email))."&size=50&default=".urlencode(_l("C_SITE.URL")."media/avatars/default.png") : "default.png";

			if (strtolower(substr($avatar, 0, 7)) != "http://")
				$avatar = _l("C_SITE.URL")."media/avatars/".(($avatar && file_exists(GEKKO_AVATAR_DIR."$avatar")) ? $avatar : "default.png");

			return $avatar;
		}

		/**
		* mkUserLink(user_id);
		* @returns: HTML formatted link to user's profile.
		*/
		function mkUserLink($id) {

			$db =& $GLOBALS["db"];

			$q = $db->query("
			SELECT u.username, p.nickname
				FROM ".db_table("user")." u, ".db_table("user_profile")." p
				WHERE u.id = '{$id}' AND u.id = p.id
			UNION
				SELECT username, username
				FROM ".db_table("user")."
				WHERE id='{$id}'",
			__FILE__, __LINE__, true);

			if (($aff = $db->numRows($q)) > 0) {
				$row = $db->fetchRow($q);
				return mkLink("index.php/module=users/action=profile/user={$row["username"]}", $row["nickname"]);
			}

			return false;
		}

		function getName($id) {
			$db =& $GLOBALS["db"];

			$q = $db->query("SELECT username FROM ".db_table("user")." WHERE id = '$id'", __FILE__, __LINE__, true);

			if ($db->numRows($q)) {
				$row = $db->fetchRow($q);
				return $row["username"];
			} else
				return false;
		}

		function getID($username) {
			$db =& $GLOBALS["db"];

			$q = $db->query("SELECT id FROM ".db_table("user")." WHERE username = '$username'", __FILE__, __LINE__, true);

			if ($db->numRows($q)) {
				$row = $db->fetchRow($q);
				return $row["id"];
			} else
				return false;
		}

		function deleteRegistry($id) {
			$GLOBALS["db"]->query ("DELETE FROM ".db_table("user")." WHERE id='{$id}'",
			__FILE__, __LINE__, true);
			$GLOBALS["db"]->query ("DELETE FROM ".db_table("user_profile")." WHERE id='{$id}'",
			__FILE__, __LINE__, true);

			slog("users: deleted user with id=$id", "actions", 1);
		}

		function setRegistryStatus($id, $status) {
			return $GLOBALS["db"]->query ("UPDATE ".db_table("user")." SET status = '{$status}'
			WHERE id='{$id}'", __FILE__, __LINE__, true);

			slog("users: changed status for user with id=$id", "actions", 5);
		}
	}
?>
