Code

63859818497e71d153566a1bdbb4b60200209f07
[gosa.git] / plugins / 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;
26   var $dialog           = NULL;       // Contains dialog object if a dialog is opened 
28   var $plugins          = array();
29   var $pluign_names     = array();
31   var $current          = "";
32   var $backup           = NULL;
33   var $acl              ;
34   var $cn;
36   function ServerService ($config, $dn)
37   {
38     plugin::plugin($config);
39     $this->dn= $dn;
40     foreach ($config->data['TABS']['SERVERSERVICE'] as $plug){
41       $name= $plug['CLASS'];
42       $this->plugin_names[]= $name;
43       $this->plugins[$name]= new $name($config, $dn);
44     }
45     $this->divList = new divListSystemService($config,$this);
46         }
49   function execute()
50   {
51     /* Variable initialisation */  
52     $s_action = "";
53     $s_entry  = "";
55     /* Walk through posts and check if there are some introductions for us */
56     $PossiblePosts = array("addNewService"  => "", 
57                            "startServices"  => "",
58                            "stopServices"   => "",
59                            "restartServices"  => "",
60                            "removeServices" => "",
62                            "StartSingleService"  => "/^StartSingleService_(.*)_[xy]$/",
63                            "StopSingleService"   => "/^StopSingleService_(.*)_[xy]$/",
64                            "RestartSingleService"  => "/^RestartSingleService_(.*)_[xy]$/",
65                            "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
66                            "EditSingleService"   => "/^EditSingleService_(.*)_[xy]$/");
68   
69     $once = true;
70     foreach($_POST as $name => $value){
71       foreach($PossiblePosts as $pregCheck => $idPreg) {
72         if(preg_match("/^".$pregCheck."/",$name) && $once){
73           $once     = false;
74           $s_action = $pregCheck;
75           
76           if(!empty($idPreg)){
77             $s_entry = preg_replace($idPreg,"\\1",$name);
78           }
79         }
80       }
81     }
84     /* Handle state changes for services */
85     $map =  array(    "startServices"         => array("type" => START_SERVICE ,   "service" => ALL_SERVICES),
86                       "stopServices"          => array("type" => STOP_SERVICE  ,   "service" => ALL_SERVICES),
87                       "restartServices"       => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES),                 
88                       "StartSingleService"    => array("type" => START_SERVICE ,   "service" => $s_entry),
89                       "StopSingleService"     => array("type" => STOP_SERVICE ,    "service" => $s_entry),
90                       "RestartSingleService"  => array("type" => RESTART_SERVICE , "service" => $s_entry));
91     if(isset($map[$s_action])){
92       $type     = $map[$s_action]['type'];
93       $service  = $map[$s_action]['service'];
94       $this->ServiceStatusUpdate($type,$service);
95     }
98     /* Open service add dialog */
99     if($s_action == "addNewService"){
100       $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
101     }
104     /* Remove service */
105     if($s_action == "RemoveSingleService"){
106       $this->plugins[$s_entry] = new $s_entry($this->config,$this->dn);
107       $this->plugins[$s_entry]->is_account = false;
108     }
111     /* Edit a service and make a backup from all attributes, 
112        to be able to restore old values after aborting dialog */ 
113     if($s_action == "EditSingleService"){
114       $this->backup   = get_object_vars($this->plugins[$s_entry]);
115       $this->dialog   = $this->plugins[$s_entry];
116       $this->current  = $s_entry;
117     }
119    
120     /* Abort service add */
121     if(isset($_POST['CancelServiceAdd'])){
122       $this->dialog   = NULL;
123       $this->backup   = NULL;
124       $this->current  = "";
125     }
127  
128     /* Abort dialog 
129        Restore vars with values before editing */
130     if(isset($_POST['CancelService'])){
131       if($this->backup == NULL){
132         $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
133       }else{
134         foreach($this->backup as $name => $value){
135           $this->plugins[$this->current]->$name = $value;
136         }
137       }
138       $this->dialog   = NULL;
139       $this->backup   = NULL;
140       $this->current  = ""; 
141     }
142   
144     /* Abort dialog */
145     if(isset($_POST['SaveService'])){
146       $this->dialog->save_object();
147       $msgs = $this->dialog->check();
148       if(count($msgs)){
149         foreach($msgs as $msg){
150           print_red($msg);
151         }
152       }else{
153         $this->plugins[$this->current] = $this->dialog;
154         $tmp  = get_object_vars($this->dialog);
155         foreach($tmp as $name => $value){
156           $this->plugins[$this->current]->$name = $value;
157         }
158         $this->current = "";
159         $this->dialog = NULL;
160         $this->backup = NULL;
161       }
162     }
165     /* Abort dialog */
166     if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
167       $serv = $_POST['ServiceName'];    
168       $this->plugins[$serv]->is_account = true;
169       $this->dialog  = $this->plugins[$serv];
170       $this->current = $serv;
171     }
174     /* There is currently a subdialog open, display this dialog */
175     if($this->dialog != NULL){
176       $this->dialog->save_object();
177       return($this->dialog->execute());
178     }
181     /* Dispaly services overview */
182     $this->divList->execute();
183     $list = array();
185     foreach($this->plugins as $name => $obj){
186       if($obj->is_account){
187         $list[$name] = $this->plugins[$name]->getListEntry(); 
188       }
189     }
190     $this->divList -> setEntries($list);
191     return($this->divList->Draw());
192   }
195   /* Get all used services 
196       CLASSNAME => _($this->plugins[*]->DisplayName);   */
197   function getAllUsedServices()
198   {
199     $ret = array();
200     foreach($this->plugins as $name => $obj){
201       if($obj->is_account){
202         if(isset($obj->DisplayName)){
203           $ret[$name] = $obj->DisplayName;
204         }else{
205           $ret[$name] = $name;
206         }
207       }
208     }
209     return($ret);
210   }
213   /* Get all unused services 
214       CLASSNAME => _($this->plugins[*]->DisplayName);  */
215   function getAllUnusedServices()
216   {
217     $tmp = $this->getAllUsedServices();
218     $pool_of_ocs =array();
219     foreach($tmp as $name => $value){
220       if(isset($this->plugins[$name]->conflicts)){
221         $pool_of_ocs = array_merge($pool_of_ocs,$this->plugins[$name]->conflicts);
222       }
223     }
224    
225     $ret = array();
226     foreach($this->plugins as $name => $obj){
228       /* Skip all pluigns that will lead into conflicts */
229       $skip = false;
230       if(isset($obj->conflicts)){
231         foreach($obj->conflicts as $oc){
232           if(in_array_ics($oc,$pool_of_ocs)){
233             $skip = true;
234           }
235         }
236       }
237       if(!$skip){
238         if(isset($obj->DisplayName)){
239           $ret[$name] = $obj->DisplayName;
240         }else{
241           $ret[$name] = $name;
242         }
243       }
244     }
245     return($ret);
246   }
247  
248  
249   /* This function sets the status var for each used 
250      service && calls an external hook if specified in gosa.conf*/
251   function ServiceStatusUpdate($method , $service)
252   {
253     /* Skip if this is a new server */
254     if($this->dn == "new"){
255       print_red(_("Can't set status while this server is not saved."));
256       return;
257     }
259     $action = "";
260     if($method == START_SERVICE){
261       $action = SERVICE_STARTED;
262     }elseif($method== STOP_SERVICE){
263       $action = SERVICE_STOPPED;
264     }elseif($method == RESTART_SERVICE){
265       $action = SERVICE_RESTARTED;
266     }else{
267       print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
268       return;
269     }
270     
271     $caseVars = array("cn","dn");
272     if($service == ALL_SERVICES){
273       foreach($this->plugins as $name => $obj){
274         foreach($caseVars as $var){
275           if(isset($this->$var)){
276             $this->plugins[$name]->$var = $this->$var;  
277           }
278         }
280         /* check if services can be restarted */
281         $map =array(SERVICE_STARTED=> "AllowStart" ,
282                     SERVICE_STOPPED => "AllowStop",
283                     SERVICE_RESTARTED => "AllowRestart");
285         /* get plugins informations, restart/start/stop actions allowed ?*/
286         $tmp = $this->plugins[$name]->getListEntry();
288         /* Check if given action is allowed for this service */
289         if($tmp[$map[$action]]){
290           if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
291             $this->plugins[$name]->setStatus($action);
292           }
293         }
294       }
295     }else{
296       foreach($caseVars as $var){
297         if(isset($this->$var)){
298           $this->plugins[$service]->$var = $this->$var;  
299         }
300       }
301       if($this->plugins[$service]->is_account){
302         $this->plugins[$service]->setStatus($action);
303       }
304     }
305   }
308   function check()
309   {
310     $message = plugin::check();
311     return $message;
312   }
315   function save_object()
316   {
317     foreach($this->plugins as $name => $obj){
318       if($obj->is_account){
319         $this->plugins[$name]->save_object();
320       }
321     }
322   }
325   function remove_from_parent()
326   {
327     $caseVars = array("cn","dn");
328     foreach($this->plugins as $name => $obj){
329       foreach($caseVars as $var){
330         if(isset($this->$var)){
331           $this->plugins[$name]->$var = $this->$var;  
332         }
333       }
334       if($this->plugins[$name]->initially_was_account){
335         $this->plugins[$name]->remove_from_parent();
336       }
337     }
338   }
341   function save()
342   {
343     $caseVars = array("cn","dn");
344     foreach($this->plugins as $name => $obj){
346       foreach($caseVars as $var){
347         if(isset($this->$var)){
348           $this->plugins[$name]->$var = $this->$var;
349         }
350       }
352       if($this->plugins[$name]->is_account){
353         $this->plugins[$name]->save();
354       }else{
355         if($this->plugins[$name]->initially_was_account){
356           $this->plugins[$name]->remove_from_parent();
357         }
358       }
359     }
360   }
361   
363 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
364 ?>