2002-10-11 17:45:06 +08:00
|
|
|
<?php
|
|
|
|
//
|
2004-01-08 19:45:57 +08:00
|
|
|
// ZoneMinder web database interface file, $Date$, $Revision$
|
2006-01-17 19:32:47 +08:00
|
|
|
// Copyright (C) 2003, 2004, 2005, 2006 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.
|
|
|
|
//
|
|
|
|
|
2007-08-30 02:11:09 +08:00
|
|
|
$db_debug = false;
|
2007-08-30 06:33:22 +08:00
|
|
|
$db_log = false;
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2007-09-07 23:39:44 +08:00
|
|
|
function dbConnect()
|
|
|
|
{
|
|
|
|
global $db_conn;
|
|
|
|
$db_conn = 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, $db_conn) or die("Could not select database: ".mysql_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
dbConnect();
|
2002-10-11 17:45:06 +08:00
|
|
|
|
2007-08-30 02:11:09 +08:00
|
|
|
function dbDebug( $sql )
|
2005-01-03 22:44:38 +08:00
|
|
|
{
|
2007-08-30 02:11:09 +08:00
|
|
|
global $db_debug;
|
2005-01-03 22:44:38 +08:00
|
|
|
|
2007-08-30 02:11:09 +08:00
|
|
|
if ( $db_debug )
|
2007-08-30 06:33:22 +08:00
|
|
|
error_log( "SQL-DEBUG: $sql" );
|
2007-08-30 02:11:09 +08:00
|
|
|
return( $db_debug );
|
|
|
|
}
|
|
|
|
|
2007-08-30 06:33:22 +08:00
|
|
|
function dbLog( $sql )
|
|
|
|
{
|
|
|
|
global $db_log;
|
|
|
|
|
|
|
|
if ( $db_log )
|
|
|
|
error_log( "SQL-LOG:$sql" );
|
|
|
|
return( $db_log );
|
|
|
|
}
|
|
|
|
|
|
|
|
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() );
|
|
|
|
die( "An error has occurred and this operation cannot continue.<br>For full details check your web logs for the code '$err_ref'" );
|
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
if ( dbDebug( $sql ) )
|
|
|
|
return;
|
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
|
|
|
return( $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
function dbFetchOne( $sql, $col=false )
|
|
|
|
{
|
|
|
|
dbDebug( $sql );
|
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
|
|
|
|
|
|
|
$db_row = mysql_fetch_assoc( $result );
|
|
|
|
return( $col?$db_row[$col]:$db_row );
|
|
|
|
}
|
|
|
|
|
|
|
|
function dbFetchAll( $sql, $col=false )
|
|
|
|
{
|
|
|
|
dbDebug( $sql );
|
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
|
|
|
|
|
|
|
$db_rows = array();
|
|
|
|
while( $db_row = mysql_fetch_assoc( $result ) )
|
|
|
|
$db_rows[] = $col?$db_row[$col]:$db_row;
|
|
|
|
return( $db_rows );
|
|
|
|
}
|
|
|
|
|
|
|
|
function dbFetch( $sql, $col=false )
|
|
|
|
{
|
|
|
|
return( dbFetchAll( $sql, $col ) );
|
|
|
|
}
|
|
|
|
|
2007-09-06 20:59:19 +08:00
|
|
|
function dbFetchNext( $result, $col=false )
|
|
|
|
{
|
|
|
|
$db_row = mysql_fetch_assoc( $result );
|
|
|
|
return( $col?$db_row[$col]:$db_row );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
dbDebug( $sql );
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
$enum_values = array();
|
2007-09-06 20:59:19 +08:00
|
|
|
$row = dbFetchOne( "DESCRIBE $table $column" );
|
2006-01-15 21:09:30 +08:00
|
|
|
preg_match_all( "/'([^']+)'/", $row['Type'], $enum_matches );
|
|
|
|
$enum_values = $enum_matches[1];
|
|
|
|
return( $enum_values );
|
2002-10-11 17:45:06 +08:00
|
|
|
}
|
2003-09-23 17:52:45 +08:00
|
|
|
|
|
|
|
function getSetValues( $table, $column )
|
|
|
|
{
|
|
|
|
return( getEnumValues( $table, $column ) );
|
|
|
|
}
|
2005-03-14 04:41:06 +08:00
|
|
|
|
|
|
|
function getUniqueValues( $table, $column, $as_string=1 )
|
|
|
|
{
|
|
|
|
$values = array();
|
2007-09-06 20:59:19 +08:00
|
|
|
$sql = "SELECT DISTINCT $column FROM $table WHERE (NOT ISNULL($column) AND $column != '') ORDER BY $column";
|
|
|
|
foreach( dbFetchAll( $sql ) as $row )
|
|
|
|
{
|
|
|
|
if ( $as_string )
|
|
|
|
$values[$row[0]] = $row[0];
|
|
|
|
else
|
|
|
|
$values = $row[0];
|
|
|
|
}
|
2005-03-14 04:41:06 +08:00
|
|
|
return( $values );
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTableColumns( $table, $as_string=1 )
|
|
|
|
{
|
|
|
|
$columns = array();
|
2007-09-06 20:59:19 +08:00
|
|
|
$sql = "DESCRIBE $table";
|
|
|
|
foreach( dbFetchAll( $sql ) as $row )
|
|
|
|
{
|
|
|
|
if ( $as_string )
|
|
|
|
$columns[$row[Field]] = $row[Type];
|
|
|
|
else
|
|
|
|
$columns[] = $row[Type];
|
|
|
|
}
|
2005-03-14 04:41:06 +08:00
|
|
|
return( $columns );
|
|
|
|
}
|
2007-09-06 20:59:19 +08:00
|
|
|
|
|
|
|
function dbFetchMonitor( $mid )
|
|
|
|
{
|
|
|
|
return( dbFetchOne( "select * from Monitors where Id = '$mid'" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
function dbFetchGroup( $gid )
|
|
|
|
{
|
|
|
|
return( dbFetchOne( "select * from Groups where Id = '$gid'" ) );
|
|
|
|
}
|
|
|
|
|
2002-10-11 17:45:06 +08:00
|
|
|
?>
|