Code

44b59d757139f99435f0c3ada98f230408df3a42
[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();
26   var $conflicts             = array("FAIrepositoryServer");
27   var $DisplayName           = "";
28   var $StatusFlag            = "";
31   function servrepository ($config, $dn= NULL)
32   {
33     plugin::plugin ($config, $dn);
35     $this->DisplayName = _("Repository service");
37     $this->repositories = array();
38     if(isset($this->attrs['FAIrepository'])){
39       for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
40         $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
41         $tmp2 = array();  
42         $tmp3 = array();   
44         if(isset($tmp[1])){
45           $tmp2['ParentServer'] = $tmp[1];
46           if(empty($tmp[1])){
47             $tmp2['ParentServer'] = "none";
48           }
49         }else{
50           $tmp2['ParentServer'] = "none";
51         }
53         if(isset($tmp[0])){
54           $tmp2['Url']          = $tmp[0];
55         }else{
56           $tmp2['Url']          = "";
57         }
58   
59         if(isset($tmp[2])){
60           $tmp2['Release']      = $tmp[2];
61         }else{
62           $tmp2['Release']      = "";
63         }
65         if(isset($tmp[3])){
66           $tmp3 = split(",",$tmp[3]);
67           foreach($tmp3 as $sec){
68             $tmp2['Sections'][$sec]=$sec;
69           }    
70         }else{
71           $tmp['Section']=array();
72         }
74         $this->repositories[$tmp[2]]=$tmp2;      
75       }
76     }
77   }
79   function execute()
80   {
81     /* Call parent execute */
82     plugin::execute();
84     /* Fill templating stuff */
85     $smarty= get_smarty();
86     $display= "";
88     /* Smarty vars*/
89     $smarty->assign("infoimage",    get_template_path('images/info.png'));
90     $smarty->assign("search_image", get_template_path('images/search.png'));
91     $smarty->assign("launchimage",  get_template_path('images/small_filter.png'));
92     $smarty->assign("tree_image",   get_template_path('images/tree.png'));
93     $smarty->assign("alphabet",     generate_alphabet());
94     $smarty->assign("apply",        apply_filter());
95     $smarty->assign("regex",        $this->regex);
98     /* Do we need to flip is_account state? */
99     if (isset($_POST['modify_state'])){
100       $this->is_account= !$this->is_account;
101     }
103     /* Show tab dialog headers */
104     if ($this->is_account){
105       $display= $this->show_header(_("Remove FAI repository extension."),
106           _("This server has FAI repository features enabled. You can disable them by clicking below."));
107     } else {
108       $display= $this->show_header(_("Add FAI repository extension."),
109           _("This server has FAI repository features disabled. You can enable them by clicking below."));
110       return ($display);
111     }
113     /*
114        ADD / EDIT Repository
115        Dialog Handling
116      */
118     $once = false;
119     if(isset($_POST['servRepository'])){
120       foreach($_POST as $name => $value){
122         if(preg_match("/AddRepository/",$name)){
123           $once = true;
124           $this->dialog = new servRepositorySetup($this->config,$this->dn);
125           $this->dialog->acl = $this->acl;
126         }
128         if((preg_match("/^delete_/",$name))&&(!$once)){
129           $once = true;
130           $value = preg_replace("/delete_/","",$name);
131           $value = base64_decode(preg_replace("/_.*$/","",$value));
133           $url = $this->repositories[$value]['Url'];
134           $release = $this->repositories[$value]['Release'];
136           $ldap = $this->config->get_ldap_link();
137           $ldap->cd ($this->config->current['BASE']);
139           $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
141           $found = false;
142           $found_in = " ";
143           while($attrs = $ldap->fetch()){
144             foreach($attrs['FAIclass'] as $class){
145               if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
146                 $found = true;  
147                 $found_in .= $attrs['cn'][0]." ";
148               }
149             }
150           }
152           if($found){
153             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));
154           }else{
155             if(isset($this->repositories[$value])){
156               unset($this->repositories[$value]);
157             }
158           }
159         }
161         if((preg_match("/^edit_/",$name))&&(!$once)){
162           $value = preg_replace("/edit_/","",$name);
163           $value = base64_decode(preg_replace("/_.$/","",$value));
165           if(isset($this->repositories[$value])){
167             $ldap = $this->config->get_ldap_link();
168             $ldap->cd ($this->config->current['BASE']);
170             $url = $this->repositories[$value]['Url'];
171             $release = $this->repositories[$value]['Release'];
173             $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
175             $found = false;
176             $found_in = " ";
177             while($attrs = $ldap->fetch()){
178               foreach($attrs['FAIclass'] as $class){
179                 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
180                   $found = true;
181                   $found_in .= $attrs['cn'][0]." ";
182                 }
183               }
184             }
186             if($found){
187               print_red(sprintf(_("Be careful editing this release, it is still used by these workstations [%s]."),$found_in));
188             } 
190             if(isset($this->repositories[$value])){
191               $once = true;
192               $obj = $this->repositories[$value];
194               /* to be able to detect if this was renamed */
195               $obj['initialy_was'] = $obj['Release'];
196               $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
197               $this->dialog->acl = $this->acl;
198             }
199           }
200         }
201       }
202     }
203     if((isset($_GET['act']))&&($_GET['act']=="open")&&(isset($_GET['id']))){
204       $obj = $this->repositories[base64_decode($_GET['id'])];
205       $obj['initialy_was'] = $obj['Release'];
206       $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
207       $this->dialog->acl = $this->acl;
208     }
210     /*
211     if(isset($_POST['AddRepository'])){
212       $this->dialog = new servRepositorySetup($this->config,$this->dn);
213       $this->dialog->acl = $this->acl;
214     }
215     */
217     if(isset($_POST['repository_setup_save'])){
218       $this->dialog->save_object();
219       if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
220         print_red(_("This name is already in use."));
221       }else
223       if(count($this->dialog->check())!=0){
224         foreach($this->dialog->check() as $msg){
225           print_red($msg);
226         }
227       }else{
228         $obj = $this->dialog->save();
229         if($this->dialog->is_new_name()){
230           $oldname = $this->dialog->initialy_was;
231           $this->repositories[$obj['Release']]=$obj;        
232           unset($this->repositories[$oldname]);
233         }else{ 
234           $this->repositories[$obj['Release']]=$obj;        
235         }
236         $this->dialog = NULL;
237         $this->is_dialog= false;
238       }
239     }
241     if(isset($_POST['repository_setup_cancel'])){
242       $this->dialog=NULL;
243       $this->is_dialog = false;
244     }
245    
246     if($this->dialog != NULL){
247       $this->dialog->save_object();
248       $this->is_dialog = true;
249       return($this->dialog->execute());
250     }
252     /*
253       Repository setup dialog handling /END
254     */
256     $divlist = new divList("repositories");
257     $divlist->SetEntriesPerPage(0);
258     $divlist->setHeight(400);
260     $divlist->setHeader(array(array("string"=>_("Release"),"attach"=>"style='width:80px;'"),
261                               array("string"=>_("Sections")),
262                               array("string"=>_("Options"),"attach"=>"style='border-right:0px;width:55px;'")
263                        )     );
265     $link   = "<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
266     $edit   = "<input type='image' value='%s' name='edit_%s'   src='images/edit.png'>&nbsp;";
267     $delete = "<input type='image' value='%s' name='delete_%s' src='images/edittrash.png'>";
268   
269     foreach($this->repositories as $name => $reps){
271       $str = " ";
273       if(preg_match("/".str_replace("*",".*",$this->regex)."/",$reps['Release'])){
274     
276       foreach($reps['Sections'] as $sec){
277         $str.=$sec." ";  
278       }    
279   
280       $divlist->AddEntry(array(
281                               array("string"=>sprintf($link,base64_encode($name),$name),"attach"=>"style='width:80px;'"),
282                               array("string"=>sprintf($link,base64_encode($name),_("Sections")." :".$str)),
283                               array("string"=>preg_replace("/%s/",base64_encode($name),$edit.$delete),"attach"=>"style='border-right:0px;width:55px;text-align:right;'")
284                               ));
285       }
286     }
287  
288     $smarty -> assign("Repositories",$divlist->DrawList());
290     $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE));
291     return($display);
292   }
294   function remove_from_parent()
295   {
296     plugin::remove_from_parent();    
297     $ldap= $this->config->get_ldap_link();
298     $ldap->cd ($this->config->current['BASE']);
299     
300     $ldap->cat($this->dn, array('dn'));
301     
302     if($ldap->count()){
303       $ldap->cd($this->dn);
304       $this->cleanup();
305       $ldap->modify ($this->attrs); 
307       $this->handle_post_events("modify");
308     }
309   }
312   /* Save data to object */
313   function save_object()
314   {
315     plugin::save_object();
316     if(isset($_POST['regex'])){
317       $this->regex = $_POST['regex'];
318     }
319   }
322   /* Check supplied data */
323   function check()
324   {
325     /* Call common method to give check the hook */
326     $message= plugin::check();
327         
328     return ($message);
329   }
332   /* Save to LDAP */
333   function save()
334   {
335     plugin::save();
337     $arr = array();
338     foreach($this->repositories as $servername => $conf){
339       $str = "";
340       foreach($conf['Sections'] as $sec){
341         $str.=$sec.",";
342       }
343       $str=preg_replace("/,$/","",$str);
344         
345       if($conf['ParentServer']=="none"){
346         $conf['ParentServer'] ="";
347       }    
348     
349       $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
350     }
351     $this->attrs['FAIrepository'] = $arr;
353     $ldap= $this->config->get_ldap_link();
354     $ldap->cd ($this->config->current['BASE']);
355     
356     $ldap->cat($this->dn, array('dn'));
357     
358     if($ldap->count()){
359       $ldap->cd($this->dn);
360       $this->cleanup();
361       $ldap->modify ($this->attrs);       
363       $this->handle_post_events("modify");
364     }else{
365       $ldap->cd ($this->config->current['BASE']);
366       $ldap->create_missing_trees($this->dn);
367       $ldap->cd($this->dn);
368       $ldap->add($this->attrs);
369       $this->handle_post_events("add");
370     }
371   }
373   /* Get updates for status flag */
374   function updateStatusState()
375   {
376     if(empty($this->StatusFlag)) return;
378     $attrs = array();
379     $flag = $this->StatusFlag;
380     $ldap = $this->config->get_ldap_link();
381     $ldap->cd($this->cn);
382     $ldap->cat($this->dn,array($flag));
383     if($ldap->count()){
384       $attrs = $ldap->fetch();
385     }
386     if(isset($attrs[$flag][0])){
387       $this->$flag = $attrs[$flag][0];
388     }
389   }
392   function getListEntry()
393   {
394     $this->updateStatusState();
395     $flag = $this->StatusFlag;
396     if(empty($flag)){
397       $fields['Status']      = "";
398     }else{
399       $fields['Status']      = $this->$flag;
400     }
401     $fields['Message']    = _("Repository service");
402     $fields['AllowStart'] = true;
403     $fields['AllowStop']  = true;
404     $fields['AllowRestart'] = true;
405     $fields['AllowRemove']= true;
406     $fields['AllowEdit']  = true;
407     return($fields);
408   }
411   /* Directly save new status flag */
412   function setStatus($value)
413   {
414     if($value == "none") return;
415     if(!$this->initially_was_account) return;
416     $ldap = $this->config->get_ldap_link();
417     $ldap->cd($this->dn);
418     $ldap->cat($this->dn,array("objectClass"));
419     if($ldap->count()){
421       $tmp = $ldap->fetch();
422       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
423         $attrs['objectClass'][] = $tmp['objectClass'][$i];
424       }
425       $flag = $this->StatusFlag;
426       $attrs[$flag] = $value;
427       $this->$flag = $value;
428       $ldap->modify($attrs);
429       show_ldap_error($ldap->get_error());
430       $this->action_hook();
431     }
432   }
436 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
437 ?>