Subversion Repositories Sites.tela-botanica.org

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 david 1
<?php
2
 
3
/***************************************************************************\
4
 *  SPIP, Systeme de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright (c) 2001-2005                                                *
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
 
14
// Ce fichier ne sera execute qu'une fois
15
if (defined("_ECRIRE_INC_STATISTIQUES")) return;
16
define("_ECRIRE_INC_STATISTIQUES", "1");
17
 
18
//
19
// Compiler les statistiques temporaires : visites
20
//
21
 
22
// Les deux fonctions suivantes sont adaptees du code des "Visiteurs",
23
// par Jean-Paul Dezelus (http://www.phpinfo.net/applis/visiteurs/)
24
 
25
function stats_load_engines() {
26
	// le moteur de recherche interne
27
	$arr_engines = Array();
28
 
29
	$file_name = 'engines-list.txt';
30
	if ($fp = @fopen($file_name, 'r'))
31
	{
32
		while ($data = fgets($fp, 256))
33
		{
34
			$data = trim(chop($data));
35
 
36
			if (!ereg('^#', $data) && $data != '')
37
			{
38
				if (ereg('^\[(.*)\]$', $data, $engines))
39
				{
40
					// engine
41
					$engine = $engines[1];
42
 
43
					// query | dir
44
					if (!feof($fp))
45
					{
46
						$data = fgets($fp, 256);
47
						$query_or_dir = trim(chop($data));
48
					}
49
				}
50
				else
51
				{
52
					$host = $data;
53
					$arr_engines[] = Array($engine, $query_or_dir, $host);
54
				}
55
			}
56
		}
57
		fclose($fp);
58
	}
59
	return $arr_engines;
60
}
61
 
62
function stats_show_keywords($kw_referer, $kw_referer_host) {
63
	static $arr_engines;
64
	static $url_site;
65
	include_ecrire("inc_filtres.php3");
66
 
67
	if (!$arr_engines) {
68
		// Charger les moteurs de recherche
69
		$arr_engines = stats_load_engines();
70
 
71
		// initialiser la recherche interne
72
		$url_site = lire_meta('adresse_site');
73
		$url_site = strtolower(eregi_replace("^((https?|ftp)://)?(www\.)?", "", $url_site));
74
	}
75
 
76
	$url   = @parse_url( $kw_referer );
77
	$query = $url['query'];
78
	$host  = strtolower($url['host']);
79
	$path  = $url['path'];
80
 
81
	// Cette fonction affecte directement les variables selon la query-string !
82
	parse_str($query);
83
 
84
	$keywords = '';
85
	$found = false;
86
 
87
	if (strpos('-'.$kw_referer, eregi_replace("^(https?:?/?/?)?(www\.)?", "",$url_site))) {
88
		if (eregi("(s|search|r|recherche)=([^&]+)", $kw_referer, $regs))
89
			$keywords = urldecode($regs[2]);
90
 
91
 
92
		else
93
			return '';
94
	} else
95
	for ($cnt = 0; $cnt < sizeof($arr_engines) && !$found; $cnt++)
96
	{
97
		if ($found = (ereg($arr_engines[$cnt][2], $host)))
98
		{
99
			$kw_referer_host = $arr_engines[$cnt][0];
100
 
101
			if (ereg('=', $arr_engines[$cnt][1])) {
102
 
103
				// Fonctionnement simple: la variable existe
104
				$keywords = ${str_replace('=', '', $arr_engines[$cnt][1])};
105
 
106
				// Si on a defini le nom de la variable en expression reguliere, chercher la bonne variable
107
				if (! strlen($keywords) > 0) {
108
					if (ereg($arr_engines[$cnt][1]."([^\&]*)", $query, $vals)) {
109
						$keywords = urldecode($vals[2]);
110
					}
111
				}
112
			} else {
113
				$keywords = "";
114
			}
115
 
116
			if ((  ($kw_referer_host == "Google")
117
				|| ($kw_referer_host == "AOL" && !ereg('enc=iso', $query))
118
				|| ($kw_referer_host == "MSN")
119
				)) {
120
				include_ecrire('inc_charsets.php3');
121
				if (!$cset = $ie) $cset = 'utf-8';
122
				$keywords = importer_charset($keywords,$cset);
123
			}
124
			$buffer["hostname"] = $kw_referer_host;
125
		}
126
	}
127
 
128
	$buffer["host"] = $host;
129
	if (!$buffer["hostname"])
130
		$buffer["hostname"] = $host;
131
 
132
	$buffer["path"] = substr($path, 1, strlen($path));
133
	$buffer["query"] = $query;
134
 
135
	if ($keywords != '')
136
	{
137
		if (strlen($keywords) > 150) {
138
			$keywords = spip_substr($keywords, 0, 148);
139
			// supprimer l'eventuelle entite finale mal coupee
140
			$keywords = preg_replace('/&#?[a-z0-9]*$/', '', $keywords);
141
		}
142
		$buffer["keywords"] = trim(entites_html(stripslashes($keywords)));
143
	}
144
 
145
	return $buffer;
146
 
147
}
148
 
149
 
150
//
151
// Optimiser les informations liees aux referers
152
//
153
 
154
function supprimer_referers($type = "") {
155
	$table = 'spip_referers';
156
	if ($type) {
157
		$table .= '_'. $type . 's';
158
		$col_id = 'id_' . $type;
159
		$query = "SELECT COUNT(DISTINCT $col_id) AS count FROM $table";
160
		$result = spip_query($query);
161
		if ($row = @spip_fetch_array($result)) {
162
			$count = $row['count'];
163
		}
164
	}
165
	if (!$count) $count = 1;
166
 
167
	$query = "SELECT visites FROM $table ".
168
		"ORDER BY visites LIMIT ".intval($count * 100).",1";
169
	$result = spip_query($query);
170
	$visites_min =  1;
171
	if ($row = @spip_fetch_array($result)) {
172
		$visites_min = $row['visites'];
173
	}
174
 
175
	$query = "DELETE FROM $table WHERE (date < DATE_SUB(NOW(),INTERVAL 7 DAY) AND visites <= $visites_min) OR (date < DATE_SUB(NOW(),INTERVAL 30 DAY))";
176
	$result = spip_query($query);
177
}
178
 
179
 
180
 
181
//
182
// Compiler les statistiques temporaires : referers (si active)
183
//
184
 
185
function calculer_n_referers($nb_referers) {
186
	$date = date("Y-m-d");
187
 
188
	$result = spip_query("SELECT COUNT(DISTINCT ip) AS visites, referer, HEX(referer_md5) AS md5 ".
189
			     "FROM spip_referers_temp GROUP BY referer_md5 LIMIT 0,$nb_referers");
190
 
191
	$tous = spip_num_rows($result);
192
 
193
	$referer_insert = "";
194
	$referer_update = "";
195
	$referer_vus = "";
196
 
197
	while ($row = @spip_fetch_array($result)) {
198
		$visites = $row['visites'];
199
		$referer = addslashes($row['referer']);
200
		$referer_md5 = '0x'.$row['md5'];
201
		$referer_update[$visites][] = $referer_md5;
202
		$referer_insert[] = "('$date', '$referer', $referer_md5, $visites, $visites)";
203
		$referer_vus .= "," . $referer_md5;
204
	}
205
	if ($referer_vus)
206
	  $referer_vus = "referer_md5 IN (" . substr($referer_vus,1) . ")";
207
 
208
	// Mise a jour de la base
209
	if (is_array($referer_update)) {
210
		while (list($visites, $referers) = each($referer_update)) {
211
			$query = "UPDATE spip_referers SET visites = visites + $visites, visites_jour = visites_jour + $visites ".
212
				"WHERE referer_md5 IN (".join(', ', $referers).")";
213
			$result = spip_query($query);
214
		}
215
	}
216
	if (is_array($referer_insert)) {
217
		$query_insert = "INSERT IGNORE INTO spip_referers ".
218
			"(date, referer, referer_md5, visites, visites_jour) VALUES ".join(', ', $referer_insert);
219
		$result_insert = spip_query($query_insert);
220
	}
221
 
222
	// Ventiler ces referers article par article
223
	$query = "SELECT COUNT(DISTINCT ip) AS visites, id_objet, referer, HEX(referer_md5) AS md5 FROM spip_referers_temp WHERE type='article'"
224
		  . ($referer_vus ? " AND $referer_vus" : '')
225
		  . " GROUP BY id_objet, referer_md5";
226
	$result = spip_query($query);
227
 
228
	$referer_insert = "";
229
	$referer_update = "";
230
 
231
	while ($row = @spip_fetch_array($result)) {
232
		$id_article = $row['id_objet'];
233
		$visites = $row['visites'];
234
		$referer = addslashes($row['referer']);
235
		$referer_md5 = '0x'.$row['md5'];
236
 
237
		$referer_update[$visites][] = "(id_article=$id_article AND referer_md5=$referer_md5)";
238
		$referer_insert[] = "('$date', '$referer', $referer_md5, $id_article, $visites)";
239
	}
240
 
241
	// Mise a jour de la base
242
	if (is_array($referer_update)) {
243
		while (list($visites, $where) = each($referer_update)) {
244
			$query = "UPDATE spip_referers_articles SET visites = visites + $visites ".
245
				"WHERE ".join(' OR ', $where);
246
			$result = spip_query($query);
247
		}
248
	}
249
	if (is_array($referer_insert)) {
250
		$query_insert = "INSERT IGNORE INTO spip_referers_articles ".
251
			"(date, referer, referer_md5, id_article, visites) VALUES ".join(', ', $referer_insert);
252
		$result_insert = spip_query($query_insert);
253
	}
254
 
255
	// Effacer les referers traites
256
	if ($referer_vus) {
257
	  spip_query("DELETE FROM spip_referers_temp WHERE $referer_vus");
258
	}
259
	return  $tous ;
260
}
261
 
262
 
263
//
264
// Afficher les referers d'un article (ou du site)
265
//
266
function aff_referers ($query, $limit=10, $plus = true) {
267
	// Charger les moteurs de recherche
268
	$arr_engines = stats_load_engines();
269
 
270
	$query .= " LIMIT 0,$limit";
271
	$result = spip_query($query);
272
 
273
	while ($row = spip_fetch_array($result)) {
274
		$referer = interdire_scripts($row['referer']);
275
		$visites = $row['vis'];
276
		$tmp = "";
277
 
278
		$buff = stats_show_keywords($referer, $referer);
279
 
280
		if ($buff["host"]) {
281
			$numero = substr(md5($buff["hostname"]),0,8);
282
 
283
			$nbvisites[$numero] = $nbvisites[$numero] + $visites;
284
 
285
			if (strlen($buff["keywords"]) > 0) {
286
				$criteres = substr(md5($buff["keywords"]),0,8);
287
				if (!$lescriteres[$numero][$criteres])
288
					$tmp = " &laquo;&nbsp;".$buff["keywords"]."&nbsp;&raquo;";
289
				$lescriteres[$numero][$criteres] = true;
290
			} else {
291
				$tmp = $buff["path"];
292
				if (strlen($buff["query"]) > 0) $tmp .= "?".$buff['query'];
293
 
294
				if (strlen($tmp) > 30)
295
					$tmp = "/".substr($tmp, 0, 27)."...";
296
				else if (strlen($tmp) > 0)
297
					$tmp = "/$tmp";
298
			}
299
 
300
			if ($tmp)
301
				$lesreferers[$numero][] = "<a href='$referer'>$tmp</a>" . (($visites > 1)?" ($visites)":"");
302
			else
303
				$lesliensracine[$numero] += $visites;
304
			$lesdomaines[$numero] = $buff["hostname"];
305
			$lesurls[$numero] = $buff["host"];
306
			$lesliens[$numero] = $referer;
307
		}
308
	}
309
 
310
	if (count($nbvisites) > 0) {
311
		arsort($nbvisites);
312
 
313
		$aff = "<ul>";
314
		for (reset($nbvisites); $numero = key($nbvisites); next($nbvisites)) {
315
			if ($lesdomaines[$numero] == '') next;
316
 
317
			$visites = pos($nbvisites);
318
			$ret = "\n<li>";
319
 
320
			if ($visites > 5) $ret .= "<font color='red'>$visites "._T('info_visites')."</font> ";
321
			else if ($visites > 1) $ret .= "$visites "._T('info_visites')." ";
322
			else $ret .= "<font color='#999999'>$visites "._T('info_visite')."</font> ";
323
 
324
			if (count($lesreferers[$numero]) > 1) {
325
				$referers = join ("</li><li>",$lesreferers[$numero]);
326
				$aff .= "<p />";
327
				$aff .= $ret;
328
				$aff .= "<a href='http://".$lesurls[$numero]."'><b><font color='$couleur_foncee'>".$lesdomaines[$numero]."</font></b></a>";
329
				if ($rac = $lesliensracine[$numero]) $aff .= " <font size='1'>($rac)</font>";
330
				$aff .= "<ul><font size='1'><li>$referers</li></font></ul>";
331
				$aff .= "</li><p />\n";
332
			} else {
333
				$aff .= $ret;
334
				$lien = $lesreferers[$numero][0];
335
				if (eregi("^(<a [^>]+>)([^ ]*)( \([0-9]+\))?", $lien, $regs))
336
					$lien = $regs[1].$lesdomaines[$numero].$regs[2];
337
				else
338
					$lien = "<a href='http://".$lesdomaines[$numero]."'>".$lesdomaines[$numero]."</a>";
339
				$aff .= "<b>$lien</b>";
340
				$aff .= "</li>\n";
341
			}
342
		}
343
		$aff .= "</ul>";
344
 
345
		// Le lien pour en afficher "plus"
346
		if ($plus AND (spip_num_rows($result) == $limit)) {
347
			$lien = $GLOBALS['clean_link'];
348
			$lien->addVar('limit',$limit+200);
349
			$aff .= "<div style='text-align:right;'><b><a href='".$lien->getUrl()."'>+++</a></b></div>";
350
		}
351
	}
352
 
353
 
354
	return $aff;
355
}
356
 
357
?>