155 lines
4.4 KiB
PHP
155 lines
4.4 KiB
PHP
<?php
|
|
/** \file include\db_config.php
|
|
* \brief DI webinterface database functions
|
|
* \author Rob Schalken, Core|Vision
|
|
* \version $Revision: 1.2 $
|
|
* \date $Date: 2013/12/03 13:21:54 $
|
|
*
|
|
* This file contains the configuration database functions. This file is always included.
|
|
*/
|
|
|
|
/**
|
|
* Get configuration settings
|
|
*
|
|
* Parameters:
|
|
* - lance_id database identifier for the device
|
|
* - device_id database identifier for the device type
|
|
* - key key for the configuration setting
|
|
*
|
|
* Returns:
|
|
* Value(s) for the requested setting, or FALSE on error
|
|
* NB: returns an empty array when no data available
|
|
*/
|
|
function db_fetch_config($lance_id, $device_id, $key = NULL)
|
|
{
|
|
// the keys in the configuration tables are case insensitive, but stored in upper case
|
|
if( $key ) $key = strtoupper($key);
|
|
|
|
if( !is_numeric($device_id) ) {
|
|
if( !is_numeric($lance_id) ) return FALSE;
|
|
|
|
$query = "SELECT `data` FROM zkl_config WHERE zkl=" . $lance_id;
|
|
if( $key ) $query .= " AND `key`='" . $key . "'";
|
|
$query .= " ORDER BY id";
|
|
$data = db_fetch_data($query);
|
|
if( $data ) {
|
|
$result = array();
|
|
|
|
foreach( $data as $row ) $result[] = $row['data'];
|
|
return $result;
|
|
}
|
|
else return FALSE;
|
|
}
|
|
else if( !is_numeric($lance_id) ) {
|
|
$query = "SELECT `data` FROM device_config WHERE device=" . $device_id;
|
|
if( $key ) $query .= " AND `key`='" . $key . "'";
|
|
$query .= " ORDER BY id";
|
|
$data = db_fetch_data($query);
|
|
if( $data ) {
|
|
$result = array();
|
|
|
|
foreach( $data as $row ) $result[] = $row['data'];
|
|
return $result;
|
|
}
|
|
else return FALSE;
|
|
}
|
|
else {
|
|
// 1st priority, data specific for the device
|
|
$query = "SELECT `data` ";
|
|
$query .= "FROM zkl_config ";
|
|
$query .= "WHERE zkl=" . $lance_id;
|
|
if( $key ) $query .= " AND `key`='" . $key . "'";
|
|
$query .= " ORDER BY id";
|
|
$data = db_fetch_data($query);
|
|
if( $data ) {
|
|
$result = array();
|
|
|
|
foreach( $data as $row ) $result[] = $row['data'];
|
|
return $result;
|
|
}
|
|
|
|
// 2nd priority, entries for the device type
|
|
$query = "SELECT `data` ";
|
|
$query .= "FROM device_config ";
|
|
$query .= "WHERE ";
|
|
$query .= "device=" . $device_id;
|
|
if( $key ) $query .= " AND `key`='" . $key . "'";
|
|
$query .= " ORDER BY id";
|
|
$data = db_fetch_data($query);
|
|
if( $data ) {
|
|
$result = array();
|
|
|
|
foreach( $data as $row ) $result[] = $row['data'];
|
|
return $result;
|
|
}
|
|
|
|
// last priority, entries for _all_ devices
|
|
$query = "SELECT `data` ";
|
|
$query .= "FROM zkl_config ";
|
|
$query .= "WHERE zkl IS NULL";
|
|
if( $key ) $query .= " AND `key`='" . $key . "'";
|
|
$query .= " ORDER BY id";
|
|
$data = db_fetch_data($query);
|
|
if( $data ) {
|
|
$result = array();
|
|
|
|
foreach( $data as $row ) $result[] = $row['data'];
|
|
return $result;
|
|
}
|
|
|
|
// else: return empty array
|
|
return array();
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Store or update configuration settings
|
|
*
|
|
* Parameters:
|
|
* - lance_id database identifier for the device (may be NULL)
|
|
* - device_id database identifier for the device type (may be NULL)
|
|
* NB: either 'lance_id' or 'device_id' _must_ be specified
|
|
* - key key for the configuration setting
|
|
* - value value for the configuration setting (string or array)
|
|
* - replace replace existing values (append if FALSE)
|
|
*
|
|
* Returns:
|
|
* Success (TRUE) or failure (FALSE)
|
|
*/
|
|
function db_store_config($lance_id, $device_id, $key, $value, $replace = TRUE)
|
|
{
|
|
if( !$lance_id ) {
|
|
$table = "device_config";
|
|
$table_key = "device";
|
|
$dbid = $device_id;
|
|
}
|
|
else {
|
|
$table = "zkl_config";
|
|
$table_key = "zkl";
|
|
$dbid = $lance_id;
|
|
}
|
|
|
|
if( $key ) {
|
|
// all keys are stored in upper case
|
|
$key = strtoupper($key);
|
|
|
|
if( $replace ) db_store_data("DELETE FROM " . $table . " WHERE " . $table_key . "=" . $dbid . " AND `key`='" . $key . "'");
|
|
|
|
if( is_array($value) ) {
|
|
$query = "INSERT INTO " . $table . " (" . $table_key . ",`key`,`data`) VALUES ";
|
|
$sep = "";
|
|
foreach( $value as $data_item ) {
|
|
$query .= $sep . "(" . $dbid . ",'" . $key . "','" . specialchars($data_item) . "')";
|
|
$sep = ",";
|
|
}
|
|
}
|
|
else {
|
|
$query = "INSERT INTO " . $table . " (" . $table_key . ",`key`,`data`) VALUES (" . $dbid . ",'" . $key . "','" . specialchars($value) . "')";
|
|
}
|
|
|
|
return db_store_data($query);
|
|
}
|
|
else return FALSE;
|
|
}
|
|
|
|
?>
|