1
0
mirror of https://github.com/glmdev/eecs448-lab10 synced 2026-03-02 03:39:24 +00:00

Initial commit

This commit is contained in:
2020-12-06 13:58:02 -06:00
commit 5a54fe251d
24 changed files with 911 additions and 0 deletions

208
common/Page.php Normal file
View File

@@ -0,0 +1,208 @@
<?php
namespace common;
class Page {
protected $_title;
protected $_writers = [];
protected $_wrote_pre = false;
protected $_wrote_post = false;
protected $_writing = false;
protected $_scripts = [];
protected $_styles = [];
public function calls(callable $writer) {
$this->_writers[] = $writer;
if ( $this->_writing ) {
$writer();
}
return $this;
}
public function writes($string) {
$this->calls(function() use($string) {
echo $string;
});
return $this;
}
public function script($src) {
$this->_scripts[] = $src;
return $this;
}
public function stylesheet($src) {
$this->_styles[] = $src;
return $this;
}
public function title($title = null) {
if ( $title ) {
$this->_title = $title;
return $this;
} else {
return $this->_title;
}
}
public function header($string = null) {
if ( !$string ) $string = $this->title();
return $this->writes('<h1>' . $string . '</h1>');
}
public function form($submit, callable $inner) {
$this->writes('<form method="POST" action="' . $submit . '">');
$this->calls($inner);
$this->writes('</form>');
return $this;
}
public function submit($text = 'Submit') {
$this->writes('<button type="submit">' . $text . '</button>');
}
public function div(callable $inner) {
$this->writes('<div class="common-page">');
$this->calls($inner);
$this->writes('</div>');
return $this;
}
public function line_break() {
$this->writes('<br/>');
return $this;
}
public function table($nested_array) {
if ( is_array($nested_array) ) {
$this->calls(function() use($nested_array) {
echo '<table style="width: 100%;">';
foreach ( $nested_array as $row_idx => $row ) {
echo '<tr>';
foreach ( $row as $col_idx => $col ) {
$tag = $row_idx === 0 ? 'th' : 'td';
echo "<{$tag}>{$col}</{$tag}>";
}
echo '</tr>';
}
echo '</table>';
});
} else {
$this->writes('<table style="width: 100%;">');
$this->calls($nested_array);
$this->writes('</table>');
}
return $this;
}
public function table_row_cell(callable $inner, $span = 1) {
$this->table_row(function() use($inner, $span) {
$this->writes('<td colspan="' . $span . '">');
$this->calls($inner);
$this->writes('</td>');
});
return $this;
}
public function table_row($inner = null) {
$this->writes('<tr>');
if ( $inner ) $this->calls($inner);
$this->writes('</tr>');
return $this;
}
public function table_head(callable $inner) {
$this->writes('<th>');
$this->calls($inner);
$this->writes('</th>');
return $this;
}
public function table_cell($inner = null) {
$this->writes('<td>');
if ( $inner ) $this->calls($inner);
$this->writes('</td>');
return $this;
}
public function fail_to($message, $redirect_url) {
$this->writes('<p>' . $message . '</p>')
->writes('<p><a href="' . $redirect_url . '">Try Again</a></p>')
->write();
exit;
}
public function preamble() {
if ( $this->_wrote_pre ) return;
$this->_styles[] = system_url('assets/common.css');
$styles = [];
foreach ( $this->_styles as $style ) {
$styles[] = '<link rel="stylesheet" href="' . $style . '"/>';
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?= $this->_title ?></title>
<?= implode("\n", $styles) ?>
</head>
<body>
<?php
$this->_wrote_pre = true;
}
public function postamble() {
if ( $this->_wrote_post ) return;
$scripts = [];
foreach ( $this->_scripts as $script ) {
$scripts[] = '<script src="' . $script . '"></script>';
}
?>
<?= implode("\n", $scripts) ?>
</body>
</html>
<?php
$this->_wrote_post = true;
}
public function write() {
$this->_writing = true;
$this->preamble();
foreach ( $this->_writers as $writer ) {
$writer();
}
$this->postamble();
$this->_writing = false;
}
public function compile() {
ob_start();
$this->_wrote_pre = false;
$this->_wrote_post = false;
$this->write();
$this->_wrote_pre = false;
$this->_wrote_post = false;
return ob_get_clean();
}
}

40
common/Request.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
namespace common;
class Request {
protected $_get = [];
protected $_post = [];
public static function capture() {
$req = new static();
$req->_get = $_GET;
$req->_post = $_POST;
return $req;
}
public function input($path = null) {
if ( !$path ) {
return array_merge($this->_get, $this->_post);
}
$path_parts = explode('.', $path);
$get_value = $this->_get;
foreach ( $path_parts as $part ) {
if ( $get_value ) {
$get_value = $get_value[$part];
}
}
$post_value = $this->_post;
foreach ( $path_parts as $part ) {
if ( $post_value ) {
$post_value = $post_value[$part];
}
}
if ( $get_value ) return $get_value;
if ( $post_value ) return $post_value;
}
}

20
common/autoload.php Normal file
View File

@@ -0,0 +1,20 @@
<?php
class AutoLoad {
public static function load($class_name) {
$file = str_replace("\\", '/', $class_name) . '.php';
$path = system_path($file);
if ( file_exists($path) ) {
include_system($file, false);
if ( class_exists($class_name) ) {
return true;
}
}
return false;
}
}
spl_autoload_register('AutoLoad::load');

View File

@@ -0,0 +1,81 @@
<?php
namespace common\database;
class Connection {
protected static $mysqli;
protected $config;
function __construct() {
$this->config = config('database');
}
public function connect() {
if ( !static::$mysqli ) {
static::$mysqli = new \mysqli(
$this->config['url'],
$this->config['username'],
$this->config['password'],
$this->config['database']
);
}
if ( static::$mysqli->connect_errno ) {
throw new \Exception('Unable to connect to database: ' . static::$mysqli->connect_error);
}
}
public function escape($value) {
return static::$mysqli->real_escape_string($value);
}
public function execute($query, $args = [], $returns_statement = false) {
$statement = static::$mysqli->prepare($query);
if ( sizeof($args) > 0 ) {
$types = '';
foreach ( $args as $arg ) {
if ( is_int($arg) ) {
$types .= 'i';
} else if ( is_float($arg) || is_double($arg) ) {
$types .= 'd';
} else if ( is_string($arg) ) {
$types .= 's';
} else {
$types .= 'b';
}
}
$statement->bind_param($types, ...$args);
}
$statement->execute();
if ( $returns_statement ) return $statement;
return $statement->get_result();
}
public function insert($query, $args = []) {
$statement = $this->execute($query, $args, true);
return $statement->insert_id;
}
public function fetch($query, $args = []) {
$result = $this->execute($query, $args);
$rows = [];
while ( $row = $result->fetch_assoc() ) {
$rows[] = $row;
}
return $rows;
}
public function close() {
if ( static::$mysqli ) {
static::$mysqli->close();
static::$mysqli = null;
}
}
}

View File

@@ -0,0 +1,134 @@
<?php
namespace common\database;
class Repository {
protected $table;
protected $primary_key;
protected $fields = [];
protected $connection;
function __construct() {
$this->connection = new Connection();
$this->connection->connect();
}
function __destruct() {
$this->connection->close();
}
public function create($record) {
$fields = [];
$arg_parts = [];
$args = [];
foreach ( $this->fields as $field ) {
if ( isset($record[$field]) ) {
$fields[] = $field;
$args[] = $record[$field];
$arg_parts[] = '?';
}
}
$query = 'INSERT INTO ' . $this->table . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $arg_parts) . ')';
$insert_id = $this->connection->insert($query, $args);
$record[$this->primary_key] = $insert_id;
return $record;
}
public function find_by_id($primary_key) {
$query = 'SELECT ' . implode(', ', $this->fields) . ' FROM ' . $this->table . ' WHERE ' . $this->primary_key . ' = ?';
$results = $this->connection->fetch($query, [$primary_key]);
if ( sizeof($results) > 0 ) {
return $results[0];
}
}
public function find($filter = []) {
$query = 'SELECT ' . implode(', ', $this->fields) . ' FROM ' . $this->table . ' WHERE ';
list($wheres, $where_args) = $this->build_wheres_from_filter($filter);
$query .= implode(' AND ', $wheres);
$query .= ' ORDER BY ' . $this->primary_key . ' ASC';
return $this->connection->fetch($query, $where_args);
}
public function find_one($filter = []) {
$query = 'SELECT ' . implode(', ', $this->fields) . ' FROM ' . $this->table . ' WHERE ';
list($wheres, $where_args) = $this->build_wheres_from_filter($filter);
$query .= implode(' AND ', $wheres) . ' LIMIT 1';
$results = $this->connection->fetch($query, $where_args);
if ( sizeof($results) > 0 ) {
return $results[0];
}
}
public function update($record) {
$query = 'UPDATE ' . $this->table . ' SET ';
$query_args = [];
$sets = [];
foreach ( $this->fields as $field ) {
if ( isset($record[$field]) ) {
$sets[] = $field . '=?';
$query_args[] = $record[$field];
}
}
$query = 'UPDATE ' . $this->table . ' SET ' . implode(', ', $sets) . ' WHERE ' . $this->primary_key . ' = ?';
$query_args[] = $record[$this->primary_key];
$this->connection->execute($query, $query_args);
return $record;
}
public function save($record) {
if ( isset($record[$this->primary_key]) ) {
return $this->update($record);
} else {
return $this->create($record);
}
}
public function delete($record) {
$primary_key = $record[$this->primary_key];
$query = 'DELETE FROM ' . $this->table . ' WHERE ' . $this->primary_key . ' = ?';
$this->connection->execute($query, [$primary_key]);
}
protected function build_wheres_from_filter($filter = []) {
if ( !$filter ) {
return [['1=1'], []];
}
$wheres = [];
$where_args = [];
foreach ( $this->fields as $field ) {
if ( isset($filter[$field]) ) {
$val = $filter[$field];
if ( is_string($val) || is_numeric($val) ) {
$wheres[] = $field . ' = ?';
$where_args[] = $val;
} else if ( is_array($val) ) {
$where_items = [];
foreach ( $val as $item ) {
$where_items[] = '?';
$where_args[] = $item;
}
$wheres[] = $field . ' IN (' . implode(',', $where_items) . ')';
}
}
}
return [$wheres, $where_args];
}
}

