0ce32c0b1e482c50a1b67663a1eb3aeab5250135
[lhc/web/www.git] / www / ecrire / inc / surligne.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2012 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) return;
14
15 // Ces commentaires vont etre substitue's en mode recherche
16 // voir balise_DEBUT_SURLIGNE et balise_FIN_SURLIGNE
17
18 define('MARQUEUR_SURLIGNE', 'debut_surligneconditionnel');
19 define('MARQUEUR_FSURLIGNE', 'finde_surligneconditionnel');
20
21
22 // http://doc.spip.org/@surligner_mots
23 function surligner_mots($page, $surcharge_surligne = '') {
24 $surlignejs_engines = array(
25 array(",".str_replace(array("/", "."), array("\/", "\."), $GLOBALS['meta']['adresse_site']).",i", ",recherche=([^&]+),i"), //SPIP
26 array(",^http://(www\.)?google\.,i", ",q=([^&]+),i"), // Google
27 array(",^http://(www\.)?search\.yahoo\.,i", ",p=([^&]+),i"), // Yahoo
28 array(",^http://(www\.)?search\.msn\.,i", ",q=([^&]+),i"), // MSN
29 array(",^http://(www\.)?search\.live\.,i", ",query=([^&]+),i"), // MSN Live
30 array(",^http://(www\.)?search\.aol\.,i", ",userQuery=([^&]+),i"), // AOL
31 array(",^http://(www\.)?ask\.com,i", ",q=([^&]+),i"), // Ask.com
32 array(",^http://(www\.)?altavista\.,i", ",q=([^&]+),i"), // AltaVista
33 array(",^http://(www\.)?feedster\.,i", ",q=([^&]+),i"), // Feedster
34 array(",^http://(www\.)?search\.lycos\.,i", ",q=([^&]+),i"), // Lycos
35 array(",^http://(www\.)?alltheweb\.,i", ",q=([^&]+),i"), // AllTheWeb
36 array(",^http://(www\.)?technorati\.com,i", ",([^\?\/]+)(?:\?.*)$,i"), // Technorati
37 );
38
39
40 $ref = $_SERVER['HTTP_REFERER'];
41 //avoid a js injection
42 if ($surcharge_surligne){
43 $surcharge_surligne = preg_replace(",(?<!\\\\)((?:(?>\\\\){2})*)('),", "$1\\\\$2", $surcharge_surligne);
44 $surcharge_surligne = str_replace("\\", "\\\\", $surcharge_surligne);
45 if ($GLOBALS['meta']['charset']=='utf-8'){
46 include_spip('inc/charsets');
47 if (!is_utf8($surcharge_surligne)) $surcharge_surligne = utf8_encode($surcharge_surligne);
48 }
49 }
50 foreach ($surlignejs_engines as $engine)
51 if ($surcharge_surligne || (preg_match($engine[0], $ref) && preg_match($engine[1], $ref))){
52
53 //good referrer found or var_recherche is not null
54 include_spip('inc/filtres');
55 $script = "
56 <script type='text/javascript' src='".url_absolue(find_in_path('javascript/SearchHighlight.js'))."'></script>
57 <script type='text/javascript'>/*<![CDATA[*/
58 if (window.jQuery)
59 (function(\$){\$(function(){
60 \$(document).SearchHighlight({
61 tag_name:'".(html5_permis() ? 'mark' : 'span')."',
62 style_name:'spip_surligne',
63 exact:'whole',
64 style_name_suffix:false,
65 engines:[/^".str_replace(array("/", "."), array("\/", "\."), $GLOBALS['meta']['adresse_site'])."/i,/recherche=([^&]+)/i],
66 highlight:'.surlignable',
67 nohighlight:'.pas_surlignable'".
68 ($surcharge_surligne ? ",
69 keys:'$surcharge_surligne'" : "").",
70 min_length: 3
71 })
72 });
73 })(jQuery);
74 /*]]>*/</script>
75 ";
76 // on l'insere juste avant </head>, sinon tout en bas
77 if (is_null($l = strpos($page, '</head>')))
78 $l = strlen($page);
79 $page = substr_replace($page, $script, $l, 0);
80 break;
81 }
82 return $page;
83 }
84
85 ?>