[PLUGINS] ~maj globale
[lhc/web/www.git] / www / plugins / seo-dev / seo_fonctions.php
1 <?php
2 /**
3 * BouncingOrange SPIP SEO plugin
4 *
5 * @category SEO
6 * @package SPIP\SEO\Fonctions
7 * @author Pierre ROUSSET (p.rousset@gmail.com)
8 * @copyright Copyright (c) 2009 BouncingOrange (http://www.bouncingorange.com)
9 * @license http://opensource.org/licenses/gpl-2.0.php General Public License (GPL 2.0)
10 */
11
12 if (!defined('_ECRIRE_INC_VERSION')) {
13 return;
14 }
15
16 include_spip('inc/texte');
17
18 function seo_interprete_contexte($contexte) {
19 static $infos;
20 $s = serialize($contexte);
21 if (isset($infos[$s])) {
22 return $infos[$s];
23 }
24 $infos[$s] = array('objet' => 'sommaire');
25 if (isset($contexte['page'])) {
26 $infos[$s] = array('objet' => $contexte['page']);
27 }
28 if (isset($contexte['type-page'])) {
29 $infos[$s]['objet'] = $contexte['type-page'];
30 if ($infos[$s]['objet']!=='sommaire'
31 and $primary = id_table_objet($infos[$s]['objet'])
32 and isset($contexte[$primary])) {
33 $infos[$s]['id_objet'] = $contexte[$primary];
34 $infos[$s]['primary'] = $primary;
35 $infos[$s]['table_sql'] = table_objet_sql($infos[$s]['objet']);
36 }
37 return $infos[$s];
38 }
39 // d'abord les rubriques
40 if (isset($contexte['id_rubrique'])) {
41 $infos[$s] = array(
42 'objet'=>'rubrique',
43 'id_objet'=>$contexte['id_rubrique'],
44 'primary'=>'id_rubrique',
45 'table_sql'=>'spip_rubriques'
46 );
47 }
48 // puis voyons si on trouve un objet plus precis
49 $tables = lister_tables_objets_sql();
50 foreach ($tables as $t => $d) {
51 if ($t!=='spip_rubriques'
52 and isset($d['key']['PRIMARY KEY'])
53 and ($infos[$s]['objet']!=='rubrique'
54 or isset($d['field']['id_rubrique']))
55 ) {
56 $primary = $d['key']['PRIMARY KEY'];
57 if (isset($contexte[$primary])) {
58 $infos[$s]['objet'] = $d['type'];
59 $infos[$s]['id_objet'] = $contexte[$primary];
60 $infos[$s]['primary'] = $primary;
61 $infos[$s]['table_sql'] = $t;
62 return $infos[$s];
63 }
64 }
65 }
66 return $infos[$s];
67 }
68
69 /**
70 * Remplace les meta du head par celles calculees par le plugin
71 * utilise par le squelette inclure/seo-head
72 *
73 * @param string $head
74 * @param array $contexte
75 * @return string
76 */
77 function seo_insere_remplace_metas($head, $contexte) {
78
79 $append = '<!--seo_insere-->';
80 // on ne fait rien si deja insere
81 if (strpos($head, $append) !== false) {
82 return $head;
83 }
84
85 include_spip('inc/config');
86 $config = lire_config('seo/');
87 $i = seo_interprete_contexte($contexte);
88 $is_sommaire = ($i['objet']=='sommaire');
89
90 if (isset($config['meta_tags']['activate']) and $config['meta_tags']['activate']=='yes') {
91 /* d'abord les meta tags */
92 $meta_tags = seo_generer_meta_tags(null, $contexte);
93
94 foreach ($meta_tags as $key => $meta) {
95 $preg = '';
96 if ($key=='title') {
97 /**
98 * Si le tag est <title>
99 */
100 $preg = "/(<{$key}[^>]*>.*<\/{$key}[^>]*>)/Uims";
101 } else {
102 /**
103 * Le tag est une <meta>
104 */
105 $preg = "/(<meta\s+name=['\"]{$key}['\"][^>]*>)/Uims";
106 }
107
108 // remplacer la meta si on la trouve
109 if ($preg and preg_match($preg, $head, $match)) {
110 if (stristr($match[0], "data-strict") === FALSE)
111 $head = str_replace($match[0], $meta, $head);
112 } else {
113 $append .= "$meta\n";
114 }
115 }
116 }
117 /* META GOOGLE WEBMASTER TOOLS */
118 if (isset($config['webmaster_tools'])
119 and $config['webmaster_tools']['activate']=='yes'
120 and $is_sommaire) {
121 $append .= "\n" . seo_generer_webmaster_tools();
122 }
123
124 if (isset($config['bing'])
125 and $config['bing']['activate'] == 'yes'
126 and $is_sommaire) {
127 $append .= "\n" . seo_generer_bing();
128 }
129
130 /* CANONICAL URL */
131 if (isset($config['canonical_url'])
132 and $config['canonical_url']['activate']=='yes') {
133 $append .= "\n" . seo_generer_urls_canoniques($contexte);
134 }
135
136 /* GOOGLE ANALYTICS */
137 if (isset($config['analytics'])
138 and $config['analytics']['activate']=='yes') {
139 $append .= "\n" . seo_generer_google_analytics();
140 }
141
142 /* ALEXA */
143 if (isset($config['alexa'])
144 and $config['alexa']['activate']=='yes'
145 and $is_sommaire) {
146 $append .= "\n" . seo_generer_alexa();
147 }
148
149 if ($append) {
150 $append = "\n$append\n";
151 // sinon ajouter en fin de </head>
152 if ($p = stripos($head, '</head>')) {
153 $head = substr_replace($head, $append, $p, 0);
154 } else {
155 $head .= $append;
156 }
157 }
158
159 return $head;
160 }
161
162 /**
163 * Renvoyer la balise <link> pour URL CANONIQUES
164 *
165 * @param array $contexte
166 * @return string
167 */
168 function seo_generer_urls_canoniques($contexte) {
169 include_spip('inc/urls');
170 $i = seo_interprete_contexte($contexte);
171
172 if (isset($i['id_objet'])) {
173 return '<link rel="canonical" href="' . generer_url_entite_absolue($i['id_objet'], $i['objet']) . '" />';
174 } elseif ($i['objet'] == 'sommaire') {
175 return '<link rel="canonical" href="' . url_de_base() . '" />';
176 }
177
178 return '';
179 }
180
181 /**
182 * Renvoyer la balise SCRIPT de Google Analytics
183 *
184 * @return string
185 */
186 function seo_generer_google_analytics() {
187 include_spip('inc/config');
188
189 /* GOOGLE ANALYTICS */
190 if ($id = lire_config('seo/analytics/id')) {
191 $id = texte_script($id);
192 // Nouvelle balise : http://www.google.com/support/analytics/bin/answer.py?hl=fr_FR&answer=174090&utm_id=ad
193 if (!lire_config('seo/analytics/universal')) {
194 return "<script type=\"text/javascript\">
195 var _gaq = _gaq || [];
196 _gaq.push(['_setAccount', '$id']);
197 _gaq.push(['_trackPageview']);
198 (function() {
199 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
200 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
201 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
202 })();
203 </script>
204 ";
205 } else {
206 return "<script type=\"text/javascript\">
207 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
208 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
209 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
210 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
211 ga('create', '$id', 'auto');
212 ga('send', 'pageview');
213 </script>
214 ";
215 }
216 }
217
218 return '';
219 }
220
221 /**
222 * Renvoyer les META Classiques
223 * - Meta Titre / Description / etc.
224 *
225 * @param null|array $contexte
226 * @return array
227 */
228 function seo_calculer_meta_tags($contexte = null) {
229 include_spip('inc/config');
230 include_spip('inc/filtres');
231 $config = lire_config('seo/');
232
233 if (is_null($contexte)) {
234 $contexte = $GLOBALS['contexte'];
235 }
236 $i = seo_interprete_contexte($contexte);
237
238 /* META TAGS */
239
240 // If the meta tags configuration is activate
241 $meta_tags = array();
242
243 if (isset($i['id_objet'])) {
244 $trouver_table = charger_fonction('trouver_table', 'base');
245 $desc = $trouver_table($i['table_sql']);
246 $select = array();
247 if (isset($desc['titre'])) {
248 $select[] = $desc['titre'];
249 } elseif (isset($desc['field']['titre'])) {
250 $select[] = 'titre';
251 }
252 if (isset($desc['field']['descriptif'])) {
253 $select[] = 'descriptif';
254 }
255 if (isset($desc['field']['chapo'])) {
256 $select[] = 'chapo';
257 }
258 if (isset($desc['field']['texte'])) {
259 $select[] = 'texte';
260 }
261
262 $tag = array();
263 if (count($select)) {
264 $select = implode(',', $select);
265 $row = sql_fetsel($select, $i['table_sql'], $i['primary'].'=' . intval($i['id_objet']));
266 if ($row) {
267 if (isset($row['titre'])) {
268 $tag['title'] = couper(extraire_multi($row['titre'], isset($contexte['lang']) ? $contexte['lang'] : $GLOBALS['spip_lang']), 64);
269 unset($row['titre']);
270 }
271 if (isset($row['lang'])) {
272 unset($row['lang']);
273 }
274
275 if (count($row)) {
276 $tag['description'] = couper(implode(' ', $row), 150, '');
277 }
278 }
279 }
280 // Get the value set by default
281 if (isset($config['meta_tags']['default']) and is_array($config['meta_tags']['default'])) {
282 foreach ($config['meta_tags']['default'] as $name => $option) {
283 $meta_tags[$name] = array();
284 if (in_array($option, array('page', 'page_sommaire'))) {
285 if (isset($tag[$name])) {
286 $meta_tags[$name][] = $tag[$name];
287 }
288 }
289 if (in_array($option, array('sommaire','page_sommaire'))) {
290 if (isset($config['meta_tags']['tag'][$name])) {
291 $meta_tags[$name][] = $config['meta_tags']['tag'][$name];
292 }
293 }
294 if (count($meta_tags[$name])) {
295 $meta_tags[$name] = implode(' - ', $meta_tags[$name]);
296 } else {
297 unset($meta_tags[$name]);
298 }
299 }
300 }
301
302 // If the meta tags rubrique and articles editing is activate (should overwrite other setting)
303 if (isset($config['meta_tags']['activate_editing'])
304 and $config['meta_tags']['activate_editing'] == 'yes') {
305 $result = sql_select('*', 'spip_seo', 'id_objet=' . intval($i['id_objet']) . ' AND objet=' . sql_quote($i['objet']));
306 while ($r = sql_fetch($result)) {
307 if ($r['meta_content'] != '') {
308 $meta_tags[$r['meta_name']] = $r['meta_content'];
309 }
310 }
311 }
312 } elseif ($i['objet'] == 'sommaire') {
313 $meta_tags = isset($config['meta_tags']['tag'])?$config['meta_tags']['tag']:array();
314 }
315 $meta_tags = pipeline('post_seo', array
316 (
317 'args' => array(
318 'contexte' => $contexte,
319 'config' => $config
320 ),
321 'data' => $meta_tags
322 )
323 );
324 return $meta_tags;
325 }
326
327 /**
328 * @param null|array $contexte
329 * @param null|array $meta_tags
330 * @return array
331 */
332 function seo_generer_meta_tags($meta_tags = null, $contexte = null) {
333 $tags = array();
334 //Set meta list if not provided
335 if (!is_array($meta_tags)) {
336 $meta_tags = seo_calculer_meta_tags($contexte);
337 }
338
339 // Print the result on the page
340 foreach ($meta_tags as $name => $content) {
341 if ($content!='') {
342 if ($name == 'title') {
343 $tags[$name] = '<title>' . trim(entites_html(supprimer_numero(textebrut(propre($content))))) . '</title>';
344 } else {
345 $tags[$name] = '<meta name="' . $name . '" content="' . trim(attribut_html(textebrut(propre($content)))) . '" />';
346 }
347 }
348 }
349 return $tags;
350 }
351
352 /**
353 * Renvoyer une META toute seule (hors balise)
354 * @param string $nom
355 * @return string
356 */
357 function seo_generer_meta_brute($nom) {
358 include_spip('inc/config');
359 return lire_config("seo/meta_tags/tag/$nom", '');
360 }
361
362 /**
363 * Renvoyer la META GOOGLE WEBMASTER TOOLS
364 * @return string
365 */
366 function seo_generer_webmaster_tools() {
367 include_spip('inc/config');
368 if ($id = lire_config('seo/webmaster_tools/id')) {
369 return '<meta name="google-site-verification" content="' . texte_script($id) . '" />';
370 }
371 }
372
373
374 /**
375 * Renvoyer la META BING TOOLS
376 * @return string
377 */
378 function seo_generer_bing() {
379 include_spip('inc/config');
380 if ($id=lire_config('seo/bing/id')) {
381 return '<meta name="msvalidate.01" content="' . texte_script($id) . '" />';
382 }
383 }
384
385 /**
386 * Renvoyer la META ALEXA
387 * @return string
388 */
389 function seo_generer_alexa() {
390 include_spip('inc/config');
391 if ($id=lire_config('seo/alexa/id')) {
392 return '<meta name="alexaVerifyID" content="' . texte_script($id) . '" />';
393 }
394 }
395
396 /**
397 * #SEO_URL
398 * Renvoyer la balise <link> pour URL CANONIQUES
399 * @param $p
400 */
401 function balise_SEO_URL_dist($p) {
402 $p->code = "seo_generer_urls_canoniques(\$Pile[0])";
403 $p->interdire_scripts = false;
404 return $p;
405 }
406
407 /**
408 * #SEO_GA
409 * Renvoyer la balise SCRIPT de Google Analytics
410 * @param $p
411 */
412 function balise_SEO_GA_dist($p) {
413 $p->code = "seo_generer_google_analytics()";
414 $p->interdire_scripts = false;
415 return $p;
416 }
417
418 /**
419 * #SEO_META_TAGS
420 * Renvoyer les META editoriales
421 * - Meta Titre / Description / etc.
422 * @param $p
423 */
424 function balise_SEO_META_TAGS_dist($p) {
425 $p->code = 'implode("\\n",seo_generer_meta_tags(null,$Pile[0]))';
426 $p->interdire_scripts = false;
427 return $p;
428 }
429
430 /**
431 * #SEO_META_BRUTE{nom_de_la_meta}
432 * Renvoyer la valeur de la meta appelée (sans balise)
433 * @param $p
434 */
435 function balise_SEO_META_BRUTE_dist($p) {
436 $_nom = str_replace("'", '', interprete_argument_balise(1, $p));
437 $p->code = "table_valeur(seo_calculer_meta_tags(),$_nom,'')";
438 $p->interdire_scripts = false;
439 return $p;
440 }
441
442 /**
443 * #SEO_GWT
444 * Renvoyer la META GOOGLE WEBMASTER TOOLS
445 * @param $p
446 */
447 function balise_SEO_GWT_dist($p) {
448 $p->code = "seo_generer_webmaster_tools()";
449 $p->interdire_scripts = false;
450 return $p;
451 }
452
453 /**
454 * #SEO : insere toutes les meta d'un coup, a l'endroit indique
455 * @param $p
456 */
457 function balise_SEO_dist($p) {
458 $p->code = "seo_insere_remplace_metas('',\$Pile[0])";
459 $p->interdire_scripts = false;
460 return $p;
461 }