Archived
1
0
Disbranĉigi 0

I've already forgotten.

This commit is contained in:
Jenga Phoenix 2019-02-13 20:43:32 -06:00
parent e3527c7600
commit d230e857f4
23 changed files with 621 additions and 34 deletions

View File

@ -1,7 +1,28 @@
<?php <?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
$depth = ""; $depth = "";
$title = "About";
include "res/lib/load.php"; include "res/lib/load.php";
echo $GLOBALS['twig']->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]);
?> ?>

29
admin/index.php Normal file
View File

@ -0,0 +1,29 @@
<?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
$title = "Control Panel";
$depth = "../";
include "../res/lib/load.php";
echo $GLOBALS['twig']->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]);
?>

View File

@ -0,0 +1,44 @@
<?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
$depth = "../../";
include "../../res/lib/load.php";
$id = $_POST['id'];
$name = $_POST['name'];
$full_name = $_POST['full_name'];
$bio = $_POST['bio'];
$email = $_POST['email'];
$url = $_POST['url'];
$password = $_POST['password'];
$login = $_POST['login'];
$invalid = input_enforce(array($id, $name, $full_name, $bio, $email, $url,
$password, $login),
array("ID", "username", "full name", "biography", "email",
"url", "password", "login"),
array("int", "string", "string", "string", "email",
"url", "password",
array("contributor", "spectator", "wizard",
"admin")));
if (!is_bool($invalid)) {
input_error("Some input is invalid: " . $invalid);
}
$result = user_create($id, $name, $password, $login,
$full_name, $email, $url, $bio);
if ($result) {
header('Location: http://localhost/blagoblag/user.php?id=' . $id);
} else {
general_error("<em>Something</em> went wrong.");
}
?>

View File

@ -1,5 +1,6 @@
{ "require": { "require":
{ {
"twig/twig": "1.*" "twig/twig": "1.*",
"falseclock/dbd-php": "1.*"
} }
} }

14
config.php.sample Normal file
View File

@ -0,0 +1,14 @@
<?php
$db_type = "mysql";
$db_host = "host=localhost;";
$db_port = "port=3306;";
$db_socket = "socket=/var/run/mysql/mysql.sock;";
$db_args = array();
$db_name = "dbname=blagoblag;";
$db_user = "USERNAME";
$db_pass = "PASSWORD";
$theme = "default";
?>

View File

@ -2,10 +2,7 @@
DATABASE SPEC DATABASE SPEC
=============================================================================== ===============================================================================
We use `dbi4php` (/lib/ext/dbi4php/) for database access, so that we don't Anything supported by DBO (MySQL, MS SQL, Oracles, PostgreSQL, ODBC, Interbase,
have to rely on a specific DB.
Anything supported by DBI (MySQL, MS SQL, Oracles, PostgreSQL, ODBC, Interbase,
SQLite, IBM DB2) will work just fine. SQLite, IBM DB2) will work just fine.
The database name will be fetched from the "config.ini" file-- and a table 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, posts (title varchar(200), date datetime, author smallint,
text longtext) text longtext)
authors (id smallint primary key, username varchar(20), users (id smallint primary key, username varchar(20),
password_hash varchar(30), full_name varchar(50)) 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), comments (id smallint primary key, displayname varchar(20),
text shorttext, date datetime) text text, date datetime)

View File

@ -1,7 +1,33 @@
<?php <?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
$depth = ""; $depth = "";
$title = "";
include "res/lib/load.php"; include "res/lib/load.php";
echo $GLOBALS['twig']->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]);
?> ?>

View File

@ -1,7 +1,39 @@
<?php <?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
$depth = ""; $depth = "";
$title = "";
include "res/lib/load.php"; include "res/lib/load.php";
echo $GLOBALS['twig']->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]);
?> ?>

30
res/lib/array.php Normal file
View File

@ -0,0 +1,30 @@
<?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
// ARRAY -- > 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;
}
?>

133
res/lib/db.php Normal file
View File

@ -0,0 +1,133 @@
<?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
// -------------------------------------
// connection setup
// create the dsn from values in /config.php
$dsn = $GLOBALS['db_type'] . ":"
. $GLOBALS['db_host']
. $GLOBALS['db_port']
. $GLOBALS['db_socket']
. $GLOBALS['db_name']
. "charset=utf8mb4";
$pdo = (new PDO($dsn, $GLOBALS['db_user'], $GLOBALS['db_pass'],
$GLOBALS['db_args']));
// -------------------------------------
// table setup
// create needed tables, if nonexistant
if (!db_table_existant("lusers")) {
db_create_table("lusers",
array("id int primary key","username varchar(20)",
"biography longtext","email varchar(50)","website varchar(50)",
"password_hash varchar(80)","full_name varchar(50)",
"login varchar(20)")); }
if (!db_table_existant("posts")) {
db_create_table("posts",
array("id int primary key","title varchar(200)","date datetime",
"user int","text longtext")); }
if (!db_table_existant("comments")) {
db_create_table("comments",
array("id int primary key","displayname varchar(20)",
"user int","text longtext", "date datetime")); }
// -------------------------------------
// STRING --> 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) . ");");
}
?>

