diff --git a/.gitignore b/.gitignore index 3f62e54..77e9928 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/config/config.php \ No newline at end of file +config/config.php \ No newline at end of file diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..6fc5bf3 --- /dev/null +++ b/TODO.md @@ -0,0 +1,8 @@ +# TODO + +Prio / description + +- 100 markdown -> parsedown +- 40 Search system +- 20 Searchbar +- 5 installer \ No newline at end of file diff --git a/config/config.php b/config/config.php index 992c217..798732f 100644 --- a/config/config.php +++ b/config/config.php @@ -1,9 +1,15 @@ view('error/404'); - } - } \ No newline at end of file diff --git a/src/Core.php b/src/Core.php deleted file mode 100644 index 190d8c3..0000000 --- a/src/Core.php +++ /dev/null @@ -1,46 +0,0 @@ -getUrl(); - - $controller = ucfirst(strtolower($url[0])); - if(file_exists(__DIR__ . '/controllers/' . $controller . '.php')) { - $this->currentController = $controller; - unset($url[0]); - } - require_once(__DIR__ . '/controllers/' . $this->currentController . '.php'); - $this->currentController = new $this->currentController; - - if(isset($url[1])) { - $method = strtolower($url[1]); - if(method_exists($this->currentController, $method)) { - $this->currentMethod = $method; - unset($url[1]); - } - } - - $this->params = $url ? array_values($url) : array(null); - - call_user_func_array([$this->currentController, $this->currentMethod], $this->params); - } - - public function getUrl() { - if(isset($_SERVER['PATH_INFO'])) { - $url = rtrim($_SERVER['PATH_INFO'], '/'); // remove last slash - $url = substr($url, 1); // remove first slash - $url = filter_var($url, FILTER_SANITIZE_URL); // sanitize URL - $url = explode('/', $url); - return $url; - } - } - } \ No newline at end of file diff --git a/src/Template.php b/src/Template.php deleted file mode 100644 index 79e64d3..0000000 --- a/src/Template.php +++ /dev/null @@ -1,11 +0,0 @@ -{method} + private $params = []; + + private $loggedInUser = null; + + public function __construct() { + $this->init(); + } + + private function init() { + session_start(); + $this->include(); + $this->getUrl(); + $this->getController(); + $this->controller = new $this->controller(); + $this->getMethod(); + $this->getParams(); + call_user_func_array([$this->controller, $this->method], $this->params); + } + + private function getController() { + $controller = ucfirst(strtolower($this->url[0])) . 'Controller'; + if(file_exists(__DIR__ . '/controllers/' . $controller . '.php')) { + $this->controller = $controller; + array_shift($this->url); + } + require_once(__DIR__ . '/controllers/' . $this->controller . '.php'); + } + + private function getMethod() { + if(isset($this->url[0])) { + $method = strtolower($this->url[0]); + if(method_exists($this->controller, $method)) { + $this->method = $method; + array_shift($this->url); + } + } + } + + private function getParams() { + $this->params = $this->url ? array_values($this->url) : [null]; + } + + private function include() { + require_once(__DIR__ . '/../../config/config.php'); + require_once(__DIR__ . '/Database.php'); + require_once(__DIR__ . '/Controller.php'); + require_once(__DIR__ . '/Model.php'); + require_once(__DIR__ . '/Linker.php'); + require_once(__DIR__ . '/lib/Parsedown.php'); + require_once(__DIR__ . '/Template.php'); + } + + private function getUrl() { + if(isset($_SERVER['PATH_INFO'])) { + $url = rtrim($_SERVER['PATH_INFO'], '/'); // remove last slash + $url = substr($url, 1); // remove first slash + $url = filter_var($url, FILTER_SANITIZE_URL); // sanitize URL + $url = explode('/', $url); + if($url[0] == '') return; + $this->url = $url; + } + } + } \ No newline at end of file diff --git a/src/Database.php b/src/classes/Database.php similarity index 95% rename from src/Database.php rename to src/classes/Database.php index 95241d6..4c123a2 100644 --- a/src/Database.php +++ b/src/classes/Database.php @@ -2,7 +2,7 @@ class Database { private $host = DB_HOST; private $user = DB_USER; - private $password = DB_PASSWORD; + private $password = DB_PASS; private $name = DB_NAME; private $stmt; @@ -20,6 +20,7 @@ public function __construct() { } catch(PDOException $e) { $this->error = $e->getMessage(); echo $this->error; + exit(); } } @@ -53,7 +54,7 @@ public function resultSet() { return $this->stmt->fetchAll(PDO::FETCH_OBJ); } - public function single() { + public function single() { $this->execute(); return $this->stmt->fetch(PDO::FETCH_OBJ); } diff --git a/src/classes/Linker.php b/src/classes/Linker.php new file mode 100644 index 0000000..bff2c08 --- /dev/null +++ b/src/classes/Linker.php @@ -0,0 +1,8 @@ +db = new Database; + $this->db = new Database(); } - } \ No newline at end of file + } diff --git a/src/classes/Template.php b/src/classes/Template.php new file mode 100644 index 0000000..002af3e --- /dev/null +++ b/src/classes/Template.php @@ -0,0 +1,11 @@ +authModel = $this->model('Authentication'); + $this->authModel = $this->model('Auth'); $this->userModel = $this->model('User'); } + + public function index() { + $this->login(); + } public function register() { if(!isset($_SESSION['loggedIn'])) { @@ -17,25 +18,25 @@ public function register() { exit(); } - $errors = null; + $msg = null; $username = ''; $password = ''; - if(isset($_POST['submit'])) { - if(empty($_POST['username'])) { - $errors[] = "Field username is empty."; + if(isset($_POST['submitInput'])) { + if(empty($_POST['usernameInput'])) { + $msg[] = "Field username is empty."; } else { - $username = $_POST['username']; + $username = $_POST['usernameInput']; } - if(empty($_POST['password'])) { - $errors[] = "Field password is empty."; + if(empty($_POST['passwordInput'])) { + $msg[] = "Field password is empty."; } else { - $password = $_POST['password']; + $password = $_POST['passwordInput']; } if($this->userModel->existsUname($username)) { - $errors[] = 'User already exists'; + $msg[] = 'User already exists'; } - if($errors == null) { + if($msg == null) { $password = password_hash($password, PASSWORD_DEFAULT); $username = htmlentities($username, ENT_QUOTES, 'utf-8'); $username = trim($username); @@ -48,61 +49,62 @@ public function register() { $data = array( 'username' => $username, - 'errors' => $errors, + 'msg' => $msg, 'backPath' => 'users/users', 'actionName' => 'Register' ); - $this->view('authentication/register', $data); + $this->render('auth/register', $data); + } public function login() { if(isset($_SESSION['loggedIn'])) { - header('Location: ' . ROOT_PATH); + header('Location: /index/index'); exit(); } - $errors = null; + $msg = null; $username = ''; $password = ''; - if(isset($_POST['submit'])) { - if(empty($_POST['username'])) { - $errors[] = "Field username is empty."; + if(isset($_POST['submitInput'])) { + if(empty($_POST['usernameInput'])) { + $msg[] = "Field username is empty."; } else { - $username = $_POST['username']; + $username = $_POST['usernameInput']; } - if(empty($_POST['password'])) { - $errors[] = "Field password is empty."; + if(empty($_POST['passwordInput'])) { + $msg[] = "Field password is empty."; } else { - $password = $_POST['password']; + $password = $_POST['passwordInput']; } - if($errors == null) { + if($msg == null) { $login = $this->authModel->login($username, $password); if($login !== false) { $_SESSION['loggedIn'] = $login; - header('Location: ' . ROOT_PATH); + header('Location: /index/index'); + exit(); } else { - $errors[] = "Login failed, password or username incorrect."; + $msg[] = "Login failed, password or username incorrect."; } } } + $data = array( 'username' => $username, - 'errors' => $errors, - 'actionName' => 'Login' + 'msg' => $msg ); - $this->view('authentication/login', $data); + $this->render('auth/login', $data); } public function logout() { if(!isset($_SESSION['loggedIn'])) { - header('Location: ' . ROOT_PATH); + header('Location: ' . Linker::link('index', 'index')); exit(); } session_destroy(); - header('Location: ' . ROOT_PATH); + header('Location: ' . Linker::link('index', 'index')); } - } \ No newline at end of file diff --git a/src/classes/controllers/PostsController.php b/src/classes/controllers/PostsController.php new file mode 100644 index 0000000..7b61a50 --- /dev/null +++ b/src/classes/controllers/PostsController.php @@ -0,0 +1,156 @@ +postModel = $this->model('post'); + } + + public function index($page = 1) { + $posts = $this->postModel->getPostsLimit($page); + + $data = [ + 'posts' => $posts + ]; + + $this->render('posts/index', $data); + } + + public function post($id, $page = 1) { + + $post = $this->postModel->getPostByIdMd($id); + + $data = [ + 'post' => $post, + 'page' => $page + ]; + + $this->render('posts/post', $data); + } + + + public function create() { + if(!isset($_SESSION['loggedIn'])) { + header('Location: ' . ROOT_PATH); + exit(); + } + + $errors = null; + $title = ''; + $body = ''; + + if(isset($_POST['submitInput'])) { + if(empty($_POST['titleInput'])) { + $errors[] = "Field Title is empty."; + } else { + $title = $_POST['titleInput']; + } + if(empty($_POST['bodyInput'])) { + $errors[] = "Field Text is empty."; + } else { + $body = $_POST['bodyInput']; + } + if($errors == null) { + $userId = $_SESSION['loggedIn']; + $title = htmlentities($title, ENT_QUOTES, "UTF-8"); + $body = htmlentities($body, ENT_QUOTES, "UTF-8"); + + $id = $this->postModel->create($title, $body, $userId); + header('Location: ' . ROOT_PATH . 'posts/post/' . $id); + exit(); + } + } + + $data = array( + 'title' => $title, + 'body' => $body, + 'errors' => $errors, + 'actionName' => 'Create', + 'backPath' => 'posts/index/', + ); + + $this->render('posts/create', $data); + } + + public function edit($id) { + if(!isset($_SESSION['loggedIn'])) { + header('Location: ' . ROOT_PATH); + exit(); + } + if($id === null || !is_numeric($id)) { + header('Location: ' . ROOT_PATH); + exit(); + } + + $id = filter_var($id, FILTER_SANITIZE_NUMBER_INT); + + $errors = null; + $title = ''; + $body = ''; + + $post = $this->postModel->getPostById($id); + + if($post == null) { + header('Location: ' . ROOT_PATH); + exit(); + } + + if(isset($_POST['submitInput'])) { + if(empty($_POST['titleInput'])) { + $errors[] = "Field Title is empty."; + } else { + $title = $_POST['titleInput']; + } + if(empty($_POST['bodyInput'])) { + $errors[] = "Field Text is empty."; + } else { + $body = $_POST['bodyInput']; + } + if($errors == null) { + $title = htmlentities($title, ENT_QUOTES, "UTF-8"); + $body = htmlentities($body, ENT_QUOTES, "UTF-8"); + $this->postModel->update($title, $body, $id); + header('Location: ' . ROOT_PATH . 'posts/post/' . $id); + exit(); + } + } + + $data = array( + 'errors' => $errors, + 'title' => $post->title, + 'body' => $post->body, + 'id' => $post->pid, + 'actionName' => 'Edit' + ); + + $this->render('posts/edit', $data); + } + + public function delete($id) { + if(!isset($_SESSION['loggedIn'])) { + header('Location: ' . ROOT_PATH); + exit(); + } + if($id === null || !is_numeric($id)) { + header('Location: ' . ROOT_PATH); + exit(); + } + $id = filter_var($id, FILTER_SANITIZE_NUMBER_INT); + + if(!$this->postModel->existsId($id)) { + header('Location: ' . ROOT_PATH); + exit(); + } + + if(isset($_POST['sure'])) { + $this->postModel->delete($id); + header('Location: ' . ROOT_PATH); + } + + $data = array( + 'id' => $id + ); + + $this->render('posts/delete', $data); + } + } \ No newline at end of file diff --git a/src/controllers/Users.php b/src/classes/controllers/UsersController.php similarity index 80% rename from src/controllers/Users.php rename to src/classes/controllers/UsersController.php index ad03137..c7b10a4 100644 --- a/src/controllers/Users.php +++ b/src/classes/controllers/UsersController.php @@ -1,17 +1,14 @@ userModel = $this->model('User'); $this->postModel = $this->model('Post'); } - public function users() { + public function index() { if(!isset($_SESSION['loggedIn'])) { header('Location: ' . ROOT_PATH); exit(); @@ -22,8 +19,8 @@ public function users() { $data = array( 'usersData' => $users ); - - $this->view('user/users', $data); + + $this->render('users/index', $data); } public function edit($id) { @@ -37,7 +34,7 @@ public function edit($id) { } $id = filter_var($id, FILTER_SANITIZE_NUMBER_INT); - $errors = null; + $msg = null; $username = ''; $password = ''; @@ -48,19 +45,19 @@ public function edit($id) { $user = $this->userModel->getUserById($id); - if(isset($_POST['submit'])) { - if(empty($_POST['username'])) { - $errors[] = "Field username is empty."; + if(isset($_POST['submitInput'])) { + if(empty($_POST['usernameInput'])) { + $msg[] = "Field username is empty."; } else { - $username = $_POST['username']; + $username = $_POST['usernameInput']; } - if(empty($_POST['password'])) { + if(empty($_POST['passwordInput'])) { $password = ''; } else { - $password = $_POST['password']; + $password = $_POST['passwordInput']; } - if($errors == null) { + if($msg == null) { $username = htmlentities($username, ENT_QUOTES, "UTF-8"); $password = $password == '' ? $user->password : password_hash($password, PASSWORD_DEFAULT); @@ -72,14 +69,14 @@ public function edit($id) { } $data = array( - 'errors' => $errors, + 'msg' => $msg, 'id' => $user->id, 'username' => $user->username, 'backPath' => 'users/users', 'actionName' => 'Edit' ); - $this->view('user/edit', $data); + $this->render('users/edit', $data); } public function delete($id) { @@ -114,6 +111,6 @@ public function delete($id) { 'backPath' => 'users/users' ); - $this->view('user/delete', $data); + $this->render('users/delete', $data); } - } + } \ No newline at end of file diff --git a/src/classes/lib/Parsedown.php b/src/classes/lib/Parsedown.php new file mode 100644 index 0000000..1832db7 --- /dev/null +++ b/src/classes/lib/Parsedown.php @@ -0,0 +1,1994 @@ +textElements($text); + + # convert to markup + $markup = $this->elements($Elements); + + # trim line breaks + $markup = trim($markup, "\n"); + + return $markup; + } + + protected function textElements($text) + { + # make sure no definitions are set + $this->DefinitionData = array(); + + # standardize line breaks + $text = str_replace(array("\r\n", "\r"), "\n", $text); + + # remove surrounding line breaks + $text = trim($text, "\n"); + + # split text into lines + $lines = explode("\n", $text); + + # iterate through lines to identify blocks + return $this->linesElements($lines); + } + + # + # Setters + # + + function setBreaksEnabled($breaksEnabled) + { + $this->breaksEnabled = $breaksEnabled; + + return $this; + } + + protected $breaksEnabled; + + function setMarkupEscaped($markupEscaped) + { + $this->markupEscaped = $markupEscaped; + + return $this; + } + + protected $markupEscaped; + + function setUrlsLinked($urlsLinked) + { + $this->urlsLinked = $urlsLinked; + + return $this; + } + + protected $urlsLinked = true; + + function setSafeMode($safeMode) + { + $this->safeMode = (bool) $safeMode; + + return $this; + } + + protected $safeMode; + + function setStrictMode($strictMode) + { + $this->strictMode = (bool) $strictMode; + + return $this; + } + + protected $strictMode; + + protected $safeLinksWhitelist = array( + 'http://', + 'https://', + 'ftp://', + 'ftps://', + 'mailto:', + 'tel:', + 'data:image/png;base64,', + 'data:image/gif;base64,', + 'data:image/jpeg;base64,', + 'irc:', + 'ircs:', + 'git:', + 'ssh:', + 'news:', + 'steam:', + ); + + # + # Lines + # + + protected $BlockTypes = array( + '#' => array('Header'), + '*' => array('Rule', 'List'), + '+' => array('List'), + '-' => array('SetextHeader', 'Table', 'Rule', 'List'), + '0' => array('List'), + '1' => array('List'), + '2' => array('List'), + '3' => array('List'), + '4' => array('List'), + '5' => array('List'), + '6' => array('List'), + '7' => array('List'), + '8' => array('List'), + '9' => array('List'), + ':' => array('Table'), + '<' => array('Comment', 'Markup'), + '=' => array('SetextHeader'), + '>' => array('Quote'), + '[' => array('Reference'), + '_' => array('Rule'), + '`' => array('FencedCode'), + '|' => array('Table'), + '~' => array('FencedCode'), + ); + + # ~ + + protected $unmarkedBlockTypes = array( + 'Code', + ); + + # + # Blocks + # + + protected function lines(array $lines) + { + return $this->elements($this->linesElements($lines)); + } + + protected function linesElements(array $lines) + { + $Elements = array(); + $CurrentBlock = null; + + foreach ($lines as $line) + { + if (chop($line) === '') + { + if (isset($CurrentBlock)) + { + $CurrentBlock['interrupted'] = (isset($CurrentBlock['interrupted']) + ? $CurrentBlock['interrupted'] + 1 : 1 + ); + } + + continue; + } + + while (($beforeTab = strstr($line, "\t", true)) !== false) + { + $shortage = 4 - mb_strlen($beforeTab, 'utf-8') % 4; + + $line = $beforeTab + . str_repeat(' ', $shortage) + . substr($line, strlen($beforeTab) + 1) + ; + } + + $indent = strspn($line, ' '); + + $text = $indent > 0 ? substr($line, $indent) : $line; + + # ~ + + $Line = array('body' => $line, 'indent' => $indent, 'text' => $text); + + # ~ + + if (isset($CurrentBlock['continuable'])) + { + $methodName = 'block' . $CurrentBlock['type'] . 'Continue'; + $Block = $this->$methodName($Line, $CurrentBlock); + + if (isset($Block)) + { + $CurrentBlock = $Block; + + continue; + } + else + { + if ($this->isBlockCompletable($CurrentBlock['type'])) + { + $methodName = 'block' . $CurrentBlock['type'] . 'Complete'; + $CurrentBlock = $this->$methodName($CurrentBlock); + } + } + } + + # ~ + + $marker = $text[0]; + + # ~ + + $blockTypes = $this->unmarkedBlockTypes; + + if (isset($this->BlockTypes[$marker])) + { + foreach ($this->BlockTypes[$marker] as $blockType) + { + $blockTypes []= $blockType; + } + } + + # + # ~ + + foreach ($blockTypes as $blockType) + { + $Block = $this->{"block$blockType"}($Line, $CurrentBlock); + + if (isset($Block)) + { + $Block['type'] = $blockType; + + if ( ! isset($Block['identified'])) + { + if (isset($CurrentBlock)) + { + $Elements[] = $this->extractElement($CurrentBlock); + } + + $Block['identified'] = true; + } + + if ($this->isBlockContinuable($blockType)) + { + $Block['continuable'] = true; + } + + $CurrentBlock = $Block; + + continue 2; + } + } + + # ~ + + if (isset($CurrentBlock) and $CurrentBlock['type'] === 'Paragraph') + { + $Block = $this->paragraphContinue($Line, $CurrentBlock); + } + + if (isset($Block)) + { + $CurrentBlock = $Block; + } + else + { + if (isset($CurrentBlock)) + { + $Elements[] = $this->extractElement($CurrentBlock); + } + + $CurrentBlock = $this->paragraph($Line); + + $CurrentBlock['identified'] = true; + } + } + + # ~ + + if (isset($CurrentBlock['continuable']) and $this->isBlockCompletable($CurrentBlock['type'])) + { + $methodName = 'block' . $CurrentBlock['type'] . 'Complete'; + $CurrentBlock = $this->$methodName($CurrentBlock); + } + + # ~ + + if (isset($CurrentBlock)) + { + $Elements[] = $this->extractElement($CurrentBlock); + } + + # ~ + + return $Elements; + } + + protected function extractElement(array $Component) + { + if ( ! isset($Component['element'])) + { + if (isset($Component['markup'])) + { + $Component['element'] = array('rawHtml' => $Component['markup']); + } + elseif (isset($Component['hidden'])) + { + $Component['element'] = array(); + } + } + + return $Component['element']; + } + + protected function isBlockContinuable($Type) + { + return method_exists($this, 'block' . $Type . 'Continue'); + } + + protected function isBlockCompletable($Type) + { + return method_exists($this, 'block' . $Type . 'Complete'); + } + + # + # Code + + protected function blockCode($Line, $Block = null) + { + if (isset($Block) and $Block['type'] === 'Paragraph' and ! isset($Block['interrupted'])) + { + return; + } + + if ($Line['indent'] >= 4) + { + $text = substr($Line['body'], 4); + + $Block = array( + 'element' => array( + 'name' => 'pre', + 'element' => array( + 'name' => 'code', + 'text' => $text, + ), + ), + ); + + return $Block; + } + } + + protected function blockCodeContinue($Line, $Block) + { + if ($Line['indent'] >= 4) + { + if (isset($Block['interrupted'])) + { + $Block['element']['element']['text'] .= str_repeat("\n", $Block['interrupted']); + + unset($Block['interrupted']); + } + + $Block['element']['element']['text'] .= "\n"; + + $text = substr($Line['body'], 4); + + $Block['element']['element']['text'] .= $text; + + return $Block; + } + } + + protected function blockCodeComplete($Block) + { + return $Block; + } + + # + # Comment + + protected function blockComment($Line) + { + if ($this->markupEscaped or $this->safeMode) + { + return; + } + + if (strpos($Line['text'], '') !== false) + { + $Block['closed'] = true; + } + + return $Block; + } + } + + protected function blockCommentContinue($Line, array $Block) + { + if (isset($Block['closed'])) + { + return; + } + + $Block['element']['rawHtml'] .= "\n" . $Line['body']; + + if (strpos($Line['text'], '-->') !== false) + { + $Block['closed'] = true; + } + + return $Block; + } + + # + # Fenced Code + + protected function blockFencedCode($Line) + { + $marker = $Line['text'][0]; + + $openerLength = strspn($Line['text'], $marker); + + if ($openerLength < 3) + { + return; + } + + $infostring = trim(substr($Line['text'], $openerLength), "\t "); + + if (strpos($infostring, '`') !== false) + { + return; + } + + $Element = array( + 'name' => 'code', + 'text' => '', + ); + + if ($infostring !== '') + { + /** + * https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#classes + * Every HTML element may have a class attribute specified. + * The attribute, if specified, must have a value that is a set + * of space-separated tokens representing the various classes + * that the element belongs to. + * [...] + * The space characters, for the purposes of this specification, + * are U+0020 SPACE, U+0009 CHARACTER TABULATION (tab), + * U+000A LINE FEED (LF), U+000C FORM FEED (FF), and + * U+000D CARRIAGE RETURN (CR). + */ + $language = substr($infostring, 0, strcspn($infostring, " \t\n\f\r")); + + $Element['attributes'] = array('class' => "language-$language"); + } + + $Block = array( + 'char' => $marker, + 'openerLength' => $openerLength, + 'element' => array( + 'name' => 'pre', + 'element' => $Element, + ), + ); + + return $Block; + } + + protected function blockFencedCodeContinue($Line, $Block) + { + if (isset($Block['complete'])) + { + return; + } + + if (isset($Block['interrupted'])) + { + $Block['element']['element']['text'] .= str_repeat("\n", $Block['interrupted']); + + unset($Block['interrupted']); + } + + if (($len = strspn($Line['text'], $Block['char'])) >= $Block['openerLength'] + and chop(substr($Line['text'], $len), ' ') === '' + ) { + $Block['element']['element']['text'] = substr($Block['element']['element']['text'], 1); + + $Block['complete'] = true; + + return $Block; + } + + $Block['element']['element']['text'] .= "\n" . $Line['body']; + + return $Block; + } + + protected function blockFencedCodeComplete($Block) + { + return $Block; + } + + # + # Header + + protected function blockHeader($Line) + { + $level = strspn($Line['text'], '#'); + + if ($level > 6) + { + return; + } + + $text = trim($Line['text'], '#'); + + if ($this->strictMode and isset($text[0]) and $text[0] !== ' ') + { + return; + } + + $text = trim($text, ' '); + + $Block = array( + 'element' => array( + 'name' => 'h' . $level, + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $text, + 'destination' => 'elements', + ) + ), + ); + + return $Block; + } + + # + # List + + protected function blockList($Line, array $CurrentBlock = null) + { + list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]{1,9}+[.\)]'); + + if (preg_match('/^('.$pattern.'([ ]++|$))(.*+)/', $Line['text'], $matches)) + { + $contentIndent = strlen($matches[2]); + + if ($contentIndent >= 5) + { + $contentIndent -= 1; + $matches[1] = substr($matches[1], 0, -$contentIndent); + $matches[3] = str_repeat(' ', $contentIndent) . $matches[3]; + } + elseif ($contentIndent === 0) + { + $matches[1] .= ' '; + } + + $markerWithoutWhitespace = strstr($matches[1], ' ', true); + + $Block = array( + 'indent' => $Line['indent'], + 'pattern' => $pattern, + 'data' => array( + 'type' => $name, + 'marker' => $matches[1], + 'markerType' => ($name === 'ul' ? $markerWithoutWhitespace : substr($markerWithoutWhitespace, -1)), + ), + 'element' => array( + 'name' => $name, + 'elements' => array(), + ), + ); + $Block['data']['markerTypeRegex'] = preg_quote($Block['data']['markerType'], '/'); + + if ($name === 'ol') + { + $listStart = ltrim(strstr($matches[1], $Block['data']['markerType'], true), '0') ?: '0'; + + if ($listStart !== '1') + { + if ( + isset($CurrentBlock) + and $CurrentBlock['type'] === 'Paragraph' + and ! isset($CurrentBlock['interrupted']) + ) { + return; + } + + $Block['element']['attributes'] = array('start' => $listStart); + } + } + + $Block['li'] = array( + 'name' => 'li', + 'handler' => array( + 'function' => 'li', + 'argument' => !empty($matches[3]) ? array($matches[3]) : array(), + 'destination' => 'elements' + ) + ); + + $Block['element']['elements'] []= & $Block['li']; + + return $Block; + } + } + + protected function blockListContinue($Line, array $Block) + { + if (isset($Block['interrupted']) and empty($Block['li']['handler']['argument'])) + { + return null; + } + + $requiredIndent = ($Block['indent'] + strlen($Block['data']['marker'])); + + if ($Line['indent'] < $requiredIndent + and ( + ( + $Block['data']['type'] === 'ol' + and preg_match('/^[0-9]++'.$Block['data']['markerTypeRegex'].'(?:[ ]++(.*)|$)/', $Line['text'], $matches) + ) or ( + $Block['data']['type'] === 'ul' + and preg_match('/^'.$Block['data']['markerTypeRegex'].'(?:[ ]++(.*)|$)/', $Line['text'], $matches) + ) + ) + ) { + if (isset($Block['interrupted'])) + { + $Block['li']['handler']['argument'] []= ''; + + $Block['loose'] = true; + + unset($Block['interrupted']); + } + + unset($Block['li']); + + $text = isset($matches[1]) ? $matches[1] : ''; + + $Block['indent'] = $Line['indent']; + + $Block['li'] = array( + 'name' => 'li', + 'handler' => array( + 'function' => 'li', + 'argument' => array($text), + 'destination' => 'elements' + ) + ); + + $Block['element']['elements'] []= & $Block['li']; + + return $Block; + } + elseif ($Line['indent'] < $requiredIndent and $this->blockList($Line)) + { + return null; + } + + if ($Line['text'][0] === '[' and $this->blockReference($Line)) + { + return $Block; + } + + if ($Line['indent'] >= $requiredIndent) + { + if (isset($Block['interrupted'])) + { + $Block['li']['handler']['argument'] []= ''; + + $Block['loose'] = true; + + unset($Block['interrupted']); + } + + $text = substr($Line['body'], $requiredIndent); + + $Block['li']['handler']['argument'] []= $text; + + return $Block; + } + + if ( ! isset($Block['interrupted'])) + { + $text = preg_replace('/^[ ]{0,'.$requiredIndent.'}+/', '', $Line['body']); + + $Block['li']['handler']['argument'] []= $text; + + return $Block; + } + } + + protected function blockListComplete(array $Block) + { + if (isset($Block['loose'])) + { + foreach ($Block['element']['elements'] as &$li) + { + if (end($li['handler']['argument']) !== '') + { + $li['handler']['argument'] []= ''; + } + } + } + + return $Block; + } + + # + # Quote + + protected function blockQuote($Line) + { + if (preg_match('/^>[ ]?+(.*+)/', $Line['text'], $matches)) + { + $Block = array( + 'element' => array( + 'name' => 'blockquote', + 'handler' => array( + 'function' => 'linesElements', + 'argument' => (array) $matches[1], + 'destination' => 'elements', + ) + ), + ); + + return $Block; + } + } + + protected function blockQuoteContinue($Line, array $Block) + { + if (isset($Block['interrupted'])) + { + return; + } + + if ($Line['text'][0] === '>' and preg_match('/^>[ ]?+(.*+)/', $Line['text'], $matches)) + { + $Block['element']['handler']['argument'] []= $matches[1]; + + return $Block; + } + + if ( ! isset($Block['interrupted'])) + { + $Block['element']['handler']['argument'] []= $Line['text']; + + return $Block; + } + } + + # + # Rule + + protected function blockRule($Line) + { + $marker = $Line['text'][0]; + + if (substr_count($Line['text'], $marker) >= 3 and chop($Line['text'], " $marker") === '') + { + $Block = array( + 'element' => array( + 'name' => 'hr', + ), + ); + + return $Block; + } + } + + # + # Setext + + protected function blockSetextHeader($Line, array $Block = null) + { + if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted'])) + { + return; + } + + if ($Line['indent'] < 4 and chop(chop($Line['text'], ' '), $Line['text'][0]) === '') + { + $Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2'; + + return $Block; + } + } + + # + # Markup + + protected function blockMarkup($Line) + { + if ($this->markupEscaped or $this->safeMode) + { + return; + } + + if (preg_match('/^<[\/]?+(\w*)(?:[ ]*+'.$this->regexHtmlAttribute.')*+[ ]*+(\/)?>/', $Line['text'], $matches)) + { + $element = strtolower($matches[1]); + + if (in_array($element, $this->textLevelElements)) + { + return; + } + + $Block = array( + 'name' => $matches[1], + 'element' => array( + 'rawHtml' => $Line['text'], + 'autobreak' => true, + ), + ); + + return $Block; + } + } + + protected function blockMarkupContinue($Line, array $Block) + { + if (isset($Block['closed']) or isset($Block['interrupted'])) + { + return; + } + + $Block['element']['rawHtml'] .= "\n" . $Line['body']; + + return $Block; + } + + # + # Reference + + protected function blockReference($Line) + { + if (strpos($Line['text'], ']') !== false + and preg_match('/^\[(.+?)\]:[ ]*+?(?:[ ]+["\'(](.+)["\')])?[ ]*+$/', $Line['text'], $matches) + ) { + $id = strtolower($matches[1]); + + $Data = array( + 'url' => $matches[2], + 'title' => isset($matches[3]) ? $matches[3] : null, + ); + + $this->DefinitionData['Reference'][$id] = $Data; + + $Block = array( + 'element' => array(), + ); + + return $Block; + } + } + + # + # Table + + protected function blockTable($Line, array $Block = null) + { + if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted'])) + { + return; + } + + if ( + strpos($Block['element']['handler']['argument'], '|') === false + and strpos($Line['text'], '|') === false + and strpos($Line['text'], ':') === false + or strpos($Block['element']['handler']['argument'], "\n") !== false + ) { + return; + } + + if (chop($Line['text'], ' -:|') !== '') + { + return; + } + + $alignments = array(); + + $divider = $Line['text']; + + $divider = trim($divider); + $divider = trim($divider, '|'); + + $dividerCells = explode('|', $divider); + + foreach ($dividerCells as $dividerCell) + { + $dividerCell = trim($dividerCell); + + if ($dividerCell === '') + { + return; + } + + $alignment = null; + + if ($dividerCell[0] === ':') + { + $alignment = 'left'; + } + + if (substr($dividerCell, - 1) === ':') + { + $alignment = $alignment === 'left' ? 'center' : 'right'; + } + + $alignments []= $alignment; + } + + # ~ + + $HeaderElements = array(); + + $header = $Block['element']['handler']['argument']; + + $header = trim($header); + $header = trim($header, '|'); + + $headerCells = explode('|', $header); + + if (count($headerCells) !== count($alignments)) + { + return; + } + + foreach ($headerCells as $index => $headerCell) + { + $headerCell = trim($headerCell); + + $HeaderElement = array( + 'name' => 'th', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $headerCell, + 'destination' => 'elements', + ) + ); + + if (isset($alignments[$index])) + { + $alignment = $alignments[$index]; + + $HeaderElement['attributes'] = array( + 'style' => "text-align: $alignment;", + ); + } + + $HeaderElements []= $HeaderElement; + } + + # ~ + + $Block = array( + 'alignments' => $alignments, + 'identified' => true, + 'element' => array( + 'name' => 'table', + 'elements' => array(), + ), + ); + + $Block['element']['elements'] []= array( + 'name' => 'thead', + ); + + $Block['element']['elements'] []= array( + 'name' => 'tbody', + 'elements' => array(), + ); + + $Block['element']['elements'][0]['elements'] []= array( + 'name' => 'tr', + 'elements' => $HeaderElements, + ); + + return $Block; + } + + protected function blockTableContinue($Line, array $Block) + { + if (isset($Block['interrupted'])) + { + return; + } + + if (count($Block['alignments']) === 1 or $Line['text'][0] === '|' or strpos($Line['text'], '|')) + { + $Elements = array(); + + $row = $Line['text']; + + $row = trim($row); + $row = trim($row, '|'); + + preg_match_all('/(?:(\\\\[|])|[^|`]|`[^`]++`|`)++/', $row, $matches); + + $cells = array_slice($matches[0], 0, count($Block['alignments'])); + + foreach ($cells as $index => $cell) + { + $cell = trim($cell); + + $Element = array( + 'name' => 'td', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $cell, + 'destination' => 'elements', + ) + ); + + if (isset($Block['alignments'][$index])) + { + $Element['attributes'] = array( + 'style' => 'text-align: ' . $Block['alignments'][$index] . ';', + ); + } + + $Elements []= $Element; + } + + $Element = array( + 'name' => 'tr', + 'elements' => $Elements, + ); + + $Block['element']['elements'][1]['elements'] []= $Element; + + return $Block; + } + } + + # + # ~ + # + + protected function paragraph($Line) + { + return array( + 'type' => 'Paragraph', + 'element' => array( + 'name' => 'p', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $Line['text'], + 'destination' => 'elements', + ), + ), + ); + } + + protected function paragraphContinue($Line, array $Block) + { + if (isset($Block['interrupted'])) + { + return; + } + + $Block['element']['handler']['argument'] .= "\n".$Line['text']; + + return $Block; + } + + # + # Inline Elements + # + + protected $InlineTypes = array( + '!' => array('Image'), + '&' => array('SpecialCharacter'), + '*' => array('Emphasis'), + ':' => array('Url'), + '<' => array('UrlTag', 'EmailTag', 'Markup'), + '[' => array('Link'), + '_' => array('Emphasis'), + '`' => array('Code'), + '~' => array('Strikethrough'), + '\\' => array('EscapeSequence'), + ); + + # ~ + + protected $inlineMarkerList = '!*_&[:<`~\\'; + + # + # ~ + # + + public function line($text, $nonNestables = array()) + { + return $this->elements($this->lineElements($text, $nonNestables)); + } + + protected function lineElements($text, $nonNestables = array()) + { + # standardize line breaks + $text = str_replace(array("\r\n", "\r"), "\n", $text); + + $Elements = array(); + + $nonNestables = (empty($nonNestables) + ? array() + : array_combine($nonNestables, $nonNestables) + ); + + # $excerpt is based on the first occurrence of a marker + + while ($excerpt = strpbrk($text, $this->inlineMarkerList)) + { + $marker = $excerpt[0]; + + $markerPosition = strlen($text) - strlen($excerpt); + + $Excerpt = array('text' => $excerpt, 'context' => $text); + + foreach ($this->InlineTypes[$marker] as $inlineType) + { + # check to see if the current inline type is nestable in the current context + + if (isset($nonNestables[$inlineType])) + { + continue; + } + + $Inline = $this->{"inline$inlineType"}($Excerpt); + + if ( ! isset($Inline)) + { + continue; + } + + # makes sure that the inline belongs to "our" marker + + if (isset($Inline['position']) and $Inline['position'] > $markerPosition) + { + continue; + } + + # sets a default inline position + + if ( ! isset($Inline['position'])) + { + $Inline['position'] = $markerPosition; + } + + # cause the new element to 'inherit' our non nestables + + + $Inline['element']['nonNestables'] = isset($Inline['element']['nonNestables']) + ? array_merge($Inline['element']['nonNestables'], $nonNestables) + : $nonNestables + ; + + # the text that comes before the inline + $unmarkedText = substr($text, 0, $Inline['position']); + + # compile the unmarked text + $InlineText = $this->inlineText($unmarkedText); + $Elements[] = $InlineText['element']; + + # compile the inline + $Elements[] = $this->extractElement($Inline); + + # remove the examined text + $text = substr($text, $Inline['position'] + $Inline['extent']); + + continue 2; + } + + # the marker does not belong to an inline + + $unmarkedText = substr($text, 0, $markerPosition + 1); + + $InlineText = $this->inlineText($unmarkedText); + $Elements[] = $InlineText['element']; + + $text = substr($text, $markerPosition + 1); + } + + $InlineText = $this->inlineText($text); + $Elements[] = $InlineText['element']; + + foreach ($Elements as &$Element) + { + if ( ! isset($Element['autobreak'])) + { + $Element['autobreak'] = false; + } + } + + return $Elements; + } + + # + # ~ + # + + protected function inlineText($text) + { + $Inline = array( + 'extent' => strlen($text), + 'element' => array(), + ); + + $Inline['element']['elements'] = self::pregReplaceElements( + $this->breaksEnabled ? '/[ ]*+\n/' : '/(?:[ ]*+\\\\|[ ]{2,}+)\n/', + array( + array('name' => 'br'), + array('text' => "\n"), + ), + $text + ); + + return $Inline; + } + + protected function inlineCode($Excerpt) + { + $marker = $Excerpt['text'][0]; + + if (preg_match('/^(['.$marker.']++)[ ]*+(.+?)[ ]*+(? strlen($matches[0]), + 'element' => array( + 'name' => 'code', + 'text' => $text, + ), + ); + } + } + + protected function inlineEmailTag($Excerpt) + { + $hostnameLabel = '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?'; + + $commonMarkEmail = '[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]++@' + . $hostnameLabel . '(?:\.' . $hostnameLabel . ')*'; + + if (strpos($Excerpt['text'], '>') !== false + and preg_match("/^<((mailto:)?$commonMarkEmail)>/i", $Excerpt['text'], $matches) + ){ + $url = $matches[1]; + + if ( ! isset($matches[2])) + { + $url = "mailto:$url"; + } + + return array( + 'extent' => strlen($matches[0]), + 'element' => array( + 'name' => 'a', + 'text' => $matches[1], + 'attributes' => array( + 'href' => $url, + ), + ), + ); + } + } + + protected function inlineEmphasis($Excerpt) + { + if ( ! isset($Excerpt['text'][1])) + { + return; + } + + $marker = $Excerpt['text'][0]; + + if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches)) + { + $emphasis = 'strong'; + } + elseif (preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches)) + { + $emphasis = 'em'; + } + else + { + return; + } + + return array( + 'extent' => strlen($matches[0]), + 'element' => array( + 'name' => $emphasis, + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $matches[1], + 'destination' => 'elements', + ) + ), + ); + } + + protected function inlineEscapeSequence($Excerpt) + { + if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters)) + { + return array( + 'element' => array('rawHtml' => $Excerpt['text'][1]), + 'extent' => 2, + ); + } + } + + protected function inlineImage($Excerpt) + { + if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[') + { + return; + } + + $Excerpt['text']= substr($Excerpt['text'], 1); + + $Link = $this->inlineLink($Excerpt); + + if ($Link === null) + { + return; + } + + $Inline = array( + 'extent' => $Link['extent'] + 1, + 'element' => array( + 'name' => 'img', + 'attributes' => array( + 'src' => $Link['element']['attributes']['href'], + 'alt' => $Link['element']['handler']['argument'], + ), + 'autobreak' => true, + ), + ); + + $Inline['element']['attributes'] += $Link['element']['attributes']; + + unset($Inline['element']['attributes']['href']); + + return $Inline; + } + + protected function inlineLink($Excerpt) + { + $Element = array( + 'name' => 'a', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => null, + 'destination' => 'elements', + ), + 'nonNestables' => array('Url', 'Link'), + 'attributes' => array( + 'href' => null, + 'title' => null, + ), + ); + + $extent = 0; + + $remainder = $Excerpt['text']; + + if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches)) + { + $Element['handler']['argument'] = $matches[1]; + + $extent += strlen($matches[0]); + + $remainder = substr($remainder, $extent); + } + else + { + return; + } + + if (preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*+"|\'[^\']*+\'))?\s*+[)]/', $remainder, $matches)) + { + $Element['attributes']['href'] = $matches[1]; + + if (isset($matches[2])) + { + $Element['attributes']['title'] = substr($matches[2], 1, - 1); + } + + $extent += strlen($matches[0]); + } + else + { + if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches)) + { + $definition = strlen($matches[1]) ? $matches[1] : $Element['handler']['argument']; + $definition = strtolower($definition); + + $extent += strlen($matches[0]); + } + else + { + $definition = strtolower($Element['handler']['argument']); + } + + if ( ! isset($this->DefinitionData['Reference'][$definition])) + { + return; + } + + $Definition = $this->DefinitionData['Reference'][$definition]; + + $Element['attributes']['href'] = $Definition['url']; + $Element['attributes']['title'] = $Definition['title']; + } + + return array( + 'extent' => $extent, + 'element' => $Element, + ); + } + + protected function inlineMarkup($Excerpt) + { + if ($this->markupEscaped or $this->safeMode or strpos($Excerpt['text'], '>') === false) + { + return; + } + + if ($Excerpt['text'][1] === '/' and preg_match('/^<\/\w[\w-]*+[ ]*+>/s', $Excerpt['text'], $matches)) + { + return array( + 'element' => array('rawHtml' => $matches[0]), + 'extent' => strlen($matches[0]), + ); + } + + if ($Excerpt['text'][1] === '!' and preg_match('/^/s', $Excerpt['text'], $matches)) + { + return array( + 'element' => array('rawHtml' => $matches[0]), + 'extent' => strlen($matches[0]), + ); + } + + if ($Excerpt['text'][1] !== ' ' and preg_match('/^<\w[\w-]*+(?:[ ]*+'.$this->regexHtmlAttribute.')*+[ ]*+\/?>/s', $Excerpt['text'], $matches)) + { + return array( + 'element' => array('rawHtml' => $matches[0]), + 'extent' => strlen($matches[0]), + ); + } + } + + protected function inlineSpecialCharacter($Excerpt) + { + if (substr($Excerpt['text'], 1, 1) !== ' ' and strpos($Excerpt['text'], ';') !== false + and preg_match('/^&(#?+[0-9a-zA-Z]++);/', $Excerpt['text'], $matches) + ) { + return array( + 'element' => array('rawHtml' => '&' . $matches[1] . ';'), + 'extent' => strlen($matches[0]), + ); + } + + return; + } + + protected function inlineStrikethrough($Excerpt) + { + if ( ! isset($Excerpt['text'][1])) + { + return; + } + + if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches)) + { + return array( + 'extent' => strlen($matches[0]), + 'element' => array( + 'name' => 'del', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $matches[1], + 'destination' => 'elements', + ) + ), + ); + } + } + + protected function inlineUrl($Excerpt) + { + if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/') + { + return; + } + + if (strpos($Excerpt['context'], 'http') !== false + and preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE) + ) { + $url = $matches[0][0]; + + $Inline = array( + 'extent' => strlen($matches[0][0]), + 'position' => $matches[0][1], + 'element' => array( + 'name' => 'a', + 'text' => $url, + 'attributes' => array( + 'href' => $url, + ), + ), + ); + + return $Inline; + } + } + + protected function inlineUrlTag($Excerpt) + { + if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches)) + { + $url = $matches[1]; + + return array( + 'extent' => strlen($matches[0]), + 'element' => array( + 'name' => 'a', + 'text' => $url, + 'attributes' => array( + 'href' => $url, + ), + ), + ); + } + } + + # ~ + + protected function unmarkedText($text) + { + $Inline = $this->inlineText($text); + return $this->element($Inline['element']); + } + + # + # Handlers + # + + protected function handle(array $Element) + { + if (isset($Element['handler'])) + { + if (!isset($Element['nonNestables'])) + { + $Element['nonNestables'] = array(); + } + + if (is_string($Element['handler'])) + { + $function = $Element['handler']; + $argument = $Element['text']; + unset($Element['text']); + $destination = 'rawHtml'; + } + else + { + $function = $Element['handler']['function']; + $argument = $Element['handler']['argument']; + $destination = $Element['handler']['destination']; + } + + $Element[$destination] = $this->{$function}($argument, $Element['nonNestables']); + + if ($destination === 'handler') + { + $Element = $this->handle($Element); + } + + unset($Element['handler']); + } + + return $Element; + } + + protected function handleElementRecursive(array $Element) + { + return $this->elementApplyRecursive(array($this, 'handle'), $Element); + } + + protected function handleElementsRecursive(array $Elements) + { + return $this->elementsApplyRecursive(array($this, 'handle'), $Elements); + } + + protected function elementApplyRecursive($closure, array $Element) + { + $Element = call_user_func($closure, $Element); + + if (isset($Element['elements'])) + { + $Element['elements'] = $this->elementsApplyRecursive($closure, $Element['elements']); + } + elseif (isset($Element['element'])) + { + $Element['element'] = $this->elementApplyRecursive($closure, $Element['element']); + } + + return $Element; + } + + protected function elementApplyRecursiveDepthFirst($closure, array $Element) + { + if (isset($Element['elements'])) + { + $Element['elements'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['elements']); + } + elseif (isset($Element['element'])) + { + $Element['element'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['element']); + } + + $Element = call_user_func($closure, $Element); + + return $Element; + } + + protected function elementsApplyRecursive($closure, array $Elements) + { + foreach ($Elements as &$Element) + { + $Element = $this->elementApplyRecursive($closure, $Element); + } + + return $Elements; + } + + protected function elementsApplyRecursiveDepthFirst($closure, array $Elements) + { + foreach ($Elements as &$Element) + { + $Element = $this->elementApplyRecursiveDepthFirst($closure, $Element); + } + + return $Elements; + } + + protected function element(array $Element) + { + if ($this->safeMode) + { + $Element = $this->sanitiseElement($Element); + } + + # identity map if element has no handler + $Element = $this->handle($Element); + + $hasName = isset($Element['name']); + + $markup = ''; + + if ($hasName) + { + $markup .= '<' . $Element['name']; + + if (isset($Element['attributes'])) + { + foreach ($Element['attributes'] as $name => $value) + { + if ($value === null) + { + continue; + } + + $markup .= " $name=\"".self::escape($value).'"'; + } + } + } + + $permitRawHtml = false; + + if (isset($Element['text'])) + { + $text = $Element['text']; + } + // very strongly consider an alternative if you're writing an + // extension + elseif (isset($Element['rawHtml'])) + { + $text = $Element['rawHtml']; + + $allowRawHtmlInSafeMode = isset($Element['allowRawHtmlInSafeMode']) && $Element['allowRawHtmlInSafeMode']; + $permitRawHtml = !$this->safeMode || $allowRawHtmlInSafeMode; + } + + $hasContent = isset($text) || isset($Element['element']) || isset($Element['elements']); + + if ($hasContent) + { + $markup .= $hasName ? '>' : ''; + + if (isset($Element['elements'])) + { + $markup .= $this->elements($Element['elements']); + } + elseif (isset($Element['element'])) + { + $markup .= $this->element($Element['element']); + } + else + { + if (!$permitRawHtml) + { + $markup .= self::escape($text, true); + } + else + { + $markup .= $text; + } + } + + $markup .= $hasName ? '' : ''; + } + elseif ($hasName) + { + $markup .= ' />'; + } + + return $markup; + } + + protected function elements(array $Elements) + { + $markup = ''; + + $autoBreak = true; + + foreach ($Elements as $Element) + { + if (empty($Element)) + { + continue; + } + + $autoBreakNext = (isset($Element['autobreak']) + ? $Element['autobreak'] : isset($Element['name']) + ); + // (autobreak === false) covers both sides of an element + $autoBreak = !$autoBreak ? $autoBreak : $autoBreakNext; + + $markup .= ($autoBreak ? "\n" : '') . $this->element($Element); + $autoBreak = $autoBreakNext; + } + + $markup .= $autoBreak ? "\n" : ''; + + return $markup; + } + + # ~ + + protected function li($lines) + { + $Elements = $this->linesElements($lines); + + if ( ! in_array('', $lines) + and isset($Elements[0]) and isset($Elements[0]['name']) + and $Elements[0]['name'] === 'p' + ) { + unset($Elements[0]['name']); + } + + return $Elements; + } + + # + # AST Convenience + # + + /** + * Replace occurrences $regexp with $Elements in $text. Return an array of + * elements representing the replacement. + */ + protected static function pregReplaceElements($regexp, $Elements, $text) + { + $newElements = array(); + + while (preg_match($regexp, $text, $matches, PREG_OFFSET_CAPTURE)) + { + $offset = $matches[0][1]; + $before = substr($text, 0, $offset); + $after = substr($text, $offset + strlen($matches[0][0])); + + $newElements[] = array('text' => $before); + + foreach ($Elements as $Element) + { + $newElements[] = $Element; + } + + $text = $after; + } + + $newElements[] = array('text' => $text); + + return $newElements; + } + + # + # Deprecated Methods + # + + function parse($text) + { + $markup = $this->text($text); + + return $markup; + } + + protected function sanitiseElement(array $Element) + { + static $goodAttribute = '/^[a-zA-Z0-9][a-zA-Z0-9-_]*+$/'; + static $safeUrlNameToAtt = array( + 'a' => 'href', + 'img' => 'src', + ); + + if ( ! isset($Element['name'])) + { + unset($Element['attributes']); + return $Element; + } + + if (isset($safeUrlNameToAtt[$Element['name']])) + { + $Element = $this->filterUnsafeUrlInAttribute($Element, $safeUrlNameToAtt[$Element['name']]); + } + + if ( ! empty($Element['attributes'])) + { + foreach ($Element['attributes'] as $att => $val) + { + # filter out badly parsed attribute + if ( ! preg_match($goodAttribute, $att)) + { + unset($Element['attributes'][$att]); + } + # dump onevent attribute + elseif (self::striAtStart($att, 'on')) + { + unset($Element['attributes'][$att]); + } + } + } + + return $Element; + } + + protected function filterUnsafeUrlInAttribute(array $Element, $attribute) + { + foreach ($this->safeLinksWhitelist as $scheme) + { + if (self::striAtStart($Element['attributes'][$attribute], $scheme)) + { + return $Element; + } + } + + $Element['attributes'][$attribute] = str_replace(':', '%3A', $Element['attributes'][$attribute]); + + return $Element; + } + + # + # Static Methods + # + + protected static function escape($text, $allowQuotes = false) + { + return htmlspecialchars($text, $allowQuotes ? ENT_NOQUOTES : ENT_QUOTES, 'UTF-8'); + } + + protected static function striAtStart($string, $needle) + { + $len = strlen($needle); + + if ($len > strlen($string)) + { + return false; + } + else + { + return strtolower(substr($string, 0, $len)) === strtolower($needle); + } + } + + static function instance($name = 'default') + { + if (isset(self::$instances[$name])) + { + return self::$instances[$name]; + } + + $instance = new static(); + + self::$instances[$name] = $instance; + + return $instance; + } + + private static $instances = array(); + + # + # Fields + # + + protected $DefinitionData; + + # + # Read-Only + + protected $specialCharacters = array( + '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|', '~' + ); + + protected $StrongRegex = array( + '*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s', + '_' => '/^__((?:\\\\_|[^_]|_[^_]*+_)+?)__(?!_)/us', + ); + + protected $EmRegex = array( + '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s', + '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us', + ); + + protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*+(?:\s*+=\s*+(?:[^"\'=<>`\s]+|"[^"]*+"|\'[^\']*+\'))?+'; + + protected $voidElements = array( + 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', + ); + + protected $textLevelElements = array( + 'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont', + 'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing', + 'i', 'rp', 'del', 'code', 'strike', 'marquee', + 'q', 'rt', 'ins', 'font', 'strong', + 's', 'tt', 'kbd', 'mark', + 'u', 'xm', 'sub', 'nobr', + 'sup', 'ruby', + 'var', 'span', + 'wbr', 'time', + ); +} \ No newline at end of file diff --git a/src/models/Authentication.php b/src/classes/models/AuthModel.php similarity index 77% rename from src/models/Authentication.php rename to src/classes/models/AuthModel.php index e59b1e9..970f7ad 100644 --- a/src/models/Authentication.php +++ b/src/classes/models/AuthModel.php @@ -1,25 +1,25 @@ db->query('SELECT id, password FROM users WHERE username LIKE :uname;'); $this->db->bind(':uname', $uname); - $row = $this->db->single(); + $user = $this->db->single(); if($this->db->rowCount() <= 0) { return false; } - $passwordHash = $row->password; + if($user == null) return false; + + $passwordHash = $user->password; if(password_verify($passwd, $passwordHash)) { - return $row->id; + return $user->id; } else { return false; } } - + public function register($uname, $passwd) { $this->db->query('INSERT INTO users (username, password) VALUES (:username, :password);'); $this->db->bind(':username', $uname); @@ -28,4 +28,5 @@ public function register($uname, $passwd) { return true; } + } \ No newline at end of file diff --git a/src/classes/models/PostModel.php b/src/classes/models/PostModel.php new file mode 100644 index 0000000..026e1c7 --- /dev/null +++ b/src/classes/models/PostModel.php @@ -0,0 +1,117 @@ +db = new Database(); + $this->parsedown = new Parsedown(); + $this->parsedown->setSafeMode(true); + } + + + public function getPostsLimit($page = 1) { + $page = $page === null || !is_numeric($page) ? 1 : filter_var($page, FILTER_SANITIZE_NUMBER_INT); + + $count = $this->getFullCount(); + $maxPage = ceil($count / ITEMS_PER_PAGE); + $page = $page < 1 ? 1 : $page; // correct if page is under limit + $page = $page > $maxPage ? $maxPage : $page; // correct if page is over limit + $start = ($page * ITEMS_PER_PAGE) - ITEMS_PER_PAGE; + $elements = $page >= $maxPage ? $count % ITEMS_PER_PAGE : ITEMS_PER_PAGE; // in case the last page has not full count of elements + + $posts['meta'] = [ + 'page' => $page, + 'maxPage' => $maxPage, + 'count' => $count, + 'elements' => $elements + ]; + + $this->db->query('SELECT posts.id as pid, users.username, posts.title, posts.body, posts.date from posts INNER JOIN users ON users.id LIKE posts.user_fk ORDER BY posts.date DESC LIMIT :from, :to;'); + $this->db->bind(':from', $start); + $this->db->bind(':to', ITEMS_PER_PAGE); + $posts['posts'] = $this->db->resultSet(); + + foreach($posts['posts'] as $post) { + $post->body = strlen($post->body) > 400 ? substr($post->body, 0, 400) . '...' : $post->body; + $post->body = strip_tags($this->parsedown->text($post->body)); + $post->date = date('d.m.Y H:i', strtotime($post->date)); + } + return $posts; + } + + public function getPostByIdMd($id) { + + $post = $this->getPostById($id); + + $post->body = $this->parsedown->text($post->body); + + return $post; + } + + public function getPostById($id) { + $id = $id === null || !is_numeric($id) ? null : filter_var($id, FILTER_SANITIZE_NUMBER_INT); + if($id == null) return null; + + $this->db->query('SELECT posts.id as pid, users.username, posts.title, posts.body, posts.date FROM posts INNER JOIN users ON users.id LIKE posts.user_fk WHERE posts.id LIKE :id;'); + $this->db->bind(':id', $id); + $post = $this->db->single(); + + if($post == null) return null; + + $post->date = date('d.m.Y H:i', strtotime($post->date)); + + return $post; + } + + public function create($title, $body, $userId) { + $this->db->query('INSERT INTO posts (title, body, user_fk) VALUES (:title, :body, :userId);'); + $this->db->bind(':title', $title); + $this->db->bind(':body', $body); + $this->db->bind(':userId', $userId); + $this->db->execute(); + + return $this->db->lastId(); + } + + public function deleteAllPostsFromUserId($id) { + $this->db->query('DELETE FROM posts WHERE user_fk LIKE :id;'); + $this->db->bind(':id', $id); + $this->db->execute(); + + return true; + } + + public function update($title, $body, $id) { + $this->db->query('UPDATE posts SET title = :title, body = :body WHERE id LIKE :id'); + $this->db->bind(':title', $title); + $this->db->bind(':body', $body); + $this->db->bind(':id', $id); + $this->db->execute(); + + return true; + } + + public function existsId($id) { + $this->db->query('SELECT id FROM posts WHERE id LIKE :id;'); + $this->db->bind(':id', $id); + $this->db->execute(); + + return $this->db->rowCount() >= 1 ? true : false; + } + + public function delete($id) { + $this->db->query('DELETE FROM posts WHERE id LIKE :id;'); + $this->db->bind(':id', $id); + $this->db->execute(); + + return true; + } + + public function getFullCount() { + $this->db->query('SELECT COUNT(id) c FROM posts;'); + $count = $this->db->single(); + + return $count->c; + } + + } \ No newline at end of file diff --git a/src/models/User.php b/src/classes/models/UserModel.php similarity index 84% rename from src/models/User.php rename to src/classes/models/UserModel.php index 7dad324..9407abb 100644 --- a/src/models/User.php +++ b/src/classes/models/UserModel.php @@ -1,20 +1,18 @@ db->query('SELECT id, username FROM users'); $results = $this->db->resultSet(); return $this->db->rowCount() > 0 ? $results : null; } - + public function getUserById($id) { - $this->db->query('SELECT id, username, password FROM users WHERE id LIKE :id'); + $this->db->query('SELECT id, username, password FROM users WHERE id LIKE :id;'); $this->db->bind(':id', $id); - $results = $this->db->single(); + $user = $this->db->single(); - return $this->db->rowCount() > 0 ? $results : null; + return $user; } public function existsUname($uname) { @@ -41,6 +39,7 @@ public function delete($id) { return true; } + public function update($uname, $passwd, $id) { $this->db->query('UPDATE users SET username = :username, password = :password WHERE id LIKE :id;'); $this->db->bind(':username', $uname); diff --git a/src/controllers/Posts.php b/src/controllers/Posts.php deleted file mode 100644 index 5aa1671..0000000 --- a/src/controllers/Posts.php +++ /dev/null @@ -1,211 +0,0 @@ -postModel = $this->model('Post'); - } - - public function index($page = 1) { - $page = $page === null || !is_numeric($page) ? 1 : $page; - $page = filter_var($page, FILTER_SANITIZE_NUMBER_INT); - - $itemsPerPage = 5; - - $postsCount = $this->postModel->getPostsCount(); - $maxPage = ceil($postsCount / $itemsPerPage); - $page = $page < 1 ? 1 : $page; - - $start = ($page * $itemsPerPage) - $itemsPerPage; - - $posts = $this->postModel->getAllPostsWithLimit($start, $itemsPerPage); - $newestPostId = $this->postModel->getNewestPostId(); - - $postsData = array(); - if($posts == null) { - $postsData = null; - } else { - foreach($posts as $post) { - $tpost = array(); - $tpost['id'] = $post->id; - $tpost['title'] = $post->title; - $tpost['body'] = strlen($post->body) > 400 ? substr($post->body, 0, 400) . '...' : $post->body; - $tpost['date'] = date('m/d/y H:i', strtotime($post->date)); - $tpost['username'] = $post->username; - array_push($postsData, $tpost); - } - } - - $data = array( - 'view' => true, - 'postsData' => $postsData, - 'newestPostId' => $newestPostId, - 'postsCount' => $postsCount, - 'currentPage' => $page, - 'maxPage' => $maxPage - ); - - $this->view('post/index', $data); - } - - public function post($id, $oldPageIndex = 1) { - if($id === null || !is_numeric($id)) { - $post = null; - } - $id = filter_var($id, FILTER_SANITIZE_NUMBER_INT); - - $post = $this->postModel->getPostById($id); - $newestPostId = $this->postModel->getNewestPostId(); - - if($post == null) { - $postData = null; - } else { - $postData = array(); - $postData['id'] = $post->id; - $postData['title'] = $post->title; - $postData['body'] = nl2br($post->body); - $postData['date'] = date('m/d/y H:i', strtotime($post->date)); - $postData['username'] = $post->username; - } - - $data = array( - 'view' => false, - 'newestPostId' => $newestPostId, - 'post' => $postData, - 'backPath' => "posts/index/" . $oldPageIndex - ); - - $this->view('post/post', $data); - } - - public function create() { - - if(!isset($_SESSION['loggedIn'])) { - header('Location: ' . ROOT_PATH); - exit(); - } - - $errors = null; - $title = ''; - $body = ''; - - if(isset($_POST['submit'])) { - if(empty($_POST['title'])) { - $errors[] = "Field Title is empty."; - } else { - $title = $_POST['title']; - } - if(empty($_POST['body'])) { - $errors[] = "Field Text is empty."; - } else { - $body = $_POST['body']; - } - if($errors == null) { - $userId = $_SESSION['loggedIn']; - $title = htmlentities($title, ENT_QUOTES, "UTF-8"); - $body = htmlentities($body, ENT_QUOTES, "UTF-8"); - - $id = $this->postModel->create($title, $body, $userId); - header('Location: ' . ROOT_PATH . 'posts/post/' . $id); - exit(); - } - } - - $data = array( - 'title' => $title, - 'body' => $body, - 'errors' => $errors, - 'actionName' => 'Create', - 'backPath' => 'posts/index/', - ); - - $this->view('post/create', $data); - } - - public function edit($id, $oldPageIndex = 1) { - if(!isset($_SESSION['loggedIn'])) { - header('Location: ' . ROOT_PATH); - exit(); - } - if($id === null || !is_numeric($id)) { - header('Location: ' . ROOT_PATH); - exit(); - } - $id = filter_var($id, FILTER_SANITIZE_NUMBER_INT); - - $errors = null; - $title = ''; - $body = ''; - - $post = $this->postModel->getPostById($id); - - if($post == null) { - header('Location: ' . ROOT_PATH); - exit(); - } - - if(isset($_POST['submit'])) { - if(empty($_POST['title'])) { - $errors[] = "Field Title is empty."; - } else { - $title = $_POST['title']; - } - if(empty($_POST['body'])) { - $errors[] = "Field Text is empty."; - } else { - $body = $_POST['body']; - } - if($errors == null) { - $title = htmlentities($title, ENT_QUOTES, "UTF-8"); - $body = htmlentities($body, ENT_QUOTES, "UTF-8"); - $this->postModel->update($title, $body, $id); - header('Location: ' . ROOT_PATH . 'posts/post/' . $id); - exit(); - } - } - - $data = array( - 'errors' => $errors, - 'title' => $post->title, - 'body' => $post->body, - 'id' => $post->id, - 'backPath' => "posts/index/" . $oldPageIndex, - 'actionName' => 'Edit' - ); - - $this->view('post/edit', $data); - } - - public function delete($id, $oldPageIndex = 1) { - if(!isset($_SESSION['loggedIn'])) { - header('Location: ' . ROOT_PATH); - exit(); - } - if($id === null || !is_numeric($id)) { - header('Location: ' . ROOT_PATH); - exit(); - } - $id = filter_var($id, FILTER_SANITIZE_NUMBER_INT); - - if(!$this->postModel->existsId($id)) { - header('Location: ' . ROOT_PATH); - exit(); - } - - if(isset($_POST['sure'])) { - $this->postModel->delete($id); - header('Location: ' . ROOT_PATH); - } - - $data = array( - 'id' => $id, - 'backPath' => "posts/index/" . $oldPageIndex - ); - - $this->view('post/delete', $data); - } - } \ No newline at end of file diff --git a/src/models/Post.php b/src/models/Post.php deleted file mode 100644 index 6821f02..0000000 --- a/src/models/Post.php +++ /dev/null @@ -1,88 +0,0 @@ -db->query('SELECT users.username, posts.id, posts.title, posts.body, posts.date FROM users, posts WHERE users.id LIKE posts.userId AND posts.id LIKE :id;'); - $this->db->bind(':id', $id); - - $row = $this->db->single(); - - return $row; - } - - public function getAllPosts() { - $this->db->query('SELECT users.username, posts.id, posts.title, posts.body, posts.date FROM users, posts WHERE users.id LIKE posts.userId ORDER BY posts.date DESC;'); - $results = $this->db->resultSet(); - - return $this->db->rowCount() > 0 ? $results : null; - } - - public function getAllPostsWithLimit($from, $to) { - $this->db->query('SELECT users.username, posts.id, posts.title, posts.body, posts.date FROM users, posts WHERE users.id LIKE posts.userId ORDER BY posts.date DESC LIMIT :from, :to ;'); - $this->db->bind(':from', $from); - $this->db->bind(':to', $to); - $results = $this->db->resultSet(); - - return $this->db->rowCount() > 0 ? $results : null; - } - - public function getPostsCount() { - $this->db->query('SELECT COUNT(*) as count FROM posts;'); - $count = $this->db->single(); - - return $count->count; - } - - public function getNewestPostId() { - $this->db->query('SELECT id FROM posts ORDER BY date DESC LIMIT 1;'); - $result = $this->db->single(); - - return $this->db->rowCount() > 0 ? $result->id : null; - } - - public function existsId($id) { - $this->db->query('SELECT id FROM posts WHERE id LIKE :id;'); - $this->db->bind(':id', $id); - $this->db->execute(); - - return $this->db->rowCount() >= 1 ? true : false; - } - - public function delete($id) { - $this->db->query('DELETE FROM posts WHERE id LIKE :id;'); - $this->db->bind(':id', $id); - $this->db->execute(); - - return true; - } - - public function deleteAllPostsFromUserId($id) { - $this->db->query('DELETE FROM posts WHERE userId LIKE :id;'); - $this->db->bind(':id', $id); - $this->db->execute(); - - return true; - } - - public function create($title, $body, $userId) { - $this->db->query('INSERT INTO posts (title, body, userId) VALUES (:title, :body, :userId);'); - $this->db->bind(':title', $title); - $this->db->bind(':body', $body); - $this->db->bind(':userId', $userId); - $this->db->execute(); - - return $this->db->lastId(); - } - - public function update($title, $body, $id) { - $this->db->query('UPDATE posts SET title = :title, body = :body WHERE id LIKE :id'); - $this->db->bind(':title', $title); - $this->db->bind(':body', $body); - $this->db->bind(':id', $id); - $this->db->execute(); - - return true; - } - } \ No newline at end of file diff --git a/src/templates/alert.php b/src/templates/alert.php new file mode 100644 index 0000000..d96d81c --- /dev/null +++ b/src/templates/alert.php @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/src/templates/backBtn.php b/src/templates/backBtn.php index 7488c8b..abdf488 100644 --- a/src/templates/backBtn.php +++ b/src/templates/backBtn.php @@ -1,3 +1,3 @@ - + \ No newline at end of file diff --git a/src/templates/deleteQuestion.php b/src/templates/delete.php similarity index 100% rename from src/templates/deleteQuestion.php rename to src/templates/delete.php diff --git a/src/templates/errorAlert.php b/src/templates/errorAlert.php deleted file mode 100644 index acff736..0000000 --- a/src/templates/errorAlert.php +++ /dev/null @@ -1,7 +0,0 @@ - - - \ No newline at end of file diff --git a/src/templates/footer.php b/src/templates/footer.php index 6988a54..43c6eb4 100644 --- a/src/templates/footer.php +++ b/src/templates/footer.php @@ -1,10 +1,20 @@ - - \ No newline at end of file diff --git a/src/templates/header.php b/src/templates/header.php index 791d5c1..22f78e4 100644 --- a/src/templates/header.php +++ b/src/templates/header.php @@ -1,35 +1,57 @@ + - - - - - - - + + + + - <?= SITE_NAME;?> - - - -
-
\ No newline at end of file + + +
+
+
\ No newline at end of file diff --git a/src/templates/pagination.php b/src/templates/pagination.php index 561ada2..15c19f3 100644 --- a/src/templates/pagination.php +++ b/src/templates/pagination.php @@ -1,23 +1,37 @@ - +
+ - \ No newline at end of file + +
\ No newline at end of file diff --git a/src/templates/post.php b/src/templates/post.php new file mode 100644 index 0000000..80ca521 --- /dev/null +++ b/src/templates/post.php @@ -0,0 +1,16 @@ +
+
+
title;?>
+

