-
Notifications
You must be signed in to change notification settings - Fork 1
/
plxMyBetterUrls.php
executable file
·182 lines (159 loc) · 5.67 KB
/
plxMyBetterUrls.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Plugin plxMyBetterUrls
*
* @author Stephane F
**/
class plxMyBetterUrls extends plxPlugin {
/**
* Constructeur de la classe
*
* @param default_lang langue par défaut
* @return stdio
* @author Stephane F
**/
public $article;
public $category;
public $static;
public $lang;
public function __construct($default_lang) {
# appel du constructeur de la classe plxPlugin (obligatoire)
parent::__construct($default_lang);
# droits pour accéder à la page config.php du plugin
$this->setConfigProfil(PROFIL_ADMIN);
# initialisation des variables de la classe
$this->article = $this->getParam('format_article')!='' ? $this->getParam('format_article').'/' : '';
$this->category = $this->getParam('format_category')!='' ? $this->getParam('format_category').'/' : '';
$this->static = $this->getParam('format_static')!='' ? $this->getParam('format_static').'/' : '';
# déclaration des hooks
$this->addHook('plxMotorConstruct', 'plxMotorConstruct');
$this->addHook('plxMotorDemarrageNewCommentaire', 'plxMotorDemarrageNewCommentaire');
$this->addHook('plxMotorConstructLoadPlugins', 'Redirect301');
$this->addHook('IndexEnd', 'RewriteUrls');
$this->addHook('FeedEnd', 'RewriteUrls');
$this->addHook('SitemapEnd', 'RewriteUrls');
$this->addHook('plxFeedPreChauffageBegin', 'plxFeedPreChauffageBegin');
}
/**
* Méthode qui fait une redirection 301 si accès à PluXml à partir des anciennes url
*
* @author Stephane F
**/
public function Redirect301() {
echo '<?php
if(!defined("PLX_ADMIN") AND substr(str_replace($_SERVER["QUERY_STRING"], "", $_SERVER["REQUEST_URI"]),-1)=="?") {
# redirection si lien http://server.com/?contenu vers http://server.com/contenu
header("Status: 301 Moved Permanently", false, 301);
header("Location: ".$this->urlRewrite($_SERVER["QUERY_STRING"]));
exit();
}
if(preg_match("/^(article|static|categorie)[0-9]+\/([a-z0-9-]+)(\/page[0-9]+)?/", $this->get, $capture)) {
if($capture[1]!="'.$this->getParam('format_article').'") {
$page=isset($capture[3])?$capture[3]:"";
header("Status: 301 Moved Permanently", false, 301);
header("Location: ".$this->urlRewrite($capture[2]."'.$this->getParam('ext_url').'".$page));
exit();
}
}
if(preg_match("/index.php\?(tag|archives)\/(.*)/", $_SERVER["REQUEST_URI"], $capture)) {
header("Status: 301 Moved Permanently", false, 301);
header("Location: ".$this->urlRewrite($capture[1]."/".$capture[2]));
exit();
}
?>';
}
/**
* Méthode qui rédirige vers la bonne url après soumission d'un commentaire
*
* @author Stephane F
**/
public function plxMotorDemarrageNewCommentaire() {
echo '<?php
$url = $this->urlRewrite("?'.$this->lang.$this->article.'".$this->plxRecord_arts->f("url")."'.$this->getParam('ext_url').'");
?>';
}
/**
* Méthode qui recrée l'url de l'article, page statique ou catégorie au format natif de PluXml
*
* @author Stephane F
**/
public function plxMotorConstruct() {
# récupération de la langue si plugin plxMyMultilingue présent
$this->lang="";
if(defined('PLX_MYMULTILINGUE')) {
$lang = plxMyMultiLingue::_Lang();
if(!empty($lang)) {
if(isset($_SESSION['default_lang']) AND $_SESSION['default_lang']!=$lang) {
$this->lang = $lang.'/';
}
}
}
echo '<?php
if(empty($this->get))
return;
# récupération url
$get = $_SERVER["QUERY_STRING"];
# récupération de la pagination si présente
$page="";
if(preg_match("/(page[0-9]+)$/", $this->get, $capture)) {
$page = "/".$capture[0];
}
# suppression de la page dans url
$get = str_replace($page, "", $get);
# pages statiques
foreach($this->aStats as $numstat => $stat) {
$link = "'.$this->lang.$this->static.'".$stat["url"]."'.$this->getParam('ext_url').'";
if($get==$stat["url"]) {
$get = "'.$this->lang.$this->static.'".$get;
}
if($link==$get) {
$this->get = "'.$this->lang.'static".intval($numstat)."/".$stat["url"];
return;
}
}
# categories
foreach($this->aCats as $numcat => $cat) {
$link = "'.$this->lang.$this->category.'".$cat["url"]."'.$this->getParam('ext_url').'";
if($link==$get) {
$this->get = "'.$this->lang.'categorie".intval($numcat)."/".$cat["url"].$page;
return;
}
}
# articles
foreach($this->plxGlob_arts->aFiles as $numart => $filename) {
if(preg_match("/^[0-9]{4}.([0-9,|home|draft]*).[0-9]{3}.[0-9]{12}.([a-z0-9-]+).xml$/", $filename,$capture)) {
$link = "'.$this->lang.$this->article.'".$capture[2]."'.$this->getParam('ext_url').'";
if($link==$get) {
$this->get = "'.$this->lang.'article".intval($numart)."/".$capture[2];
return;
}
}
}
?>';
}
/**
* Méthode qui nettoie les urls des articles, pages statiques et catégories
*
* @author Stephane F
**/
public function RewriteUrls() {
echo '<?php
$output = preg_replace("/article[0-9]+\/([a-z0-9-]+)/", "'.$this->article.'$1'.$this->getParam('ext_url').'", $output);
$output = preg_replace("/categorie[0-9]+\/([a-z0-9-]+)/", "'.$this->category.'$1'.$this->getParam('ext_url').'", $output);
$output = preg_replace("/static[0-9]+\/([a-z0-9-]+)/", "'.$this->static.'$1'.$this->getParam('ext_url').'", $output);
?>';
}
public function plxFeedPreChauffageBegin() {
# flux rss des articles d'une categorie
echo '<?php
if(preg_match("#^rss/'.$this->category.'([a-z0-9-]+)#", $this->get, $capture)) {
foreach($this->aCats as $numcat => $cat) {
if($cat["url"]==$capture[1]) {
$this->get = "rss/categorie".intval($numcat);
}
}
}
?>';
}
}
?>