2015-04-15 14:45:54 +02:00
|
|
|
<?php
|
|
|
|
|
2019-03-14 16:48:02 +01:00
|
|
|
namespace MVC\Classe;
|
|
|
|
|
2019-10-11 15:26:22 +02:00
|
|
|
//require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR."config".DIRECTORY_SEPARATOR."define-constantes.php";
|
|
|
|
|
2015-04-15 14:45:54 +02:00
|
|
|
class Url
|
|
|
|
{
|
2019-10-11 15:26:22 +02:00
|
|
|
public $page;
|
2020-04-01 09:19:34 +02:00
|
|
|
public $pageFile;
|
2019-10-11 15:26:22 +02:00
|
|
|
public $registre;
|
|
|
|
|
2019-03-05 13:26:50 +01:00
|
|
|
|
2019-12-04 15:21:19 +01:00
|
|
|
public function __construct($method, $appRequest)
|
|
|
|
{
|
2019-03-14 10:30:11 +01:00
|
|
|
|
2020-12-09 09:07:15 +01:00
|
|
|
//on créé le registre des modules d'applications tierces
|
2019-10-11 15:26:22 +02:00
|
|
|
$this->registre = new \MVC\Classe\ModularRegister();
|
|
|
|
|
|
|
|
//définition des parametres de base
|
2019-08-09 09:48:21 +02:00
|
|
|
$page = array();
|
2020-12-09 09:07:15 +01:00
|
|
|
$page['name'] = 'index';
|
2019-08-09 09:48:21 +02:00
|
|
|
$page['description'] = "";
|
|
|
|
$page['params'] = array();
|
|
|
|
$page['control'] = false;
|
2019-03-14 10:30:11 +01:00
|
|
|
|
|
|
|
|
2019-08-09 09:48:21 +02:00
|
|
|
$url = parse_url($_SERVER['REQUEST_URI']);
|
2020-12-09 10:26:26 +01:00
|
|
|
$urlTrim = trim($url['path'], '/');
|
|
|
|
$urlParts = explode('/', $urlTrim);
|
2019-08-09 09:48:21 +02:00
|
|
|
|
2020-12-18 17:33:23 +01:00
|
|
|
//suppression des sous repertoires du BASE_SERVER_DIRECTORY
|
|
|
|
$basePath = explode( '/', BASE_SERVER_DIRECTORY);
|
|
|
|
foreach($basePath as $subDir) {
|
|
|
|
if ($urlParts[0] == $subDir) {
|
|
|
|
array_shift($urlParts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 09:48:21 +02:00
|
|
|
//print_r($urlParts);
|
2020-12-09 10:26:26 +01:00
|
|
|
if (isset($urlParts[0])) {
|
2019-08-09 09:48:21 +02:00
|
|
|
//Récupération du nom de la page
|
2020-12-09 09:07:15 +01:00
|
|
|
($urlParts[0] == 'index' || $urlParts[0] == '') ? $page['name'] = 'index' : $page['name'] = $urlParts[0];
|
2019-08-09 09:48:21 +02:00
|
|
|
unset($urlParts[0]);
|
2020-12-09 10:26:26 +01:00
|
|
|
} else {
|
2020-12-09 09:07:15 +01:00
|
|
|
$page['name'] = 'index';
|
2019-08-09 09:48:21 +02:00
|
|
|
}
|
2019-03-14 10:30:11 +01:00
|
|
|
|
2020-04-01 09:19:34 +02:00
|
|
|
//il se peut que l'on ait des variable avec ? dans l'url
|
2020-12-09 10:26:26 +01:00
|
|
|
$urlQuery = explode('?', $page['name']);
|
2020-04-01 09:19:34 +02:00
|
|
|
$page['name'] = $urlQuery[0];
|
|
|
|
|
|
|
|
$page['name'] = strtolower($page['name']);
|
|
|
|
|
2020-12-09 10:26:26 +01:00
|
|
|
if ($page['name'] == 'control') {
|
2019-08-09 09:48:21 +02:00
|
|
|
$page['control'] = true;
|
2020-12-09 10:26:26 +01:00
|
|
|
($urlParts[1] == 'index' || $urlParts[1] == '') ? $page['name']='index' : $page['name']=$urlParts[1];
|
2019-08-09 09:48:21 +02:00
|
|
|
unset($urlParts[1]);
|
|
|
|
}
|
2017-06-13 22:54:16 +02:00
|
|
|
|
2019-09-09 22:37:17 +02:00
|
|
|
//vérification du nombre de parametres:
|
2019-08-09 09:48:21 +02:00
|
|
|
$numParts = count($urlParts);
|
2019-09-09 22:37:17 +02:00
|
|
|
//s'il n'existe pas autant de clé que de valeurs, ce peut ^etre un module symfony
|
2020-12-09 10:26:26 +01:00
|
|
|
if ($numParts%2 != 0) {
|
2019-09-09 22:37:17 +02:00
|
|
|
//si un module symfony n'est pas reférencé avec le nom de la page, on renvoi un erreur
|
2020-12-09 10:26:26 +01:00
|
|
|
if (!in_array($page['name'], $this->registre->getIndex())) {
|
2019-08-09 09:48:21 +02:00
|
|
|
$page['name'] = 'error';
|
|
|
|
$page['params'] = array();
|
2019-08-12 15:10:25 +02:00
|
|
|
$this->page = $page;
|
|
|
|
return;
|
2020-12-09 09:07:15 +01:00
|
|
|
} else {
|
2019-12-06 17:35:12 +01:00
|
|
|
foreach ($urlParts as $key => $value) {
|
|
|
|
$values[] = $value;
|
|
|
|
$keys[] = $key;
|
|
|
|
}
|
|
|
|
$page['params'] = $values;
|
2019-08-09 09:48:21 +02:00
|
|
|
}
|
2019-12-06 17:35:12 +01:00
|
|
|
|
2019-10-11 15:26:22 +02:00
|
|
|
//cas d'utilisation normal : il existe autant de clé que de valeurs
|
2020-12-09 10:26:26 +01:00
|
|
|
} elseif ($numParts != 0) {
|
2019-12-06 17:35:12 +01:00
|
|
|
// si c'est un module alors on charge un a un les parametres
|
2020-12-09 10:26:26 +01:00
|
|
|
if (in_array($page['name'], $this->registre->getIndex())) {
|
2019-12-06 17:35:12 +01:00
|
|
|
foreach ($urlParts as $key => $value) {
|
2019-08-09 09:48:21 +02:00
|
|
|
$values[] = $value;
|
2019-12-06 17:35:12 +01:00
|
|
|
$keys[] = $key;
|
|
|
|
}
|
|
|
|
$page['params'] = $values;
|
|
|
|
// sinon c'est une page normal du framework
|
|
|
|
} else {
|
|
|
|
$values = array();
|
|
|
|
$keys = array();
|
|
|
|
foreach ($urlParts as $key => $value) {
|
|
|
|
if ($key % 2 == 0) {
|
|
|
|
$values[] = $value;
|
|
|
|
} else {
|
|
|
|
$keys[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($page['control']) {
|
|
|
|
$page['params'] = array_combine($values, $keys);
|
2019-08-09 09:48:21 +02:00
|
|
|
} else {
|
2019-12-06 17:35:12 +01:00
|
|
|
$page['params'] = array_combine($keys, $values);
|
2019-08-09 09:48:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 09:19:34 +02:00
|
|
|
$page['name'] = lcfirst($page['name']);
|
|
|
|
$pageFile = CONTROLLERS_PATH . DIRECTORY_SEPARATOR . $page['name'] . '.php';
|
2019-08-09 09:48:21 +02:00
|
|
|
//verification de l'existence de la page dans les controlleurs
|
2020-12-09 10:26:26 +01:00
|
|
|
if ($page['control']) {
|
2019-08-09 09:48:21 +02:00
|
|
|
$pageFile = TRAITEMENT_PATH . DIRECTORY_SEPARATOR . $page['name'] . '.php';
|
2020-12-09 10:26:26 +01:00
|
|
|
} else {
|
2019-12-04 15:21:19 +01:00
|
|
|
//recherche du fichier controlleur correpondant HTTP1.1 ou HTTP1.0
|
|
|
|
switch ($method) {
|
|
|
|
//cas des requètes HTTP1.1
|
|
|
|
case 'PUT':
|
|
|
|
case 'DELETE':
|
|
|
|
case 'GET':
|
|
|
|
case 'POST':
|
|
|
|
if ($appRequest) {
|
|
|
|
$page['name'] = ucfirst($page['name']);
|
|
|
|
$pageFile = CONTROLLER_PATH . DIRECTORY_SEPARATOR . $page['name'] . 'HttpReponse.php';
|
|
|
|
} else {
|
|
|
|
$page['name'] = lcfirst($page['name']);
|
|
|
|
$pageFile = CONTROLLERS_PATH . DIRECTORY_SEPARATOR . $page['name'] . '.php';
|
|
|
|
if (!file_exists($pageFile)) {
|
|
|
|
$page['name'] = ucfirst($page['name']);
|
|
|
|
$pageFile = CONTROLLER_PATH . DIRECTORY_SEPARATOR . $page['name'] . 'HttpReponse.php';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 10:30:11 +01:00
|
|
|
}
|
2019-08-09 09:48:21 +02:00
|
|
|
|
2019-12-04 15:21:19 +01:00
|
|
|
if (!file_exists($pageFile)) {
|
|
|
|
if ($appRequest) {
|
|
|
|
$page['name'] = 'Error';
|
2019-12-05 00:50:23 +01:00
|
|
|
} else {
|
|
|
|
$page['name'] = 'error';
|
2019-12-04 15:21:19 +01:00
|
|
|
}
|
2019-08-09 09:48:21 +02:00
|
|
|
}
|
|
|
|
$this->page = $page;
|
2020-04-01 09:19:34 +02:00
|
|
|
$this->pageFile = $pageFile;
|
2019-08-09 09:48:21 +02:00
|
|
|
}
|
2019-09-26 04:27:05 +02:00
|
|
|
|
2020-12-18 17:33:23 +01:00
|
|
|
public static function asset_rewrite($urlAsset)
|
|
|
|
{
|
|
|
|
return '/' . BASE_SERVER_DIRECTORY . $urlAsset;
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:26:26 +01:00
|
|
|
public static function link_rewrite($isControlPatern, $page, $params = array())
|
2019-09-26 04:27:05 +02:00
|
|
|
{
|
|
|
|
if ($isControlPatern) {
|
|
|
|
return self::controlLink_rewrite($page, $params);
|
|
|
|
} else {
|
|
|
|
return self::link_rewrite_slashParam($page, $params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:26:26 +01:00
|
|
|
public static function module_link_rewrite($page, $params = array())
|
2020-04-01 09:19:34 +02:00
|
|
|
{
|
|
|
|
$stringParams = '';
|
|
|
|
foreach ($params as $values) {
|
|
|
|
$stringParams .= "/" . $values;
|
|
|
|
}
|
2020-12-18 17:33:23 +01:00
|
|
|
return '/' . BASE_SERVER_DIRECTORY . $page . $stringParams;
|
2020-04-01 09:19:34 +02:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:26:26 +01:00
|
|
|
private static function link_rewrite_slashParam($page, $params = array())
|
2019-09-26 04:27:05 +02:00
|
|
|
{
|
|
|
|
$stringParams = '';
|
|
|
|
foreach ($params as $key => $values) {
|
|
|
|
$stringParams .= "/" . $key . "/" . $values;
|
|
|
|
}
|
2020-12-18 17:33:23 +01:00
|
|
|
return '/' . BASE_SERVER_DIRECTORY . $page . $stringParams;
|
2019-09-26 04:27:05 +02:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:26:26 +01:00
|
|
|
private static function controlLink_rewrite($page, $params = array())
|
2019-09-26 04:27:05 +02:00
|
|
|
{
|
|
|
|
$stringParams = '';
|
|
|
|
foreach ($params as $key => $values) {
|
|
|
|
$stringParams .= "/" . $key . "/" . $values;
|
|
|
|
}
|
2020-12-18 17:33:23 +01:00
|
|
|
return '/' . BASE_SERVER_DIRECTORY . 'control' . '/' . $page . $stringParams;
|
2019-09-26 04:27:05 +02:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:26:26 +01:00
|
|
|
public static function absolute_link_rewrite($isControlPatern, $page, $params = array())
|
2019-12-04 15:21:19 +01:00
|
|
|
{
|
2021-02-09 16:16:26 +01:00
|
|
|
if(isset($_SERVER['HTTP_HOST'])) {
|
|
|
|
$url = $_SERVER['HTTP_HOST'];
|
|
|
|
if (isset($_SERVER['REQUEST_SCHEME'])) {
|
|
|
|
$scheme = $_SERVER['REQUEST_SCHEME'];
|
|
|
|
} else {
|
|
|
|
$scheme = 'http';
|
|
|
|
}
|
|
|
|
$base_url = $scheme . "://" . $url;
|
2021-02-17 13:39:05 +01:00
|
|
|
$url = $base_url;
|
2021-02-09 16:16:26 +01:00
|
|
|
}else{
|
|
|
|
$base_url = PATH_URL;
|
2021-02-17 13:39:05 +01:00
|
|
|
$url = $base_url . BASE_SERVER_DIRECTORY;
|
2021-02-09 16:16:26 +01:00
|
|
|
}
|
2019-12-04 15:21:19 +01:00
|
|
|
if ($isControlPatern) {
|
|
|
|
$uri = self::controlLink_rewrite($page, $params);
|
|
|
|
} else {
|
|
|
|
$uri = self::link_rewrite_slashParam($page, $params);
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:16:26 +01:00
|
|
|
|
2021-02-17 13:39:05 +01:00
|
|
|
return ( $url . $uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getBaseDirectory(){
|
|
|
|
return '/' . BASE_SERVER_DIRECTORY;
|
2019-12-04 15:21:19 +01:00
|
|
|
}
|
2015-04-15 14:45:54 +02:00
|
|
|
}
|