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
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
//
|
|
|
|
|
2008-07-14 21:54:50 +08:00
|
|
|
define( "DB_LOG_OFF", 0 );
|
|
|
|
define( "DB_LOG_ONLY", 1 );
|
|
|
|
define( "DB_LOG_DEBUG", 2 );
|
|
|
|
|
|
|
|
$GLOBALS['dbLogLevel'] = DB_LOG_OFF;
|
|
|
|
|
|
|
|
$GLOBALS['dbConn'] = false;
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2007-09-07 23:39:44 +08:00
|
|
|
function dbConnect()
|
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
global $dbConn;
|
|
|
|
$dbConn = mysql_pconnect( ZM_DB_HOST, ZM_DB_USER, ZM_DB_PASS ) or die( "Could not connect to database: ".mysql_error() );
|
|
|
|
mysql_select_db( ZM_DB_NAME, $dbConn ) or die( "Could not select database: ".mysql_error() );
|
2007-09-07 23:39:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dbConnect();
|
2002-10-11 17:45:06 +08:00
|
|
|
|
2008-07-14 21:54:50 +08:00
|
|
|
function dbDisconnect()
|
2005-01-03 22:44:38 +08:00
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
global $dbConn;
|
|
|
|
mysql_close( $dbConn ) or die( "Could not disconnect from database" );
|
|
|
|
}
|
2005-01-03 22:44:38 +08:00
|
|
|
|
2008-07-14 21:54:50 +08:00
|
|
|
function dbLogOff()
|
|
|
|
{
|
|
|
|
global $dbLogLevel;
|
|
|
|
$dbLogLevel = DB_LOG_OFF;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2008-07-14 21:54:50 +08:00
|
|
|
function dbLogOn()
|
2007-08-30 06:33:22 +08:00
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
global $dbLogLevel;
|
|
|
|
$dbLogLevel = DB_LOG_ONLY;
|
|
|
|
}
|
2007-08-30 06:33:22 +08:00
|
|
|
|
2008-07-14 21:54:50 +08:00
|
|
|
function dbLogDebug()
|
|
|
|
{
|
|
|
|
global $dbLogLevel;
|
|
|
|
$dbLogLevel = DB_LOG_DEBUG;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dbDebug()
|
|
|
|
{
|
|
|
|
dbLogDebug();
|
|
|
|
}
|
|
|
|
|
|
|
|
function dbLog( $sql, $update=false )
|
|
|
|
{
|
|
|
|
global $dbLogLevel;
|
|
|
|
$noExecute = $update && ($dbLogLevel >= DB_LOG_DEBUG);
|
|
|
|
if ( $dbLogLevel > DB_LOG_OFF )
|
|
|
|
error_log( "SQL-LOG: $sql".($noExecute?" (not executed)":"") );
|
|
|
|
return( $noExecute );
|
2007-08-30 06:33:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function dbError( $sql )
|
|
|
|
{
|
|
|
|
$err_ref = sprintf( "%X", rand( 0x100000, 0xffffff ) );
|
|
|
|
error_log( "SQL-ERROR($err_ref): ".$sql );
|
|
|
|
error_log( "SQL-ERROR($err_ref): ".mysql_error() );
|
2008-07-14 21:54:50 +08:00
|
|
|
die( "An error has occurred and this operation cannot continue.<br/>For full details check your web logs for the code '$err_ref'" );
|
2007-08-30 06:33:22 +08:00
|
|
|
}
|
|
|
|
|
2007-09-06 20:59:19 +08:00
|
|
|
function dbEscape( $string )
|
|
|
|
{
|
|
|
|
if ( version_compare( phpversion(), "4.3.0", "<") )
|
2008-02-11 06:12:00 +08:00
|
|
|
if ( get_magic_quotes_gpc() )
|
|
|
|
return( mysql_escape_string( stripslashes( $string ) ) );
|
|
|
|
else
|
|
|
|
return( mysql_escape_string( $string ) );
|
2007-09-06 20:59:19 +08:00
|
|
|
else
|
2008-02-11 06:12:00 +08:00
|
|
|
if ( get_magic_quotes_gpc() )
|
|
|
|
return( mysql_real_escape_string( stripslashes( $string ) ) );
|
|
|
|
else
|
|
|
|
return( mysql_real_escape_string( $string ) );
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2007-08-30 02:11:09 +08:00
|
|
|
function dbQuery( $sql )
|
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
if ( dbLog( $sql, true ) )
|
2007-08-30 02:11:09 +08:00
|
|
|
return;
|
|
|
|
if (!($result = mysql_query( $sql )))
|
2007-08-30 06:33:22 +08:00
|
|
|
dbError( $sql );
|
2007-08-30 02:11:09 +08:00
|
|
|
return( $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
function dbFetchOne( $sql, $col=false )
|
|
|
|
{
|
2007-08-30 06:33:22 +08:00
|
|
|
dbLog( $sql );
|
2007-08-30 02:11:09 +08:00
|
|
|
if (!($result = mysql_query( $sql )))
|
2007-08-30 06:33:22 +08:00
|
|
|
dbError( $sql );
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2008-07-14 21:54:50 +08:00
|
|
|
if ( $dbRow = mysql_fetch_assoc( $result ) )
|
|
|
|
return( $col?$dbRow[$col]:$dbRow );
|
|
|
|
return( false );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function dbFetchAll( $sql, $col=false )
|
|
|
|
{
|
2007-08-30 06:33:22 +08:00
|
|
|
dbLog( $sql );
|
2007-08-30 02:11:09 +08:00
|
|
|
if (!($result = mysql_query( $sql )))
|
2007-08-30 06:33:22 +08:00
|
|
|
dbError( $sql );
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2008-07-14 21:54:50 +08:00
|
|
|
$dbRows = array();
|
|
|
|
while( $dbRow = mysql_fetch_assoc( $result ) )
|
|
|
|
$dbRows[] = $col?$dbRow[$col]:$dbRow;
|
|
|
|
return( $dbRows );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2008-12-06 07:15:08 +08:00
|
|
|
function dbFetchAssoc( $sql, $indexCol, $dataCol=false )
|
|
|
|
{
|
|
|
|
dbLog( $sql );
|
|
|
|
if (!($result = mysql_query( $sql )))
|
|
|
|
dbError( $sql );
|
|
|
|
|
|
|
|
$dbRows = array();
|
|
|
|
while( $dbRow = mysql_fetch_assoc( $result ) )
|
|
|
|
$dbRows[$dbRow[$indexCol]] = $dataCol?$dbRow[$dataCol]:$dbRow;
|
|
|
|
return( $dbRows );
|
|
|
|
}
|
|
|
|
|
2007-08-30 02:11:09 +08:00
|
|
|
function dbFetch( $sql, $col=false )
|
|
|
|
{
|
|
|
|
return( dbFetchAll( $sql, $col ) );
|
|
|
|
}
|
|
|
|
|
2007-09-06 20:59:19 +08:00
|
|
|
function dbFetchNext( $result, $col=false )
|
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
if ( $dbRow = mysql_fetch_assoc( $result ) )
|
|
|
|
return( $col?$dbRow[$col]:$dbRow );
|
|
|
|
return( false );
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2007-08-30 06:33:22 +08:00
|
|
|
function dbNumRows( $sql )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
2007-08-30 06:33:22 +08:00
|
|
|
dbLog( $sql );
|
|
|
|
if (!($result = mysql_query( $sql )))
|
|
|
|
dbError( $sql );
|
|
|
|
return( mysql_num_rows( $result ) );
|
2005-01-03 22:44:38 +08:00
|
|
|
}
|
|
|
|
|
2007-09-06 20:59:19 +08:00
|
|
|
function dbInsertId()
|
|
|
|
{
|
|
|
|
return( mysql_insert_id() );
|
|
|
|
}
|
|
|
|
|
2002-10-11 17:45:06 +08:00
|
|
|
function getEnumValues( $table, $column )
|
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
$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
|
|
|
|
|
|
|
function getSetValues( $table, $column )
|
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
return( getEnumValues( $table, $column ) );
|
2003-09-23 17:52:45 +08:00
|
|
|
}
|
2005-03-14 04:41:06 +08:00
|
|
|
|
2008-07-14 21:54:50 +08:00
|
|
|
function getUniqueValues( $table, $column, $asString=1 )
|
2005-03-14 04:41:06 +08:00
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
$values = array();
|
|
|
|
$sql = "select distinct $column from $table where (not isnull($column) and $column != '') order by $column";
|
2007-09-06 20:59:19 +08:00
|
|
|
foreach( dbFetchAll( $sql ) as $row )
|
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
if ( $asString )
|
|
|
|
$values[$row[$column]] = $row[$column];
|
2007-09-06 20:59:19 +08:00
|
|
|
else
|
2008-07-14 21:54:50 +08:00
|
|
|
$values[] = $row[$column];
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
2008-07-14 21:54:50 +08:00
|
|
|
return( $values );
|
2005-03-14 04:41:06 +08:00
|
|
|
}
|
|
|
|
|
2008-07-14 21:54:50 +08:00
|
|
|
function getTableColumns( $table, $asString=1 )
|
2005-03-14 04:41:06 +08:00
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
$columns = array();
|
|
|
|
$table = dbEscape($table);
|
|
|
|
$sql = "describe $table";
|
2007-09-06 20:59:19 +08:00
|
|
|
foreach( dbFetchAll( $sql ) as $row )
|
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
if ( $asString )
|
2008-07-16 05:24:31 +08:00
|
|
|
$columns[$row['Field']] = $row['Type'];
|
2007-09-06 20:59:19 +08:00
|
|
|
else
|
2008-07-16 05:24:31 +08:00
|
|
|
$columns[] = $row['Type'];
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
2008-07-14 21:54:50 +08:00
|
|
|
return( $columns );
|
2005-03-14 04:41:06 +08:00
|
|
|
}
|
2007-09-06 20:59:19 +08:00
|
|
|
|
2009-03-31 18:53:51 +08:00
|
|
|
function getTableAutoInc( $table )
|
|
|
|
{
|
|
|
|
$sql = "show table status where Name = '".dbEscape($table)."'";
|
|
|
|
$row = dbFetchOne( $sql );
|
|
|
|
return( $row['Auto_increment'] );
|
|
|
|
}
|
|
|
|
|
2007-09-06 20:59:19 +08:00
|
|
|
function dbFetchMonitor( $mid )
|
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
return( dbFetchOne( "select * from Monitors where Id = '".dbEscape($mid)."'" ) );
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function dbFetchGroup( $gid )
|
|
|
|
{
|
2008-07-14 21:54:50 +08:00
|
|
|
return( dbFetchOne( "select * from Groups where Id = '".dbEscape($gid)."'" ) );
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2002-10-11 17:45:06 +08:00
|
|
|
?>
|