From ec9eb7903855ecc3861c1605e335abc6d691f2bd Mon Sep 17 00:00:00 2001 From: Sanchez Date: Mon, 17 Apr 2017 23:05:15 +0200 Subject: [PATCH] Base d'un site web v2 --- base.sql | 59 +++++++++++++++++++++++++++++++ index.php | 1 - src/API/API.php | 2 +- src/API/APIError.php | 1 - src/API/APIRouter.php | 5 ++- src/Autoloader.php | 4 +-- src/Config.php | 6 ++-- src/Controller/Controller.php | 4 +-- src/Controller/ControllerSite.php | 2 +- src/Controller/site/Index.php | 10 ++++-- src/Controller/site/SiteError.php | 12 +++---- src/Model/BDD.php | 18 +++++----- src/Model/BDTables.php | 2 ++ src/Model/FPDF.php | 2 +- src/Model/Logs.php | 59 ++++++++++++------------------- src/Model/Model.php | 6 ++-- src/View/Error.php | 3 ++ src/View/Site/Index.php | 2 +- src/View/Site/SiteError.php | 23 ------------ src/View/Site/tpl/head.php | 10 +++--- src/lib/functions.php | 15 ++++++-- 21 files changed, 143 insertions(+), 103 deletions(-) create mode 100644 base.sql create mode 100644 src/View/Error.php diff --git a/base.sql b/base.sql new file mode 100644 index 0000000..ce1230e --- /dev/null +++ b/base.sql @@ -0,0 +1,59 @@ +-- phpMyAdmin SQL Dump +-- version 4.6.4 +-- https://www.phpmyadmin.net/ +-- +-- Client : 127.0.0.1 +-- Généré le : Lun 17 Avril 2017 à 21:00 +-- Version du serveur : 5.7.14 +-- Version de PHP : 7.0.10 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + +-- +-- Base de données : `base` +-- + +-- -------------------------------------------------------- + +-- +-- Structure de la table `logs` +-- + +CREATE TABLE `logs` ( + `id` int(10) NOT NULL, + `level` smallint(5) NOT NULL, + `message` varchar(2000) COLLATE utf8_unicode_ci NOT NULL, + `file` varchar(255) COLLATE utf8_unicode_ci NOT NULL, + `line` varchar(6) COLLATE utf8_unicode_ci NOT NULL, + `date` datetime NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + +-- +-- Index pour les tables exportées +-- + +-- +-- Index pour la table `logs` +-- +ALTER TABLE `logs` + ADD PRIMARY KEY (`id`); + +-- +-- AUTO_INCREMENT pour les tables exportées +-- + +-- +-- AUTO_INCREMENT pour la table `logs` +-- +ALTER TABLE `logs` + MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/index.php b/index.php index a499d33..05d4b7c 100644 --- a/index.php +++ b/index.php @@ -32,7 +32,6 @@ if (count($pages) > 1 && $pages[count($pages) - 1] == '') exit(); } - if ($pages[0] == 'api' && isset($pages[1]) && preg_match('#^([a-z]+)$#', $pages[1], $api1) && isset($pages[2]) && preg_match('#^([a-z-]+)$#', $pages[2], $api2)) { new APIRouter($api1[0], $api2[0]); diff --git a/src/API/API.php b/src/API/API.php index 32ad49d..0e6f5e9 100644 --- a/src/API/API.php +++ b/src/API/API.php @@ -4,7 +4,7 @@ namespace base\API; use base\Controller\Controller; -class API extends Controller{ +class API extends Controller { private $declaredFunctions = []; diff --git a/src/API/APIError.php b/src/API/APIError.php index 11d3cfb..c613fe3 100644 --- a/src/API/APIError.php +++ b/src/API/APIError.php @@ -14,7 +14,6 @@ class APIError extends Controller { * @param string $code */ public function __construct(int $ErrCode = 500, string $devMessage = 'Erreur inconnue', string $publicMessage = 'Une erreur inconnue s\'est produite', string $code = '') { - parent::__construct(); $tabCode = [ diff --git a/src/API/APIRouter.php b/src/API/APIRouter.php index c80d37e..a1e055c 100644 --- a/src/API/APIRouter.php +++ b/src/API/APIRouter.php @@ -4,11 +4,10 @@ namespace base\API; use base\Config; -class APIRouter -{ +class APIRouter { //Les méthodes HTTP gérée par l'API - const HTTP_METHODS = ['GET','POST','DELETE','PUT']; + const HTTP_METHODS = ['GET', 'POST', 'DELETE', 'PUT']; /** * APIRouter constructor. diff --git a/src/Autoloader.php b/src/Autoloader.php index 353bec0..09a0920 100644 --- a/src/Autoloader.php +++ b/src/Autoloader.php @@ -7,7 +7,7 @@ class Autoloader { /** * Enregistre notre autoloader */ - static function register(){ + static function register() { spl_autoload_register(array(__CLASS__, 'autoload')); } @@ -15,7 +15,7 @@ class Autoloader { * Inclue le fichier correspondant à notre classe * @param $class string Le nom de la classe à charger */ - static function autoload($class){ + static function autoload($class) { if (preg_match('#^' . Config::NAMESPACE . '\\\(.+)$#', $class, $matches)) require 'src/' . str_replace('\\', '/', $matches[1]) . '.php'; } diff --git a/src/Config.php b/src/Config.php index d9bd92e..22f747c 100644 --- a/src/Config.php +++ b/src/Config.php @@ -6,7 +6,9 @@ class Config { const SITE_JS_VERSION = '1.00'; const SITE_CSS_VERSION = '1.00'; - const TITLE_HEADER = ''; - const DESCRIPTION_HEADER = ''; + const TITLE_HEADER = 'Mon titre de site'; + const DESCRIPTION_HEADER = 'Ma description pour les robots'; const NAMESPACE = 'base'; + + const FAVICON_PATH = '/img/favicon.png'; } \ No newline at end of file diff --git a/src/Controller/Controller.php b/src/Controller/Controller.php index f1733ed..237807f 100644 --- a/src/Controller/Controller.php +++ b/src/Controller/Controller.php @@ -15,13 +15,13 @@ class Controller { protected function view() { ob_start(); extract($this->data); - require 'src/View/'.str_replace('\\', '/', preg_replace('#^' . Config::NAMESPACE . '\\\Controller\\\#', '', get_class($this))).'.php'; + require 'src/View/' . str_replace('\\', '/', preg_replace('#^' . Config::NAMESPACE . '\\\Controller\\\#', '', get_class($this))) . '.php'; ob_end_flush(); exit(); } protected function addData($data) { - $this->data += $data; + $this->data += $data; } protected function returnJson($data) { diff --git a/src/Controller/ControllerSite.php b/src/Controller/ControllerSite.php index e9b1ee4..337aef1 100644 --- a/src/Controller/ControllerSite.php +++ b/src/Controller/ControllerSite.php @@ -31,7 +31,7 @@ class ControllerSite { require 'src/View/Site/tpl/head.php'; } - require 'src/View/'.str_replace('\\', '/', preg_replace('#^' . Config::NAMESPACE . '\\\Controller\\\#', '', get_class($this))).'.php'; + require 'src/View/'.str_replace('\\', '/', preg_replace('#^' . Config::NAMESPACE . '\\\Controller\\\#', '', get_class($this))) . '.php'; if ($footer) { require 'src/View/Site/tpl/footer.php'; diff --git a/src/Controller/site/Index.php b/src/Controller/site/Index.php index 77e403b..2fc9dd9 100644 --- a/src/Controller/site/Index.php +++ b/src/Controller/site/Index.php @@ -9,10 +9,16 @@ class Index extends ControllerSite { /** * Index constructor. */ - public function __construct() - { + public function __construct() { parent::__construct(); + $this->addHead([ + ]); + + $this->addFooter([ + + ]); + $this->addData([]); $this->view(); } diff --git a/src/Controller/site/SiteError.php b/src/Controller/site/SiteError.php index 9767f58..8c7f788 100644 --- a/src/Controller/site/SiteError.php +++ b/src/Controller/site/SiteError.php @@ -11,7 +11,7 @@ class SiteError extends ControllerSite { * @param int $ErrCode * @param string $message */ - public function __construct($ErrCode = 500, $message = ''){ + public function __construct($ErrCode = 500, $message = '') { parent::__construct(); $tabCode = [ @@ -74,23 +74,23 @@ class SiteError extends ControllerSite { 511 => ['label' => '511 Network Authentication Required', 'msg' => '511 Network Authentication Required'] ]; - if(isset($tabCode[$ErrCode])){ + if (isset($tabCode[$ErrCode])) { header($_SERVER['SERVER_PROTOCOL'].' '.$tabCode[$ErrCode]['label']); - if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){ + if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $this->throwError('Erreur '.$tabCode[$ErrCode]['label'], 'Erreur_'.$ErrCode); } $erreur = $tabCode[$ErrCode]['msg']; $this->addData(['erreur' => $erreur]); - }else{ + } else { header($_SERVER['SERVER_PROTOCOL'].' '.$tabCode[500]['label']); - if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){ + if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $this->throwError('Erreur '.$tabCode[500]['label'], 'Erreur_500'); } $erreur = $tabCode[500]['msg']; $this->addData(['erreur' => $erreur]); } - if(!empty($message)){ + if (!empty($message)) { $this->addData(['message' => $message]); } diff --git a/src/Model/BDD.php b/src/Model/BDD.php index 09104c4..2011b62 100644 --- a/src/Model/BDD.php +++ b/src/Model/BDD.php @@ -5,12 +5,11 @@ namespace base\Model; use Exception; use PDO; -class BDD -{ - const SQL_SERVER = 'localhost'; - const SQL_LOGIN = 'root'; - const SQL_PASSWORD = ''; - const SQL_DB = 'eldotravo'; +class BDD { + const SQL_SERVER = 'localhost'; // BDD Server + const SQL_LOGIN = 'root'; // BDD Login + const SQL_PASSWORD = ''; // BDD Password + const SQL_DB = 'base'; // BDD Name private static $bdd; @@ -22,10 +21,11 @@ class BDD PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]; - self::$bdd = new PDO('mysql:host='.self::SQL_SERVER.';dbname='.self::SQL_DB.';charset=utf8', self::SQL_LOGIN, self::SQL_PASSWORD, $pdo_options); + self::$bdd = new PDO('mysql:host=' . self::SQL_SERVER . ';dbname=' . self::SQL_DB.';charset=utf8', + self::SQL_LOGIN, self::SQL_PASSWORD, $pdo_options); } - catch(Exception $e) { - die('Erreur : '.$e->getMessage()); + catch (Exception $e) { + die('Erreur : ' . $e->getMessage()); } } diff --git a/src/Model/BDTables.php b/src/Model/BDTables.php index e542254..40fe21b 100644 --- a/src/Model/BDTables.php +++ b/src/Model/BDTables.php @@ -6,6 +6,8 @@ abstract class BDTables { // const NOM = 'bd_nom' // Ex : const ABONNEMENT = 'abonnement'; + + const LOGS = "logs"; } ?> \ No newline at end of file diff --git a/src/Model/FPDF.php b/src/Model/FPDF.php index 09e432a..6669470 100644 --- a/src/Model/FPDF.php +++ b/src/Model/FPDF.php @@ -8,7 +8,7 @@ *******************************************************************************/ namespace base\Model; -define('FPDF_VERSION','1.81'); +define('FPDF_VERSION', '1.81'); class FPDF { diff --git a/src/Model/Logs.php b/src/Model/Logs.php index 4769433..8cd0179 100644 --- a/src/Model/Logs.php +++ b/src/Model/Logs.php @@ -7,12 +7,10 @@ use PDO; class Logs { public $id; - public $site; public $level; public $message; public $file; public $line; - public $user; public $date; const ERROR_LEVEL = [ @@ -36,22 +34,19 @@ class Logs { /** * Logs constructor. - * @param int $id - * @param string $site - * @param string $level - * @param string $message - * @param string $file - * @param string $line - * @param string $date + * @param int|null $id + * @param string|null $level + * @param string|null $message + * @param string|null $file + * @param string|null $line + * @param string|null $date */ - public function __construct(int $id = null, string $site = null, string $level = null, string $message = null, - string $file = null, string $line = null, string $date = null) + public function __construct(int $id = null, string $level = null, string $message = null, string $file = null, string $line = null, string $date = null) { if ($id === NULL) { return; } $this->id = $id; - $this->site = $site; $this->level = $level; $this->message = $message; $this->file = $file; @@ -60,10 +55,14 @@ class Logs { } - public static function insert($site, $level, $message, $file, $line, $date) { - $user = NULL; - $req = BDD::instance()->prepare('INSERT INTO '.BDTables::LOGS.'(site, level, message, date, file, line) VALUES(:site, :level, :message, :date, :file, :line)'); - $req->execute(['site' => $site, 'level' => $level, 'message' => $message, 'date' => $date, 'file' => $file, 'line' => $line]); + public static function insert($level, $message, $file, $line, $date) { + Model::insert(BDTables::LOGS, [ + 'level' => $level, + 'message' => $message, + 'file' => $file, + 'line' => $line, + 'date' => date("Y-m-d H:i:s", strtotime($date)) + ]); } /** @@ -72,15 +71,16 @@ class Logs { * @return array */ public static function getLastLogs(int $limit){ - $req = BDD::instance()->prepare('SELECT l.* - FROM '.BDTables::LOGS.' l - ORDER BY date DESC LIMIT :limit'); + $req = BDD::instance()->prepare('SELECT * + FROM ' . BDTables::LOGS . ' + ORDER BY date DESC + LIMIT :limit'); $req->bindValue('limit', $limit, PDO::PARAM_INT); $req->execute(); $return = []; foreach ($req->fetchAll() as $l){ - $log = new Logs($l['id'], $l['site'], $l['level'], $l['message'], $l['file'], $l['line'], $l['date']); + $log = new Logs($l['id'], $l['level'], $l['message'], $l['file'], $l['line'], $l['date']); $return[] = $log; } @@ -103,22 +103,6 @@ class Logs { $this->id = $id; } - /** - * @return string - */ - public function getSite() - { - return htmlspecialchars($this->site); - } - - /** - * @param string $site - */ - public function setSite($site) - { - $this->site = $site; - } - /** * @return string */ @@ -203,7 +187,8 @@ class Logs { * Retourne le type d'erreur en string (label) * @return string */ - public function getErrorLabel(){ + public function getErrorLabel() + { return htmlspecialchars(self::ERROR_LEVEL[$this->level]); } } diff --git a/src/Model/Model.php b/src/Model/Model.php index d27a661..5b60b4d 100644 --- a/src/Model/Model.php +++ b/src/Model/Model.php @@ -12,7 +12,7 @@ class Model { */ public static function insert(string $tableName, array $data){ $req = BDD::instance()->prepare('INSERT INTO ' . $tableName . ' (' . implode(', ', array_keys($data)) . ') - VALUES (' . ':'. implode(', :', array_keys($data)) . ')'); + VALUES (' . ':' . implode(', :', array_keys($data)) . ')'); $req->execute($data); return BDD::lastInsertId(); } @@ -28,12 +28,12 @@ class Model { $reqStr = 'UPDATE ' . $tableName . ' SET '; $lastKey = endKey($data); foreach ($data as $key => $value){ - $reqStr .= $key.' = :'.$key; + $reqStr .= $key . ' = :' . $key; if($key != $lastKey) { $reqStr .= ', '; } } - $reqStr .= ' WHERE '.$idColumn.' = :'.$idColumn; + $reqStr .= ' WHERE ' . $idColumn . ' = :' . $idColumn; $data[$idColumn] = $idValue; //echo $reqStr; exit(); diff --git a/src/View/Error.php b/src/View/Error.php new file mode 100644 index 0000000..e015086 --- /dev/null +++ b/src/View/Error.php @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/src/View/Site/Index.php b/src/View/Site/Index.php index 7c2d5cc..0ccb255 100644 --- a/src/View/Site/Index.php +++ b/src/View/Site/Index.php @@ -1,3 +1,3 @@
- Bonjour + Bonjour 2
\ No newline at end of file diff --git a/src/View/Site/SiteError.php b/src/View/Site/SiteError.php index 44de7fe..1281cad 100644 --- a/src/View/Site/SiteError.php +++ b/src/View/Site/SiteError.php @@ -1,29 +1,6 @@ - - -
-
-
-
La ville saisie est introuvable
-
- Merci d'effectuer une nouvelle recherche avec une autre ville en utilisant les suggestions de la liste.

