<?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://gekkoware.sourceforge.net
	*/

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

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

	function get_userInfo($id, $field = null) {
		$cache = "get_userInfo($id,$field)";
		if (cache::get($cache) === false) {
			$return = false;
			$q = $GLOBALS["db"]->query("SELECT ".($field ? $field : "*")." FROM ".db_table("user")." WHERE ID = '{$id}'", __FILE__, __LINE__, true);
			if ($GLOBALS["db"]->num_rows($q)) {
				$return = Array();
				$row = $GLOBALS["db"]->fetch_row($q);
				foreach ($row as $key => $val) {
					$return[$key] = $val;
				}

			}
			cache::set($cache, $return ? ($field ? $return[$field] : $return) : false);
		}
		return cache::get($cache);
	}
	function mkUserAvatar($id, $size = 50) {
		return "<img src=\"".($url = getAvatar(db_get_field("user_profile", $id, "Avatar"), db_get_field("user", $id, "Email")))."\" width=\"{$size}\" height=\"{$size}\" alt=\"".basename($url)."\" />";
	}
	function getAvatars() {
		$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;
	}

	function getAvatar($avatar, $email = false) {

		if (!$avatar) // gravatar support, as i've seen in jaws
			$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.REL_URL")."media/avatars/".(($avatar && file_exists(GEKKO_AVATAR_DIR."$avatar")) ? $avatar : "default.png");

		return $avatar;
	}

	function getUserGroups($id) {
		return db_get_field("user", $id, "Groups");
	/*
		$cache = "user[$id].groups";
		if (Cache::Get($cache) === false) {
			// gettings user groups
			$q = $GLOBALS["db"]->query("SELECT Groups FROM ".db_table("user")."
			WHERE ID = '{$id}'", __FILE__, __LINE__, true);
			if ($GLOBALS["db"]->num_rows($q)) {
				$row = $GLOBALS["db"]->fetch_row($q);
				$usergroups = $row["Groups"];
			} else {
				$usergroups = "";
			}
			Cache::Set($cache, $usergroups);
		}
		return Cache::Get($cache);
		*/
	}

	function getUserByID($id, $link = true) {
		if ($link) {
			$q = $GLOBALS["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", __FILE__, __LINE__, true);
			if ($GLOBALS["db"]->num_rows($q)) {
				// this user has a profile
				$row = $GLOBALS["db"]->fetch_row($q);
				saneHTML($row);
				return mkLink("index.php/users/profile/".strtolower($row["Username"])."", $row["Nickname"]);
			}
		}
		$username = db_get_field("user", $id, "Username");
		return $username;
	}

	function deleteUser($id) {
		$GLOBALS["db"]->query ("DELETE FROM ".db_table("user")." WHERE ID='".$id."'", __FILE__, __LINE__, 1);
		$GLOBALS["db"]->query ("DELETE FROM ".db_table("user_profile")." WHERE ID='".$id."'", __FILE__, __LINE__, 1);
		slog("Dropped user with ID: $id", "actions", 1);
	}
	function userSetStatus($id, $status) {
		return $GLOBALS["db"]->query ("UPDATE ".db_table("user")." SET Status = '".$status."' WHERE ID='".$id."'", __FILE__, __LINE__, 1);
		slog("Changed status for user with ID: $id", "actions", 1);
	}
?>