diff --git a/include/class-App.php b/include/class-App.php index eafa9ad..72cfd12 100644 --- a/include/class-App.php +++ b/include/class-App.php @@ -1,31 +1,47 @@ . + /** * Methods of an App class */ trait AppTrait { /** * Get the App ID * * @return int */ public function getAppID() { return $this->get( 'app_ID' ); } /** * Get the App name * * @return string */ public function getAppName() { return $this->get( 'app_name' ); } } /** * Class that can wrap an App retrieved from the database */ class App { } diff --git a/include/class-Page.php b/include/class-Page.php index de5f0b2..789a473 100644 --- a/include/class-Page.php +++ b/include/class-Page.php @@ -1,71 +1,87 @@ . + /** * A website page * * It's useful to print the header, the footer, and who knows */ class Page { /** * Custom arguments * * @var array */ protected $args; /** * Constructor * * @param array $args Custom arguments */ public function __construct( $args = [] ) { $this->args = $args; $this->prepare(); } /** * Set a page argument * * @param $name string * @param $value mixed * @return self */ protected function setArg( $name, $value ) { $this->args[ $name ] = $value; return $this; } /** * Set the page title * * @param $title string * @return self */ protected function setTitle( $title ) { return $this->setArg( 'title', $title ); } /** * Do something at startup */ protected function prepare() { // actually nothing. You can override this! } /** * Print the site header */ public function printHeader() { template( 'header', $this->args ); } /** * Print the site footer */ public function printFooter() { template( 'footer', $this->args ); } } diff --git a/include/class-PageHome.php b/include/class-PageHome.php index 642ce12..a51c9c1 100644 --- a/include/class-PageHome.php +++ b/include/class-PageHome.php @@ -1,45 +1,61 @@ . + /** * Homepage of my website */ class PageHome extends Page { /** * User requested by ID */ public $requestedUser = false; /** * Do something at startup * * @override */ protected function prepare() { // read the ID from the query string or zero $id = $_GET['id'] ?? 0; // make sure that it's an integer $id = (int) $id; // if it's non-zero if( $id ) { // query the User with that ID (or get NULL) $this->requestedUser = ( new QueryUser() ) ->whereUserID( $id ) ->queryRow(); } } /** * Get the requested User (if any!) * * @return User|null */ public function getRequestedUser() { return $this->requestedUser; } } diff --git a/include/class-PageLogin.php b/include/class-PageLogin.php index 14a0ffa..b69f63e 100644 --- a/include/class-PageLogin.php +++ b/include/class-PageLogin.php @@ -1,48 +1,64 @@ . + /** * Login page of my website */ class PageLogin extends Page { /** * Do something at startup * * @override */ protected function prepare() { // set the page title $this->setTitle( __( "Login" ) ); // check if we should do the login if( $this->isLoginFormSubmitted() ) { // process the user_uid and the user_password sent via POST login(); } // eventually go to the homepage if logged-in if( is_logged() ) { http_redirect( '', 303 ); } } /** * Check if the login form was submitted * * @return boolean */ public function isLoginFormSubmitted() { return is_action( 'do-login' ); } /** * Check if the login failed * * @return boolean */ public function isLoginFormFailed() { return $this->isLoginFormSubmitted() && !is_logged(); } } diff --git a/include/class-QueryApp.php b/include/class-QueryApp.php index 9baf7a5..4ba6b93 100644 --- a/include/class-QueryApp.php +++ b/include/class-QueryApp.php @@ -1,40 +1,56 @@ . + /** * Methods for a QueryApp class */ trait QueryAppTrait { /** * Join a table with the App table * * @return */ public function joinApp( $type = 'INNER' ) { return $this->joinOn( $type, 'app', 'app.app_ID', $this->APP_ID ); } } /** * Execute a Query against an App */ class QueryApp extends Query { use QueryAppTrait; /** * Constructor * * @param object $db Database connection * @param string $class_name Default class name for the results */ public function __construct( $db = null, $class_name = 'App' ) { // set the database connection and the default class name for the results parent::__construct( $db, $class_name ); // set FROM table $this->from( 'app' ); } } diff --git a/include/class-User.php b/include/class-User.php index 5f821e4..448198e 100644 --- a/include/class-User.php +++ b/include/class-User.php @@ -1,48 +1,64 @@ . + /** * Class that can wrap an User retrieved from the database */ class User extends Sessionuser { /** * Constructor */ public function __construct() { parent::__construct(); } /** * Get the User name * * @return string */ public function getUserName() { return $this->get( 'user_name' ); } /** * Get the User ID * * @return int */ public function getUserID() { return $this->getSessionuserID(); } /** * Check if the user is me * * @return boolean */ public function isUserMe() { // if I'm logged, compare the IDs if( is_logged() ) { return $this->getUserID() === get_user()->getUserID(); } return false; } } diff --git a/include/functions.php b/include/functions.php index a188f15..672ee1c 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1,37 +1,53 @@ . + // some functions you may want to have /** * Require a certain page from the template directory * * @param $name string Template name * @param $args array Arguments to be passed to the template scope */ function template( $name, $args = [] ) { // extract the variables from the provided array extract( $args, EXTR_SKIP ); require ABSPATH . "/template/$name.php"; } /** * Require a specific permission * * @param string $permission */ function require_permission( $permission ) { if( !has_permission( $permission ) ) { if( is_logged() ) { http_response_code( 403 ); die( "ARE YOU A PIRATE?" ); } $url = http_build_get_query( 'login.php', [ 'redirect' => $_SERVER['REQUEST_URI'], ] ); http_redirect( $url, 303 ); // HTTP 303 redirect: See Other } } diff --git a/www/index.php b/www/index.php index 78a6485..1e1b3b0 100644 --- a/www/index.php +++ b/www/index.php @@ -1,19 +1,35 @@ . + // load everything require '../load.php'; // this is the homepage (the business logic of this page it's in this class) $page = new PageHome( [ 'title' => __( "Welcome!" ), ] ); // I want this stylesheet enqueue_js( 'my-style'); // print site header $page->printHeader(); ?> printFooter(); diff --git a/www/login.php b/www/login.php index 6e5bb2a..75079a2 100644 --- a/www/login.php +++ b/www/login.php @@ -1,44 +1,60 @@ . + // load everything require '../load.php'; // this is the homepage (the business logic of this page it's in this class) $page = new PageLogin(); // print site header $page->printHeader(); ?> isLoginFormSubmitted() ): ?>
= __( "Login failed!" ) ?>