Base d'un site web v2

This commit is contained in:
Sanchez 2017-04-17 23:05:15 +02:00 committed by mathieu
parent e761ff7c5a
commit ec9eb79038
21 changed files with 143 additions and 103 deletions

59
base.sql Normal file
View File

@ -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 */;

View File

@ -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]);

View File

@ -4,7 +4,7 @@ namespace base\API;
use base\Controller\Controller;
class API extends Controller{
class API extends Controller {
private $declaredFunctions = [];

View File

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

View File

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

View File

@ -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';
}

View File

@ -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';
}

View File

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

View File

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

View File

@ -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();
}

View File

@ -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]);
}

View File

@ -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());
}
}

View File

@ -6,6 +6,8 @@ abstract class BDTables {
// const NOM = 'bd_nom'
// Ex : const ABONNEMENT = 'abonnement';
const LOGS = "logs";
}
?>

View File

@ -8,7 +8,7 @@
*******************************************************************************/
namespace base\Model;
define('FPDF_VERSION','1.81');
define('FPDF_VERSION', '1.81');
class FPDF
{

View File

@ -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]);
}
}

View File

@ -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();

3
src/View/Error.php Normal file
View File

@ -0,0 +1,3 @@
<?php
echo $erreur;
?>

View File

@ -1,3 +1,3 @@
<div>
Bonjour
Bonjour 2
</div>

View File

@ -1,29 +1,6 @@
<?php if(!empty($_SESSION['ville_inconue']) && $_SESSION['ville_inconue'] == true){ ?>
<div class="content">
<div class="error-page-container">
<div class="error-title"><?= $erreur ?></div>
<div class="error-message">La ville saisie est introuvable</div>
<div class="error-prestations">
Merci d'effectuer une nouvelle recherche avec une autre ville en utilisant les suggestions de la liste.<br><br>
Que voulez-vous faire ?<br><br>
<a class="error-prestation" href="/ajout-artisan-avis">Evaluer un professionnel</a>
<a class="error-prestation" href="/demande-devis">Demander des devis</a>
</div>
</div>
</div>
<?php }else{ ?>
<div class="content">
<div class="error-page-container">
<div class="error-title"><?= $erreur ?></div>
<div class="error-message">Que voulez-vous faire ?</div>
<div class="error-prestations">
<a class="error-prestation" href="/ajout-artisan-avis">Evaluer un professionnel</a>
<a class="error-prestation" href="/demande-devis">Demander des devis</a>
</div>
</div>
</div>
<?php } ?>

View File

@ -17,17 +17,17 @@
<meta property="og:title" content="<?= $this->head['title'] ?>" />
<meta property="og:description" content="<?= $this->head['description'] ?>" />
<meta property="og:url" content="https://<?= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] ?>" />
<meta property="og:image" content="https://<?= $_SERVER['SERVER_NAME'] ?>/img/Slogan_Eldo_share.png" />
<meta property="fb:app_id" content="1000452166691027" />
<meta property="og:url" content="https://<?= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] ?>" />
<meta property="og:image" content="https://<?= $_SERVER['SERVER_NAME'] . \base\Config::FAVICON_PATH ?>" />
<!-- <meta property="fb:app_id" content="1000452166691027" /> -->
<link href='https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700%7CRoboto+Condensed:400,700%7CMaterial+Icons' rel='stylesheet' type='text/css'>
<link href="/css/theme.css?v=<?= base\Config::SITE_CSS_VERSION ?>" rel="stylesheet">
<link href="/css/select2.css" rel="stylesheet">
<link rel="image_src" href="/img/Slogan_Eldo_share.png" />
<link rel="icon" type="image/png" href="/img/" />
<link rel="image_src" href="<?php \base\Config::FAVICON_PATH ?>" />
<link rel="icon" type="image/png" href="<?php \base\Config::FAVICON_PATH ?>" />
<meta name="theme-color" content="#ffffff">

View File

@ -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);
}
?>