Skip to content

Commit

Permalink
Initial Code Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam committed Dec 7, 2020
1 parent 8adaca3 commit 07799dc
Show file tree
Hide file tree
Showing 17 changed files with 921 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

if(!function_exists('classAutoLoader')){
function classAutoLoader($class){
$parts = explode('\\', $class);
if( count($parts) == 2 ){
if( $parts[0] == 'Model' || $parts[0] == 'Controller' ){
$dir = ( $parts[0] == 'Model' ) ? 'models' : 'controllers';
if( is_file('../'.$dir.'/'.$parts[1].'.php') ){
if( !class_exists($class) ) {
include_once('../'.$dir.'/'.$parts[1].'.php');
}
}
}
}
}
}

spl_autoload_register('classAutoLoader');
20 changes: 20 additions & 0 deletions Output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class Output {

public static function error($json = array(), $http_code = 400) {
header('Content-Type: application/json');
http_response_code($http_code);
echo (gettype($json) == 'array') ? json_encode($json) : json_encode(array($json));
exit();
}


public static function success($json = array(), $http_code = 200) {
header('Content-Type: application/json');
http_response_code($http_code);
echo (gettype($json) == 'array') ? json_encode($json) : json_encode(array($json));
exit();
}

}
131 changes: 131 additions & 0 deletions Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

class Route
{

protected static $routes = array();
private static $page404 = '404';

public static function set404($str){
self::$page404 = $str;
}

/**
* @param $http_verb - The HTTP Verb for the route
* @param $page - The page URL segment to match
* @param $func - The function to execute when the route is matched
* @param bool $regex - Whether or not $page is in the form of a regex
*/
public static function add( $http_verb, $page, $func )
{

if (gettype($page) == 'array') {
foreach ($page as $i => $p) {
$page[$i] = self::makeRegex($p);
}
} else {
$page = self::makeRegex($page);
}


$http_verb = ( gettype($http_verb) == 'array' ) ? $http_verb : array($http_verb);
foreach( $http_verb AS $verb ) {
if (!isset(self::$routes[$verb])) {
self::$routes[$verb] = array();
}
if ( gettype($page) == 'array' ) {
foreach ( $page as $p ) {
self::$routes[$verb][] = array(
'regex' => $p,
'function' => $func
);
}
} else {
self::$routes[$verb][] = array(
'regex' => $page,
'function' => $func
);
}
}
}

/**
* @param $str String - The URL segment to make a regex for
* @return String
*/
private static function makeRegex($str) {
$str = str_replace('[string]', '([0-9a-zA-Z\-]{1,})', $str);
$str = str_replace('[int]', '([0-9]{1,})', $str);
$str = str_replace('[md5-hash]', '([0-9a-fA-F]{32})', $str);
$str = str_replace('[sha-hash]', '([0-9a-zA-Z]{171}[=]{1})', $str);
$str = '/^'.str_replace('/', '\/', $str).'[\/]?$/';
return $str;
}

private static function url()
{
$url = explode("?",$_SERVER["REQUEST_URI"]);
return strval($url[0]);
}

public static function run()
{
$match = false;
if( isset(self::$routes[$_SERVER["REQUEST_METHOD"]]) )
{
foreach( self::$routes[$_SERVER["REQUEST_METHOD"]] as $page )
{
$a = @preg_match($page["regex"], self::url(), $matches);
if ($a) {
$action = $page["function"];
$act = explode('@', $action);
if( count($act) == 2 ) {
$controllerName = $act[0];
$methodName = $act[1];
if( file_exists('../controllers/'.$controllerName.'.php') ){
include_once('../controllers/'.$controllerName.'.php');
if (method_exists('Controller\\' . $controllerName, $methodName)) {
if (is_callable('Controller\\' . $controllerName, $methodName)) {
call_user_func(array('Controller\\' . $controllerName, $methodName), $matches);
$match = true;
}
}else{
die("Method does not exist");
}
}else{
die("Controller does not exist");
}
}
}
}
}
if( !$match ){
View::page(self::$page404);
}
}


public static function load(){
foreach (scandir('../routes') as $f) {
if( $f != '.' && $f != '..' ) {
if (is_dir('../routes/' . $f)) {
foreach (scandir('../routes/'.$f) as $f2) {
if (substr($f2, -3, 3) == 'php') {
include_once('../routes/' . $f.'/'.$f2);
}
}
} else {
if (substr($f, -3, 3) == 'php') {
include_once('../routes/'. $f);
}
}
}
}

}


}



