From d230e857f4790a47ac524b0e75653c2cbdc4f13c Mon Sep 17 00:00:00 2001 From: Jenga Phoenix Date: Wed, 13 Feb 2019 20:43:32 -0600 Subject: [PATCH] I've already forgotten. --- about.php | 23 ++- admin/index.php | 29 ++++ admin/private/user_create.php | 44 ++++++ composer.json | 3 +- config.php.sample | 14 ++ docs/database_spec.txt | 13 +- index.php | 28 +++- post.php | 34 ++++- res/lib/array.php | 30 ++++ res/lib/db.php | 133 ++++++++++++++++++ res/lib/error.php | 28 ++++ res/lib/load.php | 17 +++ res/lib/sterilize.php | 16 +++ res/lib/string.php | 28 ++++ res/lib/user.php | 105 ++++++++++++++ res/themes/default/html/admin_index.twig.html | 37 +++++ res/themes/default/html/error.twig.html | 3 + res/themes/default/html/foot.twig.html | 2 + res/themes/default/html/head.twig.html | 11 ++ res/themes/default/html/index.twig.html | 10 -- res/themes/default/html/post.twig.html | 4 +- res/themes/default/html/user.twig.html | 20 +-- user.php | 23 ++- 23 files changed, 621 insertions(+), 34 deletions(-) create mode 100644 admin/index.php create mode 100644 admin/private/user_create.php create mode 100644 config.php.sample create mode 100644 res/lib/array.php create mode 100644 res/lib/db.php create mode 100644 res/lib/error.php create mode 100644 res/lib/sterilize.php create mode 100644 res/lib/string.php create mode 100644 res/lib/user.php create mode 100644 res/themes/default/html/admin_index.twig.html create mode 100644 res/themes/default/html/error.twig.html create mode 100644 res/themes/default/html/foot.twig.html create mode 100644 res/themes/default/html/head.twig.html diff --git a/about.php b/about.php index cb39ec0..558cbbf 100644 --- a/about.php +++ b/about.php @@ -1,7 +1,28 @@ render('about.twig.html', []); + +echo $GLOBALS['twig']->render('head.twig.html', + ['theme' => $GLOBALS['theme'], + 'depth' => $depth, + 'title' =>$title]); + +echo $GLOBALS['twig']->render('index.twig.html', + ['animal'=> "cat"]); + +echo $GLOBALS['twig']->render('foot.twig.html', + ['theme' => $GLOBALS['theme'], + 'depth' => $depth]); ?> diff --git a/admin/index.php b/admin/index.php new file mode 100644 index 0000000..c32d03f --- /dev/null +++ b/admin/index.php @@ -0,0 +1,29 @@ +render('head.twig.html', + ['theme' => $GLOBALS['theme'], + 'depth' => $depth, + 'title' =>$title]); + + +echo $GLOBALS['twig']->render('admin_index.twig.html', []); + +echo $GLOBALS['twig']->render('foot.twig.html', + ['theme' => $GLOBALS['theme'], + 'depth' => $depth]); + + + +?> diff --git a/admin/private/user_create.php b/admin/private/user_create.php new file mode 100644 index 0000000..4a0f5f2 --- /dev/null +++ b/admin/private/user_create.php @@ -0,0 +1,44 @@ +Something went wrong."); +} + +?> diff --git a/composer.json b/composer.json index 1698897..df2d1df 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { "require": { - "twig/twig": "1.*" + "twig/twig": "1.*", + "falseclock/dbd-php": "1.*" } } diff --git a/config.php.sample b/config.php.sample new file mode 100644 index 0000000..ea779f2 --- /dev/null +++ b/config.php.sample @@ -0,0 +1,14 @@ + diff --git a/docs/database_spec.txt b/docs/database_spec.txt index c862314..ccd9faa 100644 --- a/docs/database_spec.txt +++ b/docs/database_spec.txt @@ -2,10 +2,7 @@ DATABASE SPEC =============================================================================== -We use `dbi4php` (/lib/ext/dbi4php/) for database access, so that we don't -have to rely on a specific DB. - -Anything supported by DBI (MySQL, MS SQL, Oracles, PostgreSQL, ODBC, Interbase, +Anything supported by DBO (MySQL, MS SQL, Oracles, PostgreSQL, ODBC, Interbase, SQLite, IBM DB2) will work just fine. The database name will be fetched from the "config.ini" file-- and a table @@ -16,9 +13,11 @@ The tables used by Blagoblag are: posts (title varchar(200), date datetime, author smallint, text longtext) - authors (id smallint primary key, username varchar(20), - password_hash varchar(30), full_name varchar(50)) + users (id smallint primary key, username varchar(20), + biography longtext, email varchar(50), + website varchar(50), password_hash varchar(80), + full_name varchar(50), login varchar(20)) comments (id smallint primary key, displayname varchar(20), - text shorttext, date datetime) + text text, date datetime) diff --git a/index.php b/index.php index b464ade..84959d0 100644 --- a/index.php +++ b/index.php @@ -1,7 +1,33 @@ render('index.twig.html', ['animal' => 'cat girl']); +// global variable declaration +$users = user_ids(); +$user = array_map(user_data, $users); +$posts = post_ids(); +$post = array_map(post_data, $posts); + +echo $GLOBALS['twig']->render('head.twig.html', + ['theme' => $GLOBALS['theme'], + 'depth' => $depth, + 'title' =>$title]); + +echo $GLOBALS['twig']->render('index.twig.html', []); + + +echo $GLOBALS['twig']->render('foot.twig.html', + ['theme' => $GLOBALS['theme'], + 'depth' => $depth]); ?> diff --git a/post.php b/post.php index f7484bc..9d33964 100644 --- a/post.php +++ b/post.php @@ -1,7 +1,39 @@ render('post.twig.html', []); +// ------------------------------------- +// post environment +$post_id = $_GET['id']; +$text = post_text($post_id); +$author = post_author($post_id); +$date = post_data($post_id); +// ------------------------------------- + +echo $GLOBALS['twig']->render('head.twig.html', + ['theme' => $GLOBALS['theme'], + 'depth' => $depth, + 'title' =>$title]); + +echo $GLOBALS['twig']->render('post.twig.html', + ['id'=> $post_id, + 'text' => $text, + 'author' => $author, + 'date' => $date]); + + +echo $GLOBALS['twig']->render('foot.twig.html', + ['theme' => $GLOBALS['theme'], + 'depth' => $depth]); ?> diff --git a/res/lib/array.php b/res/lib/array.php new file mode 100644 index 0000000..4c1b091 --- /dev/null +++ b/res/lib/array.php @@ -0,0 +1,30 @@ + STRING +// Turn a 1D array into a comma-seperated string +function comma_sep($array) { + global $stack; + $stack = ""; + + $comma_print = function($item) { + $GLOBALS['stack'] = $GLOBALS['stack'] + . ", " . $item; + }; + + array_map($comma_print, $array); + + $stack = preg_replace('/^, /', '', $stack); + + return $stack; +} + +?> diff --git a/res/lib/db.php b/res/lib/db.php new file mode 100644 index 0000000..5b580b7 --- /dev/null +++ b/res/lib/db.php @@ -0,0 +1,133 @@ + ARRAY +// Execute a DB command +function db_cmd($query) { + $stmt = $GLOBALS['pdo']->query($query); + if (is_bool($stmt)) { + return $stmt; + } else { + return $stmt->fetch(); + } +} + +// ------------------------------------- + +// STRING STRING --> ARRAY +// Return all values of a specific column +function db_get_columns($table, $column) { + return db_cmd("select " . $column . " from " . $table); +} + +// STRING STRING VARYING --> ARRAY +// Return all rows that have an 'identifier' column set to given value +function db_get_rows($table, $identifier, $value) { + return db_cmd("select * from " . $table . " where " . $identifier . + " = " . $value . ";"); +} + +// STRING STRING VARYING STRING --> ARRAY +// Return the value of a specific column in a given row, identified by an +// 'identifier' column set to the given value +function db_get_cell($table, $identifier, $value, $cell) { + return db_get_rows($table, $identifier, $value)[$cell]; +} + +// -------------------------------------- + +// STRING STRING VARYING STRING VARYING --> NIL +// Edit the value of a cell +function db_set_cell($table, $identifier, $value, $cell, $new_value) { + if (is_string($value)) { $value = "'" . $value . "'"; } + if (is_string($new_value)) { $new_value = "'" . $new_value . "'"; } + + return db_cmd("update " . $table + . " set " . $cell . " = " . $new_value + . " where " . $identifier . " = " . $value) . ";"; +} + +// ------------------------------------- + +// STRING ARRAY ARRAY --> BOOLEAN +// Create a table with given values to given columns. +// First array is a list of columns (as would be provided to SQL), and the +// second is the list of values (as would follow " values " in SQL) +function db_insert_row($table, $variables, $values) { + $variables = comma_sep($variables); + $values = comma_sep(strings_wrap($values)); + + + return db_cmd("insert into " . $table + . " (". $variables .")" + . " values (" + . $values . ")" . ";"); +} + +// ------------------------------------- + +// STRING --> BOOLEAN +// Return whether or not a table of given name exists +function db_table_existant($table) { + if (!db_cmd("select * from information_schema.tables " + . " where table_name = " . string_wrap($table) . ";")) { + return false; + } else { + return true; + } +} + +// STRING ARRAY --> BOOLEAN +// Create a table of given name and columns (with array of column-strings) +function db_create_table($table, $columns) { + return db_cmd("create table " . $table + . " (" . comma_sep($columns) . ");"); +} + +?> diff --git a/res/lib/error.php b/res/lib/error.php new file mode 100644 index 0000000..a039aff --- /dev/null +++ b/res/lib/error.php @@ -0,0 +1,28 @@ + NIL +// Print out a general error, with $string as it's error-message. +function general_error($string) { + echo $GLOBALS['twig']->render('error.twig.html', + ['error_message'=>$string]); + exit; +} + +// STRING --> NIL +// Print out an input error, with $string as it's error-message. +function input_error($string) { + echo $GLOBALS['twig']->render('error.twig.html', + ['error_message'=>$string]); + exit; +} + +?> diff --git a/res/lib/load.php b/res/lib/load.php index afdfccf..dd73af6 100644 --- a/res/lib/load.php +++ b/res/lib/load.php @@ -1,4 +1,13 @@ PATH // Take a path from root and turn it into a relative path. @@ -8,8 +17,16 @@ function root($path) { // ------------------------------------- +include(root("config.php")); require_once root("vendor/autoload.php"); +include(root("res/lib/string.php")); +include(root("res/lib/array.php")); +include(root("res/lib/user.php")); +include(root("res/lib/post.php")); +include(root("res/lib/comment.php")); +include(root("res/lib/db.php")); + $loader= new Twig_Loader_Filesystem(root("res/themes/default/html")); $twig = new Twig_Environment($loader, ['cache' => root('cache/')]); diff --git a/res/lib/sterilize.php b/res/lib/sterilize.php new file mode 100644 index 0000000..c22603e --- /dev/null +++ b/res/lib/sterilize.php @@ -0,0 +1,16 @@ + STRING +// If a given variable is a string, wrap it with apostrophes. +// Otherwise, just return it as given. +function string_wrap($value) { + if (is_string($value)) { + return "'" . $value . "'"; + } else { + return $value; + } +} + +// ARRAY --> ARRAY +// 'Wrap' all strings in array with apostrophes. +function strings_wrap($array) { + return array_map("string_wrap", $array); +} +?> diff --git a/res/lib/user.php b/res/lib/user.php new file mode 100644 index 0000000..c8bcba4 --- /dev/null +++ b/res/lib/user.php @@ -0,0 +1,105 @@ + VARYING +// Return the value of a given user's row +function user_get($id, $variable) { + return db_get_cell("lusers", "id", $id, $variable); +} + +// NUMBER STRING VARYING --> NIL +// Set the value of a given user's cell +function user_set($id, $variable, $new_value) { + return db_set_cell("lusers", "id", $id, $variable, $new_value); +} + +// ------------------------------------- + + +// NUMBER STRING STRING [STRING STRING STRING STRING STRING] --> BOOLEAN +// Create a user of the given specification. +function user_create($id, $name, $password, $login="Spectator", + $full_name=NULL, $email=NULL, $url=NULL, $bio=NULL) { + + return db_insert_row("lusers", + array("id", "username", "password_hash", "login", + "full_name", "email", "website", "biography"), + array($id, $name, $password, $login, + $full_name, $email, $url, $bio)); +} + + +// ------------------------------------- + + +// NUMBER --> STRING +// Get a user's username from ID. +function user_id_to_name($id) { + return user_get($id, "username"); +} + +// STRING --> NUMBER +// Get a user's ID from username. +function user_name_to_id($username) { + return db_get_cell("lusers", "username", $username, "id"); +} + + +// ------------------------------------- + + +// NUMBER --> STRING +// Return a username from a user-ID +function user_name($id) { + return user_id_to_name($id, "username"); +} + +// NUMBER --> STRING +// Return a user's login-class from ID +function user_login($id) { + return user_get($id, "login"); +} + +// NUMBER --> STRING +// Return a user's full name from ID +function user_full_name($id) { + return user_get($id, "full_name"); +} + +// NUMBER --> STRING +// Return a user's email from ID +function user_email($id) { + return user_get($id, "email"); +} + +// NUMBER --> STRING +// Return a user's website URL from ID +function user_website($id) { + return user_get($id, "website"); +} + +// NUMBER --> STRING +// Return a user's biography from ID +function user_biography($id) { + return user_get($id, "biography"); +} + +// ------------------------------------- + +// NUMBER --> ARRAY +// Fetch an array of a user's posts (by ID) +function user_posts($user_id) { + return db_get_cell("posts", "user", $user_id, "id"); +} + +?> diff --git a/res/themes/default/html/admin_index.twig.html b/res/themes/default/html/admin_index.twig.html new file mode 100644 index 0000000..69f1f91 --- /dev/null +++ b/res/themes/default/html/admin_index.twig.html @@ -0,0 +1,37 @@ +
+
+

