Update of /cvsroot/php-blog/jBlog
In directory sc8-pr-cvs1:/tmp/cvs-serv23535
Modified Files:
db.sql htaccess.tpl jBlog_admin_installer.inc.php
jBlog_config_local.tpl jBlog_db_mysql.inc.php
jBlog_functions.inc.php
Added Files:
jBlog_db.inc.php jBlog_db_sqlite.inc.php
Log Message:
More db abstraction work.
Fixes a few buglets.
Provides theoretical support for sqlite.
We need a tool to generate the revise schema format;
please be careful if you need to alter the schema!
--- NEW FILE: jBlog_db.inc.php ---
<?php # $Id: jBlog_db.inc.php,v 1.1 2003/03/13 18:37:07 wez Exp $
include_once $jBlog['jBlogPath'] . "/jBlog_db_" . $jBlog['dbType'] . ".inc.php";
function jBlog_db_update($table, $keys, $values)
{
global $jBlog;
$set = "";
foreach ($values as $k => $v) {
if (strlen($set))
$set .= ", ";
$set .= "$k='" . jBlog_db_escape_string($v) . "'";
}
$where = "";
foreach ($keys as $k => $v) {
if (strlen($where))
$where .= " AND ";
$where .= "$k='" . jBlog_db_escape_string($v) . "'";
}
return jBlog_db_query("UPDATE $jBlog[dbPrefix]$table SET $set $where");
}
function jBlog_db_insert($table, $values)
{
global $jBlog;
$names = implode(",", array_keys($values));
$vals = "";
foreach ($values as $k => $v) {
if (strlen($vals))
$vals .= ", ";
$vals .= "$k='" . jBlog_db_escape_string($v) . "'";
}
return jBlog_db_query("INSERT INTO $jBlog[dbPrefix]$table ($names) values ($vals)");
}
?>
--- NEW FILE: jBlog_db_sqlite.inc.php ---
<?php
function jBlog_db_connect()
{
global $jBlog;
$jBlog['dbConn'] = sqlite_open($jBlog['jBlogPath'] . "sqlite.db");
}
function jBlog_db_escape_string($string)
{
static $replace = array('%00', '%25');
static $search = array("\x00", '%');
return addslashes(str_replace($search, $replace, $string));
}
function jBlog_db_affected_rows()
{
return sqlite_changes($GLOBALS['jBlog']['dbConn']);
}
function jBlog_db_insert_id()
{
return sqlite_last_insert_rowid($GLOBALS['jBlog']['dbConn']);
}
function jBlog_db_sqlite_fetch_array($res)
{
static $search = array('%00', '%25');
static $replace = array("\x00", '%');
$row = sqlite_fetch_array($res);
if (!is_array($row))
return $row;
/* strip any slashes */
$r = array();
foreach ($row as $i => $v) {
$r[$i] = str_replace($search, $replace, $v);
}
return $r;
}
function jBlog_db_query($sql, $single = false, $result_type = "both")
{
global $jBlog;
$res = sqlite_exec($sql, $jBlog['dbConn']);
if (!$res) {
return "problem with query";
}
if ($res === true) {
return true;
}
$rows = array();
if ($result_type == 'num') {
while (($row = jBlog_db_sqlite_fetch_array($res))) {
$rows[] = $row;
}
} else {
$field_names = jBlog_db_sqlite_fetch_array($res);
if ($result_type == 'both') {
while (($row = sqlite_fetch_array($res))) {
foreach ($field_names as $i => $name) {
$row[$name] = $row[$i];
}
$rows[] = $row;
}
} else {
/* string keys only */
while (($row = jBlog_db_sqlite_fetch_array($res))) {
$r = array();
foreach ($field_names as $i => $name) {
$r[$name] = $row[$i];
}
$rows[] = $r;
}
}
}
if ($single && count($rows) == 0) {
return false;
}
if ($single && count($rows) == 1) {
return $rows[0];
}
return $rows;
}
function jBlog_db_probe($hash, &$errs)
{
global $jBlog;
$jBlog['dbConn'] = sqlite_open($jBlog['jBlogPath'] . "sqlite.db");
if (is_resource($jBlog['dbConn'])) {
return true;
}
$errs[] = "Unable to open \"$jBlog[jBlogPath]sqlite.db\" - check permissions!";
return false;
}
function jBlog_db_schema_import($query)
{
static $search = array('{AUTOINCREMENT}');
static $replace = array('INTEGER PRIMARY KEY');
return jBlog_db_query(str_replace($search, $replace, $query));
}
?>
Index: db.sql
===================================================================
RCS file: /cvsroot/php-blog/jBlog/db.sql,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- db.sql 13 Mar 2003 03:35:59 -0000 1.17
+++ db.sql 13 Mar 2003 18:37:05 -0000 1.18
@@ -5,19 +5,18 @@
create table {PREFIX}authors (
username varchar(20) default null,
password varchar(20) default null,
- authorid int(11) not null auto_increment,
+ authorid {AUTOINCREMENT},
mail_comments int(1) default '1',
mail_trackbacks int(1) default '1',
- email varchar(128) not null default '',
- primary key (authorid)
-) type=myisam;
+ email varchar(128) not null default ''
+);
#
# table structure for table '{PREFIX}comments'
#
create table {PREFIX}comments (
- id int(10) unsigned not null auto_increment,
+ id {AUTOINCREMENT},
entry_id int(10) unsigned not null default '0',
timestamp int(10) unsigned default null,
title varchar(150) default null,
@@ -27,16 +26,15 @@
ip varchar(15) default null,
body text,
type varchar(100) default 'regular',
- primary key (id),
fulltext key body (body)
-) type=myisam;
+);
#
# table structure for table '{PREFIX}entries'
#
create table {PREFIX}entries (
- id int(10) unsigned not null auto_increment,
+ id {AUTOINCREMENT},
title varchar(200) default null,
timestamp int(10) unsigned default null,
body text,
@@ -46,21 +44,19 @@
author varchar(20) default null,
authorid int(11) default null,
categoryid int(11) default null,
- primary key (id),
fulltext key title (title,body,extended)
-) type=myisam;
+);
#
# table structure for table '{PREFIX}references'
#
create table {PREFIX}references (
- id int(10) unsigned not null auto_increment,
+ id {AUTOINCREMENT},
entry_id int(10) unsigned not null default '0',
link text,
- name text,
- primary key (id)
-) type=myisam;
+ name text
+);
#
# Table structure for table '{PREFIX}referrers'
@@ -71,18 +67,17 @@
day date NOT NULL default '0000-00-00',
count int(11) NOT NULL default '0',
PRIMARY KEY (url,day)
-) TYPE=MyISAM;
+);
#
# Table structure for table 'jBlog_css'
#
CREATE TABLE {PREFIX}css (
- cssid int(11) NOT NULL auto_increment,
+ cssid {AUTOINCREMENT},
name varchar(40) default NULL,
- data text,
- PRIMARY KEY (cssid)
-) TYPE=MyISAM;
+ data text
+);
create table {PREFIX}config (
name varchar(255) not null primary key,
@@ -94,7 +89,7 @@
ip varchar(15) default NULL,
last timestamp(14) NOT NULL,
unique key (url, ip)
-) TYPE=MyISAM;
+);
CREATE TABLE {PREFIX}plugins (
name varchar(128) not null,
@@ -104,9 +99,8 @@
);
CREATE TABLE {PREFIX}category (
- categoryid int(11) NOT NULL auto_increment,
+ categoryid {AUTOINCREMENT},
category_name varchar(255) default NULL,
category_description text,
- authorid int(11) default NULL,
- PRIMARY KEY (categoryid)
-) TYPE=MyISAM;
+ authorid int(11) default NULL
+);
Index: htaccess.tpl
===================================================================
RCS file: /cvsroot/php-blog/jBlog/htaccess.tpl,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- htaccess.tpl 5 Mar 2003 04:10:01 -0000 1.3
+++ htaccess.tpl 13 Mar 2003 18:37:06 -0000 1.4
@@ -1 +1,5 @@
ErrorDocument 404 {PREFIX}index.php
+
+<Files sqlite.db>
+ deny from all
+</Files>
Index: jBlog_admin_installer.inc.php
===================================================================
RCS file: /cvsroot/php-blog/jBlog/jBlog_admin_installer.inc.php,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- jBlog_admin_installer.inc.php 10 Mar 2003 22:25:51 -0000 1.11
+++ jBlog_admin_installer.inc.php 13 Mar 2003 18:37:06 -0000 1.12
@@ -149,17 +149,6 @@
case "check":
- // Probe database
- if (!@mysql_connect($_POST["dbHost"], $_POST["dbUser"], $_POST["dbPass"])) {
- $errs[] = "Could not connect to database, check your settings.";
- $errs[] = "The MySQL error was: " . mysql_error();
- }
- else {
- // Check that target database exists
-
- if (!@mysql_select_db($_POST["dbName"]))
- $errs[] = "The database you specified does not exist.";
- }
// Check dirs
if (!is_dir($_POST["jBlogPath"])) {
@@ -195,18 +184,28 @@
$errs[] = " -> and <i>chmod g+rwx ".$_POST["jBlogPath"]."plugins</i>";
}
+ $_POST['dbType'] = strtolower(basename($_POST['dbType']));
+ foreach ($_POST as $k => $v) {
+ if (substr($k, 0, 2) == 'db' || $k == 'jBlogPath') {
+ $jBlog[$k] = $v;
+ }
+ }
+ include_once $_POST['jBlogPath'] . "/jBlog_db_" . $_POST['dbType'] . ".inc.php";
+
+ // Probe database (do it after the dir stuff, as we need to be able to create the
+ // sqlite database)
+ jBlog_db_probe($_POST, $errs);
+
// Check imagick
if ($_POST["magick"] == "true" && !is_executable($_POST["convert"])) {
$errs[] = "Can't execute convert binary!";
}
// Something went wrong
- if (is_array($errs)) {
+ if (is_array($errs) && count($errs)) {
echo "<font color='#ff0000'>".implode("<br>", $errs)."</font><p>";
jBlog_printConfigTemplate(jBlog_parseTemplate("./jBlog_config_local.tpl"), $_POST);
- }
-
- else {
+ } else {
echo "Everything seems to be alright, saving config...<p>";
$f = implode("", file("./jBlog_config_local.tpl"));
$p = jBlog_parseTemplate("./jBlog_config_local.tpl");
@@ -250,11 +249,10 @@
// Create tables
echo "Attempting to setup Database...<br>";
- @mysql_select_db($_POST["dbName"]);
$queries = jBlog_parse_sql_tables('./db.sql');
$queries = str_replace("{PREFIX}", $_POST['dbPrefix'], $queries);
foreach ($queries as $query) {
- mysql_query($query);
+ jBlog_db_schema_import($query);
}
if($_POST['want_mail'] == true) {
$mail_comments = 1;
@@ -267,12 +265,13 @@
password('$_POST[pass]'),
'$_POST[email]',
$mail_comments) ";
- mysql_query($query);
+ jBlog_db_query($query);
- $text = mysql_escape_string(file_get_contents('./jBlog.css'));
+ $text = jBlog_db_escape_string(file_get_contents('./jBlog.css'));
$query = "INSERT into $_POST[dbPrefix]css values(NULL, 'default', '$text')";
- if (!mysql_query($query)) {
- echo mysql_error();
+ $res = jBlog_db_query($query);
+ if (is_string($res)) {
+ echo $res;
}
/* register default plugins */
Index: jBlog_config_local.tpl
===================================================================
RCS file: /cvsroot/php-blog/jBlog/jBlog_config_local.tpl,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- jBlog_config_local.tpl 10 Mar 2003 21:05:29 -0000 1.3
+++ jBlog_config_local.tpl 13 Mar 2003 18:37:07 -0000 1.4
@@ -4,6 +4,7 @@
* General configure options, adjust to your needs: *
*************************************************************************/
// Database Settings
+$jBlog["dbType"] = "{dbType|string|mysql}"; // Database type (mysql|sqlite)
$jBlog["dbHost"] = "{dbHost|string|localhost}"; // MySQL host
$jBlog["dbUser"] = "{dbUser|string|jBlog}"; // Username
$jBlog["dbPass"] = "{dbPass|string|jBlog}"; // Password
Index: jBlog_db_mysql.inc.php
===================================================================
RCS file: /cvsroot/php-blog/jBlog/jBlog_db_mysql.inc.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- jBlog_db_mysql.inc.php 13 Mar 2003 17:08:09 -0000 1.2
+++ jBlog_db_mysql.inc.php 13 Mar 2003 18:37:07 -0000 1.3
@@ -19,6 +19,7 @@
$c = mysql_db_query($jBlog['dbName'], $sql);
if (!$c) {
print mysql_error();
+ highlight_string(var_export(debug_backtrace(), 1));
return false;
}
if ($c === true) {
@@ -56,51 +57,47 @@
return mysql_affected_rows();
}
-function jBlog_db_update($table, $keys, $values)
+function jBlog_db_escape_string($string)
{
- global $jBlog;
-
- $set = "";
- foreach ($values as $k => $v) {
- if (strlen($set))
- $set .= ", ";
- $set .= "$k='" . jBlog_db_escape_string($v) . "'";
- }
- $where = "";
- foreach ($keys as $k => $v) {
- if (strlen($where))
- $where .= ", ";
- $where .= "$k='" . jBlog_db_escape_string($v) . "'";
- }
- return jBlog_db_query("UPDATE {$jBlog['dbPrefix']}$table SET $set $where");
+ return mysql_escape_string($string);
}
-function jBlog_db_insert($table, $values)
+function jBlog_db_connect()
{
global $jBlog;
- $names = implode(",", array_keys($values));
-
- $vals = "";
- foreach ($values as $k => $v) {
- if (strlen($vals))
- $vals .= ", ";
- $vals .= "$k='" . jBlog_db_escape_string($v) . "'";
- }
- return jBlog_db_query("INSERT INTO {$jBlog['dbPrefix']}$table ($names) values ($vals)");
+ $jBlog['dbConn'] = mysql_connect($jBlog['dbHost'], $jBlog['dbUser'], $jBlog['dbPass']);
+ return $jBlog['dbConn'];
}
-function jBlog_db_escape_string($string)
+function jBlog_db_schema_import($query)
{
- return mysql_escape_string($string);
+ static $search = array('{AUTOINCREMENT}');
+ static $replace = array('int(11) not null auto_increment primary key');
+
+ return jBlog_db_query(str_replace($search, $replace, $query));
}
-function jBlog_db_connect()
+/* probes the usability of the DB during installation */
+function jBlog_db_probe($hash, &$errs)
{
global $jBlog;
+
+ 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;
+ }
+
+ $jBlog['dbConn'] = $c;
- $jBlog['dbConn'] = mysql_connect($jBlog['dbHost'], $jBlog['dbUser'], $jBlog['dbPass']);
- return $jBlog['dbConn'];
+ 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;
}
?>
Index: jBlog_functions.inc.php
===================================================================
RCS file: /cvsroot/php-blog/jBlog/jBlog_functions.inc.php,v
retrieving revision 1.63
retrieving revision 1.64
diff -u -d -r1.63 -r1.64
--- jBlog_functions.inc.php 13 Mar 2003 18:18:51 -0000 1.63
+++ jBlog_functions.inc.php 13 Mar 2003 18:37:07 -0000 1.64
@@ -11,7 +11,7 @@
if (IN_JBLOG !== true) die ("Don't hack!");
-include_once "jBlog_db_mysql.inc.php";
+include_once "jBlog_db.inc.php";
$jBlog["imageList"] = array();
function jBlog_displayCommentForm($id) {
@@ -98,9 +98,11 @@
$querystring = "SELECT FROM_UNIXTIME(timestamp, '%e') as day, timestamp FROM ".$jBlog["dbPrefix"]."entries WHERE FROM_UNIXTIME(timestamp,'%c') LIKE $month AND FROM_UNIXTIME(timestamp,'%Y') LIKE $year";
$rows = jBlog_db_query($querystring);
$activeDays = array();
- foreach ($rows as $row) {
- $activeDays[$row["day"]] = $row["timestamp"];
- }
+ if (is_array($rows)) {
+ foreach ($rows as $row) {
+ $activeDays[$row["day"]] = $row["timestamp"];
+ }
+ }
// Prepare the table
?>
@@ -221,11 +223,10 @@
global $jBlog;
if ($authorid === null) {
- $authorid = $_SESSION['jBlogAuthorid'];
+ $authorid = $jBlog['authorid'];
}
- $querystring = "SELECT * FROM ".$jBlog['dbPrefix']."category
- WHERE authorid = $authorid";
+ $querystring = "SELECT * FROM ".$jBlog['dbPrefix']."category WHERE authorid = $authorid";
return jBlog_db_query($querystring);
}
@@ -240,7 +241,7 @@
$querystring = "SELECT id, author, timestamp, comments, title, body, extended FROM ".$jBlog["dbPrefix"]."entries
WHERE MATCH (title, extended, body) AGAINST ('".addslashes($term)."') ORDER BY timestamp DESC";
- return jBlog_db_query($query);
+ return jBlog_db_query($querystring);
}
@@ -1112,14 +1113,16 @@
$cats = jBlog_fetchCategories();
- $cat_list = "<select name=\"jBlog[categoryid]\">";
- foreach ($cats as $cat_data) {
- $selected = $cat_data['categoryid'] == $entry['categoryid'] ? " selected" : "";
- $cat_list .= "<option value=\"$cat_data[categoryid]\"$selected>" .
- htmlentities($cat_data['category_name']) .
- " - " .
- htmlentities($cat_data['category_description']) .
- "</option>\n";
+ $cat_list = "<select name=\"jBlog[categoryid]\"><option value=\"0\">[No Category]</option>\n";
+ if (is_array($cats)) {
+ foreach ($cats as $cat_data) {
+ $selected = $cat_data['categoryid'] == $entry['categoryid'] ? " selected" : "";
+ $cat_list .= "<option value=\"$cat_data[categoryid]\"$selected>" .
+ htmlentities($cat_data['category_name']) .
+ " - " .
+ htmlentities($cat_data['category_description']) .
+ "</option>\n";
+ }
}
$cat_list .= "</select>";
@@ -1342,8 +1345,8 @@
WHERE
username = '$username'
AND password = PASSWORD('$password')";
- $row = jBlog_db_query($query);
- if(is_string($row)) {
+ $row = jBlog_db_query($query, true);
+ if (is_string($row)) {
print $row;
}
if($row) {
|