2006-08-19 09:04:45 +02:00
|
|
|
<?php
|
2005-09-07 14:17:16 +02:00
|
|
|
|
|
|
|
require_once "config.php";
|
|
|
|
|
|
|
|
function db_connect($host, $user, $pass, $db) {
|
2011-08-12 00:51:00 +02:00
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
|
2010-08-24 12:12:04 +02:00
|
|
|
$string = "dbname=$db user=$user";
|
2011-08-12 00:51:00 +02:00
|
|
|
|
2010-08-24 12:12:04 +02:00
|
|
|
if ($pass) {
|
2011-08-12 00:51:00 +02:00
|
|
|
$string .= " password=$pass";
|
2010-08-24 12:12:04 +02:00
|
|
|
}
|
2011-08-12 00:51:00 +02:00
|
|
|
|
2005-09-14 04:41:44 +02:00
|
|
|
if ($host) {
|
2005-09-14 15:37:53 +02:00
|
|
|
$string .= " host=$host";
|
2005-09-14 04:41:44 +02:00
|
|
|
}
|
|
|
|
|
2006-03-21 11:38:41 +01:00
|
|
|
if (defined('DB_PORT')) {
|
|
|
|
$string = "$string port=" . DB_PORT;
|
|
|
|
}
|
|
|
|
|
2005-09-21 07:47:17 +02:00
|
|
|
$link = pg_connect($string);
|
|
|
|
|
|
|
|
if (!$link) {
|
2011-12-13 16:20:26 +01:00
|
|
|
die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
|
2005-09-21 07:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $link;
|
2005-09-07 14:17:16 +02:00
|
|
|
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-09-21 07:47:17 +02:00
|
|
|
$link = mysql_connect($host, $user, $pass);
|
2005-09-07 14:17:16 +02:00
|
|
|
if ($link) {
|
2011-08-12 00:51:00 +02:00
|
|
|
$result = mysql_select_db($db, $link);
|
2005-09-21 07:47:17 +02:00
|
|
|
if (!$result) {
|
|
|
|
die("Can't select DB: " . mysql_error($link));
|
2011-08-12 00:51:00 +02:00
|
|
|
}
|
2005-09-21 07:47:17 +02:00
|
|
|
return $link;
|
|
|
|
} else {
|
2011-12-13 16:20:26 +01:00
|
|
|
die("Unable to connect to database (as $user to $host, database $db): " . mysql_error());
|
2005-09-07 14:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-19 14:55:36 +01:00
|
|
|
function db_escape_string($s, $strip_tags = true) {
|
|
|
|
if ($strip_tags) $s = strip_tags($s);
|
|
|
|
|
2011-08-12 00:51:00 +02:00
|
|
|
if (DB_TYPE == "pgsql") {
|
2005-09-07 14:17:16 +02:00
|
|
|
return pg_escape_string($s);
|
|
|
|
} else {
|
2005-09-07 15:31:21 +02:00
|
|
|
return mysql_real_escape_string($s);
|
2005-09-07 14:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-13 06:46:40 +02:00
|
|
|
function db_query($link, $query, $die_on_error = true) {
|
2013-02-27 12:33:54 +01:00
|
|
|
if ($_REQUEST["qlog"] || defined('QUERY_LOG')) {
|
|
|
|
$query = trim($query);
|
|
|
|
error_log($_SESSION["uid"] . ":" . $_REQUEST["op"] . "/" . $_REQUEST["method"] .
|
|
|
|
" $query\n", 3, "/tmp/ttrss-query.log");
|
|
|
|
}
|
2011-11-11 09:26:54 +01:00
|
|
|
|
2005-09-07 14:17:16 +02:00
|
|
|
if (DB_TYPE == "pgsql") {
|
2005-09-21 07:47:17 +02:00
|
|
|
$result = pg_query($link, $query);
|
|
|
|
if (!$result) {
|
2005-10-16 10:52:44 +02:00
|
|
|
$query = htmlspecialchars($query); // just in case
|
2006-08-13 06:46:40 +02:00
|
|
|
if ($die_on_error) {
|
2011-12-13 11:49:11 +01:00
|
|
|
die("Query <i>$query</i> failed [$result]: " . ($link ? pg_last_error($link) : "No connection"));
|
2006-08-13 06:46:40 +02:00
|
|
|
}
|
2005-09-21 07:47:17 +02:00
|
|
|
}
|
|
|
|
return $result;
|
2005-09-07 14:17:16 +02:00
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-09-21 07:47:17 +02:00
|
|
|
$result = mysql_query($query, $link);
|
|
|
|
if (!$result) {
|
2005-10-16 10:52:44 +02:00
|
|
|
$query = htmlspecialchars($query);
|
2006-08-13 06:46:40 +02:00
|
|
|
if ($die_on_error) {
|
2011-12-13 11:49:11 +01:00
|
|
|
die("Query <i>$query</i> failed: " . ($link ? mysql_error($link) : "No connection"));
|
2006-08-13 06:46:40 +02:00
|
|
|
}
|
2005-09-21 07:47:17 +02:00
|
|
|
}
|
|
|
|
return $result;
|
2005-09-07 14:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function db_fetch_assoc($result) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_fetch_assoc($result);
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-09-21 07:47:17 +02:00
|
|
|
return mysql_fetch_assoc($result);
|
2005-09-07 14:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function db_num_rows($result) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
2005-09-07 15:31:21 +02:00
|
|
|
return pg_num_rows($result);
|
2005-09-07 14:17:16 +02:00
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-09-07 15:31:21 +02:00
|
|
|
return mysql_num_rows($result);
|
2005-09-07 14:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function db_fetch_result($result, $row, $param) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_fetch_result($result, $row, $param);
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-10-16 10:19:40 +02:00
|
|
|
// I hate incoherent naming of PHP functions
|
|
|
|
return mysql_result($result, $row, $param);
|
2005-09-07 14:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-16 16:48:33 +02:00
|
|
|
function db_unescape_string($str) {
|
|
|
|
$tmp = str_replace("\\\"", "\"", $str);
|
|
|
|
$tmp = str_replace("\\'", "'", $tmp);
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2005-09-07 14:17:16 +02:00
|
|
|
function db_close($link) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
|
|
|
|
return pg_close($link);
|
|
|
|
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
|
|
|
return mysql_close($link);
|
|
|
|
}
|
|
|
|
}
|
2005-10-17 05:45:44 +02:00
|
|
|
|
2006-05-16 14:37:35 +02:00
|
|
|
function db_affected_rows($link, $result) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_affected_rows($result);
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
|
|
|
return mysql_affected_rows($link);
|
|
|
|
}
|
2006-05-16 14:56:53 +02:00
|
|
|
}
|
2006-08-16 09:28:10 +02:00
|
|
|
|
|
|
|
function db_last_error($link) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_last_error($link);
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
|
|
|
return mysql_error($link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-12 00:51:00 +02:00
|
|
|
function db_quote($str){
|
|
|
|
return("'$str'");
|
|
|
|
}
|
|
|
|
|
2005-10-17 05:45:44 +02:00
|
|
|
?>
|