14 changes: 14 additions & 0 deletions View.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class View {

public static function page($file, $data = array()) {
return (file_exists('../view/' . $file . '.php')) ? include_once('../view/' . $file . '.php') : false;
}

public static function redirect($url) {
header("Location: " . $url);
exit();
}

}
104 changes: 104 additions & 0 deletions controllers/Forum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php


namespace Controller;


use Model\Comment;
use Model\Post;
use Model\Section;

class Forum
{


public static function home(){
$general = array();
foreach( Section::getAll() as $section ){
$general[] = array(
'id' => $section->getId(),
'name' => $section->getName(),
'posts' => count( Post::getBySection($section))
);
}
$admins = array();
foreach( Section::getAll(true) as $section ){
$admins[] = array(
'id' => $section->getId(),
'name' => $section->getName(),
'posts' => count( Post::getBySection($section)),
);
}
$data = array(
'admin' => false,
'header' => array(
'title' => 'Forum'
),
'section' => array(
'general' => $general,
'admins' => $admins
)
);
\View::page('home',$data);
}

public static function post($arg){
if( $post = Post::get($arg[2]) ){
if( $post->getSectionId() == $arg[1] ){
$section = Section::get($post->getSectionId());
$comments = array();
foreach( Comment::getByPost($post) as $comment ){
$comments[] = array(
'user' => $comment->getUsername(),
'content' => $comment->getContent(),
'date' => $comment->getCreatedAt()
);
}
$data = array(
'section' => array(
'id' => $section->getId(),
'name' => $section->getName()
),
'post' => array(
'name' => $post->getTitle(),
'user' => $post->getUsername(),
'content' => $post->getContent(),
'date' => $post->getCreatedAt()
),
'comments' => $comments,
);

\View::page('post',$data);
}else{
\View::page('404');
}
}else{
\View::page('404');
}
}

public static function section($arg){
if( $section = Section::get($arg[1]) ){
$posts = array();
foreach( Post::getBySection($section) as $post ){
$posts[] = array(
'id' => $post->getId(),
'name' => $post->getTitle(),
'comments' => count( Comment::getByPost($post ))
);
}
$data = array(
'section' => array(
'id' => $section->getId(),
'name' => $section->getName(),
'posts' => $posts
)
);
\View::page('section',$data);
}else{
\View::page('404');
}
}


}
57 changes: 57 additions & 0 deletions models/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php namespace Model;


class Comment
{

private $comment;

private function __construct( $data ){
$this->comment = $data;
}

public function getId(){
return (int)$this->comment["id"];
}

public function getPostId(){
return (int)$this->comment["section_id"];
}

/**
* @return string
*/
public function getContent(){
return $this->comment["content"];
}

/**
* @return string
*/
public function getUsername(){
return $this->comment["username"];
}

/**
* @return string
*/
public function getCreatedAt(){
return $this->comment["created_at"];
}

/**
* @param Post $post
* @return Comment[]
*/
public static function getByPost(Post $post ){
$resp = array();
$d = Db::read()->prepare('select * from comment where post_id = ? ');
$d->execute( array($post->getId()) );
while( $comment = $d->fetch() ){
$resp[] = new Comment( $comment );
}
return $resp;
}


}
Loading

0 comments on commit 07799dc

Please sign in to comment.