Add basic analytics capture (issue #8)

master
Skylar Ittner 6 years ago
parent 0e2bd52aa0
commit 8d8982cfce

3
.gitignore vendored

@ -2,4 +2,5 @@ vendor
settings.php
nbproject/private
*.sync-conflict*
*.bak
*.bak
GeoLite2-City.mmdb

@ -4,7 +4,8 @@
"type": "project",
"require": {
"catfan/medoo": "^1.5",
"guzzlehttp/guzzle": "^6.2"
"guzzlehttp/guzzle": "^6.2",
"geoip2/geoip2": "~2.0"
},
"license": "MPL-2.0",
"authors": [

214
composer.lock generated

@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "5c7439c6e041764f2f6b0270a95ab3ae",
"content-hash": "e4e700119f47d2f68b0ed82abaf8c5c6",
"hash": "a05b32c4ba7d59c1fa2dc87ef59b238d",
"content-hash": "2b47ec7e64412178507c9e9362debc56",
"packages": [
{
"name": "catfan/medoo",
@ -66,6 +66,114 @@
],
"time": "2018-03-26 17:54:24"
},
{
"name": "composer/ca-bundle",
"version": "1.1.1",
"source": {
"type": "git",
"url": "https://github.com/composer/ca-bundle.git",
"reference": "d2c0a83b7533d6912e8d516756ebd34f893e9169"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/composer/ca-bundle/zipball/d2c0a83b7533d6912e8d516756ebd34f893e9169",
"reference": "d2c0a83b7533d6912e8d516756ebd34f893e9169",
"shasum": ""
},
"require": {
"ext-openssl": "*",
"ext-pcre": "*",
"php": "^5.3.2 || ^7.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.5",
"psr/log": "^1.0",
"symfony/process": "^2.5 || ^3.0 || ^4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Composer\\CaBundle\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jordi Boggiano",
"email": "j.boggiano@seld.be",
"homepage": "http://seld.be"
}
],
"description": "Lets you find a path to the system CA bundle, and includes a fallback to the Mozilla CA bundle.",
"keywords": [
"cabundle",
"cacert",
"certificate",
"ssl",
"tls"
],
"time": "2018-03-29 19:57:20"
},
{
"name": "geoip2/geoip2",
"version": "v2.9.0",
"source": {
"type": "git",
"url": "https://github.com/maxmind/GeoIP2-php.git",
"reference": "a807fbf65212eef5d8d2db1a1b31082b53633d77"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/maxmind/GeoIP2-php/zipball/a807fbf65212eef5d8d2db1a1b31082b53633d77",
"reference": "a807fbf65212eef5d8d2db1a1b31082b53633d77",
"shasum": ""
},
"require": {
"maxmind-db/reader": "~1.0",
"maxmind/web-service-common": "~0.5",
"php": ">=5.4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "2.*",
"phpunit/phpunit": "4.*",
"squizlabs/php_codesniffer": "3.*"
},
"type": "library",
"autoload": {
"psr-4": {
"GeoIp2\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Gregory J. Oschwald",
"email": "goschwald@maxmind.com",
"homepage": "http://www.maxmind.com/"
}
],
"description": "MaxMind GeoIP2 PHP API",
"homepage": "https://github.com/maxmind/GeoIP2-php",
"keywords": [
"IP",
"geoip",
"geoip2",
"geolocation",
"maxmind"
],
"time": "2018-04-10 15:32:59"
},
{
"name": "guzzlehttp/guzzle",
"version": "6.3.2",
@ -247,6 +355,108 @@
],
"time": "2017-03-20 17:10:46"
},
{
"name": "maxmind-db/reader",
"version": "v1.3.0",
"source": {
"type": "git",
"url": "https://github.com/maxmind/MaxMind-DB-Reader-php.git",
"reference": "e042b4f8a2dff41e19019faf16427178b07fbd58"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/maxmind/MaxMind-DB-Reader-php/zipball/e042b4f8a2dff41e19019faf16427178b07fbd58",
"reference": "e042b4f8a2dff41e19019faf16427178b07fbd58",
"shasum": ""
},
"require": {
"php": ">=5.4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "2.*",
"phpunit/phpunit": "4.* || 5.*",
"satooshi/php-coveralls": "1.0.*",
"squizlabs/php_codesniffer": "3.*"
},
"suggest": {
"ext-bcmath": "bcmath or gmp is required for decoding larger integers with the pure PHP decoder",
"ext-gmp": "bcmath or gmp is required for decoding larger integers with the pure PHP decoder",
"ext-maxminddb": "A C-based database decoder that provides significantly faster lookups"
},
"type": "library",
"autoload": {
"psr-4": {
"MaxMind\\Db\\": "src/MaxMind/Db"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Gregory J. Oschwald",
"email": "goschwald@maxmind.com",
"homepage": "http://www.maxmind.com/"
}
],
"description": "MaxMind DB Reader API",
"homepage": "https://github.com/maxmind/MaxMind-DB-Reader-php",
"keywords": [
"database",
"geoip",
"geoip2",
"geolocation",
"maxmind"
],
"time": "2018-02-21 21:23:33"
},
{
"name": "maxmind/web-service-common",
"version": "v0.5.0",
"source": {
"type": "git",
"url": "https://github.com/maxmind/web-service-common-php.git",
"reference": "61a9836fa3bb1743ab89752bae5005d71e78c73b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/maxmind/web-service-common-php/zipball/61a9836fa3bb1743ab89752bae5005d71e78c73b",
"reference": "61a9836fa3bb1743ab89752bae5005d71e78c73b",
"shasum": ""
},
"require": {
"composer/ca-bundle": "^1.0.3",
"ext-curl": "*",
"ext-json": "*",
"php": ">=5.4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "2.*",
"phpunit/phpunit": "4.*",
"squizlabs/php_codesniffer": "3.*"
},
"type": "library",
"autoload": {
"psr-4": {
"MaxMind\\Exception\\": "src/Exception",
"MaxMind\\WebService\\": "src/WebService"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Gregory Oschwald",
"email": "goschwald@maxmind.com"
}
],
"description": "Internal MaxMind Web Service API",
"homepage": "https://github.com/maxmind/web-service-common-php",
"time": "2018-02-12 22:31:54"
},
{
"name": "psr/http-message",
"version": "1.0.1",

Binary file not shown.

@ -0,0 +1,98 @@
<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
use GeoIp2\Database\Reader;
$_SERVER['REMOTE_ADDR'] = "206.127.96.82";
try {
require_once __DIR__ . "/requiredpublic.php";
$time = date("Y-m-d H:i:s");
/**
* https://stackoverflow.com/a/2040279
*/
function gen_uuid() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
//
// Read/set the cookie
//
if (isset($_COOKIE['sw-uuid'])) {
$uuid = $_COOKIE['sw-uuid'];
} else {
$uuid = gen_uuid();
}
setcookie("sw-uuid", $uuid, time() + 60 * 60 * 24 * 30, "/", $_SERVER['HTTP_HOST'], false, true);
//
// Get the user's IP address
//
$clientip = $_SERVER['REMOTE_ADDR'];
// Check if we're behind CloudFlare and adjust accordingly
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"]) && validateCloudflare()) {
$clientip = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
//
// Lookup IP address
//
$reader = new Reader(GEOIP_DB);
$record = $reader->city($clientip);
$country = $record->country->name;
$region = $record->mostSpecificSubdivision->name;
$city = $record->city->name;
$lat = $record->location->latitude;
$lon = $record->location->longitude;
//
// Save the page visit
//
$database->insert("analytics", [
"siteid" => getsiteid(),
"pageid" => getpageid(),
"uuid" => $uuid,
"country" => $country,
"region" => $region,
"city" => $city,
"lat" => $lat,
"lon" => $lon,
"time" => $time
]);
} catch (GeoIp2\Exception\AddressNotFoundException $e) {
if (DEBUG) {
echo "<!-- The client IP was not found in the GeoIP database. -->";
}
} catch (Exception $e) {
// Silently fail so the rest of the site still works
}

@ -120,6 +120,20 @@ function getpageslug() {
return null;
}
function getpageid() {
global $database;
if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$id = "index";
}
$siteid = getsiteid();
if ($database->has("pages", ["AND" => ["slug" => $id, "siteid" => $siteid]])) {
return $database->get("pages", "pageid", ["AND" => ["slug" => $id, "siteid" => $siteid]]);
}
return null;
}
function getpagetemplate() {
global $database;
$slug = getpageslug();

@ -8,6 +8,8 @@
require_once __DIR__ . "/../lib/requiredpublic.php";
require_once __DIR__ . "/../lib/themefunctions.php";
include __DIR__ . "/../lib/gatheranalytics.php";
if (!getsiteid()) {
sendError("No website has been created yet. Please open " . SITE_TITLE . " and make one.");
}

@ -37,6 +37,13 @@ define('URL', '/sitewriter');
// Folder for public files
define('FILE_UPLOAD_PATH', __DIR__ . '/public/files');
// Location of MaxMind GeoIP database
//
// I'll just leave this here:
// This product includes GeoLite2 data created by MaxMind, available from
// http://www.maxmind.com
define('GEOIP_DB', __DIR__ . "/GeoLite2-City.mmdb");
// Use Captcheck on login screen
// https://captcheck.netsyms.com
define("CAPTCHA_ENABLED", FALSE);
@ -47,4 +54,4 @@ define('LANGUAGE', "en_us");
define("FOOTER_TEXT", "");
define("COPYRIGHT_NAME", "Netsyms Technologies");
define("COPYRIGHT_NAME", "Netsyms Technologies");

Loading…
Cancel
Save