} die; } if (!$mysqli->connect_error && $dbtest == true) { require "Lib/dbschemasetup.php"; if (!db_check($mysqli, $database)) { db_schema_setup($mysqli, load_db_schema(), true); } } // 3) User sessions require "Modules/user/rememberme_model.php"; $rememberme = new Rememberme($mysqli); require "Modules/user/user_model.php"; $user = new User($mysqli, $redis, $rememberme); if (isset($_GET['apikey'])) { $session = $user->apikey_session($_GET['apikey']); } elseif (isset($_POST['apikey'])) { $session = $user->apikey_session($_POST['apikey']); } else { $session = $user->emon_session_start(); } // 4) Language if (!isset($session['lang'])) { $session['lang'] = ''; } set_emoncms_lang($session['lang']); // 5) Get route and load controller $route = new Route(get('q')); if (get('embed') == 1) { $embed = 1; } else {
if (isset($_POST['devicekey'])) { $devicekey = $_POST['devicekey']; } else { if (isset($_SERVER["HTTP_AUTHORIZATION"])) { // Support passing apikey on Authorization header per rfc6750, like example: // GET /resource HTTP/1.1 // Host: server.example.com // Authorization: Bearer THE_API_KEY_HERE $apikey = str_replace('Bearer ', '', $_SERVER["HTTP_AUTHORIZATION"]); } } } } } if ($apikey) { $session = $user->apikey_session($apikey); if (empty($session)) { header($_SERVER["SERVER_PROTOCOL"] . " 401 Unauthorized"); header('WWW-Authenticate: Bearer realm="API KEY", error="invalid_apikey", error_description="Invalid API key"'); print "Invalid API key"; $log = new EmonLogger(__FILE__); $log->error("Invalid API key '" . $apikey . "'"); exit; } } else { if ($devicekey && @(include "Modules/device/device_model.php")) { $device = new Device($mysqli, $redis); $session = $device->devicekey_session($devicekey); if (empty($session)) { header($_SERVER["SERVER_PROTOCOL"] . " 401 Unauthorized"); header('WWW-Authenticate: Bearer realm="Device KEY", error="invalid_devicekey", error_description="Invalid device key"');
if ($usleep < 0) { $usleep = 0; } echo "Buffer length: " . $buflength . " " . $usleep . " " . $rn . "\n"; $rn = 0; } // check if there is an item in the queue to process $line_str = false; if ($redis->llen('buffer') > 0) { // check if there is an item in the queue to process $line_str = $redis->lpop('buffer'); } if ($line_str) { $rn++; $req = json_decode($line_str); $session = $user->apikey_session($req->apikey); // The first and second value in the csv is userid, time and nodeid $userid = $session['userid']; $time = $req->time; $nodeid = $req->nodeid; $line_parts = explode(',', $req->csv); // Load current user input meta data // It would be good to avoid repeated calls to this $dbinputs = $input->get_inputs($userid); $tmp = array(); // For each node input $name = 1; for ($i = 0; $i < count($line_parts); $i++) { $value = $line_parts[$i]; if (!isset($dbinputs[$nodeid][$name])) { $inputid = $input->create_input($userid, $nodeid, $name);