From b6b7adcbdb382f842f128fbe8f978ff9327d0a7f Mon Sep 17 00:00:00 2001 From: Roy Bekken Date: Wed, 16 Sep 2015 18:13:50 +0200 Subject: [PATCH] GCS: Add backend for usagetracker. Collect statistics about the usage of our software and hardware to enable us to make things better for you. --- ground/usagetracker/backend/usagetracker.php | 81 ++++++++++++++++++++ ground/usagetracker/backend/usagetracker.sql | 62 +++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 ground/usagetracker/backend/usagetracker.php create mode 100644 ground/usagetracker/backend/usagetracker.sql diff --git a/ground/usagetracker/backend/usagetracker.php b/ground/usagetracker/backend/usagetracker.php new file mode 100644 index 000000000..99389bc8f --- /dev/null +++ b/ground/usagetracker/backend/usagetracker.php @@ -0,0 +1,81 @@ +connect_error) { + die(); +} + +$sql = sprintf("SELECT id, last_date FROM usagetracker WHERE hash = '%s' LIMIT 1", + $db->real_escape_string($hash) +); + +$res = $db->query($sql); +if ($res->num_rows > 0) { + // Shouldn't normally be here but happens if GCS settings are reset + // or if the request come from another source than GCS. + + $hashUpdate = $res->fetch_assoc(); + if ($hashUpdate['last_date'] < (time() - 3600)) { + // Update timestamp and connection count. + $sql = sprintf("UPDATE usagetracker SET last_date = %u, count = count + 1 WHERE id = %u LIMIT 1", + time(), + $hashUpdate['id'] + ); + $db->query($sql); + } +} +else { + // New hash + $sql = sprintf("INSERT INTO usagetracker (first_date, last_date, ip, data, hash) + VALUES (%u, %u, '%s', '%s', '%s')", + time(), + time(), + encode_ip($_SERVER['REMOTE_ADDR']), + $db->real_escape_string($string), + $db->real_escape_string($_GET['hash']) + ); + + $db->query($sql); +} + +$db->close(); + + +function usage_error() +{ + //ob_start(); + + header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); + header("Status: 404 Not Found"); + + // Matching server 404 output can be added. + exit(); +} + +function encode_ip($dotquad_ip) +{ + $ip_sep = explode('.', $dotquad_ip); + return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]); +} + +?> diff --git a/ground/usagetracker/backend/usagetracker.sql b/ground/usagetracker/backend/usagetracker.sql new file mode 100644 index 000000000..aea215ed3 --- /dev/null +++ b/ground/usagetracker/backend/usagetracker.sql @@ -0,0 +1,62 @@ +-- phpMyAdmin SQL Dump +-- version 4.4.14.1 +-- http://www.phpmyadmin.net +-- +-- Host: localhost +-- Generation Time: Sep 13, 2015 at 03:06 PM +-- Server version: 10.0.20-MariaDB +-- PHP Version: 5.6.13 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + +-- +-- Database: `usagetracker` +-- + +-- -------------------------------------------------------- + +-- +-- Table structure for table `usagetracker` +-- + +CREATE TABLE IF NOT EXISTS `usagetracker` ( + `id` int(11) unsigned NOT NULL, + `first_date` int(11) unsigned NOT NULL, + `last_date` int(11) unsigned NOT NULL, + `ip` varchar(8) COLLATE utf8_bin NOT NULL, + `count` smallint(4) unsigned NOT NULL DEFAULT '1', + `data` varchar(2000) COLLATE utf8_bin NOT NULL, + `hash` varchar(32) COLLATE utf8_bin NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; + +-- +-- Indexes for dumped tables +-- + +-- +-- Indexes for table `usagetracker` +-- +ALTER TABLE `usagetracker` + ADD PRIMARY KEY (`id`), + ADD KEY `last_date` (`last_date`), + ADD KEY `hash` (`hash`); + +-- +-- AUTO_INCREMENT for dumped tables +-- + +-- +-- AUTO_INCREMENT for table `usagetracker` +-- +ALTER TABLE `usagetracker` + MODIFY `id` int(11) unsigned NOT NULL AUTO_INCREMENT; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;