null, 'Name' => array('type'=>'text','filter_regexp'=>'/[^\w\-\.\(\)\:\/ ]/u', 'default'=>'Group'), 'ParentId' => null, ); protected static $monitor_ids_cache = array(); public static function find( $parameters = array(), $options = array() ) { return ZM_Object::_find(self::class, $parameters, $options); } public static function find_one( $parameters = array(), $options = array() ) { return ZM_Object::_find_one(self::class, $parameters, $options); } public function delete() { if ( !property_exists($this, 'Id') or !$this->{'Id'} ) return false; if ( !dbQuery('DELETE FROM `Groups_Monitors` WHERE `GroupId`=?', array($this->{'Id'})) ) return false; # Children move up to this group's parent, read now as an earlier delete may have moved this group. $row = dbFetchOne('SELECT `ParentId` FROM `Groups` WHERE `Id`=?', NULL, array($this->{'Id'})); if ( !dbQuery('UPDATE `Groups` SET `ParentId`=? WHERE `ParentId`=?', array($row ? $row['ParentId'] : null, $this->{'Id'})) ) return false; if ( !dbQuery('DELETE FROM `Groups` WHERE Id=?', array($this->{'Id'})) ) return false; if ( isset($_COOKIE['zmGroup']) ) { if ( $this->{'Id'} == $_COOKIE['zmGroup'] ) { unset($_COOKIE['zmGroup']); zm_setcookie('zmGroup', ''); } } return true; } # end function delete() public function depth( $new = null ) { if ( isset($new) ) { $this->{'depth'} = $new; } if ( !property_exists($this, 'depth') or ($this->{'depth'} === null) ) { $this->{'depth'} = 0; if ( $this->{'ParentId'} != null ) { $Parent = Group::find_one(array('Id'=>$this->{'ParentId'})); $this->{'depth'} += $Parent->depth()+1; } } return $this->{'depth'}; } // end public function depth // Group membership feeds per-monitor access through Groups_Permissions, so moving a monitor // into or out of a group can grant access to it or lift a deny. Such a change needs edit on // every monitor it moves. Ids that are not monitors are ignored. public static function canEditMembership($monitor_ids, $u=null) { require_once(__DIR__.'/Monitor.php'); foreach (array_unique($monitor_ids) as $mid) { $monitor = Monitor::find_one(array('Id'=>$mid)); if ($monitor and !$monitor->canEdit($u)) return false; } return true; } // A group can sit under no parent, or under an existing group that is neither itself nor // one of its own descendants; anything else makes a loop that depth() never leaves. public static function validParent($group_id, $parent_id) { if ($parent_id === null or $parent_id === '') return true; if (!is_scalar($parent_id) or !ctype_digit((string)$parent_id)) return false; $seen = array(); $Parent = Group::find_one(array('Id'=>$parent_id)); if (!$Parent) return false; while ($Parent) { if ($group_id and $Parent->Id() == $group_id) return false; if (isset($seen[$Parent->Id()])) break; $seen[$Parent->Id()] = true; $Parent = $Parent->ParentId() ? Group::find_one(array('Id'=>$Parent->ParentId())) : null; } return true; } public function MonitorIds( ) { if (!property_exists($this, 'MonitorIds')) { if (!$this->{'Id'}) { return $this->{'MonitorIds'} = []; } if (!isset($monitor_ids_cache[$this->{'Id'}])) { $monitor_ids_cache[$this->{'Id'}] = dbFetchAll('SELECT `MonitorId` FROM `Groups_Monitors` WHERE `GroupId`=?', 'MonitorId', array($this->{'Id'})); if (count($this->Children())) { foreach ($this->Children() as $g) { $child_monitor_ids = $g->MonitorIds(); $monitor_ids_cache[$this->{'Id'}] = array_values(array_unique(array_merge($monitor_ids_cache[$this->{'Id'}], $child_monitor_ids))); } } } $this->{'MonitorIds'} = &$monitor_ids_cache[$this->{'Id'}]; } return $this->{'MonitorIds'}; } public static function get_group_dropdown( $view = null ) { // Get selected value from cookie $selectedValue = null; if (isset($_COOKIE['zmFilter_GroupId'])) { $cookieValue = $_COOKIE['zmFilter_GroupId']; if ($cookieValue && $cookieValue !== '') { // Try to decode JSON for array values $decoded = json_decode($cookieValue, true); $selectedValue = ($decoded !== null) ? $decoded : $cookieValue; } } // Use monitorFilterOnChange on console view for AJAX refresh, submitThisForm elsewhere $onChangeFunction = ($view == 'console') ? 'monitorFilterOnChange' : 'submitThisForm'; return htmlSelect('GroupId[]', Group::get_dropdown_options(), $selectedValue, array( 'data-on-change' => $onChangeFunction, 'class'=>'chosen', 'multiple'=>'multiple', 'data-placeholder'=>'All', ) ); } # end public static function get_group_dropdown public static function get_dropdown_options() { $Groups = array(); foreach ( Group::find(array(), array('order'=>'lower(Name)')) as $Group ) { if ($Group->canView()) $Groups[$Group->Id()] = $Group; } # This array is indexed by parent_id $children = array(); foreach ( $Groups as $id=>$Group ) { if ( $Group->ParentId() != null ) { if ( ! isset( $children[$Group->ParentId()] ) ) $children[$Group->ParentId()] = array(); $children[$Group->ParentId()][] = $Group; } } # end foreach # A named function declared here is declared again on every call, which is fatal. $get_options = function($Group) use (&$get_options, &$children) { $options = array($Group->Id() => str_repeat('   ', $Group->depth()) . $Group->Name()); if ( isset($children[$Group->Id()]) ) { foreach ( $children[$Group->Id()] as $child ) { $options += $get_options($child); } } return $options; }; # end function get_options $group_options = array(); foreach ( $Groups as $id=>$Group ) { if ( ! $Group->ParentId() ) { $group_options += $get_options($Group); } } return $group_options; } # end function get_dropdown_options public static function get_group_sql($group_id) { $groupSql = ''; if ( $group_id ) { # The selection can outlive its group (the filter cookie after a delete). $selected = is_array($group_id) ? array_values($group_id) : array($group_id); $group_id = dbFetchAll('SELECT `Id` FROM `Groups` WHERE `Id` IN ('.implode(',', array_map(function(){return '?';}, $selected)).')', 'Id', $selected); } if ( $group_id ) { $group_ids = is_array($group_id) ? array_values($group_id) : array($group_id); $all_ids = $group_ids; $seen = array_flip($all_ids); $parent_ids = $group_ids; while (count($parent_ids)) { $child_ids = dbFetchAll('SELECT `Id` FROM `Groups` WHERE `ParentId` IN ('.implode(',', array_map(function(){return '?';}, $parent_ids)).')', 'Id', $parent_ids); $parent_ids = array(); foreach ($child_ids as $child_id) { if (isset($seen[$child_id])) continue; $seen[$child_id] = true; $all_ids[] = $child_id; $parent_ids[] = $child_id; } } $MonitorIds = dbFetchAll('SELECT `MonitorId` FROM `Groups_Monitors` WHERE `GroupId` IN ('.implode(',', array_map(function(){return '?';}, $all_ids)).')', 'MonitorId', $all_ids); $groupSql = " find_in_set( M.Id, '".implode(',', $MonitorIds)."' )"; } return $groupSql; } # end public static function get_group_sql( $group_id ) public static function get_monitors_dropdown($options = null) { $monitor_id = 0; if ( isset($_REQUEST['monitor_id']) ) { $monitor_id = $_REQUEST['monitor_id']; } else if ( isset($_COOKIE['zmMonitorId']) ) { $monitor_id = $_COOKIE['zmMonitorId']; } $sql = 'SELECT `Id`,`Name` FROM `Monitors`'; if ( $options ) { $sql .= ' WHERE '. implode(' AND ', array( '`Deleted` != 1', ( isset($options['groupSql']) ? $options['groupSql']:'') ) ).' ORDER BY `Sequence` ASC'; } $monitors_dropdown = array(''=>'All'); foreach ( dbFetchAll($sql) as $monitor ) { if ( !visibleMonitor($monitor['Id']) ) { continue; } $monitors_dropdown[$monitor['Id']] = $monitor['Name']; } echo htmlSelect('monitor_id', $monitors_dropdown, $monitor_id, array('data-on-change-this'=>'changeMonitor')); return $monitor_id; } public function Parent( ) { if ( $this->{'ParentId'} ) { return Group::find_one(array('Id'=>$this->{'ParentId'})); } return null; } public function Parents() { $Parents = array(); $Parent = $this->Parent(); $seen_parents = array(); while ($Parent) { $seen_parents[$Parent->Id()] = $Parent; array_unshift($Parents, $Parent); $Parent = $Parent->Parent(); if ($Parent and isset($seen_parents[$Parent->Id()])) { Warning("Detected hierarchy loop in group {$Parent->Name()}"); break; } } return $Parents; } public function Children() { if (!property_exists($this, 'Children')) { $this->{'Children'} = Group::find(array('ParentId'=>$this->Id())); } return $this->{'Children'}; } public function Monitors() { if (!property_exists($this, 'Monitors') ) { $monitor_ids = $this->MonitorIds(); if (count($monitor_ids)) { $this->{'Monitors'} = Monitor::find(array('Id'=>$monitor_ids, 'Deleted'=>0)); } else { $this->{'Monitors'} = array(); } } if (count($this->Children())) { foreach ($this->Children() as $g) $this->{'Monitors'} += $g->Monitors(); } return $this->{'Monitors'}; } public function canView($u=null) { global $user; if (!$u) $u = $user; if (!count($this->Monitors()) and !count($this->Children())) { return true; } # Can view if we can view any of the monitors in it. foreach ($this->Monitors() as $monitor) { if ($monitor->canView($u)) return true; } foreach ($this->Children() as $child) { if ($child->canView($u)) return true; } return false; } public function Permissions($new=null) { if ($new) { $this->{'Permissions'} = $new; } if (!property_exists($this, 'Permissions') or $this->{'Permissions'}==null) { $this->{'Permissions'} = array(); if ($this->Id()) { foreach (Group_Permission::find(array('GroupId'=>$this->Id())) as $p) { $this->{'Permissions'}[$p->UserId()] = $p; } } } return $this->{'Permissions'}; } public function Group_Permission($user_id) { $permissions = $this->Permissions(); if (isset($permissions[$user_id])) { return $permissions[$user_id]; } $permission = new Group_Permission(); $permission->UserId($user_id); $permission->GroupId($this->Id()); return $permission;; } public function permission($user_id) { $permission = $this->Group_Permission($user_id); return $permission ? $permission->Permission() : 'Inherit'; } } # end class Group ?>