<?
/*---------------------------------------------------------*\
| ZitPG v0.04 |
| http://zoomit.vertigoproject.net |
+---------------------------------------------------------+
| Copyright©2004|2005 Aldemar Bernal |
| hide@address.com |
+---------------------------------------------------------+
| License |
| |
| This program is an open source project and you can use |
| or distribute it in your website but always keeping its |
| original copyright information and license. |
| |
| If you want to know more about this license, please |
| refer to the license.txt file or go to the official |
| LGPL Website http://www.gnu.org/licenses/lgpl.txt |
+---------------------------------------------------------+
| File: /misc.php |
| Description: Misc site functions. |
\*---------------------------------------------------------*/
//Checks the database connection
//Creates the database object
function check_database()
{
$i_ret = TRUE;
$GLOBALS["database_object"] = new db_wrapper($GLOBALS["database"], $GLOBALS["db_database"], FALSE, $GLOBALS["db_server"], $GLOBALS["db_username"], $GLOBALS["db_password"], $GLOBALS["db_port"], $GLOBALS["db_socket"]);
if($GLOBALS["database_object"]->connect() == FALSE)
{
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
$i_ret = FALSE;
}
return $i_ret;
}
//Register the user session
//Updates that the user stills online
function register_user_session()
{
$i_db_wrapper_result = NULL;
$i_user = NULL;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT * FROM user_sessions WHERE id_session = '" . session_id() . "'");
if($_SESSION["username"] != "")
$i_user = new user($_SESSION["username"]);
if($i_db_wrapper_result != FALSE)
{
if($i_db_wrapper_result->num_rows == 0)
{
if($_SESSION["username"] != "")
{
if($i_user->show_online == 1)
$GLOBALS["database_object"]->query("INSERT INTO user_sessions (id_session, username, last_time_online) VALUES ('" . session_id() . "', '" . $_SESSION["username"] . "', '" . date("Y-m-d H:i:s") . "')", "user_sessions", "id_session");
else
$GLOBALS["database_object"]->query("INSERT INTO user_sessions (id_session, username, last_time_online) VALUES ('" . session_id() . "', '', '" . date("Y-m-d H:i:s") . "')", "user_sessions", "id_session");
}
else
$GLOBALS["database_object"]->query("INSERT INTO user_sessions (id_session, username, last_time_online) VALUES ('" . session_id() . "', '" . $_SESSION["username"] . "', '" . date("Y-m-d H:i:s") . "')", "user_sessions", "id_session");
}
else
{
if($_SESSION["username"] != "")
{
if($i_user->show_online == 1)
$GLOBALS["database_object"]->query("UPDATE user_sessions SET username = '" . $_SESSION["username"] . "', last_time_online = '" . date("Y-m-d H:i:s") . "' WHERE id_session = '" . session_id() . "'");
else
$GLOBALS["database_object"]->query("UPDATE user_sessions SET username = '', last_time_online = '" . date("Y-m-d H:i:s") . "' WHERE id_session = '" . session_id() . "'");
}
else
$GLOBALS["database_object"]->query("UPDATE user_sessions SET username = '', last_time_online = '" . date("Y-m-d H:i:s") . "' WHERE id_session = '" . session_id() . "'");
}
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
}
//Gets sessions info
//How many users are online and who
//How many times the site has been visited
function get_sessions_info()
{
$i_db_wrapper_result = NULL;
$i_user_sessions = array();
$i_user = NULL;
//Consider user offline if the session has been inactive for more than 5 minutes
//The next query doesn't work using sqlite :S, I decided to wrote the next lines in orden to delete those rows
//$GLOBALS["database_object"]->query("DELETE FROM user_sessions WHERE (last_time_online + '00:05') < '" . date("Y-m-d H:i:s") . "')");
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT * FROM user_sessions");
if($i_db_wrapper_result != FALSE)
{
while($i_user_sessions = $i_db_wrapper_result->fetch_assoc())
{
if($i_user_sessions["max_users_online"] > $_SESSION["max_users_online"])
{
$_SESSION["max_users_online"] = $i_user_sessions["max_users_online"];
$_SESSION["time_max_users_online"] = $i_user_sessions["time_max_users_online"];
}
}
$i_db_wrapper_result->close();
}
//Get user sessions array
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT * FROM user_sessions WHERE last_time_online >= '" . date("Y-m-d H:i:s", strtotime('-5 minutes')) ."' ORDER BY username");
if($i_db_wrapper_result != FALSE)
{
while($i_user_sessions = $i_db_wrapper_result->fetch_assoc())
{
if($i_user_sessions["username"] != "")
{
$i_user = new user($i_user_sessions["username"]);
if($i_user->show_online == "1")
$GLOBALS["users_online"][] = $i_user->username;
else
$GLOBALS["number_anonymous_users"]++;
}
else
$GLOBALS["number_anonymous_users"]++;
if($i_user_sessions["hit_number"] > $GLOBALS["number_hits"])
$GLOBALS["number_hits"] = $i_user_sessions["hit_number"];
$i_user = NULL;
}
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
$GLOBALS["users_online"] = array_unique($GLOBALS["users_online"]);
if((count($GLOBALS["users_online"]) + $GLOBALS["number_anonymous_users"]) >= $_SESSION["max_users_online"])
{
$_SESSION["max_users_online"] = (count($GLOBALS["users_online"]) + $GLOBALS["number_anonymous_users"]);
$_SESSION["time_max_users_online"] = date("Y-m-d H:i:s");
if(!$GLOBALS["database_object"]->query("UPDATE user_sessions SET max_users_online = " . (count($GLOBALS["users_online"]) + $GLOBALS["number_anonymous_users"]) . ", time_max_users_online = '" . date("Y-m-d H:i:s") . "' WHERE last_time_online >= '" . date("Y-m-d H:i:s", strtotime('-5 minutes')) ."'"))
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
}
}
//Returns the total number of photos
function get_number_photos()
{
$i_ret = 0;
$i_db_wrapper_result = NULL;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT * FROM photos");
if($i_db_wrapper_result != FALSE)
{
$i_ret = $i_db_wrapper_result->num_rows;
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
//Returns the total number of albums
function get_number_albums()
{
$i_ret = 0;
$i_db_wrapper_result = NULL;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT id_album FROM albums");
if($i_db_wrapper_result != FALSE)
{
$i_ret = $i_db_wrapper_result->num_rows;
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
//Returns the total number of users
function get_number_users()
{
$i_ret = 0;
$i_db_wrapper_result = NULL;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT username FROM users");
if($i_db_wrapper_result != FALSE)
{
$i_ret = $i_db_wrapper_result->num_rows;
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
//Returns the last registered user
function get_last_registered_user()
{
$i_ret = "";
$i_user_result = array();
$i_db_wrapper_result = NULL;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT username FROM users ORDER BY member_since");
if($i_db_wrapper_result != FALSE)
{
if($i_db_wrapper_result->num_rows > 0)
{
$i_db_wrapper_result->data_seek($i_db_wrapper_result->num_rows - 1);
$i_user_result = $i_db_wrapper_result->fetch_assoc();
$i_ret = $i_user_result["username"];
}
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
//Returns get vars
function get_request_vars($prm_include_page = TRUE, $prm_return_page = FALSE)
{
$i_ret = "";
$i_get_key = "";
foreach(array_keys(array_unique($_GET)) as $i_get_key)
{
switch($i_get_key)
{
case "logout":
break;
case "page":
if($prm_include_page == TRUE)
$i_ret .= "&last_page=" . $_GET[$i_get_key];
break;
case "last_page":
if($prm_return_page == TRUE)
$i_ret .= "&page=" . $_GET[$i_get_key];
else
$i_ret .= "&last_page=" . $_GET[$i_get_key];
break;
default:
$i_ret .= "&" . $i_get_key . "=" . $_GET[$i_get_key];
break;
}
}
return $i_ret;
}
//Returns users whom birthday is today
//(obviously only those users whose birthday visible option is enabled in their profile)
function get_users_birthday_today()
{
$i_ret = array();
$i_user_result = array();
$i_db_wrapper_result = NULL;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT username, birthday FROM users WHERE show_birthday = 1 ORDER BY username");
if($i_db_wrapper_result != FALSE)
{
if($i_db_wrapper_result->num_rows > 0)
{
while($i_user_result = $i_db_wrapper_result->fetch_assoc())
{
if(date("m-d", strtotime($i_user_result["birthday"])) == date("m-d"))
$i_ret[] = $i_user_result["username"];
}
}
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
//Returns an array of news
function get_news($prm_offset = 0, $prm_max = 100000)
{
$i_ret = array();
$i_news_result = array();
$i_db_wrapper_result = NULL;
$i_offset = 0;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT id_news FROM news ORDER BY date DESC");
if($i_db_wrapper_result != FALSE)
{
while($i_news_result = $i_db_wrapper_result->fetch_assoc())
{
if($i_offset >= $prm_offset && $prm_max > 0)
{
$i_ret[] = new news($i_news_result["id_news"]);
$prm_max--;
}
$i_offset++;
}
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
//Returns an array of users
function get_users($prm_offset = 0, $prm_max = 100000)
{
$i_ret = array();
$i_user_result = array();
$i_db_wrapper_result = NULL;
$i_offset = 0;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT username FROM users");
if($i_db_wrapper_result != FALSE)
{
while($i_user_result = $i_db_wrapper_result->fetch_assoc())
{
if($i_offset >= $prm_offset && $prm_max > 0)
{
$i_ret[] = new user($i_user_result["username"]);
$prm_max--;
}
$i_offset++;
}
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
//Returns an array of the lastest photos published
function get_lastest_photos($prm_max)
{
$i_ret = array();
$i_photo_result = array();
$i_db_wrapper_result = NULL;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT id_photo FROM photos ORDER BY id_photo DESC");
if($i_db_wrapper_result != FALSE)
{
while($i_photo_result = $i_db_wrapper_result->fetch_assoc())
{
if($prm_max > 0)
{
$i_ret[] = new photo($i_photo_result["id_photo"]);
$prm_max--;
}
}
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
//Returns an array of the lastest albums published
function get_lastest_albums($prm_max)
{
$i_ret = array();
$i_album_result = array();
$i_db_wrapper_result = NULL;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT id_album FROM albums ORDER BY id_album DESC");
if($i_db_wrapper_result != FALSE)
{
while($i_album_result = $i_db_wrapper_result->fetch_assoc())
{
if($prm_max > 0)
{
$i_ret[] = new album($i_album_result["id_album"]);
$prm_max--;
}
}
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
//Returns an array of the top rated photos published
function get_top_rated_photos($prm_max)
{
$i_ret = array();
$i_photos = array();
$i_photo = NULL;
$i_photo_result = array();
$i_db_wrapper_result = NULL;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT id_photo FROM photos ORDER BY id_photo DESC");
if($i_db_wrapper_result != FALSE)
{
while($i_photo_result = $i_db_wrapper_result->fetch_assoc())
{
$i_photo = new photo($i_photo_result["id_photo"]);
$i_photos[$i_photo->id_photo] = $i_photo->get_photo_rating();
}
$i_db_wrapper_result->close();
arsort($i_photos);
reset($i_photos);
while((list($photo, $rating) = each($i_photos)) && ($prm_max > 0))
{
$i_ret[] = new photo($photo);
$prm_max--;
}
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
//Returns an array of the most viewed photos published
function get_most_viewed_photos($prm_max)
{
$i_ret = array();
$i_photo_result = array();
$i_db_wrapper_result = NULL;
$i_db_wrapper_result = $GLOBALS["database_object"]->query("SELECT id_photo FROM photos ORDER BY times_visited DESC");
if($i_db_wrapper_result != FALSE)
{
while($i_photo_result = $i_db_wrapper_result->fetch_assoc())
{
if($prm_max > 0)
{
$i_ret[] = new photo($i_photo_result["id_photo"]);
$prm_max--;
}
}
$i_db_wrapper_result->close();
}
else
echo "<br /><strong>Database Error:</strong> " . $GLOBALS["database_object"]->error;
return $i_ret;
}
?>