Code

7dd8e9cbd119b914680b548ef142bfae7c4ea40e
[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   function servrepository ($config, $dn= NULL)
26   {
27     plugin::plugin ($config, $dn);
29     $this->repositories = array();
30     if(isset($this->attrs['FAIrepository'])){
31       for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
32         $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
33         $tmp2 = array();  
34         $tmp3 = array();   
36         if(isset($tmp[1])){
37           $tmp2['ParentServer'] = $tmp[1];
38           if(empty($tmp[1])){
39             $tmp2['ParentServer'] = "none";
40           }
41         }else{
42           $tmp2['ParentServer'] = "none";
43         }
45         if(isset($tmp[0])){
46           $tmp2['Url']          = $tmp[0];
47         }else{
48           $tmp2['Url']          = "";
49         }
50   
51         if(isset($tmp[2])){
52           $tmp2['Release']      = $tmp[2];
53         }else{
54           $tmp2['Release']      = "";
55         }
57         if(isset($tmp[3])){
58           $tmp3 = split(",",$tmp[3]);
59           foreach($tmp3 as $sec){
60             $tmp2['Sections'][$sec]=$sec;
61           }    
62         }else{
63           $tmp['Section']=array();
64         }
66         $this->repositories[$tmp[2]]=$tmp2;      
67       }
68     }
69   }
71   function execute()
72   {
73     /* Call parent execute */
74     plugin::execute();
76     /* Fill templating stuff */
77     $smarty= get_smarty();
78     $display= "";
80     /* Smarty vars*/
81     $smarty->assign("infoimage",    get_template_path('images/info.png'));
82     $smarty->assign("search_image", get_template_path('images/search.png'));
83     $smarty->assign("launchimage",  get_template_path('images/small_filter.png'));
84     $smarty->assign("tree_image",   get_template_path('images/tree.png'));
85     $smarty->assign("alphabet",     generate_alphabet());
86     $smarty->assign("apply",        apply_filter());
87     $smarty->assign("regex",        $this->regex);
90     /* Do we need to flip is_account state? */
91     if (isset($_POST['modify_state'])){
92       $this->is_account= !$this->is_account;
93     }
95     /* Show tab dialog headers */
96     if ($this->is_account){
97       $display= $this->show_header(_("Remove FAI repository extension."),
98           _("This server has FAI repository features enabled. You can disable them by clicking below."));
99     } else {
100       $display= $this->show_header(_("Add FAI repository extension."),
101           _("This server has FAI repository features disabled. You can enable them by clicking below."));
102       return ($display);
103     }
104  
105     /*
106       ADD / EDIT Repository
107       Dialog Handling
108     */
109     
110     $once = false;
111     foreach($_POST as $name => $value){
113       if(preg_match("/AddRepository/",$name)){
114         $once = true;
115         $this->dialog = new servRepositorySetup($this->config,$this->dn);
116         $this->dialog->acl = $this->acl;
117       }
119       if((preg_match("/^delete_/",$name))&&(!$once)){
120         $once = true;
121         $value = preg_replace("/delete_/","",$name);
122         $value = base64_decode(preg_replace("/_.*$/","",$value));
124         $url = $this->repositories[$value]['Url'];
125         $release = $this->repositories[$value]['Release'];
127         $ldap = $this->config->get_ldap_link();
128         $ldap->cd ($this->config->current['BASE']);
129         
130         $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
131         
132         $found = false;
133         $found_in = " ";
134         while($attrs = $ldap->fetch()){
135           foreach($attrs['FAIclass'] as $class){
136             if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
137               $found = true;  
138               $found_in .= $attrs['cn'][0]." ";
139             }
140           }
141         }
142          
143         if($found){
144           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));
145         }else{
146           if(isset($this->repositories[$value])){
147             unset($this->repositories[$value]);
148           }
149         }
150       }
152       if((preg_match("/^edit_/",$name))&&(!$once)){
153         $value = preg_replace("/edit_/","",$name);
154         $value = base64_decode(preg_replace("/_.$/","",$value));
156         if(isset($this->repositories[$value])){
158           $ldap = $this->config->get_ldap_link();
159           $ldap->cd ($this->config->current['BASE']);
161           $url = $this->repositories[$value]['Url'];
162           $release = $this->repositories[$value]['Release'];
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(_("Be careful editing this release, it is still used by these workstations [%s]."),$found_in));
179           } 
181           if(isset($this->repositories[$value])){
182             $once = true;
183             $obj = $this->repositories[$value];
185             /* to be able to detect if this was renamed */
186             $obj['initialy_was'] = $obj['Release'];
187             $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
188             $this->dialog->acl = $this->acl;
189           }
190         }
191       }
192     }
194     if((isset($_GET['act']))&&($_GET['act']=="open")&&(isset($_GET['id']))){
195       $obj = $this->repositories[base64_decode($_GET['id'])];
196       $obj['initialy_was'] = $obj['Release'];
197       $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
198       $this->dialog->acl = $this->acl;
199     }
201     /*
202     if(isset($_POST['AddRepository'])){
203       $this->dialog = new servRepositorySetup($this->config,$this->dn);
204       $this->dialog->acl = $this->acl;
205     }
206     */
208     if(isset($_POST['repository_setup_save'])){
209       $this->dialog->save_object();
210       if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
211         print_red(_("This name is already in use."));
212       }else
214       if(count($this->dialog->check())!=0){
215         foreach($this->dialog->check() as $msg){
216           print_red($msg);
217         }
218       }else{
219         $obj = $this->dialog->save();
220         if($this->dialog->is_new_name()){
221           $oldname = $this->dialog->initialy_was;
222           $this->repositories[$obj['Release']]=$obj;        
223           unset($this->repositories[$oldname]);
224         }else{ 
225           $this->repositories[$obj['Release']]=$obj;        
226         }
227         $this->dialog = NULL;
228         $this->is_dialog= false;
229       }
230     }
232     if(isset($_POST['repository_setup_cancel'])){
233       $this->dialog=NULL;
234       $this->is_dialog = false;
235     }
236    
237     if($this->dialog != NULL){
238       $this->dialog->save_object();
239       $this->is_dialog = true;
240       return($this->dialog->execute());
241     }
243     /*
244       Repository setup dialog handling /END
245     */
247     $divlist = new divList("repositories");
248     $divlist->SetEntriesPerPage(0);
249     $divlist->setHeight(400);
251     $divlist->setHeader(array(array("string"=>_("Release"),"attach"=>"style='width:80px;'"),
252                               array("string"=>_("Sections")),
253                               array("string"=>_("Options"),"attach"=>"style='border-right:0px;width:55px;'")
254                        )     );
256     $link   = "<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
257     $edit   = "<input type='image' value='%s' name='edit_%s'   src='images/edit.png'>&nbsp;";
258     $delete = "<input type='image' value='%s' name='delete_%s' src='images/edittrash.png'>";
259   
260     foreach($this->repositories as $name => $reps){
262       $str = " ";
264       if(preg_match("/".str_replace("*",".*",$this->regex)."/",$reps['Release'])){
265     
267       foreach($reps['Sections'] as $sec){
268         $str.=$sec." ";  
269       }    
270   
271       $divlist->AddEntry(array(
272                               array("string"=>sprintf($link,base64_encode($name),$name),"attach"=>"style='width:80px;'"),
273                               array("string"=>sprintf($link,base64_encode($name),_("Sections")." :".$str)),
274                               array("string"=>preg_replace("/%s/",base64_encode($name),$edit.$delete),"attach"=>"style='border-right:0px;width:55px;text-align:right;'")
275                               ));
276       }
277     }
278  
279     $smarty -> assign("Repositories",$divlist->DrawList());
281     $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE));
282     return($display);
283   }
285   function remove_from_parent()
286   {
287     plugin::remove_from_parent();    
288     $ldap= $this->config->get_ldap_link();
289     $ldap->cd ($this->config->current['BASE']);
290     
291     $ldap->cat($this->dn, array('dn'));
292     
293     if($ldap->count()){
294       $ldap->cd($this->dn);
295       $this->cleanup();
296       $ldap->modify ($this->attrs); 
298       $this->handle_post_events("modify");
299     }
300   }
303   /* Save data to object */
304   function save_object()
305   {
306     plugin::save_object();
307     if(isset($_POST['regex'])){
308       $this->regex = $_POST['regex'];
309     }
310   }
313   /* Check supplied data */
314   function check()
315   {
316     /* Call common method to give check the hook */
317     $message= plugin::check();
318         
319     return ($message);
320   }
323   /* Save to LDAP */
324   function save()
325   {
326     plugin::save();
328     $arr = array();
329     foreach($this->repositories as $servername => $conf){
330       $str = "";
331       foreach($conf['Sections'] as $sec){
332         $str.=$sec.",";
333       }
334       $str=preg_replace("/,$/","",$str);
335         
336       if($conf['ParentServer']=="none"){
337         $conf['ParentServer'] ="";
338       }    
339     
340       $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
341     }
342     $this->attrs['FAIrepository'] = $arr;
344     $ldap= $this->config->get_ldap_link();
345     $ldap->cd ($this->config->current['BASE']);
346     
347     $ldap->cat($this->dn, array('dn'));
348     
349     if($ldap->count()){
350       $ldap->cd($this->dn);
351       $this->cleanup();
352       $ldap->modify ($this->attrs);       
354       $this->handle_post_events("modify");
355     }else{
356       $ldap->cd ($this->config->current['BASE']);
357       $ldap->create_missing_trees($this->dn);
358       $ldap->cd($this->dn);
359       $ldap->add($this->attrs);
360       $this->handle_post_events("add");
361     }
362   }
366 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
367 ?>