From 03b86c222f6ae25a6ce741f81a1007d8159e4567 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 2 Oct 2016 17:07:17 -0400 Subject: [PATCH] Start on SQLite database driver --- .gitignore | 1 + bootstrap.php | 4 +- locale/en.php | 14 ++++++ .../JKingWeb/NewsSync/Auth/DriverInternal.php | 1 - vendor/JKingWeb/NewsSync/Conf.php | 22 ++++------ vendor/JKingWeb/NewsSync/Database.php | 15 +++++++ .../JKingWeb/NewsSync/Db/DriverInterface.php | 7 +++ vendor/JKingWeb/NewsSync/Db/DriverSQLite3.php | 44 +++++++++++++++++++ vendor/JKingWeb/NewsSync/Db/Exception.php | 6 +++ vendor/JKingWeb/NewsSync/RuntimeData.php | 4 +- 10 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 vendor/JKingWeb/NewsSync/Database.php create mode 100644 vendor/JKingWeb/NewsSync/Db/DriverInterface.php create mode 100644 vendor/JKingWeb/NewsSync/Db/DriverSQLite3.php create mode 100644 vendor/JKingWeb/NewsSync/Db/Exception.php diff --git a/.gitignore b/.gitignore index bacb72d9..51fc87aa 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ vendor/simplepie/* #temp files cache/* test.php +newssync.db* # Windows image file caches Thumbs.db diff --git a/bootstrap.php b/bootstrap.php index 0168e834..df0e0235 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -5,6 +5,8 @@ namespace JKingWeb\NewsSync; const BASE = __DIR__.DIRECTORY_SEPARATOR; const NS_BASE = __NAMESPACE__."\\"; +if(!defined(NS_BASE."INSTALL")) define(NS_BASE."INSTALL", false); + spl_autoload_register(function ($class) { if($class=="SimplePie") return; $file = str_replace("\\", DIRECTORY_SEPARATOR, $class); @@ -14,4 +16,4 @@ spl_autoload_register(function ($class) { } }); -$conf = new Conf(); \ No newline at end of file +$data = new RuntimeData(new Conf()); \ No newline at end of file diff --git a/locale/en.php b/locale/en.php index 1a7e7dc7..06d2b5bf 100644 --- a/locale/en.php +++ b/locale/en.php @@ -5,4 +5,18 @@ return [ "Exception.JKingWeb/NewsSync/Lang/Exception.fileUnreadable" => "Insufficient permissions to read language file \"{0}\"", "Exception.JKingWeb/NewsSync/Lang/Exception.fileCorrupt" => "Language file \"{0}\" is corrupt or does not conform to expected format", "Exception.JKingWeb/NewsSync/Lang/Exception.stringMissing" => "Message string \"{msgID}\" missing from all loaded language files ({fileList})", + + "Exception.JKingWeb/NewsSync/Conf/Exception.fileMissing" => "Configuration file \"{0}\" does not exist", + "Exception.JKingWeb/NewsSync/Conf/Exception.fileUnreadable" => "Insufficient permissions to read configuration file \"{0}\"", + "Exception.JKingWeb/NewsSync/Conf/Exception.fileUncreatable" => "Insufficient permissions to write new configuration file \"{0}\"", + "Exception.JKingWeb/NewsSync/Conf/Exception.fileUnwritable" => "Insufficient permissions to overwrite configuration file \"{0}\"", + "Exception.JKingWeb/NewsSync/Conf/Exception.fileCorrupt" => "Configuration file \"{0}\" is corrupt or does not conform to expected format", + + "Exception.JKingWeb/NewsSync/Db/Exception.extMissing" => "Required PHP extension for driver \"{0}\" not installed", + "Exception.JKingWeb/NewsSync/Db/Exception.fileMissing" => "Database file \"{0}\" does not exist", + "Exception.JKingWeb/NewsSync/Db/Exception.fileUnreadable" => "Insufficient permissions to open database file \"{0}\" for reading", + "Exception.JKingWeb/NewsSync/Db/Exception.fileUnwritable" => "Insufficient permissions to open database file \"{0}\" for writing", + "Exception.JKingWeb/NewsSync/Db/Exception.fileUnusable" => "Insufficient permissions to open database file \"{0}\" for reading or writing", + "Exception.JKingWeb/NewsSync/Db/Exception.fileUncreatable" => "Insufficient permissions to create new database file \"{0}\"", + "Exception.JKingWeb/NewsSync/Db/Exception.fileCorrupt" => "Database file \"{0}\" is corrupt or not a valid database", ]; \ No newline at end of file diff --git a/vendor/JKingWeb/NewsSync/Auth/DriverInternal.php b/vendor/JKingWeb/NewsSync/Auth/DriverInternal.php index e1760ab0..62f06bec 100644 --- a/vendor/JKingWeb/NewsSync/Auth/DriverInternal.php +++ b/vendor/JKingWeb/NewsSync/Auth/DriverInternal.php @@ -1,7 +1,6 @@ import_file($import_file); + if($import_file != "") $this->importFile($import_file); } public function importFile(string $file): self { if(!file_exists($file)) throw new Conf\Exception("fileMissing"); if(!is_readable($file)) throw new Conf\Exception("fileUnreadable"); - $json = @file_get_contents($file); - if($json===false) throw new Conf\Exception("fileUnreadable"); - return $this->import($json); + $arr = (@include $file); + if(!is_array($arr)) throw new Conf\Exception("fileCorrupt"); + return $this->import($arr); } - public function import(string $json): self { - if($json=="") throw new Conf\Exception("blank"); - $json = json_decode($json, true); - if(!is_array($json)) throw new Conf\Exception("corrupt"); - foreach($json as $key => $value) { + public function import(array $arr): self { + foreach($arr as $key => $value) { $this->$$key = $value; } return $this; - } public function export(string $file = ""): string { - return json_encode($this, JSON_PRETTY_PRINT); + // TODO } public function __toString(): string { diff --git a/vendor/JKingWeb/NewsSync/Database.php b/vendor/JKingWeb/NewsSync/Database.php new file mode 100644 index 00000000..7b897ede --- /dev/null +++ b/vendor/JKingWeb/NewsSync/Database.php @@ -0,0 +1,15 @@ +dbClass; + $this->drv = new $driver($conf); + } + + static public function listDrivers() { + + } +} \ No newline at end of file diff --git a/vendor/JKingWeb/NewsSync/Db/DriverInterface.php b/vendor/JKingWeb/NewsSync/Db/DriverInterface.php new file mode 100644 index 00000000..27de3f90 --- /dev/null +++ b/vendor/JKingWeb/NewsSync/Db/DriverInterface.php @@ -0,0 +1,7 @@ +pdo = false; + } else if(class_exists("PDO") && in_array("sqlite",\PDO::getAvailableDrivers())) { + $this->pdo = true; + } else { + throw new Exception("extMissing", self::driverName()); + } + // if the file exists (or we're initializing the database), try to open it and set initial options + if((!$install && file_exists($conf->dbSQLite3File)) || $install) { + try { + $this->db = ($this->PDO) ? (new \SQLite3($conf->dbSQLite3File, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $conf->dbSQLite3Key)) : (new PDO("sqlite:".$conf->dbSQLite3File)); + //FIXME: add foreign key enforcement, WAL mode + } catch(\Throwable $e) { + // if opening the database doesn't work, check various pre-conditions to find out what the problem might be + foreach([$conf->dbSQLite3File, $conf->dbSQLite3File."-wal", $conf->dbSQLite3File."-shm"] as $file) { + if(!file_exists($file)) { + if($install && !is_writable(dirname($file))) throw new Exception("fileUncreatable", dirname($file)); + throw new Exception("fileMissing", $file); + } + if(!is_readable($file) && !is_writable($file)) throw new Exception("fileUnusable", $file); + if(!is_readable($file)) throw new Exception("fileUnreadable", $file); + if(!is_writable($file)) throw new Exception("fileUnwritable", $file); + } + // otherwise the database is probably corrupt + throw new Exception("fileCorrupt", $conf->dbSQLite3File); + } + } else { + throw new Exception("fileMissing", $conf->dbSQLite3File); + } + } + + static public function driverName(): string { + return "SQLite3"; + } +} \ No newline at end of file diff --git a/vendor/JKingWeb/NewsSync/Db/Exception.php b/vendor/JKingWeb/NewsSync/Db/Exception.php new file mode 100644 index 00000000..bf4dea10 --- /dev/null +++ b/vendor/JKingWeb/NewsSync/Db/Exception.php @@ -0,0 +1,6 @@ +conf = $conf; - //$this->db = new Database($this); + Lang::set($conf->lang); + $this->db = new Database($this->conf); //$this->auth = new Authenticator($this); } } \ No newline at end of file