Explorar o código

Statistics system

seavenois %!s(int64=10) %!d(string=hai) anos
pai
achega
9a5a72f08f

+ 21 - 1
sql/setup.sql

@@ -110,7 +110,27 @@ CREATE TABLE sponsor (
 	link			VARCHAR(300),
 	lat				DOUBLE,
 	lon				DOUBLE,
-	ammount			INT
+	ammount			INT,
+	print 			INT				DEFAULT 0,
+	print_static	INT				DEFAULT 0
+);
+
+DROP TABLE IF EXISTS stat_visit;
+CREATE TABLE stat_visit (
+	id		INT				NOT NULL	AUTO_INCREMENT	PRIMARY KEY,
+	ip		VARCHAR(50),
+	uagent	VARCHAR(400),
+	os		VARCHAR(150),
+	browser	VARCHAR(150)
+);
+
+DROP TABLE IF EXISTS stat_view;
+CREATE TABLE stat_view (
+	id		INT				NOT NULL	AUTO_INCREMENT	PRIMARY KEY,
+	visit	INT				NOT NULL	REFERENCES stat_visit.id,
+	section	VARCHAR(80)		NOT NULL,
+	entry	VARCHAR(100)	NOT NULL,
+	dtime	TIMESTAMP		NOT NULL	DEFAULT now()
 );
 
 DROP TABLE IF EXISTS photo;

+ 5 - 1
www/actividades/actividad.php

@@ -222,6 +222,10 @@
 				</div>
 			</div>
 		</div>
-		<?php include("../footer.php"); ad($con, $lang, $lng); ?>
+		<?php
+			include("../footer.php");
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "actividades", "$id");
+		?>
 	</body>
 </html>

+ 5 - 1
www/actividades/index.php

@@ -178,6 +178,10 @@
 			}
 		?>
 		</div>
-		<?php include("../footer.php"); ad($con, $lang, $lng); ?>
+		<?php
+			include("../footer.php");
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "actividades", "$r_activity[id]");
+		?>
 	</body>
 </html>

+ 5 - 1
www/ayuda/index.php

@@ -138,6 +138,10 @@
 				</div>
 			</div>
 		</div>
-		<?php include("../footer.php"); ad($con, $lang, $lng); ?>
+		<?php
+			include("../footer.php");
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "ayuda", "");
+		?>
 	</body>
 </html>

+ 5 - 1
www/blog/buscar.php

@@ -154,6 +154,10 @@
 			</div>
 			<?php include("common/rightpanel.php"); ?>
 		</div>
-		<?php include("../footer.php"); ad($con, $lang, $lng); ?>
+		<?php
+			include("../footer.php");
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "blog", "");
+		?>
 	</body>
 </html>

+ 5 - 1
www/blog/index.php

@@ -157,6 +157,10 @@
 			</div>
 			<?php include("common/rightpanel.php"); ?>
 		</div>
-		<?php include("../footer.php"); ad($con, $lang, $lng); ?>
+		<?php
+			include("../footer.php");
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "blog", "");
+		?>
 	</body>
 </html>

+ 2 - 1
www/blog/post.php

@@ -204,7 +204,8 @@
 		</div>
 		<?php
 			include("../footer.php");
-			mysqli_close($con);
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "blog", "$id");
 		?>
 	</body>
 </html>

+ 4 - 1
www/error/404.php

@@ -110,6 +110,9 @@
 				</ul>
 			</div>
 		</div>
-		<?php include("../footer.php"); ?>
+		<?php
+			include("../footer.php");
+			stats(-1, $ad_static, "error", "404");
+		?>
 	</body>
 </html>

+ 5 - 1
www/footer.php

@@ -15,11 +15,15 @@
 			<div class="td" id="footer_center">
 				<br class="mobile"/><br class="mobile"/>
 				<?php
