<?php /*
Plugin Name: Statictis Plugin URI: http://makiwaki.no-ip.org/Statictis/ Description: widget que muestra estadisticas sobre las visitas a tu blog Version: 2.0 Author: Emilio díaz peña
/*
Copyright 2011 Emilio díaz peña (email : emiliotuccy@gmail.com)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
/* functiones de operaciones */ /* los proxys van añadiendo al final de esta cabecera las direcciones ip que van "ocultando". Para localizar la ip real del usuario se comienza a mirar por el principio hasta encontrar una dirección ip que no sea del rango privado. En caso de no encontrarse ninguna se toma como valor el REMOTE_ADDR */ function Statictis_getIp(){ if($_SERVER[HTTP_X_FORWARDED_FOR]!=){ $client_ip =(!empty($_SERVER[REMOTE_ADDR]))?$_SERVER[REMOTE_ADDR]:((!empty($_ENV[REMOTE_ADDR]))?$_ENV[REMOTE_ADDR]:"unknown" ); $entries = split('[, ]', $_SERVER[HTTP_X_FORWARDED_FOR]); reset($entries); while(list(, $entry)=each($entries)){ $entry = trim($entry); if ( preg_match("/^([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)/",$entry,$ip_list)){ $private_ip = array('/^0\\./','/^127\\.0\\.0\\.1/','/^192\\.168\\..*/','/^172\\.((1[6-9])|(2[0-9])|(3[0-1]))\\..*/','/^10\\..*/'); $found_ip = preg_replace($private_ip,$client_ip,$ip_list[1]); if ($client_ip != $found_ip){ $client_ip = $found_ip; break; } } } } else{ $client_ip=(!empty($_SERVER[REMOTE_ADDR]))?$_SERVER[REMOTE_ADDR]:((!empty($_ENV[REMOTE_ADDR]))?$_ENV[REMOTE_ADDR]:"unknown" ); } return $client_ip; unset($client_ip, $private_ip, $found_ip, $entries); } function Statictis_getOs(){
$userAgent=$_SERVER[HTTP_USER_AGENT];
if(preg_match('/Linux/i',$userAgent)) { $os='Linux'; } elseif(preg_match('/Windows/i',$userAgent)) { $os='Windows'; } elseif(preg_match('Macintosh',$userAgent)) { $os='Macintosh'; } else { $os='Otros'; } return $os; unset($os,$userAgent); } /* malabares para distingir ie de opera y mozilla cromium, sfari y peiphany entre ellos 1.-por descartes como mozilla solo devuelve mozilla si se encuentra mozilla en la cadena y ninguno de los otros que tienen mozilla en sus cadenas el navegador es mozilla 2.-descartadas las confusiones con mozilla si se encuentra safari pero no se encuentra epiphany ni chrome el navegador es safari ya q la cadena de safari no contiene crhomee ni epiphany 3.-si se encuentra crhomium pero no se encuentra epiphany el navegador es cromium ya q mozilla y safari qedaron descartados 4.-confusiones entre opera e ie si la cadena contiene msie pero no opera el navegador es ie ya que opera ademas de contener msie contiene opera en su cadena */ function Statictis_getBrowser(){
$userAgent=$_SERVER[HTTP_USER_AGENT];
if(preg_match('/Mozilla/i',$userAgent)&&!preg_match('/Chromium/i',$userAgent)&&!preg_match('/Epiphany/i',$userAgent)&&!preg_match('/Safari/i',$userAgent)){ $browser='Mozilla'; } elseif(preg_match('/Safari/i',$userAgent)&&!preg_match('/Chrome/i',$userAgent)&&!preg_match('/Epiphany/i',$userAgent)) { $browser= 'Safari'; } elseif(preg_match('/Chrome/i',$userAgent)&&!preg_match('/Epiphany/i',$userAgent)){ $browser='Chromium'; } elseif(preg_match('/MSIE/i',$userAgent)&&!preg_match('/Opera/i',$userAgent)){ $browser='Internet Explorer'; } elseif(preg_match('/Opera/i',$userAgent)){ $browser='Opera'; } elseif(preg_match('/Epiphany/i',$userAgent)){ $browser='Epiphany'; } elseif(preg_match('/Netscape/i',$userAgent)){ $browser='Netscape'; } elseif(preg_match('/Midori/i',$userAgent)){ $browser='Midori'; } else{ $browser='Otros'; } return $browser; unset($browser,$userAgent); } function getTable(){ global $wpdb; $table= $wpdb->prefix."Statictis"; return $table; unset($table); } /* Funciones de conexion con db */
function Statictis_install(){ $table= getTable(); $statment="CREATE TABLE IF NOT EXISTS `$table` (
`Browser` varchar(20) COLLATE utf8_unicode_ci NOT NULL, `Os` varchar(20) COLLATE utf8_unicode_ci NOT NULL, `Ip` varchar(255) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`Ip`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $query=mysql_query($statment); unset($query,$statment,$table); } function Statictis_uninstall(){ $table= getTable(); $statment="DROP TABLE $table"; $query=mysql_query($statment); mysql_free_result($query); unset($query,$statment,$table); } function Statictis_update($ip,$os,$browser){ $table=getTable(); $statment="SELECT * FROM $table WHERE Ip='$ip'"; $query=mysql_query($statment); if($row=mysql_fetch_array($query, MYSQL_ASSOC)){ if($row[Os]!=$os&&$row[Browser]!=$browser){ $statment="UPDATE $table SET Os='$os', Browser='$browser' WHERE Ip='$ip'"; } elseif($row[Os]!=$os){ $statment="UPDATE $table SET Os='$os' WHERE Ip='$ip'"; } elseif($row[Browser]!=$browser){ $statment="UPDATE $table SET Browser='$browser' WHERE Ip='$ip'"; } else{ mysql_free_result($query); unset($query,$statment,$table,$row,$os,$browser,$ip); return; } } else{ $statment="INSERT INTO $table VALUES('$browser','$os', '$ip')"; } $query=mysql_query($statment); mysql_free_result($query); unset($query,$statment,$table,$os,$browser,$ip); } function Statictis_getStats(){ $table=getTable(); $count_IE=$count_MOZILLA=$count_CHROMIUM=$count_OPERA=$count_SAFARI=$count_EPIPHANY=$count_NETSCAPE=$count_MIDORI=$count_OTHERS_BROWSERS=$count_WINDOWS=$count_LINUX=$count_MACINTOSH=$count_OTHERS_OS=0; $statment="SELECT * FROM $table"; $query=mysql_query($statment); while($row=mysql_fetch_array($query)){ switch ($row[Browser]){ case 'Internet Explorer':$count_IE++;break; case 'Chromium':$count_CHROMIUM++;break; case 'Mozilla':$count_MOZILLA++;break; case 'Opera':$count_OPERA++;break; case 'Safari':$count_SAFARI++;break; case 'Epiphany':$count_EPIPHANY++;break; case 'Netscape':$count_NETSCAPE++;break; case 'Midori':$count_MIDORI++;break; case 'Otros':$count_OTHERS_BROWSERS++;break; } switch ($row[Os]){ case 'Windows':$count_WINDOWS++;break; case 'Linux':$count_LINUX++;break; case 'Macintosh':$count_MACINTOSH++; case 'Otros':$count_OTHERS_OS++;break; } } mysql_free_result($query); $total=$count_WINDOWS+$count_LINUX+$count_MACINTOSH+$count_OTHERS_OS; $stats=array( 'IE' => $count_IE, 'CHROMIUM' => $count_CHROMIUM, 'MOZILLA' => $count_MOZILLA, 'OPERA' => $count_OPERA, 'SAFARI' => $count_SAFARI, 'EPIPHANY' => $count_EPIPHANY, 'NETSCAPE' => $count_NETSCAPE, 'MIDORI' => $count_MIDORI, 'OTHERS_BROWSERS' => $count_OTHERS_BROWSERS, 'WINDOWS' => $count_WINDOWS, 'LINUX' => $count_LINUX, 'MACINTOSH' => $count_MACINTOSH, 'OTHERS_OS' => $count_OTHERS_OS, 'TOTAL' => $total, 'PERCENT_IE' => round(($count_IE*100)/$total), 'PERCENT_CHROMIUM' => round(($count_CHROMIUM*100)/$total), 'PERCENT_MOZILLA' => round(($count_MOZILLA*100)/$total), 'PERCENT_OPERA' => round(($count_OPERA*100)/$total), 'PERCENT_SAFARI' => round(($count_SAFARI*100)/$total), 'PERCENT_EPIPHANY' => round(($count_EPIPHANY*100)/$total), 'PERCENT_NETSCAPE' => round(($count_NETSCAPE*100)/$total), 'PERCENT_MIDORI' => round(($count_MIDORI*100)/$total), 'PERCENT_OTHERS_BROWSERS' => round(($count_OTHERS_BROWSERS*100)/$total), 'PERCENT_WINDOWS' => round(($count_WINDOWS*100)/$total), 'PERCENT_LINUX' => round(($count_LINUX*100)/$total), 'PERCENT_MACINTOSH' => round(($count_MACINTOSH*100)/$total), 'PERCENT_OTHERS_OS' => round(($count_OTHERS_OS*100)/$total) ); return $stats; unset($table,$statment,$query,$count_IE,$count_MOZILLA,$count_CHROMIUM,$count_OPERA,$count_SAFARI,$count_EPIPHANY,$count_NETSCAPE,$count_MIDORI,$count_OTHERS_BROWSERS,$count_WINDOWS,$count_LINUX,$count_OTHERS_OS,$total,$stats); } function Statictis_printTitle($options_site,$stats){ if($options_site[title]!=null){
echo "} else{
echo " }
unset($options_site,$stats);
}
function Statictis_printBrowsers($options_site,$path,$stats){
if($options_site[allowBrowserStats]==true){
if($options_site[allowIcons]==true&&$options_site[allowPercent]==true){
echo "BUSCADORES:
";
echo "<img src='$path/CHROMIUM.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>CHROMIUM: $stats[PERCENT_CHROMIUM]%
";
echo "<img src='$path/MOZILLA.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>MOZILLA: $stats[PERCENT_MOZILLA]%
";
echo "<img src='$path/IE.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>IE: $stats[PERCENT_IE]%
";
echo "<img src='$path/OPERA.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>OPERA: $stats[PERCENT_OPERA]%
";
echo "<img src='$path/SAFARI.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>SAFARI: $stats[PERCENT_SAFARI]%
";
echo "<img src='$path/EPIPHANY.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>EPIPHANY: $stats[PERCENT_EPIPHANY]%
";
echo "<img src='$path/NETSCAPE.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>NETSCAPE: $stats[PERCENT_NETSCAPE]%
";
echo "<img src='$path/MIDORI.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>MIDORI: $stats[PERCENT_MIDORI]%
";
echo "<img src='$path/OTROS.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>OTROS: $stats[PERCENT_OTHERS_BROWSERS]%
";
}
elseif($options_site[allowIcons]==false&&$options_site[allowPercent]==true){
echo "BUSCADORES:
";
echo "CHROMIUM: $stats[PERCENT_CHROMIUM]%
";
echo "MOZILLA: $stats[PERCENT_MOZILLA]%
";
echo "IE: $stats[PERCENT_IE]%
";
echo "OPERA: $stats[PERCENT_OPERA]%
";
echo "SAFARI: $stats[PERCENT_SAFARI]%
";
echo "EPIPHANY: $stats[PERCENT_EPIPHANY]%
";
echo "NETSCAPE: $stats[PERCENT_NETSCAPE]%
";
echo "MIDORI: $stats[PERCENT_MIDORI]%
";
echo "OTROS: $stats[PERCENT_OTHERS_BROWSERS]%
";
}
elseif($options_site[allowIcons]==true&&$options_site[allowPercent]==false){
echo "BUSCADORES:
";
echo "<img src='$path/CHROMIUM.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>CHROMIUM: $stats[CHROMIUM]
";
echo "<img src='$path/MOZILLA.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>MOZILLA: $stats[MOZILLA]
";
echo "<img src='$path/IE.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>IE: $stats[IE]
";
echo "<img src='$path/OPERA.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>OPERA: $stats[OPERA]
";
echo "<img src='$path/SAFARI.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>SAFARI: $stats[SAFARI]
";
echo "<img src='$path/EPIPHANY.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>EPIPHANY: $stats[EPIPHANY]
";
echo "<img src='$path/NETSCAPE.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>NETSCAPE: $stats[NETSCAPE]
";
echo "<img src='$path/MIDORI.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>MIDORI: $stats[MIDORI]
";
echo "<img src='$path/OTROS.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>OTROS: $stats[OTHERS_BROWSERS]
";
}
elseif($options_site[allowIcons]==false&&$options_site[allowPercent]==false){
echo "BUSCADORES:
";
echo "CHROMIUM: $stats[CHROMIUM]
";
echo "MOZILLA: $stats[MOZILLA]
";
echo "IE: $stats[IE]
";
echo "OPERA: $stats[OPERA]
";
echo "SAFARI: $stats[SAFARI]
";
echo "EPIPHANY: $stats[EPIPHANY]
";
echo "NETSCAPE: $stats[NETSCAPE]
";
echo "MIDORI: $stats[MIDORI]
";
echo "OTROS: $stats[OTHERS_BROWSERS]
";
}
}
unset($options_site,$path,$stats);
}
function Statictis_printOs($options_site,$path,$stats){
if($options_site[allowOsStats]==true){
if($options_site[allowIcons]==true&&$options_site[allowPercent]==true){
echo "SISTEMAS OPERATIVOS:
";
echo "<img src='$path/LINUX.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>LINUX: $stats[PERCENT_LINUX]%
";
echo "<img src='$path/WINDOWS.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>WINDOWS: $stats[PERCENT_WINDOWS]%
";
echo "<img src='$path/MACINTOSH.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>MACINTOSH: $stats[PERCENT_MACINTOSH]%
";
echo "<img src='$path/OTROS.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>OTROS: $stats[PERCENT_OTHERS_OS]%
";
}
elseif($options_site[allowIcons]==false&&$options_site[allowPercent]==true){
echo "SISTEMAS OPERATIVOS:
";
echo "LINUX: $stats[PERCENT_LINUX]%
";
echo "WINDOWS: $stats[PERCENT_WINDOWS]%
";
echo "MACINTOSH: $stats[PERCENT_MACINTOSH]%
";
echo "OTROS: $stats[PERCENT_OTHERS_OS]%
";
}
elseif($options_site[allowIcons]==true&&$options_site[allowPercent]==false){
echo "SISTEMAS OPERATIVOS:
";
echo "<img src='$path/LINUX.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>LINUX: $stats[LINUX]
";
echo "<img src='$path/WINDOWS.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>WINDOWS: $stats[WINDOWS]
";
echo "<img src='$path/MACINTOSH.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>MACINTOSH: $stats[MACINTOSH]
";
echo "<img src='$path/OTROS.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>OTROS: $stats[OTHERS_OS]
";
}
elseif($options_site[allowIcons]==false&&$options_site[allowPercent]==false){
echo "SISTEMAS OPERATIVOS:
";
echo "LINUX: $stats[LINUX]
";
echo "WINDOWS: $stats[WINDOWS]
";
echo "MACINTOSH: $stats[MACINTOSH]
";
echo "OTROS: $stats[OTHERS_OS]
";
}
}
unset($options_site,$path,$stats);
}
function Statictis_printVisits($options_site,$stats){
if($options_site[allowTotalVisits]==true){
echo "VISITAS: $stats[TOTAL]
";
}
unset($options_site,$stats);
}
function Statictis_PrintYourBrowser($options_site,$path,$browser){
if($options_site[allowIcons]==true){
switch ($browser){
case 'Internet Explorer':$image='IE';break;
case 'Chromium':$image='CHROMIUM';break;
case 'Mozilla':$image='MOZILLA';break;
case 'Opera':$image='OPERA';break;
case 'Safari':$image='SAFARI';break;
case 'Epiphany':$image='EPIPHANY';break;
case 'Netscape':$image='NETSCAPE';break;
case 'Midori':$image='MIDORI';break;
case 'Otros':$image='OTROS';break;
}
echo "Tu navegador es: <img src='$path/$image.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>$browser
";
}
else{
echo "Tu navegador es: $browser
";
}
unset($options_site,$path,$browser,$image);
}
function Statictis_PrintYourOs($options_site,$path,$os){
if($options_site[allowIcons]==true){
switch ($os){
case 'Windows':$image='WINDOWS';break;
case 'Linux':$image='LINUX';break;
case 'Macintosh':$image='MACINTOSH';break;
case 'Otros':$image='OTROS';break;
}
echo "Tu sistema operativo es: <img src='$path/$image.png' style='width:$options_site[imageSize]px; height:$options_site[imageSize]px; vertical-align:middle'>$os
";
}
else{
echo "Tu sistema operativo es: $os
";
}
unset($options_site,$path,$os,$image);
}
/*
funciones de wordpress
*/
function Statictis_public($args=false){
$options_site=get_option('Statictis');
$ip=Statictis_getIp();
$browser=Statictis_getBrowser();
$os=Statictis_getOs();
Statictis_update($ip,$os,$browser);
$stats=Statictis_getStats();
$url=get_option('siteurl');
$path="$url/wp-content/plugins/Statictis/images";
Statictis_printTitle($options_site,$stats);
Statictis_printBrowsers($options_site,$path,$stats);
Statictis_printOs($options_site,$path,$stats);
Statictis_printVisits($options_site,$stats);
Statictis_PrintYourBrowser($options_site,$path,$browser);
Statictis_PrintYourOs($options_site,$path,$os);
unset($options_site,$ip,$browser,$stats,$path);
}
/*
menú de opciones del widget */
function Statictis_admin() { if ($_SERVER[REQUEST_METHOD]=='POST'){ $options_site=array( 'title'=>$_POST[title], 'allowBrowserStats'=>$_POST[allowBrowserStats], 'allowOsStats'=>$_POST[allowOsStats], 'allowTotalVisits'=>$_POST[allowTotalVisits], 'allowIcons'=>$_POST[allowIcons], 'imageSize'=>$_POST[imageSize], 'allowPercent'=>$_POST[allowPercent], 'allowYourBrowser'=>$_POST[allowYourBrowser], 'allowYourOs'=>$_POST[allowYourOs] ); update_option('Statictis',$options_site); } else{ $options_site=get_option('Statictis');
echo "Titulo: <input id='title' type='text' name='title' style='width:99%;'/>
"; echo "Ver estadísticas de navegadores: <input id='allowBrowserStats' type='checkbox' name='allowBrowserStats' checked='true' style='width:99%;'/>
"; echo "Ver estadísticas de Sistemas Operativos: <input id='allowOsStats' type='checkbox' name='allowOsStats' checked='true' style='width:99%;'/>
"; echo "Ver total de visitas: <input id='allowTotalVisits' type='checkbox' name='allowTotalVisits' checked='true' style='width:99%;'/>
"; echo "Mostrar Iconos: <input id='allowIcons' type='checkbox' name='allowIcons' checked='true' style='width:99%;'/>
"; echo "Tamaño de iconos: <input id='imageSize' type='text' name='imageSize' value='32' style='width:30%;'/>px
"; echo "Mostrar en tantos %: <input id='allowPercent' type='checkbox' name='allowPercent' checked='true' style='width:99%;'/>
"; echo "Mostrar al visitante cual es su navegador: <input id='allowYourBrowser' type='checkbox' name='allowYourBrowser' checked='true' style='width:99%;'/>
"; echo "Mostrar al visitante cual es su sistema operativo: <input id='allowYourOs' type='checkbox' name='allowYourOs' checked='true' style='width:99%;'/>
";} } /*
colocar plugin en modo debug */
define('WP_DEBUG',true); /*
función de registro del plugin como widget */
function Statictis_init(){ register_sidebar_widget('Statictis','Statictis_public'); register_widget_control('Statictis','Statictis_admin'); } /*
función de registro del plugin */
add_action('plugins_loaded','Statictis_init'); /*
función de instalación y desinstalación */
add_action('activate_Statictis/Statictis.php','Statictis_install'); add_action('deactivate_Statictis/Statictis.php','Statictis_uninstall'); ?>