2004-02-02 23:34:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once('../inc/util.inc');
|
|
|
|
|
|
|
|
// database-related functions.
|
|
|
|
// Presentation code (HTML) shouldn't be here
|
|
|
|
|
2007-03-14 18:05:30 +00:00
|
|
|
function db_init_aux($try_replica=false) {
|
2004-06-30 18:53:35 +00:00
|
|
|
$config = get_config();
|
|
|
|
$user = parse_config($config, "<db_user>");
|
|
|
|
$pass = parse_config($config, "<db_passwd>");
|
2007-03-14 18:05:30 +00:00
|
|
|
$host = null;
|
|
|
|
if ($try_replica == true) {
|
|
|
|
$host = parse_config($config, "<replica_db_host>");
|
|
|
|
}
|
|
|
|
if ($host == null) {
|
|
|
|
$host = parse_config($config, "<db_host>");
|
|
|
|
}
|
2004-06-30 18:53:35 +00:00
|
|
|
if ($host == null) {
|
|
|
|
$host = "localhost";
|
|
|
|
}
|
|
|
|
$retval = mysql_pconnect($host, $user, $pass);
|
2004-02-02 23:34:39 +00:00
|
|
|
if (!$retval) {
|
2004-09-14 20:45:17 +00:00
|
|
|
return 1;
|
2004-02-02 23:34:39 +00:00
|
|
|
}
|
2004-06-30 18:53:35 +00:00
|
|
|
$db_name = parse_config($config, "<db_name>");
|
2004-02-02 23:34:39 +00:00
|
|
|
if(!mysql_select_db($db_name)) {
|
2004-09-14 20:45:17 +00:00
|
|
|
return 2;
|
2004-02-02 23:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-09-21 21:56:10 +00:00
|
|
|
function lookup_user_auth($auth) {
|
|
|
|
$result = mysql_query("select * from user where authenticator='$auth'");
|
|
|
|
if ($result) {
|
|
|
|
$user = mysql_fetch_object($result);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2004-02-02 23:34:39 +00:00
|
|
|
function lookup_user_id($id) {
|
|
|
|
$result = mysql_query("select * from user where id=$id");
|
|
|
|
if ($result) {
|
|
|
|
$user = mysql_fetch_object($result);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2005-02-25 00:41:22 +00:00
|
|
|
function lookup_user_email_addr($email_addr) {
|
|
|
|
$result = mysql_query("select * from user where email_addr='$email_addr'");
|
|
|
|
if ($result) {
|
|
|
|
$user = mysql_fetch_object($result);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2007-04-29 14:22:28 +00:00
|
|
|
function lookup_user_name($name) {
|
|
|
|
$result = mysql_query("SELECT * FROM user WHERE name='".mysql_escape_string($name)."'");
|
|
|
|
if ($result) {
|
|
|
|
if (mysql_num_rows($result) == 1) {
|
|
|
|
return mysql_fetch_object($result);
|
|
|
|
} elseif (mysql_num_rows($result) == 0) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return -1; // Non-unique username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2004-02-02 23:34:39 +00:00
|
|
|
function lookup_host($id) {
|
|
|
|
$result = mysql_query("select * from host where id=$id");
|
|
|
|
if ($result) {
|
|
|
|
$host = mysql_fetch_object($result);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $host;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function lookup_team($id) {
|
|
|
|
$result = mysql_query("select * from team where id=$id");
|
|
|
|
if ($result) {
|
|
|
|
$team = mysql_fetch_object($result);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $team;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2006-10-30 09:03:52 +00:00
|
|
|
function lookup_team_founder($id) {
|
|
|
|
$result = mysql_query("select * from team where userid=$id");
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2004-02-02 23:34:39 +00:00
|
|
|
function lookup_wu($id) {
|
|
|
|
$result = mysql_query("select * from workunit where id=$id");
|
|
|
|
if ($result) {
|
|
|
|
$wu = mysql_fetch_object($result);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $wu;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2005-02-15 22:29:32 +00:00
|
|
|
function lookup_result($id) {
|
|
|
|
$result = mysql_query("select * from result where id=$id");
|
|
|
|
if ($result) {
|
|
|
|
$r = mysql_fetch_object($result);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2004-02-02 23:34:39 +00:00
|
|
|
function lookup_app($id) {
|
|
|
|
$result = mysql_query("select * from app where id=$id");
|
|
|
|
if ($result) {
|
|
|
|
$app = mysql_fetch_object($result);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $app;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2005-02-25 00:41:22 +00:00
|
|
|
function lookup_tentative_user($nonce) {
|
|
|
|
$result = mysql_query("select * from tentative_user where nonce='$nonce'");
|
|
|
|
if ($result) {
|
|
|
|
$tu = mysql_fetch_object($result);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $tu;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2004-12-27 03:42:11 +00:00
|
|
|
|
|
|
|
// apply this to any user-supplied strings used in queries
|
|
|
|
//
|
|
|
|
function boinc_real_escape_string($x) {
|
2005-02-23 00:42:14 +00:00
|
|
|
if (version_compare(phpversion(),"4.3.0")>=0) {
|
2004-12-27 03:42:11 +00:00
|
|
|
return mysql_real_escape_string($x);
|
2005-02-23 00:42:14 +00:00
|
|
|
} else {
|
|
|
|
$x = str_replace("'", "\'", $x);
|
|
|
|
$x = str_replace("\"", "\\\"", $x);
|
|
|
|
return $x;
|
|
|
|
}
|
2004-12-27 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
2005-02-22 19:37:55 +00:00
|
|
|
// Process user-supplied text prior to using in query;
|
|
|
|
// trims whitespace and escapes quotes.
|
|
|
|
// Does NOT remove HTML tags.
|
2004-12-27 03:42:11 +00:00
|
|
|
//
|
|
|
|
function process_user_text($value) {
|
|
|
|
$value = trim($value);
|
|
|
|
if (get_magic_quotes_gpc()) {
|
|
|
|
$value = stripslashes($value);
|
|
|
|
}
|
|
|
|
return boinc_real_escape_string($value);
|
|
|
|
}
|
|
|
|
|
2004-08-06 12:12:26 +00:00
|
|
|
// escape a string for MySQL "like"
|
|
|
|
//
|
|
|
|
function escape_pattern($str) {
|
|
|
|
$str = str_replace('_', '\\\\_', $str);
|
|
|
|
$str = str_replace('%', '\\\\%', $str);
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2004-02-02 23:34:39 +00:00
|
|
|
?>
|