function check_db() { // Check if table configuration exists $sql = "SHOW TABLES FROM cultibox LIKE 'power';"; $db = \db_priv_pdo_start("root"); try { $sth = $db->prepare($sql); $sth->execute(); $res = $sth->fetchAll(\PDO::FETCH_ASSOC); } catch (\PDOException $e) { $ret = $e->getMessage(); } // If table exists, return if ($res == null) { // Buil MySQL command to create table $sql = "CREATE TABLE power (" . "timestamp varchar(14) NOT NULL DEFAULT ''," . "record int(3) DEFAULT NULL," . "plug_number int(3) DEFAULT NULL," . "date_catch varchar(10) DEFAULT NULL," . "time_catch varchar(10) DEFAULT NULL," . "KEY timestamp (timestamp));"; // Create table try { $sth = $db->prepare($sql); $sth->execute(); } catch (\PDOException $e) { $ret = $e->getMessage(); print_r($ret); } } $db = null; }
function getDB($plug = "") { // get table if ($plug == "") { $sql = "SELECT * FROM plugs ORDER BY id ASC;"; } else { $sql = "SELECT * FROM plugs WHERE id = '{$plug}';"; } $db = \db_priv_pdo_start("root"); $res = array(); try { $sth = $db->prepare($sql); $sth->execute(); $res = $sth->fetchAll(\PDO::FETCH_ASSOC); } catch (\PDOException $e) { $ret = $e->getMessage(); } return $res; }
<?php require_once '../../libs/db_get_common.php'; require_once '../../libs/config.php'; if (isset($_GET['name']) && !empty($_GET['name'])) { $name = $_GET['name']; } else { echo json_encode(""); return 0; } $sql = <<<EOF SELECT name FROM program_index WHERE name LIKE '{$name}' LIMIT 1; EOF; $db = db_priv_pdo_start(); try { $sth = $db->prepare("{$sql}"); $sth->execute(); $res = $sth->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $ret = $e->getMessage(); } $db = null; if (count($res) > 0) { echo json_encode("NOK"); return 0; } echo json_encode("");
function getTriggers() { // Select * triggers $sql = "SELECT * FROM trigger_index ;"; $db = \db_priv_pdo_start("root"); $res = array(); try { $sth = $db->prepare($sql); $sth->execute(); $res = $sth->fetchAll(\PDO::FETCH_ASSOC); } catch (\PDOException $e) { $ret = $e->getMessage(); } /* J'en suis la */ return $res; }
function check_sensors_def() { $sql = <<<EOF SELECT * FROM `sensors` EOF; $db = db_priv_pdo_start(); try { $sth = $db->prepare("{$sql}"); $sth->execute(); $res = $sth->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $ret = $e->getMessage(); } $db = null; if (isset($ret) && !empty($ret)) { if ($GLOBALS['DEBUG_TRACE']) { $out[] = __('ERROR_SELECT_SQL') . $ret; } else { $out[] = __('ERROR_SELECT_SQL'); } } $tab = array(); if (count($res) > 0) { foreach ($res as $sensor) { if (!array_key_exists($sensor['type'], $GLOBALS['SENSOR_DEFINITION']) && $sensor['type'] != 0) { $tab[] = $sensor['id']; } } } if (count($tab) > 0) { $sql = " "; foreach ($tab as $index) { $sql = $sql . " UPDATE `sensors` SET type=0 WHERE id=" . $index . "; "; } $db = db_priv_pdo_start(); try { $db->exec("{$sql}"); } catch (PDOException $e) { $ret = $e->getMessage(); } $db = null; if (isset($ret) && !empty($ret)) { if ($GLOBALS['DEBUG_TRACE']) { $out[] = __('ERROR_UPDATE_SQL') . $ret; } else { $out[] = __('ERROR_UPDATE_SQL'); } } } }
function updateTable($table, $id, $parameter, $value) { // Update position conf $sql = "UPDATE {$table} SET {$parameter}='{$value}' WHERE id='{$id}' ;"; $db = \db_priv_pdo_start("root"); $res = array(); try { $sth = $db->prepare($sql); $sth->execute(); } catch (\PDOException $e) { $ret = $e->getMessage(); } echo $sql . "\n"; return 0; }
function saveEmailUserConf($param) { // Open connection to dabase $db = \db_priv_pdo_start(); $str = ""; foreach ($param as $key => $value) { if ($str != "") { $str = $str . " , "; } $str = $str . "{$key}='{$value}'"; } $sql = "UPDATE configuration SET {$str} ;"; try { $sth = $db->prepare($sql); $sth->execute(); } catch (\PDOException $e) { $ret = $e->getMessage(); print_r($ret); } }
function create_plgidx($data) { $plgidx = array(); $return = array(); // Open database connexion $db = \db_priv_pdo_start(); // Foreach event foreach ($data as $event) { // If this is a program index event if ($event['program_index'] != "") { // Query plugv filename associated try { $sql = "SELECT plugv_filename FROM program_index WHERE id = \"" . $event['program_index'] . "\";"; $sth = $db->prepare($sql); $sth->execute(); $res = $sth->fetch(); } catch (\PDOException $e) { $ret = $e->getMessage(); } // $today = strtotime(date("Y-m-d")); $nextYear = strtotime("+1 year", strtotime(date("Y-m-d"))); // Start date $date = $event['start_year'] . "-" . $event['start_month'] . "-" . $event['start_day']; // End date $end_date = $event['end_year'] . "-" . $event['end_month'] . "-" . $event['end_day']; while (strtotime($date) <= strtotime($end_date)) { // Save only for futur element if (strtotime($date) >= $today && strtotime($date) < $nextYear) { $plgidx[$date] = $res['plugv_filename']; } // Incr date $date = date("Y-m-d", strtotime("+1 day", strtotime($date))); } } } // Close connexion $db = null; // For each day for ($month = 1; $month <= 13; $month++) { for ($day = 1; $day <= 31; $day++) { // Format day and month $monthToWrite = $month; if (strlen($monthToWrite) < 2) { $monthToWrite = "0{$monthToWrite}"; } $dayToWrite = $day; if (strlen($dayToWrite) < 2) { $dayToWrite = "0{$dayToWrite}"; } // Date to search in event $dateToSearch = date("Y") . "-" . $monthToWrite . "-" . $dayToWrite; $plugvToUse = "00"; if (array_key_exists($dateToSearch, $plgidx)) { $plugvToUse = $plgidx[$dateToSearch]; if (strlen($plugvToUse) < 2) { $plugvToUse = "0{$plugvToUse}"; } } // Write the day $return[] = $monthToWrite . $dayToWrite . $plugvToUse; } } return $return; }
function get_nb_daily_program(&$out) { $sql = "SELECT count(id) as nb_daily FROM program_index WHERE id > 1;"; $db = db_priv_pdo_start(); $res = ""; try { $sth = $db->prepare($sql); $sth->execute(); $res = $sth->fetch(PDO::FETCH_ASSOC); } catch (PDOException $e) { $ret = $e->getMessage(); } $db = null; return $res['nb_daily']; }
require_once '../../../main/libs/utilfunc.php'; require_once '../../../main/libs/utilfunc_sd_card.php'; if (isset($_GET["title"]) && !empty($_GET["title"]) && isset($_GET["start"]) && !empty($_GET["start"]) && isset($_GET["end"]) && !empty($_GET["end"]) && isset($_GET["id"]) && !empty($_GET["id"]) && isset($_GET["color"]) && !empty($_GET["color"])) { $title = $_GET["title"]; $start = $_GET["start"]; $end = $_GET["end"]; $id = $_GET["id"]; $color = $_GET["color"]; $sd_card = $_GET["card"]; if (!isset($_GET["important"]) || empty($_GET["important"])) { $important = 0; } else { $important = 1; } $main_error = array(); if ($db = db_priv_pdo_start()) { if (isset($_GET["desc"]) && !empty($_GET["desc"])) { $description = $db->quote($_GET["desc"]); } if (isset($description) && !empty($description)) { $sql = <<<EOF UPDATE `calendar` SET `Title`={$db->quote($title)},`StartTime`="{$start}",`EndTime`="{$end}",`Color`="{$color}", `Description`={$description}, `Important`={$important} WHERE `Id` = {$id} EOF; } else { $sql = <<<EOF UPDATE `calendar` SET `Title`={$db->quote($title)},`StartTime`="{$start}",`EndTime`="{$end}", `Color`="{$color}", `Description`= NULL, `Important`={$important} WHERE `Id` = {$id} EOF; } $sql_old = <<<EOF SELECT * FROM `calendar` WHERE `Id` = {$id} EOF;
function reset_fake_log() { $sql = "DELETE FROM logs WHERE fake_log LIKE 'True';"; $db = \db_priv_pdo_start("root"); try { $db->exec($sql); } catch (\PDOException $e) { $ret = $e->getMessage(); } $db = null; if (isset($ret) && !empty($ret)) { return $ret; } return 1; }
function insert_calendar($event, &$out) { // If there is no event to add, return if (count($event) == 0) { return false; } // Create sql line $sql = ""; foreach ($event as $evt) { // Check if program_index exists. If not add empty if (!array_key_exists("program_index", $evt)) { $evt["program_index"] = ""; } $sql .= "INSERT INTO calendar" . " (Title, StartTime, EndTime, Description, Color, Icon, program_index) " . " VALUES ('{$evt['title']}', '{$evt['start']}', '{$evt['end']}', '{$evt['description']}', '{$evt['color']}', '{$evt['icon']}', '{$evt['program_index']}');"; } $db = \db_priv_pdo_start(); $db->exec($sql); $db = null; return true; }
<?php require_once '../../../main/libs/config.php'; require_once '../../../main/libs/db_get_common.php'; require_once '../../../main/libs/db_set_common.php'; require_once '../../../main/libs/utilfunc.php'; require_once '../../../main/libs/utilfunc_sd_card.php'; $id = $_GET["id"]; $sd_card = $_GET["card"]; $main_error = array(); // Init connexion to DB $db = db_priv_pdo_start("root"); // Read days to update $sql = "SELECT * FROM calendar WHERE Id = '{$id}' ;"; try { $sth = $db->prepare($sql); $sth->execute(); $res = $sth->fetch(); } catch (\PDOException $e) { print_r($e->getMessage()); } $start = $res['StartTime']; $end = $res['EndTime']; // Delete event from calendar $sql = "DELETE FROM calendar WHERE Id = '{$id}' ;"; $sth = $db->prepare($sql); $sth->execute(); $res = $sth->fetchAll(PDO::FETCH_ASSOC); $db = null;
function export_program($id, $program_index, $file = "") { $sql = "SELECT * FROM programs WHERE plug_id = {$id} AND number = {$program_index}"; $db = \db_priv_pdo_start(); try { $sth = $db->prepare("{$sql}"); $sth->execute(); $res = $sth->fetchAll(\PDO::FETCH_ASSOC); } catch (\PDOException $e) { $ret = $e->getMessage(); } $db = null; if (strcmp("{$file}", "") == 0) { $file = "../../../tmp/export/program_plug{$id}.csv"; } if ($f = fopen("{$file}", "w")) { fputs($f, "time_start\ttime_stop\tvalue\ttype\r\n"); if (count($res) > 0) { foreach ($res as $record) { fputs($f, $record['time_start'] . "\t" . $record['time_stop'] . "\t" . $record['value'] . "\t" . $record['type'] . "\r\n"); } } else { fputs($f, "000000\t235959\t0\t0\r\n"); } } fclose($f); }