function create_feed($userid, $name, $NoOfDataFields, $datatype) { // Check if feed of given name by the user already exists $feedid = get_feed_id($userid, $name); if ($feedid != 0) { return $feedid; } $result = db_query("INSERT INTO feeds (name,status,datatype) VALUES ('{$name}','0','{$datatype}')"); // Create the feed entry $feedid = db_insert_id(); if ($feedid > 0) { db_query("INSERT INTO feed_relation (userid,feedid) VALUES ('{$userid}','{$feedid}')"); // Create a user->feed relation $feedname = "feed_" . $feedid; // Feed name if ($NoOfDataFields == 1) { // Create a table with one data field $result = db_query("CREATE TABLE {$feedname} (\n\t time INT UNSIGNED, data float,\n INDEX ( `time` ))"); } if ($NoOfDataFields == 2) { // Create a table with two data fields $result = db_query("CREATE TABLE {$feedname} (\n\t time INT UNSIGNED, data float, data2 float,\n INDEX ( `time` ))"); } return $feedid; // Return created feed id } else { return 0; } }
function sync_controller() { require "Models/feed_model.php"; global $session, $action, $format; $output['content'] = ""; $output['message'] = ""; $url = urldecode($_GET['url']); $remotekey = db_real_escape_string(preg_replace('/[^.\\/A-Za-z0-9]/', '', $_GET['remotekey'])); // Register a feed to be downloaded action // sync/feed?url=URL &remotekey=REMOTEKEY &id=FEEDID &name=FEEDNAME if ($action == "feed" && $session['write']) { $id = intval($_GET['id']); $name = preg_replace('/[^\\w\\s-.]/', '', $_GET["name"]); $localfeedid = get_feed_id($session['userid'], $name); if (!$localfeedid) { $localfeedid = create_feed($session['userid'], $name, 1, 0); } // Make sure feed is not already in que $result = db_query("SELECT * FROM importqueue WHERE baseurl='{$url}' AND apikey='{$remotekey}' AND feedid='{$id}' AND localfeedid='{$localfeedid}'"); if (!db_fetch_array($result)) { db_query("INSERT INTO importqueue (`baseurl`,`apikey`,`feedid`,`localfeedid`) VALUES ('{$url}','{$remotekey}','{$id}','{$localfeedid}')"); } } // SYNC Page display if ($session['write']) { $settingsarray = get_user_settingsarray($session['userid']); if (!$url || !$remotekey) { $url = $settingsarray->remoteurl; $remotekey = $settingsarray->remotekey; } if ($url && $remotekey) { $settingsarray->remoteurl = $url; $settingsarray->remotekey = $remotekey; set_user_settingsarray($session['userid'], $settingsarray); // Request feed list $fh = @fopen($url . "/feed/list.json?apikey=" . $remotekey, 'r'); // Read reply $data = ""; while (($buffer = fgets($fh)) !== false) { $data .= $buffer; } // Convert into feedlist array $remote_feeds = json_decode($data); fclose($fh); /* This section gets import queue location of the feeds */ // Get id position of first feed in line $result = db_query("SELECT * FROM importqueue ORDER BY `queid` Asc LIMIT 1"); $row = db_fetch_array($result); $first_in_line = $row['queid']; // For each feed check that the feed exists in the import que and calculate its position for ($i = 0; $i < count($remote_feeds); $i++) { $id = $remote_feeds[$i][0]; $localfeedid = get_feed_id($session['userid'], $remote_feeds[$i][1]); $result = db_query("SELECT * FROM importqueue WHERE baseurl='{$url}' AND apikey='{$remotekey}' AND feedid='{$id}' AND localfeedid='{$localfeedid}'"); if ($row = db_fetch_array($result)) { $remote_feeds[$i]['inque'] = "Queue position: " . ($row['queid'] - $first_in_line); } if ($localfeedid) { $localfeedname = "feed_" . trim($localfeedid) . ""; $localfeedtime_result = db_query("SELECT * FROM {$localfeedname} ORDER BY time Desc LIMIT 1"); $localfeedtime_row = db_fetch_array($localfeedtime_result); $time_diff = ($remote_feeds[$i][3] / 1000 - $localfeedtime_row[0]) / 3600; $remote_feeds[$i]['synctime'] = intval($time_diff) . " hours"; if ($time_diff > 48) { $remote_feeds[$i]['synctime'] = intval($time_diff / 24) . " days"; } if ($time_diff > 24 * 365) { $remote_feeds[$i]['synctime'] = intval($time_diff / (24 * 365)) . " years"; } } else { $remote_feeds[$i]['synctime'] = "no local feed"; } } } $output['content'] = view("sync_view.php", array('url' => $url, 'remotekey' => $remotekey, 'feeds' => $remote_feeds)); } return $output; }
function process_controller() { require "Models/process_model.php"; require "Models/input_model.php"; require "Models/feed_model.php"; global $session, $action, $format; $output['content'] = ""; $output['message'] = ""; //--------------------------------------------------------------------------------------------------------- // Get process list of input // http://yoursite/emoncms/process/list.html?inputid=1 // http://yoursite/emoncms/process/list.json?inputid=1 //--------------------------------------------------------------------------------------------------------- if ($action == "list" && $session['read']) { $inputid = intval($_GET["inputid"]); $input_processlist = get_input_processlist_desc($session['userid'], $inputid); if ($format == 'json') { $output['content'] = json_encode($input_processlist); } elseif ($format == 'html') { $output['content'] = view("process/list_view.php", array('inputid' => $inputid, 'input_processlist' => $input_processlist, 'process_list' => get_process_list())); } } elseif ($action == "add" && $session['write']) { $inputid = intval($_GET['inputid']); $processid = intval($_GET['type']); // get process type (ProcessArg::) $arg = floatval($_GET['arg']); // This is actual value, inputid or feedid //arg2 is feed name if arg=-1 (create new feed) $arg2 = preg_replace('/[^\\w\\s-.]/', '', $_GET['arg2']); // filter out all except for alphanumeric white space and dash $arg2 = db_real_escape_string($arg2); $process = get_process($processid); $valid = false; // Flag to determine if argument is valid switch ($process[1]) { case ProcessArg::VALUE: // If arg type value $arg = floatval($arg); $id = $arg; if ($arg != '') { $valid = true; } else { $output['message'] = 'ERROR: Argument must be a valid number greater or less than 0.'; } break; case ProcessArg::INPUTID: // If arg type input // Check if input exists (returns 0 if invalid) $name = get_input_name($arg); $id = get_input_id($_SESSION['userid'], $name); if ($name == '' || $id == '') { $output['message'] = 'ERROR: Input does not exist!'; } else { $valid = true; } break; case ProcessArg::FEEDID: // If arg type feed // First check if feed exists of given feed id and user. $name = get_feed_name($arg); $id = get_feed_id($_SESSION['userid'], $name); if ($name == '' || $id == '') { // If it doesnt then create a feed, $process[3] is the number of datafields in the feed table $id = create_feed($_SESSION['userid'], $arg2, $process[3], $process[4]); // Check feed was created successfully $name = get_feed_name($id); if ($name == '') { $output['message'] = 'ERROR: Could not create new feed!'; } else { $valid = true; } } else { $valid = true; } break; } if ($valid) { add_input_process($session['userid'], $inputid, $processid, $id); } if ($format == 'json') { $output['message'] = json_encode($output['message']); } elseif ($format == 'html') { header('Location: list?inputid=' . $inputid); } } elseif ($action == 'delete' && $session['write']) { $inputid = intval($_GET['inputid']); $processid = intval($_GET['processid']); delete_input_process($session['userid'], $inputid, $processid); if ($format == 'html') { header('Location: list?inputid=' . $inputid); } } elseif ($action == 'move' && $session['write']) { $inputid = intval($_GET['inputid']); $processid = intval($_GET['processid']); $moveby = intval($_GET['moveby']); move_input_process($session['userid'], $inputid, $processid, $moveby); if ($format == 'html') { header('Location: list?inputid=' . $inputid); } } elseif ($action == 'query' && $session['read']) { $inputid = intval($_GET["inputid"]); $processid = intval($_GET["type"]); // get process type $arg = preg_replace('/[^\\w\\s-.]/', '', $_GET["arg"]); // filter out all except for alphanumeric white space and dash $arg = db_real_escape_string($arg); $process = get_process($processid); $newprocess[0] = $process[1]; // Process arg type switch ($process[1]) { case ProcessArg::VALUE: $newprocess[1] = "Value"; break; case ProcessArg::INPUTID: $newprocess[1] = "Input"; $newprocess[2] = get_user_input_names($session['userid']); break; case ProcessArg::FEEDID: $newprocess[1] = "Feed"; $newprocess[2] = get_user_feed_names($session['userid']); break; default: $newprocess[1] = "ERROR"; } if ($format == 'json') { $output['content'] = json_encode($newprocess); } //if ($format == 'html') $output['content'] = $argboxhtml; } elseif ($action == "test" && $_SESSION['write']) { set_time_limit(360); // Increase PHP limit // Create Histogram data - (to feed, from feed, from date, to date). // $rows = histogram_history(4,1,"2008-01-01","2012-05-01"); } elseif ($action == "autoconfigure" && $session['write']) { $inputs = get_user_inputs($session['userid']); foreach ($inputs as $input) { auto_configure_inputs($session['userid'], $input[0], $input[1]); } if ($format == 'html') { header("Location: ../feed/list"); } } return $output; }