1<?php
 2/*
 3  Copyright (C) 2020  Denver Gingerich <denver@ossguy.com>
 4
 5  This file is part of sgx-bwmsgsv2.
 6
 7  sgx-bwmsgsv2 is free software: you can redistribute it and/or modify it under
 8  the terms of the GNU Affero General Public License as published by the Free
 9  Software Foundation, either version 3 of the License, or (at your option) any
10  later version.
11
12  sgx-bwmsgsv2 is distributed in the hope that it will be useful, but WITHOUT
13  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  details.
16
17  You should have received a copy of the GNU Affero General Public License along
18  with sgx-bwmsgsv2.  If not, see <http://www.gnu.org/licenses/>.
19*/
20
21$time = microtime(true);
22$tai_timestamp = trim(shell_exec('./tai'));
23
24$raw_data = file_get_contents('php://input');
25
26# below must set $redis_host, $redis_port, $queue_name ($redis_auth is optional)
27# *it is the settings file for this program, the bwmsgsv2 HTTP to Redis acceptor
28include 'settings-h2r.php';
29
30$redis = new Redis();
31$rv = $redis->pconnect($redis_host, $redis_port);
32
33if (!$rv) {
34	error_log("ERROR 1a in Redis connect at $time - cannot save $raw_data");
35	http_response_code(503);  # Service Unavailable
36	exit(0);
37}
38
39if (!empty($redis_auth)) {
40	# TODO: check return value to confirm login succeeded
41	$redis->auth($redis_auth);
42}
43
44$time2 = microtime(true);
45$tai_timestamp2 = trim(shell_exec('./tai'));
46
47$rv = $redis->lPush('incoming_messages-'.$queue_name,
48	'"ts_020_tai-first_db_hit":'.$tai_timestamp2.
49	',"ts_020_unix-first_db_hit":'.$time2.
50	',"ts_010_tai-first_received":'.$tai_timestamp.
51	',"ts_010_unix-first_received":'.$time.',"MSG":'.$raw_data);
52
53if (!$rv) {
54	error_log("ERROR 2a in lPush at $time $time2 - cannot save $raw_data");
55	http_response_code(503);  # Service Unavailable
56	exit(0);
57}
58
59print 'ok';
60
61?>