This repository has been archived on 2025-05-24. You can view files and clone it, but cannot push or open issues or pull requests.
Files
qemudb/include/user.php

426 lines
12 KiB
PHP
Raw Normal View History

<?php
/************************************/
/* user class and related functions */
/************************************/
2004-03-15 16:22:00 +00:00
/**
* User class for handling users
*/
class User {
var $iUserId;
var $sEmail;
var $sRealname;
var $sStamp;
var $sDateCreated;
var $sWineRelease;
2004-03-15 16:22:00 +00:00
/**
* Constructor.
* If $iUserId is provided, logs in user.
2004-03-15 16:22:00 +00:00
*/
function User($iUserId="")
2004-03-15 16:22:00 +00:00
{
if($iUserId)
{
$sQuery = "SELECT *
FROM user_list
WHERE userId = '".$iUserId."'";
$hResult = query_appdb($sQuery);
$oRow = mysql_fetch_object($hResult);
$this->iUserId = $oRow->userid;
$this->sEmail = $oRow->email;
$this->sRealname = $oRow->realname;
$this->sStamp = $oRow->stamp;
$this->sDateCreated = $oRow->created;
$this->sWineRelease = $oRow->CVSrelease;
}
return $this->isLoggedIn();
2004-03-15 16:22:00 +00:00
}
/**
* Logs in an user using e-mail and password.
2004-03-15 16:22:00 +00:00
*/
function login($sEmail, $sPassword)
2004-03-15 16:22:00 +00:00
{
$sQuery = "SELECT *
FROM user_list
WHERE email = '".$sEmail."'
AND password = password('".$sPassword."')";
$hResult = query_appdb($sQuery);
$oRow = mysql_fetch_object($hResult);
$this->iUserId = $oRow->userid;
$this->sEmail = $oRow->email;
$this->sRealname = $oRow->realname;
$this->sStamp = $oRow->stamp;
$this->sDateCreated = $oRow->created;
$this->sWineRelease = $oRow->CVSrelease;
if($this->isLoggedIn())
{
// Update timestamp
query_appdb("UPDATE user_list SET stamp=null WHERE userid=".$this->iUserId);
return true;
}
return false;
2004-03-15 16:22:00 +00:00
}
2004-03-15 16:22:00 +00:00
/*
* Creates a new user.
* returns true on success, false on failure
2004-03-15 16:22:00 +00:00
*/
function create($sEmail, $sPassword, $sRealname, $sWineRelease)
2004-03-15 16:22:00 +00:00
{
if(user_exists($sEmail))
{
addMsg("An account with this e-mail exists already.","red");
return false;
} else
{
2005-01-11 02:32:20 +00:00
$aInsert = compile_insert_string(array( 'realname' => $sRealname,
'email' => $sEmail,
'CVSrelease' => $sWineRelease ));
2005-01-08 18:38:29 +00:00
$sFields = "({$aInsert['FIELDS']}, `password`, `stamp`, `created`)";
2005-01-11 02:32:20 +00:00
$sValues = "({$aInsert['VALUES']}, password('".$sPassword."'), NOW(), NOW() )";
2005-01-08 18:38:29 +00:00
query_appdb("INSERT INTO user_list $sFields VALUES $sValues", "Error while creating a new user.");
return $this->login($sEmail, $sPassword);
}
2004-03-15 16:22:00 +00:00
}
/**
* Update User Account;
*/
function update($sEmail = null, $sPassword = null, $sRealname = null, $sWineRelease = null)
2004-03-15 16:22:00 +00:00
{
if(!$this->isLoggedIn()) return false;
if ($sEmail)
{
if(user_exists($sEmail) && $sEmail != $this->sEmail)
{
addMsg("An account with this e-mail exists already.","red");
return false;
}
if (!query_appdb("UPDATE user_list SET email = '".addslashes($sEmail)."' WHERE userid = ".$this->iUserId))
return false;
$this->sEmail = $sEmail;
}
if ($sPassword)
{
if (!query_appdb("UPDATE user_list SET password = password('$sPassword') WHERE userid = ".$this->iUserId))
return false;
}
if ($sRealname)
{
if (!query_appdb("UPDATE user_list SET realname = '".addslashes($sRealname)."' WHERE userid = ".$this->iUserId))
return false;
$this->sRealname = $sRealname;
}
if ($sWineRelease)
{
if (!query_appdb("UPDATE user_list SET CVSrelease = '".addslashes($sWineRelease)."' WHERE userid = ".$this->iUserId))
return false;
$this->sWineRelease = $sWineRelease;
}
return true;
2004-03-15 16:22:00 +00:00
}
/**
* Removes the current, or specified user and preferences from the database.
* returns true on success and false on failure.
2004-03-15 16:22:00 +00:00
*/
function delete()
2004-03-15 16:22:00 +00:00
{
if(!$this->isLoggedIn()) return false;
$hResult2 = query_appdb("DELETE FROM user_privs WHERE id = '".$this->iUserId."'");
$hResult3 = query_appdb("DELETE FROM user_prefs WHERE id = '".$this->iUserId."'");
return($hResult = query_appdb("DELETE FROM user_list WHERE id = '".$this->iUserId."'"));
2004-03-15 16:22:00 +00:00
}
/**
* Get a preference for the current user.
*/
function getPref($sKey, $sDef = null)
2004-03-15 16:22:00 +00:00
{
if(!$this->isLoggedIn() || !$sKey)
return $sDef;
2004-03-15 16:22:00 +00:00
$hResult = query_appdb("SELECT * FROM user_prefs WHERE userid = ".$this->iUserId." AND name = '$sKey'");
if(!$hResult || mysql_num_rows($hResult) == 0)
return $sDef;
$ob = mysql_fetch_object($hResult);
return $ob->value;
2004-03-15 16:22:00 +00:00
}
/**
* Set a preference for the current user.
*/
function setPref($sKey, $sValue)
2004-03-15 16:22:00 +00:00
{
if(!$this->isLoggedIn() || !$sKey || !$sValue)
return false;
2004-03-15 16:22:00 +00:00
$hResult = query_appdb("DELETE FROM user_prefs WHERE userid = ".$this->iUserId." AND name = '$sKey'");
$hResult = query_appdb("INSERT INTO user_prefs VALUES(".$this->iUserId.", '$sKey', '$sValue')");
return $hResult;
2004-03-15 16:22:00 +00:00
}
/**
* Check if this user has $priv.
2004-03-15 16:22:00 +00:00
*/
function hasPriv($sPriv)
2004-03-15 16:22:00 +00:00
{
if(!$this->isLoggedIn() || !$sPriv)
return false;
2004-03-15 16:22:00 +00:00
$hResult = query_appdb("SELECT * FROM user_privs WHERE userid = ".$this->iUserId." AND priv = '".$sPriv."'");
if(!$hResult)
return false;
return mysql_num_rows($hResult);
2004-03-15 16:22:00 +00:00
}
/**
* Check if this user is a maintainer of a given appId/versionId.
*/
function isMaintainer($iAppId=null, $iVersionId=null)
{
if(!$this->isLoggedIn()) return false;
/* if this user is a super maintainer of this appid then they */
/* are a maintainer of all of the versionId's of it as well */
if($this->isSuperMaintainer($iAppId))
{
return true;
}
if($iAppId && $iVersionId)
{
$sQuery = "SELECT * FROM appMaintainers WHERE userid = '".$this->iUserId."' AND appId = '".$iAppId."' AND versionId = '$iVersionId'";
2005-02-02 00:12:35 +00:00
} elseif($iAppId) // isMaintainer ? and we only provided appId => isSupermaintainer.
{
return $this->isSuperMaintainer($iAppId);
} else
{
$sQuery = "SELECT * FROM appMaintainers WHERE userid = '".$this->iUserId."'";
}
$hResult = query_appdb($sQuery);
if(!$hResult)
return false;
return mysql_num_rows($hResult);
}
/*
* Check if this user is a maintainer of a given appId/versionId.
*/
function isSuperMaintainer($iAppId=null)
{
if(!$this->isLoggedIn()) return false;
if($iAppId)
{
$sQuery = "SELECT * FROM appMaintainers WHERE userid = '$this->iUserId' AND appId = '$iAppId' AND superMaintainer = '1'";
} else
{
$sQuery = "SELECT * FROM appMaintainers WHERE userid = '$this->iUserId' AND superMaintainer = '1'";
}
$hResult = query_appdb($sQuery);
if(!$hResult)
return false;
return mysql_num_rows($hResult);
}
function addPriv($sPriv)
2004-03-15 16:22:00 +00:00
{
if(!$this->isLoggedIn() || !$sPriv)
return false;
2004-03-15 16:22:00 +00:00
if($this->hasPriv($sPriv))
return true;
2004-03-15 16:22:00 +00:00
$hResult = query_appdb("INSERT INTO user_privs VALUES ($this->iUserId, '$sPriv')");
return $hResult;
2004-03-15 16:22:00 +00:00
}
function delPriv($sPriv)
2004-03-15 16:22:00 +00:00
{
if(!$this->isLoggedIn() || !$sPriv)
return false;
2004-03-15 16:22:00 +00:00
$hRresult = query_appdb("DELETE FROM user_privs WHERE userid = $this->iUserId AND priv = '$sPriv'");
return $hRresult;
2004-03-15 16:22:00 +00:00
}
/**
* Checks if the current user is valid.
*/
function isLoggedIn()
{
return $this->iUserId;
}
2004-03-15 16:22:00 +00:00
/**
* Checks if user should see debugging infos.
*/
function showDebuggingInfos()
{
return (($this->isLoggedIn() && $this->getPref("debug") == "yes") || APPDB_DEBUG == 1);
}
/**
* Checks if user wants to get e-mails.
*/
function wantsEmail()
{
return ($this->isLoggedIn() && $this->getPref("send_email","yes")=="yes");
}
2004-03-15 16:22:00 +00:00
}
/*
* User functions that are not part of the class
*/
/**
* Creates a new random password.
*/
2004-03-15 16:22:00 +00:00
function generate_passwd($pass_len = 10)
{
$nps = "";
mt_srand ((double) microtime() * 1000000);
while (strlen($nps)<$pass_len)
{
$c = chr(mt_rand (0,255));
if (eregi("^[a-z0-9]$", $c)) $nps = $nps.$c;
}
return ($nps);
}
/**
* Get the email address of people to notify for this appId and versionId.
*/
function get_notify_email_address_list($iAppId = null, $iVersionId = null)
{
$aUserId = array();
$c = 0;
$retval = "";
/*
* Retrieve version maintainers.
*/
/*
* If versionId was supplied we fetch supermaintainers of application and maintainer of version.
*/
if($iVersionId)
{
$sQuery = "SELECT appMaintainers.userId
FROM appMaintainers, appVersion
WHERE appVersion.appId = appMaintainers.appId
AND appVersion.versionId = '".$iVersionId."'";
}
/*
* If versionId was not supplied we fetch supermaintainers of application and maintainer of all versions.
*/
elseif($iAppId)
{
$sQuery = "SELECT userId
FROM appMaintainers
WHERE appId = '".$iAppId."'";
}
$hResult = query_appdb($sQuery);
if(mysql_num_rows($hResult) > 0)
{
while($oRow = mysql_fetch_object($hResult))
{
$aUserId[$c] = array($oRow->userId);
$c++;
}
}
/*
* Retrieve administrators.
*/
$hResult = query_appdb("SELECT * FROM user_privs WHERE priv = 'admin'");
if(mysql_num_rows($hResult) > 0)
{
while($oRow = mysql_fetch_object($hResult))
{
$i = array_search($oRow->userid, $aUserId);
if ($aUserId[$i] != array($oRow->userid))
{
$aUserId[$c] = array($oRow->userid);
$c++;
}
}
}
if ($c > 0)
{
while(list($index, list($userIdValue)) = each($aUserId))
{
$oUser = new User($userIdValue);
if ($oUser->wantsEmail())
$retval .= $oUser->sEmail." ";
}
}
return $retval;
}
/**
* Get the number of users in the database
*/
function get_number_of_users()
{
$result = query_appdb("SELECT count(*) as num_users FROM user_list;");
$row = mysql_fetch_object($result);
return $row->num_users;
}
/**
* Get the number of active users within $days of the current day
*/
function get_active_users_within_days($days)
{
$result = query_appdb("SELECT count(*) as num_users FROM user_list WHERE stamp >= DATE_SUB(CURDATE(), interval $days day);");
$row = mysql_fetch_object($result);
return $row->num_users;
}
/**
* Check if a user exists.
2005-02-06 22:34:43 +00:00
* returns the userid if the user exists
*/
function user_exists($sEmail)
{
2005-02-06 22:34:43 +00:00
$result = query_appdb("SELECT userid FROM user_list WHERE email = '$sEmail'");
if(!$result || mysql_num_rows($result) != 1)
return 0;
2005-02-06 22:34:43 +00:00
else
{
$oRow = mysql_fetch_object($result);
return $oRow->userid;
}
}
2004-03-15 16:22:00 +00:00
?>