mysql_query("INSERT INTO user (id) VALUES (NULL)", $SESS_DBH) or die("<p>".mysql_error());
if (mysql_affected_rows($SESS_DBH)) { # to check whether the above insert failed or not
$uid = mysql_insert_id($SESS_DBH);
session_register("uid");
} else {
displayMessage("Error",
"Unable to create new guest user",
"logfile");
$uid = 0;
}
return $uid;
}
function authenticate($email,$rule,$doctype="*",$action="*")
{
global $ADMINEMAIL;
if (eregi($ADMINEMAIL,"$email"))
return true;
$res = mysql_query("select id from rules where name='superuser'");
$row = mysql_fetch_row($res);
$id_superuser = $row[0];
$res = mysql_query("select id_user from user_rule where id_rule='${id_superuser}' and (param1='$doctype' or param1='*') and (param2 LIKE '$action' or param2='*')");
while ($row = mysql_fetch_row($res))
{
$iduser = $row[0];
$emailuser = getEmail($iduser);
if (eregi("$email","$emailuser"))
return true;
}
$res = mysql_query("select id from rules where name='$rule'");
if (mysql_num_rows($res) == 0)
return false;
else
{
$row = mysql_fetch_row($res);
$idrule = $row[0];
$res = mysql_query("select id_user from user_rule where id_rule='$idrule' and (param1 LIKE '$doctype' or param1='*') and (param2 LIKE '$action' or param2='*')");
while ($row = mysql_fetch_row($res))
{
$iduser = $row[0];
$emailuser = getEmail($iduser);
if (eregi("$email","$emailuser"))
return true;
}
return false;
}
}
function getRuleID($rule)
{
////////////////////////////
// get the id of the rule //
////////////////////////////
$res = mysql_query("
SELECT id
FROM rules
WHERE name='$rule'");
if (mysql_num_rows($res) == 0)
{
// if it does not exist, attempt to create it
$res = mysql_query("
INSERT
INTO rules (name,description)
VALUES ('$rule','')");
$idrule = mysql_insert_id();
}
else
{
$row = mysql_fetch_row($res);
$idrule = $row[0];
}
return $idrule;
}
function getNumberOfBaskets() {
global $SESS_DBH;
global $SESS_LIFE;
$count = 0;
$query = "SELECT COUNT(*) FROM shelf";
$result = mysql_perform_query($query, $SESS_DBH);
if(mysql_num_rows($result)) {
$row = mysql_fetch_row($result);
$count = $row[0];
}
return $count;
}
function getNumberOfRegisteredUsers() {
global $SESS_DBH;
global $SESS_LIFE;
$count = 0;
$query = "SELECT COUNT(*) FROM user WHERE email <> ''";
$result = mysql_perform_query($query, $SESS_DBH);
if(mysql_num_rows($result)) {
$row = mysql_fetch_row($result);
$count = $row[0];
}
return $count;
}
function getNumberOfAlerts() {
global $SESS_DBH;
global $SESS_LIFE;
$count = 0;
$query = "SELECT COUNT(*) FROM user_query_shelf";
$result = mysql_perform_query($query, $SESS_DBH);
if(mysql_num_rows($result)) {
$row = mysql_fetch_row($result);
$count = $row[0];
}
return $count;
}
## getNumberOfSessions():
function getNumberOfSessions($time_diff) {
# returns nb of active sessions that were created not longer than $time_diff seconds ago
global $SESS_DBH;
global $SESS_LIFE;
# 0) argument check:
if ($time_diff > $SESS_LIFE) {
# if desired $time_diff is greater than session lifetime, then garbage collector
# might have run and the result would not be meaningful => we need to set
# maximum $time_diff that is available, i.e. $SESS_LIFE
$time_diff = $SESS_LIFE;
}
# 1) find out the number of sessions:
$num_sessions = "unknown";
$since_when = time() + $SESS_LIFE - $time_diff;
$query = "SELECT COUNT(*) FROM session WHERE session_expiry > $since_when";
$res = mysql_query($query, $SESS_DBH);
if (mysql_num_rows($res)) {
if ($row = mysql_fetch_row($res)) {
$num_sessions = $row[0];
}
mysql_free_result($res);
}
# 2) find out the number of queries:
$num_queries = "unknown";
$query = "SELECT COUNT(*) FROM user_query WHERE date > DATE_SUB(NOW(), INTERVAL $time_diff SECOND);";
$res = mysql_query($query, $SESS_DBH);
if (mysql_num_rows($res)) {
if ($row = mysql_fetch_row($res)) {
$num_queries = $row[0];
}
mysql_free_result($res);
}
$out = "<small>There have been <strong>" . $num_sessions . "</strong> active users " .
"in the past " . sprintf("%d",$time_diff/60) . " minutes. ".
"They have made <strong>" . $num_queries . "</strong> queries.</small>";
return $out;
}
###
# function to call as an alternative to mysql_query
# the function stops the execution if the query couldn't be executed
# and prints an error message (HTML formatted) (default behaviour)
# if behaviour is set to 'continue', then the function just goes on
#
function mysql_perform_query($query, $link_identifier, $behaviour="die") {
if($behaviour == "continue")
$result = mysql_query($query, $link_identifier);
else {
$result = mysql_query($query, $link_identifier)
or die ("<p>MySQL: could not execute your query<br>$query" .
"<br>Contact the <a href=\"mailto:search.support@cds.cern.ch\">" .
"CDS Support Team</a>.<br>" .
"Error " . mysql_errno($link_identifier) .
": " . mysql_error($link_identifier) . ".</p>");
}
return $result;
}
## displayMessage():
function displayMessage($type, $msg, $where = "screen") {