28
res/lib/error.php Normal file
View File

@ -0,0 +1,28 @@
<?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
// STRING --> 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;
}
?>

View File

@ -1,4 +1,13 @@
<?php <?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
// PATH --> PATH // PATH --> PATH
// Take a path from root and turn it into a relative 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"); 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")); $loader= new Twig_Loader_Filesystem(root("res/themes/default/html"));
$twig = new Twig_Environment($loader, ['cache' => $twig = new Twig_Environment($loader, ['cache' =>
root('cache/')]); root('cache/')]);

16
res/lib/sterilize.php Normal file
View File

@ -0,0 +1,16 @@
<?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
function input_enforce($values, $names, $types) {
$stack = "";
while ($i < length($valuesw)) {
if (function

28
res/lib/string.php Normal file
View File

@ -0,0 +1,28 @@
<?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
// VARYING --> 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);
}
?>

105
res/lib/user.php Normal file
View File

@ -0,0 +1,105 @@
<?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
// -------------------------------------
// NUMBER STRING --> 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");
}
?>

View File

@ -0,0 +1,37 @@
<section id="creation">
<form id="user_creation" action="private/user_create.php" method="post">
<p><label>User ID</label><input name="id" type="number" /></p>
<p><label>Full Name</label><input name="full_name" type="text"/>
</p>
<p><label>Biography</label><input name="bio" type="text" />
</p>
<p><label>E-mail</label><input name="email" type="email" /></p>
<p><label>Website</label><input name="url" type="url" /></p>
<hr />
<p><label>Username</label><input name="name" type="text" /></p>
<p><label>Password</label>
<input name="password" type="password" />
</p>
<hr />
<p><label>Login Group</label>
<select name="login">
<option value="contributor">Contributor</option>
<option value="spectator">Spectator</option>
<option value="admin">Admin</option>
<option value="wizard">Wizard</option>
</select>
</p>
<p><input type="submit" /></p>
</form>
</section>
<section id="destruction">
<form action="private/user_delete.php" method="post">
</form>
<form action="private/post_delete.php" method="post">
</form>
</section>

View File

@ -0,0 +1,3 @@
<h1>Eek!</h1>
<h2>Something was hecked.</h2>
<h3>{{ error_message }} Go back and try again.</h3>

View File

@ -0,0 +1,2 @@
</body>
</html>

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="{{ depth }}res/{{ theme }}/css/global.css">
<link rel="stylesheet" type="text/css" href="{{ depth }}res/{{ theme }}/css/{{ page }}.css">
<title>Index</title>
</head>
<body>
<!-- Powered by blagoblag, which is Free Software under the GNU AGPLv3.
Git your sources here: <https://git.eunichx.us/blagoblag.git> -->

View File

@ -1,11 +1 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="../css/global.css">
<link rel="stylesheet" type="text/css" href="../css/index.css">
<title>Index</title>
</head>
<body>
<p>{{ animal }}s are SUPER RAD.</p> <p>{{ animal }}s are SUPER RAD.</p>
</body>
</html

View File

@ -1,8 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<link rel="stylesheet" type="text/css" href="../css/global.css"> <link rel="stylesheet" type="text/css" href="res/default/css/global.css">
<link rel="stylesheet" type="text/css" href="../css/post.css"> <link rel="stylesheet" type="text/css" href="res/default/css/post.css">
<title>Index</title> <title>Index</title>
</head> </head>
<body> <body>

View File

@ -1,10 +1,10 @@
<!DOCTYPE html> <h1>{{ full_name }}</h1>
<html lang="en"> <h2>{{ username }}</h2>
<head> <h3>{{ email }}</h3>
<link rel="stylesheet" type="text/css" href="../css/global.css"> <h3>{{ url }}</h3>
<link rel="stylesheet" type="text/css" href="../css/user.css"> <p>{{ bio }}</p>
<title>Index</title> <ul>
</head> {% for post in posts %}
<body> <li>{{ post }}</li>
</body> {% endfor %}
</html </ul>

View File

@ -1,7 +1,28 @@
<?php <?php
/* This file is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
$depth = ""; $depth = "";
$title = "About";
include "res/lib/load.php"; include "res/lib/load.php";
echo $GLOBALS['twig']->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]);
?> ?>