From 0b380e56ad794821387ae0bd60ae71d09a48f77a Mon Sep 17 00:00:00 2001 From: psc Date: Fri, 5 Mar 2010 13:22:19 +0000 Subject: [PATCH] Fix for #144 - Improve tests::is_in_ip_range: - Check if the specified address is a valid address at all, otherwise return FALSE - If both 'to' and 'from' are set to '*' or empty return TRUE - Add new function ldap_equality_check - This function compares a string similar as a LDAP equality match would do. It does so by replacing any occurence of a * with a regex pattern and test it with preg_match. The default mapping is but the test can be extended by changing that mapping, so that the asterisk can stand for a certain range of chars for example. - Add new filters for MAC-Address, FAI-Classes and FAI-release based on this. - Add a new function AddTextCheckbox. This is similar to AddCheckbox but also has a parameter regexes that gets a list of regexes (as returned by AddRegex with the return_regex=TRUE parameter) which are displayed directly after the checkbox. - Add a function Draw() that gets a smarty object from MultiSelectWindow::Draw and additionally renders the special filters with a special template. - Make save_object() handle the somewhat special structure of this->array_TextCheckboxes which stores the TextCheckboxes as created by AddTextCheckbox - Make filter_iprange (and its text field ifrom and ipto) use the nex AddCheckBox feature. - Add a template for the event target add list - Add a new parameter to AddRegex so that it is possible to get a regex array instead of returning the objects array_Regexes - Add new functions GetCheckbox and GetRegex that contain the code from the Draw() function to draw textfields and checkboxes. This enables inheritting implementations to use these functions when altering the behaviour. - Allow Draw function to return smarty object This change allows to change the behaviour of the Draw function to return a smarty object. If the class that inherits from MultiSelectWindow sets $this->DrawReturnsSmartyObject to a true value a smarty object is returned instead of html. A certain class can now alter this smarty object and after that fetch the HTML itself. git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.6-lhm@16231 594d385d-05f5-0310-b6e9-bd551577e9d8 --- .../include/class_MultiSelectWindow.inc | 120 +++++--- trunk/gosa-core/include/functions.inc | 29 ++ trunk/gosa-core/include/utils/class_tests.inc | 8 + .../addons/goto/events/EventTargetAddList.tpl | 149 ++++++++++ .../goto/events/class_EventTargetAddList.inc | 268 +++++++++++++++++- 5 files changed, 519 insertions(+), 55 deletions(-) create mode 100644 trunk/gosa-plugins/goto/addons/goto/events/EventTargetAddList.tpl diff --git a/trunk/gosa-core/include/class_MultiSelectWindow.inc b/trunk/gosa-core/include/class_MultiSelectWindow.inc index ba2e4629a..8aae53bb7 100644 --- a/trunk/gosa-core/include/class_MultiSelectWindow.inc +++ b/trunk/gosa-core/include/class_MultiSelectWindow.inc @@ -70,6 +70,10 @@ class MultiSelectWindow{ var $footer = ""; var $post_id = "1 2 3"; + /*! \brief If this is set to TRUE the Draw() function returns a smarty object */ + var $DrawReturnsSmartyObject = FALSE; + + function ClearElementsList() { $ui =get_userinfo(); @@ -92,8 +96,8 @@ class MultiSelectWindow{ } /* Adds a regex input field to the current dialog */ - function AddRegex($name,$string,$value,$conn,$image="images/lists/search.png") - { + function AddRegex($name,$string,$value,$conn,$image="images/lists/search.png", $return_array=FALSE) + { $arr = array(); /* Check if the given input field name was already used @@ -109,8 +113,13 @@ class MultiSelectWindow{ $arr['name'] = $name; $arr['string'] = $string; $arr['image'] = $image; - $arr['connAlpha'] = $conn; // Connect with alphabet select - $this->array_Regexes[] = $arr; + $arr['connAlpha'] = $conn; // Connect with alphabet select + if (!$return_array) { + $this->array_Regexes[] = $arr; + } + else { + return($arr); + } } @@ -369,7 +378,60 @@ class MultiSelectWindow{ $this->IgnoreAccount = !$ignore; } - /*! \brief Draw the list with all list elements and filters */ + function GetCheckbox($box, $start_table=FALSE) + { + $ret = ""; + $boxClick = " onClick='document.mainform.submit();' "; + if($box['name'] == SEPERATOR){ + $ret = "
"; + return($ret); + } + + + /* Skip disabled boxes */ + if(!$box['enabled']) return; + + /* Check if box is checked */ + if($box['default'] == true){ + $ret .=" ".$box['string']."
"; + }else{ + $ret .=" ".$box['string']."
"; + } + + return($ret); + } + + function GetRegex($regex, $draw_line=TRUE) + { + $ret = ""; + $line_css = ""; + if ($draw_line) { + $line_css = "solid #B0B0B0;"; + } + $ret=" + + + + +
+ + + +
"; + + return($ret); + } + + + /*! \brief Draw the list with all list elements and filters + * + * Returns a string with the HTML code that should be displayed. + * If this->DrawReturnsSmartyObject is TRUE (default: FALSE) it + * returns a smarty object. + * */ function Draw() { @@ -414,44 +476,17 @@ class MultiSelectWindow{ /* Create checkboxes fields */ $boxes = ""; - $boxClick = " onClick='document.mainform.submit();' "; - foreach($this->array_Checkboxes as $box){ - - if($box['name'] == SEPERATOR){ - $boxes .= "
"; - continue; - } - - /* Skip disabled boxes */ - if(!$box['enabled']) continue; - - /* Check if box is checked */ - if($box['default'] == true){ - $boxes .=" ".$box['string']."
"; - }else{ - $boxes .=" ".$box['string']."
"; - } - } + foreach($this->array_Checkboxes as $box){ + $boxes .= $this->GetCheckbox($box); + } $smarty->assign("CheckBoxes", $boxes); /* Assign regex fields */ $regexes = ""; - foreach($this->array_Regexes as $regex){ - $regexes.=" - - - - -
- - - -
"; - } + foreach($this->array_Regexes as $regex){ + $regexes .= $this->GetRegex($regex); + } $smarty->assign("regexes" , $regexes ); /* Hide Filter Part if Requested or empty */ @@ -499,8 +534,13 @@ class MultiSelectWindow{ $smarty->assign("filterName" , $this->filterName); $smarty->assign("is_headpage" , $this->is_headpage); - $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl")); - return($display); + if (!$this->DrawReturnsSmartyObject) { + $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl")); + return($display); + } + else { + return($smarty); + } } /*! \brief Set the close var (simulates a press of the the close button) */ diff --git a/trunk/gosa-core/include/functions.inc b/trunk/gosa-core/include/functions.inc index 3bf03eab2..62869d6b0 100644 --- a/trunk/gosa-core/include/functions.inc +++ b/trunk/gosa-core/include/functions.inc @@ -3225,6 +3225,35 @@ function set_object_info($str = "") session::set('objectinfo',$str); } +/*! \brief Compare two strings similar as an LDAP search would do it + * + * This function compares a string similar as a LDAP equality match + * would do. It does so by replacing any occurence of a * with a + * regex pattern and test it with preg_match. The default mapping is '.*' + * but the test can be extended by changing that mapping, so that the asterisk + * can stand for a certain range of chars for example. + * + * Example: + * \code + * ldap_equality_check('halut', '*'); # true + * ldap_equality_check('halut/1.2.3', 'halut/*'); # true + * ldap_equality_check('halut/1.2.3', 'hal\*\/1.2.3'); # true + * \endcode + * + * */ +function ldap_equality_check($string1, $string2, $mapping=".*", $case_sensitive=TRUE) { + if (!(strstr($string2, '*')) && ($string1 == $string2)) { + return TRUE; + } + + /* Set modifier string to i if case insensitivity is requested */ + $modifier = $case_sensitive ? "" : "i"; + $string2 = preg_quote($string2, "/"); + $string2 = str_replace('\*', $mapping, $string2); + + $result = preg_match('/' . $string2 . '$/' . $modifier, $string1); + return $result; +} // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: ?> diff --git a/trunk/gosa-core/include/utils/class_tests.inc b/trunk/gosa-core/include/utils/class_tests.inc index a34eac4ca..1be764926 100644 --- a/trunk/gosa-core/include/utils/class_tests.inc +++ b/trunk/gosa-core/include/utils/class_tests.inc @@ -262,6 +262,14 @@ class tests { /* \brief Check if the specified IP address $address is inside the given network */ public static function is_in_ip_range($from, $to, $address) { + if (!preg_match("/[0-2]?[0-9]?[0-9].[0-2]?[0-9]?[0-9].[0-2]?[0-9]?[0-9].[0-2]?[0-9]?[0-9]/", $address)) { + return FALSE; + } + + if (($from == '*' && $to == '*') || ($from == '' && $to == '')) { + return TRUE; + } + $from = split('\.', $from); $to = split('\.', $to); $ad = split('\.', $address); diff --git a/trunk/gosa-plugins/goto/addons/goto/events/EventTargetAddList.tpl b/trunk/gosa-plugins/goto/addons/goto/events/EventTargetAddList.tpl new file mode 100644 index 000000000..27d5d765a --- /dev/null +++ b/trunk/gosa-plugins/goto/addons/goto/events/EventTargetAddList.tpl @@ -0,0 +1,149 @@ + + + + + + + + + {if $is_headpage} + + {else} + + {/if} + +
+
+

+ {$Summary} {$hint} +

+
+
+
+ + + + {if $HeaderDropDown != ""} + + + {/if} + +
+ {$Header} + + - + + + {$HeaderDropDown} +
+
+
+
+
+
+ {$DivList} + +
+
+ {$UserBox1} + {if $Information != ""} +
+

+ [i]{t}Information{/t} +

+
+
+ {$Information} +
+
+ {/if} + {$UserBox2} + {if !$Skip_Filter_Part } +
+

+ [F]{t}Filters{/t} +

+
+
+ {if $Display_alphabet} + + {$alphabet} +
+ {/if} + { if $CheckBoxes != ""} + + + + +
+ {$CheckBoxes} +
+ {/if} + {$regexes} + {$special_filters} + {$apply} +
+ {$UserBox3} + {/if} +
+ {$UserBox1} +
+

+ [i]{t}Information{/t} +

+
+
+ {$Information} +
+ {$UserBox2} +
+ {if !$Skip_Filter_Part } +
+

+ [F]{t}Filters{/t} +

+
+
+ {if $Display_alphabet} + + {$alphabet} +
+ {/if} + { if $CheckBoxes != ""} + + + + +
+ {$CheckBoxes} +
+ {/if} + {$regexes} + {$special_filters} + {$apply} + {$UserBox3} +
+ {/if} +
+{if $Display_Save | $Display_Close} +

  +

+
+

+ {if $Display_Save} + + {/if} + {if $Display_Close} + + {/if} +

+
+{/if} +{if $IgnoreAccount} + +{/if} + + diff --git a/trunk/gosa-plugins/goto/addons/goto/events/class_EventTargetAddList.inc b/trunk/gosa-plugins/goto/addons/goto/events/class_EventTargetAddList.inc index fbc32992a..fc8e17cad 100644 --- a/trunk/gosa-plugins/goto/addons/goto/events/class_EventTargetAddList.inc +++ b/trunk/gosa-plugins/goto/addons/goto/events/class_EventTargetAddList.inc @@ -26,15 +26,26 @@ class EventTargetAddList extends MultiSelectWindow public $display_workstation = TRUE; public $display_ogroup = TRUE; public $filter_iprange = FALSE; + public $filter_mac_addr = FALSE; + public $filter_fai_release = FALSE; + public $filter_fai_class = FALSE; public $regex = "*"; public $ipfrom = "0.0.0.0"; public $ipto = "*"; + public $mac_addr = "*:*:*:*:*:*"; + public $fai_release = "*"; + public $fai_class = "*"; public $_target_list = array(); public $workstation_list = array(); public $server_list = array(); + public $array_TextCheckboxes = array(); + + public $SubSearch = FALSE; + /* Define that Draw has to return a smarty object */ + public $DrawReturnsSmartyObject = TRUE; function __construct(&$config,$parent) { @@ -67,15 +78,31 @@ class EventTargetAddList extends MultiSelectWindow //$name,$string,$value,$conn,$image="images/lists/search.png") $this->AddRegex("regex" ,"regex" ,"*" , TRUE); - $this->AddRegex("ipfrom","ipfrom" ,"0.0.0.0" , FALSE); - $this->AddRegex("ipto" ,"ipto" ,"255.255.255.255" , FALSE); $this->AddCheckBox("display_server","1" ,_("Display server"),TRUE); $this->AddCheckBox("display_workstation","1",_("Display workstation"),TRUE); $this->AddCheckBox("display_ogroup","1" ,_("Display object groups"),TRUE); - $this->AddCheckBox("filter_iprange","1" ,_("Filter by IP range"),FALSE); + + /* Add SubSearch checkbox */ + $this->AddCheckBox(SEPERATOR); + $this->AddCheckBox("SubSearch", msgPool::selectToView("","subsearch"), msgPool::selectToView("","subsearch_small"), false); + + $regexes = array(); + $regexes[] = $this->AddRegex("ipfrom","ipfrom" ,"0.0.0.0" , FALSE, "images/lists/search.png", TRUE); + $regexes[] = $this->AddRegex("ipto" ,"ipto" ,"255.255.255.255" , FALSE, "images/lists/search.png", TRUE); + $this->AddTextCheckBox("filter_iprange","1" ,_("Filter by IP range"),FALSE, $regexes); + $regexes = array(); + $regexes[] = $this->AddRegex("mac_addr", "mac_addr", "*:*:*:*:*:*", FALSE, "images/lists/search.png", TRUE); + $this->AddTextCheckBox("filter_mac_addr", "1", _("Filter by MAC address"), FALSE, $regexes); + $regexes = array(); + $regexes[] = $this->AddRegex("fai_release", "fai_release", "*", FALSE, "images/lists/search.png", TRUE); + $this->AddTextCheckBox("filter_fai_release", "1", _("Filter by FAI release"), FALSE, $regexes); + + $regexes = array(); + $regexes[] = $this->AddRegex("fai_class", "fai_class", "*", FALSE, "images/lists/search.png", TRUE); + $this->AddTextCheckBox("filter_fai_class", "1", _("Filter by FAI class"), FALSE, $regexes); /* Create a list of servers */ @@ -194,22 +221,43 @@ class EventTargetAddList extends MultiSelectWindow { $_target_list = array(); if($this->display_server){ - $_target_list = array_merge($_target_list, - get_list("(&(cn=".$this->regex.")(objectClass=goServer))", - "server",get_ou("serverRDN").$this->selectedBase, - array("cn","objectClass","description","ipHostNumber","macAddress"),GL_NONE)); + if (!$this->SubSearch) { + $_target_list = array_merge($_target_list, + get_list("(&(cn=".$this->regex.")(objectClass=goServer))", + "server",get_ou("serverRDN").$this->selectedBase, + array("cn","objectClass","description","ipHostNumber","macAddress", "FAIclass"),GL_NONE)); + }else { + $_target_list = array_merge($_target_list, + get_sub_list("(&(cn=".$this->regex.")(objectClass=goServer))", + "server",get_ou("serverRDN"),$this->selectedBase, + array("cn","objectClass","description","ipHostNumber","macAddress"),GL_SUBSEARCH)); + } } if($this->display_workstation){ - $_target_list = array_merge($_target_list, - get_list("(&(cn=".$this->regex.")(objectClass=gotoWorkstation))", - "workstation",get_ou("workstationRDN").$this->selectedBase, - array("cn","objectClass","description","ipHostNumber","macAddress"),GL_NONE)); + if (!$this->SubSearch) { + $_target_list = array_merge($_target_list, + get_list("(&(cn=".$this->regex.")(objectClass=gotoWorkstation))", + "workstation",get_ou("workstationRDN").$this->selectedBase, + array("cn","objectClass","description","ipHostNumber","macAddress", "FAIclass"),GL_NONE)); + }else { + $_target_list = array_merge($_target_list, + get_sub_list("(&(cn=".$this->regex.")(objectClass=gotoWorkstation))", + "workstation",get_ou("workstationRDN"), $this->selectedBase, + array("cn","objectClass","description","ipHostNumber","macAddress", "FAIclass"),GL_SUBSEARCH)); + } } if($this->display_ogroup){ - $_target_list = array_merge($_target_list, - get_list("(&(cn=".$this->regex.")(member=*)(objectClass=gosaGroupOfNames))", - "ogroups",get_ou("ogroupRDN").$this->selectedBase, - array("cn","objectClass","description","member"),GL_NONE)); + if (!$this->SubSearch) { + $_target_list = array_merge($_target_list, + get_list("(&(cn=".$this->regex.")(member=*)(objectClass=gosaGroupOfNames))", + "ogroups",get_ou("ogroupRDN").$this->selectedBase, + array("cn","objectClass","description","member", "FAIclass"),GL_NONE)); + }else { + $_target_list = array_merge($_target_list, + get_sub_list("(&(cn=".$this->regex.")(member=*)(objectClass=gosaGroupOfNames))", + "ogroups",get_ou("ogroupRDN"), $this->selectedBase, + array("cn","objectClass","description","member", "FAIclass"),GL_SUBSEARCH)); + } } $this->_target_list = $_target_list; @@ -242,6 +290,52 @@ class EventTargetAddList extends MultiSelectWindow continue; } } + + if($this->filter_mac_addr) { + if(!isset($obj['macAddress']) || !ldap_equality_check($obj['macAddress'][0], $this->mac_addr, "[0-9A-F]{2}", FALSE)) { + continue; + } + } + + if($this->filter_fai_release) { + if(isset($obj['FAIclass'])) { + $fai_release = split(':', $obj['FAIclass'][0]); + if (isset($fai_release[1])) { + $fai_release = $fai_release[1]; + } else { + continue; + } + + if (!ldap_equality_check($fai_release, $this->fai_release)) { + continue; + } + } + else { + continue; + } + } + + if ($this->filter_fai_class) { + if(isset($obj['FAIclass'])) { + $fai_classes = split(':', $obj['FAIclass'][0]); + $fai_classes = split(' ', $fai_classes[0]); + + $found = FALSE; + foreach($fai_classes as $used_class) { + if(ldap_equality_check($used_class, $this->fai_class)){ + $found = TRUE; + break; + } + } + if (!$found) { + continue; + } + } + else { + continue; + } + } + if(!isset($this->server_list[$obj['dn']])){ continue; } @@ -252,10 +346,93 @@ class EventTargetAddList extends MultiSelectWindow continue; } } + + if($this->filter_mac_addr){ + if(!isset($obj['macAddress']) || !ldap_equality_check($obj['macAddress'][0], $this->mac_addr, "[0-9A-F]{2}", FALSE)) { + continue; + } + } + + if($this->filter_fai_release) { + if (isset($obj['FAIclass'])) { + $fai_release = split(':', $obj['FAIclass'][0]); + if (isset($fai_release[1])) { + $fai_release = $fai_release[1]; + } else { + continue; + } + if (!ldap_equality_check($fai_release, $this->fai_release)) { + continue; + } + } + else { + continue; + } + } + + if ($this->filter_fai_class) { + if(isset($obj['FAIclass'])) { + $fai_classes = split(':', $obj['FAIclass'][0]); + $fai_classes = split(' ', $fai_classes[0]); + + $found = FALSE; + foreach($fai_classes as $used_class) { + if(ldap_equality_check($used_class, $this->fai_class)){ + $found = TRUE; + break; + } + } + if (!$found) { + continue; + } + } + else { + continue; + } + } + if(!isset($this->workstation_list[$obj['dn']])){ continue; } }elseif(in_array("gosaGroupOfNames",$obj['objectClass'])){ + if($this->filter_fai_release) { + if (isset($obj['FAIclass'])) { + $fai_release = split(':', $obj['FAIclass'][0]); + if (isset($fai_release[1])) { + $fai_release = $fai_release[1]; + } else { + continue; + } + if (!ldap_equality_check($fai_release, $this->fai_release)) { + continue; + } + } + else { + continue; + } + } + + if ($this->filter_fai_class) { + if(isset($obj['FAIclass'])) { + $fai_classes = split(':', $obj['FAIclass'][0]); + $fai_classes = split(' ', $fai_classes[0]); + + $found = FALSE; + foreach($fai_classes as $used_class) { + if(ldap_equality_check($used_class, $this->fai_class)){ + $found = TRUE; + break; + } + } + if (!$found) { + continue; + } + } + else { + continue; + } + } + $img = 'O'; } @@ -268,6 +445,42 @@ class EventTargetAddList extends MultiSelectWindow } } + function AddTextCheckbox($name,$value="Unset", $string="Unset", $default=false, $regexes=array()) + { + $arr = array(); + $MultiDialogFilters = session::get('MultiDialogFilters'); + if(isset($MultiDialogFilters[$this->filterName][$name])){ + $arr['default'] = $MultiDialogFilters[$this->filterName][$name]; + $this->$name = $arr['default']; + }else { + $arr['default'] = $default; + $this->$name = $default; + } + $arr['name'] = $name; + $arr['string'] = $string; + $arr['value'] = $value; + $arr['enabled'] = true; + $arr['regexes'] = $regexes; + + $this->array_TextCheckboxes[] = $arr; + + } + + function Draw() { + $smarty = MultiSelectWindow::Draw(); + $special_filter = ""; + foreach($this->array_TextCheckboxes as $textcheckbox) { + $special_filter .= "
"; + $special_filter .= $this->GetCheckbox($textcheckbox); + foreach($textcheckbox['regexes'] as $regex) { + $special_filter .= $this->GetRegex($regex, FALSE); + } + } + $special_filter .= "
"; + $smarty->assign("special_filters" , $special_filter); + $display = $smarty->fetch(get_template_path('EventTargetAddList.tpl', TRUE, dirname(__FILE__))); + return($display); + } /*! \brief Returns a set of elements selected in a MultiSelectWindow @return Array[integer]=integer @@ -283,6 +496,31 @@ class EventTargetAddList extends MultiSelectWindow } return($ids); } + + function save_object() + { + MultiSelectWindow::save_object(); + if(isset($_POST['MultiSelectWindow'.$this->filterName])){ + foreach($this->array_TextCheckboxes as $key => $box) { + $MultiDialogFilters = session::get('MultiDialogFilters'); + if(isset($_POST[$box['name']])){ + $this->array_TextCheckboxes[$key]['default'] = true; + $this->$box['name'] = true; + }else{ + $this->array_TextCheckboxes[$key]['default'] = false; + $this->$box['name'] = false; + } + $MultiDialogFilters[$this->filterName][$box['name']] = $this->$box['name']; + + foreach($this->array_TextCheckboxes[$key]['regexes'] as $regex_key => $regex_box) { + $this->array_TextCheckboxes[$key]['regexes'][$regex_key]['value'] = $_POST[$regex_box['name']]; + $this->$regex_box['name'] = $_POST[$regex_box['name']]; + $MultiDialogFilters[$this->filterName][$regex_box['name']] = $this->$regex_box['name']; + } + session::set('MultiDialogFilters',$MultiDialogFilters); + } + } + } } // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: ?> -- 2.30.2