weblog-content /wp-content/plugins/wassup/lib/main.php

Language PHP Lines 1340
MD5 Hash 63386f157a0e14f1dedb76120f68e179 Estimated Cost $27,784 (why?)
Repository https://bitbucket.org/openfarmtech/weblog-content View Raw File View Project SPDX
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
<?php
if (!class_exists('pagination')) { 	//in case another app uses this class...
class pagination{
/*
Script Name: *Digg Style Paginator Class
Script URI: http://www.mis-algoritmos.com/2007/05/27/digg-style-pagination-class/
Description: Class in PHP that allows to use a pagination like a digg or sabrosus style.
Script Version: 0.3.2
Author: Victor De la Rocha
Author URI: http://www.mis-algoritmos.com
*/
	/*Default values*/
        var $total_pages;
        var $limit;
        var $target;
        var $page;
        var $adjacents;
        var $showCounter;
        var $className;
        var $parameterName;
        var $urlF ;

        /*Buttons next and previous*/
        var $nextT;
        var $nextI;
        var $prevT;
        var $prevI;

        /*****/
        var $calculate;
	
	#Total items
	function items($value){$this->total_pages = intval($value);}
	
	#how many items to show per page
	function limit($value){$this->limit = intval($value);}
	
	#Page to sent the page value
	function target($value){$this->target = $value;}
	
	#Current page
	function currentPage($value){$this->page = intval($value);}
	
	#How many adjacent pages should be shown on each side of the current page?
	function adjacents($value){$this->adjacents = intval($value);}
	
	#show counter?
	function showCounter($value=""){$this->showCounter=($value===true)?true:false;}

	#to change the class name of the pagination div
	function changeClass($value=""){$this->className=$value;}

	function nextLabel($value){$this->nextT = $value;}
	function nextIcon($value){$this->nextI = $value;}
	function prevLabel($value){$this->prevT = $value;}
	function prevIcon($value){$this->prevI = $value;}

	#to change the class name of the pagination div
	function parameterName($value=""){$this->parameterName=$value;}

	#to change urlFriendly
	function urlFriendly($value="%"){
			if(eregi('^ *$',$value)){
					$this->urlF=false;
					return false;
				}
			$this->urlF=$value;
		}
	
	var $pagination;

	function pagination(){
                /*Set Default values*/
                $this->total_pages = null;
                $this->limit = null;
                $this->target = "";
                $this->page = 1;
                $this->adjacents = 2;
                $this->showCounter = false;
                $this->className = "pagination";
                $this->parameterName = "pages";
                $this->urlF = false;//urlFriendly

                /*Buttons next and previous*/
                $this->nextT = __("Next","wassup");
                $this->nextI = "&#187;"; //&#9658;
                $this->prevT = __("Previous","wassup");
                $this->prevI = "&#171;"; //&#9668;

                $this->calculate = false;
	}
	function show(){
			if(!$this->calculate)
				if($this->calculate())
					echo "<div class=\"$this->className\">$this->pagination</div>";
		}
	function get_pagenum_link($id){
			if(strpos($this->target,'?')===false)
					if($this->urlF)
							return str_replace($this->urlF,$id,$this->target);
						else
							return "$this->target?$this->parameterName=$id";
				else
					return "$this->target&$this->parameterName=$id";
		}
	
	function calculate(){
			$this->pagination = "";
			$this->calculate == true;
			$error = false;
			if($this->urlF and $this->urlF != '%' and strpos($this->target,$this->urlF)===false){
					//Es necesario especificar el comodin para sustituir
					echo 'Especificaste un wildcard para sustituir, pero no existe en el target<br />';
                                        $error = true;
                                }elseif($this->urlF and $this->urlF == '%' and strpos($this->target,$this->urlF)===false){
                                        echo 'Es necesario especificar en el target el comodin';
                                        $error = true;
                                }
                        if($this->total_pages == null){
                                        echo __("It is necessary to specify the","wassup")." <strong>".__("number of pages","wassup")."</strong> (\$class->items(1000))<br />";
                                        $error = true;
                                }
                        if($this->limit == null){
                                        echo __("It is necessary to specify the","wassup")." <strong>".__("limit of items","wassup")."</strong> ".__("to show per page","wassup")." (\$class->limit(10))<br />";
                                        $error = true;
				}
			if($error)return false;
			
			$n = trim($this->nextT.' '.$this->nextI);
			$p = trim($this->prevI.' '.$this->prevT);
			
			/* Setup vars for query. */
			if($this->page) 
				$start = ($this->page - 1) * $this->limit;             //first item to display on this page
			else
				$start = 0;                                //if no page var is given, set start to 0
		
			/* Setup page vars for display. */
			if ($this->page == 0) $this->page = 1;                    //if no page var is given, default to 1.
			$prev = $this->page - 1;                            //previous page is page - 1
			$next = $this->page + 1;                            //next page is page + 1
			$lastpage = ceil($this->total_pages/$this->limit);        //lastpage is = total pages / items per page, rounded up.
			$lpm1 = $lastpage - 1;                        //last page minus 1
			
			/* 
				Now we apply our rules and draw the pagination object. 
				We're actually saving the code to a variable in case we want to draw it more than once.
			*/
			
			if($lastpage > 1){
					//anterior button
					if($this->page > 1)
							$this->pagination .= "<a href=\"".$this->get_pagenum_link($prev)."\">$p</a>";
						else
							$this->pagination .= "<span class=\"disabled\">$p</span>";
					//pages	
					if ($lastpage < 7 + ($this->adjacents * 2)){//not enough pages to bother breaking it up
							for ($counter = 1; $counter <= $lastpage; $counter++){
									if ($counter == $this->page)
											$this->pagination .= "<span class=\"current\">$counter</span>";
										else
											$this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
								}
						}
					elseif($lastpage > 5 + ($this->adjacents * 2)){//enough pages to hide some
							//close to beginning; only hide later pages
							if($this->page < 1 + ($this->adjacents * 2)){
									for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++){
											if ($counter == $this->page)
													$this->pagination .= "<span class=\"current\">$counter</span>";
												else
													$this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
										}
									$this->pagination .= "...";
									$this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
									$this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
								}
							//in middle; hide some front and some back
							elseif($lastpage - ($this->adjacents * 2) > $this->page && $this->page > ($this->adjacents * 2)){
									$this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
									$this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
									$this->pagination .= "...";
									for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++)
										if ($counter == $this->page)
												$this->pagination .= "<span class=\"current\">$counter</span>";
											else
												$this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
									$this->pagination .= "...";
									$this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
									$this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
								}
							//close to end; only hide early pages
							else{
									$this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
									$this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
									$this->pagination .= "...";
									for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++)
										if ($counter == $this->page)
												$this->pagination .= "<span class=\"current\">$counter</span>";
											else
												$this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
								}
						}
					//siguiente button
					if ($this->page < $counter - 1)
							$this->pagination .= "<a href=\"".$this->get_pagenum_link($next)."\">$n</a>";
						else
							$this->pagination .= "<span class=\"disabled\">$n</span>";
						if($this->showCounter)$this->pagination .= "<div class=\"pagination_data\">($this->total_pages ".__("Pages","wassup").")</div>";
				}

			return true;
		}
	} //end class pagination
} //end if !class_exists('pagination')

