Code

Updated workstation - Startup
[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;
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     }
119     /* Open service add dialog */
120     if($s_action == "addNewService"){
121       $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
122     }
125     /* Remove service */
126     if($s_action == "RemoveSingleService"){
128       /* Create resetted obj */
129       $new_obj = new $s_entry($this->config,$this->dn);
130       $new_obj -> set_acl_base($this->acl_base);
131       $new_obj -> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
132       $tmp     = $new_obj->getListEntry();
134       if($tmp['AllowRemove']){
136         /* Check if we are allowed to remove this service
137          */
138         $str = $this->plugins[$s_entry]->allow_remove();
140         if(empty($str)){
141           $this->plugins[$s_entry] = $new_obj;
142           $this->plugins[$s_entry]->is_account = false;
143         }else{
144           print_red($str);
145         }
146       }
147     }
150     /* Edit a service and make a backup from all attributes, 
151        to be able to restore old values after aborting dialog */ 
152     if($s_action == "EditSingleService"){
153       $this->backup   = get_object_vars($this->plugins[$s_entry]);
154       $this->dialog   = $this->plugins[$s_entry];
155       $this->current  = $s_entry;
156     }
158    
159     /* Abort service add */
160     if(isset($_POST['CancelServiceAdd'])){
161       $this->dialog   = FALSE;
162       $this->backup   = NULL;
163       $this->current  = "";
164     }
166  
167     /* Abort dialog 
168        Restore vars with values before editing */
169     if(isset($_POST['CancelService']) && !empty($this->current)){
170       if($this->backup == NULL){
171         $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
172         $this->plugins[$this->current]-> set_acl_base($this->acl_base);
173         $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
175       }else{
176         foreach($this->backup as $name => $value){
177           $this->plugins[$this->current]->$name = $value;
178         }
179       }
180       $this->dialog   = FALSE;
181       $this->backup   = NULL;
182       $this->current  = ""; 
183     }
184   
186     /* Abort dialog */
187     if(isset($_POST['SaveService'])){
188       $this->dialog->save_object();
189       $msgs = $this->dialog->check();
190       if(count($msgs)){
191         foreach($msgs as $msg){
192           print_red($msg);
193         }
194       }else{
195         $this->plugins[$this->current] = $this->dialog;
196         $tmp  = get_object_vars($this->dialog);
197         foreach($tmp as $name => $value){
198           $this->plugins[$this->current]->$name = $value;
199         }
200         $this->current = "";
201         $this->dialog = FALSE;
202         $this->backup = NULL;
203       }
204     }
207     /* Abort dialog */
208     if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
209       $serv = $_POST['ServiceName'];    
210       $this->plugins[$serv]->is_account = true;
211       $this->dialog  = $this->plugins[$serv];
212       $this->current = $serv;
213     }
216     /* There is currently a subdialog open, display this dialog */
217     if(is_object($this->dialog)){
218       $this->dialog->save_object();
219       return($this->dialog->execute());
220     }
223     /* Dispaly services overview */
224     $this->divList->execute();
225     $list = array();
227     foreach($this->plugins as $name => $obj){
228       if($obj->is_account){
229         $list[$name] = $this->plugins[$name]->getListEntry(); 
230       }
231     }
232     $this->divList -> setEntries($list);
233     return("<table style='width:100%;'><tr><td>".$this->divList->Draw()."</td></tr></table>");
234   }
237   /* Get all used services 
238       CLASSNAME => _($this->plugins[*]->DisplayName);   */
239   function getAllUsedServices()
240   {
241     $ret = array();
242     foreach($this->plugins as $name => $obj){
243       if($obj->is_account){
244         if(isset($obj->DisplayName)){
245           $ret[$name] = $obj->DisplayName;
246         }else{
247           $ret[$name] = $name;
248         }
249       }
250     }
251     return($ret);
252   }
255   /* Get all unused services 
256       CLASSNAME => _($this->plugins[*]->DisplayName);  */
257   function getAllUnusedServices()
258   {
259     $tmp = $this->getAllUsedServices();
260     $pool_of_ocs =array();
261     foreach($tmp as $name => $value){
262       if(isset($this->plugins[$name]->conflicts)){
263         $pool_of_ocs[]= get_class($this->plugins[$name]);
264       }
265     }
266    
267     $ret = array();
268     foreach($this->plugins as $name => $obj){
270       /* Skip all pluigns that will lead into conflicts */
271       $skip = false;
272       if(isset($obj->conflicts)){
273         foreach($obj->conflicts as $oc){
274           if(in_array_ics($oc,$pool_of_ocs)){
275             $skip = true;
276           }
277         }
278       }
280       /* Only show createable services */
281       if(!$obj->acl_is_createable()){
282         $skip = true;
283       }
285       if(!$skip){
286         if(isset($obj->DisplayName)){
287           $ret[$name] = $obj->DisplayName;
288         }else{
289           $ret[$name] = $name;
290         }
291       }
292     }
293     return($ret);
294   }
295  
296  
297   /* This function sets the status var for each used 
298      service && calls an external hook if specified in gosa.conf*/
299   function ServiceStatusUpdate($method , $service)
300   {
301     /* Skip if this is a new server */
302     if($this->dn == "new"){
303       print_red(_("Can't set status while this server is not saved."));
304       return;
305     }
307     $action = "";
308     if($method == START_SERVICE){
309       $action = SERVICE_STARTED;
310     }elseif($method== STOP_SERVICE){
311       $action = SERVICE_STOPPED;
312     }elseif($method == RESTART_SERVICE){
313       $action = SERVICE_RESTARTED;
314     }else{
315       print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
316       return;
317     }
318     
319     $caseVars = array("cn","dn");
320     if($service == ALL_SERVICES){
321       foreach($this->plugins as $name => $obj){
322         foreach($caseVars as $var){
323           if(isset($this->$var)){
324             $this->plugins[$name]->$var = $this->$var;  
325           }
326         }
328         /* check if services can be restarted */
329         $map =array(SERVICE_STARTED=> "AllowStart" ,
330                     SERVICE_STOPPED => "AllowStop",
331                     SERVICE_RESTARTED => "AllowRestart");
333         /* get plugins informations, restart/start/stop actions allowed ?*/
334         $tmp = $this->plugins[$name]->getListEntry();
336         /* Check if given action is allowed for this service */
337         if($tmp[$map[$action]]){
338           if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
339             $this->plugins[$name]->setStatus($action);
340           }
341         }
342       }
343     }else{
344       foreach($caseVars as $var){
345         if(isset($this->$var)){
346           $this->plugins[$service]->$var = $this->$var;  
347         }
348       }
349       if($this->plugins[$service]->is_account){
350         $this->plugins[$service]->setStatus($action);
351       }
352     }
353   }
356   function check()
357   {
358     $message = plugin::check();
359     return $message;
360   }
363   function save_object()
364   {
365     foreach($this->plugins as $name => $obj){
366       if($obj->is_account){
367         $this->plugins[$name]->save_object();
368       }
369     }
370   }
373   function remove_from_parent()
374   {
375     $caseVars = array("cn","dn");
376     foreach($this->plugins as $name => $obj){
377       foreach($caseVars as $var){
378         if(isset($this->$var)){
379           $this->plugins[$name]->$var = $this->$var;  
380         }
381       }
382       if($this->plugins[$name]->initially_was_account){
383         $this->plugins[$name]->remove_from_parent();
384       }
385     }
386   }
389   function save()
390   {
391     $caseVars = array("cn","dn");
392     foreach($this->plugins as $name => $obj){
394       foreach($caseVars as $var){
395         if(isset($this->$var)){
396           $this->plugins[$name]->$var = $this->$var;
397         }
398       }
400       if($this->plugins[$name]->is_account){
401         $this->plugins[$name]->save();
402       }else{
403         if($this->plugins[$name]->initially_was_account){
404           $this->plugins[$name]->remove_from_parent();
405         }
406       }
407     }
408   }
410   
411   function PrepareForCopyPaste($source)
412   {
413     plugin::PrepareForCopyPaste($source);
415     foreach($this->plugins as $name => $plugin){
416       $this->plugins[$name]->PrepareForCopyPaste($source);
417     }
418   }
421   /* Check if all plugins allow a remove ..  */
422   function allow_remove()
423   {
424     foreach($this->plugins as $name => $obj){
425       $str = $obj->allow_remove();
426       if(!empty($str)){
427         return($str);
428       }
429     }
430   }
431   
433 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
434 ?>