Code

Added option to divlist.
[gosa.git] / plugins / admin / systems / class_servRepository.inc
1 <?php
3 class servrepository extends plugin
4 {
5   /* attribute list for save action */
6   //  var $ignore_account         = TRUE;
7   var $attributes             = array("FAIrepository");
8   var $objectclasses          = array("FAIrepositoryServer");
9   
10   /* Search filter */
11   var $regex                  = "*";
13   /* Configurationdialog for repositories */
14   var $dialog                 = NULL;
16   /* Repositories */
17   var $repositories           = array();
18   var $FAIrepository          = array();
20   var $fai_activated          = FALSE;
22   function servrepository ($config, $dn= NULL, $parent= NULL)
23   {
24     plugin::plugin ($config, $dn, $parent);
26     /* Skip this if fai is deactivated */
27     $tmp = search_config($this->config->data,"faiManagement","CLASS");
28     if(!empty($tmp)){
29       $this->fai_activated = TRUE;    
30     }else{
31       return;
32     }
34     $ui = get_userinfo();
35     $tmp= get_permissions ($this->dn, $ui->subtreeACL);
36     $this->acl= get_module_permission($tmp, "servrepository", $this->dn);
37     
38     $this->repositories = array();
39     if(isset($this->attrs['FAIrepository'])){
40       for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
41         $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
42         $tmp2 = array();  
43         $tmp3 = array();   
45         if(isset($tmp[1])){
46           $tmp2['ParentServer'] = $tmp[1];
47           if(empty($tmp[1])){
48             $tmp2['ParentServer'] = "none";
49           }
50         }else{
51           $tmp2['ParentServer'] = "none";
52         }
54         if(isset($tmp[0])){
55           $tmp2['Url']          = $tmp[0];
56         }else{
57           $tmp2['Url']          = "";
58         }
59   
60         if(isset($tmp[2])){
61           $tmp2['Release']      = $tmp[2];
62         }else{
63           $tmp2['Release']      = "";
64         }
66         if(isset($tmp[3])){
67           $tmp3 = split(",",$tmp[3]);
68           foreach($tmp3 as $sec){
69             $tmp2['Sections'][$sec]=$sec;
70           }    
71         }else{
72           $tmp['Section']=array();
73         }
75         $this->repositories[$tmp[2]]=$tmp2;      
76       }
77     }
78   }
80   function execute()
81   {
82     /* Call parent execute */
83     plugin::execute();
85     if(!$this->fai_activated){
86       $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
87       return $str;
88     }
90     /* Fill templating stuff */
91     $smarty= get_smarty();
92     $display= "";
94     $ui = get_userinfo();
95     $tmp= get_permissions ($this->dn, $ui->subtreeACL);
96     $this->acl= get_module_permission($tmp, "servrepository", $this->dn);
97     $allow_edit = !preg_match("/disabled/i",chkacl($this->acl,"FAIclass"));
98     
100     /* Smarty vars*/
101     $smarty->assign("infoimage",    get_template_path('images/info.png'));
102     $smarty->assign("search_image", get_template_path('images/search.png'));
103     $smarty->assign("launchimage",  get_template_path('images/small_filter.png'));
104     $smarty->assign("tree_image",   get_template_path('images/tree.png'));
105     $smarty->assign("alphabet",     generate_alphabet());
106     $smarty->assign("apply",        apply_filter());
107     $smarty->assign("regex",        $this->regex);
110     /* Do we need to flip is_account state? */
111     if (isset($_POST['modify_state'])){
113       /* Only change account state if allowed */
114       if($this->is_account && $this->acl == "#all#"){
115         $this->is_account= !$this->is_account;
116         $this->is_modified = true;
117       }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
118         $this->is_account= !$this->is_account;
119         $this->is_modified = true;
120       }
121     }
123     /* Show tab dialog headers */
124     if ($this->is_account){
125       $display= $this->show_header(_("Remove FAI repository extension."),
126           _("This server has FAI repository features enabled. You can disable them by clicking below."));
127     } else {
128       $display= $this->show_header(_("Add FAI repository extension."),
129           _("This server has FAI repository features disabled. You can enable them by clicking below."));
130       return ($display);
131     }
133     /*
134        ADD / EDIT Repository
135        Dialog Handling
136      */
138     $once = false;
139     if(isset($_POST['servRepository'])){
140       foreach($_POST as $name => $value){
142         if(preg_match("/AddRepository/",$name) && !$once && $allow_edit){
143           $once = true;
144           $this->dialog = new servRepositorySetup($this->config,$this->dn);
145           $this->dialog->acl = $this->acl;
146         }
148         if((preg_match("/^delete_/",$name))&&(!$once) && $allow_edit){
149           $once = true;
150           $value = preg_replace("/delete_/","",$name);
151           $value = base64_decode(preg_replace("/_.*$/","",$value));
153           $url = $this->repositories[$value]['Url'];
154           $release = $this->repositories[$value]['Release'];
156           $ldap = $this->config->get_ldap_link();
157           $ldap->cd ($this->config->current['BASE']);
159           $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
161           $found = false;
162           $found_in = " ";
163           while($attrs = $ldap->fetch()){
164             foreach($attrs['FAIclass'] as $class){
165               if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
166                 $found = true;  
167                 $found_in .= $attrs['cn'][0]." ";
168               }
169             }
170           }
172           if($found){
173             print_red(sprintf(_("You can't delete this release, it is still used by these workstations [%s]. Please solve this dependencies first, to keep data base consistency."),$found_in));
174           }else{
175             if(isset($this->repositories[$value])){
176               unset($this->repositories[$value]);
177             }
178           }
179         }
181         if((preg_match("/^edit_/",$name))&&(!$once) && $allow_edit){
182           $value = preg_replace("/edit_/","",$name);
183           $value = base64_decode(preg_replace("/_.$/","",$value));
185           if(isset($this->repositories[$value])){
187             $ldap = $this->config->get_ldap_link();
188             $ldap->cd ($this->config->current['BASE']);
190             $url = $this->repositories[$value]['Url'];
191             $release = $this->repositories[$value]['Release'];
193             $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
195             $found = false;
196             $found_in = " ";
197             while($attrs = $ldap->fetch()){
198               foreach($attrs['FAIclass'] as $class){
199                 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
200                   $found = true;
201                   $found_in .= $attrs['cn'][0]." ";
202                 }
203               }
204             }
206             if($found){
207               print_red(sprintf(_("Be careful editing this release, it is still used by these workstations [%s]."),$found_in));
208             } 
210             if(isset($this->repositories[$value])){
211               $once = true;
212               $obj = $this->repositories[$value];
214               /* to be able to detect if this was renamed */
215               $obj['initialy_was'] = $obj['Release'];
216               $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
217               $this->dialog->acl = $this->acl;
218             }
219           }
220         }
221       }
222     }
223     if((isset($_GET['act']))&&($_GET['act']=="open")&&(isset($_GET['id'])) && $allow_edit){
224       $obj = $this->repositories[base64_decode($_GET['id'])];
225       $obj['initialy_was'] = $obj['Release'];
226       $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
227       $this->dialog->acl = $this->acl;
228     }
230     /*
231     if(isset($_POST['AddRepository'])){
232       $this->dialog = new servRepositorySetup($this->config,$this->dn);
233       $this->dialog->acl = $this->acl;
234     }
235     */
237     if(isset($_POST['repository_setup_save'])){
238       $this->dialog->save_object();
239       if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
240         print_red(_("This name is already in use."));
241       }else
243       if(count($this->dialog->check())!=0){
244         foreach($this->dialog->check() as $msg){
245           print_red($msg);
246         }
247       }else{
248         $obj = $this->dialog->save();
249         if($this->dialog->is_new_name()){
250           $oldname = $this->dialog->initialy_was;
251           $this->repositories[$obj['Release']]=$obj;        
252           unset($this->repositories[$oldname]);
253         }else{ 
254           $this->repositories[$obj['Release']]=$obj;        
255         }
256         $this->dialog = NULL;
257         $this->is_dialog= false;
258       }
259     }
261     if(isset($_POST['repository_setup_cancel'])){
262       $this->dialog=NULL;
263       $this->is_dialog = false;
264     }
265    
266     if($this->dialog != NULL){
267       $this->dialog->save_object();
268       $this->is_dialog = true;
269       return($this->dialog->execute());
270     }
272     /*
273       Repository setup dialog handling /END
274     */
276     $divlist = new divList("repositories");
277     $divlist->SetEntriesPerPage(0);
278     $divlist->setHeight(400);
279     $divlist->SetPluginMode();
281     $divlist->setHeader(array(array("string"=>_("Release"),"attach"=>"style='width:80px;'"),
282                               array("string"=>_("Sections")),
283                               array("string"=>_("Options"),"attach"=>"style='border-right:0px;width:55px;'")
284                        )     );
286     $link   = "<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
287     $edit   = "<input type='image' value='%s' name='edit_%s'   src='images/edit.png'>&nbsp;";
288     $delete = "<input type='image' value='%s' name='delete_%s' src='images/edittrash.png'>";
289   
290     foreach($this->repositories as $name => $reps){
292       $str = " ";
293       if(preg_match("/".str_replace("*",".*",$this->regex)."/",$reps['Release'])){
295       foreach($reps['Sections'] as $sec){
296         $str.=$sec." ";  
297       }    
299       if($allow_edit){
300         $link_str = sprintf($link,base64_encode($name),$name);
301         $sections = sprintf($link,base64_encode($name),_("Sections")." :".$str);
302         $options  = preg_replace("/%s/",base64_encode($name),$edit.$delete);
303       }else{
304         $link_str = $name;
305         $sections = _("Sections")." :".$str;
306         $options  = "";
307       }
308     
309  
310       $divlist->AddEntry(array(
311                               array("string"=>$link_str,"attach"=>"style='width:80px;'"),
312                               array("string"=>$sections),
313                               array("string"=>$options,"attach"=>"style='border-right:0px;width:55px;text-align:right;'")));
314       }
315     }
316  
317     $smarty -> assign("Repositories",$divlist->DrawList());
318     $smarty -> assign("FAIclassACL",chkacl($this->acl,"FAIclass"));
320     $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE));
321     return($display);
322   }
324   function remove_from_parent()
325   {
327     if(!$this->fai_activated) return;
328     
329     /* Skip if not allowed */ 
330     $ui = get_userinfo();
331     $tmp= get_permissions ($this->dn, $ui->subtreeACL);
332     $this->acl= get_module_permission($tmp, "servrepository", $this->dn);
334     if(preg_match("/disabled/",chkacl($this->acl,"FAIclass"))) return;
336     plugin::remove_from_parent();    
337     $ldap= $this->config->get_ldap_link();
338     $ldap->cd ($this->config->current['BASE']);
339     
340     $ldap->cat($this->dn, array('dn'));
341     
342     if($ldap->count()){
343       $ldap->cd($this->dn);
344       $this->cleanup();
345       $ldap->modify ($this->attrs); 
347       $this->handle_post_events("modify");
348     }
349   }
352   /* Save data to object */
353   function save_object()
354   {
355     plugin::save_object();
356     if(isset($_POST['regex'])){
357       $this->regex = $_POST['regex'];
358     }
359   }
362   /* Check supplied data */
363   function check()
364   {
365     /* Call common method to give check the hook */
366     $message= plugin::check();
367         
368     return ($message);
369   }
372   /* Save to LDAP */
373   function save()
374   {
375     if(!$this->fai_activated) return;
377     /* Skip if not allowed */ 
378     $ui = get_userinfo();
379     $tmp= get_permissions ($this->dn, $ui->subtreeACL);
380     $this->acl= get_module_permission($tmp, "servrepository", $this->dn);
382     if(preg_match("/disabled/",chkacl($this->acl,"FAIclass"))) return;
383     plugin::save();
385     $arr = array();
386     foreach($this->repositories as $servername => $conf){
387       $str = "";
388       foreach($conf['Sections'] as $sec){
389         $str.=$sec.",";
390       }
391       $str=preg_replace("/,$/","",$str);
392         
393       if($conf['ParentServer']=="none"){
394         $conf['ParentServer'] ="";
395       }    
396     
397       $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
398     }
399     $this->attrs['FAIrepository'] = $arr;
401     $ldap= $this->config->get_ldap_link();
402     $ldap->cd ($this->config->current['BASE']);
403     
404     $ldap->cat($this->dn, array('dn'));
405     
406     if($ldap->count()){
407       $ldap->cd($this->dn);
408       $this->cleanup();
409       $ldap->modify ($this->attrs);       
411       $this->handle_post_events("modify");
412     }else{
413       $ldap->cd ($this->config->current['BASE']);
414       $ldap->create_missing_trees($this->dn);
415       $ldap->cd($this->dn);
416       $ldap->add($this->attrs);
417       $this->handle_post_events("add");
418     }
419   }
423 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
424 ?>