mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2024-11-28 06:24:10 +01:00
GCS: Add backend for usagetracker.
Collect statistics about the usage of our software and hardware to enable us to make things better for you.
This commit is contained in:
parent
26289e63ae
commit
b6b7adcbdb
81
ground/usagetracker/backend/usagetracker.php
Normal file
81
ground/usagetracker/backend/usagetracker.php
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
if (!$_SERVER['REQUEST_METHOD'] === 'GET' || empty($_GET['hash'])) {
|
||||||
|
usage_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: $hash not sanitized because of the checksum match test.
|
||||||
|
$hash = $_GET['hash'];
|
||||||
|
$string = str_replace('&hash=' . $hash, '', rawurldecode($_SERVER['QUERY_STRING']));
|
||||||
|
|
||||||
|
if ($hash != md5($string)) {
|
||||||
|
usage_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$dbhost = 'localhost';
|
||||||
|
$dbname = '';
|
||||||
|
$dbpasswd = '';
|
||||||
|
$dbuser = '';
|
||||||
|
|
||||||
|
$db = new mysqli($dbhost, $dbuser, $dbpasswd, $dbname);
|
||||||
|
unset($dbhost, $dbuser, $dbpasswd, $dbname);
|
||||||
|
|
||||||
|
if ($db->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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
62
ground/usagetracker/backend/usagetracker.sql
Normal file
62
ground/usagetracker/backend/usagetracker.sql
Normal file
@ -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 */;
|
Loading…
Reference in New Issue
Block a user