body;?>

+ + Read more + + + Edit + Delete + +
+ +
\ No newline at end of file diff --git a/src/templates/postCard.php b/src/templates/postCard.php deleted file mode 100644 index 20b950a..0000000 --- a/src/templates/postCard.php +++ /dev/null @@ -1,21 +0,0 @@ -
- -
- Newest -
- -
-
-

- - View - - - Edit - Delete - -
- -
\ No newline at end of file diff --git a/src/templates/postForm.php b/src/templates/postForm.php index 2f1cc51..bfc9838 100644 --- a/src/templates/postForm.php +++ b/src/templates/postForm.php @@ -1,19 +1,16 @@ -
-
-
-
-

Post

- $errors));?> -
- - -
-
- - -
- -
+ +
+
+

Post

+ 'danger', 'msg' => $errors]);?> +
+ +
-
-
\ No newline at end of file +
+ + +
+ + +
diff --git a/src/templates/postsList.php b/src/templates/postsList.php new file mode 100644 index 0000000..a38f2e0 --- /dev/null +++ b/src/templates/postsList.php @@ -0,0 +1,13 @@ + + +
+ + ADD +
+ + of posts on page of + + $post, 'view' => true, 'page' => $meta['page']]);?> + + + 'danger', 'msg' => 'No posts found!']);?> + \ No newline at end of file diff --git a/src/templates/templateNotFound.php b/src/templates/templateNotFound.php deleted file mode 100644 index 8b2adc5..0000000 --- a/src/templates/templateNotFound.php +++ /dev/null @@ -1 +0,0 @@ -Module not found \ No newline at end of file diff --git a/src/templates/userForm.php b/src/templates/userForm.php index 1c70f21..3be1693 100644 --- a/src/templates/userForm.php +++ b/src/templates/userForm.php @@ -1,19 +1,21 @@ -
-
-
-
-

