Location: PHPKode > projects > pkZoos Website > includes/functions.php
<?php
	if(!defined('PK_MAGIC'))
		die('hack or what?');





	/* */

	function html_escape($s)
	{
		return htmlentities($s, ENT_QUOTES);
	}





	/* */

	function generate_password()
	{
		global $config;

        mt_srand((double)microtime() * 1000000);

        $length = strlen($config['password_chars']) - 1;

        $password = '';

        for($i = 0; $i < $config['password_length']; $i++)
        {
            $password .= substr($config['password_chars'], mt_rand(0, $length), 1);
        }

        return $password;
	}





	/* */

	function generate_activation_code()
	{
        mt_srand((double)microtime() * 1000000);

		$activation_code = md5(uniqid(mt_rand()));

        return $activation_code;
	}





	/* */

	function generate_sid()
	{
        mt_srand((double)microtime() * 1000000);

		$sid = md5(uniqid(mt_rand()));

        return $sid;
	}





	/* */

	function make_date($timestamp)
	{
		global $config;

		return date($config['date_format'], $timestamp);
	}





	/* */

	function base_url($host_only = false)
	{
		global $config;

		if($config['site_protocol'] == 'http://' && $config['site_port'] == 80)
		{
			$port = '';
		}
		else if($config['site_protocol'] == 'https://' && $config['site_port'] == 443)
		{
			$port = '';
		}
		else
		{
			$port = ':' . $config['site_port'];
		}

		$base_url = $config['site_protocol'] . $config['site_domain'] . $port . ($host_only ? '/' : $config['site_path']);

		return $base_url;
	}





	/* */

	function make_url($view, $params)
	{
		global $config;
		global $user;

		if($view == '')
		{
			$view = $config['view_default'];
		}

		$url = base_url() . 'index.php?view=' . urlencode($view);

		foreach($params as $key => $value)
		{
			if(strval($value) != '')
			{
				$url .= '&' . $key . '=' . urlencode($value);
			}
		}

		/* if(!$user['logged_in'] &&  if(!$session_cookie)
		{
			$url = append_sid($url);
		}*/

		return $url;
	}





	/* */

	function append_sid($url)
	{
		global $session_sid;

        if(!preg_match('#sid=#', $url))
        {
            $url .= ((strpos($url, '?') !== false) ? '&' : '?') . 'sid=' . urlencode($session_sid);
        }

        return $url;
	}





	/* */

	function redirect($url)
	{
		header('Location: ' . $url);
		exit;
	}





	/* */

	function check_newline($s)
	{
		if(substr($s, 0, 2) == "\r\n")
		{
			return "\r\n".$s;
		}

		if(substr($s, 0, 1) == "\n")
		{
			return "\n".$s;
		}

		return  $s;
	}





	/* */

	function check_int($s)
	{
		return preg_match('#^[0-9]+$#', $s);
	}





	/* */

	function check_sid($s)
	{
		return preg_match('#^[0-9a-f]{32}$#', $s);
	}





	/* */

	function encode_ip($s)
	{
		return $s;
	}





	/* */

	function decode_ip($s)
	{
		return $s;
	}





	/* */

	function get_online_users()
	{
		global $config, $db;

        $q = "
            SELECT COUNT(session_id) AS count
            FROM " . SESSIONS_TABLE . "
            WHERE
            	session_is_user  <> 0 AND
            	last_online_time >  " . (CURRENT_TIME - $config['session_online_time']) . "
        ";

        $q = "
            SELECT COUNT(user_id) AS count
            FROM " . USERS_TABLE . "
            WHERE last_online_time > " . (CURRENT_TIME - $config['session_online_time']) . "
        ";

        $session_res = $db->query($q);
        $session_row = $db->fetch_row($session_res);

        return $session_row['count'];
	}





	/* */

	function get_online_guests()
	{
		global $config, $db;

		return 0;

        $q = "
            SELECT COUNT(session_id) AS count
            FROM " . SESSIONS_TABLE . "
            WHERE
            	session_is_user  = 0 AND
            	last_online_time > " . (CURRENT_TIME - $config['session_online_time']) . "
        ";

        $session_res = $db->query($q);
        $session_row = $db->fetch_row($session_res);

        return $session_row['count'];
	}





	/* */

	function set_last_online_time($user_id, $timestamp)
	{
		global $user, $db;

		if($user['logged_in'])
		{
            $q = "
                UPDATE " . USERS_TABLE . "
                SET last_online_time = " . $timestamp . "
                WHERE user_id = " . $user_id . "
            ";

            $db->query($q);
        }
	}





	/* */

	function update_topic_views($topic_id)
	{
		global $db;

        $q = "
            UPDATE " . TOPICS_TABLE . "
            SET topic_views = topic_views + 1
            WHERE topic_id = " . $topic_id . "
        ";

        $db->query($q);
	}
?>
Return current item: pkZoos Website