4 Plugin URI: http://forum.irisco.it/forum.php?id=1
5 Description: Real time stats for your blog
8 Author URI: http://www.irisco.it
11 $_STATPRESS['version']='1.x';
12 $_STATPRESS['feedtype']='';
14 if ($_GET['statpress_action'] == 'exportnow') {
15 iriStatPressExportNow();
18 include ABSPATH.'wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/includes/charts.php';
20 function iri_add_pages() {
21 # Crea/aggiorna tabella se non esiste
23 $table_name = $wpdb->prefix . "statpress";
24 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
25 iri_StatPress_CreateTable();
28 $mincap=get_option('statpress_mincap');
32 // ORIG add_submenu_page('index.php', 'StatPress', 'StatPress', 8, 'statpress', 'iriStatPress');
34 add_menu_page('StatPress', 'StatPress', $mincap, __FILE__, 'iriStatPress');
35 add_submenu_page(__FILE__, __('Overview','statpress'), __('Overview','statpress'), $mincap, __FILE__, 'iriStatPress');
36 add_submenu_page(__FILE__, __('Details','statpress'), __('Details','statpress'), $mincap, __FILE__ . '&statpress_action=details', 'iriStatPress');
37 add_submenu_page(__FILE__, __('Spy','statpress'), __('Spy','statpress'), $mincap, __FILE__ . '&statpress_action=spy', 'iriStatPress');
38 add_submenu_page(__FILE__, __('Search','statpress'), __('Search','statpress'), $mincap, __FILE__ . '&statpress_action=search', 'iriStatPress');
39 add_submenu_page(__FILE__, __('Export','statpress'), __('Export','statpress'), $mincap, __FILE__ . '&statpress_action=export', 'iriStatPress');
40 add_submenu_page(__FILE__, __('Options','statpress'), __('Options','statpress'), $mincap, __FILE__ . '&statpress_action=options', 'iriStatPress');
41 add_submenu_page(__FILE__, __('StatPressUpdate','statpress'), __('StatPressUpdate','statpress'), $mincap, __FILE__ . '&statpress_action=up', 'iriStatPress');
42 // add_submenu_page(__FILE__, __('Support','statpress'), __('Support','statpress'), $mincap, 'http://www.irisco.it/forums/forum.php?id=1');
43 add_submenu_page(__FILE__, __('Donate','statpress'), __('Donate','statpress'), $mincap, 'https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=daniele%2elippi%40gmail%2ecom&item_name=wp%2dstatpress&no_shipping=0&no_note=1&tax=0¤cy_code=EUR&lc=IT&bn=PP%2dDonationsBF&charset=UTF%2d8');
48 function iriStatPress() {
51 if ($_GET['statpress_action'] == 'export') {
53 } elseif ($_GET['statpress_action'] == 'up') {
55 } elseif ($_GET['statpress_action'] == 'spy') {
57 } elseif ($_GET['statpress_action'] == 'search') {
59 } elseif ($_GET['statpress_action'] == 'details') {
60 iriStatPressDetails();
61 } elseif ($_GET['statpress_action'] == 'options') {
62 iriStatPressOptions();
68 function iriStatPressOptions() {
69 if($_POST['saveit'] == 'yes') {
70 update_option('statpress_collectloggeduser', $_POST['statpress_collectloggeduser']);
71 update_option('statpress_autodelete', $_POST['statpress_autodelete']);
72 update_option('statpress_daysinoverviewgraph', $_POST['statpress_daysinoverviewgraph']);
73 update_option('statpress_mincap', $_POST['statpress_mincap']);
74 update_option('statpress_donotcollectspider', $_POST['statpress_donotcollectspider']);
75 update_option('statpress_cryptip', $_POST['statpress_cryptip']);
78 iri_StatPress_CreateTable();
79 print "<br /><div class='updated'><p>".__('Saved','statpress')."!</p></div>";
82 <div class='wrap'><h2><?php _e('Options','statpress'); ?></h2>
83 <form method=post><table width=100%>
85 print "<tr><td><input type=checkbox name='statpress_collectloggeduser' value='checked' ".get_option('statpress_collectloggeduser')."> ".__('Collect data about logged users, too.','statpress')."</td></tr>";
86 print "<tr><td><input type=checkbox name='statpress_donotcollectspider' value='checked' ".get_option('statpress_donotcollectspider')."> ".__('Do not collect spiders visits','statpress')."</td></tr>";
87 print "<tr><td><input type=checkbox name='statpress_cryptip' value='checked' ".get_option('statpress_cryptip')."> ".__('Crypt IP addresses','statpress')."</td></tr>";
90 <tr><td><?php _e('Automatically delete visits older than','statpress'); ?>
91 <select name="statpress_autodelete">
92 <option value="" <?php if(get_option('statpress_autodelete') =='' ) print "selected"; ?>><?php _e('Never delete!','statpress'); ?></option>
93 <option value="1 month" <?php if(get_option('statpress_autodelete') == "1 month") print "selected"; ?>>1 <?php _e('month','statpress'); ?></option>
94 <option value="3 months" <?php if(get_option('statpress_autodelete') == "3 months") print "selected"; ?>>3 <?php _e('months','statpress'); ?></option>
95 <option value="6 months" <?php if(get_option('statpress_autodelete') == "6 months") print "selected"; ?>>6 <?php _e('months','statpress'); ?></option>
96 <option value="1 year" <?php if(get_option('statpress_autodelete') == "1 year") print "selected"; ?>>1 <?php _e('year','statpress'); ?></option>
99 <tr><td><?php _e('Days in Overview graph','statpress'); ?>
100 <select name="statpress_daysinoverviewgraph">
101 <option value="7" <?php if(get_option('statpress_daysinoverviewgraph') == 7) print "selected"; ?>>7</option>
102 <option value="10" <?php if(get_option('statpress_daysinoverviewgraph') == 10) print "selected"; ?>>10</option>
103 <option value="20" <?php if(get_option('statpress_daysinoverviewgraph') == 20) print "selected"; ?>>20</option>
104 <option value="30" <?php if(get_option('statpress_daysinoverviewgraph') == 30) print "selected"; ?>>30</option>
105 <option value="50" <?php if(get_option('statpress_daysinoverviewgraph') == 50) print "selected"; ?>>50</option>
108 <tr><td><?php _e('Minimum capability to view stats','statpress'); ?>
109 <select name="statpress_mincap">
110 <?php iri_dropdown_caps(get_option('statpress_mincap')); ?>
112 <a href="http://codex.wordpress.org/Roles_and_Capabilities" target="_blank"><?php _e("more info",'statpress'); ?></a>
115 <tr><td><br><input type=submit value="<?php _e('Save options','statpress'); ?>"></td></tr>
118 <input type=hidden name=saveit value=yes>
119 <input type=hidden name=page value=statpress><input type=hidden name=statpress_action value=options>
128 function iri_dropdown_caps( $default = false ) {
130 $role = get_role('administrator');
131 foreach($role->capabilities as $cap => $grant) {
133 if($default == $cap) { print "selected "; }
134 print ">$cap</option>";
139 function iriStatPressExport() {
141 <div class='wrap'><h2><?php _e('Export stats to text file','statpress'); ?> (csv)</h2>
142 <form method=get><table>
143 <tr><td><?php _e('From','statpress'); ?></td><td><input type=text name=from> (YYYYMMDD)</td></tr>
144 <tr><td><?php _e('To','statpress'); ?></td><td><input type=text name=to> (YYYYMMDD)</td></tr>
145 <tr><td><?php _e('Fields delimiter','statpress'); ?></td><td><select name=del><option>,</option><option>;</option><option>|</option></select></tr>
146 <tr><td></td><td><input type=submit value=<?php _e('Export','statpress'); ?>></td></tr>
147 <input type=hidden name=page value=statpress><input type=hidden name=statpress_action value=exportnow>
154 function iriStatPressExportNow() {
156 $table_name = $wpdb->prefix . "statpress";
157 $filename=get_bloginfo('title' )."-statpress_".$_GET['from']."-".$_GET['to'].".csv";
158 header('Content-Description: File Transfer');
159 header("Content-Disposition: attachment; filename=$filename");
160 header('Content-Type: text/plain charset=' . get_option('blog_charset'), true);
161 $qry = $wpdb->get_results("SELECT * FROM $table_name WHERE date>='".(date("Ymd",strtotime(substr($_GET['from'],0,8))))."' AND date<='".(date("Ymd",strtotime(substr($_GET['to'],0,8))))."';");
162 $del=substr($_GET['del'],0,1);
163 print "date".$del."time".$del."ip".$del."urlrequested".$del."agent".$del."referrer".$del."search".$del."nation".$del."os".$del."browser".$del."searchengine".$del."spider".$del."feed\n";
164 foreach ($qry as $rk) {
165 print '"'.$rk->date.'"'.$del.'"'.$rk->time.'"'.$del.'"'.$rk->ip.'"'.$del.'"'.$rk->urlrequested.'"'.$del.'"'.$rk->agent.'"'.$del.'"'.$rk->referrer.'"'.$del.'"'.$rk->search.'"'.$del.'"'.$rk->nation.'"'.$del.'"'.$rk->os.'"'.$del.'"'.$rk->browser.'"'.$del.'"'.$rk->searchengine.'"'.$del.'"'.$rk->spider.'"'.$del.'"'.$rk->feed.'"'."\n";
171 function iriStatPressMain() {
173 $table_name = $wpdb->prefix . "statpress";
176 $unique_color="#114477";
177 $web_color="#3377B6";
178 $rss_color="#f38f36";
179 $spider_color="#83b4d8";
180 $lastmonth = iri_StatPress_lastmonth();
181 $thismonth = gmdate('Ym', current_time('timestamp'));
182 $yesterday = gmdate('Ymd', current_time('timestamp')-86400);
183 $today = gmdate('Ymd', current_time('timestamp'));
184 $tlm[0]=substr($lastmonth,0,4); $tlm[1]=substr($lastmonth,4,2);
186 print "<div class='wrap'><h2>". __('Overview','statpress'). "</h2>";
187 print "<table class='widefat'><thead><tr>
188 <th scope='col'></th>
189 <th scope='col'>". __('Total','statpress'). "</th>
190 <th scope='col'>". __('Last month','statpress'). "<br /><font size=1>" . gmdate('M, Y',gmmktime(0,0,0,$tlm[1],1,$tlm[0])) ."</font></th>
191 <th scope='col'>". __('This month','statpress'). "<br /><font size=1>" . gmdate('M, Y', current_time('timestamp')) ."</font></th>
192 <th scope='col'>Target ". __('This month','statpress'). "<br /><font size=1>" . gmdate('M, Y', current_time('timestamp')) ."</font></th>
193 <th scope='col'>". __('Yesterday','statpress'). "<br /><font size=1>" . gmdate('d M, Y', current_time('timestamp')-86400) ."</font></th>
194 <th scope='col'>". __('Today','statpress'). "<br /><font size=1>" . gmdate('d M, Y', current_time('timestamp')) ."</font></th>
196 <tbody id='the-list'>";
198 ################################################################################################
200 print "<tr><td><div style='background:$unique_color;width:10px;height:10px;float:left;margin-top:4px;margin-right:5px;'></div>". __('Visitors','statpress'). "</td>";
203 $qry_total = $wpdb->get_row("
204 SELECT count(DISTINCT ip) AS visitors
209 print "<td>" . $qry_total->visitors . "</td>\n";
212 $qry_lmonth = $wpdb->get_row("
213 SELECT count(DISTINCT ip) AS visitors
217 AND date LIKE '" . $lastmonth . "%'
219 print "<td>" . $qry_lmonth->visitors . "</td>\n";
222 $qry_tmonth = $wpdb->get_row("
223 SELECT count(DISTINCT ip) AS visitors
227 AND date LIKE '" . $thismonth . "%'
229 if($qry_lmonth->visitors <> 0) {
230 $pc = round( 100 * ($qry_tmonth->visitors / $qry_lmonth->visitors ) - 100,1);
231 if($pc >= 0) $pc = "+" . $pc;
232 $qry_tmonth->change = "<code> (" . $pc . "%)</code>";
234 print "<td>" . $qry_tmonth->visitors . $qry_tmonth->change . "</td>\n";
237 $qry_tmonth->target = round($qry_tmonth->visitors / date("d", current_time('timestamp')) * 30);
238 if($qry_lmonth->visitors <> 0) {
239 $pt = round( 100 * ($qry_tmonth->target / $qry_lmonth->visitors ) - 100,1);
240 if($pt >= 0) $pt = "+" . $pt;
241 $qry_tmonth->added = "<code> (" . $pt . "%)</code>";
243 print "<td>" . $qry_tmonth->target . $qry_tmonth->added . "</td>\n";
246 $qry_y = $wpdb->get_row("
247 SELECT count(DISTINCT ip) AS visitors
251 AND date = '$yesterday'
253 print "<td>" . $qry_y->visitors . "</td>\n";
256 $qry_t = $wpdb->get_row("
257 SELECT count(DISTINCT ip) AS visitors
263 print "<td>" . $qry_t->visitors . "</td>\n";
266 ################################################################################################
268 print "<tr><td><div style='background:$web_color;width:10px;height:10px;float:left;margin-top:4px;margin-right:5px;'></div>". __('Pageviews','statpress'). "</td>";
271 $qry_total = $wpdb->get_row("
272 SELECT count(date) as pageview
277 print "<td>" . $qry_total->pageview . "</td>\n";
281 $qry_lmonth = $wpdb->get_row("
282 SELECT count(date) as pageview
286 AND date LIKE '" . $lastmonth . "%'
288 print "<td>".$qry_lmonth->pageview."</td>\n";
291 $qry_tmonth = $wpdb->get_row("
292 SELECT count(date) as pageview
296 AND date LIKE '" . $thismonth . "%'
298 if($qry_lmonth->pageview <> 0) {
299 $pc = round( 100 * ($qry_tmonth->pageview / $qry_lmonth->pageview ) - 100,1);
300 if($pc >= 0) $pc = "+" . $pc;
301 $qry_tmonth->change = "<code> (" . $pc . "%)</code>";
303 print "<td>" . $qry_tmonth->pageview . $qry_tmonth->change . "</td>\n";
306 $qry_tmonth->target = round($qry_tmonth->pageview / date("d", current_time('timestamp')) * 30);
307 if($qry_lmonth->pageview <> 0) {
308 $pt = round( 100 * ($qry_tmonth->target / $qry_lmonth->pageview ) - 100,1);
309 if($pt >= 0) $pt = "+" . $pt;
310 $qry_tmonth->added = "<code> (" . $pt . "%)</code>";
312 print "<td>" . $qry_tmonth->target . $qry_tmonth->added . "</td>\n";
315 $qry_y = $wpdb->get_row("
316 SELECT count(date) as pageview
320 AND date = '$yesterday'
322 print "<td>" . $qry_y->pageview . "</td>\n";
325 $qry_t = $wpdb->get_row("
326 SELECT count(date) as pageview
332 print "<td>" . $qry_t->pageview . "</td>\n";
334 ################################################################################################
336 print "<tr><td><div style='background:$spider_color;width:10px;height:10px;float:left;margin-top:4px;margin-right:5px;'></div>Spiders</td>";
338 $qry_total = $wpdb->get_row("
339 SELECT count(date) as spiders
344 print "<td>" . $qry_total->spiders . "</td>\n";
347 $qry_lmonth = $wpdb->get_row("
348 SELECT count(date) as spiders
352 AND date LIKE '" . $lastmonth . "%'
354 print "<td>" . $qry_lmonth->spiders. "</td>\n";
357 $prec=$qry_lmonth->spiders;
358 $qry_tmonth = $wpdb->get_row("
359 SELECT count(date) as spiders
363 AND date LIKE '" . $thismonth . "%'
365 if($qry_lmonth->spiders <> 0) {
366 $pc = round( 100 * ($qry_tmonth->spiders / $qry_lmonth->spiders ) - 100,1);
367 if($pc >= 0) $pc = "+" . $pc;
368 $qry_tmonth->change = "<code> (" . $pc . "%)</code>";
370 print "<td>" . $qry_tmonth->spiders . $qry_tmonth->change . "</td>\n";
373 $qry_tmonth->target = round($qry_tmonth->spiders / date("d", current_time('timestamp')) * 30);
374 if($qry_lmonth->spiders <> 0) {
375 $pt = round( 100 * ($qry_tmonth->target / $qry_lmonth->spiders ) - 100,1);
376 if($pt >= 0) $pt = "+" . $pt;
377 $qry_tmonth->added = "<code> (" . $pt . "%)</code>";
379 print "<td>" . $qry_tmonth->target . $qry_tmonth->added . "</td>\n";
382 $qry_y = $wpdb->get_row("
383 SELECT count(date) as spiders
387 AND date = '$yesterday'
389 print "<td>" . $qry_y->spiders . "</td>\n";
392 $qry_t = $wpdb->get_row("
393 SELECT count(date) as spiders
399 print "<td>" . $qry_t->spiders . "</td>\n";
401 ################################################################################################
403 print "<tr><td><div style='background:$rss_color;width:10px;height:10px;float:left;margin-top:4px;margin-right:5px;'></div>Feeds</td>";
405 $qry_total = $wpdb->get_row("
406 SELECT count(date) as feeds
411 print "<td>".$qry_total->feeds."</td>\n";
414 $qry_lmonth = $wpdb->get_row("
415 SELECT count(date) as feeds
419 AND date LIKE '" . $lastmonth . "%'
421 print "<td>".$qry_lmonth->feeds."</td>\n";
424 $qry_tmonth = $wpdb->get_row("
425 SELECT count(date) as feeds
429 AND date LIKE '" . $thismonth . "%'
431 if($qry_lmonth->feeds <> 0) {
432 $pc = round( 100 * ($qry_tmonth->feeds / $qry_lmonth->feeds ) - 100,1);
433 if($pc >= 0) $pc = "+" . $pc;
434 $qry_tmonth->change = "<code> (" . $pc . "%)</code>";
436 print "<td>" . $qry_tmonth->feeds . $qry_tmonth->change . "</td>\n";
439 $qry_tmonth->target = round($qry_tmonth->feeds / date("d", current_time('timestamp')) * 30);
440 if($qry_lmonth->feeds <> 0) {
441 $pt = round( 100 * ($qry_tmonth->target / $qry_lmonth->feeds ) - 100,1);
442 if($pt >= 0) $pt = "+" . $pt;
443 $qry_tmonth->added = "<code> (" . $pt . "%)</code>";
445 print "<td>" . $qry_tmonth->target . $qry_tmonth->added . "</td>\n";
447 $qry_y = $wpdb->get_row("
448 SELECT count(date) as feeds
452 AND date = '".$yesterday."'
454 print "<td>".$qry_y->feeds."</td>\n";
456 $qry_t = $wpdb->get_row("
457 SELECT count(date) as feeds
463 print "<td>".$qry_t->feeds."</td>\n";
465 print "</tr></table><br />\n\n";
467 ################################################################################################
468 ################################################################################################
471 # last "N" days graph NEW
472 $gdays=get_option('statpress_daysinoverviewgraph'); if($gdays == 0) { $gdays=20; }
473 // $start_of_week = get_settings('start_of_week');
474 $start_of_week = get_option('start_of_week');
475 print '<table width="100%" border="0"><tr>';
476 $qry = $wpdb->get_row("
477 SELECT count(date) as pageview, date
479 GROUP BY date HAVING date >= '".gmdate('Ymd', current_time('timestamp')-86400*$gdays)."'
480 ORDER BY pageview DESC
483 $maxxday=$qry->pageview;
484 if($maxxday == 0) { $maxxday = 1; }
487 for($gg=$gdays-1;$gg>=0;$gg--)
490 $qry_visitors = $wpdb->get_row("
491 SELECT count(DISTINCT ip) AS total
495 AND date = '".gmdate('Ymd', current_time('timestamp')-86400*$gg)."'
497 $px_visitors = round($qry_visitors->total*100/$maxxday);
499 #TOTAL PAGEVIEWS (we do not delete the uniques, this is falsing the info.. uniques are not different visitors!)
500 $qry_pageviews = $wpdb->get_row("
501 SELECT count(date) as total
505 AND date = '".gmdate('Ymd', current_time('timestamp')-86400*$gg)."'
507 $px_pageviews = round($qry_pageviews->total*100/$maxxday);
510 $qry_spiders = $wpdb->get_row("
511 SELECT count(ip) AS total
515 AND date = '".gmdate('Ymd', current_time('timestamp')-86400*$gg)."'
517 $px_spiders = round($qry_spiders->total*100/$maxxday);
520 $qry_feeds = $wpdb->get_row("
521 SELECT count(ip) AS total
525 AND date = '".gmdate('Ymd', current_time('timestamp')-86400*$gg)."'
527 $px_feeds = round($qry_feeds->total*100/$maxxday);
529 $px_white = 100 - $px_feeds - $px_spiders - $px_pageviews - $px_visitors;
531 print '<td width="'.$gd.'" valign="bottom"';
532 if($start_of_week == gmdate('w',current_time('timestamp')-86400*$gg)) { print ' style="border-left:2px dotted gray;"'; } # week-cut
533 print "><div style='float:left;height: 100%;width:100%;font-family:Helvetica;font-size:7pt;text-align:center;border-right:1px solid white;color:black;'>
534 <div style='background:#ffffff;width:100%;height:".$px_white."px;'></div>
535 <div style='background:$unique_color;width:100%;height:".$px_visitors."px;' title='".$qry_visitors->total." visitors'></div>
536 <div style='background:$web_color;width:100%;height:".$px_pageviews."px;' title='".$qry_pageviews->total." pageviews'></div>
537 <div style='background:$spider_color;width:100%;height:".$px_spiders."px;' title='".$qry_spiders->total." spiders'></div>
538 <div style='background:$rss_color;width:100%;height:".$px_feeds."px;' title='".$qry_feeds->total." feeds'></div>
539 <div style='background:gray;width:100%;height:1px;'></div>
540 <br />".gmdate('d', current_time('timestamp')-86400*$gg) . ' ' . gmdate('M', current_time('timestamp')-86400*$gg) . "</div></td>\n";
542 print '</tr></table>';
546 ####################################################################################################
551 $querylimit="LIMIT 10";
554 print "<div class='wrap'><h2>". __('Last hits','statpress'). "</h2><table class='widefat'><thead><tr><th scope='col'>". __('Date','statpress'). "</th><th scope='col'>". __('Time','statpress'). "</th><th scope='col'>IP</th><th scope='col'>". __('Language','statpress'). "</th><th scope='col'>". __('Page','statpress'). "</th><th scope='col'>OS</th><th scope='col'>Browser</th><th scope='col'>Feed</th></tr></thead>";
555 print "<tbody id='the-list'>";
557 $fivesdrafts = $wpdb->get_results("SELECT * FROM $table_name WHERE (os<>'' OR feed<>'') order by id DESC $querylimit");
558 foreach ($fivesdrafts as $fivesdraft) {
560 print "<td>". irihdate($fivesdraft->date) ."</td>";
561 print "<td>". $fivesdraft->time ."</td>";
562 print "<td>". $fivesdraft->ip ."</td>";
563 print "<td>". $fivesdraft->nation ."</td>";
564 print "<td>". iri_StatPress_Abbrevia(iri_StatPress_Decode($fivesdraft->urlrequested),30) ."</td>";
565 print "<td>". $fivesdraft->os . "</td>";
566 print "<td>". $fivesdraft->browser . "</td>";
567 print "<td>". $fivesdraft->feed . "</td>";
570 print "</table></div>";
574 print "<div class='wrap'><h2>" . __('Last search terms','statpress') . "</h2><table class='widefat'><thead><tr><th scope='col'>".__('Date','statpress')."</th><th scope='col'>".__('Time','statpress')."</th><th scope='col'>".__('Terms','statpress')."</th><th scope='col'>". __('Engine','statpress'). "</th><th scope='col'>". __('Result','statpress'). "</th></tr></thead>";
575 print "<tbody id='the-list'>";
576 $qry = $wpdb->get_results("SELECT date,time,referrer,urlrequested,search,searchengine FROM $table_name WHERE search<>'' ORDER BY id DESC $querylimit");
577 foreach ($qry as $rk) {
578 print "<tr><td>".irihdate($rk->date)."</td><td>".$rk->time."</td><td><a href='".$rk->referrer."'>".$rk->search."</a></td><td>".$rk->searchengine."</td><td><a href='".get_bloginfo('url')."/?".$rk->urlrequested."'>". __('page viewed','statpress'). "</a></td></tr>\n";
580 print "</table></div>";
583 print "<div class='wrap'><h2>".__('Last referrers','statpress')."</h2><table class='widefat'><thead><tr><th scope='col'>".__('Date','statpress')."</th><th scope='col'>".__('Time','statpress')."</th><th scope='col'>".__('URL','statpress')."</th><th scope='col'>".__('Result','statpress')."</th></tr></thead>";
584 print "<tbody id='the-list'>";
585 $qry = $wpdb->get_results("SELECT date,time,referrer,urlrequested FROM $table_name WHERE ((referrer NOT LIKE '".get_option('home')."%') AND (referrer <>'') AND (searchengine='')) ORDER BY id DESC $querylimit");
586 foreach ($qry as $rk) {
587 print "<tr><td>".irihdate($rk->date)."</td><td>".$rk->time."</td><td><a href='".$rk->referrer."'>".iri_StatPress_Abbrevia($rk->referrer,80)."</a></td><td><a href='".get_bloginfo('url')."/?".$rk->urlrequested."'>". __('page viewed','statpress'). "</a></td></tr>\n";
589 print "</table></div>";
593 print "<div class='wrap'><h2>".__('Last agents','statpress')."</h2><table class='widefat'><thead><tr><th scope='col'>".__('Date','statpress')."</th><th scope='col'>".__('Time','statpress')."</th><th scope='col'>".__('Agent','statpress')."</th><th scope='col'>".__('What','statpress')."</th></tr></thead>";
594 print "<tbody id='the-list'>";
595 $qry = $wpdb->get_results("SELECT date,time,agent,os,browser,spider FROM $table_name WHERE (agent <>'') ORDER BY id DESC $querylimit");
596 foreach ($qry as $rk) {
597 print "<tr><td>".irihdate($rk->date)."</td><td>".$rk->time."</td><td>".$rk->agent."</td><td> ".$rk->os. " ".$rk->browser." ".$rk->spider."</td></tr>\n";
599 print "</table></div>";
603 print "<div class='wrap'><h2>".__('Last agents','statpress')."</h2><table class='widefat'><thead><tr><th scope='col'>".__('Agent','statpress')."</th><th scope='col'>OS</th><th scope='col'></th><th scope='col'>Browser/Spider</th></tr></thead>";
604 print "<tbody id='the-list'>";
605 $qry = $wpdb->get_results("SELECT agent,os,browser,spider FROM $table_name GROUP BY agent,os,browser,spider ORDER BY id DESC $querylimit");
606 foreach ($qry as $rk) {
607 print "<tr><td>".$rk->agent."</td><td>".$rk->os."</td>";
608 if($rk->browser != '') {
609 $img=str_replace(" ","",strtolower($rk->browser)).".png";
610 print "<td><IMG style='border:0px;width:16px;height:16px;' SRC='/wp-content/plugins/".dirname(plugin_basename(__FILE__))."/images/browsers/$img'></td>";
612 print "<td>".$rk->browser." ".$rk->spider."</td></tr>\n";
614 print "</table></div>";
618 print "<div class='wrap'><h2>".__('Last pages','statpress')."</h2><table class='widefat'><thead><tr><th scope='col'>".__('Date','statpress')."</th><th scope='col'>".__('Time','statpress')."</th><th scope='col'>".__('Page','statpress')."</th><th scope='col'>".__('What','statpress')."</th></tr></thead>";
619 print "<tbody id='the-list'>";
620 $qry = $wpdb->get_results("SELECT date,time,urlrequested,os,browser,spider FROM $table_name WHERE (spider='' AND feed='') ORDER BY id DESC $querylimit");
621 foreach ($qry as $rk) {
622 print "<tr><td>".irihdate($rk->date)."</td><td>".$rk->time."</td><td>".iri_StatPress_Abbrevia(iri_StatPress_Decode($rk->urlrequested),60)."</td><td> ".$rk->os. " ".$rk->browser." ".$rk->spider."</td></tr>\n";
624 print "</table></div>";
627 print "<div class='wrap'><h2>".__('Last spiders','statpress')."</h2><table class='widefat'><thead><tr><th scope='col'>".__('Date','statpress')."</th><th scope='col'>".__('Time','statpress')."</th><th scope='col'>".__('Spider','statpress')."</th><th scope='col'>".__('Agent','statpress')."</th></tr></thead>";
628 print "<tbody id='the-list'>";
629 $qry = $wpdb->get_results("SELECT date,time,agent,os,browser,spider FROM $table_name WHERE (spider<>'') ORDER BY id DESC $querylimit");
630 foreach ($qry as $rk) {
631 print "<tr><td>".irihdate($rk->date)."</td><td>".$rk->time."</td><td>".$rk->spider."</td><td> ".$rk->agent."</td></tr>\n";
633 print "</table></div>";
637 print " <i>StatPress table size: <b>".iritablesize($wpdb->prefix . "statpress")."</b></i><br />";
638 print " <i>StatPress current time: <b>".current_time('mysql')."</b></i><br />";
639 print " <i>RSS2 url: <b>".get_bloginfo('rss2_url').' ('.iri_StatPress_extractfeedreq(get_bloginfo('rss2_url')).")</b></i><br />";
644 function iri_StatPress_extractfeedreq($url) {
645 list($null,$q)=explode("?",$url);
646 list($res,$null)=explode("&",$q);
650 function iriStatPressDetails() {
652 $table_name = $wpdb->prefix . "statpress";
654 $querylimit="LIMIT 10";
657 iriValueTable2("date","Top days",5);
660 iriValueTable2("os","O.S.",0,"","","AND feed='' AND spider='' AND os<>''");
663 iriValueTable2("browser","Browser",0,"","","AND feed='' AND spider='' AND browser<>''");
666 iriValueTable2("feed","Feeds",5,"","","AND feed<>''");
669 iriValueTable2("searchengine","Search engines",10,"","","AND searchengine<>''");
672 iriValueTable2("search","Top search terms",20,"","","AND search<>''");
675 iriValueTable2("referrer","Top referrer",10,"","","AND referrer<>'' AND referrer NOT LIKE '%".get_bloginfo('url')."%'");
678 iriValueTable2("nation","Languages",10,"","","AND nation<>'' AND spider=''");
681 iriValueTable2("spider","Spiders",10,"","","AND spider<>''");
684 iriValueTable2("urlrequested","Top pages",5,"","urlrequested","AND feed='' and spider=''");
687 # Top Days - Unique visitors
688 iriValueTable2("date","Top Days - Unique visitors",5,"distinct","ip","AND feed='' and spider=''"); /* Maddler 04112007: required patching iriValueTable */
690 # Top Days - Pageviews
691 iriValueTable2("date","Top Days - Pageviews",5,"","urlrequested","AND feed='' and spider=''"); /* Maddler 04112007: required patching iriValueTable */
693 # Top IPs - Pageviews
694 iriValueTable2("ip","Top IPs - Pageviews",5,"","urlrequested","AND feed='' and spider=''"); /* Maddler 04112007: required patching iriValueTable */
698 function iriStatPressSpy() {
700 $table_name = $wpdb->prefix . "statpress";
703 $today = gmdate('Ymd', current_time('timestamp'));
704 $yesterday = gmdate('Ymd', current_time('timestamp')-86400);
705 print "<div class='wrap'><h2>".__('Spy','statpress')."</h2>";
706 $sql="SELECT ip,nation,os,browser,agent FROM $table_name WHERE (spider='' AND feed='') AND (date BETWEEN '$yesterday' AND '$today') GROUP BY ip ORDER BY id DESC LIMIT 20";
707 $qry = $wpdb->get_results($sql);
711 function ttogle(thediv){
712 if (document.getElementById(thediv).style.display=="inline") {
713 document.getElementById(thediv).style.display="none"
714 } else {document.getElementById(thediv).style.display="inline"}
718 <table id="mainspytab" name="mainspytab" width="99%" border="0" cellspacing="0" cellpadding="4">
720 foreach ($qry as $rk) {
721 print "<tr><td colspan='2' bgcolor='#dedede'><div align='left'>";
722 print "<IMG SRC='http://api.hostip.info/flag.php?ip=".$rk->ip."' border=0 width=18 height=12>";
723 print " <strong><span><font size='2' color='#7b7b7b'>".$rk->ip."</font></span></strong> ";
724 print "<span style='color:#006dca;cursor:pointer;border-bottom:1px dotted #AFD5F9;font-size:8pt;' onClick=ttogle('".$rk->ip."');>".__('more info','statpress')."</span></div>";
725 print "<div id='".$rk->ip."' name='".$rk->ip."'>".$rk->os.", ".$rk->browser;
726 if(get_option('statpress_cryptip')!='checked') {
727 print "<br><iframe style='overflow:hide;border:0px;width:100%;height:30px;font-family:helvetica;paddng:0;' scrolling='no' marginwidth=0 marginheight=0 src=http://api.hostip.info/get_html.php?ip=".$rk->ip."></iframe>";
729 print "<br><small>".gethostbyaddr($rk->ip)."</small>";
730 print "<br><small>".$rk->agent."</small>";
732 print "<script>document.getElementById('".$rk->ip."').style.display='none';</script>";
734 $qry2=$wpdb->get_results("SELECT * FROM $table_name WHERE ip='".$rk->ip."' AND (date BETWEEN '$yesterday' AND '$today') order by id LIMIT 10");
735 foreach ($qry2 as $details) {
737 print "<td valign='top' width='151'><div><font size='1' color='#3B3B3B'><strong>".irihdate($details->date)." ".$details->time."</strong></font></div></td>";
738 print "<td><div><a href='".get_bloginfo('url')."/?".$details->urlrequested."' target='_blank'>".iri_StatPress_Decode($details->urlrequested)."</a>";
739 if($details->searchengine != '') {
740 print "<br><small>".__('arrived from','statpress')." <b>".$details->searchengine."</b> ".__('searching','statpress')." <a href='".$details->referrer."' target=_blank>".$details->search."</a></small>";
741 } elseif($details->referrer != '' && strpos($details->referrer,get_option('home'))===FALSE) {
742 print "<br><small>".__('arrived from','statpress')." <a href='".$details->referrer."' target=_blank>".$details->referrer."</a></small>";
756 function iri_CheckIP($ip) {
757 return ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $ip)) ? FALSE : TRUE;
760 function iriStatPressSearch($what='') {
762 $table_name = $wpdb->prefix . "statpress";
764 $f['urlrequested']=__('URL Requested','statpress');
765 $f['agent']=__('Agent','statpress');
766 $f['referrer']=__('Referrer','statpress');
767 $f['search']=__('Search terms','statpress');
768 $f['searchengine']=__('Search engine','statpress');
769 $f['os']=__('Operative system','statpress');
770 $f['browser']="Browser";
771 $f['spider']="Spider";
774 <div class='wrap'><h2><?php _e('Search','statpress'); ?></h2>
775 <form method=get><table>
777 for($i=1;$i<=3;$i++) {
779 print "<td>".__('Field','statpress')." <select name=where$i><option value=''></option>";
780 foreach ( array_keys($f) as $k ) {
781 print "<option value='$k'";
782 if($_GET["where$i"] == $k) { print " SELECTED "; }
783 print ">".$f[$k]."</option>";
785 print "</select></td>";
786 print "<td><input type=checkbox name=groupby$i value='checked' ".$_GET["groupby$i"]."> ".__('Group by','statpress')."</td>";
787 print "<td><input type=checkbox name=sortby$i value='checked' ".$_GET["sortby$i"]."> ".__('Sort by','statpress')."</td>";
788 print "<td>, ".__('if contains','statpress')." <input type=text name=what$i value='".$_GET["what$i"]."'></td>";
798 <tr><td><input type=checkbox name=oderbycount value=checked <?php print $_GET['oderbycount'] ?>> <?php _e('sort by count if grouped','statpress'); ?></td></tr>
799 <tr><td><input type=checkbox name=spider value=checked <?php print $_GET['spider'] ?>> <?php _e('include spiders/crawlers/bot','statpress'); ?></td></tr>
800 <tr><td><input type=checkbox name=feed value=checked <?php print $_GET['feed'] ?>> <?php _e('include feed','statpress'); ?></td></tr>
807 <td><?php _e('Limit results to','statpress'); ?>
808 <select name=limitquery><?php if($_GET['limitquery'] >0) { print "<option>".$_GET['limitquery']."</option>";} ?><option>1</option><option>5</option><option>10</option><option>20</option><option>50</option></select>
811 <tr><td> </td></tr>
813 <td align=right><input type=submit value=<?php _e('Search','statpress'); ?> name=searchsubmit></td>
819 <input type=hidden name=page value='wp-statpress/statpress.php'><input type=hidden name=statpress_action value=search>
823 if(isset($_GET['searchsubmit'])) {
828 for($i=1;$i<=3;$i++) {
829 if($_GET["where$i"] != '') {
830 $fields.=$_GET["where$i"].",";
833 $fields=rtrim($fields,",");
836 if($_GET['spider'] != 'checked') { $where.=" AND spider=''"; }
837 if($_GET['feed'] != 'checked') { $where.=" AND feed=''"; }
838 for($i=1;$i<=3;$i++) {
839 if(($_GET["what$i"] != '') && ($_GET["where$i"] != '')) {
840 $where.=" AND ".$_GET["where$i"]." LIKE '%".$_GET["what$i"]."%'";
845 for($i=1;$i<=3;$i++) {
846 if(($_GET["sortby$i"] == 'checked') && ($_GET["where$i"] != '')) {
847 $orderby.=$_GET["where$i"].',';
853 for($i=1;$i<=3;$i++) {
854 if(($_GET["groupby$i"] == 'checked') && ($_GET["where$i"] != '')) {
855 $groupby.=$_GET["where$i"].',';
859 $groupby="GROUP BY ".rtrim($groupby,',');
860 $fields.=",count(*) as totale";
861 if($_GET['oderbycount'] == 'checked') { $orderby="totale DESC,".$orderby; }
864 if($orderby != '') { $orderby="ORDER BY ".rtrim($orderby,','); }
867 $limit="LIMIT ".$_GET['limitquery'];
870 print "<h2>".__('Results','statpress')."</h2>";
871 $sql="SELECT $fields FROM $table_name $where $groupby $orderby $limit;";
873 print "<table class='widefat'><thead><tr>";
874 for($i=1;$i<=3;$i++) {
875 if($_GET["where$i"] != '') { print "<th scope='col'>".ucfirst($_GET["where$i"])."</th>"; }
877 if($groupby != '') { print "<th scope='col'>".__('Count','statpress')."</th>"; }
878 print "</tr></thead><tbody id='the-list'>";
879 $qry=$wpdb->get_results($sql,ARRAY_N);
880 foreach ($qry as $rk) {
882 for($i=1;$i<=3;$i++) {
884 if($_GET["where$i"] == 'urlrequested') { print iri_StatPress_Decode($rk[$i-1]); } else { print $rk[$i-1]; }
890 print "<br /><br /><font size=1 color=gray>sql: $sql</font></div>";
895 function iri_StatPress_Abbrevia($s,$c) {
896 $res=""; if(strlen($s)>$c) { $res="..."; }
897 return substr($s,0,$c).$res;
902 function iri_StatPress_Decode($out_url) {
903 if($out_url == '') { $out_url=__('Page','statpress').": Home"; }
904 if(substr($out_url,0,4)=="cat=") { $out_url=__('Category','statpress').": ".get_cat_name(substr($out_url,4)); }
905 if(substr($out_url,0,2)=="m=") { $out_url=__('Calendar','statpress').": ".substr($out_url,6,2)."/".substr($out_url,2,4); }
906 if(substr($out_url,0,2)=="s=") { $out_url=__('Search','statpress').": ".substr($out_url,2); }
907 if(substr($out_url,0,2)=="p=") {
908 $post_id_7 = get_post(substr($out_url,2), ARRAY_A);
909 $out_url = $post_id_7['post_title'];
911 if(substr($out_url,0,8)=="page_id=") {
912 $post_id_7=get_page(substr($out_url,8), ARRAY_A);
913 $out_url = __('Page','statpress').": ".$post_id_7['post_title'];
919 function iri_StatPress_URL() {
920 $urlRequested = (isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '' );
921 if ( $urlRequested == "" ) { // SEO problem!
922 $urlRequested = (isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : '' );
924 if(substr($urlRequested,0,2) == '/?') { $urlRequested=substr($urlRequested,2); }
925 if($urlRequested == '/') { $urlRequested=''; }
926 return $urlRequested;
930 # Converte da data us to default format di Wordpress
931 function irihdate($dt = "00000000") {
932 return mysql2date(get_option('date_format'), substr($dt,0,4)."-".substr($dt,4,2)."-".substr($dt,6,2));
936 function iritablesize($table) {
938 $res = $wpdb->get_results("SHOW TABLE STATUS LIKE '$table'");
939 foreach ($res as $fstatus) {
940 $data_lenght = $fstatus->Data_length;
941 $data_rows = $fstatus->Rows;
943 return number_format(($data_lenght/1024/1024), 2, ",", " ")." Mb ($data_rows records)";
947 function iriValueTable2($fld,$fldtitle,$limit = 0,$param = "", $queryfld = "", $exclude= "") {
949 $table_name = $wpdb->prefix . "statpress";
951 if ($queryfld == '') { $queryfld = $fld; }
952 print "<div class='wrap'><table class='widefat'><thead><tr><th scope='col' style='width:400px;'><h2>$fldtitle</h2></th><th scope='col' style='width:100px;'>".__('Visits','statpress')."</th><th></th></tr></thead>";
953 $rks = $wpdb->get_var("SELECT count($param $queryfld) as rks FROM $table_name WHERE 1=1 $exclude;");
955 $sql="SELECT count($param $queryfld) as pageview, $fld FROM $table_name WHERE 1=1 $exclude GROUP BY $fld ORDER BY pageview DESC";
956 if($limit > 0) { $sql=$sql." LIMIT $limit"; }
957 $qry = $wpdb->get_results($sql);
962 foreach ($qry as $rk) {
963 $pc=round(($rk->pageview*100/$rks),1);
964 if($fld == 'date') { $rk->$fld = irihdate($rk->$fld); }
965 if($fld == 'urlrequested') { $rk->$fld = iri_StatPress_Decode($rk->$fld); }
967 $data[substr($rk->$fld,0,50)]=$rk->pageview;
972 print "<tbody id='the-list'>";
973 if($rks > 0) { // Chart!
974 $chart=iriGoogleChart("","400x200",$data);
975 print "<tr><td></td><td></td><td rowspan='".($limit+2)."'>$chart</td></tr>";
976 foreach ($data as $key => $value) {
977 print "<tr><td style='width:400px;overflow: hidden; white-space: nowrap; text-overflow: ellipsis;'>".$key;
978 print "</td><td style='width:100px;text-align:center;'>".$value."</td>";
982 print "</tbody></table></div><br>\n";
988 function iriDomain($ip) {
989 $host=gethostbyaddr($ip);
990 if (ereg('^([0-9]{1,3}\.){3}[0-9]{1,3}$', $host)) {
993 return substr(strrchr($host,"."),1);
998 function iriGetLanguage($accepted) {
999 return substr($accepted,0,2);
1003 function iriGetQueryPairs($url){
1004 $parsed_url = parse_url($url);
1005 $tab=parse_url($url);
1006 $host = $tab['host'];
1007 if(key_exists("query",$tab)){
1008 $query=$tab["query"];
1009 return explode("&",$query);
1015 function iriGetOS($arg){
1016 $arg=str_replace(" ","",$arg);
1017 $lines = file(ABSPATH.'wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/def/os.dat');
1018 foreach($lines as $line_num => $os) {
1019 list($nome_os,$id_os)=explode("|",$os);
1020 if(strpos($arg,$id_os)===FALSE) continue;
1021 return $nome_os; // riconosciuto
1027 function iriGetBrowser($arg){
1028 $arg=str_replace(" ","",$arg);
1029 $lines = file(ABSPATH.'wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/def/browser.dat');
1030 foreach($lines as $line_num => $browser) {
1031 list($nome,$id)=explode("|",$browser);
1032 if(strpos($arg,$id)===FALSE) continue;
1033 return $nome; // riconosciuto
1038 function iriCheckBanIP($arg){
1039 $lines = file(ABSPATH.'wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/def/banips.dat');
1040 foreach($lines as $line_num => $banip) {
1041 if(strpos($arg,rtrim($banip,"\n"))===FALSE) continue;
1042 return ''; // riconosciuto, da scartare
1047 function iriGetSE($referrer = null){
1049 $lines = file(ABSPATH.'wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/def/searchengines.dat');
1050 foreach($lines as $line_num => $se) {
1051 list($nome,$url,$key)=explode("|",$se);
1052 if(strpos($referrer,$url)===FALSE) continue;
1054 $variables = iriGetQueryPairs(html_entity_decode($referrer));
1055 $i = count($variables);
1057 $tab=explode("=",$variables[$i]);
1058 if($tab[0] == $key){return ($nome."|".urldecode($tab[1]));}
1064 function iriGetSpider($agent = null){
1065 $agent=str_replace(" ","",$agent);
1067 $lines = file(ABSPATH.'wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/def/spider.dat');
1068 foreach($lines as $line_num => $spider) {
1069 list($nome,$key)=explode("|",$spider);
1070 if(strpos($agent,$key)===FALSE) continue;
1078 function iri_StatPress_lastmonth() {
1079 $ta = getdate(current_time('timestamp'));
1081 $year = $ta['year'];
1082 $month = $ta['mon'];
1084 --$month; // go back 1 month
1086 if( $month === 0 ): // if this month is Jan
1087 --$year; // go back a year
1088 $month = 12; // last month is Dec
1091 // return in format 'YYYYMM'
1092 return sprintf( $year.'%02d', $month);
1096 function iri_StatPress_CreateTable() {
1098 global $wp_db_version;
1099 $table_name = $wpdb->prefix . "statpress";
1100 $sql_createtable = "CREATE TABLE " . $table_name . " (
1101 id mediumint(9) NOT NULL AUTO_INCREMENT,
1119 if($wp_db_version >= 5540) $page = 'wp-admin/includes/upgrade.php';
1120 else $page = 'wp-admin/upgrade'.'-functions.php';
1121 require_once(ABSPATH . $page);
1122 dbDelta($sql_createtable);
1125 function iri_StatPress_is_feed($url) {
1126 if (stristr($url,get_bloginfo('rdf_url')) != FALSE) { return 'RDF'; }
1127 if (stristr($url,get_bloginfo('rss2_url')) != FALSE) { return 'RSS2'; }
1128 if (stristr($url,get_bloginfo('rss_url')) != FALSE) { return 'RSS'; }
1129 if (stristr($url,get_bloginfo('atom_url')) != FALSE) { return 'ATOM'; }
1130 if (stristr($url,get_bloginfo('comments_rss2_url')) != FALSE) { return 'COMMENT RSS'; }
1131 if (stristr($url,get_bloginfo('comments_atom_url')) != FALSE) { return 'COMMENT ATOM'; }
1132 if (stristr($url,'wp-feed.php') != FALSE) { return 'RSS2'; }
1133 if (stristr($url,'/feed/') != FALSE) { return 'RSS2'; }
1137 function iriStatAppend() {
1139 $table_name = $wpdb->prefix . "statpress";
1142 get_currentuserinfo();
1146 $timestamp = current_time('timestamp');
1147 $vdate = gmdate("Ymd",$timestamp);
1148 $vtime = gmdate("H:i:s",$timestamp);
1151 $ipAddress = $_SERVER['REMOTE_ADDR'];
1152 if(iriCheckBanIP($ipAddress) == '') { return ''; }
1153 if(get_option('statpress_cryptip')=='checked') {
1154 $ipAddress = crypt($ipAddress,'statpress');
1158 $urlRequested=iri_StatPress_URL();
1159 if (eregi(".ico$", $urlRequested)) { return ''; }
1160 if (eregi("favicon.ico", $urlRequested)) { return ''; }
1161 if (eregi(".css$", $urlRequested)) { return ''; }
1162 if (eregi(".js$", $urlRequested)) { return ''; }
1163 if (stristr($urlRequested,"/wp-content/plugins") != FALSE) { return ''; }
1164 if (stristr($urlRequested,"/wp-content/themes") != FALSE) { return ''; }
1165 if (stristr($urlRequested,"/wp-admin/") != FALSE) { return ''; }
1167 $referrer = (isset($_SERVER['HTTP_REFERER']) ? htmlentities($_SERVER['HTTP_REFERER']) : '');
1168 $userAgent = (isset($_SERVER['HTTP_USER_AGENT']) ? htmlentities($_SERVER['HTTP_USER_AGENT']) : '');
1169 $spider=iriGetSpider($userAgent);
1171 if(($spider != '') and (get_option('statpress_donotcollectspider')=='checked')) { return ''; }
1174 $os=''; $browser='';
1177 $feed=iri_StatPress_is_feed(get_bloginfo('url').$_SERVER['REQUEST_URI']);
1178 // Get OS and browser
1179 $os=iriGetOS($userAgent);
1180 $browser=iriGetBrowser($userAgent);
1181 list($searchengine,$search_phrase)=explode("|",iriGetSE($referrer));
1183 // Auto-delete visits if...
1184 if(get_option('statpress_autodelete') != '') {
1185 $t=gmdate("Ymd",strtotime('-'.get_option('statpress_autodelete')));
1186 $results = $wpdb->query( "DELETE FROM " . $table_name . " WHERE date < '" . $t . "'");
1188 if ((!is_user_logged_in()) OR (get_option('statpress_collectloggeduser')=='checked')) {
1189 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
1190 iri_StatPress_CreateTable();
1192 $insert = "INSERT INTO " . $table_name .
1193 " (date, time, ip, urlrequested, agent, referrer, search,nation,os,browser,searchengine,spider,feed,user,timestamp) " .
1194 "VALUES ('$vdate','$vtime','$ipAddress','$urlRequested','".addslashes(strip_tags($userAgent))."','$referrer','" .
1195 addslashes(strip_tags($search_phrase))."','".iriGetLanguage($_SERVER['HTTP_ACCEPT_LANGUAGE']) .
1196 "','$os','$browser','$searchengine','$spider','$feed','$userdata->user_login','$timestamp')";
1197 $results = $wpdb->query( $insert );
1202 function iriStatPressUpdate() {
1204 $table_name = $wpdb->prefix . "statpress";
1206 $wpdb->show_errors();
1208 print "Updating table struct $table_name... ";
1209 iri_StatPress_CreateTable();
1210 print "".__('done','statpress')."<br>";
1213 print "Updating Feeds... ";
1214 $wpdb->query("UPDATE $table_name SET feed='';");
1216 $wpdb->query("UPDATE $table_name SET feed='RSS2' WHERE urlrequested LIKE '%/feed/%';");
1217 $wpdb->query("UPDATE $table_name SET feed='RSS2' WHERE urlrequested LIKE '%wp-feed.php%';");
1218 # standard blog info urls
1219 $s=iri_StatPress_extractfeedreq(get_bloginfo('comments_atom_url'));
1221 $wpdb->query("UPDATE $table_name SET feed='COMMENT ATOM' WHERE INSTR(urlrequested,'$s')>0;");
1223 $s=iri_StatPress_extractfeedreq(get_bloginfo('comments_rss2_url'));
1225 $wpdb->query("UPDATE $table_name SET feed='COMMENT RSS' WHERE INSTR(urlrequested,'$s')>0;");
1227 $s=iri_StatPress_extractfeedreq(get_bloginfo('atom_url'));
1229 $wpdb->query("UPDATE $table_name SET feed='ATOM' WHERE INSTR(urlrequested,'$s')>0;");
1231 $s=iri_StatPress_extractfeedreq(get_bloginfo('rdf_url'));
1233 $wpdb->query("UPDATE $table_name SET feed='RDF' WHERE INSTR(urlrequested,'$s')>0;");
1235 $s=iri_StatPress_extractfeedreq(get_bloginfo('rss_url'));
1237 $wpdb->query("UPDATE $table_name SET feed='RSS' WHERE INSTR(urlrequested,'$s')>0;");
1239 $s=iri_StatPress_extractfeedreq(get_bloginfo('rss2_url'));
1241 $wpdb->query("UPDATE $table_name SET feed='RSS2' WHERE INSTR(urlrequested,'$s')>0;");
1244 # elim $wpdb->query("UPDATE $table_name SET feed='Y' WHERE (urlrequested LIKE '%feed=%') or (urlrequested LIKE '%wp-rss%') or (urlrequested LIKE '%wp-rdf%') or (urlrequested LIKE '%wp-commentsrss%') or (urlrequested LIKE '%wp-atom%');");
1245 print "".__('done','statpress')."<br>";
1248 print "Updating OSes... ";
1249 $wpdb->query("UPDATE $table_name SET os = '';");
1250 $lines = file(ABSPATH.'wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/def/os.dat');
1251 foreach($lines as $line_num => $os) {
1252 list($nome_os,$id_os)=explode("|",$os);
1253 $qry="UPDATE $table_name SET os = '$nome_os' WHERE os='' AND replace(agent,' ','') LIKE '%".$id_os."%';";
1256 print "".__('done','statpress')."<br>";
1259 print "Updating Browsers... ";
1260 $wpdb->query("UPDATE $table_name SET browser = '';");
1261 $lines = file(ABSPATH.'wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/def/browser.dat');
1262 foreach($lines as $line_num => $browser) {
1263 list($nome,$id)=explode("|",$browser);
1264 $qry="UPDATE $table_name SET browser = '$nome' WHERE browser='' AND replace(agent,' ','') LIKE '%".$id."%';";
1267 print "".__('done','statpress')."<br>";
1270 print "Updating Spiders... ";
1271 $wpdb->query("UPDATE $table_name SET spider = '';");
1272 $lines = file(ABSPATH.'wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/def/spider.dat');
1273 foreach($lines as $line_num => $spider) {
1274 list($nome,$id)=explode("|",$spider);
1275 $qry="UPDATE $table_name SET spider = '$nome',os='',browser='' WHERE spider='' AND replace(agent,' ','') LIKE '%".$id."%';";
1278 print "".__('done','statpress')."<br>";
1281 print "Updating Feeds... ";
1282 $wpdb->query("UPDATE $table_name SET feed = '' WHERE isnull(feed);");
1285 # Update Search engine
1286 print "Updating Search engines... ";
1288 $wpdb->query("UPDATE $table_name SET searchengine = '', search='';");
1289 print "...null-ed!<br>";
1290 $qry = $wpdb->get_results("SELECT id, referrer FROM $table_name");
1291 print "...select-ed!<br>";
1292 foreach ($qry as $rk) {
1293 list($searchengine,$search_phrase)=explode("|",iriGetSE($rk->referrer));
1294 if($searchengine <> '') {
1295 $q="UPDATE $table_name SET searchengine = '$searchengine', search='".addslashes($search_phrase)."' WHERE id=".$rk->id;
1299 print "".__('done','statpress')."<br>";
1301 $wpdb->hide_errors();
1303 print "<br> <h1>".__('Updated','statpress')."!</h1>";
1306 function StatPress_Widget($w='') {
1310 function StatPress_Print($body='') {
1311 print iri_StatPress_Vars($body);
1315 function iri_StatPress_Vars($body) {
1317 $table_name = $wpdb->prefix . "statpress";
1318 if(strpos(strtolower($body),"%visits%") !== FALSE) {
1319 $qry = $wpdb->get_results("SELECT count(DISTINCT(ip)) as pageview FROM $table_name WHERE date = '".gmdate("Ymd",current_time('timestamp'))."' and spider='' and feed='';");
1320 $body = str_replace("%visits%", $qry[0]->pageview, $body);
1322 if(strpos(strtolower($body),"%totalvisits%") !== FALSE) {
1323 $qry = $wpdb->get_results("SELECT count(DISTINCT(ip)) as pageview FROM $table_name WHERE spider='' and feed='';");
1324 $body = str_replace("%totalvisits%", $qry[0]->pageview, $body);
1326 if(strpos(strtolower($body),"%thistotalvisits%") !== FALSE) {
1327 $qry = $wpdb->get_results("SELECT count(DISTINCT(ip)) as pageview FROM $table_name WHERE spider='' and feed='' AND urlrequested='".iri_StatPress_URL()."';");
1328 $body = str_replace("%thistotalvisits%", $qry[0]->pageview, $body);
1330 if(strpos(strtolower($body),"%since%") !== FALSE) {
1331 $qry = $wpdb->get_results("SELECT date FROM $table_name ORDER BY date LIMIT 1;");
1332 $body = str_replace("%since%", irihdate($qry[0]->date), $body);
1334 if(strpos(strtolower($body),"%os%") !== FALSE) {
1335 $userAgent = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '');
1336 $os=iriGetOS($userAgent);
1337 $body = str_replace("%os%", $os, $body);
1339 if(strpos(strtolower($body),"%browser%") !== FALSE) {
1340 $browser=iriGetBrowser($userAgent);
1341 $body = str_replace("%browser%", $browser, $body);
1343 if(strpos(strtolower($body),"%ip%") !== FALSE) {
1344 $ipAddress = $_SERVER['REMOTE_ADDR'];
1345 $body = str_replace("%ip%", $ipAddress, $body);
1347 if(strpos(strtolower($body),"%visitorsonline%") !== FALSE) {
1348 $to_time = current_time('timestamp');
1349 $from_time = strtotime('-4 minutes', $to_time);
1350 $qry = $wpdb->get_results("SELECT count(DISTINCT(ip)) as visitors FROM $table_name WHERE spider='' and feed='' AND timestamp BETWEEN $from_time AND $to_time;");
1351 $body = str_replace("%visitorsonline%", $qry[0]->visitors, $body);
1353 if(strpos(strtolower($body),"%usersonline%") !== FALSE) {
1354 $to_time = current_time('timestamp');
1355 $from_time = strtotime('-4 minutes', $to_time);
1356 $qry = $wpdb->get_results("SELECT count(DISTINCT(ip)) as users FROM $table_name WHERE spider='' and feed='' AND user<>'' AND timestamp BETWEEN $from_time AND $to_time;");
1357 $body = str_replace("%usersonline%", $qry[0]->users, $body);
1359 if(strpos(strtolower($body),"%toppost%") !== FALSE) {
1360 $qry = $wpdb->get_results("SELECT urlrequested,count(*) as totale FROM $table_name WHERE spider='' AND feed='' AND urlrequested LIKE '%p=%' GROUP BY urlrequested ORDER BY totale DESC LIMIT 1;");
1361 $body = str_replace("%toppost%", iri_StatPress_Decode($qry[0]->urlrequested), $body);
1363 if(strpos(strtolower($body),"%topbrowser%") !== FALSE) {
1364 $qry = $wpdb->get_results("SELECT browser,count(*) as totale FROM $table_name WHERE spider='' AND feed='' GROUP BY browser ORDER BY totale DESC LIMIT 1;");
1365 $body = str_replace("%topbrowser%", iri_StatPress_Decode($qry[0]->browser), $body);
1367 if(strpos(strtolower($body),"%topos%") !== FALSE) {
1368 $qry = $wpdb->get_results("SELECT os,count(*) as totale FROM $table_name WHERE spider='' AND feed='' GROUP BY os ORDER BY totale DESC LIMIT 1;");
1369 $body = str_replace("%topos%", iri_StatPress_Decode($qry[0]->os), $body);
1375 function iri_StatPress_TopPosts($limit=5, $showcounts='checked') {
1378 $table_name = $wpdb->prefix . "statpress";
1379 $qry = $wpdb->get_results("SELECT urlrequested,count(*) as totale FROM $table_name WHERE spider='' AND feed='' AND urlrequested LIKE '%p=%' GROUP BY urlrequested ORDER BY totale DESC LIMIT $limit;");
1380 foreach ($qry as $rk) {
1381 $res.="<li><a href='?".$rk->urlrequested."'>".iri_StatPress_Decode($rk->urlrequested)."</a></li>\n";
1382 if(strtolower($showcounts) == 'checked') { $res.=" (".$rk->totale.")"; }
1384 return "$res</ul>\n";
1388 function widget_statpress_init($args) {
1389 if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
1391 // Multifunctional StatPress pluging
1392 function widget_statpress_control() {
1393 $options = get_option('widget_statpress');
1394 if ( !is_array($options) )
1395 $options = array('title'=>'StatPress', 'body'=>'Visits today: %visits%');
1396 if ( $_POST['statpress-submit'] ) {
1397 $options['title'] = strip_tags(stripslashes($_POST['statpress-title']));
1398 $options['body'] = stripslashes($_POST['statpress-body']);
1399 update_option('widget_statpress', $options);
1401 $title = htmlspecialchars($options['title'], ENT_QUOTES);
1402 $body = htmlspecialchars($options['body'], ENT_QUOTES);
1404 echo '<p style="text-align:right;"><label for="statpress-title">' . __('Title:') . ' <input style="width: 250px;" id="statpress-title" name="statpress-title" type="text" value="'.$title.'" /></label></p>';
1405 echo '<p style="text-align:right;"><label for="statpress-body"><div>' . __('Body:', 'widgets') . '</div><textarea style="width: 288px;height:100px;" id="statpress-body" name="statpress-body" type="textarea">'.$body.'</textarea></label></p>';
1406 echo '<input type="hidden" id="statpress-submit" name="statpress-submit" value="1" /><div style="font-size:7pt;">%totalvisits% %visits% %thistotalvisits% %os% %browser% %ip% %since% %visitorsonline% %usersonline% %toppost% %topbrowser% %topos%</div>';
1408 function widget_statpress($args) {
1410 $options = get_option('widget_statpress');
1411 $title = $options['title'];
1412 $body = $options['body'];
1413 echo $before_widget;
1414 print($before_title . $title . $after_title);
1415 print iri_StatPress_Vars($body);
1418 register_sidebar_widget('StatPress', 'widget_statpress');
1419 register_widget_control(array('StatPress','widgets'), 'widget_statpress_control', 300, 210);
1422 function widget_statpresstopposts_control() {
1423 $options = get_option('widget_statpresstopposts');
1424 if ( !is_array($options) ) {
1425 $options = array('title'=>'StatPress TopPosts', 'howmany'=>'5', 'showcounts'=>'checked');
1427 if ( $_POST['statpresstopposts-submit'] ) {
1428 $options['title'] = strip_tags(stripslashes($_POST['statpresstopposts-title']));
1429 $options['howmany'] = stripslashes($_POST['statpresstopposts-howmany']);
1430 $options['showcounts'] = stripslashes($_POST['statpresstopposts-showcounts']);
1431 if($options['showcounts'] == "1") {$options['showcounts']='checked';}
1432 update_option('widget_statpresstopposts', $options);
1434 $title = htmlspecialchars($options['title'], ENT_QUOTES);
1435 $howmany = htmlspecialchars($options['howmany'], ENT_QUOTES);
1436 $showcounts = htmlspecialchars($options['showcounts'], ENT_QUOTES);
1438 echo '<p style="text-align:right;"><label for="statpresstopposts-title">' . __('Title','statpress') . ' <input style="width: 250px;" id="statpress-title" name="statpresstopposts-title" type="text" value="'.$title.'" /></label></p>';
1439 echo '<p style="text-align:right;"><label for="statpresstopposts-howmany">' . __('Limit results to','statpress') . ' <input style="width: 100px;" id="statpresstopposts-howmany" name="statpresstopposts-howmany" type="text" value="'.$howmany.'" /></label></p>';
1440 echo '<p style="text-align:right;"><label for="statpresstopposts-showcounts">' . __('Visits','statpress') . ' <input id="statpresstopposts-showcounts" name="statpresstopposts-showcounts" type=checkbox value="checked" '.$showcounts.' /></label></p>';
1441 echo '<input type="hidden" id="statpress-submitTopPosts" name="statpresstopposts-submit" value="1" />';
1443 function widget_statpresstopposts($args) {
1445 $options = get_option('widget_statpresstopposts');
1446 $title = htmlspecialchars($options['title'], ENT_QUOTES);
1447 $howmany = htmlspecialchars($options['howmany'], ENT_QUOTES);
1448 $showcounts = htmlspecialchars($options['showcounts'], ENT_QUOTES);
1449 echo $before_widget;
1450 print($before_title . $title . $after_title);
1451 print iri_StatPress_TopPosts($howmany,$showcounts);
1454 register_sidebar_widget('StatPress TopPosts', 'widget_statpresstopposts');
1455 register_widget_control(array('StatPress TopPosts','widgets'), 'widget_statpresstopposts_control', 300, 110);
1459 load_plugin_textdomain('statpress', 'wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/locale');
1461 add_action('admin_menu', 'iri_add_pages');
1462 add_action('plugins_loaded', 'widget_statpress_init');
1463 add_action('send_headers', 'iriStatAppend'); //add_action('wp_head', 'iriStatAppend');
1465 register_activation_hook(__FILE__,'iri_StatPress_CreateTable');