Skip to content

Commit db98c23

Browse files
committed
Merge branch 'php'
2 parents 0cfe290 + 8d8d8b7 commit db98c23

File tree

6 files changed

+356
-0
lines changed

6 files changed

+356
-0
lines changed

customization/www_content/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
</script>
8787
</head>
8888
<body>
89+
<img src="../vc_counter.php" />
8990
<div id="page">
9091
<!--====================================Navigation================================================================-->
9192
<nav id="top_nav">
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
<?php
3+
4+
/********************************
5+
6+
HTML Generation function for PHP-Download-Counter Statistics
7+
8+
will be called by vc_display.php with an
9+
10+
11+
$resutlt = array (
12+
array ( 'url' => "/Shared/......" , "counter" => 4 ),
13+
array ( 'url' => "/Shared/......" , "counter" => 5 ),
14+
)
15+
16+
17+
(optional) $arguments = array (
18+
....
19+
);
20+
21+
The HTML is separated in that file, to enable easy exchangable looks
22+
23+
*******************************/
24+
25+
26+
function print_header () {
27+
28+
29+
echo <<<EOD
30+
31+
<html>
32+
<head><title>Download-Statistics</title></head>
33+
<body>
34+
EOD;
35+
36+
37+
}
38+
39+
function print_table_head() {
40+
41+
echo <<<EOD
42+
<table>
43+
<tr><th></th><th><a href="vc_display.php?sortBy=day">Day:</a></th><th><a href="vc_display.php?sortBy=counter&sortOrder=DESC">Visitors:</th></tr>
44+
EOD;
45+
}
46+
47+
function print_table_line($no , $day = "" , $count = 0) {
48+
49+
echo <<<EOD
50+
<tr><td></td><td>$day</td><td>$count</td></tr>
51+
EOD;
52+
53+
}
54+
55+
function print_footer() {
56+
57+
echo <<<EOD
58+
</table>
59+
</body>
60+
</html>
61+
EOD;
62+
63+
}
64+
65+
function output_html ( $result = array () , $arguments = array () ) {
66+
67+
print_header();
68+
print_table_head();
69+
70+
71+
foreach ( $result as $no => $line ) {
72+
print_table_line ( $no, $line['day'], $line['counter'] );
73+
74+
}
75+
76+
print_footer();
77+
}
78+
?>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/******** Config File for download statistics *****/
4+
5+
// Everything must be inside the function, because within the dirlisting
6+
// the globals term does not work correct :(
7+
8+
function vc_get_config () {
9+
10+
$config = array (
11+
12+
"SQLITE_FILE" => "sqlite:/opt/piratebox/share/vc_statistics.sqlite",
13+
"HTML_TEMPLATE_FILE" => "content/vc_statistics.html.php" ,
14+
"sortBy" => "day" ,
15+
"sortOrder" => 'DESC' , # ASC, DESC
16+
"top_max" => "5", #Display top n on option "top"
17+
"output_type" => "html" , # Display HTML per default or only JSON
18+
"list_type" => "top" , #Display "all" or only "top" on default
19+
20+
);
21+
return $config ;
22+
}
23+
24+
global $config;
25+
26+
?>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
require_once "vc.conf.php";
4+
5+
6+
function vc_do_db_connect() {
7+
$config = vc_get_config();
8+
if ( ! $db = new PDO ( $config['SQLITE_FILE'] ) ) {
9+
print_r ( $db->errorInfo() ) ;
10+
die ( "Error, couldn't open database " );
11+
}
12+
13+
$sth = $db->prepare ( 'CREATE TABLE IF NOT EXISTS vc_statistics ( day text, visitor text, PRIMARY KEY ( day , visitor ) ON CONFLICT IGNORE )');
14+
if ( !$sth ) {
15+
print_r ( $db->errorInfo());
16+
die ( "statement error" );
17+
}
18+
19+
if ( ! $sth->execute () )
20+
die ( "Error creating table: ". $sth->errorInfo ());
21+
22+
return $db;
23+
}
24+
25+
26+
function vc_save_visitor ( $visitor_key , $debug = false ) {
27+
if ( !$debug ) {
28+
error_reporting(0);
29+
}
30+
31+
if ( date ( 'Y') < 2013 ) {
32+
if ( $debug ) print "not saving because system-year less then 2013";
33+
return false;
34+
}
35+
$db= vc_do_db_connect();
36+
37+
$sth = $db->prepare ( 'INSERT OR IGNORE INTO vc_statistics ( day , visitor ) VALUES ( :day , :visitor )');
38+
39+
if ( !$sth ) {
40+
print_r ( $db->errorInfo());
41+
die ( "statement error" );
42+
}
43+
44+
45+
$sth->bindParam ( ':day' , date ( 'Y-m-d' ) );
46+
$sth->bindParam ( ':visitor', $visitor_key );
47+
48+
if ( ! $sth->execute () ) {
49+
if ( $debug )
50+
print_r ( $sth->errorInfo() );
51+
}
52+
53+
}
54+
55+
56+
57+
function vc_read_stat_sum_per_day_only ( $day="%" ) {
58+
$config = dl_get_config();
59+
return dl_read_stat_sum_per_day ($day , $config["sortBy"] , $config["sortOrder"] , "all" , $config["top_max"] );
60+
61+
}
62+
63+
function vc_read_stat_sum_per_day ($path="%" , $sortBy , $sort, $type="all" , $limit ) {
64+
65+
$config = vc_get_config();
66+
if ( ! isset ( $sortBy ) )
67+
$sortBy=$config["sortBy"];
68+
69+
if ( ! isset ( $sort ) )
70+
$sort=$config["sortOrder"];
71+
72+
if ( ! isset ( $limit ))
73+
$limit=$config["top_max"];
74+
75+
76+
$db = vc_do_db_connect();
77+
78+
if ( $type == "all" ) {
79+
$sth = $db->prepare ( " SELECT day, count( visitor) as counter FROM vc_statistics WHERE day LIKE :day GROUP BY day ORDER by $sortBy $sort ");
80+
} elseif ( $type == "top" ) {
81+
$sth = $db->prepare ( " SELECT day, count(visitor) as counter FROM vc_statistics WHERE day LIKE :day GROUP BY day ORDER by $sortBy $sort LIMIT 0 , :max ");
82+
$sth->bindParam (':max' , $limit, PDO::PARAM_INT );
83+
}
84+
85+
if ( $sth ) {
86+
$generic_day = "";
87+
if ( $day == "%" ) {
88+
$generic_day = "%" ;
89+
} else {
90+
$generic_day = $day.'%' ;
91+
}
92+
$sth->bindParam ( ':day' , $generic_day );
93+
if ( ! $sth->execute() ) {
94+
print_r ( $sth->errorInfo() );
95+
die ( "Error executing statement ");
96+
}
97+
$result = $sth->fetchAll();
98+
# Tidy array up, I only want named keys
99+
foreach ( $result as &$line ) {
100+
unset ( $line[0] );
101+
unset ( $line[1] );
102+
}
103+
return $result;
104+
105+
} else {
106+
print_r ($db->errorInfo());
107+
die ("\n no valid statement could be found");
108+
}
109+
110+
}
111+
112+
?>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
$debug = false;
4+
5+
if ( ! isset ($_GET['debug']) ) {
6+
7+
Header("Content-type: image/gif");
8+
Header("Expires: Wed, 11 Nov 1998 11:11:11 GMT");
9+
Header("Cache-Control: no-cache");
10+
Header("Cache-Control: must-revalidate");
11+
12+
// This prints the raw 1 pixel gif to the browser
13+
// make sure this is one long line!
14+
15+
printf ("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",71,73,70,56,57,97,1,0,1,0,128,255,0,192,192,192,0,0,0,33,249,4,1,0,0,0,0,44,0,0,0,0,1,0,1,0,0,2,2,68,1,0,59);
16+
17+
} else {
18+
$debug = true;
19+
}
20+
21+
22+
$date = date ( 'Y-m-d' );
23+
24+
#Encrypt combination of IP + Date + Useragend. So one user only have a specific string per day
25+
$client_string = $_ENV['REMOTE_ADDR'] . $_ENV['HTTP_USER_AGENT'] . $date ;
26+
27+
$sha = sha1($client_string);
28+
29+
include ("vc.func.php");
30+
31+
vc_save_visitor ( $sha , $debug );
32+
33+
34+
?>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/******* Display Visitor - statistics *****
4+
5+
VERSION 0.1 - Matthias Strubel (c) 2013 - GPL3
6+
7+
Very simple script to get access to the statistic data.
8+
9+
Following GET-Options are possible:
10+
11+
sortOrder = ASC / DESC - Ascendening or decsending sort order
12+
sortBy =
13+
list_type = "all" display all data ; "top" - limit display with top n entries
14+
top_max = Limit entry list in "top" mode by that value
15+
output_type = none or html resulsts in a simple html output
16+
"json" results in a json structure
17+
18+
The HTML output is based on a file pointed in "vc.conf.php" to.
19+
That file lays on librarybox in the content folder
20+
http://librarybox.us/content/....
21+
which is in reality on the USB stick. That file can simply exchanged without the need
22+
of touching the logic behind.
23+
24+
Currently I don't have the path filter programmed in that. script
25+
26+
27+
CHANGELOG:
28+
0.1 RELEASE
29+
30+
********************************************/
31+
32+
require_once "vc.conf.php";
33+
include "vc.func.php";
34+
35+
$config=vc_get_config();
36+
37+
$sort=$config["sortOrder"];
38+
$sortBy=$config["sortBy"];
39+
$top_max=$config["top_max"];
40+
$output_type=$config["output_type"];
41+
$list_type=$config["list_type"];
42+
43+
if ( isset ($_GET['sortOrder'] )) {
44+
if ( $_GET['sortOrder'] == 'ASC' ) {
45+
$sort='ASC';
46+
} else {
47+
$sort='DESC';
48+
}
49+
}
50+
51+
if ( isset ($_GET['sortBy']) ) {
52+
if ( $_GET["sortBy"] == "url" ) {
53+
$sortBy="url";
54+
} elseif ( $_GET["sortBy"] == "counter" ) {
55+
$sortBy="counter";
56+
}
57+
}
58+
59+
if ( isset ($_GET['top_max'] )) {
60+
$top_max = $_GET['top_max'];
61+
}
62+
63+
64+
if ( isset ($_GET['output_type'] )) {
65+
if ( $_GET["output_type"] == "json" ) {
66+
$output_type= "json";
67+
} elseif ( $_GET["output_type"] == "html" ) {
68+
$output_type="html";
69+
}
70+
}
71+
72+
73+
if ( isset ( $_GET['list_type'] )) {
74+
$list_type= $_GET['list_type'];
75+
}
76+
77+
#----------------------------------
78+
# Detect which statement
79+
80+
81+
$result= vc_read_stat_sum_per_day ( '%' , $sortBy , $sort , $list_type , $top_max );
82+
83+
#------------------------------------------------
84+
# Output
85+
86+
if ( is_array ( $result ) ) {
87+
if ( $output_type == "html" ) {
88+
# Template file for HTML output
89+
include $config["HTML_TEMPLATE_FILE"];
90+
output_html ( $result, array (
91+
'list_type' => $list_type,
92+
'top_max' => $top_max ,
93+
"sortBy" => $sortBy ,
94+
"sortOrder" => $sort,
95+
"filter_path" => false ,
96+
"script_url" => $_ENV['REQUEST_URI'],
97+
));
98+
} elseif ( $output_type == "json" ) {
99+
header('Content-Type: application/json');
100+
print json_encode ( $result );
101+
}
102+
103+
}
104+
105+
?>

0 commit comments

Comments
 (0)