1 | <?php |
---|
2 | |
---|
3 | # ***** BEGIN LICENSE BLOCK ***** |
---|
4 | # This file is part of DotClear. |
---|
5 | # Copyright (c) 2005 Olivier Meunier and contributors. All rights |
---|
6 | # reserved. |
---|
7 | # |
---|
8 | # DotClear is free software; you can redistribute it and/or modify |
---|
9 | # it under the terms of the GNU General Public License as published by |
---|
10 | # the Free Software Foundation; either version 2 of the License, or |
---|
11 | # (at your option) any later version. |
---|
12 | # |
---|
13 | # DotClear is distributed in the hope that it will be useful, |
---|
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | # GNU General Public License for more details. |
---|
17 | # |
---|
18 | # You should have received a copy of the GNU General Public License |
---|
19 | # along with DotClear; if not, write to the Free Software |
---|
20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | # |
---|
22 | # ***** END LICENSE BLOCK ***** |
---|
23 | |
---|
24 | $core->tpl->addValue('googleSpyPurposePosts',array('googleSpyTpl','purposePosts')); |
---|
25 | |
---|
26 | class googleSpyTpl { |
---|
27 | |
---|
28 | # {{tpl:googleSpyKeyWords}} |
---|
29 | |
---|
30 | public static function track($title, $description, $numLinks, $numKeywords) |
---|
31 | { |
---|
32 | global $core; |
---|
33 | global $_ctx; |
---|
34 | |
---|
35 | try{ |
---|
36 | $google_str = "/^http:\/\/([a-z]+).google\.([a-z]{2,3})|(co\.[a-z]{2})\//i"; |
---|
37 | $keywords = ""; |
---|
38 | |
---|
39 | // Si le referer est Google |
---|
40 | if ( isset($_SERVER['HTTP_REFERER']) && preg_match( $google_str,$_SERVER['HTTP_REFERER']) ) { |
---|
41 | |
---|
42 | $url_array = parse_url($_SERVER['HTTP_REFERER']); |
---|
43 | if (!isset($url_array['query'])){ |
---|
44 | return ""; |
---|
45 | } |
---|
46 | |
---|
47 | parse_str($url_array['query'],$variables); |
---|
48 | |
---|
49 | if (!isset($variables['q'])){ |
---|
50 | return ""; |
---|
51 | } |
---|
52 | |
---|
53 | // On transforme la chaine de caractère en minuscule |
---|
54 | $keywords_text = strtolower(urldecode($variables['q'])); |
---|
55 | |
---|
56 | // On supprime les espaces doublons et on ajoute un + sur le dernier caractère que la suppression |
---|
57 | // des mots à ignorer puisse fonctionner. |
---|
58 | $keywords_text = preg_replace('/\s{1,}/', '+', $keywords_text); |
---|
59 | |
---|
60 | // On ignore les mots suivants |
---|
61 | $ignore = array( "un", "une", |
---|
62 | "de", "du", "des", |
---|
63 | "la", "le", |
---|
64 | "pour", "sans", "avec", |
---|
65 | "sous", "dessus", |
---|
66 | "tu", "je", "il", "elle", "on", "nous", "vous", "ils", "elles", |
---|
67 | "mes ", "mon", "ton", "son ", "ses", "tes", "mes", |
---|
68 | "as ", "ai", "ont", "avons ", "avez", |
---|
69 | "suis ", "es", "est", "sont", "êtes", |
---|
70 | "on", "i", "your", "it", "its", "my", "she", "he", "you", "the", "a", "we"); |
---|
71 | |
---|
72 | // On découpe la chaine |
---|
73 | $keywords = split('\+',$keywords_text); |
---|
74 | |
---|
75 | if (count($keywords) > 0){ |
---|
76 | $strReq ='SELECT P.post_url, P.post_title '. |
---|
77 | 'FROM '.$core->prefix.'post P WHERE P.post_status=1 '. |
---|
78 | 'AND P.blog_id="'.$core->blog->id.'" '. |
---|
79 | 'AND P.post_id!="'.$_ctx->posts->post_id.'" AND ('; |
---|
80 | |
---|
81 | $count = 1; |
---|
82 | foreach ($keywords as $i => $w) { |
---|
83 | if (!in_array($w,$ignore) && $count <= $numKeywords){ |
---|
84 | $keywords_sql[$i] = "P.post_words LIKE '%".$core->con->escape($w)."%'"; |
---|
85 | $count++; |
---|
86 | } else { |
---|
87 | $keywords[$i] = null; |
---|
88 | } |
---|
89 | } |
---|
90 | $strReq .= implode(' OR ',$keywords_sql).' '; |
---|
91 | $strReq .= ") ORDER by P.post_dt desc "; |
---|
92 | $strReq .= $core->con->limit($numLinks); |
---|
93 | |
---|
94 | // On lance la requête |
---|
95 | $rs = $core->con->select($strReq); |
---|
96 | |
---|
97 | $buffer = ""; |
---|
98 | |
---|
99 | // Si des articles ont été trouvés |
---|
100 | if(!$rs->isEmpty()){ |
---|
101 | |
---|
102 | $buffer="<div id=\"purposedLinks\"><h3>".$title."</h3>"; |
---|
103 | if ($description != ""){ |
---|
104 | $buffer.="<p>".$description."</p>"; |
---|
105 | } |
---|
106 | $buffer.="<ul>"; |
---|
107 | while ($rs->fetch()) { |
---|
108 | $buffer.="<li><a href=\"".$core->blog->url."post/".$rs->post_url."\"/>".bold($rs->post_title,$keywords)."</a></li>"; |
---|
109 | } |
---|
110 | $buffer.="</ul><br />"; |
---|
111 | |
---|
112 | |
---|
113 | //foreach($keywords as $keyword){ |
---|
114 | // if ($keyword != null){ |
---|
115 | // $buffer.= $keyword." / "; |
---|
116 | // } |
---|
117 | //} |
---|
118 | $buffer.="</div>"; |
---|
119 | } |
---|
120 | |
---|
121 | return $buffer; |
---|
122 | } |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | } catch (Exception $e) { |
---|
127 | $core->error->add($e->getMessage()); |
---|
128 | } |
---|
129 | |
---|
130 | return ""; |
---|
131 | |
---|
132 | } |
---|
133 | |
---|
134 | public static function purposePosts($args) |
---|
135 | { |
---|
136 | if (isset($args["num_links"])){ |
---|
137 | $numLinks = $args["num_links"]; |
---|
138 | } else { |
---|
139 | $numLinks = 5; |
---|
140 | } |
---|
141 | |
---|
142 | if (isset($args["num_keywords"])){ |
---|
143 | $numKeywords = $args["num_keywords"]; |
---|
144 | } else { |
---|
145 | $numKeywords = 3; |
---|
146 | } |
---|
147 | |
---|
148 | if (isset($args["title"])){ |
---|
149 | $title = $args["title"]; |
---|
150 | } else { |
---|
151 | $title = "A Lire :"; |
---|
152 | } |
---|
153 | |
---|
154 | if (isset($args["description"])){ |
---|
155 | $description = $args["description"]; |
---|
156 | } else { |
---|
157 | $description = ""; |
---|
158 | } |
---|
159 | |
---|
160 | $phpCode = '<?php echo googleSpyTpl::track("'.$title.'","'.$description.'",'.$numLinks.','.$numKeywords.') ?>'; |
---|
161 | |
---|
162 | return $phpCode; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | function bold ($texte, $mots) { |
---|
167 | if (!is_array ($mots) || empty ($mots) || !is_string ($texte)) { |
---|
168 | return false; |
---|
169 | }else{ |
---|
170 | $mots=implode ('|', $mots); |
---|
171 | return preg_replace ('@\b('.$mots.')\b@si', '<strong>$1</strong>', $texte); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|