DB "canary" added - with fail/restart when connection dropped
[weathermon.git] / web / send.php
index 7a698389538913b30237a4518f22d55723d8026c..86286ef77d2a4c1f29a9a1f75f218976b09c8a0c 100644 (file)
@@ -1,42 +1,60 @@
 <?php
     require_once 'config_local.php';
 
-    $client_ip = ip2long($_SERVER["REMOTE_ADDR"]);
 
-    if (!client_ip || $client_ip > $valid_ip_end || $valid_ip_start > $client_ip) {
+    $client_ip = $_SERVER["REMOTE_ADDR"];
+
+    if ((strpos($client_ip, "192.168.") === 0) || (strpos($client_ip, "10.8.") === 0) || (strpos($client_ip, "2001:470:6f:9d5:") === 0)) {
+
+   
+      $local_net = True;
+            
+    } else {
+  
+      $local_net = False;
+  
+    }
+
+
+
+    if (! $local_net) { 
       header('HTTP/1.1 403 Forbidden');
       echo "IP not in allowed range";
       exit;
     }
 
+
     $stype = $_REQUEST['stype'];
     $sid   = $_REQUEST['sid'];
     $param = $_REQUEST['param'];
     $value = $_REQUEST['value'];
 
+    if (isset($_REQUEST['time'])) {
+    
+      $timestamp = "'".$_REQUEST['time']."'";
+    
+    } else {
+    
+      $timestamp = 'NULL';
+    
+    }
+
+    
+
     $connection = new mysqli($mysql_host, $mysql_user, $mysql_pwd, $mysql_schema, $mysql_port);
     if ($connection->connect_errno) {
         header('HTTP/1.1 500 Internal Server Error');
         exit;    
     }
         
-    $str = "CALL meteo.submit_value('".$stype."','".$sid."','".$param."',".$value.",NULL)";
-        if (!$connection->query($str)) {
-            header('HTTP/1.1 500 Internal Server Error');
-            exit;    
-        } else {
-            $connection->commit();
-?>        
-<html>
-<head>
-  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-  <title>Meteo logger</title>
-</head>
-<body>
-OK Logged
-</body>
-</html>
-<?
+    $str = "CALL meteo.submit_value('".$stype."','".$sid."','".$param."',".$value.",".$timestamp.")";
+    if (!$connection->query($str)) {
+        header('HTTP/1.1 500 Internal Server Error');
+        echo "$str\n";
+        echo "Call Failed\n";
+        exit;    
+    } else {
+        $connection->commit();
+        echo "OK Logged";    
     }
 
-?>