Code

Apply bugfix for #3285
[gosa.git] / trunk / gosa-plugins / fai / admin / systems / services / repository / class_servRepository.inc
1 <?php
3 class servrepository extends goService
4 {
5   /* attribute list for save action */
6   //  var $ignore_account         = TRUE;
7   var $attributes             = array("FAIrepository");
8   var $objectclasses          = array("FAIrepositoryServer");
9   
10   /* Repositories */
11   var $repositories          = array();
12   var $FAIrepository         = array();
13   var $conflicts             = array("servrepository");
14   var $DisplayName           = "";
15   var $StatusFlag            = "";
16   
17   var $view_logged            = FALSE;
18   var $fai_activated          = FALSE;
20   var $divlist = NULL;
22   function servrepository (&$config, $dn= NULL, $parent= NULL)
23   {
24     plugin::plugin ($config, $dn,$parent);
26     $this->DisplayName = _("Repository service");
28     /* Skip this if fai is deactivated */
29     $tmp= $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
30     if(!empty($tmp)){
31       $this->fai_activated = TRUE;    
32     }else{
33       return;
34     }
36     $this->repositories = array();
37     if(isset($this->attrs['FAIrepository'])){
38       for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
39         $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
40         $tmp2 = array();  
41         $tmp3 = array();   
43         if(isset($tmp[1])){
44           $tmp2['ParentServer'] = $tmp[1];
45           if(empty($tmp[1])){
46             $tmp2['ParentServer'] = "none";
47           }
48         }else{
49           $tmp2['ParentServer'] = "none";
50         }
52         if(isset($tmp[0])){
53           $tmp2['Url']          = $tmp[0];
54         }else{
55           $tmp2['Url']          = "";
56         }
57   
58         if(isset($tmp[2])){
59           $tmp2['Release']      = $tmp[2];
60         }else{
61           $tmp2['Release']      = "";
62         }
64         if(isset($tmp[3])){
65           $tmp3 = split(",",$tmp[3]);
66           foreach($tmp3 as $sec){
67             $tmp2['Sections'][$sec]=$sec;
68           }    
69         }else{
70           $tmp['Section']=array();
71         }
73         $this->repositories[$tmp[2]]=$tmp2;      
74       }
75     }
78     /* Create divlist */
79     $this->divlist = new divListRepository($this->config,$this);
80   }
82   function execute()
83   {
84     /* Call parent execute */
85     plugin::execute();
87     if($this->is_account && !$this->view_logged){
88       $this->view_logged = TRUE;
89       new log("view","server/".get_class($this),$this->dn);
90     }
92     if(!$this->fai_activated){
93       $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
94       return $str;
95     }
97     /* Fill templating stuff */
98     $smarty= get_smarty();
99     $smarty->assign("is_createable",$this->acl_is_createable());
100     $display= "";
102     /* Show tab dialog headers */
103     /*
104        ADD / EDIT Repository
105        Dialog Handling
106      */
107     $once = false;
108     if(isset($_POST['servRepository'])){
109       foreach($_POST as $name => $value){
111         if(preg_match("/AddRepository/",$name) && $this->acl_is_createable()){
112           $once = true;
113           $this->dialog = new servRepositorySetup($this->config,$this->dn);
114           $this->dialog->parent = $this;
115         }
117         if((preg_match("/^delete_/",$name)) && (!$once) && $this->acl_is_removeable()){
118           $once = true;
119           $value = preg_replace("/delete_/","",$name);
120           $value = base64_decode(preg_replace("/_.*$/","",$value));
122           $url = $this->repositories[$value]['Url'];
123           $release = $this->repositories[$value]['Release'];
125           $ldap = $this->config->get_ldap_link();
126           $ldap->cd ($this->config->current['BASE']);
128           $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
129           if ($ldap->count() != 0){
130             $obj= array();
131             $found= false;
132             while($attrs = $ldap->fetch()){
133               foreach($attrs['FAIclass'] as $class){
134                 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
135                   $obj[$ldap->getDN()]= $attrs['cn'][0];
136                   $found= true;
137                 }
138               }
139             }
141             if ($found){
142               msg_dialog::display(_("Error"), msgPool::stillInUse(_("FAI release"), msgPool::buildList($obj)), ERROR_DIALOG);
143             }else{
144               if(isset($this->repositories[$value])){
145                 unset($this->repositories[$value]);
146               }
147             }
148           } else {
149             if(isset($this->repositories[$value])) {
150               unset($this->repositories[$value]);
151             }
152           }
153         }
155         if((preg_match("/^edit_/",$name))&&(!$once)){
156           $value = preg_replace("/edit_/","",$name);
157           $value = base64_decode(preg_replace("/_.$/","",$value));
159           if(isset($this->repositories[$value])){
160             $once = true;
161             $obj = $this->repositories[$value];
163             /* to be able to detect if this was renamed */
164             $obj['initialy_was'] = $obj['Release'];
165             $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
166             $this->dialog->parent = $this;
167           }
168         }
169       }
170     }
171     if((isset($_GET['act']))&&($_GET['act']=="open_repository")&&(isset($_GET['id']))){
172       $obj = $this->repositories[base64_decode($_GET['id'])];
173       $obj['initialy_was'] = $obj['Release'];
174       $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
175       $this->dialog->parent = $this;
176     }
178     if(isset($_POST['repository_setup_save']) && is_object($this->dialog)){
179       $this->dialog->save_object();
180       if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
181         msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
182       }else
184       if(count($this->dialog->check())!=0){
185         foreach($this->dialog->check() as $msg){
186           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
187         }
188       }else{
189         $obj = $this->dialog->save();
190         if($this->dialog->is_new_name()){
191           $oldname = $this->dialog->initialy_was;
192           $this->repositories[$obj['Release']]=$obj;        
193           unset($this->repositories[$oldname]);
194         }else{ 
195           $this->repositories[$obj['Release']]=$obj;        
196         }
197         $this->dialog = FALSE;
198         $this->is_dialog= false;
199       }
200     }
202     if(isset($_POST['repository_setup_cancel'])){
203       $this->dialog=FALSE;
204       $this->is_dialog = false;
205     }
206    
207     if(is_object($this->dialog)){
208       $this->dialog->save_object();
209       $this->is_dialog = true;
210       return($this->dialog->execute());
211     }
213     /*
214       Repository setup dialog handling /END
215     */
218     $link   = "<a href='?plug=".$_GET['plug']."&amp;act=open_repository&amp;id=%s'>%s</a>";
219     $edit   = "<input type='image' value='%s' name='edit_%s'   src='images/lists/edit.png'>&nbsp;";
220    
221     /* Hide delete icon, if delete is not allowed */ 
222     if($this->acl_is_removeable()){
223       $delete = "<input type='image' value='%s' name='delete_%s' src='images/lists/trash.png'>";
224     }else{
225       $delete = "<img src='images/empty.png' alt='&nbsp;'>";
226     }
228     $this->divlist->execute(); 
229     $this->divlist->setEntries($this->repositories);
230     $smarty->assign("Repositories",$this->divlist->Draw());
231     $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE,dirname(__FILE__)));
232     return($display);
233   }
236   /* Save data to object */
237   function save_object()
238   {
239     plugin::save_object();
240     if(is_object($this->divlist)){
241       $this->divlist->save_object();
242     }
243   }
245   function allow_remove()
246   {
247     /* Check if the service is still in use */
248     $ldap = $this->config->get_ldap_link();
249     $ldap->cd($this->config->current['BASE']);
250     foreach($this->repositories as $repository) {
251       $url = $repository['Url'];
252       $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
253       if ($ldap->count() != 0){
254         $obj= array();
255         $found= false;
256         while($attrs = $ldap->fetch()){
257           foreach($attrs['FAIclass'] as $class){
258               $obj[$ldap->getDN()]= $attrs['cn'][0];
259               $found= true;
260           }
261         }
263         if ($found){
264           $message = msgPool::stillInUse(_("Repository service")) . msgPool::buildList($obj);
265           return $message;
266         }
267       }
268     }
269    }
272   /* Check supplied data */
273   function check()
274   {
275     /* Call common method to give check the hook */
276     $message= plugin::check();
277     return ($message);
278   }
281   /* Save to LDAP */
282   function save()
283   {
284     if(!$this->fai_activated) return;
286     plugin::save();
288     $arr = array();
289     foreach($this->repositories as $servername => $conf){
290       $str = "";
291       foreach($conf['Sections'] as $sec){
292         $str.=$sec.",";
293       }
294       $str=preg_replace("/,$/","",$str);
295         
296       if($conf['ParentServer']=="none"){
297         $conf['ParentServer'] ="";
298       }    
299     
300       $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
301     }
302     $this->attrs['FAIrepository'] = $arr;
304     $ldap= $this->config->get_ldap_link();
305     $ldap->cd ($this->config->current['BASE']);
306     
307     $ldap->cat($this->dn, array('dn'));
308     
309     if($ldap->count()){
310       $ldap->cd($this->dn);
311       $this->cleanup();
312       $ldap->modify ($this->attrs);       
313       $this->handle_post_events("modify");
314     }else{
315       $ldap->cd ($this->config->current['BASE']);
316       $ldap->create_missing_trees($this->dn);
317       $ldap->cd($this->dn);
318       $ldap->add($this->attrs);
319       $this->handle_post_events("add");
320     }
322     # If there were changes, just tell the server to reload information
323     if(count($this->attrs)){
324             $this->trigger_si_fai_server_reload();
325     }
327     if($this->initially_was_account){
328       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
329     }else{
330       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
331     }
332   }
335   function getListEntry()
336   {
337     $fields = goService::getListEntry();
338     $fields['Message']    = _("Repository service");
339     return($fields);
340   }
343   function trigger_si_fai_server_reload()
344   {
345     /* Reload GOsa si FAI DB/cache
346      */
347     if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
348       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);        
349       if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_server_db'])){
350         $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_server_db']; 
351         $tmp = new $evt['CLASS_NAME']($this->config);
352         $tmp->set_type(TRIGGERED_EVENT);
353         $tmp->add_targets(array("GOSA"));
354         $o_queue = new gosaSupportDaemon();
355         if(!$o_queue->append($tmp)){
356           msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
357         }
358       }
359     }
360   }
363   function remove_from_parent()
364   {
365     goService::remove_from_parent();
366     $this->trigger_si_fai_server_reload();
367   }
369   /* Return plugin informations for acl handling */
370   static function plInfo()
371   {
372     return (array(
373           "plShortName"   => _("Repository"),
374           "plDescription" => _("Repository service")." ("._("Services").")",
375           "plSelfModify"  => FALSE,
376           "plDepends"     => array(),
377           "plPriority"    => 84,
378           "plSection"     => array("administration"),
379           "plCategory"    => array("server"),
381           "plProvidedAcls"=> array(
382 #            "start"         => _("Service start"),  // Remove this to hide the start button at all.
383 #            "stop"          => _("Service stop"),   // Remove this to hide the stop button at all.
384 #            "restart"       => _("Service restart"),// Remove this to hide the restart button at all.
386 #            "cn"            => _("Name"),
387             "ParentServer"  => _("Parent server"),
388             "Release"       => _("Releases"),
389             "Url"           => _("URL"),
390             "Section"       => _("Sections"))
391           ));
392   }
395 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
396 ?>