|
| 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 | +?> |
0 commit comments