2002-10-11 17:45:06 +08:00
|
|
|
<?php
|
|
|
|
//
|
2004-01-08 19:45:57 +08:00
|
|
|
// ZoneMinder web database interface file, $Date$, $Revision$
|
2008-07-25 17:48:16 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2002-10-11 17:45:06 +08:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2016-12-26 23:23:16 +08:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2002-10-11 17:45:06 +08:00
|
|
|
//
|
|
|
|
|
2017-08-22 21:52:14 +08:00
|
|
|
define( 'DB_LOG_OFF', 0 );
|
|
|
|
define( 'DB_LOG_ONLY', 1 );
|
|
|
|
define( 'DB_LOG_DEBUG', 2 );
|
2008-07-14 21:54:50 +08:00
|
|
|
|
|
|
|
$GLOBALS['dbLogLevel'] = DB_LOG_OFF;
|
|
|
|
|
|
|
|
$GLOBALS['dbConn'] = false;
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbConnect() {
|
|
|
|
global $dbConn;
|
|
|
|
|
|
|
|
if (strpos(ZM_DB_HOST, ':')) {
|
|
|
|
// Host variable may carry a port or socket.
|
|
|
|
list($host, $portOrSocket) = explode(':', ZM_DB_HOST, 2);
|
|
|
|
if (ctype_digit($portOrSocket)) {
|
|
|
|
$socket = ':host='.$host . ';port='.$portOrSocket;
|
|
|
|
} else {
|
|
|
|
$socket = ':unix_socket='.$portOrSocket;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$socket = ':host='.ZM_DB_HOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-08-15 06:22:52 +08:00
|
|
|
$dbOptions = null;
|
|
|
|
if ( defined( 'ZM_DB_SSL_CA_CERT' ) and ZM_DB_SSL_CA_CERT ) {
|
|
|
|
$dbOptions = array(
|
|
|
|
PDO::MYSQL_ATTR_SSL_CA => ZM_DB_SSL_CA_CERT,
|
|
|
|
PDO::MYSQL_ATTR_SSL_KEY => ZM_DB_SSL_CLIENT_KEY,
|
|
|
|
PDO::MYSQL_ATTR_SSL_CERT => ZM_DB_SSL_CLIENT_CERT,
|
|
|
|
);
|
|
|
|
$dbConn = new PDO( ZM_DB_TYPE . $socket . ';dbname='.ZM_DB_NAME, ZM_DB_USER, ZM_DB_PASS, $dbOptions );
|
|
|
|
} else {
|
|
|
|
$dbConn = new PDO( ZM_DB_TYPE . $socket . ';dbname='.ZM_DB_NAME, ZM_DB_USER, ZM_DB_PASS );
|
|
|
|
}
|
2017-08-16 07:40:02 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
$dbConn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
|
|
|
$dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
} catch(PDOException $ex ) {
|
2017-08-22 21:52:14 +08:00
|
|
|
echo 'Unable to connect to ZM db.' . $ex->getMessage();
|
|
|
|
error_log('Unable to connect to ZM DB ' . $ex->getMessage() );
|
2017-05-17 02:16:32 +08:00
|
|
|
$dbConn = null;
|
|
|
|
}
|
2007-09-07 23:39:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dbConnect();
|
2002-10-11 17:45:06 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbDisconnect() {
|
|
|
|
global $dbConn;
|
|
|
|
$dbConn = null;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
2005-01-03 22:44:38 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbLogOff() {
|
|
|
|
global $dbLogLevel;
|
|
|
|
$dbLogLevel = DB_LOG_OFF;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbLogOn() {
|
|
|
|
global $dbLogLevel;
|
|
|
|
$dbLogLevel = DB_LOG_ONLY;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
2007-08-30 06:33:22 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbLogDebug() {
|
|
|
|
global $dbLogLevel;
|
|
|
|
$dbLogLevel = DB_LOG_DEBUG;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbDebug() {
|
|
|
|
dbLogDebug();
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbLog( $sql, $update=false ) {
|
|
|
|
global $dbLogLevel;
|
|
|
|
$noExecute = $update && ($dbLogLevel >= DB_LOG_DEBUG);
|
|
|
|
if ( $dbLogLevel > DB_LOG_OFF )
|
2017-05-19 01:54:06 +08:00
|
|
|
Logger::Debug( "SQL-LOG: $sql".($noExecute?" (not executed)":"") );
|
2017-05-17 02:16:32 +08:00
|
|
|
return( $noExecute );
|
2007-08-30 06:33:22 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbError( $sql ) {
|
2018-02-02 00:31:25 +08:00
|
|
|
Error( "SQL-ERR '".$dbConn->errorInfo()."', statement was '".$sql."'" );
|
2007-08-30 06:33:22 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbEscape( $string ) {
|
|
|
|
global $dbConn;
|
2017-08-22 21:52:14 +08:00
|
|
|
if ( version_compare( phpversion(), '4.3.0', '<') )
|
2017-05-17 02:16:32 +08:00
|
|
|
if ( get_magic_quotes_gpc() )
|
|
|
|
return( $dbConn->quote( stripslashes( $string ) ) );
|
|
|
|
else
|
|
|
|
return( $dbConn->quote( $string ) );
|
|
|
|
else
|
|
|
|
if ( get_magic_quotes_gpc() )
|
|
|
|
return( $dbConn->quote( stripslashes( $string ) ) );
|
2007-09-06 20:59:19 +08:00
|
|
|
else
|
2017-05-17 02:16:32 +08:00
|
|
|
return( $dbConn->quote( $string ) );
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2014-09-27 02:43:58 +08:00
|
|
|
function dbQuery( $sql, $params=NULL ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
global $dbConn;
|
|
|
|
if ( dbLog( $sql, true ) )
|
|
|
|
return;
|
|
|
|
$result = NULL;
|
|
|
|
try {
|
|
|
|
if ( isset($params) ) {
|
2017-10-31 08:20:40 +08:00
|
|
|
if ( ! $result = $dbConn->prepare( $sql ) ) {
|
|
|
|
Error("SQL: Error preparing $sql: " . $pdo->errorInfo);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $result->execute( $params ) ) {
|
|
|
|
Error("SQL: Error executing $sql: " . implode(',', $result->errorInfo() ) );
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-17 02:16:32 +08:00
|
|
|
} else {
|
|
|
|
$result = $dbConn->query( $sql );
|
2013-10-19 02:15:24 +08:00
|
|
|
}
|
2018-01-23 00:42:19 +08:00
|
|
|
if ( defined('ZM_DB_DEBUG') ) {
|
|
|
|
if ( $params )
|
|
|
|
Warning("SQL: $sql" . implode(',',$params) . ' rows: '.$result->rowCount() );
|
|
|
|
else
|
|
|
|
Warning("SQL: $sql: rows:" . $result->rowCount() );
|
|
|
|
}
|
2017-05-17 02:16:32 +08:00
|
|
|
} catch(PDOException $e) {
|
2017-11-09 00:17:46 +08:00
|
|
|
Error( "SQL-ERR '".$e->getMessage()."', statement was '".$sql."' params:" . ($params?implode(',',$params):'') );
|
2018-01-30 02:41:24 +08:00
|
|
|
return NULL;
|
2017-05-17 02:16:32 +08:00
|
|
|
}
|
2018-01-30 02:41:24 +08:00
|
|
|
return $result;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbFetchOne( $sql, $col=false, $params=NULL ) {
|
|
|
|
$result = dbQuery( $sql, $params );
|
|
|
|
if ( ! $result ) {
|
2018-02-02 00:31:25 +08:00
|
|
|
Error( "SQL-ERR dbFetchOne no result, statement was '".$sql."'" . ( $params ? 'params: ' . join(',',$params) : '' ) );
|
2017-05-17 02:16:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $result && $dbRow = $result->fetch( PDO::FETCH_ASSOC ) ) {
|
|
|
|
if ( $col ) {
|
|
|
|
if ( ! isset( $dbRow[$col] ) ) {
|
|
|
|
Warning( "$col does not exist in the returned row" );
|
|
|
|
}
|
|
|
|
return $dbRow[$col];
|
|
|
|
}
|
|
|
|
return $dbRow;
|
|
|
|
}
|
|
|
|
return( false );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbFetchAll( $sql, $col=false, $params=NULL ) {
|
|
|
|
$result = dbQuery( $sql, $params );
|
|
|
|
if ( ! $result ) {
|
2018-02-02 00:31:25 +08:00
|
|
|
Error( "SQL-ERR dbFetchAll no result, statement was '".$sql."'" . ( $params ? 'params: ' .join(',', $params) : '' ) );
|
2017-05-17 02:16:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbRows = array();
|
|
|
|
while( $dbRow = $result->fetch( PDO::FETCH_ASSOC ) )
|
|
|
|
$dbRows[] = $col?$dbRow[$col]:$dbRow;
|
|
|
|
return( $dbRows );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbFetchAssoc( $sql, $indexCol, $dataCol=false ) {
|
|
|
|
$result = dbQuery( $sql );
|
2008-12-06 07:15:08 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
$dbRows = array();
|
|
|
|
while( $dbRow = $result->fetch( PDO::FETCH_ASSOC ) )
|
|
|
|
$dbRows[$dbRow[$indexCol]] = $dataCol?$dbRow[$dataCol]:$dbRow;
|
|
|
|
return( $dbRows );
|
2008-12-06 07:15:08 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbFetch( $sql, $col=false ) {
|
|
|
|
return( dbFetchAll( $sql, $col ) );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbFetchNext( $result, $col=false ) {
|
|
|
|
if ( $dbRow = $result->fetch( PDO::FETCH_ASSOC ) )
|
|
|
|
return( $col?$dbRow[$col]:$dbRow );
|
|
|
|
return( false );
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbNumRows( $sql ) {
|
|
|
|
$result = dbQuery( $sql );
|
|
|
|
return( $result->rowCount() );
|
2005-01-03 22:44:38 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbInsertId() {
|
|
|
|
global $dbConn;
|
|
|
|
return( $dbConn->lastInsertId() );
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function getEnumValues( $table, $column ) {
|
|
|
|
$row = dbFetchOne( "describe $table $column" );
|
|
|
|
preg_match_all( "/'([^']+)'/", $row['Type'], $matches );
|
|
|
|
return( $matches[1] );
|
2002-10-11 17:45:06 +08:00
|
|
|
}
|
2003-09-23 17:52:45 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function getSetValues( $table, $column ) {
|
|
|
|
return( getEnumValues( $table, $column ) );
|
2003-09-23 17:52:45 +08:00
|
|
|
}
|
2005-03-14 04:41:06 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function getUniqueValues( $table, $column, $asString=1 ) {
|
|
|
|
$values = array();
|
|
|
|
$sql = "select distinct $column from $table where (not isnull($column) and $column != '') order by $column";
|
|
|
|
foreach( dbFetchAll( $sql ) as $row ) {
|
|
|
|
if ( $asString )
|
|
|
|
$values[$row[$column]] = $row[$column];
|
|
|
|
else
|
|
|
|
$values[] = $row[$column];
|
|
|
|
}
|
|
|
|
return( $values );
|
2005-03-14 04:41:06 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function getTableColumns( $table, $asString=1 ) {
|
|
|
|
$columns = array();
|
|
|
|
$sql = "describe $table";
|
|
|
|
foreach( dbFetchAll( $sql ) as $row ) {
|
|
|
|
if ( $asString )
|
|
|
|
$columns[$row['Field']] = $row['Type'];
|
|
|
|
else
|
|
|
|
$columns[] = $row['Type'];
|
|
|
|
}
|
|
|
|
return( $columns );
|
2005-03-14 04:41:06 +08:00
|
|
|
}
|
2007-09-06 20:59:19 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function getTableAutoInc( $table ) {
|
2017-08-22 21:52:14 +08:00
|
|
|
$row = dbFetchOne( 'show table status where Name=?', NULL, array($table) );
|
2017-05-17 02:16:32 +08:00
|
|
|
return( $row['Auto_increment'] );
|
2009-03-31 18:53:51 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function getTableDescription( $table, $asString=1 ) {
|
|
|
|
$columns = array();
|
|
|
|
foreach( dbFetchAll( "describe $table" ) as $row ) {
|
|
|
|
$desc = array(
|
|
|
|
'name' => $row['Field'],
|
|
|
|
'required' => ($row['Null']=='NO')?true:false,
|
|
|
|
'default' => $row['Default'],
|
|
|
|
'db' => $row,
|
2010-05-27 20:18:06 +08:00
|
|
|
);
|
2017-05-17 02:16:32 +08:00
|
|
|
if ( preg_match( "/^varchar\((\d+)\)$/", $row['Type'], $matches ) ) {
|
|
|
|
$desc['type'] = 'text';
|
|
|
|
$desc['typeAttrib'] = 'varchar';
|
|
|
|
$desc['maxLength'] = $matches[1];
|
|
|
|
} elseif ( preg_match( "/^(\w+)?text$/", $row['Type'], $matches ) ) {
|
|
|
|
$desc['type'] = 'text';
|
|
|
|
if (!empty($matches[1]) )
|
|
|
|
$desc['typeAttrib'] = $matches[1];
|
|
|
|
switch ( $matches[1] ) {
|
|
|
|
case 'tiny' :
|
|
|
|
$desc['maxLength'] = 255;
|
|
|
|
break;
|
|
|
|
case 'medium' :
|
|
|
|
$desc['maxLength'] = 32768;
|
|
|
|
break;
|
|
|
|
case '' :
|
|
|
|
case 'big' :
|
|
|
|
//$desc['minLength'] = -128;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
Error( "Unexpected text qualifier '".$matches[1]."' found for field '".$row['Field']."' in table '".$table."'" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} elseif ( preg_match( "/^(enum|set)\((.*)\)$/", $row['Type'], $matches ) ) {
|
|
|
|
$desc['type'] = 'text';
|
|
|
|
$desc['typeAttrib'] = $matches[1];
|
|
|
|
preg_match_all( "/'([^']+)'/", $matches[2], $matches );
|
|
|
|
$desc['values'] = $matches[1];
|
|
|
|
} elseif ( preg_match( "/^(\w+)?int\(\d+\)(?:\s+(unsigned))?$/", $row['Type'], $matches ) ) {
|
|
|
|
$desc['type'] = 'integer';
|
|
|
|
switch ( $matches[1] ) {
|
|
|
|
case 'tiny' :
|
|
|
|
$desc['minValue'] = -128;
|
|
|
|
$desc['maxValue'] = 127;
|
|
|
|
break;
|
|
|
|
case 'small' :
|
|
|
|
$desc['minValue'] = -32768;
|
|
|
|
$desc['maxValue'] = 32767;
|
|
|
|
break;
|
|
|
|
case 'medium' :
|
|
|
|
$desc['minValue'] = -8388608;
|
|
|
|
$desc['maxValue'] = 8388607;
|
|
|
|
break;
|
|
|
|
case '' :
|
|
|
|
$desc['minValue'] = -2147483648;
|
|
|
|
$desc['maxValue'] = 2147483647;
|
|
|
|
break;
|
|
|
|
case 'big' :
|
|
|
|
//$desc['minValue'] = -128;
|
|
|
|
//$desc['maxValue'] = 127;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
Error( "Unexpected integer qualifier '".$matches[1]."' found for field '".$row['Field']."' in table '".$table."'" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( !empty($matches[1]) )
|
|
|
|
$desc['typeAttrib'] = $matches[1];
|
|
|
|
if ( $desc['unsigned'] = ( isset($matches[2]) && $matches[2] == 'unsigned' ) ) {
|
|
|
|
$desc['maxValue'] += (-$desc['minValue']);
|
|
|
|
$desc['minValue'] = 0;
|
|
|
|
}
|
|
|
|
} elseif ( preg_match( "/^(?:decimal|numeric)\((\d+)(?:,(\d+))?\)(?:\s+(unsigned))?$/", $row['Type'], $matches ) ) {
|
|
|
|
$desc['type'] = 'fixed';
|
|
|
|
$desc['range'] = $matches[1];
|
|
|
|
if ( isset($matches[2]) )
|
|
|
|
$desc['precision'] = $matches[2];
|
|
|
|
else
|
|
|
|
$desc['precision'] = 0;
|
|
|
|
$desc['unsigned'] = ( isset($matches[3]) && $matches[3] == 'unsigned' );
|
|
|
|
} elseif ( preg_match( "/^(datetime|timestamp|date|time)$/", $row['Type'], $matches ) ) {
|
|
|
|
$desc['type'] = 'datetime';
|
|
|
|
switch ( $desc['typeAttrib'] = $matches[1] ) {
|
|
|
|
case 'datetime' :
|
|
|
|
case 'timestamp' :
|
|
|
|
$desc['hasDate'] = true;
|
|
|
|
$desc['hasTime'] = true;
|
|
|
|
break;
|
|
|
|
case 'date' :
|
|
|
|
$desc['hasDate'] = true;
|
|
|
|
$desc['hasTime'] = false;
|
|
|
|
break;
|
|
|
|
case 'time' :
|
|
|
|
$desc['hasDate'] = false;
|
|
|
|
$desc['hasTime'] = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Error( "Can't parse database type '".$row['Type']."' found for field '".$row['Field']."' in table '".$table."'" );
|
2010-05-27 20:18:06 +08:00
|
|
|
}
|
2017-05-17 02:16:32 +08:00
|
|
|
|
|
|
|
if ( $asString )
|
|
|
|
$columns[$row['Field']] = $desc;
|
|
|
|
else
|
|
|
|
$columns[] = $desc;
|
|
|
|
}
|
|
|
|
return( $columns );
|
2010-05-27 20:18:06 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbFetchMonitor( $mid ) {
|
2017-08-22 21:52:14 +08:00
|
|
|
return( dbFetchOne( 'select * from Monitors where Id = ?', NULL, array($mid) ) );
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbFetchGroup( $gid ) {
|
2017-08-22 21:52:14 +08:00
|
|
|
return( dbFetchOne( 'select * from Groups where Id = ?', NULL, array($gid) ) );
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2002-10-11 17:45:06 +08:00
|
|
|
?>
|