Update of /cvsroot/php-blog/serendipity/include/db
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23806/include/db
Added Files:
Tag: branch-smarty
db.inc.php mysql.inc.php mysqli.inc.php postgres.inc.php
sqlite.inc.php
Log Message:
* renamed files as announced on the list. upgrader will take care of moving
old files to a 'backup' directory for upgrading (non-CVS) users.
* MFH'ed some parts
* Fixed quicksave button display
* Fixed footer
* Fixed entry preview properly
* Fixed printing categories where no categories appeard in entry view
--- NEW FILE: db.inc.php ---
<?php # $Id: db.inc.php,v 1.1.2.1 2004/11/06 11:22:50 garvinhicking Exp $
# Copyright (c) 2003-2004, Jannis Hermanns
# All rights reserved. See LICENSE file for licensing details
@define('S9Y_DB_INCLUDED', TRUE);
include_once(S9Y_INCLUDE_PATH . "include/db/{$serendipity['dbType']}.inc.php");
function serendipity_db_update($table, $keys, $values)
{
global $serendipity;
$set = '';
foreach ($values as $k => $v) {
if (strlen($set))
$set .= ', ';
$set .= $k . '=\'' . serendipity_db_escape_string($v) . '\'';
}
$where = '';
foreach ($keys as $k => $v) {
if (strlen($where))
$where .= ' AND ';
$where .= $k . '=\'' . serendipity_db_escape_string($v) . '\'';
}
if (strlen($where)) {
$where = " WHERE $where";
}
return serendipity_db_query("UPDATE {$serendipity['dbPrefix']}$table SET $set $where");
}
function serendipity_db_insert($table, $values)
{
global $serendipity;
$names = implode(',', array_keys($values));
$vals = '';
foreach ($values as $k => $v) {
if (strlen($vals))
$vals .= ', ';
$vals .= '\'' . serendipity_db_escape_string($v) . '\'';
}
return serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}$table ($names) values ($vals)");
}
function serendipity_db_bool ($val)
{
if(($val === true) || ($val == 'true') || ($val == 't') || ($val == '1'))
return true;
#elseif (($val === false || $val == 'false' || $val == 'f'))
else
return false;
}
/* vim: set sts=4 ts=4 expandtab : */
?>
--- NEW FILE: mysqli.inc.php ---
<?php # $Id: mysqli.inc.php,v 1.1.2.1 2004/11/06 11:22:50 garvinhicking Exp $
/* Issues a query to the underlying database;
* returns:
* false if there was an error,
* true if the query succeeded but did not generate any rows
* array of field values if it returned a single row and $single is true
* array of array of field values if it returned row(s)
*/
function serendipity_db_query($sql, $single = false, $result_type = "both", $reportErr=false, $assocKey=false, $assocVal=false) {
global $serendipity;
$type_map = array('assoc' => MYSQLI_ASSOC, 'num' => MYSQLI_NUM, 'both' => MYSQLI_BOTH);
$c = mysqli_query($serendipity['dbConn'], $sql);
if ( mysqli_error($serendipity['dbConn']) != '' ) {
return mysqli_error($serendipity['dbConn']);
}
if (!$c) {
if (!$serendipity['production']) {
print mysqli_error($serendipity['dbConn']);
if (function_exists('debug_backtrace') && $reportErr == true) {
highlight_string(var_export(debug_backtrace(), 1));
}
}
return false;
}
if ($c === true) {
return true;
}
$result_type = $type_map[$result_type];
switch(mysqli_num_rows($c)) {
case 0:
if ($single) {
return false;
}
return true;
case 1:
if ($single) {
return mysqli_fetch_array($c, $result_type);
}
default:
if ($single) {
return mysqli_fetch_array($c, $result_type);
}
$rows = array();
while ($row = mysqli_fetch_array($c, $result_type)) {
if (!empty($assocKey) && !empty($assocVal)) {
// You can fetch a key-associated array via the two function parameters assocKey and assocVal
$rows[$row[$assocKey]] = $row[$assocVal];
} else {
$rows[] = $row;
}
}
return $rows;
}
}
function serendipity_db_insert_id() {
global $serendipity;
return mysqli_insert_id($serendipity['dbConn']);
}
function serendipity_db_affected_rows() {
global $serendipity;
return mysqli_affected_rows($serendipity['dbConn']);
}
function serendipity_db_escape_string($string) {
global $serendipity;
return mysqli_escape_string($serendipity['dbConn'], $string);
}
function serendipity_db_limit($start, $offset) {
return $start . ', ' . $offset;
}
function serendipity_db_limit_sql($limitstring) {
return ' LIMIT ' . $limitstring;
}
function serendipity_db_connect() {
global $serendipity;
if (isset($serendipity['dbConn'])) {
return $serendipity['dbConn'];
}
$serendipity['dbConn'] = mysqli_connect($serendipity['dbHost'], $serendipity['dbUser'], $serendipity['dbPass']);
mysqli_select_db($serendipity['dbConn'], $serendipity['dbName']);
return $serendipity['dbConn'];
}
function serendipity_db_schema_import($query) {
static $search = array('{AUTOINCREMENT}', '{PRIMARY}',
'{UNSIGNED}', '{FULLTEXT}', '{FULLTEXT_MYSQL}', '{BOOLEAN}');
static $replace = array('int(11) not null auto_increment', 'primary key',
'unsigned' , 'FULLTEXT', 'FULLTEXT', 'enum (\'true\', \'false\') NOT NULL default \'true\'');
return serendipity_db_query(str_replace($search, $replace, $query));
}
/* probes the usability of the DB during installation */
function serendipity_db_probe($hash, &$errs) {
global $serendipity;
if (!function_exists('mysqli_connect')) {
$errs[] = 'No mySQLi extension found. Please check your webserver installation or contact your systems administrator regarding this problem.';
return false;
}
if (!($c = @mysqli_connect($hash['dbHost'], $hash['dbUser'], $hash['dbPass']))) {
$errs[] = 'Could not connect to database; check your settings.';
$errs[] = 'The mySQL error was: ' . mysqli_connect_error();
return false;
}
$serendipity['dbConn'] = $c;
if ( !@mysqli_select_db($c, $hash['dbName']) ) {
$errs[] = 'The database you specified does not exist.';
$errs[] = 'The mySQL error was: ' . mysqli_error($c);
return false;
}
return true;
}
function serendipity_db_concat($string) {
return 'concat(' . $string . ')';
}
/* vim: set sts=4 ts=4 expandtab : */
?>
--- NEW FILE: mysql.inc.php ---
<?php # $Id: mysql.inc.php,v 1.1.2.1 2004/11/06 11:22:50 garvinhicking Exp $
/* Issues a query to the underlying database;
* returns:
* false if there was an error,
* true if the query succeeded but did not generate any rows
* array of field values if it returned a single row and $single is true
* array of array of field values if it returned row(s)
*/
function serendipity_db_query($sql, $single = false, $result_type = "both", $reportErr=false, $assocKey=false, $assocVal=false) {
global $serendipity;
static $type_map = array(
'assoc' => MYSQL_ASSOC,
'num' => MYSQL_NUM,
'both' => MYSQL_BOTH
);
// highlight_string(var_export($sql, 1));
$c = mysql_query($sql, $serendipity['dbConn']);
if (mysql_error($serendipity['dbConn']) != '') {
return '<pre>' . $sql . '</pre> / ' . mysql_error($serendipity['dbConn']);
}
if (!$c) {
if (!$serendipity['production']) {
print '<pre>' . $sql . '</pre> / ' . mysql_error($serendipity['dbConn']);
if (function_exists('debug_backtrace') && $reportErr == true) {
highlight_string(var_export(debug_backtrace(), 1));
}
}
return false;
}
if ($c === true) {
return true;
}
$result_type = $type_map[$result_type];
switch(mysql_num_rows($c)) {
case 0:
if ($single) {
return false;
}
return true;
case 1:
if ($single) {
return mysql_fetch_array($c, $result_type);
}
default:
if ($single) {
return mysql_fetch_array($c, $result_type);
}
$rows = array();
while (($row = mysql_fetch_array($c, $result_type))) {
if (!empty($assocKey) && !empty($assocVal)) {
// You can fetch a key-associated array via the two function parameters assocKey and assocVal
$rows[$row[$assocKey]] = $row[$assocVal];
} else {
$rows[] = $row;
}
}
return $rows;
}
}
function serendipity_db_insert_id() {
global $serendipity;
return mysql_insert_id($serendipity['dbConn']);
}
function serendipity_db_affected_rows() {
global $serendipity;
return mysql_affected_rows($serendipity['dbConn']);
}
function serendipity_db_escape_string($string) {
return mysql_escape_string($string);
}
function serendipity_db_limit($start, $offset) {
return $start . ', ' . $offset;
}
function serendipity_db_limit_sql($limitstring) {
return ' LIMIT ' . $limitstring;
}
function serendipity_db_connect() {
global $serendipity;
if (isset($serendipity['dbConn'])) {
return $serendipity['dbConn'];
}
$serendipity['dbConn'] = mysql_connect($serendipity['dbHost'], $serendipity['dbUser'], $serendipity['dbPass']);
mysql_select_db($serendipity['dbName']);
return $serendipity['dbConn'];
}
function serendipity_db_schema_import($query) {
static $search = array('{AUTOINCREMENT}', '{PRIMARY}',
'{UNSIGNED}', '{FULLTEXT}', '{FULLTEXT_MYSQL}', '{BOOLEAN}');
static $replace = array('int(11) not null auto_increment', 'primary key',
'unsigned' , 'FULLTEXT', 'FULLTEXT', 'enum (\'true\', \'false\') NOT NULL default \'true\'');
return serendipity_db_query(str_replace($search, $replace, $query));
}
/* probes the usability of the DB during installation */
function serendipity_db_probe($hash, &$errs) {
global $serendipity;
if (!function_exists('mysql_connect')) {
$errs[] = 'No mySQL extension found. Please check your webserver installation or contact your systems administrator regarding this problem.';
return false;
}
if (!($c = @mysql_connect($hash['dbHost'], $hash['dbUser'], $hash['dbPass']))) {
$errs[] = 'Could not connect to database; check your settings.';
$errs[] = 'The mySQL error was: ' . mysql_error();
return false;
}
$serendipity['dbConn'] = $c;
if (!@mysql_select_db($hash['dbName'])) {
$errs[] = 'The database you specified does not exist.';
$errs[] = 'The mySQL error was: ' . mysql_error();
return false;
}
return true;
}
function serendipity_db_concat($string) {
return 'concat(' . $string . ')';
}
/* vim: set sts=4 ts=4 expandtab : */
?>
--- NEW FILE: postgres.inc.php ---
<?php # $Id: postgres.inc.php,v 1.1.2.1 2004/11/06 11:22:50 garvinhicking Exp $
function serendipity_db_connect() {
global $serendipity;
$serendipity['dbConn'] = pg_connect(
sprintf(
'%sdbname=%s user=%s password=%s',
strlen($serendipity['dbHost']) ? ('host=' . $serendipity['dbHost'] . ' ') : '',
$serendipity['dbName'],
$serendipity['dbUser'],
$serendipity['dbPass']
)
);
return $serendipity['dbConn'];
}
function serendipity_db_escape_string($string) {
return pg_escape_string($string);
}
function serendipity_db_limit($start, $offset) {
return $offset . ', ' . $start;
}
function serendipity_db_limit_sql($limitstring) {
$limit_split = split(',', $limitstring);
if (count($limit_split) > 1) {
$limit = ' LIMIT ' . $limit_split[0] . ' OFFSET ' . $limit_split[1];
} else {
$limit = ' LIMIT ' . $limit_split[0];
}
return $limit;
}
function serendipity_db_affected_rows() {
global $serendipity;
return pg_affected_rows($serendipity['dbLastResult']);
}
function serendipity_db_insert_id($table = '', $id = '') {
global $serendipity;
if (empty($table) || empty($id)) {
return pg_last_oid($serendipity['dbLastResult']);
} else {
$oid = pg_last_oid($serendipity['dbLastResult']);
$query = "SELECT $id FROM {$serendipity['dbPrefix']}$table WHERE oid=$oid";
$res = pg_query($serendipity['dbConn'], $query);
if (pg_num_rows($res)) {
$insert_id = pg_fetch_array($res, 0, PGSQL_ASSOC);
return $insert_id[$id];
} else {
return $oid;
}
}
}
function serendipity_db_query($sql, $single = false, $result_type = "both", $reportErr=false, $assocKey=false, $assocVal=false) {
global $serendipity;
static $type_map = array(
'assoc' => PGSQL_ASSOC,
'num' => PGSQL_NUM,
'both' => PGSQL_BOTH
);
if ($reportErr || !$serendipity['production']) {
$serendipity['dbLastResult'] = pg_query($serendipity['dbConn'], $sql);
} else {
$serendipity['dbLastResult'] = @pg_query($serendipity['dbConn'], $sql);
}
if (!$serendipity['dbLastResult']) {
if (!$serendipity['production']) {
print "Error in $sql<br/>\n";
print pg_last_error($serendipity['dbConn']) . "<BR/>\n";
if (function_exists('debug_backtrace')) {
highlight_string(var_export(debug_backtrace(), 1));
}
print "<br><code>$sql</code>\n";
}
return false;
}
if ($serendipity['dbLastResult'] === true) {
return true;
}
$result_type = $type_map[$result_type];
$n = pg_num_rows($serendipity['dbLastResult']);
switch ($n) {
case 0:
if ($single) {
return false;
}
return true;
case 1:
if ($single) {
return pg_fetch_array($serendipity['dbLastResult'], 0, $result_type);
}
default:
$rows = array();
for ($i = 0; $i < $n; $i++) {
if (!empty($assocKey) && !empty($assocVal)) {
// You can fetch a key-associated array via the two function parameters assocKey and assocVal
$row = pg_fetch_array($serendipity['dbLastResult'], $i, $result_type);
$rows[$row[$assocKey]] = $row[$assocVal];
} else {
$rows[] = pg_fetch_array($serendipity['dbLastResult'], $i, $result_type);
}
}
return $rows;
}
}
function serendipity_db_schema_import($query) {
static $search = array('{AUTOINCREMENT}', '{PRIMARY}', '{UNSIGNED}',
'{FULLTEXT}', '{BOOLEAN}', 'int(1)', 'int(10)', 'int(11)', 'int(4)');
static $replace = array('SERIAL', 'primary key', '', '', 'BOOLEAN NOT NULL', 'int2',
'int4', 'int4', 'int4');
if (stristr($query, '{FULLTEXT_MYSQL}')) {
return true;
}
return serendipity_db_query(str_replace($search, $replace, $query));
}
function serendipity_db_probe($hash, &$errs) {
global $serendipity;
if (!function_exists('pg_connect')) {
$errs[] = 'No PostgreSQL extension found. Please check your webserver installation or contact your systems administrator regarding this problem.';
return false;
}
$serendipity['dbConn'] = pg_connect(
sprintf(
'%sdbname=%s user=%s password=%s',
strlen($hash['dbHost']) ? ('host=' . $hash['dbHost'] . ' ') : '',
$hash['dbName'],
$hash['dbUser'],
$hash['dbPass']
)
);
if (!$serendipity['dbConn']) {
$errs[] = 'Could not connect to database; check your settings.';
return false;
}
return true;
}
function serendipity_db_concat($string) {
return 'concat_text(' . $string . ')';
}
/* vim: set sts=4 ts=4 expandtab : */
?>
--- NEW FILE: sqlite.inc.php ---
<?php # $Id: sqlite.inc.php,v 1.1.2.1 2004/11/06 11:22:50 garvinhicking Exp $
if (!function_exists('sqlite_open')) {
@dl('sqlite.so');
@dl('sqlite.dll');
}
function serendipity_db_connect()
{
global $serendipity;
if (isset($serendipity['dbConn'])) {
return $serendipity['dbConn'];
}
$serendipity['dbConn'] = sqlite_open($serendipity['serendipityPath'] . $serendipity['dbName'] . '.db');
return $serendipity['dbConn'];
}
function serendipity_db_escape_string($string)
{
static $search = array("\x00", '%', "'", '\"');
static $replace = array('%00', '%25', "''", '\\\"');
return str_replace($search, $replace, $string);
}
function serendipity_db_affected_rows()
{
global $serendipity;
return sqlite_changes($serendipity['dbConn']);
}
function serendipity_db_insert_id()
{
global $serendipity;
return sqlite_last_insert_rowid($serendipity['dbConn']);
}
function serendipity_db_sqlite_fetch_array($res, $type = SQLITE_BOTH)
{
static $search = array('%00', '%25');
static $replace = array("\x00", '%');
$row = sqlite_fetch_array($res, $type);
if (!is_array($row)) {
return $row;
}
/* strip any slashes, correct fieldname */
foreach ($row as $i => $v) {
// TODO: If a query of the format 'SELECT a.id, b.text FROM table' is used,
// the sqlite extension will give us key indizes 'a.id' and 'b.text'
// instead of just 'id' and 'text' like in mysql/postgresql extension.
// To fix that, we use a preg-regex; but that is quite performance costy.
// Either we always need to use 'SELECT a.id AS id, b.text AS text' in query,
// or the sqlite extension may get fixed. :-)
$row[preg_replace('@^.+\.(.*)@', '\1', $i)] = str_replace($search, $replace, $v);
}
return $row;
}
function serendipity_db_query($sql, $single = false, $result_type = "both", $reportErr=true, $assocKey=false, $assocVal=false)
{
global $serendipity;
static $type_map = array(
'assoc' => SQLITE_ASSOC,
'num' => SQLITE_NUM,
'both' => SQLITE_BOTH
);
static $debug = false;
if ($debug) $fp = @fopen('sqlite.log', 'a');
if ($reportErr) {
$res = sqlite_query($sql, $serendipity['dbConn']);
} else {
$res = @sqlite_query($sql, $serendipity['dbConn']);
}
if (!$res) {
if (!$serendipity['production']) {
var_dump($res);
var_dump($sql);
return "problem with query";
}
if ($debug) fwrite($fp, '[' . date('d.m.Y H:i') . '] [ERROR] SQLITE QUERY: ' . $sql . "\n\n");
return false;
} elseif ($debug) {
fwrite($fp, '[' . date('d.m.Y H:i') . '] SQLITE QUERY: ' . $sql . "\n\n");
}
if ($res === true) {
return true;
}
if (sqlite_num_rows($res) == 0) {
if ($single) {
return false;
}
return true;
} else {
$rows = array();
while (($row = serendipity_db_sqlite_fetch_array($res, $type_map[$result_type]))) {
if (!empty($assocKey) && !empty($assocVal)) {
// You can fetch a key-associated array via the two function parameters assocKey and assocVal
$rows[$row[$assocKey]] = $row[$assocVal];
} else {
$rows[] = $row;
}
}
if ($debug) fwrite($fp, '[' . date('d.m.Y H:i') . '] SQLITE RESULT: ' . print_r($rows, true). "\n\n");
if ($debug) fclose($fp);
if ($single && count($rows) == 1) {
return $rows[0];
}
return $rows;
}
}
function serendipity_db_probe($hash, &$errs)
{
global $serendipity;
$dbName = (isset($hash['sqlitedbName']) ? $hash['sqlitedbName'] : $hash['dbName']);
if (!function_exists('sqlite_open')) {
$errs[] = 'SQLite extension not installed. Run "pear install sqlite" on your webserver or contact your systems administrator regarding this problem.';
return false;
}
$serendipity['dbConn'] = sqlite_open($serendipity['serendipityPath'] . $dbName . '.db');
if ($serendipity['dbConn']) {
return true;
}
$errs[] = "Unable to open \"{$serendipity['serendipityPath']}$dbName.db\" - check permissions (directory needs to be writeable for webserver)!";
return false;
}
function serendipity_db_schema_import($query)
{
static $search = array('{AUTOINCREMENT}', '{PRIMARY}', '{UNSIGNED}', '{FULLTEXT}', '{BOOLEAN}');
static $replace = array('INTEGER', 'PRIMARY KEY', '', '', 'BOOLEAN NOT NULL');
if (stristr($query, '{FULLTEXT_MYSQL}')) {
return true;
}
return serendipity_db_query(str_replace($search, $replace, $query));
}
function serendipity_db_limit($start, $offset) {
return $start . ', ' . $offset;
}
function serendipity_db_limit_sql($limitstring) {
return ' LIMIT ' . $limitstring;
}
function serendipity_db_concat($string) {
return 'concat(' . $string . ')';
}
/* vim: set sts=4 ts=4 expandtab : */
?>
|