leaf_api: Add OTA

Change-Id: Ib710519bc381ed71cce7ee868ac67528c591ce2c
diff --git a/ota/index.php b/ota/index.php
new file mode 100644
index 0000000..2203fe0
--- /dev/null
+++ b/ota/index.php
@@ -0,0 +1,31 @@
+<?php
+
+header("Content-Type: application/json");
+
+if (isset($_GET['device']) and isset($_GET['flavor'])) {
+	$device = $_GET['device'];
+	$flavor = $_GET['flavor'];
+	$incremental = isset($_GET['incremental']) ? $_GET['incremental'] : '';
+
+	$mysqli = new mysqli("localhost", "leaf", "leaf", "leaf_ota");
+	if ($mysqli->connect_errno) {
+		die("Database unavailable!");
+	}
+
+	$stmt = $mysqli->prepare("SELECT * FROM leaf_ota WHERE device = ? AND flavor = ? AND (incremental_base = ? OR incremental_base is NULL) ORDER BY incremental DESC");
+	$stmt->bind_param('sss', $device, $flavor, $incremental);
+	$stmt->execute();
+
+	$result = $stmt->get_result();
+
+	$json = [];
+	$json['response'] = [];
+
+	while ($row = $result->fetch_assoc()) {
+		array_push($json['response'], array_filter($row));
+	}
+
+	echo json_encode($json, JSON_PRETTY_PRINT);
+}
+
+?>
diff --git a/ota/leaf_ota.sql b/ota/leaf_ota.sql
new file mode 100644
index 0000000..0cb474d
--- /dev/null
+++ b/ota/leaf_ota.sql
@@ -0,0 +1,17 @@
+CREATE DATABASE IF NOT EXISTS leaf_ota;
+GRANT ALL ON leaf_ota.* TO 'leaf'@'localhost' IDENTIFIED BY 'leaf';
+CREATE TABLE IF NOT EXISTS leaf_ota.leaf_ota (
+	device varchar(255) NOT NULL,
+	datetime BIGINT NOT NULL,
+	filename varchar(255) NOT NULL,
+	id varchar(255) NOT NULL,
+	romtype varchar(255) NOT NULL,
+	size BIGINT NOT NULL,
+	url varchar(255) NOT NULL,
+	version varchar(255) NOT NULL,
+	flavor varchar(255) NOT NULL,
+	incremental varchar(255) NOT NULL,
+	incremental_base varchar(255),
+	upgrade varchar(255),
+	PRIMARY KEY (incremental)
+);
diff --git a/ota/nginx/get.leafos.org b/ota/nginx/get.leafos.org
new file mode 100644
index 0000000..4efee6a
--- /dev/null
+++ b/ota/nginx/get.leafos.org
@@ -0,0 +1,45 @@
+server {
+        listen 80;
+        listen [::]:80;
+        server_name get.leafos.org;
+        return 301 https://$server_name$request_uri;
+        root /var/www/get.leafos.org/;
+
+        access_log /var/log/nginx/access/get.leafos.org.log;
+        error_log /var/log/nginx/error/get.leafos.org.log;
+}
+
+server {
+	listen 443 ssl http2;
+	listen [::]:443 ssl http2;
+
+	root /var/www/get.leafos.org/;
+
+	# Add index.php to the list if you are using PHP
+	index index.html index.htm index.php;
+
+	server_name get.leafos.org;
+	access_log /var/log/nginx/access/get.leafos.org.log;
+	error_log /var/log/nginx/error/get.leafos.org.log;
+
+	location / {
+		# First attempt to serve request as file, then
+		# as directory, then fall back to displaying a 404.
+		try_files $uri $uri/ index.html index.php;
+		index index.html index.htm index.php;
+	}
+
+	# OTA check
+	rewrite ^/ota/(.*)/(.*)/(.*) /index.php?device=$1&flavor=$2&incremental=$3 break;
+	rewrite ^/ota/(.*)/(.*) /index.php?device=$1&flavor=$2 break;
+
+	# pass PHP scripts to FastCGI server
+	#
+	location ~ \.php$ {
+		include snippets/fastcgi-php.conf;
+		fastcgi_pass php8;
+	}
+
+	ssl_certificate /etc/letsencrypt/live/leafos.org/fullchain.pem; # managed by Certbot
+	ssl_certificate_key /etc/letsencrypt/live/leafos.org/privkey.pem; # managed by Certbot
+}