-					$q = mysqli_query($con, "SELECT name_$lang AS name, image, link FROM sponsor WHERE image != '' ORDER BY ammount DESC;");
+					$q = mysqli_query($con, "SELECT id, name_$lang AS name, image, link FROM sponsor WHERE image != '' ORDER BY ammount DESC;");
+					$ad_static = Array();
 					if (mysqli_num_rows($q) > 0){
 						echo("<span class='desktop'>$lng[footer_sponsors]</span></br class='desktop'>\n");
 						$idx = 0;
 						while ($r = mysqli_fetch_array($q)){
+							
+							array_push($ad_static, $r['id']);
+						
 							if ($idx == 0){
 								echo("<a target='_blank' href='$r[link]'><span class='desktop'>$r[name]</span><img src='/img/spo/miniature/$r[image]'/></a>");
 							}

+ 89 - 1
www/functions.php

@@ -287,6 +287,7 @@
 	}
 	
 	function ad($con, $lang, $lng){
+	$id = -1;
 		//If the user hasn't still see an add on this session
 		if (isset($_SESSION['ad']) == false){
 			//25% chance of seeing an add
@@ -294,7 +295,7 @@
 				//Create an array with weighted values
 				$q = mysqli_query($con, "SELECT id, round(ammount/10) AS value FROM sponsor;");
 				if (mysqli_num_rows($q) == 0){
-					return;
+					return $id;
 				}
 				$total = 0;
 				$sponsors = array();
@@ -336,6 +337,93 @@
 			}
 			
 		}
+		return $id;
+	}
+	
+	// Function to get visitor ip
+	function getUserIP(){
+		$client  = @$_SERVER['HTTP_CLIENT_IP'];
+		$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
+		$remote  = $_SERVER['REMOTE_ADDR'];
+		if(filter_var($client, FILTER_VALIDATE_IP)){
+			$ip = $client;
+		}
+		elseif(filter_var($forward, FILTER_VALIDATE_IP)){
+			$ip = $forward;
+		}
+		else{
+			$ip = $remote;
+		}
+		return $ip;
+	}
+	
+	function stats($ad, $ad_static, $section, $id){
+		
+		// Get client data
+		$ip = getUserIP();
+		$browser_data = get_browser(null, true);
+		$os = $browser_data['platform'];
+		$browser = $browser_data['browser'];
+		$uagent = $browser_data['browser_name_pattern'];
+		//If bot, do nothing
+		$bot_kw = Array();
+		$bot_kw[0] = 'bot';
+		$bot_kw[1] = 'spider';
+		$bot_kw[2] = 'crawl';
+		$bot_kw[3] = '.com';
+		$bot_kw[4] = '.ru';
+		$bot_kw[5] = 'baidu';
+		$bot_kw[6] = 'survey';
+		$bot_kw[7] = 'scan';
+		$bot_kw[8] = 'feed';
+		$bot_kw[9] = 'bing';
+		$bot_kw[10] = 'yahoo';
+		$bot_kw[11] = 'engine';
+		$bot_kw[12] = 'preview';
+		$bot_kw[13] = 'checker';
+		$bot_kw[14] = 'catalog';
+		$bot_kw[15] = 'accelerator';
+		$bot_kw[16] = 'python';
+		$bot_kw[14] = 'qt';
+		$bot_kw[15] = 'webdav';
+		$bot_kw[16] = 'http';
+		$bot_kw[17] = 'url';
+		$bot_kw[18] = 'fake';
+		$bot_kw[19] = 'library';
+		$bot_kw[20] = 'commerce';
+		$bot_kw[21] = 'html';
+		$bot_kw[22] = 'fetch';
+		
+		$i = 1;
+		while ($i < sizeof($bot_kw)) {
+			if (strpos(strtolower($browser), $bot_kw[$i]) !== false){
+				mysqli_close($con);
+				return;
+			}
+			$i ++;
+		}
+		
+		//Look for a visit with the same IP in the last 30 mins.
+		$con = startdb('rw');
+		$q = mysqli_query($con, "SELECT stat_visit.id AS visitid FROM stat_view, stat_visit WHERE visit = stat_visit.id AND dtime > DATE_SUB(now(), INTERVAL 30 MINUTE) AND ip = '$ip' AND uagent = '$uagent';");
+		if (mysqli_num_rows($q) == 0){
+			mysqli_query($con, "INSERT INTO stat_visit (ip, uagent, os, browser) VALUES ('$ip', '$uagent', '$os', '$browser');");
+			$q = mysqli_query($con, "SELECT stat_visit.id AS visitid FROM stat_visit WHERE ip = '$ip' AND uagent = '$uagent' ORDER BY stat_visit.id DESC LIMIT 1;");
+		}
+		$r = mysqli_fetch_array($q);
+		$visit = $r['visitid'];
+		mysqli_query($con, "INSERT INTO stat_view (visit, section, entry) VALUES ($visit, '$section', '$id');");
+		
+		//Increase pop ad advertiser count
+		if ($ad > 0){
+			mysqli_query($con, "UPDATE sponsor SET print = print + 1 WHERE id = $ad;");
+		}
+		
+		if (is_array($ad_static)){
+			foreach ($ad_static as &$sponsor) {
+				mysqli_query($con, "UPDATE sponsor SET print_static = print_static + 1 WHERE id = $sponsor;");
+			}
+		}
 	}
 ?>
 	

+ 6 - 1
www/galeria/album.php

@@ -19,6 +19,7 @@
 	}
 	else{
 		$r = mysqli_fetch_array($q);
+		$id = $r['id'];
 ?>
 <!DOCTYPE html>
 <html>
@@ -105,7 +106,11 @@
 		</div>
 		<div id="photo_viewer" class="section">
 		</div>
-		<?php include("../footer.php"); ad($con, $lang, $lng); ?>
+		<?php
+			include("../footer.php");
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "galeria", "$id");
+		?>
 	</body>
 </html>
 

+ 5 - 1
www/galeria/index.php

@@ -100,6 +100,10 @@
 				?>
 			</div>
 		</div>
-		<?php include("../footer.php"); ad($con, $lang, $lng); ?>
+		<?php
+			include("../footer.php");
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "galeria", "");
+		?>
 	</body>
 </html>

+ 2 - 0
www/galeria/load_photo.php

@@ -78,5 +78,7 @@
 		echo "</div>\n"; //Cell
 		echo "</div>\n"; //Row
 		echo "</div>\n"; //Table
+		
+		stats(-1, 0, "photo", "$r[id]");
 	}
 ?>

+ 5 - 1
www/index.php

@@ -400,6 +400,10 @@
 				}
 			?>
 		</div>
-		<?php include("footer.php"); ad($con, $lang, $lng); ?>
+		<?php
+			include("footer.php");
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "index", "");
+		?>
 	</body>
 </html>

+ 5 - 1
www/lablanca/index.php

@@ -143,6 +143,10 @@
 				}
 			?>
 		</div>
-		<?php include("../footer.php"); ad($con, $lang, $lng); ?>
+		<?php
+			include("../footer.php");
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "fiestas", "");
+		?>
 	</body>
 </html>

+ 5 - 1
www/nosotros/index.php

@@ -105,6 +105,10 @@
 				</div>
 			</div>
 		</div>
-		<?php include("../footer.php"); ad($con, $lang, $lng); ?>
+		<?php
+			include("../footer.php");
+			$ad = ad($con, $lang, $lng); 
+			stats($ad, $ad_static, "nosotros", "");
+		?>
 	</body>
 </html>