- Que voulez-vous faire ?

- Evaluer un professionnel - Demander des devis -
-
-
- - -
Que voulez-vous faire ?
-
- - \ No newline at end of file diff --git a/src/View/Site/tpl/head.php b/src/View/Site/tpl/head.php index b383fe4..0111f2f 100644 --- a/src/View/Site/tpl/head.php +++ b/src/View/Site/tpl/head.php @@ -17,17 +17,17 @@ - - - + + + - - + + diff --git a/src/lib/functions.php b/src/lib/functions.php index d56d623..39631e9 100644 --- a/src/lib/functions.php +++ b/src/lib/functions.php @@ -114,17 +114,17 @@ function darkroom($img, $to, $width = 0, $height = 0, $quality = 100, $useGD = t function errorHandler($errno, $errstr, $errfile, $errline) { if (!(error_reporting() & $errno)) { // Ce code d'erreur n'est pas inclus dans error_reporting() - return; + return ; } // Insertion des logs -// \base\Model\Logs::insert('eldo', $errno, $errstr, $errfile, $errline, date('Y-m-d H:i:s')); + \base\Model\Logs::insert($errno, $errstr, $errfile, $errline, date('Y-m-d H:i:s')); ob_clean(); new \base\Controller\Site\SiteError(500); /* Ne pas exécuter le gestionnaire interne de PHP */ - return; + return ; } /** @@ -385,4 +385,13 @@ function cleanArray(array $data) { return $data; } +/** + * @param $array + * @return mixed + */ +function endKey($array){ + end($array); + return key($array); +} + ?> \ No newline at end of file