if (!class_exists('Detector')) { 	//in case another app uses this class...
//
// Detector class (c) Mohammad Hafiz bin Ismail 2006
// detect location by ipaddress
// detect browser type and operating system
//
// November 27, 2006
//
// by : Mohammad Hafiz bin Ismail (info@mypapit.net)
// 
// You are allowed to use this work under the terms of 
// Creative Commons Attribution-Share Alike 3.0 License
// 
// Reference : http://creativecommons.org/licenses/by-sa/3.0/
// 

class Detector {

	var $town;
	var $state;
	var $country;
	var $Ctimeformatode;
	var $longitude;
	var $latitude;
	var $ipaddress;
	var $txt;

	var $browser;
	var $browser_version;
	var $os_version;
	var $os;
	var $useragent;

	function Detector($ip="", $ua="")
	{	
		$apiserver="http://showip.fakap.net/txt/";
		if ($ip != "") {	
		if (preg_match('/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/',$ip,$matches))
		  {
		    $this->ipaddress=$ip;
		  }

		else { $this->ipaddress = "0.0.0.0"; }

		//uncomment this below if CURL doesnt work		

		$this->txt=file_get_contents($apiserver . "$ip");

		$wtf=$this->txt;
		$this->processTxt($wtf);
		}

		$this->useragent=$ua;
		$this->check_os($ua);
		$this->check_browser($ua);
	}

	function processTxt($wtf)

	{
//	  	$tok = strtok($txt, ',');
	  	$this->town = strtok($wtf,',');
	  	$this->state = strtok(',');
	  	$this->country=strtok(',');
	  	$this->ccode = strtok(',');
	  	$this->latitude=strtok(',');
	  	$this->longitude=strtok(',');
	}

	function check_os($useragent) {

			$os = "N/A"; $version = "";

			if (preg_match("/Windows NT 5.1/",$useragent,$match)) {
				$os = "WinXP"; $version = "";
			} elseif (preg_match("/Windows NT 5.2/",$useragent,$match)) {
				$os = "Win2003"; $version = "";
			} elseif (preg_match("/Windows NT 6.0/",$useragent,$match)) {
				$os = "WinVista"; $version = "";
			} elseif (preg_match("/(?:Windows NT 5.0|Windows 2000)/",$useragent,$match)) {
				$os = "Win2000"; $version = "";
			} elseif (preg_match("/Windows ME/",$useragent,$match)) {
				$os = "WinME"; $version = "";
			} elseif (preg_match("/(?:WinNT|Windows\s?NT)\s?([0-9\.]+)?/",$useragent,$match)) {
				$os = "WinNT"; $version = $match[1];
			} elseif (preg_match("/Mac OS X/",$useragent,$match)) {
				$os = "MacOSX"; $version = "";
			} elseif (preg_match("/(Mac_PowerPC|Macintosh)/",$useragent,$match)) {
				$os = "MacPPC"; $version = "";
			} elseif (preg_match("/(?:Windows95|Windows 95|Win95|Win 95)/",$useragent,$match)) {
				$os = "Win95"; $version = "";
			} elseif (preg_match("/(?:Windows98|Windows 98|Win98|Win 98|Win 9x)/",$useragent,$match)) {
				$os = "Win98"; $version = "";
			} elseif (preg_match("/(?:WindowsCE|Windows CE|WinCE|Win CE)/",$useragent,$match)) {
				$os = "WinCE"; $version = "";
			} elseif (preg_match("/PalmOS/",$useragent,$match)) {
				$os = "PalmOS";
			} elseif (preg_match("/\(PDA(?:.*)\)(.*)Zaurus/",$useragent,$match)) {
				$os = "Sharp Zaurus";
			} elseif (preg_match("/Linux\s*((?:i[0-9]{3})?\s*(?:[0-9]\.[0-9]{1,2}\.[0-9]{1,2})?\s*(?:i[0-9]{3})?)?/",$useragent,$match)) {
				$os = "Linux"; $version = $match[1];
			} elseif (preg_match("/NetBSD\s*((?:i[0-9]{3})?\s*(?:[0-9]\.[0-9]{1,2}\.[0-9]{1,2})?\s*(?:i[0-9]{3})?)?/",$useragent,$match)) {
				$os = "NetBSD"; $version = $match[1];
			} elseif (preg_match("/OpenBSD\s*([0-9\.]+)?/",$useragent,$match)) {
				$os = "OpenBSD"; $version = $match[1];
			} elseif (preg_match("/CYGWIN\s*((?:i[0-9]{3})?\s*(?:[0-9]\.[0-9]{1,2}\.[0-9]{1,2})?\s*(?:i[0-9]{3})?)?/",$useragent,$match)) {
				$os = "CYGWIN"; $version = $match[1];
			} elseif (preg_match("/SunOS\s*([0-9\.]+)?/",$useragent,$match)) {
				$os = "SunOS"; $version = $match[1];
			} elseif (preg_match("/IRIX\s*([0-9\.]+)?/",$useragent,$match)) {
				$os = "SGI IRIX"; $version = $match[1];
			} elseif (preg_match("/FreeBSD\s*((?:i[0-9]{3})?\s*(?:[0-9]\.[0-9]{1,2})?\s*(?:i[0-9]{3})?)?/",$useragent,$match)) {
				$os = "FreeBSD"; $version = $match[1];
			} elseif (preg_match("/SymbianOS\/([0-9.]+)/i",$useragent,$match)) {
				$os = "SymbianOS"; $version = $match[1];
			} elseif (preg_match("/Symbian\/([0-9.]+)/i",$useragent,$match)) {
				$os = "Symbian"; $version = $match[1];
			} elseif (preg_match("/PLAYSTATION 3/",$useragent,$match)) {
				$os = "Playstation"; $version = 3;
			}

			$this->os = $os;
			$this->os_version = $version;
		}

		function check_browser($useragent) {

			$browser = "";

			if (preg_match("/^Mozilla(?:.*)compatible;\sMSIE\s(?:.*)Opera\s([0-9\.]+)/",$useragent,$match)) {
				$browser = "Opera";
			} elseif (preg_match("/^Opera\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "Opera";
			} elseif (preg_match("/^Mozilla(?:.*)compatible;\siCab\s([0-9\.]+)/",$useragent,$match)) {
				$browser = "iCab";
			} elseif (preg_match("/^iCab\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "iCab";
			} elseif (preg_match("/^Mozilla(?:.*)compatible;\sMSIE\s([0-9\.]+)/",$useragent,$match)) {
				$browser = "IE";
			} elseif (preg_match("/^(?:.*)compatible;\sMSIE\s([0-9\.]+)/",$useragent,$match)) {
				$browser = "IE";
			} elseif (preg_match("/^Mozilla(?:.*)(?:.*)Chrome/",$useragent,$match)) {
				$browser = "Google Chrome";
			} elseif (preg_match("/^Mozilla(?:.*)(?:.*)Safari\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "Safari";
			} elseif (preg_match("/^Mozilla(?:.*)\(Macintosh(?:.*)OmniWeb\/v([0-9\.]+)/",$useragent,$match)) {
				$browser = "Omniweb";
			} elseif (preg_match("/^Mozilla(?:.*)\(compatible; Google Desktop/",$useragent,$match)) {
				$browser = "Google Desktop";
			} elseif (preg_match("/^Mozilla(?:.*)\(compatible;\sOmniWeb\/([0-9\.v-]+)/",$useragent,$match)) {
				$browser = "Omniweb";
			} elseif (preg_match("/^Mozilla(?:.*)Gecko(?:.*?)(?:Camino|Chimera)\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "Camino";
			} elseif (preg_match("/^Mozilla(?:.*)Gecko(?:.*?)Netscape\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "Netscape";
			} elseif (preg_match("/^Mozilla(?:.*)Gecko(?:.*?)(?:Fire(?:fox|bird)|Phoenix)\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "Firefox";
			} elseif (preg_match("/^Mozilla(?:.*)Gecko(?:.*?)Minefield\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "Minefield";
			} elseif (preg_match("/^Mozilla(?:.*)Gecko(?:.*?)Epiphany\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "Epiphany";
			} elseif (preg_match("/^Mozilla(?:.*)Galeon\/([0-9\.]+)\s(?:.*)Gecko/",$useragent,$match)) {
				$browser = "Galeon";
			} elseif (preg_match("/^Mozilla(?:.*)Gecko(?:.*?)K-Meleon\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "K-Meleon";
			} elseif (preg_match("/^Mozilla(?:.*)rv:([0-9\.]+)\)\sGecko/",$useragent,$match)) {
				$browser = "Mozilla";
			} elseif (preg_match("/^Mozilla(?:.*)compatible;\sKonqueror\/([0-9\.]+);/",$useragent,$match)) {
				$browser = "Konqueror";
			} elseif (preg_match("/^Mozilla\/(?:[34]\.[0-9]+)(?:.*)AvantGo\s([0-9\.]+)/",$useragent,$match)) {
				$browser = "AvantGo";
			} elseif (preg_match("/^Mozilla(?:.*)NetFront\/([34]\.[0-9]+)/",$useragent,$match)) {
				$browser = "NetFront";
			} elseif (preg_match("/^Mozilla\/([34]\.[0-9]+)/",$useragent,$match)) {
				$browser = "Netscape";
			} elseif (preg_match("/^Liferea\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "Liferea";
			} elseif (preg_match("/^curl\/([0-9\.]+)/",$useragent,$match)) {
				$browser = "curl";
			} elseif (preg_match("/^links\/([0-9\.]+)/i",$useragent,$match)) {
				$browser = "Links";
			} elseif (preg_match("/^links\s?\(([0-9\.]+)/i",$useragent,$match)) {
				$browser = "Links";
			} elseif (preg_match("/^lynx\/([0-9a-z\.]+)/i",$useragent,$match)) {
				$browser = "Lynx";
			} elseif (preg_match("/^Wget\/([0-9\.]+)/i",$useragent,$match)) {
				$browser = "Wget";
			} elseif (preg_match("/^Xiino\/([0-9\.]+)/i",$useragent,$match)) {
				$browser = "Xiino";
			} elseif (preg_match("/^W3C_Validator\/([0-9\.]+)/i",$useragent,$match)) {
				$browser = "W3C Validator";
			} elseif (preg_match("/^Jigsaw(?:.*) W3C_CSS_Validator_(?:[A-Z]+)\/([0-9\.]+)/i",$useragent,$match)) {
				$browser = "W3C CSS Validator";
			} elseif (preg_match("/^Dillo\/([0-9\.]+)/i",$useragent,$match)) {
				$browser = "Dillo";
			} elseif (preg_match("/^amaya\/([0-9\.]+)/i",$useragent,$match)) {
				$browser = "Amaya";
			} elseif (preg_match("/^DocZilla\/([0-9\.]+)/i",$useragent,$match)) {
				$browser = "DocZilla";
			} elseif (preg_match("/^fetch\slibfetch\/([0-9\.]+)/i",$useragent,$match)) {
				$browser = "FreeBSD libfetch";
			} elseif (preg_match("/^Nokia([0-9a-zA-Z\-.]+)\/([0-9\.]+)/i",$useragent,$match)) {
				$browser="Nokia";
			} elseif (preg_match("/^SonyEricsson([0-9a-zA-Z\-.]+)\/([a-zA-Z0-9\.]+)/i",$useragent,$match)) {
				$browser="SonyEricsson";
			}

			//$version = $match[1];
			//restrict version to major and minor version #'s
			preg_match("/^\d+(\.\d+)?/",$match[1],$majorvers);
			$version = $majorvers[0];

			$this->browser = $browser;
			$this->browser_version = $version;
	}
} //end class Detector
} //end if !class_exists('Detector')

function wassup_get_time() {
	$timeright = gmdate("U");
	$offset = (get_option("gmt_offset")*60*60);
	$timeright = ($timeright + $offset) ;
	return $timeright;
}

/*
# PHP Calendar (version 2.3), written by Keith Devens
# http://keithdevens.com/software/php_calendar
#  see example at http://keithdevens.com/weblog
# License: http://keithdevens.com/software/license
*/
//
// Currently not used in WassUp it's a next implementation idea
//
function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array()){
	$first_of_month = gmmktime(0,0,0,$month,1,$year);
	#remember that mktime will automatically correct if invalid dates are entered
	# for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
	# this provides a built in "rounding" feature to generate_calendar()

	$day_names = array(); #generate all the day names according to the current locale
	for($n=0,$t=(3+$first_day)*86400; $n<7; $n++,$t+=86400) #January 4, 1970 was a Sunday
		$day_names[$n] = ucfirst(gmstrftime('%A',$t)); #%A means full textual day name

	list($month, $year, $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
	$weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
	$title   = htmlentities(ucfirst($month_name)).'&nbsp;'.$year;  #note that some locales don't capitalize month and day names

	#Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
	@list($p, $pl) = each($pn); @list($n, $nl) = each($pn); #previous and next links, if applicable
	if($p) $p = '<span class="calendar-prev">'.($pl ? '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' : $p).'</span>&nbsp;';
	if($n) $n = '&nbsp;<span class="calendar-next">'.($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n).'</span>';
	$calendar = '<table class="calendar">'."\n".
		'<caption class="calendar-month">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."</caption>\n<tr>";

	if($day_name_length){ #if the day names should be shown ($day_name_length > 0)
		#if day_name_length is >3, the full name of the day will be printed
		foreach($day_names as $d)
			$calendar .= '<th abbr="'.htmlentities($d).'">'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</th>';
		$calendar .= "</tr>\n<tr>";
	}

	if($weekday > 0) $calendar .= '<td colspan="'.$weekday.'">&nbsp;</td>'; #initial 'empty' days
	for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++){
		if($weekday == 7){
			$weekday   = 0; #start a new week
			$calendar .= "</tr>\n<tr>";
		}
		if(isset($days[$day]) and is_array($days[$day])){
			@list($link, $classes, $content) = $days[$day];
			if(is_null($content))  $content  = $day;
			$calendar .= '<td'.($classes ? ' class="'.htmlspecialchars($classes).'">' : '>').
				($link ? '<a href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
		}
		else $calendar .= "<td>$day</td>";
	}
	if($weekday != 7) $calendar .= '<td colspan="'.(7-$weekday).'">&nbsp;</td>'; #remaining "empty" days

	return $calendar."</tr>\n</table>\n";
}

//Truncate $input string to a length of $max
function stringShortener($input, $max=0, $separator="(...)", $exceedFromEnd=0){
	if(!$input || !is_string($input)){return false;};
	
	//Replace all %-hex chars with literals and trim the input string 
	//  of whitespaces ...because it's shorter and more legible. 
	//  -Helene D. 11/18/07
	$instring = trim(stripslashes(rawurldecode(html_entity_decode($input)))," +\t");	//insecure

	$inputlen=strlen($instring);
	$max=(is_numeric($max))?(integer)$max:$inputlen;
	//if($max>=$inputlen){return $input;};	//caused security loophole ...only $outstring should be returned
	if ($max < $inputlen) {
		$separator=($separator)?$separator:"(...)";
		$modulus=(($max%2));
		$halfMax=floor($max/2);
		$begin="";
		if(!$modulus){$begin=substr($instring, 0, $halfMax);}
		else{$begin=(!$exceedFromEnd)? substr($instring, 0, $halfMax+1) : substr($instring, 0, $halfMax);}
		$end="";
		if(!$modulus){$end=substr($instring,$inputlen-$halfMax);}
		else{$end=($exceedFromEnd)? substr($instring,$inputlen-$halfMax-1) :substr($instring,$inputlen-$halfMax);}
		$extracted=substr($instring, strpos($instring,$begin)+strlen($begin), $inputlen-$max );
		$outstring = $begin.$separator.$end;
		if (strlen($outstring) >= $inputlen) {  //Because "Fir(...)fox" is longer than "Firefox"
			$outstring = $instring;
		}
		//# use WordPress 2.x function attribute_escape and 1.2.x 
		//  function wp_specialchars to make malicious code 
		//  harmless when echoed to the screen
		$outstring=attribute_escape(wp_specialchars($outstring,ENT_QUOTES));
	} else {
		$outstring = attribute_escape(wp_specialchars($instring,ENT_QUOTES));
	}
	return $outstring;
} //end function stringShortener

//# Return a value of true if url argument is a root url and false when
//#  url constains a subdirectory path or query parameters...
//#  - Helene D. 2007
function url_rootcheck($urltocheck) {
	$isroot = false;
	//url must begin with 'http://'
	if (strncasecmp($urltocheck,'http://',7) == 0) {
		$isroot = true;
		$urlparts=parse_url($urltocheck);
		if (!empty($urlparts['path']) && $urlparts['path'] != "/") {
			$isroot=false;
		} elseif (!empty($urlparts['query'])) {
			$isroot=false;
		}
	}
	return $isroot;
}

//#from a page/post url input, output a url with "$blogurl" prepended for 
//#  blogs that have wordpress installed in a separate folder
//#  -Helene D. 1/22/08
function wAddSiteurl($inputurl) {
	$wpurl = rtrim(get_bloginfo('wpurl'),"/");
	$blogurl = rtrim(get_bloginfo('home'),"/");
	if (strcasecmp($blogurl, $wpurl) == 0) {
		$outputurl=$inputurl;
	} elseif (stristr($inputurl,$blogurl) === FALSE && url_rootcheck($blogurl))  {
		$outputurl=$blogurl."/".ltrim($inputurl,"/");
	} else {
		$outputurl=$inputurl;
	}
	$outputurl = rawurldecode(html_entity_decode($outputurl)); //dangerous
	$outputurl = wCleanURL($outputurl);	//safe
	return $outputurl;
}

//sanitize url of potentially dangerous code before display
function wCleanURL($url="") { 
	if (empty($url)) { 
		return;
	}
	//$urlstring = stripslashes($url);
	if (function_exists('esc_url')) {	//#WP 2.8+
		$cleaned_url = esc_url(stripslashes($url));
	} else {
		$cleaned_url = clean_url(stripslashes($url));
	}
	if (empty($cleaned_url)) {	//oops, clean_url chomp
		$cleaned_url = attribute_escape(stripslashes($url));
	}
	return $cleaned_url;
} //end function

//Output wassup records in Digg spy style...
function spyview ($from_date="",$to_date="",$rows="999",$spytype="",$spy_datasource="") {
	global $wpdb, $wp_version, $debug_mode;

	$whereis="";
	if ($spytype == 'spider') {
		$whereis = " AND spider!=''";
	} elseif ($spytype == 'nospider') {
		$whereis = " AND spider=''";
	} elseif ($spytype == 'spam') {
		$whereis = " AND spam>0";
	} elseif ($spytype == 'nospam') {
		$whereis = " AND spam=0";
	} elseif ($spytype == 'nospamspider') {
		$whereis = " AND spam=0 AND spider=''";
	} elseif ($spytype == 'searchengine') {
		$whereis = " AND searchengine!='' AND search!=''";
	} elseif ($spytype == 'referrer') {
		$whereis = " AND referrer!='' AND referrer NOT LIKE '%$wpurl%' AND searchengine='' AND search=''";
	} elseif ($spytype == 'comauthor') {
		$whereis = " AND comment_author!=''";
	} elseif ($spytype == 'loggedin') {
		$whereis = " AND username!=''";
	}
	//check for arguments...
	if(empty($to_date)) $to_date = wassup_get_time();
	if (empty($from_date)) $from_date = ($to_date - 5);
	if (empty($spy_datasource)) {
		//temp table is default data source unless not exists
		$spy_datasource = $wpdb->prefix . "wassup_tmp";
		if ($wpdb->get_var("SHOW TABLES LIKE '$spy_datasource'") != $spy_datasource) { 
			$spy_datasource = $wpdb->prefix . "wassup";
		}
	}

	if (function_exists('get_option')) {
		$wassup_settings = get_option('wassup_settings');
	}
	if (!empty($wassup_settings['wassup_screen_res'])) {
		$screen_res_size = (int) $wassup_settings['wassup_screen_res'];
	} else { 
		$screen_res_size = 670;
	}
	$max_char_len = ($screen_res_size)/10;
	//set smaller screen_res_size to make room for sidebar in WP2.7+
	if (version_compare($wp_version, '2.7', '>=')) { 
		$screen_res_size = $screen_res_size-160;
		$max_char_len = $max_char_len-16;
	}
	$wpurl = get_bloginfo('wpurl');
	$blogurl = get_bloginfo('home');
	$unclass = "sum-box";

	$qryC = $wpdb->get_results("SELECT id, wassup_id, `timestamp`, ip, hostname, searchengine, urlrequested, agent, referrer, spider, username, comment_author FROM $spy_datasource WHERE `timestamp` BETWEEN $from_date AND $to_date $whereis ORDER BY `timestamp` DESC");
	if (!empty($qryC)) {
		//restrict # of rows to display when needed...
		$row_count = 0;
	//display the rows...
	foreach ($qryC as $cv) {
		$unclass = "sum-box";
		if ( $row_count < (int)$rows ) {
		   $timestamp = $cv->timestamp;
		   $ip = @explode(",", $cv->ip);
		   if ($cv->referrer != '') {
		   	if ($cv->searchengine != "" || stristr($cv->referrer,$wpurl)!=$cv->referrer) { 
		   	if ($cv->searchengine == "") {
				$referrer = '<a href="'.wCleanURL($cv->referrer).'" target=_"BLANK"><span style="font-weight: bold;">'.stringShortener("{$cv->referrer}", round($max_char_len*.8,0)).'</span></a>';
		   	} else {
				 $referrer = '<a href="'.wCleanURL($cv->referrer).'" target=_"BLANK">'.stringShortener("{$cv->referrer}", round($max_char_len*.9,0)).'</a>';
		   	}
		   	} else { 
		   		$referrer = __('From your blog','wassup');
		   	}
		   } else {
		   	$referrer = __('Direct hit','wassup');
		   } 
		   // User is logged in or is a comment's author
		   if ($cv->username != "") {
		   	$unclass .= "-log";
		   	$map_icon = "marker_loggedin.png";
		   } elseif ($cv->comment_author != "") {
		   	$unclass .= "-aut";
			$map_icon = "marker_author.png";
		   } elseif ($cv->spider != "") {
		   	$unclass .= "-spider";
		   	$map_icon = "marker_bot.png";
		   } else {
		   	$map_icon = "marker_user.png";
		   }

		// Start getting GEOIP info
		   $location="";
		   $lat = 0;
		   $lon = 0;
		   if (function_exists('curl_init')) {
			//TODO: save geo data in 'wassup_tmp_geoloc' table
			//      so multi-page visits from save ip don't do
			//      redundant curl lookups
		   	$geo_url = "http://api.hostip.info/get_html.php?ip=".$ip[0]."&position=true";
		   	$ci = curl_init();

		   	curl_setopt($ci, CURLOPT_URL, $geo_url);
		   	curl_setopt($ci, CURLOPT_HEADER,0);
		   	curl_setopt($ci, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		   	@curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 0);
		   	curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);

		   	$data = curl_exec($ci);
		   	curl_close($ci);

	 	   	$data = explode("\n",$data);
		   	$loc_country = "";
			$loc_city = "";
			if (stristr("unknown", $data[0]) === FALSE) {
		   		$loc_country = preg_replace('/country: /i', "", $data[0]);
		   	}
		   	if (!empty($data[1]) && stristr("unknown", $data[1]) === FALSE) {
		   		$loc_city = preg_replace('/city: /i', '', $data[1]);
		   	}
			$geoloc = trim($loc_country." ".$loc_city);
			if ($debug_mode) {
				echo "<!--\n Curl data: \n"; //debug
				echo var_dump($data);	//debug
				echo "\n geoloc=$geoloc \n";	//debug
				echo "-->\n"; //debug
			}

		   	if ($wassup_settings['wassup_geoip_map'] == 1) {
		   		$gkey = $wassup_settings['wassup_googlemaps_key'];
		   	if ($geoloc != "") {
		   		$geocode = geocodeWassUp($geoloc, $gkey);
		   		if($geocode[0] != 200) {
		   			$lat = explode(":", $data[2]);
		   			$lat = $lat[1];
		      			$lon = explode(":", $data[3]);
		   			$lon = $lon[1];
		   		} else { 
		   			$lat = $geocode[2];
		   			$lon = $geocode[3];
		   		}
		   	}
		   	}
		   	$location = $data[0];
		   	if (!empty($data[1])) { 
				$location .= " - ".$data[1];
			}
		   	echo "<!-- heartbeat -->\n";
		   } //end if curl_init

	// Print the JS code to add marker on the map
	if ($wassup_settings['wassup_geoip_map'] == 1) {
		if ($lat!=0 && $lon!=0) {
		$item_id = $cv->id;
		$img_dir = WASSUPURL.'/img';
                echo "
                <script type=\"text/javascript\">
                var icon$item_id = new GIcon();
                icon$item_id.image = '".$img_dir."/".$map_icon."';
                icon$item_id.shadow = '$img_dir/shadow.png';
                icon$item_id.iconSize = new GSize(20.0, 34.0);
                icon$item_id.shadowSize = new GSize(38.0, 34.0);
                icon$item_id.iconAnchor = new GPoint(10.0, 17.0);
                icon$item_id.infoWindowAnchor = new GPoint(10.0, 17.0);
                var point = new GLatLng($lat,$lon);
                var marker$item_id = new GMarker(point, icon$item_id);
                map.addOverlay(marker$item_id);
                GEvent.addListener(marker$item_id, 'click', function() {
                marker$item_id.openInfoWindowHtml('<div style=\"white-space:nowrap\"><div class=\"bubble\">Ip: ".
			$ip[0]
			."<br />Hour: ".
			gmdate('H:i:s', $timestamp)
			."<br />Request: <a href=".
			wAddSiteurl($cv->urlrequested)
			." target=\"_BLANK\">".
			stringShortener($cv->urlrequested, round($max_char_len*.9,0))
			."</a><br />".
			"</div></div>');
                });
                map.panTo(new GLatLng($lat,$lon),3);
                </script>";
                } //end if $lat!=0
	} // end if wassup_geoip_map
?>
		<div class="sum-spy">
		<span class="<?php print $unclass; ?>">
		   	<?php print $ip[0]; ?></span>
		<div class="sum-det-spy"><span class="det1">
		<?php
			print '<a href="'.wAddSiteurl("{$cv->urlrequested}").'" target="_BLANK">';
			print stringShortener("{$cv->urlrequested}", round($max_char_len*.9,0)); ?>
		</a></span><br />
		<span class="det2"><strong><?php print gmdate("H:i:s", $timestamp); ?> - </strong>
		<?php 
		print $referrer;
		if (!empty($location)) {
			print "<br />".$location; 
		} ?>
		</span>
		</div>
		</div>
<?php
		} //end if row_count
		$row_count=$row_count+1;
		} //end foreach
	} else {
		//display "no activity" periodically so we know spy is running...
		if ((int)$to_date%59 == 0 ) {
			echo '<div class="sum-spy"><span class="det3">'.gmdate("H:i:s",$to_date).' - '.__("No visitor activity","wassup").' &nbsp; &nbsp; :-( &nbsp; </span></div>';
		}
	} //end if !empty($qryC)
} //end function spyview

// Geocoding location with Google Maps
function geocodeWassUp($location, $key) {
	//Three parts to the querystring: q is address, output is the format (
	$address = urlencode($location);
	$url = "http://maps.google.com/maps/geo?q=".$address."&output=csv&key=".$key;

	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER,0);
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	$data = curl_exec($ch);
	curl_close($ch);

	$data = explode(",",$data);
	if ($data[0] == 200) {
		return $data;

	} else {
		$error = $data[0];
		return $error;
	}
}

/* wGetStats- Return an associative array containing the top statistics 
 *  numbers of "stat_type" from wassup table. Associative array fields are 
 *  'top_count', 'top_item', and optionally, 'top_link', when data is url.
 *  Results are sorted in descending count order and known spam is 
 *  automatically excluded when spam check is enabled in 'Wassup Options'.
 * Input parameters are 'stat_type'=[name of any column in wassup table],
 * and 2 optional parameters: 
 *  stat_limit=N-- limits results to the top N values. Default=10.
 *  stat_condition='mysql where clause'-- usually a date range clause on 
 *  `timestamp`.  Defaults to 24 hours.
 * Used by action.php TopTen and wassup_widget to retrieve statistics data.
 * - Helene D. 2009-03-04
 */
function wGetStats($stat_type, $stat_limit=10, $stat_condition="") {
	global $wpdb, $debug_mode;

	$wassup_settings = get_option('wassup_settings');
	$top_ten = unserialize(html_entity_decode($wassup_settings['wassup_top10']));
	$wpurl =  get_bloginfo('wpurl');
	$blogurl =  get_bloginfo('home');
	$table_name = (!empty($wassup_settings['wassup_table']))? $wassup_settings['wassup_table'] : $wpdb->prefix . "wassup";
	$table_tmp_name = $table_name . "_tmp";

	if (empty($stat_limit) || !(is_numeric($stat_limit))) {
		$stat_limit=10;
	}
	if (empty($stat_condition)) {
		$to_date = wassup_get_time();
		$from_date = ((int)$to_date - 24*(60*60)); //24 hours
		$stat_condition = " `timestamp` >= $from_date ";
	}
	//exclude spam if it is being recorded
	if ($wassup_settings['wassup_spamcheck'] == 1) {
		$spam_condition = " AND spam=0";
	} else {
		$spam_condition = "";
	}
	$stat_condition .= $spam_condition; 

	//get the stats data
	//top search phrases...
	if ($stat_type == "searches") {
		$stat_results = $wpdb->get_results("SELECT count(search) AS top_count, search AS top_item, referrer AS top_link FROM $table_name WHERE $stat_condition AND search!='' AND spider='' GROUP BY search ORDER BY top_count DESC LIMIT $stat_limit");

	//top external referrers...
	} elseif ($stat_type == "referrers") {
		//exclude internal referrals
		$url = parse_url($blogurl);
		$sitedomain = $url['host'];
		$exclude_list = $sitedomain;
		if ($wpurl != $blogurl) {
			$url = parse_url($wpurl);
			$wpdomain = $url['host'];
			$exclude_list .= ",".$wpdomain;
		}
		//exclude external referrers
		if (!empty($top_ten['topreferrer_exclude'])) {
			$exclude_list .= ",".$top_ten['topreferrer_exclude'];
		}
		//create mysql conditional statement to exclude referrers
		$exclude_referrers = "";
		$exclude_array = array_unique(explode(",", $exclude_list));
		foreach ($exclude_array as $exclude_domain) {
			$exclude_domain = trim($exclude_domain);
			if ($exclude_domain != "" ) {
				$exclude_referrers .= " AND referrer NOT LIKE 'http://".$exclude_domain."%' AND referrer NOT LIKE 'http://www.".$exclude_domain."%'";
			}
		}
		$stat_results = $wpdb->get_results("SELECT count(*) AS top_count, LOWER(referrer) AS top_item, referrer AS top_link FROM $table_name WHERE $stat_condition AND referrer!='' AND search='' AND spider='' $exclude_referrers GROUP BY top_item ORDER BY top_count DESC LIMIT $stat_limit");

	//top url requests...
	} elseif ($stat_type == "urlrequested") {
		$stat_results = $wpdb->get_results("SELECT count(*) AS top_count, LOWER(REPLACE(urlrequested, '/', '')) AS top_group, LOWER(urlrequested) AS top_item, urlrequested AS top_link FROM $table_name WHERE $stat_condition GROUP BY top_group ORDER BY top_count DESC LIMIT $stat_limit");

	//top browser...
	} elseif ($stat_type == "browser") {
		$stat_results = $wpdb->get_results("SELECT count(*) AS top_count, SUBSTRING_INDEX(SUBSTRING_INDEX(browser, ' 0.', 1), '.', 1) AS top_item FROM $table_name WHERE $stat_condition AND `browser`!='' AND `browser` NOT LIKE 'N/A%' AND `spider`='' GROUP BY top_item ORDER BY top_count DESC LIMIT $stat_limit");

	//top os...
	} elseif ($stat_type == "os") {
		$stat_results = $wpdb->get_results("SELECT count(os) as top_count, `os` AS top_item FROM $table_name WHERE $stat_condition AND `os`!='' AND `os` NOT LIKE 'N/A%' AND spider='' GROUP BY top_item ORDER BY top_count DESC LIMIT $stat_limit");

	//top language/locale..
	} elseif ($stat_type == "language" || $stat_type == "locale") {
		$stat_results = $wpdb->get_results("SELECT count(LOWER(language)) as top_count, LOWER(language) as top_item FROM $table_name WHERE $stat_condition AND language!='' AND spider='' GROUP BY top_item ORDER BY top_count DESC LIMIT $stat_limit");

	} else {
		//TODO: check that wp_wassup.$stat_type column exist and is char
		if (!empty($stat_type)) {
			$stat_results = $wpdb->get_results("SELECT count($stat_type) AS top_count, `$stat_type` AS top_item FROM $table_name WHERE $stat_condition AND `$stat_type`!='' AND `$stat_type` NOT LIKE 'N/A%' GROUP BY `$stat_type` ORDER BY top_count DESC LIMIT $stat_limit");
		}
	}

	if (!empty($stat_results[0]->top_count)) {
		return $stat_results;
	} else { 
		return array();
	}
} //end function wGetStats

function backup_wassup($savefile="") {	//untested
	global $wpdb, $wassup_options;
	//# save to server file, $savefile - alternate method to export wassup table.
	//#   Useful when browser keeps timing out during export. -Helene D. 2009-03-16
	if (!empty($savefile)) {
		$savedir = dirname($savefile);
	} else {
		$savefile="wassup_backup.php";
		$savedir = "";
	}
	//use web root or home directory for backup, if full path not specified
	if ($savedir == "" || preg_match('#^/#',$savefile)==0) { 
		if (!empty($_ENV['DOCUMENT_ROOT'])) {
			$savedir = $_ENV['DOCUMENT_ROOT'];
			$savefile = rtrim($_ENV['DOCUMENT_ROOT'],'/').'/'.$savefile;
		} elseif (!empty($_ENV['HOME'])) {
			$savedir = $_ENV['HOME'];
			$savefile = rtrim($_ENV['HOME'],'/').'/'.$savefile;
		}
	}
	if (!$wassup_options->isWritableFolder($savedir)) {
		//unable to save to file
		$wassup_options->wassup_alert_message = "ERROR: Unable to Save file to $savedir";
		$wassup_option->saveOptions();
		return false;
	} else {
		$records = $wpdb->get_results("SELECT * FROM $table_name");
		$wassup_rec = array_map('stripslashes',$records);
		$fb = @fopen($savefile,'w');
		if (!$fb) { 
			$wassup_options->wassup_alert_message = "ERROR: Unable to backup to file, $savefile";
			$wassup_option->saveOptions();
			return false;
		} else { 
			$bytes=0;
			foreach ($records as $record) {
				$wassup_data=sprintf("INSERT INTO `wp_wassup` (`wassup_id`, `timestamp`, `ip`, `hostname`, `urlrequested`, `agent`, `referrer`, `search`, `searchpage`, `os`, `browser`, `language`, `screen_res`, `searchengine`, `spider`, `feed`, `username`, `comment_author`, `spam`) VALUES '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s'\n", mysql_real_escape_string($record->wassup_id),
			mysql_real_escape_string($record->timestamp),
			mysql_real_escape_string($record->ip),
			mysql_real_escape_string($record->hostname),
			mysql_real_escape_string($record->urlrequested),
			mysql_real_escape_string($record->agent),
			mysql_real_escape_string($record->referrer),
			mysql_real_escape_string($record->search),
			mysql_real_escape_string($record->searchpage),
			mysql_real_escape_string($record->os),
			mysql_real_escape_string($record->browser),
			mysql_real_escape_string($record->language),
			mysql_real_escape_string($record->screen_res),
			mysql_real_escape_string($record->searchengine),
			mysql_real_escape_string($record->spider),
			mysql_real_escape_string($record->feed),
			mysql_real_escape_string($record->username),
			mysql_real_escape_string($record->comment_author),
			mysql_real_escape_string($record->spam));

				$bytes += fwrite($fb, $wassup_data);
			} //end foreach
			fclose($fb);
			$wassup_options->wassup_alert_message = "$bytes Wassup data bytes saved file to $savefile";
			$wassup_option->saveOptions();
			return $bytes;
		} //end else fb
	} //end else wassup_options
} //end function

// How many digits have an integer
function digit_count($n, $base=10) {

  if($n == 0) return 1;

  if($base == 10) {
    # using the built-in log10(x)
    # might be more accurate than log(x)/log(10).
    return 1 + floor(log10(abs($n)));
  }else{
    # here  logB(x) = log(x)/log(B) will have to do.
   return 1 + floor(log(abs($n))/ log($base));
  }
}

//Round the integer to the next near 10
function roundup($value) {
	$dg = digit_count($value);
	if ($dg <= 2) {
		$dg = 1;
	} else {
		$dg = ($dg-2);
	}
	return (ceil(intval($value)/pow(10, $dg))*pow(10, $dg)+pow(10, $dg));
}

function chart_data($Wvisits, $pages=null, $atime=null, $type, $charttype=null, $axes=null, $chart_type=null) {
	global $debug_mode;
	$chartAPIdata = false;
// Port of JavaScript from http://code.google.com/apis/chart/
// http://james.cridland.net/code
   // First, find the maximum value from the values given
   if ($axes == 1) {
	$maxValue = roundup(max(array_merge($Wvisits, $pages)));
	//$maxValue = roundup(max($Wvisits));
	$halfValue = ($maxValue/2); 
	$maxPage = $maxValue;
   } else {
	$maxValue = roundup(max($Wvisits));
	$halfValue = ($maxValue/2);
	$maxPage = roundup(max($pages));
	$halfPage = ($maxPage/2);
   }

   // A list of encoding characters to help later, as per Google's example
   $simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

   $chartData = "s:";

	// Chart type has two datasets
	if ($charttype == "main") {
		$label_time = "";
		for ($i = 0; $i < count($Wvisits); $i++) {
			$currentValue = $Wvisits[$i];
			$currentTime = $atime[$i];
			if ($chart_type == "dashboard") {
				$label_time="|";
			} else {
				$label_time.=str_replace(' ', '+', $currentTime)."|";
			}
     
			if ($currentValue > -1) {
				$chartData.=substr($simpleEncoding,61*($currentValue/$maxValue),1);
			} else {
				$chartData.='_';
			}
		} 
		// Add pageviews line to the chart
		if (count($pages) != 0) {
			$chartData.=",";
			for ($i = 0; $i < count($pages); $i++) {
				$currentPage = $pages[$i];
				$currentTime = $atime[$i];
     
				if ($currentPage > -1) {
					$chartData.=substr($simpleEncoding,61*($currentPage/$maxPage),1);
				} else {
					$chartData.='_';
				}
			}
		}
		// Return the chart data - and let the Y axis to show the maximum value
   		if ($axes == 1) {
			$chartAPIdata=$chartData."&chxt=x,y&chxl=0:|".$label_time."1:|0|".$halfValue."|".$maxValue."&chxs=0,6b6b6b,9";
		} else {
			$chartAPIdata=$chartData."&chxt=x,y,r&chxl=0:|".$label_time."1:|0|".$halfValue."|".$maxValue."|2:|0|".$halfPage."|".$maxPage."&chxs=0,6b6b6b,9";
		}
	
	// Chart type has one one dataset
	// It's unused now
	} else {
		for ($i = 0; $i < count($Wvisits); $i++) {
			$currentValue = $Wvisits[$i];
			$currentTime = $atime[$i];
			$label_time.=str_replace(' ', '+', $currentTime)."|";

			if ($currentValue > -1) {
				$chartData.=substr($simpleEncoding,61*($currentValue/$maxValue),1);
			} else {
				$chartData.='_';
			}
		}
		$chartAPIdata=$chartData."&chxt=x,y&chxl=0:|".$label_time."|1:|0|".$halfValue."|".$maxValue."&chxs=0,6b6b6b,9";
	}
	return $chartAPIdata;

} //end function

// Used to show main visitors details query, to count items and to extract data for main chart
class MainItems {
	// declare variables
        var $tableName;
        var $from_date;
        var $to_date;
        var $searchString;
        var $whereis;
        var $ItemsType;
        var $Limit;
        var $Last;
	var $WpUrl;

	/* Constructor */
	function mainitems($table_name,$date_from,$date_to,$whereis=null,$limit=null) {
		global $wpdb;
		$this->tableName = $table_name;
		$this->from_date = $date_from;
		$this->to_date = $date_to;
		$this->whereis = $whereis;
		$this->limit = $limit;
	}
	/* Methods */
	// Function to show main query and count items
        function calc_tot($Type, $Search="", $specific_where_clause=null, $distinct_type=null) {
		global $wpdb, $wassup_options, $debug_mode;

                $this->ItemsType = $Type;
		$this->searchString = $Search;
		$ss = "";
		if (!empty($Search) || !empty($specific_where_clause)) {
			$ss = $this->buildSearch($Search,$specific_where_clause);
		}

		// Switch by every (global) items type (visits, pageviews, spams, etc...)
                switch ($Type) {
                        // This is the MAIN query to show the chronology
		case "main":
			//## Extend mysql wait timeout to 2.5 minutes and extend
			//#  php script timeout to 3 minutes to prevent script
			//#  hangs with large tables on slow server.
			if (!ini_get('safe_mode')) @set_time_limit(3*60);
			$results = $wpdb->query("SET wait_timeout = 160");

			//TODO: use a subquery for MySQL 5+
			//main query
			//  - retrieve one row per wassup_id with timestamp = max(timestamp) (ie. latest record)
			// "sql_buffer_result" select option helps in cases where it takes a long time to retrieve results.  -Helene D. 2/29/09
			$qry = sprintf("SELECT SQL_BUFFER_RESULT *, max(`timestamp`) as max_timestamp, count(wassup_id) as page_hits FROM %s WHERE `timestamp` >= %s %s %s GROUP BY wassup_id ORDER BY max_timestamp DESC %s",
				$this->tableName,
				$this->from_date, 
				$ss,
				$this->whereis,
				$this->Limit);
			$results = $wpdb->get_results($qry);
			//return $results;
			break;
		case "count":
			// These are the queries to count the items hits/pages/spam
			$qry = sprintf("SELECT COUNT(%s `wassup_id`) AS itemstot FROM %s WHERE `timestamp` >= %s %s %s",
					$distinct_type,
					$this->tableName,
					$this->from_date,
					$ss,
					$this->whereis);
			$results = $wpdb->get_var($qry);
			//$itemstot = $wpdb->get_var($qry);
			//return $itemstot;
			break;
		} //end switch
		if (!empty($results)) {
			return $results;
		} else {
			return false;
		}
	} //end function calc_tot

	// $Ctype = chart's type by time
	// $Res = resolution
	// $Search = string to add to where clause
        function TheChart($Ctype, $Res, $chart_height, $Search="", $axes_type, $chart_bg, $chart_type=null) {
		global $wpdb, $debug_mode;

		$mysqlversion=substr(mysql_get_server_info(),0,3);
		$this->searchString = $Search;
		$this->Last = $Ctype;

		//# MySql 'FROM_UNIXTIME' converts a UTC timestamp to a
		//#  local datetime based on the server host timezone(TZ).
		//#  Wassup's 'timestamp' is neither UTC nor server host
		//#  time. It is Wordpress time and must be converted to
		//#  UTC minus the TZ difference between the server host
		//#  and Wordpress time offset to get an accurate output 
		//#  from 'FROM_UNIXTIME'
		$WPoffset = (int)(get_option("gmt_offset")*60*60);
		$UTCoffset = $WPoffset + ((int)date('Z') - $WPoffset);
		//
		//#for US/Euro date display: USA Timezone=USA date format.
		//TODO: Use date format in Wordpress to determine x-axis format
		if (in_array(date('T'), array("ADT","AST","AKDT","AKST","CDT","CST","EDT","EST","HADT","HAST","MDT","MST","PDT","PST"))) { 
			$USAdate = true;
		} else {
			$USAdate = false;
		}
		// Options by chart type
		switch ($Ctype) {
		case .4:
			$label = __("Last 6 Hours", "wassup");
			$strto = "6 hours";
			$Ctimeformat = "%H";
			$x_axes_label = "%H:00";
			break;
		case 7:
			$label = __("Last 7 Days", "wassup");
			$strto = "7 days";
			$Ctimeformat = "%d";
			if ($USAdate) { $x_axes_label = "%a %b %d"; }
			else { $x_axes_label = "%a %d %b"; }
			break;
		case 30:
			$label = __("Last Month", "wassup");
			$strto = "30 days";
			$Ctimeformat = "%d";
			if ($USAdate) { $x_axes_label = " %b %d"; }
			else { $x_axes_label = "%d %b"; }
			break;
		case 90:
			$label = __("Last 3 Months", "wassup");
			$strto = "3 months";
			$Ctimeformat = "%u";
			$x_axes_label = "%b, wk-%u";
			break;
		case 180:
			$label = __("Last 6 Months", "wassup");
			$strto = "6 months";
			$Ctimeformat = "%m";
			$x_axes_label = " %b %Y";
			break;
		case 365:
			$label = __("Last Year", "wassup");
			$strto = "12 months";
			$Ctimeformat = "%m";
			$x_axes_label = "%b %Y";
			break;
		case 0:
			$label = __("All Time", "wassup");
			$strto = "";
			$Ctimeformat = "%m";
			$x_axes_label = "%b %Y";
			break;
		case 1:
		default:
			$label = __("Last 24 Hours", "wassup");
			$strto = "24 hours";
			$Ctimeformat = "%H";
			$x_axes_label = "%H:00";
		}

		// Add Search variable to WHERE clause
		$ss = $this->buildSearch($Search);

                $hour_todate = $this->to_date;
		if ($strto != "") {
                	$hour_fromdate = strtotime("-".$strto, $hour_todate);
		} else {
                	$hour_fromdate = 0;
		}
		//if ($hour_fromdate == "") $hour_fromdate = strtotime("-24 hours", $hour_todate);

		$qry = sprintf("SELECT COUNT(DISTINCT `wassup_id`) as items, COUNT(`wassup_id`) as pages, DATE_FORMAT(FROM_UNIXTIME(CAST((`timestamp` - %s) AS UNSIGNED)), '%s') as thedate FROM %s WHERE `timestamp` > %s %s %s GROUP BY DATE_FORMAT(FROM_UNIXTIME(CAST((`timestamp` - %s) AS UNSIGNED)), '%s') ORDER BY `timestamp`",
			$UTCoffset,
			$x_axes_label,
			$this->tableName,
			$hour_fromdate, 
			$this->whereis,
			$ss, 
			$UTCoffset,
			$Ctimeformat);
                $aitems = $wpdb->get_results($qry,ARRAY_A);
		// Extract arrays for Visits, Pages and X_Axis_Label
		if (count($aitems) > 0) {
			foreach ($aitems as $bhits) {
                		$ahits[] = $bhits['items'];
	                	$apages[] = $bhits['pages'];
				$atime[] = $bhits['thedate'];
                	}
			// return the url to the google chart image 
			$chart_url ="http://chart.apis.google.com/chart?chf=".$chart_bg."&chtt=".urlencode($label)."&chls=4,1,0|2,6,2&chco=2683ae,FF6D06&chm=B,2683ae30,0,0,0&chg=10,20,2,5&cht=lc&chs=".$Res."x".$chart_height."&chd=".chart_data($ahits, $apages, $atime, $Ctimeformat, "main", $axes_type, $chart_type);
		}
		if (!empty($chart_url)) {
			return $chart_url;
		} else {
			return false;
		}
	} //end function theChart

	//  buildSearch() added to protect against Sql injection code 
	//  in user-input parameter: "Search".  -Helene D. 2/27/09
	function buildSearch($Search,$specific_where_clause=null) {
		global $wpdb;
		$ss="";
		//create the Search portion of a MySql WHERE clause 
		if (!empty($Search)) {
			//escape chars that have special meaning in mysql 'like' [%\]
			if (function_exists('like_escape')) {	//WP 2.5+ function
                		$searchString = like_escape(trim($Search));
			} else {
				$searchString = str_replace(array("%", "_"), array("\\%", "\\_"), trim($Search));
			}
			$searchParam = mysql_real_escape_string($searchString);

			// Create the Search portion of MySQL WHERE clause
			$ss = sprintf(" AND (`ip` LIKE '%%%s%%' OR `hostname` LIKE '%%%s%%' OR `urlrequested` LIKE '%%%s%%' OR `agent` LIKE '%%%s%%' OR `referrer` LIKE '%%%s%%' OR `username` LIKE '%s%%' OR `comment_author` LIKE '%s%%')",
				$searchParam,
				$searchParam,
				$searchParam,
				$searchParam,
				$searchParam,
				$searchParam,
				$searchParam);
		} //if $Search
		if (!empty($specific_where_clause)) {
			$ss .= " ".trim($specific_where_clause);
		}
		return $ss;
	} //end function buildSearch

} //end class mainItems

// Class to check if a previous comment with a specific IP was detected as SPAM by Akismet default plugin
class CheckComment {
        var $tablePrefix;

	function isSpammer ($authorIP) {
		global $wpdb;
	        $spam_comment = $wpdb->get_var("SELECT COUNT(comment_ID) AS spam_comment FROM ".$this->tablePrefix."comments WHERE comment_author_IP='$authorIP' AND comment_approved='spam'");
		return $spam_comment;
	}
}
Back to Top