summaryrefslogtreecommitdiffstats
path: root/app/class/database.class.php
diff options
context:
space:
mode:
authorMalf Furious <m@lfurio.us>2018-10-27 18:28:22 -0400
committerMalf Furious <m@lfurio.us>2018-10-27 18:28:22 -0400
commit0421aa1b60f4fe6bf140888159c58059c1013588 (patch)
treec3285276f6c53b6789e2f6dc82cb3b0fd17b38a4 /app/class/database.class.php
parent495157341d60522084dcc9f6219877b6ba497312 (diff)
parent6512655aee73d3d295daa4de0e4ef25c08cfec9e (diff)
downloadscrott-0421aa1b60f4fe6bf140888159c58059c1013588.tar.gz
scrott-0421aa1b60f4fe6bf140888159c58059c1013588.zip
Merge branch 'rel/v0.1'v0.1
Diffstat (limited to '')
-rw-r--r--app/class/database.class.php168
1 files changed, 168 insertions, 0 deletions
diff --git a/app/class/database.class.php b/app/class/database.class.php
new file mode 100644
index 0000000..a2cab42
--- /dev/null
+++ b/app/class/database.class.php
@@ -0,0 +1,168 @@
+<?php
+
+/*
+ * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * For more information, please refer to UNLICENSE
+ */
+
+/* Include database configuration file if it exists */
+define("DATABASE_CONFIG_FILE", "dbconfig.php");
+is_file(DATABASE_CONFIG_FILE) && require_once DATABASE_CONFIG_FILE;
+
+require_once "class/globals.php";
+require_once "class/mysql.class.php";
+
+/*
+ * This class provides a common interface to various database drivers.
+ * Scrott provides facilities for interacting with any DBMS that we can
+ * use in PHP. To provide support for a new DBMS, extend this class and
+ * implement the abstract functions. Additionally, update the getInstance()
+ * function in this class so that the new implementation is used under
+ * appropriate configuration.
+ */
+abstract class database
+{
+ private static $instance = NULL;
+
+ /*
+ * Call this object's close function while being destructed
+ */
+ public function __destruct()
+ {
+ $this->_close();
+ }
+
+ /*
+ * Return the database instance object, creating it if this is the
+ * first call to this function. This function will need maintained
+ * as new DBMSs are supported. This function will throw if the
+ * database is not configured.
+ */
+ private static function getInstance() : database
+ {
+ global $_SCROTT;
+
+ if (self::$instance)
+ return self::$instance;
+
+ if (!isset($_SCROTT['conf']))
+ throw new Exception("Scrott database configuration is missing.");
+
+ switch ($_SCROTT['dbEngine'])
+ {
+ case "mysql":
+ $host = $_SCROTT['dbHost'];
+ $uname = $_SCROTT['dbUname'];
+ $passwd = $_SCROTT['dbPasswd'];
+ $dbname = $_SCROTT['dbName'];
+ self::$instance = new mysql($host, $uname, $passwd, $dbname);
+ break;
+
+ default:
+ throw new Exception("Problem with Scrott database configuration. Invalid " .
+ "database engine specified.");
+ break;
+ }
+
+ return self::$instance;
+ }
+
+ /*
+ * These functions are to be implemented by DBMS extensions,
+ * providing a uniform interface to database engines.
+ */
+ protected abstract function _close() : void;
+ protected abstract function _query(string $query) : array;
+ protected abstract function _esc(string $str) : string;
+
+ /*
+ * Perform a database query and return the results as an array
+ * of arrays.
+ */
+ public static function query(string $query) : array
+ {
+ return self::getInstance()->_query($query);
+ }
+
+ /*
+ * Escape a given string for use in a database query
+ */
+ public static function esc(string $str) : string
+ {
+ return self::getInstance()->_esc($str);
+ }
+
+ /*
+ * Check whether Scrott's database config is loaded
+ */
+ public static function checkConfig() : bool
+ {
+ global $_SCROTT;
+
+ if (!isset($_SCROTT['conf']))
+ return false;
+
+ $db = self::getInstance();
+ return true;
+ }
+
+ /*
+ * Test and set new database configuration parameters.
+ * If the given params fail, error's are set and this
+ * function returns false. On success, parameters are
+ * written to 'dbconfig.php' and true is returned.
+ */
+ public static function setConfig(string $engine, string $host,
+ string $uname, string $passwd, string $name) : bool
+ {
+ global $_SCROTT;
+
+ /* test configuration */
+ $_SCROTT['conf'] = "conf";
+ $_SCROTT['dbEngine'] = $engine;
+ $_SCROTT['dbHost'] = $host;
+ $_SCROTT['dbUname'] = $uname;
+ $_SCROTT['dbPasswd'] = $passwd;
+ $_SCROTT['dbName'] = $name;
+
+ try
+ {
+ $db = self::getInstance();
+ }
+ catch (Exception $e)
+ {
+ logError(ERROR, $e->getMessage());
+ return false;
+ }
+
+ /* write file */
+ $f = fopen(DATABASE_CONFIG_FILE, "w");
+
+ if (!$f)
+ {
+ logError(ERROR, "Can not create configuration file");
+ return false;
+ }
+
+ fwrite($f, "<?php\n");
+ fwrite($f, "\$_SCROTT['conf'] = 'conf';\n");
+ fwrite($f, "\$_SCROTT['dbEngine'] = '" . $engine . "';\n");
+ fwrite($f, "\$_SCROTT['dbHost'] = '" . $host . "';\n");
+ fwrite($f, "\$_SCROTT['dbUname'] = '" . $uname . "';\n");
+ fwrite($f, "\$_SCROTT['dbPasswd'] = '" . $passwd . "';\n");
+ fwrite($f, "\$_SCROTT['dbName'] = '" . $name . "';\n");
+ fwrite($f, "?>\n");
+
+ fclose($f);
+ return true;
+ }
+}
+
+?>