Initial commit

This commit is contained in:
Tobias Hopp 2020-10-02 08:06:04 +02:00
parent 25708d01db
commit 1fd648bc4a
17 changed files with 567 additions and 1 deletions

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View File

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/online-faq.iml" filepath="$PROJECT_DIR$/.idea/online-faq.iml" />
</modules>
</component>
</project>

8
.idea/online-faq.iml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

21
LICENSE Normal file
View File

@ -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.

View File

@ -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

18
SECURITY.md Normal file
View File

@ -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.

View File

@ -0,0 +1,79 @@
<?php
/**
* Class Autoloader
* Loads a class on call
*/
class Autoloader
{
/**
* All directorys to search for classes
* You can add directorys if you want
* @var static $directorys
*
**/
private static $directorys = array('classes', 'controller', 'model', 'views');
/**
* Load's the autoloader
*
* @return void
*/
public function __construct()
{
if ( !spl_autoload_register( array( $this, 'load_class' ) ) )
{
die('A fatal error has occurred.');
}
} # function __construct()
/**
* register the autoloader
*
* @return void
*/
public static function register()
{
new Autoloader();
} # function register()
/**
* Loads a class
*
* @param string $a_class_name Klassen-Name
* @return void
*
*/
public function load_class( $a_class_name )
{
foreach ( self::$directorys as $folder )
{
$file = __DIR__ . '/../' . $folder . '/' . $a_class_name . '.class.inc.php';
if ( file_exists( $file ) )
{
require_once $file;
}
else
{
$file = __DIR__ . '/../' . $folder . '/' . mb_strtolower( $a_class_name ) . '.class.inc.php';
if ( file_exists( $file ) )
{
require_once $file;
}
else
{
$file = __DIR__ . '/../' . $folder . '/' . ucfirst( $a_class_name ) . '.class.inc.php';
if ( file_exists( $file ) )
{
require_once $file;
}
}
}
}
} # function load_class(...)
} # class

View File

@ -0,0 +1,35 @@
<?php
/**
* Class database
* This class is for connecting with a database with PDO
*/
class Database
{
/**
* @var PDO
*/
private static $pdo;
/**
* @param PDO $a_pdo PDO instance
*
* @return void
*/
public static function setConnection( PDO $a_pdo )
{
self::$pdo = $a_pdo;
} # function setConnection(...)
/**
* @return PDO
*/
public static function getConnection()
{
return self::$pdo;
} # function getConnection()
} # class

View File

@ -0,0 +1,65 @@
<?php
/**
* Class Factory
* This class returns the action and controller for index file
*/
class Factory
{
/**
* Returns the right controller
*
* @param Request $a_request Request
*
* @return mixed
*/
public static function getController( Request $a_request )
{
# Prepare name of controller
$controller_name = mb_strtolower( $a_request->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

View File

@ -0,0 +1,56 @@
<?php
/**
* Behandelt die Variablen im Request GET / POST
* Syntax:
* $request = getVar( "Name", "Default Wert falls kein Wert gefunden wurde", "POST/GET" );
*/
class Request
{
/**
* GET-Request Name
* @var static
*/
public static $GET = 'GET';
/**
* POST-Request Name
* @var static
*/
public static $POST = 'POST';
/**
* Request Variablen-Wert zurückgeben
*
* @param string $a_name Name des Parameters
* @param string|null $a_default Default-Wert, falls nicht vorhadnen
* @param string $a_request Request(GET oder POST)
*
* @return mixed
*/
public function getVar( $a_name, $a_default = null, $a_request = 'GET' )
{
if ( $a_request === self::$GET )
{
$requestArray = $_GET;
}
else if ( $a_request === self::$POST )
{
$requestArray = $_POST;
}
else
{
$requestArray = array();
}
# Wert im Request vorhanden
if ( isset( $requestArray[$a_name] ) )
{
return htmlspecialchars( $requestArray[$a_name] );
}
return $a_default;
} # function getVar(...)
} # class

View File

@ -0,0 +1,29 @@
<?php
/**
* Class DefaultController
*/
class DefaultController
{
/**
* The default index test controller
*
* @return string HTML Code
* @throws Exception
*/
public function indexAction()
{
$view = new Template( 'test' );
$DefaultModel = new DefaultModel();
$value = $DefaultModel->getTestMessage();
$view->set_placeholder( 'test', $value );
// Return the HTML Code to the index.php
return $view->getHtml();
} # function indexAction()
} # class

26
index.php Normal file
View File

@ -0,0 +1,26 @@
<?php
/**
* Includes the main inc
* This is the index file.
*/
require_once __DIR__ . '/main.inc.php';
try
{
# Initialize Request
$request = new Request();
# Get Controller from Factory
$controller = Factory::getController( $request );
# Get Action from Factory
$action = Factory::getAction( $controller, $request );
# Call controller and action
echo $controller->$action();
}
catch ( Exception $e )
{
# Please replace it with error template
die( 'An internal error occured while processing your request.' );
}

29
main.inc.php Normal file
View File

@ -0,0 +1,29 @@
<?php
/**
* Main file load's all the important files and the autoloader.
* Bindet alles ein und setzt einige Variablen sowie die Datenbank.
*/
require_once __DIR__ . '/classes/Autoloader.class.inc.php';
require_once __DIR__ . '/config.inc.php';
# Registering autoloader
Autoloader::register();
try
{
$host = MYSQL_HOST;
$dbname = MYSQL_DB;
# Creates a mysql connection
$pdo = new PDO( "mysql:host={$host};dbname={$dbname}", MYSQL_USER, MYSQL_PASS, array( PDO::ATTR_ERRMODE => 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' );
}

View File

@ -0,0 +1,10 @@
<?php
class DefaultModel
{
public function getTestMessage()
{
return "Rabbits can't fly without Red Bull.";
}
}

10
templates/test.tmpl.html Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
[[TEST]]
</body>
</html>

View File

@ -0,0 +1,89 @@
<?php
/**
* Template Class
* Templates are stored under templates/
* A placeholder is created with [[NAME OF PLACEHOLDER]]
*/
class Template
{
/**
* Variables for Template Class
*/
/**
* @var string To store informations while class is defined
*/
private $template;
/**
* Template constructor
* .
* @param string $a_file_name Template file name(without .tmpl.html)
* @throws Exception Template not found Exception
*/
public function __construct( $a_file_name )
{
$path = __DIR__ . "/../templates/{$a_file_name}.tmpl.html";
# Template existiert nicht
if ( !file_exists( $path ) )
{
throw new RuntimeException( "Template '$a_file_name' doesn't exist!", 2 );
}
$this->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