+

+

+

+

+

+

+ +
+ +

+

+ +

+ +
+ +

+ +

+

+
+
+ +
+
+
+
+
+
diff --git a/res/themes/default/html/error.twig.html b/res/themes/default/html/error.twig.html new file mode 100644 index 0000000..5015ded --- /dev/null +++ b/res/themes/default/html/error.twig.html @@ -0,0 +1,3 @@ +

Eek!

+

Something was hecked.

+

{{ error_message }} Go back and try again.

diff --git a/res/themes/default/html/foot.twig.html b/res/themes/default/html/foot.twig.html new file mode 100644 index 0000000..308b1d0 --- /dev/null +++ b/res/themes/default/html/foot.twig.html @@ -0,0 +1,2 @@ + + diff --git a/res/themes/default/html/head.twig.html b/res/themes/default/html/head.twig.html new file mode 100644 index 0000000..2889f26 --- /dev/null +++ b/res/themes/default/html/head.twig.html @@ -0,0 +1,11 @@ + + + + + + Index + + + + diff --git a/res/themes/default/html/index.twig.html b/res/themes/default/html/index.twig.html index c806fd0..5935977 100644 --- a/res/themes/default/html/index.twig.html +++ b/res/themes/default/html/index.twig.html @@ -1,11 +1 @@ - - - - - - Index - -

{{ animal }}s are SUPER RAD.

- - - - + + Index diff --git a/res/themes/default/html/user.twig.html b/res/themes/default/html/user.twig.html index 3ccf9ae..81805b2 100644 --- a/res/themes/default/html/user.twig.html +++ b/res/themes/default/html/user.twig.html @@ -1,10 +1,10 @@ - - - - - - Index - - - -{{ full_name }} +

{{ username }}

+

{{ email }}

+

{{ url }}

+

{{ bio }}

+ diff --git a/user.php b/user.php index aa04787..558cbbf 100644 --- a/user.php +++ b/user.php @@ -1,7 +1,28 @@ render('user.twig.html', []); + +echo $GLOBALS['twig']->render('head.twig.html', + ['theme' => $GLOBALS['theme'], + 'depth' => $depth, + 'title' =>$title]); + +echo $GLOBALS['twig']->render('index.twig.html', + ['animal'=> "cat"]); + +echo $GLOBALS['twig']->render('foot.twig.html', + ['theme' => $GLOBALS['theme'], + 'depth' => $depth]); ?>