Code

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