- $errors));?> -
- - -
-
- - -
- -
+
+
+
-
\ No newline at end of file diff --git a/src/views/auth/login.php b/src/views/auth/login.php new file mode 100644 index 0000000..b3fe90f --- /dev/null +++ b/src/views/auth/login.php @@ -0,0 +1,5 @@ + 'Login']);?> + + $data['username'], 'msg' => $data['msg'], 'action' => 'Login']);?> + + \ No newline at end of file diff --git a/src/views/auth/register.php b/src/views/auth/register.php new file mode 100644 index 0000000..25ad3af --- /dev/null +++ b/src/views/auth/register.php @@ -0,0 +1,5 @@ + 'Register']);?> + + $data['username'], 'msg' => $data['msg'], 'action' => 'Register']);?> + + \ No newline at end of file diff --git a/src/views/authentication/login.php b/src/views/authentication/login.php deleted file mode 100644 index 1f43443..0000000 --- a/src/views/authentication/login.php +++ /dev/null @@ -1,3 +0,0 @@ - - $data['actionName'], 'username' => $data['username'], 'errors' => $data['errors']));?> - \ No newline at end of file diff --git a/src/views/authentication/register.php b/src/views/authentication/register.php deleted file mode 100644 index 44253ec..0000000 --- a/src/views/authentication/register.php +++ /dev/null @@ -1,4 +0,0 @@ - - $data['backPath']));?> - $data['actionName'], 'username' => $data['username'], 'errors' => $data['errors']));?> - \ No newline at end of file diff --git a/src/views/error/404.php b/src/views/error/404.php deleted file mode 100644 index 3dfc78e..0000000 --- a/src/views/error/404.php +++ /dev/null @@ -1 +0,0 @@ -Error 404, Page not found \ No newline at end of file diff --git a/src/views/post/create.php b/src/views/post/create.php deleted file mode 100644 index 5c8dc7a..0000000 --- a/src/views/post/create.php +++ /dev/null @@ -1,4 +0,0 @@ - - $data['backPath']));?> - $data['title'], 'body' => $data['body'], 'actionName' => $data['actionName'], 'errors' => $data['errors']));?> - \ No newline at end of file diff --git a/src/views/post/delete.php b/src/views/post/delete.php deleted file mode 100644 index 70a963b..0000000 --- a/src/views/post/delete.php +++ /dev/null @@ -1,4 +0,0 @@ - - $data['backPath']));?> - 'Post', 'id' => $data['id']));?> - \ No newline at end of file diff --git a/src/views/post/edit.php b/src/views/post/edit.php deleted file mode 100644 index 5c8dc7a..0000000 --- a/src/views/post/edit.php +++ /dev/null @@ -1,4 +0,0 @@ - - $data['backPath']));?> - $data['title'], 'body' => $data['body'], 'actionName' => $data['actionName'], 'errors' => $data['errors']));?> - \ No newline at end of file diff --git a/src/views/post/index.php b/src/views/post/index.php deleted file mode 100644 index 7a34e8e..0000000 --- a/src/views/post/index.php +++ /dev/null @@ -1,17 +0,0 @@ - - - Create - - - - - - - $post['id'], 'currentPage' => $data['currentPage'], 'body' => $post['body'], 'title' => $post['title'], 'newestPostId' => $data['newestPostId'], 'date' => $post['date'], 'username' => $post['username'], 'view' => $data['view']));?> - - - - $data['currentPage'], 'maxPage' => $data['maxPage']));?> - \ No newline at end of file diff --git a/src/views/post/post.php b/src/views/post/post.php deleted file mode 100644 index 7ff3de0..0000000 --- a/src/views/post/post.php +++ /dev/null @@ -1,12 +0,0 @@ - - $data['backPath']));?> - - - - - $post['id'], 'currentPage' => '', 'body' => $post['body'], 'title' => $post['title'], 'newestPostId' => $data['newestPostId'], 'date' => $post['date'], 'username' => $post['username'], 'view' => $data['view']));?> - - - \ No newline at end of file diff --git a/src/views/posts/create.php b/src/views/posts/create.php new file mode 100644 index 0000000..9f45312 --- /dev/null +++ b/src/views/posts/create.php @@ -0,0 +1,6 @@ + 'Home']);?> + + 'posts', 'method' => 'index', 'args' => ''])?> + $data['title'], 'body' => $data['body'], 'actionName' => $data['actionName'], 'errors' => $data['errors']));?> + + \ No newline at end of file diff --git a/src/views/posts/delete.php b/src/views/posts/delete.php new file mode 100644 index 0000000..3b28508 --- /dev/null +++ b/src/views/posts/delete.php @@ -0,0 +1,6 @@ + 'Home']);?> + + 'posts', 'method' => 'index', 'args' => ''])?> + 'Post', 'id' => $data['id']]);?> + + \ No newline at end of file diff --git a/src/views/posts/edit.php b/src/views/posts/edit.php new file mode 100644 index 0000000..fccb9c0 --- /dev/null +++ b/src/views/posts/edit.php @@ -0,0 +1,6 @@ + 'Home']);?> + + 'posts', 'method' => 'index', 'args' => ''])?> + $data['title'], 'body' => $data['body'], 'actionName' => $data['actionName'], 'errors' => $data['errors']));?> + + \ No newline at end of file diff --git a/src/views/posts/index.php b/src/views/posts/index.php new file mode 100644 index 0000000..44b94c1 --- /dev/null +++ b/src/views/posts/index.php @@ -0,0 +1,6 @@ + 'Home']);?> + + $data['posts']['posts'], 'meta' => $data['posts']['meta']]);?> + $data['posts']['meta'], 'controller' => 'posts', 'method' => 'index']);?> + + \ No newline at end of file diff --git a/src/views/posts/post.php b/src/views/posts/post.php new file mode 100644 index 0000000..8a92245 --- /dev/null +++ b/src/views/posts/post.php @@ -0,0 +1,10 @@ + $data['post'] != null ? $data['post']->title : 'post not found']);?> + + 'posts', 'method' => 'index', 'args' => $data['page']])?> + + $data['post'], 'view' => false]);?> + + 'danger', 'msg' => ['Post not found']]);?> + + + \ No newline at end of file diff --git a/src/views/user/delete.php b/src/views/user/delete.php deleted file mode 100644 index 7dd222c..0000000 --- a/src/views/user/delete.php +++ /dev/null @@ -1,4 +0,0 @@ - - $data['backPath']));?> - 'User', 'id' => $data['id']));?> - \ No newline at end of file diff --git a/src/views/user/edit.php b/src/views/user/edit.php deleted file mode 100644 index 44253ec..0000000 --- a/src/views/user/edit.php +++ /dev/null @@ -1,4 +0,0 @@ - - $data['backPath']));?> - $data['actionName'], 'username' => $data['username'], 'errors' => $data['errors']));?> - \ No newline at end of file diff --git a/src/views/users/delete.php b/src/views/users/delete.php new file mode 100644 index 0000000..4da11db --- /dev/null +++ b/src/views/users/delete.php @@ -0,0 +1,6 @@ + 'Home']);?> + + 'users', 'method' => 'index', 'args' => ''])?> + 'User', 'id' => $data['id']]);?> + + \ No newline at end of file diff --git a/src/views/users/edit.php b/src/views/users/edit.php new file mode 100644 index 0000000..663ae46 --- /dev/null +++ b/src/views/users/edit.php @@ -0,0 +1,5 @@ + 'Edit']);?> + + $data['username'], 'msg' => $data['msg'], 'action' => 'Edit']);?> + + \ No newline at end of file diff --git a/src/views/user/users.php b/src/views/users/index.php similarity index 81% rename from src/views/user/users.php rename to src/views/users/index.php index 15b47c8..551dcd6 100644 --- a/src/views/user/users.php +++ b/src/views/users/index.php @@ -1,13 +1,13 @@ - + 'Users']);?> -Register ++ Register - +
@@ -33,4 +33,4 @@
#
- \ No newline at end of file + \ No newline at end of file