1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class target_list extends MultiSelectWindow
24 {
25 var $config;
26 var $list =array();
27 var $Targets_used =array();
29 /* Current base */
30 var $selectedBase = "";
31 var $departments = array();
33 /* Regex */
34 var $Regex = "*";
35 var $IP_start = "0.0.0.0";
36 var $IP_end = "255.255.255.255";
38 /* CheckBoxes, to change default values modify $this->AddCheckBox */
39 var $ogroups ;
40 var $servers ;
41 var $workstations ;
42 var $incoming ;
45 /* Subsearch checkbox */
46 var $SubSearch ;
47 var $IPMatch ;
48 var $parent ;
49 var $ui ;
52 function target_list(&$config,$Targets_used)
53 {
54 MultiSelectWindow::MultiSelectWindow($config, "Targetselection", array("ogroup","server","incoming","workstation","gotomasses"));
56 $this->Targets_used = $Targets_used;
58 $this->SetInformation( _("Select the target objects for your scheduled action."));
59 $this->SetTitle( _("Available targets"));
60 $this->SetSummary( _("Available targets"));
61 $this->SetHeadpageMode(FALSE);
63 /* set Page header */
64 $chk = "<input type='checkbox' id='select_all' name='select_all'
65 onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
66 $this->AddHeader(array("string"=> $chk, "attach"=>"style='width:20px;'"));
68 $this->AddHeader(array("string" => " ", "attach" => "style='text-align:center;width:20px;'"));
69 $this->AddHeader(array("string" => _("Object name"), "attach" => "style=''"));
71 /* Text ,Value, Name, Is selected */
72 $this->AddCheckBox("ogroups", _("Select to see object groups"), _("Show object groups"), true);
73 $this->AddCheckBox("servers", _("Select to see servers") , _("Show servers"), true);
74 $this->AddCheckBox("workstations", _("Select to see workstations"),_("Show workstations"), true);
75 $this->AddCheckBox("incoming", _("Select to see incoming objects") , _("Show new objects"), true);
77 /* Add SubSearch checkbox */
78 $this->AddCheckBox(SEPERATOR);
79 $this->AddCheckBox("SubSearch", msgPool::selectToView("","subsearch"), msgPool::selectToView("","subsearch_small"), false);
80 $this->AddCheckBox("IPMatch", _("Select to search for a specific IP range only"), _("Match IP range"), false);
82 /* Name,Text,Default , Connect with alphabet */
83 $this->AddRegex ("Regex", _("Regular expression for matching group names"), "*" , true);
84 $this->AddRegex ("IP_start", _("IP range start"), "0.0.0.0" , true);
85 $this->AddRegex ("IP_end", _("IP range end"), "255.255.255.255" , true);
86 $this->EnableAplhabet(TRUE);
87 }
90 function GenHeader()
91 {
92 $options= "";
94 /* Get all departments within this subtree */
95 $ui= get_userinfo();
96 $first = "";
97 $found = FALSE;
98 $base = $this->config->current['BASE'];
100 /* Add base */
101 $tmp = array();
102 $tmp[] = array("dn"=>$this->config->current['BASE']);
103 $tmp= array_merge($tmp,get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->module, $base,
104 array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
106 $deps = array();
107 foreach($tmp as $tm){
108 $deps[$tm['dn']] = $tm['dn'];
109 }
111 /* Load possible departments */
112 $ui= get_userinfo();
113 $tdeps= $ui->get_module_departments("ogroups");
114 $ids = $this->config->idepartments;
115 $first = "";
116 $found = FALSE;
117 foreach($ids as $dep => $name){
118 if(isset($deps[$dep]) && in_array_ics($dep, $tdeps)){
120 /* Keep first base dn in mind, we could need this
121 * info if no valid base was found
122 */
123 if(empty($first)) {
124 $first = $dep['dn'];
125 }
127 $value = $ids[$dep];
128 if ($this->selectedBase == $dep){
129 $found = TRUE;
130 $options.= "<option selected='selected' value='".$dep."'>$value</option>";
131 } else {
132 $options.= "<option value='".$dep."'>$value</option>";
133 }
134 }
135 }
137 /* The currently used base is not visible with your acl setup.
138 * Set base to first useable base.
139 */
140 if(!$found){
141 $this->selectedBase = $first;
142 }
144 /* Get copy & paste icon */
145 $acls = $ui->get_permissions($this->selectedBase,"ogroups/ogroup");
146 $acl_all= $ui->has_complete_category_acls($this->selectedBase,"ogroups");
148 /* Add default header */
149 $listhead = MultiSelectWindow::get_default_header();
151 /* Add department selector */
152 $listhead .= _("Base")." <select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
153 " <input class='center' type='image' src='images/list_submit.png' align='middle'
154 title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ";
155 $listhead .="</div>";
157 $this->SetListHeader($listhead);
158 }
161 function execute()
162 {
163 $this->ClearElementsList();
164 $this->GenHeader();
165 $this->reload();
166 $this->SetEntries($this->list);
167 return($this->Draw());
168 }
171 function SetEntries($list)
172 {
173 /* Add Copy & Paste buttons if copy&paste is enabled
174 */
175 // Defining Links
176 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
177 $editlink = "<a href='?plug=".$_GET['plug']."&id=%s&act=edit_entry'>%s</a>";
179 $ui = get_userinfo();
181 // Assigning ogroups
182 foreach($list as $key => $val){
184 if(in_array($val['cn'][0],$this->Targets_used) ||
185 isset($val['macAddress'][0]) && in_array($val['macAddress'][0],$this->Targets_used)) continue;
187 $title = "title='".preg_replace('/ /', ' ', @LDAP::fix($val['dn']))."'";
188 if(!isset($val['description'][0])){
189 $desc = "";
190 }else{
191 $desc = " - [ ".$val['description'][0]." ]";
192 }
193 if(!isset($val['ipHostNumber'][0])){
194 $desc.= "";
195 }else{
196 $desc.= " - ".$val['ipHostNumber'][0]."";
197 }
200 $img ="Hmm";
201 if(in_array("goServer",$val['objectClass'])){
202 $img = "<img src='images/select_server.png' alt='"._("Server")."' ".$title.">";
203 }elseif(in_array("gotoWorkstation",$val['objectClass'])){
204 $img = "<img src='images/select_workstation.png' alt='"._("Workstation")."' ".$title.">";
205 }elseif(in_array("gosaGroupOfNames",$val['objectClass'])){
206 $img = "<img src='images/list_ogroup.png' alt='"._("Object group")."' ".$title.">";
207 }
209 /* Create each field */
210 $field0 = array("string" => "<input type='checkbox' id='item_selected_".$key."' name='item_selected_".$key."'>" ,
211 "attach" => "style='width:20px;'");
212 $field1 = array("string" => $img,
213 "attach" => "style='text- align:center;width: 20px;'");
214 $field2 = array("string" => $val['cn'][0].$desc,
215 "attach" => "style='border-right:0px;' ".$title);
217 $this->AddElement(array($field0,$field1,$field2));
218 }
219 }
222 function save()
223 {
224 $ret = array();
225 $items = $this->list_get_selected_items();
226 foreach($items as $item){
227 $ret[] = $this->list[$item];
228 }
229 return($ret);
230 }
233 function save_object()
234 {
235 MultiSelectWindow::save_object();
236 }
239 function reload()
240 {
241 /* Set base for all searches && initialise some vars */
242 $this->list= array();
243 $base = $this->selectedBase;
244 $filter = "";
245 $Regex = $this->Regex;
246 $IP_start = $this->IP_start;
247 $IP_end = $this->IP_end;
249 if($this->IPMatch){
250 if(!tests::is_ip($IP_start)){
251 msg_dialog::display(_("Error"), msgPool::invalid(_("IP range")), ERROR_DIALOG);
252 return;
253 }
254 if(!tests::is_ip($IP_end)){
255 msg_dialog::display(_("Error"), msgPool::invalid(_("IP range")), ERROR_DIALOG);
256 return;
257 }
258 }
261 $chk = array(
262 "ogroups" => "(&(objectClass=gosaGroupOfNames)(|(gosaGroupObjects=*S*)(gosaGroupObjects=*W*)))" ,
263 "servers" => "(objectClass=goServer)" ,
264 "incoming" => "(objectClass=GOhard)" ,
265 "workstations" => "(objectClass=gotoWorkstation)");
267 /* Create filter */
268 foreach($chk as $chkBox => $FilterPart){
269 if($this->$chkBox){
270 $filter .= $FilterPart;
271 }
272 }
273 $filter= "(&(cn=".$Regex.")(|".$filter."))";
275 if($this->SubSearch){
276 $res= get_list($filter, array("ogroups","workstations","servers"), $base,
277 array("cn","objectClass","gosaGroupObjects","ipHostNumber","description"), GL_SIZELIMIT | GL_SUBSEARCH);
278 }else{
279 $res= get_list($filter, "ogroups", get_groups_ou().$base,
280 array("cn","objectClass","ipHostNumber","description"), GL_SIZELIMIT );
281 $res= array_merge($res,get_list($filter, "workstation", get_ou('workstationou').$base,
282 array("cn","objectClass","ipHostNumber","description"), GL_SIZELIMIT ));
283 $res= array_merge($res,get_list($filter, "server", get_ou('serverou').$base,
284 array("cn","objectClass","ipHostNumber","description"), GL_SIZELIMIT ));
286 $deps_a = array(
287 get_ou("workstationou"),
288 get_ou("incominou"),
289 get_ou("serverou"),
290 get_ou("ogroupou"));
292 $res = get_sub_list($filter,array("server","incoming","workstation","ogroup"),
293 $deps_a,get_ou("systemsou").$base,array("cn","objectClass","ipHostNumber","description"),GL_SIZELIMIT);
294 }
296 $this->list= $res;
297 ksort ($this->list);
298 reset ($this->list);
299 $tmp=array();
300 foreach($this->list as $tkey => $val ){
302 if($this->IPMatch){
303 if(isset($val['ipHostNumber'][0])){
304 if(tests::is_ip_range($IP_start,$val['ipHostNumber'][0]) && tests::is_ip_range($val['ipHostNumber'][0],$IP_end)){
305 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
306 }
307 }
308 }else{
309 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
310 }
311 }
312 ksort($tmp);
313 $this->list=array();
314 foreach($tmp as $val){
315 $this->list[]=$val;
316 }
317 reset ($this->list);
318 }
320 function list_get_selected_items()
321 {
322 $ids = array();
323 foreach($_POST as $name => $value){
324 if(preg_match("/^item_selected_[0-9]*$/",$name)){
325 $id = preg_replace("/^item_selected_/","",$name);
326 $ids[$id] = $id;
327 }
328 }
329 return($ids);
330 }
331 }
334 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
335 ?>