Code

338052b7e3b4a6e6b8b681d64ace29ab7cb4c64e
[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){
40       $name= $plug['CLASS'];
41       $this->plugin_names[]= $name;
42       $this->plugins[$name]= new $name($config, $dn);
43  
44       /* Capture all service objectClases, necessary for acl handling */ 
45       if(isset($this->plugins[$name]->objectclasses)){
46         foreach($this->plugins[$name]->objectclasses as $oc){
47           $this->objectclasses[] = $oc;
48         }
49       }
50     }
51     $this->divList = new divListSystemService($config,$this);
52         }
54   function set_acl_base($base)
55   {
56     plugin::set_acl_base($base);
57     foreach($this->plugins as $name => $obj){
58       $this->plugins[$name]->set_acl_base($base);
59     }
60   }
62   function set_acl_category($category)
63   {
64     plugin::set_acl_category($category);
65     foreach($this->plugins as $name => $obj){
66       $this->plugins[$name]->set_acl_category($category);
67     }
68   }
70   function execute()
71   {
72     /* Variable initialisation */  
73     $s_action = "";
74     $s_entry  = "";
76     /* Walk through posts and check if there are some introductions for us */
77     $PossiblePosts = array("addNewService"  => "", 
78                            "startServices"  => "",
79                            "stopServices"   => "",
80                            "restartServices"  => "",
81                            "removeServices" => "",
83                            "StartSingleService"  => "/^StartSingleService_(.*)_[xy]$/",
84                            "StopSingleService"   => "/^StopSingleService_(.*)_[xy]$/",
85                            "RestartSingleService"  => "/^RestartSingleService_(.*)_[xy]$/",
86                            "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
87                            "EditSingleService"   => "/^EditSingleService_(.*)_[xy]$/");
89   
90     $once = true;
91     foreach($_POST as $name => $value){
92       foreach($PossiblePosts as $pregCheck => $idPreg) {
93         if(preg_match("/^".$pregCheck."/",$name) && $once){
94           $once     = false;
95           $s_action = $pregCheck;
96           
97           if(!empty($idPreg)){
98             $s_entry = preg_replace($idPreg,"\\1",$name);
99           }
100         }
101       }
102     }
105     /* Handle state changes for services */
106     $map =  array(    "startServices"         => array("type" => START_SERVICE ,   "service" => ALL_SERVICES),
107                       "stopServices"          => array("type" => STOP_SERVICE  ,   "service" => ALL_SERVICES),
108                       "restartServices"       => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES),                 
109                       "StartSingleService"    => array("type" => START_SERVICE ,   "service" => $s_entry),
110                       "StopSingleService"     => array("type" => STOP_SERVICE ,    "service" => $s_entry),
111                       "RestartSingleService"  => array("type" => RESTART_SERVICE , "service" => $s_entry));
112     if(isset($map[$s_action])){
113       $type     = $map[$s_action]['type'];
114       $service  = $map[$s_action]['service'];
115       $this->ServiceStatusUpdate($type,$service);
116     }
118     /* Handle actions linked via href */
119     if(isset($_GET['act']) && $_GET['act'] == "open" && isset($_GET['id'])){
120       $id = $_GET['id'];
121       if(isset($this->plugins[$id])){
122         $s_entry = $id;
123         $s_action = "EditSingleService";
124       } 
125     }
127     /* Open service add dialog */
128     if($s_action == "addNewService"){
129       $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
130     }
133     /* Remove service */
134     if($s_action == "RemoveSingleService"){
136       /* Create resetted obj */
137       $new_obj = new $s_entry($this->config,$this->dn);
138       $new_obj -> set_acl_base($this->acl_base);
139       $new_obj -> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
140       $tmp     = $new_obj->getListEntry();
142       if($tmp['AllowRemove']){
144         /* Check if we are allowed to remove this service
145          */
146         $str = $this->plugins[$s_entry]->allow_remove();
148         if(empty($str)){
149           $this->plugins[$s_entry] = $new_obj;
150           $this->plugins[$s_entry]->is_account = false;
151         }else{
152           print_red($str);
153         }
154       }
155     }
158     /* Edit a service and make a backup from all attributes, 
159        to be able to restore old values after aborting dialog */ 
160     if($s_action == "EditSingleService"){
161       $this->backup   = get_object_vars($this->plugins[$s_entry]);
162       $this->dialog   = $this->plugins[$s_entry];
163       $this->current  = $s_entry;
164     }
166    
167     /* Abort service add */
168     if(isset($_POST['CancelServiceAdd'])){
169       $this->dialog   = FALSE;
170       $this->backup   = NULL;
171       $this->current  = "";
172     }
174  
175     /* Abort dialog 
176        Restore vars with values before editing */
177     if(isset($_POST['CancelService']) && !empty($this->current)){
178       if($this->backup == NULL){
179         $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
180         $this->plugins[$this->current]-> set_acl_base($this->acl_base);
181         $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
183       }else{
184         foreach($this->backup as $name => $value){
185           $this->plugins[$this->current]->$name = $value;
186         }
187       }
188       $this->dialog   = FALSE;
189       $this->backup   = NULL;
190       $this->current  = ""; 
191     }
192   
194     /* Abort dialog */
195     if(isset($_POST['SaveService']) && is_object($this->dialog)){
196       $this->dialog->save_object();
197       $msgs = $this->dialog->check();
198       if(count($msgs)){
199         foreach($msgs as $msg){
200           print_red($msg);
201         }
202       }else{
203         $this->plugins[$this->current] = $this->dialog;
204         $tmp  = get_object_vars($this->dialog);
205         foreach($tmp as $name => $value){
206           $this->plugins[$this->current]->$name = $value;
207         }
208         $this->current = "";
209         $this->dialog = FALSE;
210         $this->backup = NULL;
211       }
212     }
215     /* Abort dialog */
216     if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
217       $serv = $_POST['ServiceName'];    
218       $this->plugins[$serv]->is_account = true;
219       $this->dialog  = $this->plugins[$serv];
220       $this->current = $serv;
221     }
224     /* There is currently a subdialog open, display this dialog */
225     if(is_object($this->dialog)){
226       $this->dialog->save_object();
227       return($this->dialog->execute());
228     }
231     /* Dispaly services overview */
232     $this->divList->execute();
233     $list = array();
235     foreach($this->plugins as $name => $obj){
236       if($obj->is_account){
237         $list[$name] = $this->plugins[$name]->getListEntry(); 
238       }
239     }
240     $this->divList -> setEntries($list);
241     return("<table style='width:100%;'><tr><td>".$this->divList->Draw()."</td></tr></table>");
242   }
245   /* Get all used services 
246       CLASSNAME => _($this->plugins[*]->DisplayName);   */
247   function getAllUsedServices()
248   {
249     $ret = array();
250     foreach($this->plugins as $name => $obj){
251       if($obj->is_account){
252         if(isset($obj->DisplayName)){
253           $ret[$name] = $obj->DisplayName;
254         }else{
255           $ret[$name] = $name;
256         }
257       }
258     }
259     return($ret);
260   }
263   /* Get all unused services 
264       CLASSNAME => _($this->plugins[*]->DisplayName);  */
265   function getAllUnusedServices()
266   {
267     $tmp = $this->getAllUsedServices();
268     $pool_of_ocs =array();
269     foreach($tmp as $name => $value){
270       if(isset($this->plugins[$name]->conflicts)){
271         $pool_of_ocs[]= get_class($this->plugins[$name]);
272       }
273     }
274    
275     $ret = array();
276     foreach($this->plugins as $name => $obj){
278       /* Skip all pluigns that will lead into conflicts */
279       $skip = false;
280       if(isset($obj->conflicts)){
281         foreach($obj->conflicts as $oc){
282           if(in_array_ics($oc,$pool_of_ocs)){
283             $skip = true;
284           }
285         }
286       }
288       /* Only show createable services */
289       if(!$obj->acl_is_createable()){
290         $skip = true;
291       }
293       if(!$skip){
294         if(isset($obj->DisplayName)){
295           $ret[$name] = $obj->DisplayName;
296         }else{
297           $ret[$name] = $name;
298         }
299       }
300     }
301     return($ret);
302   }
303  
304  
305   /* This function sets the status var for each used 
306      service && calls an external hook if specified in gosa.conf*/
307   function ServiceStatusUpdate($method , $service)
308   {
309     /* Skip if this is a new server */
310     if($this->dn == "new"){
311       print_red(_("Can't set status while this server is not saved."));
312       return;
313     }
315     $action = "";
316     if($method == START_SERVICE){
317       $action = SERVICE_STARTED;
318     }elseif($method== STOP_SERVICE){
319       $action = SERVICE_STOPPED;
320     }elseif($method == RESTART_SERVICE){
321       $action = SERVICE_RESTARTED;
322     }else{
323       print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
324       return;
325     }
326     
327     $caseVars = array("cn","dn");
328     if($service == ALL_SERVICES){
329       foreach($this->plugins as $name => $obj){
330         foreach($caseVars as $var){
331           if(isset($this->$var)){
332             $this->plugins[$name]->$var = $this->$var;  
333           }
334         }
336         /* check if services can be restarted */
337         $map =array(SERVICE_STARTED=> "AllowStart" ,
338                     SERVICE_STOPPED => "AllowStop",
339                     SERVICE_RESTARTED => "AllowRestart");
341         /* get plugins informations, restart/start/stop actions allowed ?*/
342         $tmp = $this->plugins[$name]->getListEntry();
344         /* Check if given action is allowed for this service */
345         if($tmp[$map[$action]]){
346           if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
347             $this->plugins[$name]->setStatus($action);
348           }
349         }
350       }
351     }else{
352       foreach($caseVars as $var){
353         if(isset($this->$var)){
354           $this->plugins[$service]->$var = $this->$var;  
355         }
356       }
357       if($this->plugins[$service]->is_account){
358         $this->plugins[$service]->setStatus($action);
359       }
360     }
361   }
364   function check()
365   {
366     $message = plugin::check();
367     return $message;
368   }
371   function save_object()
372   {
373     foreach($this->plugins as $name => $obj){
374       if($obj->is_account){
375         $this->plugins[$name]->save_object();
376       }
377     }
378   }
381   function remove_from_parent()
382   {
383     $caseVars = array("cn","dn");
384     foreach($this->plugins as $name => $obj){
385       foreach($caseVars as $var){
386         if(isset($this->$var)){
387           $this->plugins[$name]->$var = $this->$var;  
388         }
389       }
390       if($this->plugins[$name]->initially_was_account){
391         $this->plugins[$name]->remove_from_parent();
392       }
393     }
394   }
397   function save()
398   {
399     $caseVars = array("cn","dn");
400     foreach($this->plugins as $name => $obj){
402       foreach($caseVars as $var){
403         if(isset($this->$var)){
404           $this->plugins[$name]->$var = $this->$var;
405         }
406       }
408       if($this->plugins[$name]->is_account){
409         $this->plugins[$name]->save();
410       }else{
411         if($this->plugins[$name]->initially_was_account){
412           $this->plugins[$name]->remove_from_parent();
413         }
414       }
415     }
416   }
418   
419   function PrepareForCopyPaste($source)
420   {
421     plugin::PrepareForCopyPaste($source);
423     foreach($this->plugins as $name => $plugin){
424       $this->plugins[$name]->PrepareForCopyPaste($source);
425     }
426   }
429   /* Check if all plugins allow a remove ..  */
430   function allow_remove()
431   {
432     foreach($this->plugins as $name => $obj){
433       $str = $obj->allow_remove();
434       if(!empty($str)){
435         return($str);
436       }
437     }
438   }
439   
441 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
442 ?>