From 1fd648bc4a8e17cde3708e14adcdb7c031f6f74a Mon Sep 17 00:00:00 2001 From: Tobias Hopp Date: Fri, 2 Oct 2020 08:06:04 +0200 Subject: [PATCH] Initial commit --- .idea/codeStyles/codeStyleConfig.xml | 5 ++ .idea/modules.xml | 8 ++ .idea/online-faq.iml | 8 ++ .idea/vcs.xml | 6 ++ LICENSE | 21 +++++ README.md | 74 +++++++++++++++++- SECURITY.md | 18 +++++ classes/Autoloader.class.inc.php | 79 +++++++++++++++++++ classes/Database.class.inc.php | 35 +++++++++ classes/Factory.class.inc.php | 65 ++++++++++++++++ classes/Request.class.inc.php | 56 ++++++++++++++ controller/DefaultController.class.inc.php | 29 +++++++ index.php | 26 +++++++ main.inc.php | 29 +++++++ model/DefaultModel.class.inc.php | 10 +++ templates/test.tmpl.html | 10 +++ views/Template.class.inc.php | 89 ++++++++++++++++++++++ 17 files changed, 567 insertions(+), 1 deletion(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/online-faq.iml create mode 100644 .idea/vcs.xml create mode 100644 LICENSE create mode 100644 SECURITY.md create mode 100644 classes/Autoloader.class.inc.php create mode 100644 classes/Database.class.inc.php create mode 100644 classes/Factory.class.inc.php create mode 100644 classes/Request.class.inc.php create mode 100644 controller/DefaultController.class.inc.php create mode 100644 index.php create mode 100644 main.inc.php create mode 100644 model/DefaultModel.class.inc.php create mode 100644 templates/test.tmpl.html create mode 100644 views/Template.class.inc.php diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c628b69 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/online-faq.iml b/.idea/online-faq.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/online-faq.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..34584c0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Tobias Hopp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 3743302..e8406d0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,74 @@ -# online-faq +# Model View Controller principle for PHP + +This MVC is a **M**odel **V**iew **C**ontroller "principle" for PHP 7. +It forms PHP to an object-oriented programming language. + +## Getting Started + +Download the latest version of our project under releases and extract the ZIP file into your project folder. + +Have a look around first. +The following files/folders are important: +- config.inc.php +- index.php +* controller/ +* model/ +* templates/ +* views/ + +Under views/ you can find the current default template loader. +It replaces the following string in an HTML document: "[[KEY]]". + +Templates can be found in the templates/ folder. +For example, if you don't want to use HTML as output, you can create your own view or edit the existing one instead. + +Under controller/ is the main part. +There all models are addressed and actions are executed. +As a little thought support, there should already be a sample controller under controller/. + +The model/ folder is used to execute certain actions, e.g. a MySQL query. It does not "think" itself. + +The index file actually does the least. +It should be edited as once as possible and then everything should only be done with controllers and models. + +The config.inc.php is used for the central storage of e.g. user data like MySQL data. Let your imagination run wild ^^. + + +### Prerequisites + +Please use at least a PHP version of 7.0. +Only PHP7 should be installed. + +### Installing + +To download the project, go to the [releases](https://git.gaminggeneration.de/Tobstr_/modelviewcontroller-php/-/releases). and download the latest ZIP file. + +Now unzipping all files into your project directory. +And that's it! +Have fun with tinkering. + +**PS:** Yeah, it's it's *normally* that you enjoy an error when you run it the first time without any configuration! +It's because the connection to the MySQL server fails. + +## Versioning + +We use normal version names like v1.0. +Just download the newest ZIP. +To find out if the version you want to download is still supported, you can view [Security.md](https://github.com/Tobstr02/modelviewcontroller-php/blob/master/SECURITY.md) here. + +The following PHP versions have been tested and run without problems under the version: +- *7.0* +- 7.1 +- 7.2 +- 7.3 +- **7.4** + +## Documentation + +To view some documentation about this PHP-MVC, visit the [Wiki](https://git.gaminggeneration.de/Tobstr_/modelviewcontroller-php/-/wikis/home) page of this repository. + + +## License + +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..805003c --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,18 @@ +# Security Policy + +## Supported Versions +We are currently maintaining the following versions: + +| Version | Supported | +| ------- | ------------------ | +| v1.0 | :white_check_mark: | +| v1.1 | :ballot_box_with_check: | + +## Supported & Tested PHP-Versions: +- 7.0 +- 7.1 +- 7.2 +- 7.3 + + +PHP versions below 7.0 get errors because of unique type assignments. diff --git a/classes/Autoloader.class.inc.php b/classes/Autoloader.class.inc.php new file mode 100644 index 0000000..dd9ffbb --- /dev/null +++ b/classes/Autoloader.class.inc.php @@ -0,0 +1,79 @@ +getVar( 'controller' ) ); + $controller_name = ucfirst( $controller_name ); + $controller_name .= 'Controller'; + + if ( $controller_name === 'Controller') + { + $controller = new DefaultController(); + + return $controller; + } + + # if controller doesn't exist + if ( !file_exists( __DIR__ . '/../controller/' . $controller_name . '.class.inc.php' ) ) + { + return new DefaultController(); + } + + # Call controller and returns it + $controller = new $controller_name(); + + return $controller; + + } # function getController(...) + + /** + * Returns the right action + * @param $a_controller + * @param Request $a_request Request + * @return string + */ + public static function getAction( $a_controller, Request $a_request ) + { + # Prepare name of the action + $action_name = mb_strtolower( $a_request->getVar('action') ); + $action_name = str_ireplace( 'action', '', $action_name ); + $action_name .= 'Action'; + + # Action not found + if ( !method_exists( $a_controller, $action_name ) ) + { + return 'indexAction'; + } + return $action_name; + + } # function getAction(...) + +} # class diff --git a/classes/Request.class.inc.php b/classes/Request.class.inc.php new file mode 100644 index 0000000..f1ddc25 --- /dev/null +++ b/classes/Request.class.inc.php @@ -0,0 +1,56 @@ +getTestMessage(); + + $view->set_placeholder( 'test', $value ); + + // Return the HTML Code to the index.php + return $view->getHtml(); + + } # function indexAction() + + +} # class diff --git a/index.php b/index.php new file mode 100644 index 0000000..5b9c7e4 --- /dev/null +++ b/index.php @@ -0,0 +1,26 @@ +$action(); + +} +catch ( Exception $e ) +{ + # Please replace it with error template + die( 'An internal error occured while processing your request.' ); +} diff --git a/main.inc.php b/main.inc.php new file mode 100644 index 0000000..a4fb7a7 --- /dev/null +++ b/main.inc.php @@ -0,0 +1,29 @@ + PDO::ERRMODE_EXCEPTION ) ); + + # Set DB connection + Database::setConnection( $pdo ); +} + + +catch ( Exception $e ) +{ + # Error occured, please replace it with error template + die( 'An internal error occured while proccessing your request' ); +} diff --git a/model/DefaultModel.class.inc.php b/model/DefaultModel.class.inc.php new file mode 100644 index 0000000..87e51b5 --- /dev/null +++ b/model/DefaultModel.class.inc.php @@ -0,0 +1,10 @@ + + + + + Test + + + [[TEST]] + + \ No newline at end of file diff --git a/views/Template.class.inc.php b/views/Template.class.inc.php new file mode 100644 index 0000000..70947b3 --- /dev/null +++ b/views/Template.class.inc.php @@ -0,0 +1,89 @@ +template = file_get_contents( $path ); + + } # function __construct(...) + + /** + * Replaces a placeholder with a string + * + * @param string $a_key Key of the placeholder + * @param string $a_value Replace this with the key + * + * @throws Exception Wirft Exception + * @return void + * + */ + public function set_placeholder( $a_key, $a_value ) + { + if ( empty( $a_value ) ) + { + $a_value = '-'; + } + $a_key = mb_strtoupper( $a_key ); + $key = '[[' . $a_key . ']]'; + + $this->template = str_replace( $key, $a_value, $this->template ); + + } # function set_placeholder(...) + + /** + * Removes all unused placeholders + * + * @return void + */ + private function removePlaceholder() + { + $this->template = preg_replace( '/\[\[.*]]/', '', $this->template ); + + } # function removePlaceholder() + + /** + * Return the template as string + * + * @return string + */ + public function getHtml() + { + # Remove all unreplaced placeholders + $this->removePlaceholder(); + + return $this->template; + + } # function getHtml() + + + + +} # class \ No newline at end of file