[PLUGINS] +set de base
[lhc/web/www.git] / www / plugins / seo-v1 / seo_fonctions.php
1 <?php
2
3 if (!defined("_ECRIRE_INC_VERSION")) return;
4
5 include_spip('inc/texte');
6
7 /**
8 * Renvoyer la balise <link> pour URL CANONIQUES
9 * @return string $flux
10 */
11 function generer_urls_canoniques(){
12 include_spip('balise/url_');
13
14 if (count($GLOBALS['contexte'])==0){
15 $objet = 'sommaire';
16 } elseif (isset($GLOBALS['contexte']['id_article'])) {
17 $id_objet = $GLOBALS['contexte']['id_article'];
18 $objet = 'article';
19 } elseif (isset($GLOBALS['contexte']['id_rubrique'])) {
20 $id_objet = $GLOBALS['contexte']['id_rubrique'];
21 $objet = 'rubrique';
22 }
23
24 switch ($objet) {
25 case 'sommaire':
26 $flux .= '<link rel="canonical" href="' . url_de_base() . '" />';
27 break;
28 default:
29 $flux .= '<link rel="canonical" href="' . url_de_base() . generer_url_entite($id_objet, $objet) . '" />';
30 break;
31 }
32
33 return $flux;
34 }
35
36 /**
37 * Renvoyer la balise SCRIPT de Google Analytics
38 * @return string $flux
39 */
40 function generer_google_analytics(){
41 /* CONFIG */
42 $config = unserialize($GLOBALS['meta']['seo']);
43
44 /* GOOGLE ANALYTICS */
45 if ($config['analytics']['id']){
46 // Nouvelle balise : http://www.google.com/support/analytics/bin/answer.py?hl=fr_FR&answer=174090&utm_id=ad
47 $flux .= "<script type=\"text/javascript\">
48 var _gaq = _gaq || [];
49 _gaq.push(['_setAccount', '" . $config['analytics']['id'] . "']);
50 _gaq.push(['_trackPageview']);
51 (function() {
52 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
53 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
54 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
55 })();
56 </script>
57 ";
58 }
59
60 return $flux;
61 }
62
63 /**
64 * Renvoyer les META Classiques
65 * - Meta Titre / Description / etc.
66 * @return string $flux
67 */
68 function calculer_meta_tags(){
69
70 include_spip('inc/texte');
71
72 /* CONFIG */
73 $config = unserialize($GLOBALS['meta']['seo']);
74
75 if (isset($GLOBALS['contexte']['id_article'])){
76 $id_objet = $GLOBALS['contexte']['id_article'];
77 $objet = 'article';
78 } elseif (isset($GLOBALS['contexte']['id_rubrique'])) {
79 $id_objet = $GLOBALS['contexte']['id_rubrique'];
80 $objet = 'rubrique';
81 } else {
82 $objet = 'sommaire';
83 }
84
85 /* META TAGS */
86
87 // If the meta tags configuration is activate
88 $meta_tags = array();
89
90 switch ($objet) {
91 case 'sommaire':
92 $meta_tags = $config['meta_tags']['tag'];
93 break;
94 default:
95 $table = table_objet_sql($objet);
96 $id_table_objet = id_table_objet($objet);
97 $title = couper(sql_getfetsel("titre", $table, "$id_table_objet = " . intval($id_objet)), 64);
98 $requete = sql_allfetsel("descriptif,texte", $table, "$id_table_objet = " . intval($id_objet));
99 if ($requete) $description = couper(implode(" ", $requete[0]), 150, '');
100 // Get the value set by default
101 foreach ($config['meta_tags']['default'] as $name => $option){
102 if ($option=='sommaire'){
103 $meta_tags[$name] = $config['meta_tags']['tag'][$name];
104 } elseif ($option=='page') {
105 if ($name=='title') $meta_tags['title'] = $title;
106 if ($name=='description') $meta_tags['description'] = $description;
107 } elseif ($option=='page_sommaire') {
108 if ($name=='title') $meta_tags['title'] = $title . (($title!='') ? ' - ' : '') . $config['meta_tags']['tag'][$name];
109 if ($name=='description') $meta_tags['description'] = $description . (($description!='') ? ' - ' : '') . $config['meta_tags']['tag'][$name];
110 }
111 }
112
113 // If the meta tags rubrique and articles editing is activate (should overwrite other setting)
114 if ($config['meta_tags']['activate_editing']=='yes' && ($objet=='article' || $objet=='rubrique')){
115 $result = sql_select("*", "spip_seo", "id_objet = " . intval($id_objet) . " AND objet = " . sql_quote($objet));
116 while ($r = sql_fetch($result)){
117 if ($r['meta_content']!='')
118 $meta_tags[$r['meta_name']] = $r['meta_content'];
119 }
120 }
121 break;
122 }
123
124 return $meta_tags;
125 }
126
127 function generer_meta_tags($meta_tags = null){
128 $flux = '';
129 //Set meta list if not provided
130 if (!is_array($meta_tags))
131 $meta_tags = calculer_meta_tags();
132
133 // Print the result on the page
134 foreach ($meta_tags as $name => $content){
135 if ($content!='')
136 if ($name=='title')
137 $flux .= '<title>' . trim(htmlspecialchars(supprimer_numero(textebrut(propre($content))))) . '</title>' . "\n";
138 else
139 $flux .= '<meta name="' . $name . '" content="' . trim(htmlspecialchars(textebrut(propre($content)))) . '" />' . "\n";
140
141 }
142 return $flux;
143 }
144
145 /**
146 * Renvoyer une META toute seule (hors balise)
147 * @param string $nom
148 * @return string|bool
149 */
150 function generer_meta_brute($nom){
151 $config = unserialize($GLOBALS['meta']['seo']);
152 $nom = strtolower($nom);
153
154 if ($config['meta_tags']['tag'][$nom]){
155 return $config['meta_tags']['tag'][$nom];
156 }
157
158 return false;
159 }
160
161 /**
162 * Renvoyer la META GOOGLE WEBMASTER TOOLS
163 * @return string $flux
164 */
165 function generer_webmaster_tools(){
166 /* CONFIG */
167 $config = unserialize($GLOBALS['meta']['seo']);
168
169 if ($config['webmaster_tools']['id'])
170 return '<meta name="google-site-verification" content="' . $config['webmaster_tools']['id'] . '" />
171 ';
172 }
173
174
175 /**
176 * Renvoyer la META BING TOOLS
177 * @return string $flux
178 */
179 function generer_bing(){
180 /* CONFIG */
181 $config = unserialize($GLOBALS['meta']['seo']);
182
183 if ($config['bing']['id'])
184 return '<meta name="msvalidate.01" content="' . $config['bing']['id'] . '" />
185 ';
186 }
187
188 /**
189 * Renvoyer la META ALEXA
190 * @return string $flux
191 */
192 function generer_alexa(){
193 /* CONFIG */
194 $config = unserialize($GLOBALS['meta']['seo']);
195
196 if ($config['alexa']['id'])
197 return '<meta name="alexaVerifyID" content="' . $config['alexa']['id'] . '"/>';
198 }
199
200 /**
201 * #SEO_URL
202 * Renvoyer la balise <link> pour URL CANONIQUES
203 */
204 function balise_SEO_URL($p){
205 $p->code = "calculer_balise_SEO_URL()";
206 return $p;
207 }
208
209 function calculer_balise_SEO_URL(){
210 $flux = generer_urls_canoniques();
211 return $flux;
212 }
213
214 /**
215 * #SEO_GA
216 * Renvoyer la balise SCRIPT de Google Analytics
217 */
218 function balise_SEO_GA($p){
219 $p->code = "calculer_balise_SEO_GA()";
220 return $p;
221 }
222
223 function calculer_balise_SEO_GA(){
224 $flux = generer_google_analytics();
225 return $flux;
226 }
227
228 /**
229 * #SEO_META_TAGS
230 * Renvoyer les META Classiques
231 * - Meta Titre / Description / etc.
232 */
233 function balise_SEO_META_TAGS($p){
234 $p->code = "calculer_balise_SEO_META_TAGS()";
235 return $p;
236 }
237
238 function calculer_balise_SEO_META_TAGS(){
239 $flux = generer_meta_tags();
240 return $flux;
241 }
242
243 /**
244 * #SEO_META_BRUTE{nom_de_la_meta}
245 * Renvoyer la valeur de la meta appelée (sans balise)
246 */
247 function balise_SEO_META_BRUTE($p){
248 $_nom = str_replace("'", "", interprete_argument_balise(1, $p));
249 $p->code = "calculer_balise_META_BRUTE($_nom)";
250 $p->interdire_scripts = false;
251 return $p;
252 }
253
254 function calculer_balise_META_BRUTE($_nom){
255 $metas = calculer_meta_tags();
256 $meta = $metas[$_nom];
257 if (!$meta)
258 return "";
259 return $meta;
260 }
261
262 /**
263 * #SEO_GWT
264 * Renvoyer la META GOOGLE WEBMASTER TOOLS
265 */
266 function balise_SEO_GWT($p){
267 $p->code = "calculer_balise_SEO_GWT()";
268 return $p;
269 }
270
271 function calculer_balise_SEO_GWT(){
272 $flux = generer_webmaster_tools();
273 return $flux;
274 }
275