diff --git a/web/zm_actions.php b/web/zm_actions.php index d2358a1ee..c7ac97b7c 100644 --- a/web/zm_actions.php +++ b/web/zm_actions.php @@ -157,11 +157,7 @@ if ( !empty($action) ) { if ( $action == "control" ) { - $result = mysql_query( "select C.*,M.* from Monitors as M inner join Controls as C on (M.ControlId = C.Id) where M.Id = '$mid'" ); - if ( !$result ) - die( mysql_error() ); - $monitor = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $monitor = dbFetchOne( "select C.*,M.* from Monitors as M inner join Controls as C on (M.ControlId = C.Id) where M.Id = '$mid'" ); $ctrl_command = "zmcontrol.pl"; if ( !preg_match( '/^\//', $ctrl_command ) ) @@ -825,21 +821,14 @@ if ( !empty($action) ) { if ( canEdit( 'Control' ) ) { - $sql = "select * from ControlPresets where MonitorId = '".$monitor['Id']."' and Preset = '".$preset."'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $row = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $row = dbFetchOne( "select * from ControlPresets where MonitorId = '".$monitor['Id']."' and Preset = '".$preset."'" ); if ( $new_label != $row['Label'] ) { if ( $new_label ) $sql = "replace into ControlPresets ( MonitorId, Preset, Label ) values ( '".$monitor['Id']."', '".$preset."', '".addslashes($new_label)."' )"; else $sql = "delete from ControlPresets where MonitorId = '".$monitor['Id']."' and Preset = '".$preset."'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( $sql ); $refresh_parent = true; } } @@ -893,10 +882,7 @@ if ( !empty($action) ) $zmu_command = getZmuCommand( " -m $mid -B$new_brightness -C$new_contrast -H$new_hue -O$new_colour" ); $zmu_output = exec( escapeshellcmd( $zmu_command ) ); list( $brightness, $contrast, $hue, $colour ) = split( ' ', $zmu_output ); - $sql = "update Monitors set Brightness = '$brightness', Contrast = '$contrast', Hue = '$hue', Colour = '$colour' where Id = '$mid'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "update Monitors set Brightness = '$brightness', Contrast = '$contrast', Hue = '$hue', Colour = '$colour' where Id = '$mid'" ); } } @@ -907,11 +893,7 @@ if ( !empty($action) ) { if ( !empty($cid) ) { - $result = mysql_query( "select * from Controls where Id = '$cid'" ); - if ( !$result ) - die( mysql_error() ); - $control = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $control = dbFetchOne( "select * from Controls where Id = '$cid'" ); } else { @@ -942,11 +924,8 @@ if ( !empty($action) ) } else { - $sql = "insert into Controls set ".implode( ", ", $changes ); - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $cid = mysql_insert_id(); + dbQuery( "insert into Controls set ".implode( ", ", $changes ) ); + $cid = dbInsertId(); } $refresh_parent = true; } @@ -970,12 +949,7 @@ if ( !empty($action) ) { if ( $action == "function" ) { - $sql = "select * from Monitors where Id = '$mid'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $monitor = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + dbQuery( "select * from Monitors where Id = '$mid'" ); $old_function = $monitor['Function']; $old_enabled = $monitor['Enabled']; @@ -997,19 +971,11 @@ if ( !empty($action) ) } elseif ( $action == "zone" && isset( $zid ) ) { - $result = mysql_query( "select * from Monitors where Id = '$mid'" ); - if ( !$result ) - die( mysql_error() ); - $monitor = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $monitor = dbFetchOne( "select * from Monitors where Id = '$mid'" ); if ( $zid > 0 ) { - $result = mysql_query( "select * from Zones where MonitorId = '$mid' and Id = '$zid'" ); - if ( !$result ) - die( mysql_error() ); - $zone = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $zone = dbFetchOne( "select * from Zones where MonitorId = '$mid' and Id = '$zid'" ); } else { @@ -1059,25 +1025,11 @@ if ( !empty($action) ) } elseif ( $action == "sequence" && isset($smid) ) { - $result = mysql_query( "select * from Monitors where Id = '$mid'" ); - if ( !$result ) - die( mysql_error() ); - $monitor = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); - $result = mysql_query( "select * from Monitors where Id = '$smid'" ); - if ( !$result ) - die( mysql_error() ); - $smonitor = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $monitor = dbFetchOne( "select * from Monitors where Id = '$mid'" ); + $smonitor = dbFetchOne( "select * from Monitors where Id = '$smid'" ); - $sql = "update Monitors set Sequence = '".$smonitor['Sequence']."' where Id = '".$monitor['Id']."'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $sql = "update Monitors set Sequence = '".$monitor['Sequence']."' where Id = '".$smonitor['Id']."'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "update Monitors set Sequence = '".$smonitor['Sequence']."' where Id = '".$monitor['Id']."'" ); + dbQuery( "update Monitors set Sequence = '".$monitor['Sequence']."' where Id = '".$smonitor['Id']."'" ); $refresh_parent = true; fixSequences(); @@ -1089,14 +1041,13 @@ if ( !empty($action) ) $deleted_zid = 0; foreach( $mark_zids as $mark_zid ) { - $result = mysql_query( "delete from Zones where MonitorId = '$mid' && Id = '$mark_zid'" ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "delete from Zones where MonitorId = '$mid' && Id = '$mark_zid'" ); $deleted_zid = 1; } if ( $deleted_zid ) { - if ( $cookies ) session_write_close(); + if ( $cookies ) + session_write_close(); if ( daemonCheck() ) { zmaControl( $mid, "restart" ); @@ -1114,22 +1065,13 @@ if ( !empty($action) ) { if ( !empty($mid) ) { - $result = mysql_query( "select * from Monitors where Id = '$mid'" ); - if ( !$result ) - die( mysql_error() ); - $monitor = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $monitor = dbFetchOne( "select * from Monitors where Id = '$mid'" ); if ( ZM_OPT_X10 ) { - $result = mysql_query( "select * from TriggersX10 where MonitorId = '$mid'" ); - if ( !$result ) - die( mysql_error() ); - if ( !($x10_monitor = mysql_fetch_assoc( $result )) ) - { + $x10_monitor = dbFetchOne( "select * from TriggersX10 where MonitorId = '$mid'" ); + if ( !$x10_monitor ) $x10_monitor = array(); - } - mysql_free_result( $result ); } } else @@ -1169,15 +1111,7 @@ if ( !empty($action) ) $old_h = $monitor['Height']; $old_a = $old_w * $old_h; - $result = mysql_query( "select * from Zones where MonitorId = '$mid'" ); - if ( !$result ) - die( mysql_error() ); - $zones = array(); - while ( $zone = mysql_fetch_assoc( $result ) ) - { - $zones[] = $zone; - } - mysql_free_result( $result ); + $zones = dbFetchAll( "select * from Zones where MonitorId = '$mid'" ); foreach ( $zones as $zone ) { $new_zone = $zone; @@ -1200,33 +1134,20 @@ if ( !empty($action) ) if ( count( $changes ) ) { - $sql = "update Zones set ".implode( ", ", $changes )." where MonitorId = '$mid' and Id = '".$zone['Id']."'"; - //echo "$sql"; - dbQuery( $sql ); + dbQuery( "update Zones set ".implode( ", ", $changes )." where MonitorId = '$mid' and Id = '".$zone['Id']."'" ); } } } } elseif ( !$user['MonitorIds'] ) { - $sql = "select max(Sequence) as MaxSequence from Monitors"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $row = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); - $changes[] = "Sequence = ".($row['MaxSequence']+1); + $max_seq = dbFetchOne( "select max(Sequence) as MaxSequence from Monitors", "MaxSequence" ); + $changes[] = "Sequence = ".($max_seq+1); - $sql = "insert into Monitors set ".implode( ", ", $changes ); - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $mid = mysql_insert_id(); + dbQuery( "insert into Monitors set ".implode( ", ", $changes ) ); + $mid = dbInsertId(); $zone_area = $new_monitor['Width'] * $new_monitor['Height']; - $sql = "insert into Zones set MonitorId = $mid, Name = 'All', Type = 'Active', Units = 'Percent', NumCoords = 4, Coords = '".sprintf( "%d,%d %d,%d %d,%d %d,%d", 0, 0, $new_monitor['Width']-1, 0, $new_monitor['Width']-1, $new_monitor['Height']-1, 0, $new_monitor['Height']-1 )."', Area = ".$zone_area.", AlarmRGB = 0xff0000, CheckMethod = 'Blobs', MinPixelThreshold = 25, MinAlarmPixels = ".intval(($zone_area*3)/100).", MaxAlarmPixels = ".intval(($zone_area*75)/100).", FilterX = 3, FilterY = 3, MinFilterPixels = ".intval(($zone_area*3)/100).", MaxFilterPixels = ".intval(($zone_area*75)/100).", MinBlobPixels = ".intval(($zone_area*2)/100).", MinBlobs = 1"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "insert into Zones set MonitorId = $mid, Name = 'All', Type = 'Active', Units = 'Percent', NumCoords = 4, Coords = '".sprintf( "%d,%d %d,%d %d,%d %d,%d", 0, 0, $new_monitor['Width']-1, 0, $new_monitor['Width']-1, $new_monitor['Height']-1, 0, $new_monitor['Height']-1 )."', Area = ".$zone_area.", AlarmRGB = 0xff0000, CheckMethod = 'Blobs', MinPixelThreshold = 25, MinAlarmPixels = ".intval(($zone_area*3)/100).", MaxAlarmPixels = ".intval(($zone_area*75)/100).", FilterX = 3, FilterY = 3, MinFilterPixels = ".intval(($zone_area*3)/100).", MaxFilterPixels = ".intval(($zone_area*75)/100).", MinBlobPixels = ".intval(($zone_area*2)/100).", MinBlobs = 1" ); //$view = 'none'; mkdir( ZM_DIR_EVENTS."/".$mid, 0755 ); chdir( ZM_DIR_EVENTS ); @@ -1244,26 +1165,17 @@ if ( !empty($action) ) { if ( $x10_monitor && $new_x10_monitor ) { - $sql = "update TriggersX10 set ".implode( ", ", $x10_changes )." where MonitorId = '$mid'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "update TriggersX10 set ".implode( ", ", $x10_changes )." where MonitorId = '$mid'" ); } elseif ( !$user['MonitorIds'] ) { if ( !$x10_monitor ) { - $sql = "insert into TriggersX10 set MonitorId = '$mid', ".implode( ", ", $x10_changes ); - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "insert into TriggersX10 set MonitorId = '$mid', ".implode( ", ", $x10_changes ) ); } else { - $sql = "delete from TriggersX10 where MonitorId = '$mid'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "delete from TriggersX10 where MonitorId = '$mid'" ); } } $restart = true; @@ -1272,13 +1184,10 @@ if ( !empty($action) ) if ( $restart ) { - $result = mysql_query( "select * from Monitors where Id = '$mid'" ); - if ( !$result ) - die( mysql_error() ); - $monitor = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $monitor = dbFetchOne( "select * from Monitors where Id = '$mid'" ); fixDevices(); - if ( $cookies ) session_write_close(); + if ( $cookies ) + session_write_close(); if ( daemonCheck() ) { zmcControl( $monitor, "restart" ); @@ -1301,26 +1210,13 @@ if ( !empty($action) ) zmcControl( $monitor, "stop" ); $sql = "select * from Monitors where Id = '$mark_mid'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - if ( !($monitor = mysql_fetch_assoc( $result )) ) + if ( !($monitor = dbFetchOne( $sql )) ) { continue; } - mysql_free_result( $result ); $sql = "select Id from Events where MonitorId = '$mark_mid'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - - $mark_eids = array(); - while( $row = mysql_fetch_assoc( $result ) ) - { - $mark_eids[] = $row['Id']; - } - mysql_free_result( $result ); + $mark_eids = dbFetchAll( $sql, 'Id' ); foreach( $mark_eids as $mark_eid ) { deleteEvent( $mark_eid ); @@ -1328,18 +1224,12 @@ if ( !empty($action) ) unlink( ZM_DIR_EVENTS."/".$monitor['Name'] ); system( "rm -rf ".ZM_DIR_EVENTS."/".$monitor['Id'] ); - $result = mysql_query( "delete from Zones where MonitorId = '$mark_mid'" ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "delete from Zones where MonitorId = '$mark_mid'" ); if ( ZM_OPT_X10 ) { - $result = mysql_query( "delete from TriggersX10 where MonitorId = '$mark_mid'" ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "delete from TriggersX10 where MonitorId = '$mark_mid'" ); } - $result = mysql_query( "delete from Monitors where Id = '$mark_mid'" ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "delete from Monitors where Id = '$mark_mid'" ); fixSequences(); } @@ -1415,10 +1305,7 @@ if ( !empty($action) ) } case 'ignore' : { - $sql = "update Config set Value = '".ZM_DYN_LAST_VERSION."' where Name = 'ZM_DYN_CURR_VERSION'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "update Config set Value = '".ZM_DYN_LAST_VERSION."' where Name = 'ZM_DYN_CURR_VERSION'" ); break; } case 'hour' : @@ -1438,18 +1325,12 @@ if ( !empty($action) ) { $next_reminder += 7*24*60*60; } - $sql = "update Config set Value = '".$next_reminder."' where Name = 'ZM_DYN_NEXT_REMINDER'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "update Config set Value = '".$next_reminder."' where Name = 'ZM_DYN_NEXT_REMINDER'" ); break; } case 'never' : { - $sql = "update Config set Value = '0' where Name = 'ZM_CHECK_FOR_UPDATES'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "update Config set Value = '0' where Name = 'ZM_CHECK_FOR_UPDATES'" ); break; } } @@ -1485,19 +1366,13 @@ if ( !empty($action) ) { $next_reminder += 30*24*60*60; } - $sql = "update Config set Value = '".$next_reminder."' where Name = 'ZM_DYN_DONATE_REMINDER_TIME'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "update Config set Value = '".$next_reminder."' where Name = 'ZM_DYN_DONATE_REMINDER_TIME'" ); break; } case 'never' : case 'already' : { - $sql = "update Config set Value = '0' where Name = 'ZM_DYN_SHOW_DONATE_REMINDER'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "update Config set Value = '0' where Name = 'ZM_DYN_SHOW_DONATE_REMINDER'" ); break; } } @@ -1518,11 +1393,7 @@ if ( !empty($action) ) } if ( $value['Value'] != $new_config[$name] ) { - $sql = "update Config set Value = '".$new_config[$name]."' where Name = '".$name."'"; - //echo $sql; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "update Config set Value = '".$new_config[$name]."' where Name = '".$name."'" ); $changed = true; } } @@ -1557,11 +1428,7 @@ if ( !empty($action) ) { if ( !empty($uid) ) { - $result = mysql_query( "select * from Users where Id = '$uid'" ); - if ( !$result ) - die( mysql_error() ); - $db_user = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $db_user = dbFetchOne( "select * from Users where Id = '$uid'" ); } else { @@ -1585,9 +1452,7 @@ if ( !empty($action) ) { $sql = "insert into Users set ".implode( ", ", $changes ); } - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( $sql ); $refresh_parent = true; if ( $db_user['Username'] == $user['Username'] ) { @@ -1610,16 +1475,11 @@ if ( !empty($action) ) if ( $run_state || $new_state ) { $sql = "select Id,Function,Enabled from Monitors order by Id"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $definitions = array(); - while( $monitor = mysql_fetch_assoc( $result ) ) + foreach( dbFetchAll( $sql ) as $monitor ) { $definitions[] = $monitor['Id'].":".$monitor['Function'].":".$monitor['Enabled']; } - mysql_free_result( $result ); $definition = join( ',', $definitions ); if ( $new_state ) $run_state = $new_state; @@ -1679,9 +1539,7 @@ if ( !empty($action) ) { foreach( $mark_eids as $mark_eid ) { - $result = mysql_query( "update Events set LearnState = '$learn_state' where Id = '$mark_eid'" ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "update Events set LearnState = '$learn_state' where Id = '$mark_eid'" ); } } } diff --git a/web/zm_control_funcs.php b/web/zm_control_funcs.php index 512374ae8..d7a7f00e3 100644 --- a/web/zm_control_funcs.php +++ b/web/zm_control_funcs.php @@ -429,15 +429,11 @@ function controlPreset( command ) For full details check your web logs for the code '$err_ref'" ); } +function dbEscape( $string ) +{ + if ( version_compare( phpversion(), "4.3.0", "<") ) + return( mysql_escape_string( $string ) ); + else + return( mysql_real_escape_string( $string ) ); +} + function dbQuery( $sql ) { if ( dbDebug( $sql ) ) @@ -91,6 +99,12 @@ function dbFetch( $sql, $col=false ) return( dbFetchAll( $sql, $col ) ); } +function dbFetchNext( $result, $col=false ) +{ + $db_row = mysql_fetch_assoc( $result ); + return( $col?$db_row[$col]:$db_row ); +} + function dbNumRows( $sql ) { dbDebug( $sql ); @@ -100,14 +114,15 @@ function dbNumRows( $sql ) return( mysql_num_rows( $result ) ); } +function dbInsertId() +{ + return( mysql_insert_id() ); +} + function getEnumValues( $table, $column ) { $enum_values = array(); - $result = mysql_query( "DESCRIBE $table $column" ); - if ( !$result ) - die( mysql_error() ); - $row = mysql_fetch_assoc($result); - mysql_free_result( $result ); + $row = dbFetchOne( "DESCRIBE $table $column" ); preg_match_all( "/'([^']+)'/", $row['Type'], $enum_matches ); $enum_values = $enum_matches[1]; return( $enum_values ); @@ -121,44 +136,39 @@ function getSetValues( $table, $column ) function getUniqueValues( $table, $column, $as_string=1 ) { $values = array(); - $result = mysql_query( "SELECT DISTINCT $column FROM $table WHERE (NOT ISNULL($column) AND $column != '') ORDER BY $column" ); - if ( $result ) - { - while ( $row = mysql_fetch_array($result) ) - { - if ( $as_string ) - $values[$row[0]] = $row[0]; - else - $values = $row[0]; - } - mysql_free_result( $result ); - } - else - { - echo mysql_error(); - } + $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]; + } return( $values ); } function getTableColumns( $table, $as_string=1 ) { $columns = array(); - $result = mysql_query( "DESCRIBE $table" ); - if ( $result ) - { - while( $row = mysql_fetch_assoc($result) ) - { - if ( $as_string ) - $columns[$row[Field]] = $row[Type]; - else - $columns[] = $row[Type]; - } - mysql_free_result( $result ); - } - else - { - echo mysql_error(); - } + $sql = "DESCRIBE $table"; + foreach( dbFetchAll( $sql ) as $row ) + { + if ( $as_string ) + $columns[$row[Field]] = $row[Type]; + else + $columns[] = $row[Type]; + } return( $columns ); } + +function dbFetchMonitor( $mid ) +{ + return( dbFetchOne( "select * from Monitors where Id = '$mid'" ) ); +} + +function dbFetchGroup( $gid ) +{ + return( dbFetchOne( "select * from Groups where Id = '$gid'" ) ); +} + ?> diff --git a/web/zm_export_funcs.php b/web/zm_export_funcs.php index f7cd2b0ee..363cff0d9 100644 --- a/web/zm_export_funcs.php +++ b/web/zm_export_funcs.php @@ -99,14 +99,7 @@ function exportEventFrames( $event ) global $zmSlangYes, $zmSlangNo, $zmSlangNoFramesRecorded; $sql = "select *, unix_timestamp( TimeStamp ) as UnixTimeStamp from Frames where EventID = '".$event['Id']."' order by FrameId"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - while ( $row = mysql_fetch_assoc( $result ) ) - { - $frames[] = $row; - } - mysql_free_result( $result ); + $frames = dbFetchAll( $sql ); ob_start(); exportHeader( $zmSlangFrames." ".$event['Id'] ); @@ -188,10 +181,7 @@ function exportFileList( $eid ) if ( canView( 'Events' ) && $eid ) { $sql = "select E.Id,E.MonitorId,M.Name As MonitorName,M.Width,M.Height,E.Name,E.Cause,E.Notes,E.StartTime,E.Length,E.Frames,E.AlarmFrames,E.TotScore,E.AvgScore,E.MaxScore,E.Archived from Monitors as M inner join Events as E on (M.Id = E.MonitorId) where E.Id = '$eid'"; - if ( !($result = mysql_query( $sql )) ) - die( mysql_error() ); - $event = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $event = dbFetchOne( $sql ); $event_path = getEventPath( $event ); $files = array(); diff --git a/web/zm_funcs.php b/web/zm_funcs.php index ebc89739b..9f0629e74 100644 --- a/web/zm_funcs.php +++ b/web/zm_funcs.php @@ -37,28 +37,17 @@ function userLogin( $username, $password="" ) global $_SESSION, $_SERVER; } - if ( version_compare( phpversion(), "4.3.0", "<") ) - { - $mysql_username = mysql_escape_string($username); - $mysql_password = mysql_escape_string($password); - } - else - { - $mysql_username = mysql_real_escape_string($username); - $mysql_password = mysql_real_escape_string($password); - } + $db_username = dbEscape($username); + $db_password = dbEscape($password); if ( ZM_AUTH_TYPE == "builtin" ) { - $sql = "select * from Users where Username = '$mysql_username' and Password = password('$mysql_password') and Enabled = 1"; + $sql = "select * from Users where Username = '$db_username' and Password = password('$db_password') and Enabled = 1"; } else { - $sql = "select * from Users where Username = '$mysql_username' and Enabled = 1"; + $sql = "select * from Users where Username = '$db_username' and Enabled = 1"; } - $result = mysql_query( $sql ); - if ( !$result ) - echo mysql_error(); $_SESSION['username'] = $username; if ( ZM_AUTH_RELAY == "plain" ) { @@ -66,7 +55,7 @@ function userLogin( $username, $password="" ) $_SESSION['password'] = $password; } $_SESSION['remote_addr'] = $_SERVER['REMOTE_ADDR']; // To help prevent session hijacking - if ( $db_user = mysql_fetch_assoc( $result ) ) + if ( $db_user = dbFetchOne( $sql ) ) { $_SESSION['user'] = $user = $db_user; if ( ZM_AUTH_TYPE == "builtin" ) @@ -78,8 +67,8 @@ function userLogin( $username, $password="" ) { unset( $user ); } - mysql_free_result( $result ); - if ( $cookies ) session_write_close(); + if ( $cookies ) + session_write_close(); } function userLogout() @@ -461,17 +450,11 @@ function deleteEvent( $eid, $mid=false ) $mid = '*'; if ( $user['Events'] == 'Edit' && $eid ) { - $result = mysql_query( "delete from Events where Id = '$eid'" ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "delete from Events where Id = '$eid'" ); if ( !ZM_OPT_FAST_DELETE ) { - $result = mysql_query( "delete from Stats where EventId = '$eid'" ); - if ( !$result ) - die( mysql_error() ); - $result = mysql_query( "delete from Frames where EventId = '$eid'" ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "delete from Stats where EventId = '$eid'" ); + dbQuery( "delete from Frames where EventId = '$eid'" ); if ( ZM_USE_DEEP_STORAGE ) { error_log( "Globbing ".ZM_DIR_EVENTS.'/'.$mid.'/*/*/*/.'.$eid ); @@ -802,10 +785,7 @@ function zmcControl( $monitor, $mode=false ) $sql = "select count(if(Function!='None',1,NULL)) as ActiveCount from Monitors where Id = '".$monitor['Id']."'"; $zmc_args = "-m ".$monitor['Id']; } - $result = mysql_query( $sql ); - if ( !$result ) - echo mysql_error(); - $row = mysql_fetch_assoc( $result ); + $row = dbFetchOne( $sql ); $active_count = $row['ActiveCount']; if ( !$active_count ) @@ -827,10 +807,7 @@ function zmaControl( $monitor, $mode=false ) if ( !is_array( $monitor ) ) { $sql = "select Id,Function,Enabled from Monitors where Id = '$monitor'"; - $result = mysql_query( $sql ); - if ( !$result ) - echo mysql_error(); - $monitor = mysql_fetch_assoc( $result ); + $monitor = dbFetchOne( $sql ); } switch ( $monitor['Function'] ) { @@ -1046,10 +1023,7 @@ function getImageSrc( $event, $frame, $scale, $capture_only=false, $overwrite=fa function createListThumbnail( $event, $overwrite=false ) { $sql = "select * from Frames where EventId = '".$event['Id']."' and Score = '".$event['MaxScore']."' order by FrameId limit 0,1"; - if ( !($result = mysql_query( $sql )) ) - die( mysql_error() ); - $frame = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $frame = dbFetchOne( $frame ); $frame_id = $frame['FrameId']; if ( ZM_WEB_LIST_THUMB_WIDTH ) @@ -1110,9 +1084,7 @@ function executeFilter( $filter ) { $command = ZM_PATH_BIN."/zmfilter.pl --filter ".$filter; $result = exec( $command, $output, $status ); - $result = mysql_query( "delete from Filters where Name like '_TempFilter%'" ); - if ( !$result ) - die( mysql_error() ); + dbQuery( "delete from Filters where Name like '_TempFilter%'" ); return( $status ); } @@ -1437,22 +1409,16 @@ function verNum( $version ) function fixSequences() { $sql = "select * from Monitors order by Sequence asc, Id asc"; - $result = mysql_query( $sql ); - if ( !$result ) - echo mysql_error(); $sequence = 1; - while ( $monitor = mysql_fetch_assoc( $result ) ) + foreach( dbFetchAll( $monitors ) as $monitor ) { if ( $monitor['Sequence'] != $sequence ) { $sql2 = "update Monitors set Sequence = '".$sequence."' where Id = '".$monitor['Id']."'"; - $result2 = mysql_query( $sql2 ); - if ( !$result2 ) - echo mysql_error(); + dbQuery( $sql2 ); } $sequence++; } - mysql_free_result( $result ); } function firstSet() @@ -1823,10 +1789,7 @@ function monitorIdsToNames( $ids ) if ( !$mITN_monitors ) { $sql = "select Id, Name from Monitors"; - $result = mysql_query( $sql ); - if ( !$result ) - echo mysql_error(); - while ( $monitor = mysql_fetch_assoc( $result ) ) + foreach( dbFetchAll( $sql ) as $monitor ) { $mITN_monitors[$monitor['Id']] = $monitor; } diff --git a/web/zm_html_view_control.php b/web/zm_html_view_control.php index b208865ee..78a5e1cf3 100644 --- a/web/zm_html_view_control.php +++ b/web/zm_html_view_control.php @@ -24,11 +24,7 @@ if ( !canView( 'Control' ) ) return; } -$result = mysql_query( "select * from Monitors where Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchOne( "select * from Monitors where Id = '$mid'" ); ?> diff --git a/web/zm_html_view_controlcap.php b/web/zm_html_view_controlcap.php index 924272a78..a9fabc2c9 100644 --- a/web/zm_html_view_controlcap.php +++ b/web/zm_html_view_controlcap.php @@ -40,11 +40,7 @@ if ( !isset($tab) ) if ( !empty($cid) ) { - $result = mysql_query( "select * from Controls where Id = '$cid'" ); - if ( !$result ) - die( mysql_error() ); - $control = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $control = dbFetchOne( "select * from Controls where Id = '$cid'" ); } else { diff --git a/web/zm_html_view_controlcaps.php b/web/zm_html_view_controlcaps.php index 5c09cb772..c2e715a1a 100644 --- a/web/zm_html_view_controlcaps.php +++ b/web/zm_html_view_controlcaps.php @@ -25,15 +25,7 @@ if ( !canView( 'Control' ) ) } $sql = "select * from Controls order by Id"; -$result = mysql_query( $sql ); -if ( !$result ) - echo mysql_error(); -$controls = array(); -while( $row = mysql_fetch_assoc( $result ) ) -{ - $controls[] = $row; -} -mysql_free_result( $result ); +$controls = dbFetchAll( $sql ); ?> diff --git a/web/zm_html_view_controlmenu.php b/web/zm_html_view_controlmenu.php index 44e5101a8..8953c9276 100644 --- a/web/zm_html_view_controlmenu.php +++ b/web/zm_html_view_controlmenu.php @@ -27,19 +27,12 @@ if ( !canView( 'Control' ) ) if ( $group ) { $sql = "select * from Groups where Id = '$group'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $row = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $row = dbFetchOne( $sql ); $group_sql = "and find_in_set( Id, '".$row['MonitorIds']."' )"; } $sql = "select * from Monitors where Function != 'None' and Controllable = 1 $group_sql order by Sequence"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); $mids = array(); -while( $row = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $row ) { if ( !visibleMonitor( $row['Id'] ) ) { @@ -47,7 +40,6 @@ while( $row = mysql_fetch_assoc( $result ) ) } $mids[$row['Id']] = $row['Name']; } -mysql_free_result( $result ); ?> diff --git a/web/zm_html_view_controlpanel.php b/web/zm_html_view_controlpanel.php index ac1c5ed52..5dd8456ae 100644 --- a/web/zm_html_view_controlpanel.php +++ b/web/zm_html_view_controlpanel.php @@ -27,11 +27,7 @@ if ( !canView( 'Control' ) ) require_once( 'zm_control_funcs.php' ); $sql = "select C.*,M.* from Monitors as M inner join Controls as C on (M.ControlId = C.Id ) where M.Id = '$mid'"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchOne( $sql ); $cmds = getControlCommands( $monitor ); diff --git a/web/zm_html_view_controlpreset.php b/web/zm_html_view_controlpreset.php index 1a0bdb657..018ff78fe 100644 --- a/web/zm_html_view_controlpreset.php +++ b/web/zm_html_view_controlpreset.php @@ -24,22 +24,14 @@ if ( !canEdit( 'Monitors' ) ) return; } -$result = mysql_query( "select C.*,M.* from Monitors as M inner join Controls as C on (M.ControlId = C.Id ) where M.Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchOne( "select C.*,M.* from Monitors as M inner join Controls as C on (M.ControlId = C.Id ) where M.Id = '$mid'" ); $sql = "select * from ControlPresets where MonitorId = '".$monitor['Id']."'"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); $labels = array(); -while( $row = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $row ) { $labels[$row['Preset']] = $row['Label']; } -mysql_free_result( $result ); $presets = array(); for ( $i = 1; $i <= $monitor['NumPresets']; $i++ ) diff --git a/web/zm_html_view_cycle.php b/web/zm_html_view_cycle.php index 2592fa554..e17030167 100644 --- a/web/zm_html_view_cycle.php +++ b/web/zm_html_view_cycle.php @@ -34,21 +34,14 @@ if ( empty($mode) ) if ( $group ) { $sql = "select * from Groups where Id = '$group'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $row = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $row = dbFetchOne( $sql ); $group_sql = "and find_in_set( Id, '".$row['MonitorIds']."' )"; } $sql = "select * from Monitors where Function != 'None' $group_sql order by Sequence"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); $monitors = array(); $mon_idx = 0; -while( $row = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $row ) { if ( !visibleMonitor( $row['Id'] ) ) { @@ -60,7 +53,6 @@ while( $row = mysql_fetch_assoc( $result ) ) $row['ScaledHeight'] = reScale( $monitor['Height'], $row['DefaultScale'], ZM_WEB_DEFAULT_SCALE ); $monitors[] = $row; } -mysql_free_result( $result ); $monitor = $monitors[$mon_idx]; $next_mid = $mon_idx==(count($monitors)-1)?$monitors[0]['Id']:$monitors[$mon_idx+1]['Id']; diff --git a/web/zm_html_view_device.php b/web/zm_html_view_device.php index b9e1f6acf..170cb0eaa 100644 --- a/web/zm_html_view_device.php +++ b/web/zm_html_view_device.php @@ -26,11 +26,7 @@ if ( !canEdit( 'Devices' ) ) if ( $did ) { $sql = "select * from Devices where Id = '$did'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $new_device = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $new_device = dbFetchOne( $sql ); } else { diff --git a/web/zm_html_view_devices.php b/web/zm_html_view_devices.php index 070e8bd2e..079de5706 100644 --- a/web/zm_html_view_devices.php +++ b/web/zm_html_view_devices.php @@ -25,16 +25,12 @@ if ( !canView( 'Devices' ) ) } $sql = "select * from Devices where Type = 'X10' order by Name"; -$result = mysql_query( $sql ); -if ( !$result ) - echo mysql_error(); $devices = array(); -while( $row = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $row ) { $row['Status'] = getDeviceStatusX10( $row['KeyString'] ); $devices[] = $row; } -mysql_free_result( $result ); ?> diff --git a/web/zm_html_view_event.php b/web/zm_html_view_event.php index 94fc5c7d6..b5658611e 100644 --- a/web/zm_html_view_event.php +++ b/web/zm_html_view_event.php @@ -41,42 +41,33 @@ else } $sql = "select E.*,M.Name as MonitorName,M.Width,M.Height,M.DefaultRate,M.DefaultScale from Events as E inner join Monitors as M on E.MonitorId = M.Id where E.Id = '$eid'$mid_sql"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$event = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$event = dbFetchOne( $sql ); parseSort(); parseFilter( $filter ); $sql = "select E.* from Events as E inner join Monitors as M on E.MonitorId = M.Id where $sort_column ".($sort_order=='asc'?'<=':'>=')." '".$event[preg_replace( '/^.*\./', '', $sort_column )]."'$filter_sql$mid_sql order by $sort_column ".($sort_order=='asc'?'desc':'asc'); -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -while ( $row = mysql_fetch_assoc( $result ) ) +$result = dbQuery( $sql ); +foreach( dbFetchAll( $sql ) as $row ) +while ( $row = dbFetchNext( $result ) ) { if ( $row['Id'] == $eid ) { - $prev_event = mysql_fetch_assoc( $result ); + $prev_event = dbFetchNext( $result ); break; } } -mysql_free_result( $result ); $sql = "select E.* from Events as E inner join Monitors as M on E.MonitorId = M.Id where $sort_column ".($sort_order=='asc'?'>=':'<=')." '".$event[preg_replace( '/^.*\./', '', $sort_column )]."'$filter_sql$mid_sql order by $sort_column $sort_order"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -while ( $row = mysql_fetch_assoc( $result ) ) +$result = dbQuery( $sql ); +while ( $row = dbFetchNext( $result ) ) { if ( $row['Id'] == $eid ) { - $next_event = mysql_fetch_assoc( $result ); + $next_event = dbFetchNext( $result ); break; } } -mysql_free_result( $result ); if ( !isset( $rate ) ) $rate = reScale( RATE_BASE, $event['DefaultRate'], ZM_WEB_DEFAULT_RATE ); @@ -93,11 +84,7 @@ $paged = $event['Frames'] > $frames_per_page; if ( $mode == "stream" ) { $sql = "select max(Delta)-min(Delta) as Duration from Frames where EventId = '$eid'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $frame_data = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $frame_data = dbFetchOne( $sql ); $frame_data['RealDuration'] = ($frame_data['Duration']*RATE_BASE)/$rate; $panel_init_color = '#eeeeee'; @@ -461,15 +448,11 @@ else $sql .= " order by FrameId"; if ( $paged && !empty($page) ) $sql .= " limit $lo_frame_id, ".($hi_frame_id-$lo_frame_id); - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); $frames = array(); - while( $frame = mysql_fetch_assoc( $result ) ) + foreach( dbFetchAll( $sql ) as $frame ) { $frames[$frame['FrameId']] = $frame; } - mysql_free_result( $result ); ?>
diff --git a/web/zm_html_view_frame.php b/web/zm_html_view_frame.php index d29e81c82..3a7df57ad 100644 --- a/web/zm_html_view_frame.php +++ b/web/zm_html_view_frame.php @@ -24,30 +24,19 @@ if ( !canView( 'Events' ) ) return; } $sql = "select E.*,M.Name as MonitorName,M.Width,M.Height,M.DefaultScale from Events as E inner join Monitors as M on E.MonitorId = M.Id where E.Id = '$eid'"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$event = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$event = dbFetchOne( $sql ); if ( $fid ) { - $result = mysql_query( "select * from Frames where EventId = '$eid' and FrameId = '$fid'" ); - if ( !$result ) - die( mysql_error() ); - if ( !($frame = mysql_fetch_assoc( $result )) ) + $sql = "select * from Frames where EventId = '$eid' and FrameId = '$fid'"; + if ( !($frame = dbFetchOne( $sql )) ) { $frame = array( 'FrameId'=>$fid, 'Type'=>'Normal', 'Score'=>0 ); } - mysql_free_result( $result ); } else { - $result = mysql_query( "select * from Frames where EventId = '$eid' and Score = '".$event['MaxScore']."'" ); - if ( !$result ) - die( mysql_error() ); - $frame = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $frame = dbFetchOne( "select * from Frames where EventId = '$eid' and Score = '".$event['MaxScore']."'" ); $fid = $frame['FrameId']; } diff --git a/web/zm_html_view_frames.php b/web/zm_html_view_frames.php index 2092322ba..ce8a3e918 100644 --- a/web/zm_html_view_frames.php +++ b/web/zm_html_view_frames.php @@ -24,21 +24,10 @@ if ( !canView( 'Events' ) ) return; } $sql = "select E.*,M.Name as MonitorName,M.Width,M.Height from Events as E inner join Monitors as M on E.MonitorId = M.Id where E.Id = '$eid'"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$event = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$event = dbFetchOne( $sql ); $sql = "select *, unix_timestamp( TimeStamp ) as UnixTimeStamp from Frames where EventID = '$eid' order by FrameId"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -while ( $row = mysql_fetch_assoc( $result ) ) -{ - $frames[] = $row; -} -mysql_free_result( $result ); +$frames = dbFetchAll( $sql ); ?> diff --git a/web/zm_html_view_function.php b/web/zm_html_view_function.php index 1fcf81314..9e8c930d2 100644 --- a/web/zm_html_view_function.php +++ b/web/zm_html_view_function.php @@ -23,11 +23,8 @@ if ( !canEdit( 'Monitors' ) ) $view = "error"; return; } -$result = mysql_query( "select * from Monitors where Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); + +$monitor = dbFetchMonitor( $mid ); ?> diff --git a/web/zm_html_view_group.php b/web/zm_html_view_group.php index 307d3b550..da96e6fad 100644 --- a/web/zm_html_view_group.php +++ b/web/zm_html_view_group.php @@ -25,12 +25,7 @@ if ( !canEdit( 'System' ) ) } if ( $gid ) { - $sql = "select * from Groups where Id = '$gid'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $new_group = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $new_group = dbFetchGroup( $gid ); } else { diff --git a/web/zm_html_view_groups.php b/web/zm_html_view_groups.php index a14757b53..40122845b 100644 --- a/web/zm_html_view_groups.php +++ b/web/zm_html_view_groups.php @@ -24,12 +24,10 @@ if ( !canView( 'System' ) ) return; } -$result = mysql_query( "select * from Groups order by Name" ); -if ( !$result ) - die( mysql_error() ); +$sql = "select * from Groups order by Name"; $groups = array(); $selected = false; -while ( $row = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $row ) { if ( $row['Id'] == $cgroup ) { @@ -38,7 +36,7 @@ while ( $row = mysql_fetch_assoc( $result ) ) } $groups[] = $row; } -mysql_free_result( $result ); + ?> diff --git a/web/zm_html_view_monitor.php b/web/zm_html_view_monitor.php index 1c3a3a04f..824b32398 100644 --- a/web/zm_html_view_monitor.php +++ b/web/zm_html_view_monitor.php @@ -44,18 +44,10 @@ if ( !isset($tab) ) if ( !empty($mid) ) { - $result = mysql_query( "select * from Monitors where Id = '$mid'" ); - if ( !$result ) - die( mysql_error() ); - $monitor = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $monitor = dbFetchMonitor( $mid ); if ( ZM_OPT_X10 ) { - $result = mysql_query( "select * from TriggersX10 where MonitorId = '$mid'" ); - if ( !$result ) - die( mysql_error() ); - $x10_monitor = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $x10_monitor = dbFetchOne( "select * from TriggersX10 where MonitorId = '$mid'" ); } } else @@ -115,11 +107,7 @@ if ( !isset( $new_monitor ) ) } if ( !empty($preset) ) { - $result = mysql_query( "select Type, Device, Channel, Format, Host, Port, Path, Width, Height, Palette, MaxFPS, Controllable, ControlId, ControlDevice, ControlAddress, DefaultRate, DefaultScale from MonitorPresets where Id = '$preset'" ); - if ( !$result ) - die( mysql_error() ); - $preset = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $preset = dbFetchOne( "select Type, Device, Channel, Format, Host, Port, Path, Width, Height, Palette, MaxFPS, Controllable, ControlId, ControlDevice, ControlAddress, DefaultRate, DefaultScale from MonitorPresets where Id = '$preset'" ); foreach ( $preset as $name=>$value ) { if ( isset($value) ) @@ -318,11 +306,8 @@ function loadLocations( form ) var option_count = 1; $zmSlangNone ); - while( $row = mysql_fetch_assoc( $result ) ) + foreach( dbFetchAll( $sql ) as $row ) { $control_types[$row['Id']] = $row['Name']; ?> @@ -349,7 +334,6 @@ function loadLocations( form ) returnLocationSelect.options.length = option_count; } else diff --git a/web/zm_html_view_monitorpreset.php b/web/zm_html_view_monitorpreset.php index 072e3ef92..ef7eef80a 100644 --- a/web/zm_html_view_monitorpreset.php +++ b/web/zm_html_view_monitorpreset.php @@ -23,16 +23,13 @@ if ( !canEdit( 'Monitors' ) ) $view = "error"; return; } -$result = mysql_query( "select Id,Name from MonitorPresets" ); -if ( !$result ) - die( mysql_error() ); +$sql( "select Id,Name from MonitorPresets" ); $presets = array(); $presets[0] = $zmSlangChoosePreset; -while ( $preset = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $preset ) { $presets[$preset['Id']] = htmlentities( $preset['Name'] ); } -mysql_free_result( $result ); ?> diff --git a/web/zm_html_view_monitorselect.php b/web/zm_html_view_monitorselect.php index e54111055..68484b4c8 100644 --- a/web/zm_html_view_monitorselect.php +++ b/web/zm_html_view_monitorselect.php @@ -24,15 +24,12 @@ if ( !canView( 'System' ) ) return; } -$result = mysql_query( "select Id,Name from Monitors order by Sequence asc" ); -if ( !$result ) - die( mysql_error() ); +$sql = "select Id,Name from Monitors order by Sequence asc"; $monitors = array(); -while ( $monitor = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $monitor ) { $monitors[] = $monitor; } -mysql_free_result( $result ); ?> diff --git a/web/zm_html_view_montage.php b/web/zm_html_view_montage.php index d62ca58a5..6d780d1c1 100644 --- a/web/zm_html_view_montage.php +++ b/web/zm_html_view_montage.php @@ -27,20 +27,13 @@ if ( !canView( 'Stream' ) ) if ( $group ) { $sql = "select * from Groups where Id = '$group'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $row = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $row = dbFetchOne( $sql ); $group_sql = "and find_in_set( Id, '".$row['MonitorIds']."' )"; } $sql = "select * from Monitors where Function != 'None' $group_sql order by Sequence"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); $monitors = array(); -while( $row = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $row ) { if ( !visibleMonitor( $row['Id'] ) ) { @@ -48,7 +41,6 @@ while( $row = mysql_fetch_assoc( $result ) ) } $monitors[] = $row; } -mysql_free_result( $result ); $rows = intval(((count($monitors)-1)/ZM_WEB_MONTAGE_MAX_COLS)+1); $cols = intval(ceil(count($monitors)/$rows)); diff --git a/web/zm_html_view_montagefeed.php b/web/zm_html_view_montagefeed.php index 46cda2629..3497f5e13 100644 --- a/web/zm_html_view_montagefeed.php +++ b/web/zm_html_view_montagefeed.php @@ -40,11 +40,8 @@ else { $sql = "select * from Monitors where Id = '$mid'"; } -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); + +$monitor = dbFetchOne( $sql ); $montage_width = ZM_WEB_MONTAGE_WIDTH?ZM_WEB_MONTAGE_WIDTH:reScale( $monitor['Width'], $monitor['DefaultScale'], ZM_WEB_DEFAULT_SCALE ); $montage_height = ZM_WEB_MONTAGE_HEIGHT?ZM_WEB_MONTAGE_HEIGHT:reScale( $monitor['Height'], $monitor['DefaultScale'], ZM_WEB_DEFAULT_SCALE ); diff --git a/web/zm_html_view_montagemenu.php b/web/zm_html_view_montagemenu.php index 09808c3c0..659d87b80 100644 --- a/web/zm_html_view_montagemenu.php +++ b/web/zm_html_view_montagemenu.php @@ -33,30 +33,19 @@ if ( empty($mode) ) if ( $mid ) { - $result = mysql_query( "select * from Monitors where Id = '$mid'" ); - if ( !$result ) - die( mysql_error() ); - $monitor = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $monitor = dbFetchMonitor( $mid ); } elseif ( ZM_OPT_CONTROL ) { if ( $group ) { $sql = "select * from Groups where Id = '$group'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $row = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $row = dbFetchOne( $sql ); $group_sql = "and find_in_set( Id, '".$row['MonitorIds']."' )"; } $sql = "select * from Monitors where Function != 'None' and Controllable = 1 $group_sql order by Sequence"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); $control_mid = 0; - while( $row = mysql_fetch_assoc( $result ) ) + foreach( dbFetchAl( $sql ) as $row ) { if ( !visibleMonitor( $row['Id'] ) ) { @@ -67,7 +56,6 @@ elseif ( ZM_OPT_CONTROL ) $control_mid = $row['Id']; } } - mysql_free_result( $result ); } ?> diff --git a/web/zm_html_view_options.php b/web/zm_html_view_options.php index b2948c3e6..f0fd84eb2 100644 --- a/web/zm_html_view_options.php +++ b/web/zm_html_view_options.php @@ -176,10 +176,8 @@ if ( $tab == "users" ) @@ -198,7 +196,6 @@ if ( $tab == "users" )   disabled  diff --git a/web/zm_html_view_settings.php b/web/zm_html_view_settings.php index dabbd643f..b7edc6a36 100644 --- a/web/zm_html_view_settings.php +++ b/web/zm_html_view_settings.php @@ -23,11 +23,7 @@ if ( !canView( 'Control' ) ) $view = "error"; return; } -$result = mysql_query( "select * from Monitors where Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchMonitor( $mid ); $zmu_command = getZmuCommand( " -m $mid -B -C -H -O" ); $zmu_output = exec( escapeshellcmd( $zmu_command ) ); diff --git a/web/zm_html_view_state.php b/web/zm_html_view_state.php index fe7a4c850..93b3f6dac 100644 --- a/web/zm_html_view_state.php +++ b/web/zm_html_view_state.php @@ -25,15 +25,7 @@ if ( !canEdit( 'System' ) ) } $running = daemonCheck(); -$result = mysql_query( "select * from States" ); -if ( !$result ) - die( mysql_error() ); -$states = array(); -while( $state = mysql_fetch_assoc( $result ) ) -{ - $states[] = $state; -} -mysql_free_result( $result ); +$states = dbFetchAll( "select * from States" ); ?> diff --git a/web/zm_html_view_stats.php b/web/zm_html_view_stats.php index 7504fcac0..81725ab4d 100644 --- a/web/zm_html_view_stats.php +++ b/web/zm_html_view_stats.php @@ -23,14 +23,8 @@ if ( !canView( 'Events' ) ) $view = "error"; return; } -$result = mysql_query( "select S.*,E.*,Z.Name as ZoneName,Z.Units,Z.Area,M.Name as MonitorName,M.Width,M.Height from Stats as S left join Events as E on S.EventId = E.Id left join Zones as Z on S.ZoneId = Z.Id left join Monitors as M on E.MonitorId = M.Id where S.EventId = '$eid' and S.FrameId = '$fid' order by S.ZoneId" ); -if ( !$result ) - die( mysql_error() ); -while ( $row = mysql_fetch_assoc( $result ) ) -{ - $stats[] = $row; -} -mysql_free_result( $result ); +$sql = "select S.*,E.*,Z.Name as ZoneName,Z.Units,Z.Area,M.Name as MonitorName,M.Width,M.Height from Stats as S left join Events as E on S.EventId = E.Id left join Zones as Z on S.ZoneId = Z.Id left join Monitors as M on E.MonitorId = M.Id where S.EventId = '$eid' and S.FrameId = '$fid' order by S.ZoneId"; +$stats = dbFetchAll( $sql ); ?> diff --git a/web/zm_html_view_timeline.php b/web/zm_html_view_timeline.php index cfbb9877c..6444bb56a 100644 --- a/web/zm_html_view_timeline.php +++ b/web/zm_html_view_timeline.php @@ -131,10 +131,8 @@ $chart = array( $monitors = array(); $monitors_sql = "select * from Monitors order by Sequence asc"; -if ( !($result = mysql_query( $monitors_sql )) ) - die( mysql_error() ); //srand( 97981 ); -while ( $row = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $monitors_sql ) as $row ) { //if ( empty($row['WebColour']) ) //{ @@ -142,7 +140,6 @@ while ( $row = mysql_fetch_assoc( $result ) ) //} $monitors[$row['Id']] = $row; } -mysql_free_result( $result ); $range_sql = "select min(E.StartTime) as MinTime, max(E.EndTime) as MaxTime from Events as E inner join Monitors as M on (E.MonitorId = M.Id) where not isnull(E.StartTime) and not isnull(E.EndTime)"; $events_sql = "select E.Id,E.Name,E.StartTime,E.EndTime,E.Length,E.Frames,E.MaxScore,E.Cause,E.Notes,E.Archived,E.MonitorId from Events as E inner join Monitors as M on (E.MonitorId = M.Id) where not isnull(StartTime)"; @@ -232,10 +229,7 @@ else if ( !isset($min_time) || !isset($max_time) ) { // Dynamically determine range - if ( !($result = mysql_query( $range_sql )) ) - die( mysql_error() ); - $row = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $row = dbFetchOne( $range_sql ); if ( !isset($min_time) ) $min_time = $row['MinTime']; @@ -328,10 +322,8 @@ $chart['data']['x']['density'] = $chart['data']['x']['range']/$chart['graph']['w $mon_event_slots = array(); $mon_frame_slots = array(); -if ( !($event_result = mysql_query( $events_sql )) ) - die( mysql_error() ); $monitor_ids = array(); -while( $event = mysql_fetch_assoc( $event_result ) ) +foreach( dbFetchAll( $events_sql ) as $event ) { if ( !isset($monitor_ids[$event['MonitorId']]) ) $monitor_ids[$event['MonitorId']] = true; @@ -406,9 +398,7 @@ while( $event = mysql_fetch_assoc( $event_result ) ) else { $frames_sql = "select F.FrameId,F.Delta,unix_timestamp(F.TimeStamp) as TimeT,F.Score from Frames as F where F.EventId = '".$event['Id']."' and F.Score > 0"; - if ( !($frame_result = mysql_query( $frames_sql )) ) - die( mysql_error() ); - while( $frame = mysql_fetch_assoc( $frame_result ) ) + foreach( dbFetchAll( $frames_sql ) as $frame ) { $frame_time_t = $frame['TimeT']; $frame_time_t = $start_time_t + $frame['Delta']; @@ -437,11 +427,9 @@ while( $event = mysql_fetch_assoc( $event_result ) ) $chart['data']['y']['hi'] = $frame['Score']; } } - mysql_free_result( $frame_result ); } } } -mysql_free_result( $event_result ); ksort($monitor_ids,SORT_NUMERIC); ksort($mon_event_slots,SORT_NUMERIC); @@ -461,10 +449,7 @@ foreach( array_keys($mon_frame_slots) as $monitor_id ) { $xcount++; $frames_sql = "select F.FrameId,F.Score from Frames as F where F.EventId = '".$curr_frame_slots[$i]['event']['Id']."' and F.Score > 0 order by F.FrameId limit 0,1"; - if ( !($frame_result = mysql_query( $frames_sql )) ) - die( mysql_error() ); - $curr_frame_slots[$i]['frame'] = mysql_fetch_assoc( $frame_result ); - mysql_free_result( $frame_result ); + $curr_frame_slots[$i]['frame'] = dbFetchOne( $frame_sql ); } } } diff --git a/web/zm_html_view_user.php b/web/zm_html_view_user.php index 6b500e517..53d1b8523 100644 --- a/web/zm_html_view_user.php +++ b/web/zm_html_view_user.php @@ -23,16 +23,13 @@ if ( !canEdit( 'System' ) ) $view = "error"; return; } -$result = mysql_query( "select * from Users where Id = '$uid'" ); -if ( !$result ) - die( mysql_error() ); -if ( !($db_user = mysql_fetch_assoc( $result )) ) +$sql = "select * from Users where Id = '$uid'"; +if ( !($db_user = dbFetchOne( $sql )) ) { $db_user = array(); $db_user['Username'] = $zmSlangNewUser; $db_user['Enabled'] = 1; } -mysql_free_result( $result ); $new_user = $db_user; diff --git a/web/zm_html_view_video.php b/web/zm_html_view_video.php index a89f3eaf7..071693237 100644 --- a/web/zm_html_view_video.php +++ b/web/zm_html_view_video.php @@ -33,11 +33,7 @@ else $mid_sql = ''; } $sql = "select E.*,M.Name as MonitorName,M.Width,M.Height,M.DefaultRate,M.DefaultScale from Events as E inner join Monitors as M on E.MonitorId = M.Id where E.Id = '$eid'$mid_sql"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$event = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$event = dbFetchOne( $sql ); if ( !isset( $rate ) ) $rate = reScale( RATE_BASE, $event['DefaultRate'], ZM_WEB_DEFAULT_RATE ); diff --git a/web/zm_html_view_watch.php b/web/zm_html_view_watch.php index 3cc778802..e8a26356f 100644 --- a/web/zm_html_view_watch.php +++ b/web/zm_html_view_watch.php @@ -23,11 +23,8 @@ if ( !canView( 'Stream' ) ) $view = "error"; return; } -$result = mysql_query( "select * from Monitors where Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); + +$monitor = dbFetchMonitor( $mid ); if ( !isset($control) ) { diff --git a/web/zm_html_view_watchX.php b/web/zm_html_view_watchX.php index 6e5c83491..19f6df4b6 100644 --- a/web/zm_html_view_watchX.php +++ b/web/zm_html_view_watchX.php @@ -25,11 +25,7 @@ if ( !canView( 'Stream' ) ) } $sql = "select C.*, M.* from Monitors as M left join Controls as C on (M.ControlId = C.Id ) where M.Id = '$mid'"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchOne( $sql ); $showControls = ( ZM_OPT_CONTROL && $monitor['Controllable'] && canView( 'Control' ) ); diff --git a/web/zm_html_view_watchevents.php b/web/zm_html_view_watchevents.php index 0008ad90e..617b2aa69 100644 --- a/web/zm_html_view_watchevents.php +++ b/web/zm_html_view_watchevents.php @@ -38,11 +38,7 @@ if ( ZM_WEB_REFRESH_METHOD == "http" ) header("Refresh: ".ZM_WEB_REFRESH_EVENTS."; URL=$PHP_SELF?view=watchevents&mid=$mid&max_events=".MAX_EVENTS ); noCacheHeaders(); -$result = mysql_query( "select * from Monitors where Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchMonitor( $mid ); ?> @@ -117,12 +113,9 @@ window.setTimeout( "window.location.replace( ' @@ -141,7 +134,7 @@ $n_events = mysql_num_rows( $result ); disabled> @@ -155,7 +148,6 @@ while( $event = mysql_fetch_assoc( $result ) ) diff --git a/web/zm_html_view_watchfeed.php b/web/zm_html_view_watchfeed.php index 9c026b81e..3c6f46855 100644 --- a/web/zm_html_view_watchfeed.php +++ b/web/zm_html_view_watchfeed.php @@ -33,11 +33,7 @@ if ( empty($mode) ) } $sql = "select M.*,C.CanMoveMap,C.CanMoveRel,C.CanMoveCon from Monitors as M left join Controls as C on (M.ControlId = C.Id ) where M.Id = '$mid'"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchOne( $sql ); if ( !isset( $scale ) ) $scale = reScale( SCALE_BASE, $monitor['DefaultScale'], ZM_WEB_DEFAULT_SCALE ); diff --git a/web/zm_html_view_watchmenu.php b/web/zm_html_view_watchmenu.php index ff0c7c225..af65cf367 100644 --- a/web/zm_html_view_watchmenu.php +++ b/web/zm_html_view_watchmenu.php @@ -32,11 +32,7 @@ if ( empty($mode) ) $mode = "still"; } -$result = mysql_query( "select * from Monitors where Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchMonitor( $mid ); if ( !isset( $scale ) ) $scale = $monitor['DefaultScale'] * ZM_WEB_DEFAULT_SCALE; diff --git a/web/zm_html_view_watchpos.php b/web/zm_html_view_watchpos.php index 941df2888..bd8e5cab6 100644 --- a/web/zm_html_view_watchpos.php +++ b/web/zm_html_view_watchpos.php @@ -30,11 +30,7 @@ if ( ZM_WEB_REFRESH_METHOD == "http" ) header("Refresh: ".ZM_POS_REFRESH."; URL=$PHP_SELF?view=watchpos&mid=$mid" ); noCacheHeaders(); -$result = mysql_query( "select * from Events where MonitorId = '$mid' order by Id desc limit 1" ); -if ( !$result ) - die( mysql_error() ); -$event = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$event = dbFetchOne( "select * from Events where MonitorId = '$mid' order by Id desc limit 1" ); ?> diff --git a/web/zm_html_view_zone.php b/web/zm_html_view_zone.php index 0487db6e2..b08e7c68d 100644 --- a/web/zm_html_view_zone.php +++ b/web/zm_html_view_zone.php @@ -38,24 +38,17 @@ $marker = array( "height"=>7, ); -$result = mysql_query( "select *, Units-1 as UnitsIndex, CheckMethod-1 as CheckMethodIndex from ZonePresets order by Id asc" ); -if ( !$result ) - die( mysql_error() ); +$sql( "select *, Units-1 as UnitsIndex, CheckMethod-1 as CheckMethodIndex from ZonePresets order by Id asc" ); $presets = array(); $preset_names = array(); $preset_names[0] = $zmSlangChoosePreset; -while ( $preset = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $preset ) { $preset_names[$preset['Id']] = $preset['Name']; $presets[] = $preset; } -mysql_free_result( $result ); -$result = mysql_query( "select * from Monitors where Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchMonitor ( $mid ); $min_x = 0; $max_x = $monitor['Width']-1; @@ -66,11 +59,7 @@ if ( !isset($new_zone) ) { if ( $zid > 0 ) { - $result = mysql_query( "select * from Zones where MonitorId = '$mid' and Id = '$zid'" ); - if ( !$result ) - die( mysql_error() ); - $zone = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $zone = dbFetchOne( "select * from Zones where MonitorId = '$mid' and Id = '$zid'" ); } else { diff --git a/web/zm_html_view_zones.php b/web/zm_html_view_zones.php index f52f87a50..564fa8c35 100644 --- a/web/zm_html_view_zones.php +++ b/web/zm_html_view_zones.php @@ -27,17 +27,11 @@ chdir( ZM_DIR_IMAGES ); $status = exec( escapeshellcmd( getZmuCommand( " -m $mid -z" ) ) ); chdir( '..' ); -$result = mysql_query( "select * from Monitors where Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchMonitor( $mid ); -$result = mysql_query( "select * from Zones where MonitorId = '$mid' order by Area desc" ); -if ( !$result ) - die( mysql_error() ); +$sql = "select * from Zones where MonitorId = '$mid' order by Area desc"; $zones = array(); -while( $row = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $row ) { if ( $row['Points'] = coordsToPoints( $row['Coords'] ) ) { @@ -45,7 +39,6 @@ while( $row = mysql_fetch_assoc( $result ) ) $zones[] = $row; } } -mysql_free_result( $result ); $image = $monitor['Name']."-Zones.jpg"; diff --git a/web/zm_request_control.php b/web/zm_request_control.php index c23ceea3a..81f6746ce 100644 --- a/web/zm_request_control.php +++ b/web/zm_request_control.php @@ -955,20 +955,14 @@ else if ( canEdit( 'Control' ) ) { $sql = "select * from ControlPresets where MonitorId = '".$monitor['Id']."' and Preset = '".$preset."'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); - $row = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $row = dbFetchOne( $sql ); if ( $new_label != $row['Label'] ) { if ( $new_label ) $sql = "replace into ControlPresets ( MonitorId, Preset, Label ) values ( '".$monitor['Id']."', '".$preset."', '".addslashes($new_label)."' )"; else $sql = "delete from ControlPresets where MonitorId = '".$monitor['Id']."' and Preset = '".$preset."'"; - $result = mysql_query( $sql ); - if ( !$result ) - die( mysql_error() ); + dbQuery( $sql ); $refresh_parent = true; } } diff --git a/web/zm_request_event.php b/web/zm_request_event.php index 13c3f6117..2bab45d5f 100644 --- a/web/zm_request_event.php +++ b/web/zm_request_event.php @@ -19,13 +19,13 @@ $refresh_parent = false; // Event scope actions, edit permissions required if ( $_REQUEST['action'] == "rename" && $_REQUEST['id'] && $_REQUEST['eventName'] ) { - dbQuery( "update Events set Name = '".mysql_real_escape_string($_REQUEST['eventName'])."' where Id = '".mysql_real_escape_string($_REQUEST['id'])."'" ); + dbQuery( "update Events set Name = '".dbEscape($_REQUEST['eventName'])."' where Id = '".dbEscape($_REQUEST['id'])."'" ); } else if ( $_REQUEST['action'] == "eventdetail" ) { if ( $_REQUEST['id'] ) { - dbQuery( "update Events set Cause = '".mysql_real_escape_string($_REQUEST['new_event']['Cause'])."', Notes = '".mysql_real_escape_string($_REQUEST['new_event']['Notes'])."' where Id = '".mysql_real_escape_string($_REQUEST['id'])."'" ); + dbQuery( "update Events set Cause = '".dbEscape($_REQUEST['new_event']['Cause'])."', Notes = '".dbEscape($_REQUEST['new_event']['Notes'])."' where Id = '".dbEscape($_REQUEST['id'])."'" ); $refresh_parent = true; } } @@ -35,7 +35,7 @@ elseif ( $_REQUEST['action'] == "archive" || $_REQUEST['action'] == "unarchive" if ( $_REQUEST['id'] ) { - dbQuery( "update Events set Archived = ".$archive_val." where Id = '".mysql_real_escape_string($_REQUEST['id'])."'" ); + dbQuery( "update Events set Archived = ".$archive_val." where Id = '".dbEscape($_REQUEST['id'])."'" ); } elseif ( $mark_eids || $mark_eid ) { diff --git a/web/zm_request_status.php b/web/zm_request_status.php index 655df547c..a34e8b6ff 100644 --- a/web/zm_request_status.php +++ b/web/zm_request_status.php @@ -204,28 +204,21 @@ function collectData( $spec ) $limit = $_REQUEST['count']; if ( !empty( $limit ) ) $sql .= " limit ".$limit; - $result = mysql_query( $sql ); - if ( !$result ) - { - error_log( $sql ); - error_log( mysql_error() ); - } if ( isset($limit) && $limit == 1 ) { - $sql_data = mysql_fetch_assoc( $result ); + $sql_data = dbFetchOne( $sql ); $data = array_merge( $data, $sql_data ); } else { $count = 0; - while ( $sql_data = mysql_fetch_assoc( $result ) ) + foreach( dbFetchAll( $sql ) as $sql_data ) { $data[] = $sql_data; if ( ++$count >= $limit ) break; } } - mysql_free_result( $result ); } #print_r( $data ); return( $data ); diff --git a/web/zm_xhtml_view_console.php b/web/zm_xhtml_view_console.php index 89a70579d..0c02ead47 100644 --- a/web/zm_xhtml_view_console.php +++ b/web/zm_xhtml_view_console.php @@ -42,11 +42,7 @@ $event_counts = array( $running = daemonCheck(); $status = $running?$zmSlangRunning:$zmSlangStopped; -$sql = "select * from Groups where Name = 'Mobile'"; -$result = mysql_query( $sql ); -if ( !$result ) - echo mysql_error(); -if ( $group = dbFetchOne( "select * from Groups where Id = '$cgroup'" ) ) +if ( $group = dbFetchOne( "select * from Groups where Name = 'Mobile'" ) ) $group_ids = array_flip(split( ',', $group['MonitorIds'] )); $db_now = strftime( STRF_FMT_DATETIME_DB ); @@ -87,7 +83,6 @@ for ( $i = 0; $i < count($monitors); $i++ ) } $monitors[$i] = array_merge( $monitors[$i], $counts ); } -mysql_free_result( $result ); ?> diff --git a/web/zm_xhtml_view_devices.php b/web/zm_xhtml_view_devices.php index 661accffc..a98775f3a 100644 --- a/web/zm_xhtml_view_devices.php +++ b/web/zm_xhtml_view_devices.php @@ -19,16 +19,12 @@ // $sql = "select * from Devices where Type = 'X10' order by Name"; -$result = mysql_query( $sql ); -if ( !$result ) - echo mysql_error(); $devices = array(); -while( $row = mysql_fetch_assoc( $result ) ) +foreach ( dbFetchAll( $sql ) as $row ) { $row['Status'] = getDeviceStatusX10( $row['KeyString'] ); $devices[] = $row; } -mysql_free_result( $result ); ?> diff --git a/web/zm_xhtml_view_event.php b/web/zm_xhtml_view_event.php index 191b4acef..6b53999f6 100644 --- a/web/zm_xhtml_view_event.php +++ b/web/zm_xhtml_view_event.php @@ -39,27 +39,15 @@ else } $sql = "select E.*,M.Name as MonitorName,M.Width,M.Height from Events as E inner join Monitors as M on E.MonitorId = M.Id where E.Id = '$eid'$mid_sql"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$event = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$event = dbFetchOne( $sql ); if ( $fid ) { - $result = mysql_query( "select * from Frames where EventID = '$eid' and FrameId = '$fid'" ); - if ( !$result ) - die( mysql_error() ); - $frame = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $frame = dbFetchOne( "select * from Frames where EventID = '$eid' and FrameId = '$fid'" ); } elseif ( isset( $fid ) ) { - $result = mysql_query( "select * from Frames where EventID = '$eid' and Score = '".$event['MaxScore']."'" ); - if ( !$result ) - die( mysql_error() ); - $frame = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $frame = dbFetchOne( "select * from Frames where EventID = '$eid' and Score = '".$event['MaxScore']."'" ); $fid = $frame['FrameId']; } @@ -67,32 +55,26 @@ parseSort( true, '&' ); parseFilter( true, '&' ); $sql = "select E.* from Events as E inner join Monitors as M on E.MonitorId = M.Id where $sort_column ".($sort_order=='asc'?'<=':'>=')." '".$event[$sort_field]."'$filter_sql$mid_sql order by $sort_column ".($sort_order=='asc'?'desc':'asc'); -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -while ( $row = mysql_fetch_assoc( $result ) ) +$result = dbQuery( $sql ); +while ( $row = dbFetchNext( $result ) ) { if ( $row[Id] == $eid ) { - $prev_event = mysql_fetch_assoc( $result ); + $prev_event = dbFetchNext( $result ); break; } } -mysql_free_result( $result ); $sql = "select E.* from Events as E inner join Monitors as M on E.MonitorId = M.Id where $sort_column ".($sort_order=='asc'?'>=':'<=')." '".$event[$sort_field]."'$filter_sql$mid_sql order by $sort_column $sort_order"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -while ( $row = mysql_fetch_assoc( $result ) ) +$result = dbQuery( $sql ); +while ( $row = dbFetchNext( $result ) ) { if ( $row[Id] == $eid ) { - $next_event = mysql_fetch_assoc( $result ); + $next_event = dbFetchNext( $result ); break; } } -mysql_free_result( $result ); $frames_per_page = 15; $frames_per_line = 3; @@ -222,18 +204,14 @@ $sql = "select * from Frames where EventID = '$eid'"; if ( $paged && !empty($page) ) $sql .= " and FrameId between $lo_frame_id and $hi_frame_id"; $sql .= " order by FrameId"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); $alarm_frames = array(); -while( $row = mysql_fetch_assoc( $result ) ) +foreach ( dbFetchAll( $sql ) as $row ) { if ( $row['Type'] == 'Alarm' ) { $alarm_frames[$row['FrameId']] = $row; } } -mysql_free_result( $result ); ?> - $limit ) { $n_events = $limit; @@ -228,9 +216,7 @@ if ( !empty($limit) && $n_events > $limit ) $limit )

diff --git a/web/zm_xhtml_view_filter.php b/web/zm_xhtml_view_filter.php index 0ebfa4c52..8eb9cc8cc 100644 --- a/web/zm_xhtml_view_filter.php +++ b/web/zm_xhtml_view_filter.php @@ -24,14 +24,11 @@ if ( !canView( 'Events' ) ) return; } $filter_names = array(); -$result = mysql_query( "select * from Filters order by Name" ); -if ( !$result ) - die( mysql_error() ); -while ( $row = mysql_fetch_assoc( $result ) ) +$sql = "select * from Filters order by Name"; +foreach( dbFetchAll( $sql ) as $row ) { $filter_names[$row['Name']] = $row['Name']; } -mysql_free_result( $result ); ?> diff --git a/web/zm_xhtml_view_frame.php b/web/zm_xhtml_view_frame.php index a58f0b234..a4ec82d5a 100644 --- a/web/zm_xhtml_view_frame.php +++ b/web/zm_xhtml_view_frame.php @@ -24,27 +24,15 @@ if ( !canView( 'Events' ) ) return; } $sql = "select E.*,M.Name as MonitorName,M.Width,M.Height,M.DefaultScale from Events as E inner join Monitors as M on E.MonitorId = M.Id where E.Id = '$eid'"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$event = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$event = dbFetchOne( $sql ); if ( $fid ) { - $result = mysql_query( "select * from Frames where EventID = '$eid' and FrameId = '$fid'" ); - if ( !$result ) - die( mysql_error() ); - $frame = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $frame = dbFetchOne( "select * from Frames where EventID = '$eid' and FrameId = '$fid'" ); } else { - $result = mysql_query( "select * from Frames where EventID = '$eid' and Score = '".$event['MaxScore']."'" ); - if ( !$result ) - die( mysql_error() ); - $frame = mysql_fetch_assoc( $result ); - mysql_free_result( $result ); + $frame = dbFetchOne( "select * from Frames where EventID = '$eid' and Score = '".$event['MaxScore']."'" ); $fid = $frame['FrameId']; } diff --git a/web/zm_xhtml_view_function.php b/web/zm_xhtml_view_function.php index 955ed9c18..11b1321ab 100644 --- a/web/zm_xhtml_view_function.php +++ b/web/zm_xhtml_view_function.php @@ -23,11 +23,7 @@ if ( !canEdit( 'Monitors' ) ) $view = "error"; return; } -$result = mysql_query( "select * from Monitors where Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$monitor = dbFetchMonitor( $mid ); ?> diff --git a/web/zm_xhtml_view_montage.php b/web/zm_xhtml_view_montage.php index 3ad3f13ed..f2ffdbcc8 100644 --- a/web/zm_xhtml_view_montage.php +++ b/web/zm_xhtml_view_montage.php @@ -21,17 +21,13 @@ $images_per_line = 2; $sql = "select * from Groups where Name = 'Mobile'"; -$result = mysql_query( $sql ); -if ( !$result ) - echo mysql_error(); -$group = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$group = dbFetchOne( $sql ); -$result = mysql_query( "select * from Monitors where Function != 'None' order by Sequence" ); +$sql = "select * from Monitors where Function != 'None' order by Sequence"; $monitors = array(); $max_width = 0; $max_height = 0; -while( $row = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $row ) { if ( !visibleMonitor( $row['Id'] ) ) { @@ -46,7 +42,6 @@ while( $row = mysql_fetch_assoc( $result ) ) if ( $max_height < $row['Height'] ) $max_height = $row['Height']; $monitors[] = $row; } -mysql_free_result( $result ); ?> diff --git a/web/zm_xhtml_view_state.php b/web/zm_xhtml_view_state.php index 914f08fa2..36ef26687 100644 --- a/web/zm_xhtml_view_state.php +++ b/web/zm_xhtml_view_state.php @@ -25,14 +25,7 @@ if ( !canEdit( 'System' ) ) } $running = daemonCheck(); -$result = mysql_query( "select * from States" ); -if ( !$result ) - die( mysql_error() ); -while( $state = mysql_fetch_assoc( $result ) ) -{ - $states[] = $state; -} -mysql_free_result( $result ); +$status = dbFetchAll( "select * from States" ); ?> diff --git a/web/zm_xhtml_view_video.php b/web/zm_xhtml_view_video.php index 38f191c1e..b414c5726 100644 --- a/web/zm_xhtml_view_video.php +++ b/web/zm_xhtml_view_video.php @@ -33,11 +33,7 @@ else $mid_sql = ''; } $sql = "select E.*,M.Name as MonitorName,M.Width,M.Height,M.DefaultRate from Events as E inner join Monitors as M on E.MonitorId = M.Id where E.Id = '$eid'$mid_sql"; -$result = mysql_query( $sql ); -if ( !$result ) - die( mysql_error() ); -$event = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); +$event = dbFetchOne( $sql ); $device_width = (isset($device)&&!empty($device['width']))?$device['width']:DEVICE_WIDTH; $device_height = (isset($device)&&!empty($device['height']))?$device['height']:DEVICE_HEIGHT; diff --git a/web/zm_xhtml_view_watch.php b/web/zm_xhtml_view_watch.php index ca3d29ec4..c093b26d2 100644 --- a/web/zm_xhtml_view_watch.php +++ b/web/zm_xhtml_view_watch.php @@ -23,11 +23,8 @@ if ( !canView( 'Stream' ) ) $view = "error"; return; } -$result = mysql_query( "select * from Monitors where Id = '$mid'" ); -if ( !$result ) - die( mysql_error() ); -$monitor = mysql_fetch_assoc( $result ); -mysql_free_result( $result ); + +$monitor = dbFetchMonitor( $mid ); $zmu_command = getZmuCommand( " -m $mid -s -f" ); $zmu_output = exec( escapeshellcmd( $zmu_command ) ); @@ -61,12 +58,12 @@ $was_alarmed = ( $last_status == STATE_ALARM || $last_status == STATE_ALERT ); $new_alarm = ( $is_alarmed && !$was_alarmed ); $old_alarm = ( !$is_alarmed && $was_alarmed ); -$result = mysql_query( "select * from Monitors where Function != 'None' order by Sequence" ); +$sql = "select * from Monitors where Function != 'None' order by Sequence"; $monitors = array(); $mon_idx = 0; $max_width = 0; $max_height = 0; -while( $row = mysql_fetch_assoc( $result ) ) +foreach( dbFetchAll( $sql ) as $row ) { if ( !visibleMonitor( $row['Id'] ) ) { @@ -78,7 +75,6 @@ while( $row = mysql_fetch_assoc( $result ) ) if ( $max_height < $row['Height'] ) $max_height = $row['Height']; $monitors[] = $row; } -mysql_free_result( $result ); //$monitor = $monitors[$mon_idx]; $next_mid = $mon_idx==(count($monitors)-1)?$monitors[0]['Id']:$monitors[$mon_idx+1]['Id'];