diff --git a/web/ajax/add_monitors.php b/web/ajax/add_monitors.php index c5664f06b..69ce0332f 100644 --- a/web/ajax/add_monitors.php +++ b/web/ajax/add_monitors.php @@ -93,7 +93,7 @@ Info("Testing connection to " . $url_bits['host'].':'.$port); foreach ( $available_streams as &$stream ) { # check for existence in db. $stream['url'] = unparse_url( $stream, array('path'=>'/','query'=>'action=stream') ); - $monitors = Monitor::find_all( array('Path'=>$stream['url']) ); + $monitors = Monitor::find( array('Path'=>$stream['url']) ); if ( count($monitors) ) { Info("Found monitors matching " . $stream['url'] ); $stream['Monitor'] = $monitors[0]; @@ -135,7 +135,7 @@ if ( canEdit( 'Monitors' ) ) { if ( 0 ) { // Shortcut test - $monitors = Monitor::find_all( array( 'Path'=>$_REQUEST['url'] ) ); + $monitors = Monitor::find( array('Path'=>$_REQUEST['url']) ); if ( count( $monitors ) ) { Info("Monitor found for " . $_REQUEST['url']); $url_bits['url'] = $_REQUEST['url']; diff --git a/web/ajax/log.php b/web/ajax/log.php index 375b53a05..59595d216 100644 --- a/web/ajax/log.php +++ b/web/ajax/log.php @@ -33,7 +33,7 @@ switch ( $_REQUEST['task'] ) { if ( !canView('System') ) ajaxError('Insufficient permissions to view log entries'); - $servers = Server::find_all(); + $servers = Server::find(); $servers_by_Id = array(); # There is probably a better way to do this. foreach ( $servers as $server ) { @@ -153,7 +153,7 @@ switch ( $_REQUEST['task'] ) { } $sortOrder = (isset($_POST['sortOrder']) and $_POST['sortOrder']) == 'asc' ? 'asc':'desc'; - $servers = Server::find_all(); + $servers = Server::find(); $servers_by_Id = array(); # There is probably a better way to do this. foreach ( $servers as $server ) { diff --git a/web/includes/Control.php b/web/includes/Control.php index 74beeca05..fcb35ac63 100644 --- a/web/includes/Control.php +++ b/web/includes/Control.php @@ -166,8 +166,7 @@ private $defaults = array( } } } - public static function find_all( $parameters = null, $options = null ) { - $filters = array(); + public static function find( $parameters = null, $options = null ) { $sql = 'SELECT * FROM Controls '; $values = array(); @@ -189,15 +188,37 @@ private $defaults = array( } $sql .= implode(' AND ', $fields ); } - if ( $options and isset($options['order']) ) { - $sql .= ' ORDER BY ' . $options['order']; + if ( $options ) { + if ( isset($options['order']) ) { + $sql .= ' ORDER BY ' . $options['order']; + } + if ( isset($options['limit']) ) { + if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) { + $sql .= ' LIMIT ' . $limit; + } else { + $backTrace = debug_backtrace(); + $file = $backTrace[1]['file']; + $line = $backTrace[1]['line']; + Error("Invalid value for limit($limit) passed to Control::find from $file:$line"); + return; + } + } } + $controls = array(); $result = dbQuery($sql, $values); $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Control'); foreach ( $results as $row => $obj ) { - $filters[] = $obj; + $controls[] = $obj; } - return $filters; + return $controls; + } + + public static function find_one( $parameters = array() ) { + $results = Control::find( $parameters, array('limit'=>1) ); + if ( ! sizeof($results) ) { + return; + } + return $results[0]; } public function save( $new_values = null ) { diff --git a/web/includes/Event.php b/web/includes/Event.php index d7ce25dc4..2bb05bf5d 100644 --- a/web/includes/Event.php +++ b/web/includes/Event.php @@ -486,7 +486,7 @@ class Event { isset($event_cache[$parameters['Id']]) ) { return $event_cache[$parameters['Id']]; } - $results = Event::find_all( $parameters, $options ); + $results = Event::find( $parameters, $options ); if ( count($results) > 1 ) { Error("Event Returned more than 1"); return $results[0]; @@ -497,7 +497,7 @@ class Event { } } - public static function find_all( $parameters = null, $options = null ) { + public static function find( $parameters = null, $options = null ) { $filters = array(); $sql = 'SELECT * FROM Events '; $values = array(); @@ -520,8 +520,21 @@ class Event { } $sql .= implode(' AND ', $fields ); } - if ( $options and isset($options['order']) ) { - $sql .= ' ORDER BY ' . $options['order']; + if ( $options ) { + if ( isset($options['order']) ) { + $sql .= ' ORDER BY ' . $options['order']; + } + if ( isset($options['limit']) ) { + if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) { + $sql .= ' LIMIT ' . $limit; + } else { + $backTrace = debug_backtrace(); + $file = $backTrace[1]['file']; + $line = $backTrace[1]['line']; + Error("Invalid value for limit($limit) passed to Event::find from $file:$line"); + return; + } + } } $result = dbQuery($sql, $values); $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Event'); diff --git a/web/includes/Filter.php b/web/includes/Filter.php index 721302edb..f65399320 100644 --- a/web/includes/Filter.php +++ b/web/includes/Filter.php @@ -26,12 +26,12 @@ public $defaults = array( public function __construct( $IdOrRow=NULL ) { $row = NULL; if ( $IdOrRow ) { - if ( is_integer( $IdOrRow ) or is_numeric( $IdOrRow ) ) { - $row = dbFetchOne( 'SELECT * FROM Filters WHERE Id=?', NULL, array( $IdOrRow ) ); + if ( is_integer($IdOrRow) or is_numeric($IdOrRow) ) { + $row = dbFetchOne('SELECT * FROM Filters WHERE Id=?', NULL, array($IdOrRow)); if ( ! $row ) { - Error('Unable to load Filter record for Id=' . $IdOrRow ); + Error('Unable to load Filter record for Id=' . $IdOrRow); } - } elseif ( is_array( $IdOrRow ) ) { + } elseif ( is_array($IdOrRow) ) { $row = $IdOrRow; } else { $backTrace = debug_backtrace(); @@ -47,8 +47,8 @@ public $defaults = array( foreach ($row as $k => $v) { $this->{$k} = $v; } - if ( array_key_exists( 'Query', $this ) and $this->{'Query'} ) { - $this->{'Query'} = jsonDecode( $this->{'Query'} ); + if ( array_key_exists('Query', $this) and $this->{'Query'} ) { + $this->{'Query'} = jsonDecode($this->{'Query'}); } else { $this->{'Query'} = array(); } @@ -111,18 +111,62 @@ public $defaults = array( return $this->defaults{'limit'}; } - public static function find_all() { + public static function find( $parameters = null, $options = null ) { $filters = array(); - $result = dbQuery( 'SELECT * FROM Filters ORDER BY Name'); - $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Filter' ); + $sql = 'SELECT * FROM Filters '; + $values = array(); + + if ( $parameters ) { + $fields = array(); + $sql .= 'WHERE '; + foreach ( $parameters as $field => $value ) { + if ( $value == null ) { + $fields[] = $field.' IS NULL'; + } else if ( is_array( $value ) ) { + $func = function(){return '?';}; + $fields[] = $field.' IN ('.implode(',', array_map($func, $value)). ')'; + $values += $value; + } else { + $fields[] = $field.'=?'; + $values[] = $value; + } + } + $sql .= implode(' AND ', $fields); + } + if ( $options ) { + if ( isset($options['order']) ) { + $sql .= ' ORDER BY ' . $options['order']; + } + if ( isset($options['limit']) ) { + if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) { + $sql .= ' LIMIT ' . $limit; + } else { + $backTrace = debug_backtrace(); + $file = $backTrace[1]['file']; + $line = $backTrace[1]['line']; + Error("Invalid value for limit($limit) passed to Filter::find from $file:$line"); + return; + } + } + } + $result = dbQuery($sql, $values); + $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Filter'); foreach ( $results as $row => $obj ) { $filters[] = $obj; } return $filters; - } + } # end find() + + public static function find_one( $parameters = array() ) { + $results = Filter::find($parameters, array('limit'=>1)); + if ( ! sizeof($results) ) { + return; + } + return $results[0]; + } # end find_one() public function delete() { - dbQuery( 'DELETE FROM Filters WHERE Id = ?', array($this->{'Id'}) ); + dbQuery('DELETE FROM Filters WHERE Id = ?', array($this->{'Id'})); } # end function delete() public function set( $data ) { @@ -141,8 +185,6 @@ public $defaults = array( } } } - - } # end class ?> diff --git a/web/includes/Group.php b/web/includes/Group.php index 441eff70e..4cbdaa83b 100644 --- a/web/includes/Group.php +++ b/web/includes/Group.php @@ -58,27 +58,7 @@ class Group { } } - public static function find_one($parameters = null, $options = null) { - global $group_cache; - if ( - ( count($parameters) == 1 ) and - isset($parameters['Id']) and - isset($group_cache[$parameters['Id']]) ) { - return $group_cache[$parameters['Id']]; - } - $results = Group::find_all($parameters, $options); - if ( count($results) > 1 ) { - Error("Group::find_one Returned more than 1"); - return $results[0]; - } else if ( count($results) ) { - return $results[0]; - } else { - return null; - } - } - - public static function find_all( $parameters = null ) { - $filters = array(); + public static function find( $parameters = null, $options = null ) { $sql = 'SELECT * FROM Groups '; $values = array(); @@ -90,22 +70,57 @@ class Group { $fields[] = $field.' IS NULL'; } else if ( is_array( $value ) ) { $func = function(){return '?';}; - $fields[] = $field.' IN ('.implode(',', array_map( $func, $value ) ). ')'; + $fields[] = $field.' IN ('.implode(',', array_map($func, $value)). ')'; $values += $value; } else { $fields[] = $field.'=?'; $values[] = $value; } } - $sql .= implode(' AND ', $fields ); - } - $sql .= ' ORDER BY Name'; + $sql .= implode(' AND ', $fields); + } # end if parameters + if ( $options ) { + if ( isset($options['order']) ) { + $sql .= ' ORDER BY ' . $options['order']; + } + if ( isset($options['limit']) ) { + if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) { + $sql .= ' LIMIT ' . $limit; + } else { + $backTrace = debug_backtrace(); + $file = $backTrace[1]['file']; + $line = $backTrace[1]['line']; + Error("Invalid value for limit($limit) passed to Group::find from $file:$line"); + return; + } + } + } # end if options + $groups = array(); $result = dbQuery($sql, $values); $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Group'); foreach ( $results as $row => $obj ) { - $filters[] = $obj; + $groups[] = $obj; + } + return $groups; + } # end find() + + public static function find_one($parameters = null, $options = null) { + global $group_cache; + if ( + ( count($parameters) == 1 ) and + isset($parameters['Id']) and + isset($group_cache[$parameters['Id']]) ) { + return $group_cache[$parameters['Id']]; + } + $results = Group::find($parameters, $options); + if ( count($results) > 1 ) { + Error("Group::find_one Returned more than 1"); + return $results[0]; + } else if ( count($results) ) { + return $results[0]; + } else { + return null; } - return $filters; } public function delete() { @@ -182,7 +197,7 @@ class Group { public static function get_dropdown_options() { $Groups = array(); - foreach ( Group::find_all( ) as $Group ) { + foreach ( Group::find( ) as $Group ) { $Groups[$Group->Id()] = $Group; } diff --git a/web/includes/Monitor.php b/web/includes/Monitor.php index efa530c0a..19aaf920b 100644 --- a/web/includes/Monitor.php +++ b/web/includes/Monitor.php @@ -278,8 +278,7 @@ private $control_fields = array( } # end if method_exists } # end foreach $data as $k=>$v } - public static function find_all( $parameters = null, $options = null ) { - $filters = array(); + public static function find( $parameters = null, $options = null ) { $sql = 'SELECT * FROM Monitors '; $values = array(); @@ -289,9 +288,9 @@ private $control_fields = array( foreach ( $parameters as $field => $value ) { if ( $value == null ) { $fields[] = $field.' IS NULL'; - } else if ( is_array( $value ) ) { + } else if ( is_array($value) ) { $func = function(){return '?';}; - $fields[] = $field.' IN ('.implode(',', array_map( $func, $value ) ). ')'; + $fields[] = $field.' IN ('.implode(',', array_map($func, $value)). ')'; $values += $value; } else { @@ -299,18 +298,40 @@ private $control_fields = array( $values[] = $value; } } - $sql .= implode(' AND ', $fields ); + $sql .= implode(' AND ', $fields); } - if ( $options and isset($options['order']) ) { - $sql .= ' ORDER BY ' . $options['order']; + if ( $options ) { + if ( isset($options['order']) ) { + $sql .= ' ORDER BY ' . $options['order']; + } + if ( isset($options['limit']) ) { + if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) { + $sql .= ' LIMIT ' . $limit; + } else { + $backTrace = debug_backtrace(); + $file = $backTrace[1]['file']; + $line = $backTrace[1]['line']; + Error("Invalid value for limit($limit) passed to Control::find from $file:$line"); + return; + } + } } + $monitors = array(); $result = dbQuery($sql, $values); $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Monitor'); foreach ( $results as $row => $obj ) { - $filters[] = $obj; + $monitors[] = $obj; } - return $filters; - } + return $monitors; + } # end find + + public static function find_one( $parameters = array() ) { + $results = Monitor::find( $parameters, array('limit'=>1) ); + if ( ! sizeof($results) ) { + return; + } + return $results[0]; + } # end find_one public function save($new_values = null) { @@ -509,5 +530,7 @@ private $control_fields = array( } return $source; } // end function Source + + } // end class Monitor ?> diff --git a/web/includes/Server.php b/web/includes/Server.php index 591563b52..f9a82c9bb 100644 --- a/web/includes/Server.php +++ b/web/includes/Server.php @@ -13,12 +13,12 @@ class Server { public function __construct( $IdOrRow = NULL ) { $row = NULL; if ( $IdOrRow ) { - if ( is_integer( $IdOrRow ) or ctype_digit( $IdOrRow ) ) { - $row = dbFetchOne( 'SELECT * FROM Servers WHERE Id=?', NULL, array( $IdOrRow ) ); - if ( ! $row ) { - Error("Unable to load Server record for Id=" . $IdOrRow ); + if ( is_integer($IdOrRow) or ctype_digit($IdOrRow) ) { + $row = dbFetchOne('SELECT * FROM Servers WHERE Id=?', NULL, array($IdOrRow)); + if ( !$row ) { + Error("Unable to load Server record for Id=" . $IdOrRow); } - } elseif ( is_array( $IdOrRow ) ) { + } elseif ( is_array($IdOrRow) ) { $row = $IdOrRow; } } # end if isset($IdOrRow) @@ -31,39 +31,6 @@ class Server { $this->{'Hostname'} = ''; } } - public static function find_all( $parameters = null, $options = null ) { - $filters = array(); - $sql = 'SELECT * FROM Servers '; - $values = array(); - - if ( $parameters ) { - $fields = array(); - $sql .= 'WHERE '; - foreach ( $parameters as $field => $value ) { - if ( $value == null ) { - $fields[] = $field.' IS NULL'; - } else if ( is_array( $value ) ) { - $func = function(){return '?';}; - $fields[] = $field.' IN ('.implode(',', array_map( $func, $value ) ). ')'; - $values += $value; - - } else { - $fields[] = $field.'=?'; - $values[] = $value; - } - } - $sql .= implode(' AND ', $fields ); - } - if ( $options and isset($options['order']) ) { - $sql .= ' ORDER BY ' . $options['order']; - } - $result = dbQuery($sql, $values); - $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Server'); - foreach ( $results as $row => $obj ) { - $filters[] = $obj; - } - return $filters; - } public function Url() { if ( $this->Id() ) { @@ -96,26 +63,45 @@ class Server { } } } - - public static function find( $parameters = array(), $limit = NULL ) { - $sql = 'SELECT * FROM Servers'; + public static function find( $parameters = null, $options = null ) { + $filters = array(); + $sql = 'SELECT * FROM Servers '; $values = array(); - if ( sizeof($parameters) ) { - $sql .= ' WHERE ' . implode( ' AND ', array_map( - function($v){ return $v.'=?'; }, - array_keys( $parameters ) - ) ); - $values = array_values( $parameters ); + + if ( $parameters ) { + $fields = array(); + $sql .= 'WHERE '; + foreach ( $parameters as $field => $value ) { + if ( $value == null ) { + $fields[] = $field.' IS NULL'; + } else if ( is_array( $value ) ) { + $func = function(){return '?';}; + $fields[] = $field.' IN ('.implode(',', array_map( $func, $value ) ). ')'; + $values += $value; + + } else { + $fields[] = $field.'=?'; + $values[] = $value; + } + } + $sql .= implode(' AND ', $fields ); + } + if ( $options ) { + if ( isset($options['order']) ) { + $sql .= ' ORDER BY ' . $options['order']; + } + if ( isset($options['limit']) ) { + if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) { + $sql .= ' LIMIT ' . $limit; + } else { + $backTrace = debug_backtrace(); + $file = $backTrace[1]['file']; + $line = $backTrace[1]['line']; + Error("Invalid value for limit($limit) passed to Server::find from $file:$line"); + return; + } + } } - if ( is_integer( $limit ) or ctype_digit( $limit ) ) { - $sql .= ' LIMIT ' . $limit; - } else { - $backTrace = debug_backtrace(); - $file = $backTrace[1]['file']; - $line = $backTrace[1]['line']; - Error("Invalid value for limit($limit) passed to Server::find from $file:$line"); - return; - } $results = dbFetchAll( $sql, NULL, $values ); if ( $results ) { return array_map( function($id){ return new Server($id); }, $results ); @@ -123,8 +109,8 @@ class Server { } public static function find_one( $parameters = array() ) { - $results = Server::find( $parameters, 1 ); - if ( ! sizeof( $results ) ) { + $results = Server::find( $parameters, array('limit'=>1) ); + if ( ! sizeof($results) ) { return; } return $results[0]; diff --git a/web/includes/Storage.php b/web/includes/Storage.php index 70f2f831d..bdd4bdf26 100644 --- a/web/includes/Storage.php +++ b/web/includes/Storage.php @@ -40,7 +40,6 @@ class Storage { $this->{'Path'} = ZM_DIR_EVENTS; } return $this->{'Path'}; - } return $this->{'Name'}; } @@ -53,19 +52,19 @@ class Storage { return $this->{'Name'}; } - public function __call( $fn, array $args= NULL){ - if ( count( $args ) ) { + public function __call( $fn, array $args= NULL ) { + if ( count($args) ) { $this->{$fn} = $args[0]; } - if ( array_key_exists( $fn, $this ) ) { + if ( array_key_exists($fn, $this) ) return $this->{$fn}; - $backTrace = debug_backtrace(); - $file = $backTrace[1]['file']; - $line = $backTrace[1]['line']; - Warning( "Unknown function call Storage->$fn from $file:$line" ); - } + $backTrace = debug_backtrace(); + $file = $backTrace[1]['file']; + $line = $backTrace[1]['line']; + Warning("Unknown function call Storage->$fn from $file:$line"); } + public static function find_one( $parameters = null, $options = null ) { global $storage_cache; if ( @@ -74,7 +73,7 @@ class Storage { isset($storage_cache[$parameters['Id']]) ) { return $storage_cache[$parameters['Id']]; } - $results = Storage::find_all( $parameters, $options ); + $results = Storage::find($parameters, $options); if ( count($results) > 1 ) { Error("Storage Returned more than 1"); return $results[0]; @@ -84,8 +83,7 @@ class Storage { return null; } } - public static function find_all( $parameters = null, $options = null ) { - $filters = array(); + public static function find( $parameters = null, $options = null ) { $sql = 'SELECT * FROM Storage '; $values = array(); @@ -95,7 +93,7 @@ class Storage { foreach ( $parameters as $field => $value ) { if ( $value == null ) { $fields[] = $field.' IS NULL'; - } else if ( is_array( $value ) ) { + } else if ( is_array($value) ) { $func = function(){return '?';}; $fields[] = $field.' IN ('.implode(',', array_map($func, $value)). ')'; $values += $value; @@ -106,32 +104,47 @@ class Storage { } } $sql .= implode(' AND ', $fields); - } - if ( $options and isset($options['order']) ) { - $sql .= ' ORDER BY ' . $options['order']; - } + } # end if parameters + if ( $options ) { + if ( isset($options['order']) ) { + $sql .= ' ORDER BY ' . $options['order']; + } # end if options + if ( isset($options['limit']) ) { + if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) { + $sql .= ' LIMIT ' . $limit; + } else { + $backTrace = debug_backtrace(); + $file = $backTrace[1]['file']; + $line = $backTrace[1]['line']; + Error("Invalid value for limit($limit) passed to Control::find from $file:$line"); + return; + } + } # end if limit + } # end if options + $storage_areas = array(); $result = dbQuery($sql, $values); if ( $result ) { $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Storage'); foreach ( $results as $row => $obj ) { - $filters[] = $obj; + $storage_areas[] = $obj; } } - return $filters; - } + return $storage_areas; + } # end find() + public function disk_usage_percent() { $path = $this->Path(); if ( ! $path ) { - Warning("Storage::disk_usage_percent: path is empty"); + Warning('Storage::disk_usage_percent: path is empty'); return 0; - } else if ( ! file_exists( $path ) ) { + } else if ( ! file_exists($path) ) { Warning("Storage::disk_usage_percent: path $path does not exist"); return 0; } $total = $this->disk_total_space(); if ( ! $total ) { - Error("disk_total_space returned false for " . $path ); + Error('disk_total_space returned false for ' . $path ); return 0; } $used = $this->disk_used_space(); @@ -139,6 +152,7 @@ class Storage { //Logger::Debug("Used $usage = round( ( $used / $total ) * 100 )"); return $usage; } + public function disk_total_space() { if ( !array_key_exists('disk_total_space', $this) ) { $path = $this->Path(); @@ -175,8 +189,8 @@ class Storage { if ( (! array_key_exists('DiskSpace', $this)) or (!$this->{'DiskSpace'}) ) { $used = dbFetchOne('SELECT SUM(DiskSpace) AS DiskSpace FROM Events WHERE StorageId=? AND DiskSpace IS NOT NULL', 'DiskSpace', array($this->Id()) ); - foreach ( Event::find_all( array('StorageId'=>$this->Id(), 'DiskSpace'=>null) ) as $Event ) { - $Event->Storage( $this ); // Prevent further db hit + foreach ( Event::find(array('StorageId'=>$this->Id(), 'DiskSpace'=>null)) as $Event ) { + $Event->Storage($this); // Prevent further db hit $used += $Event->DiskSpace(); } $this->{'DiskSpace'} = $used; diff --git a/web/skins/classic/includes/functions.php b/web/skins/classic/includes/functions.php index 66243daf3..f85d863c9 100644 --- a/web/skins/classic/includes/functions.php +++ b/web/skins/classic/includes/functions.php @@ -329,7 +329,7 @@ if ($reload == 'reload') ob_start(); ?>