You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
8.6 KiB

<?php
/***************************************************************************\
* SPIP, Systeme de publication pour l'internet *
* *
* Copyright (c) 2001-2011 *
* Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
* *
* Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
* Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
\***************************************************************************/
if (!defined("_ECRIRE_INC_VERSION")) return;
//
// Afficher les referers d'un article (ou du site)
//
// http://doc.spip.org/@inc_referenceurs_dist
12 years ago
function inc_referenceurs_dist($id_article, $select, $table, $where, $groupby, $limit, $serveur = ''){
$nbvisites = $lescriteres = array();
12 years ago
$result = sql_select("referer_md5, referer, $select AS vis", $table, $where, $groupby, "maj DESC", $limit, '', $serveur);
while ($row = sql_fetch($result, $serveur)){
$referer = interdire_scripts($row['referer']);
$buff = stats_show_keywords($referer, $referer);
12 years ago
if ($buff["host"]){
$numero = $buff["hostname"];
$visites = $row['vis'];
$referermd5 = $row['referer_md5'];
$lesreferermd5[$numero] = $referermd5;
$lesliens[$numero] = $referer;
$lesurls[$numero] = $buff["host"];
if (!isset($nbvisites[$numero]))
$nbvisites[$numero] = $visites;
else
$nbvisites[$numero] += $visites;
if (!isset($lesreferers[$numero]))
$lesreferers[$numero] = array();
if (!isset($lesliensracine[$numero]))
12 years ago
$lesliensracine[$numero] = 0;
if (isset($buff["keywords"])
12 years ago
AND $c = $buff["keywords"]){
if (!isset($lescriteres[$numero][$c])){
$lescriteres[$numero][$c] = true;
12 years ago
$tmp = " &laquo;&nbsp;$c&nbsp;&raquo;";
} else $tmp = "";
} else {
$tmp = $buff["path"];
if ($buff["query"])
$tmp .= "?".$buff['query'];
12 years ago
if (strlen($tmp)>18)
$tmp = "/".substr($tmp, 0, 15)."...";
12 years ago
else if (strlen($tmp)>0)
$tmp = "/$tmp";
}
12 years ago
if ($tmp){
$lesreferers[$numero][] = "<a href=\"".attribut_html($referer)."\">".quote_amp(urldecode($tmp))."</a>".(($visites>1) ? " ($visites)" : "").($id_article ? '' : referes($referermd5));
} else $lesliensracine[$numero] += $visites;
}
}
12 years ago
if (!count($nbvisites)) return array();
arsort($nbvisites);
return referers_group($nbvisites, $id_article, $lesliensracine, $lesreferermd5, $lesreferers, $lesurls);
}
12 years ago
function referers_group($nbvisites, $id_article, $lesliensracine, $lesreferermd5, $lesreferers, $lesurls){
global $spip_lang_right, $source_vignettes;
12 years ago
$vign = ((strlen($source_vignettes)>0) &&
$GLOBALS['meta']["activer_captures_referers"]!='non');
$aff = array();
12 years ago
foreach ($nbvisites as $numero => $visites){
if (!$numero) next;
$referermd5 = $lesreferermd5[$numero];
$bouton = $ret = "";
if ($vign)
$ret = "\n<a href=\"http://".$lesurls[$numero]."\"><img src=\"$source_vignettes".rawurlencode($lesurls[$numero])."\"\nstyle=\"float: $spip_lang_right; margin-bottom: 3px; margin-left: 3px;\" alt='' /></a>";
12 years ago
if ($visites>5) $bouton .= "<span class='visites visites3'>$visites "._T('info_visites')."</span> ";
else if ($visites>1) $bouton .= "<span class='visites visites2'>$visites "._T('info_visites')."</span> ";
else $bouton .= "<span class='visites visites1'>$visites "._T('info_visite')."</span> ";
12 years ago
if ($numero=="(email)"){
$ret .= $bouton."<b>".$numero."</b>";
}
else {
$n = count($lesreferers[$numero]);
12 years ago
if (($n>1) || ($n>0 && substr(supprimer_tags($lesreferers[$numero][0]), 0, 1)!='/')){
$rac = $lesliensracine[$numero];
$bouton .= "<a href=\"http://".attribut_html($lesurls[$numero])."\" style='font-weight: bold;'>".$numero."</a>"
12 years ago
.(!$rac ? '' : (" <span class='spip_x-small'>(".$rac.")</span>"));
$ret .= bouton_block_depliable($bouton, false)
.debut_block_depliable(false)
."\n<ul><li>"
.join("</li><li>", $lesreferers[$numero])
."</li></ul>"
.fin_block();
} else {
$ret .= $bouton;
$lien = $n ? $lesreferers[$numero][0] : '';
12 years ago
if (preg_match(",^(<a [^>]+>)([^ ]*)( \([0-9]+\))?,i", $lien, $regs)){
$lien = quote_amp($regs[1]).$numero.$regs[2];
if (!strpos($lien, '</a>')) $lien .= '</a>';
} else
$lien = "<a href=\"http://".attribut_html($numero)."\">".$numero."</a>";
$ret .= "<b>".quote_amp($lien)."</b>"
12 years ago
.($id_article ? '' : referes($referermd5));
}
}
12 years ago
$aff[] = $ret;
}
return $aff;
}
// Les deux fonctions suivantes sont adaptees du code des "Visiteurs",
// par Jean-Paul Dezelus (http://www.phpinfo.net/applis/visiteurs/)
// http://doc.spip.org/@stats_load_engines
12 years ago
function stats_load_engines(){
$arr_engines = Array();
lire_fichier(find_in_path('engines-list.txt'), $moteurs);
12 years ago
foreach (array_filter(preg_split("/([\r\n]|#.*)+/", $moteurs)) as $ligne){
$ligne = trim($ligne);
12 years ago
if (preg_match(',^\[([^][]*)\]$,S', $ligne, $regs)){
$moteur = $regs[1];
$query = '';
} else if (preg_match(',=$,', $ligne, $regs))
$query = $ligne;
else
12 years ago
$arr_engines[] = array($moteur, $query, $ligne);
}
return $arr_engines;
}
// http://doc.spip.org/@stats_show_keywords
12 years ago
function stats_show_keywords($kw_referer, $kw_referer_host){
static $arr_engines = '';
static $url_site;
12 years ago
if (!is_array($arr_engines)){
// Charger les moteurs de recherche
$arr_engines = stats_load_engines();
// initialiser la recherche interne
$url_site = $GLOBALS['meta']['adresse_site'];
$url_site = preg_replace(",^((https?|ftp):?/?/?)?(www\.)?,", "", strtolower($url_site));
}
12 years ago
if ($url = @parse_url($kw_referer)){
$query = isset($url['query']) ? $url['query'] : "";
$host = strtolower($url['host']);
$path = $url['path'];
} else $query = $host = $path = '';
// Cette fonction affecte directement les variables selon la query-string !
parse_str($query);
$keywords = '';
$found = false;
12 years ago
if (!empty($url_site)){
if (strpos('-'.$kw_referer, $url_site)!==false){
if (preg_match(",(s|search|r|recherche)=([^&]+),i", $kw_referer, $regs))
$keywords = urldecode($regs[2]);
else
return array('host' => '');
}
else {
for ($cnt = 0; $cnt<sizeof($arr_engines) && !$found; $cnt++)
{
if ($found = preg_match(','.$arr_engines[$cnt][2].',', $host)
OR $found = preg_match(','.$arr_engines[$cnt][2].',', $path)){
$kw_referer_host = $arr_engines[$cnt][0];
if (strpos($arr_engines[$cnt][1], '=')!==false){
// Fonctionnement simple: la variable existe
$v = str_replace('=', '', $arr_engines[$cnt][1]);
$keywords = isset($$v) ? $$v : "";
// Si on a defini le nom de la variable en expression reguliere, chercher la bonne variable
if (!strlen($keywords)>0){
if (preg_match(",".$arr_engines[$cnt][1]."([^\&]*),", $query, $vals)){
$keywords = urldecode($vals[2]);
}
}
} else {
$keywords = "";
}
12 years ago
if ((($kw_referer_host=="Google")
|| ($kw_referer_host=="AOL" && strpos($query, 'enc=iso')===false)
|| ($kw_referer_host=="MSN")
)){
include_spip('inc/charsets');
if (!isset($ie) OR !$cset = $ie) $cset = 'utf-8';
$keywords = importer_charset($keywords, $cset);
}
$buffer["hostname"] = $kw_referer_host;
}
}
}
}
$buffer["host"] = $host;
if (!isset($buffer["hostname"]) OR !$buffer["hostname"])
$buffer["hostname"] = $host;
12 years ago
$buffer["path"] = substr($path, 1, strlen($path));
$buffer["query"] = $query;
12 years ago
if ($keywords!=''){
if (strlen($keywords)>150){
$keywords = spip_substr($keywords, 0, 148);
// supprimer l'eventuelle entite finale mal coupee
$keywords = preg_replace('/&#?[a-z0-9]*$/', '', $keywords);
}
$buffer["keywords"] = trim(entites_html(urldecode(stripslashes($keywords))));
}
return $buffer;
}
//
// Recherche des articles pointes par le referer
//
// http://doc.spip.org/@referes
12 years ago
function referes($referermd5, $serveur = ''){
$retarts = sql_allfetsel('J2.id_article, J2.titre', 'spip_referers_articles AS J1 LEFT JOIN spip_articles AS J2 ON J1.id_article = J2.id_article', "(referer_md5='$referermd5' AND J1.maj>=DATE_SUB(".sql_quote(date('Y-m-d H:i:s')).", INTERVAL 2 DAY))", '', "titre", '', '', $serveur);
12 years ago
foreach ($retarts as $k => $rowart){
$titre = typo($rowart['titre']);
$url = generer_url_entite($rowart['id_article'], 'article', '', '', TRUE);
$retarts[$k] = "<a href='$url'><i>$titre</i></a>";
}
12 years ago
if (count($retarts)>1)
return '<br />&rarr; '.join(',<br />&rarr; ', $retarts);
if (count($retarts)==1)
return '<br />&rarr; '.array_shift($retarts);
return '';
}
?>