Code

Replaced echo with trigger_error
[gosa.git] / gosa-plugins / systems / admin / systems / class_serverService.inc
1 <?php
3 define("START_SERVICE",1);
4 define("STOP_SERVICE",2);
5 define("RESTART_SERVICE",3);
7 define("SERVICE_STOPPED",_("Stop"));
8 define("SERVICE_STARTED",_("Start"));
9 define("SERVICE_RESTARTED",_("Restart"));
11 define("ALL_SERVICES",100);
13 class ServerService extends plugin
14 {
15   /* CLI vars */
16   var $cli_summary      = "Manage server services";
17   var $cli_description  = "Managing services by adding,removing and configuring services. Allows to start/stop used services.";
18   var $cli_parameters   = array("config"=>"Config object" , "dn"=>"Object dn");
20   /* attribute list for save action */
21   var $ignore_account   = TRUE;
22   var $attributes       = array();
23   var $objectclasses    = array();
25   var $divList          = NULL;
27   var $plugins          = array();
28   var $pluign_names     = array();
30   var $current          = "";
31   var $backup           = NULL;
32   var $acl              ;
33   var $cn;
35   function ServerService (&$config, $dn)
36   {
37     plugin::plugin($config);
38     $this->dn= $dn;
39     foreach ($config->data['TABS']['SERVERSERVICE'] as $plug){
41       if(class_available($plug['CLASS'])){
43         $name= $plug['CLASS'];
44         $this->plugin_names[]= $name;
45         $this->plugins[$name]= new $name($config, $dn);
47         /* Capture all service objectClases, necessary for acl handling */ 
48         if(isset($this->plugins[$name]->objectclasses)){
49           foreach($this->plugins[$name]->objectclasses as $oc){
50             $this->objectclasses[] = $oc;
51           }
52         }
53       }else{
54         trigger_error("Service class missing: ".$plug['CLASS']);
55       }
56     }
57     $this->divList = new divListSystemService($config,$this);
58         }
60   function set_acl_base($base)
61   {
62     plugin::set_acl_base($base);
63     foreach($this->plugins as $name => $obj){
64       $this->plugins[$name]->set_acl_base($base);
65     }
66   }
68   function set_acl_category($category)
69   {
70     plugin::set_acl_category($category);
71     foreach($this->plugins as $name => $obj){
72       $this->plugins[$name]->set_acl_category($category);
73     }
74   }
76   function execute()
77   {
78     /* Variable initialisation */  
79     $s_action = "";
80     $s_entry  = "";
82     /* Walk through posts and check if there are some introductions for us */
83     $PossiblePosts = array("addNewService"  => "", 
84                            "startServices"  => "",
85                            "stopServices"   => "",
86                            "restartServices"  => "",
87                            "removeServices" => "",
89                            "StartSingleService"  => "/^StartSingleService_(.*)_[xy]$/",
90                            "StopSingleService"   => "/^StopSingleService_(.*)_[xy]$/",
91                            "RestartSingleService"  => "/^RestartSingleService_(.*)_[xy]$/",
92                            "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
93                            "EditSingleService"   => "/^EditSingleService_(.*)_[xy]$/");
95   
96     $once = true;
97     foreach($_POST as $name => $value){
98       foreach($PossiblePosts as $pregCheck => $idPreg) {
99         if(preg_match("/^".$pregCheck."/",$name) && $once){
100           $once     = false;
101           $s_action = $pregCheck;
102           
103           if(!empty($idPreg)){
104             $s_entry = preg_replace($idPreg,"\\1",$name);
105           }
106         }
107       }
108     }
111     /* Handle state changes for services */
112     $map =  array(    "startServices"         => array("type" => START_SERVICE ,   "service" => ALL_SERVICES),
113                       "stopServices"          => array("type" => STOP_SERVICE  ,   "service" => ALL_SERVICES),
114                       "restartServices"       => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES),                 
115                       "StartSingleService"    => array("type" => START_SERVICE ,   "service" => $s_entry),
116                       "StopSingleService"     => array("type" => STOP_SERVICE ,    "service" => $s_entry),
117                       "RestartSingleService"  => array("type" => RESTART_SERVICE , "service" => $s_entry));
118     if(isset($map[$s_action])){
119       $type     = $map[$s_action]['type'];
120       $service  = $map[$s_action]['service'];
121       $this->ServiceStatusUpdate($type,$service);
122     }
124     /* Handle actions linked via href */
125     if(isset($_GET['act']) && $_GET['act'] == "open" && isset($_GET['id'])){
126       $id = $_GET['id'];
127       if(isset($this->plugins[$id])){
128         $s_entry = $id;
129         $s_action = "EditSingleService";
130       } 
131     }
133     /* Open service add dialog */
134     if($s_action == "addNewService"){
135       $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
136     }
139     /* Remove service */
140     if($s_action == "RemoveSingleService"){
142       /* Create resetted obj */
143       $new_obj = new $s_entry($this->config,$this->dn);
144       $new_obj -> set_acl_base($this->acl_base);
145       $new_obj -> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
146       $tmp     = $new_obj->getListEntry();
148       if($tmp['AllowRemove']){
150         /* Check if we are allowed to remove this service
151          */
152         $str = $this->plugins[$s_entry]->allow_remove();
154         if(empty($str)){
155           $this->plugins[$s_entry] = $new_obj;
156           $this->plugins[$s_entry]->is_account = false;
157         }else{
158           print_red($str);
159         }
160       }
161     }
164     /* Edit a service and make a backup from all attributes, 
165        to be able to restore old values after aborting dialog */ 
166     if($s_action == "EditSingleService"){
167       $this->backup   = get_object_vars($this->plugins[$s_entry]);
168       $this->dialog   = $this->plugins[$s_entry];
169       $this->current  = $s_entry;
170     }
172    
173     /* Abort service add */
174     if(isset($_POST['CancelServiceAdd'])){
175       $this->dialog   = FALSE;
176       $this->backup   = NULL;
177       $this->current  = "";
178     }
180  
181     /* Abort dialog 
182        Restore vars with values before editing */
183     if(isset($_POST['CancelService']) && !empty($this->current)){
184       if($this->backup == NULL){
185         $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
186         $this->plugins[$this->current]-> set_acl_base($this->acl_base);
187         $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
189       }else{
190         foreach($this->backup as $name => $value){
191           $this->plugins[$this->current]->$name = $value;
192         }
193       }
194       $this->dialog   = FALSE;
195       $this->backup   = NULL;
196       $this->current  = ""; 
197     }
198   
200     /* Abort dialog */
201     if(isset($_POST['SaveService']) && is_object($this->dialog)){
202       $this->dialog->save_object();
203       $msgs = $this->dialog->check();
204       if(count($msgs)){
205         foreach($msgs as $msg){
206           print_red($msg);
207         }
208       }else{
209         $this->plugins[$this->current] = $this->dialog;
210         $tmp  = get_object_vars($this->dialog);
211         foreach($tmp as $name => $value){
212           $this->plugins[$this->current]->$name = $value;
213         }
214         $this->current = "";
215         $this->dialog = FALSE;
216         $this->backup = NULL;
217       }
218     }
221     /* Abort dialog */
222     if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
223       $serv = $_POST['ServiceName'];    
224       $this->plugins[$serv]->is_account = true;
225       $this->dialog  = $this->plugins[$serv];
226       $this->current = $serv;
227     }
230     /* There is currently a subdialog open, display this dialog */
231     if(is_object($this->dialog)){
232       $this->dialog->save_object();
233       return($this->dialog->execute());
234     }
237     /* Dispaly services overview */
238     $this->divList->execute();
239     $list = array();
241     foreach($this->plugins as $name => $obj){
242       if($obj->is_account){
243         $list[$name] = $this->plugins[$name]->getListEntry(); 
244       }
245     }
246     $this->divList -> setEntries($list);
247     return("<table style='width:100%;'><tr><td>".$this->divList->Draw()."</td></tr></table>");
248   }
251   /* Get all used services 
252       CLASSNAME => _($this->plugins[*]->DisplayName);   */
253   function getAllUsedServices()
254   {
255     $ret = array();
256     foreach($this->plugins as $name => $obj){
257       if($obj->is_account){
258         if(isset($obj->DisplayName)){
259           $ret[$name] = $obj->DisplayName;
260         }else{
261           $ret[$name] = $name;
262         }
263       }
264     }
265     return($ret);
266   }
269   /* Get all unused services 
270       CLASSNAME => _($this->plugins[*]->DisplayName);  */
271   function getAllUnusedServices()
272   {
273     $tmp = $this->getAllUsedServices();
274     $pool_of_ocs =array();
275     foreach($tmp as $name => $value){
276       if(isset($this->plugins[$name]->conflicts)){
277         $pool_of_ocs[]= get_class($this->plugins[$name]);
278       }
279     }
280    
281     $ret = array();
282     foreach($this->plugins as $name => $obj){
284       /* Skip all pluigns that will lead into conflicts */
285       $skip = false;
286       if(isset($obj->conflicts)){
287         foreach($obj->conflicts as $oc){
288           if(in_array_ics($oc,$pool_of_ocs)){
289             $skip = true;
290           }
291         }
292       }
294       /* Only show createable services */
295       if(!$obj->acl_is_createable()){
296         $skip = true;
297       }
299       if(!$skip){
300         if(isset($obj->DisplayName)){
301           $ret[$name] = $obj->DisplayName;
302         }else{
303           $ret[$name] = $name;
304         }
305       }
306     }
307     return($ret);
308   }
309  
310  
311   /* This function sets the status var for each used 
312      service && calls an external hook if specified in gosa.conf*/
313   function ServiceStatusUpdate($method , $service)
314   {
315     /* Skip if this is a new server */
316     if($this->dn == "new"){
317       print_red(_("Can't set status while this server is not saved."));
318       return;
319     }
321     $action = "";
322     if($method == START_SERVICE){
323       $action = SERVICE_STARTED;
324     }elseif($method== STOP_SERVICE){
325       $action = SERVICE_STOPPED;
326     }elseif($method == RESTART_SERVICE){
327       $action = SERVICE_RESTARTED;
328     }else{
329       print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
330       return;
331     }
332     
333     $caseVars = array("cn","dn");
334     if($service == ALL_SERVICES){
335       foreach($this->plugins as $name => $obj){
336         foreach($caseVars as $var){
337           if(isset($this->$var)){
338             $this->plugins[$name]->$var = $this->$var;  
339           }
340         }
342         /* check if services can be restarted */
343         $map =array(SERVICE_STARTED=> "AllowStart" ,
344                     SERVICE_STOPPED => "AllowStop",
345                     SERVICE_RESTARTED => "AllowRestart");
347         /* get plugins informations, restart/start/stop actions allowed ?*/
348         $tmp = $this->plugins[$name]->getListEntry();
350         /* Check if given action is allowed for this service */
351         if($tmp[$map[$action]]){
352           if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
353             $this->plugins[$name]->setStatus($action);
354           }
355         }
356       }
357     }else{
358       foreach($caseVars as $var){
359         if(isset($this->$var)){
360           $this->plugins[$service]->$var = $this->$var;  
361         }
362       }
363       if($this->plugins[$service]->is_account){
364         $this->plugins[$service]->setStatus($action);
365       }
366     }
367   }
370   function check()
371   {
372     $message = plugin::check();
373     return $message;
374   }
377   function save_object()
378   {
379     foreach($this->plugins as $name => $obj){
380       if($obj->is_account){
381         $this->plugins[$name]->save_object();
382       }
383     }
384   }
387   function remove_from_parent()
388   {
389     $caseVars = array("cn","dn");
390     foreach($this->plugins as $name => $obj){
391       foreach($caseVars as $var){
392         if(isset($this->$var)){
393           $this->plugins[$name]->$var = $this->$var;  
394         }
395       }
396       if($this->plugins[$name]->initially_was_account){
397         $this->plugins[$name]->remove_from_parent();
398       }
399     }
400   }
403   function save()
404   {
405     $caseVars = array("cn","dn");
406     foreach($this->plugins as $name => $obj){
408       foreach($caseVars as $var){
409         if(isset($this->$var)){
410           $this->plugins[$name]->$var = $this->$var;
411         }
412       }
414       if($this->plugins[$name]->is_account){
415         $this->plugins[$name]->save();
416       }else{
417         if($this->plugins[$name]->initially_was_account){
418           $this->plugins[$name]->remove_from_parent();
419         }
420       }
421     }
422   }
424   
425   function PrepareForCopyPaste($source)
426   {
427     plugin::PrepareForCopyPaste($source);
429     foreach($this->plugins as $name => $plugin){
430       $this->plugins[$name]->PrepareForCopyPaste($source);
431     }
432   }
435   /* Check if all plugins allow a remove ..  */
436   function allow_remove()
437   {
438     foreach($this->plugins as $name => $obj){
439       $str = $obj->allow_remove();
440       if(!empty($str)){
441         return($str);
442       }
443     }
444   }
445   
447 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
448 ?>