View File

@@ -0,0 +1,18 @@
<?php
function config($path, $default_value = null) {
$parts = explode('.', $path);
if ( !$parts[0] ) {
return $default_value;
}
$config = require_system('config/' . $parts[0] . '.php');
foreach ( $parts as $i => $part ) {
if ( $i !== 0 && $config ) {
$config = $config[$part];
}
}
return $config;
}

View File

@@ -0,0 +1,8 @@
<?php
function dd($something) {
echo "<pre><code>";
var_dump($something);
echo "</code></pre>";
exit;
}

View File

@@ -0,0 +1,54 @@
<?php
function system_root() {
$root = dirname(dirname(__FILE__));
return $root;
}
function system_path($path) {
if ( $path[0] === '/' ) {
$path = substr($path, 1);
}
return implode('/', [system_root(), $path]);
}
function require_system($path, $once = true) {
if ( $once ) {
return require_once system_path($path);
} else {
return require system_path($path);
}
}
function include_system($path, $once = true) {
if ( $once ) {
return include_once system_path($path);
} else {
return include system_path($path);
}
}
function system_url($path) {
if ( $path[0] === '/' ) {
$path = substr($path, 1);
}
return implode('/', [SYSTEM_URL, $path]);
}
function system_redirect($path) {
header('Location: ' . system_url($path));
exit;
}
function no_direct_access() {
$key = array_search(__FUNCTION__, array_column(debug_backtrace(), 'function'));
$caller_file = debug_backtrace()[$key]['file'];
if ( $caller_file === $_SERVER['SCRIPT_FILENAME'] ) {
header("HTTP/1.1 401 Unauthorized");
echo 'Forbidden';
exit;
}
}