Refactoring : La (nouvelle) classe CheminPage s'occupe de la manipulation des chemins du site (gallerie/évènement3/photo5), la classe Page l'utilise et ne fait plus de manipulations de chemins directement.
This commit is contained in:
parent
b27eabf106
commit
3afe99ebb4
42
controleur/chemin_page.php
Normal file
42
controleur/chemin_page.php
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Note : L'implémentation de CheminPage pourrait utiliser une pile au lieu des chaînes de caractère :
|
||||||
|
// ["Chemin", "Vers", "Page"] == "/Chemin/Vers/Page"
|
||||||
|
|
||||||
|
class CheminPage {
|
||||||
|
public function __construct($chemin) {
|
||||||
|
$this->chemin = CheminPage::nettoyer_chemin($chemin);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get() {
|
||||||
|
return $this->chemin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function enfant($nom) {
|
||||||
|
return $this->chemin . '/' . CheminPage::nettoyer_chemin($nom);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parent() {
|
||||||
|
$slash = strrpos($this->chemin, '/');
|
||||||
|
if ($slash !== false) {
|
||||||
|
return substr($this->chemin, 0, $slash);
|
||||||
|
} else {
|
||||||
|
return '/';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function nettoyer_chemin($chemin) {
|
||||||
|
// SECURITE : $chemin_nettoyé
|
||||||
|
// * Ne contient pas '\0'
|
||||||
|
// * Ne contient pas '../'
|
||||||
|
// * Ne contient pas de double occurence de '/'
|
||||||
|
// * Ni d'autres bizarreries des chemins de fichiers.
|
||||||
|
// * Ne contient pas _prop_
|
||||||
|
// * Ne se termine pas par '/'
|
||||||
|
// * Commence par '/'
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
return $chemin;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,57 +1,48 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once("util.php");
|
||||||
require_once("config.php");
|
require_once("config.php");
|
||||||
|
require_once("controleur/chemin_page.php");
|
||||||
|
|
||||||
// Protocole : http://site/actualités/?nouveau=Le%20titre
|
// Protocole : http://site/actualités/?nouveau=Le%20titre
|
||||||
|
|
||||||
// SECURITE : Invariants de sécurité :
|
// Structure des répertoires
|
||||||
// Page::chemin ne contient jamais de chaîne '../' ou autres bizarreries des chemins de fichiers.
|
// article/_prop_article
|
||||||
// Donc on peut concaténer Page::chemin à un chemin dans le système de fichiers et être sûr d'être dans un sous-dossier.
|
// /_prop_type
|
||||||
// TODO : Lors de la construction d'un chemin, tous les composants doivent être nettoyés.
|
// /_prop_photo
|
||||||
|
// /_prop_date
|
||||||
// TODO : créer une classe chemin_page
|
// /_prop_lieu
|
||||||
|
// /article_1 // Sous article
|
||||||
|
// /article_2 // Sous article
|
||||||
|
|
||||||
class Page {
|
class Page {
|
||||||
// article/prop_article
|
|
||||||
// /prop_type
|
|
||||||
// /prop_photo
|
|
||||||
// /prop_date
|
|
||||||
// /prop_lieu
|
|
||||||
// /article_1 // Sous article
|
|
||||||
// /article_2 // Sous article
|
|
||||||
|
|
||||||
public function __construct($chemin) {
|
public function __construct($chemin) {
|
||||||
// SECURITE : chemin doit être un sous-dossier de .../modele/
|
$this->chemin = new CheminPage($chemin);
|
||||||
$this->chemin = nettoyer_chemin($chemin);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Nettoie un chemin de page pour qu'il respecte l'invariant de sécurité.
|
|
||||||
public static function nettoyer_chemin($chemin) {
|
|
||||||
return $chemin;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Renvoie le chemin de la page dans le système de fichiers
|
// Renvoie le chemin de la page dans le système de fichiers
|
||||||
private function chemin_fs() {
|
private function chemin_fs() {
|
||||||
return concaténer_chemin_fs($config_chemin_base, $this->chemin);
|
global $config_chemin_base;
|
||||||
|
return concaténer_chemin_fs($config_chemin_base, $this->chemin->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function liste_enfants() {
|
public function liste_enfants() {
|
||||||
$lst = scandir($this->chemin_fs());
|
$scandir = scandir($this->chemin_fs());
|
||||||
$lst_enfants = Array();
|
if ($scandir === false) { error_log("Impossible d'accéder à la liste des pages enfant de " . $this->chemin->get()); }
|
||||||
if ($lst !== false) {
|
|
||||||
foreach ($lst as $k => $v) {
|
$enfants = Array();
|
||||||
$lst_enfants[] = $this->enfant($v);
|
foreach ($scandir as $k => $v) {
|
||||||
}
|
$enfants[] = $this->enfant($v);
|
||||||
}
|
}
|
||||||
return $lst_enfants;
|
return $enfants;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function enfant($nom) {
|
public function enfant($nom) {
|
||||||
return new Page(nettoyer_chemin($this->chemin) . '/' . nettoyer_chemin($nom));
|
return new Page($this->chemin->enfant($nom));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function parent() {
|
public function parent() {
|
||||||
return new Page(nettoyer_chemin($this->chemin) . '/..'); // TODO
|
return new Page($this->chemin->parent());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function nouveau($nom) {
|
public function nouveau($nom) {
|
||||||
|
@ -78,11 +69,11 @@ class Page {
|
||||||
public function url() {
|
public function url() {
|
||||||
// calculer l'url de cette page en fonction de son chemin et de l'url de base
|
// calculer l'url de cette page en fonction de son chemin et de l'url de base
|
||||||
global $config_url_base;
|
global $config_url_base;
|
||||||
return $config_url_base . $this->chemin;
|
return $config_url_base . $this->chemin->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function vue() {
|
public function vue() {
|
||||||
return "Aucune vue pour «" . $this->chemin . "» .";
|
return "Aucune vue pour «" . $this->chemin->get() . "» .";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user