Code

Added ;
[gosa.git] / gosa-plugins / systems / admin / systems / class_serverService.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 define("START_SERVICE",1);
24 define("STOP_SERVICE",2);
25 define("RESTART_SERVICE",3);
27 define("SERVICE_STOPPED",_("Stop"));
28 define("SERVICE_STARTED",_("Start"));
29 define("SERVICE_RESTARTED",_("Restart"));
31 define("ALL_SERVICES",100);
33 class ServerService extends plugin
34 {
35   /* attribute list for save action */
36   var $ignore_account   = TRUE;
37   var $attributes       = array();
38   var $objectclasses    = array();
40   var $divList          = NULL;
42   var $plugins          = array();
43   var $pluign_names     = array();
45   var $current          = "";
46   var $backup           = NULL;
47   var $acl              ;
48   var $cn;
49   var $parent           ;
51   function ServerService (&$config, $dn,$parent)
52   {
53     plugin::plugin($config);
54     $this->parent = $parent;
55     $this->dn= $dn;
57     /* Adapt parent attributes */
58     if(isset($this->parent->attrs)){
59       $this->attrs = $this->parent->attrs;
60     }
62     foreach ($config->data['TABS']['SERVERSERVICE'] as $plug){
64       if(class_available($plug['CLASS'])){
66         $name= $plug['CLASS'];
67         $this->plugin_names[]= $name;
68         $this->plugins[$name]= new $name($config, $dn, $this);
70         /* Initialize kerberos key support */ 
71         if(isset($this->plugins[$name]->krb_service_prefix) && !empty($this->plugins[$name]->krb_service_prefix)){
72           $this->plugins[$name]->krb_host_key = &$this->parent->kerberos_key_service;
73         }
75         /* Capture all service objectClases, necessary for acl handling */ 
76         if(isset($this->plugins[$name]->objectclasses)){
77           foreach($this->plugins[$name]->objectclasses as $oc){
78             $this->objectclasses[] = $oc;
79           }
80         }
81       }else{
82         #trigger_error("Service class missing: ".$plug['CLASS']);
83       }
84     }
85     $this->divList = new divListSystemService($config,$this);
86         }
88   function set_acl_base($base)
89   {
90     plugin::set_acl_base($base);
91     foreach($this->plugins as $name => $obj){
92       $this->plugins[$name]->set_acl_base($base);
93     }
94   }
96   function set_acl_category($category)
97   {
98     plugin::set_acl_category($category);
99     foreach($this->plugins as $name => $obj){
100       $this->plugins[$name]->set_acl_category($category);
101     }
102   }
104   function execute()
105   {
106     /* Variable initialisation */  
107     $s_action = "";
108     $s_entry  = "";
110     /* Walk through posts and check if there are some introductions for us */
111     $PossiblePosts = array("addNewService"  => "", 
112                            "startServices"  => "",
113                            "stopServices"   => "",
114                            "restartServices"  => "",
115                            "removeServices" => "",
117                            "StartSingleService"  => "/^StartSingleService_(.*)_[xy]$/",
118                            "StopSingleService"   => "/^StopSingleService_(.*)_[xy]$/",
119                            "RestartSingleService"  => "/^RestartSingleService_(.*)_[xy]$/",
120                            "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
121                            "EditSingleService"   => "/^EditSingleService_(.*)_[xy]$/");
123   
124     $once = true;
125     foreach($_POST as $name => $value){
126       foreach($PossiblePosts as $pregCheck => $idPreg) {
127         if(preg_match("/^".$pregCheck."/",$name) && $once){
128           $once     = false;
129           $s_action = $pregCheck;
130           
131           if(!empty($idPreg)){
132             $s_entry = preg_replace($idPreg,"\\1",$name);
133           }
134         }
135       }
136     }
139     /* Handle state changes for services */
140     $map =  array(    "startServices"         => array("type" => START_SERVICE ,   "service" => ALL_SERVICES),
141                       "stopServices"          => array("type" => STOP_SERVICE  ,   "service" => ALL_SERVICES),
142                       "restartServices"       => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES),                 
143                       "StartSingleService"    => array("type" => START_SERVICE ,   "service" => $s_entry),
144                       "StopSingleService"     => array("type" => STOP_SERVICE ,    "service" => $s_entry),
145                       "RestartSingleService"  => array("type" => RESTART_SERVICE , "service" => $s_entry));
146     if(isset($map[$s_action])){
147       $type     = $map[$s_action]['type'];
148       $service  = $map[$s_action]['service'];
149       $this->ServiceStatusUpdate($type,$service);
150     }
152     /* Handle actions linked via href */
153     if(isset($_GET['act']) && $_GET['act'] == "open" && isset($_GET['id'])){
154       $id = $_GET['id'];
155       if(isset($this->plugins[$id])){
156         $s_entry = $id;
157         $s_action = "EditSingleService";
158       } 
159     }
161     /* Open service add dialog */
162     if($s_action == "addNewService"){
163       $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
164     }
167     /* Remove service */
168     if($s_action == "RemoveSingleService"){
170       /* Create resetted obj */
171       $new_obj = new $s_entry($this->config,$this->dn, $this);
172       $new_obj -> set_acl_base($this->acl_base);
173       $new_obj -> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
174       $tmp     = $new_obj->getListEntry();
176       if($tmp['AllowRemove']){
178         /* Check if we are allowed to remove this service
179          */
180         $str = $this->plugins[$s_entry]->allow_remove();
182         if(empty($str)){
183           $this->plugins[$s_entry] = $new_obj;
184           $this->plugins[$s_entry]->is_account = false;
185         }else{
186           msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
187         }
188       }
189     }
192     /* Edit a service and make a backup from all attributes, 
193        to be able to restore old values after aborting dialog */ 
194     if($s_action == "EditSingleService"){
195       if($this->plugins[$s_entry]->acl_is_readable("")){
196         $this->backup   = get_object_vars($this->plugins[$s_entry]);
197         $this->dialog   = $this->plugins[$s_entry];
198         $this->current  = $s_entry;
199       }
200     }
202    
203     /* Abort service add */
204     if(isset($_POST['CancelServiceAdd'])){
205       $this->dialog   = FALSE;
206       $this->backup   = NULL;
207       $this->current  = "";
208     }
210  
211     /* Abort dialog 
212        Restore vars with values before editing */
213     if(isset($_POST['CancelService']) && !empty($this->current)){
214       if($this->backup == NULL){
215         $this->plugins[$this->current] = new $this->current($this->config,$this->dn,$this);
216         $this->plugins[$this->current]-> set_acl_base($this->acl_base);
217         $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
219       }else{
220         foreach($this->backup as $name => $value){
221           $this->plugins[$this->current]->$name = $value;
222         }
223       }
224       $this->dialog   = FALSE;
225       $this->backup   = NULL;
226       $this->current  = ""; 
227     }
228   
230     /* Abort dialog */
231     if(isset($_POST['SaveService']) && is_object($this->dialog)){
232 #      $this->dialog->save_object();
233       $msgs = $this->dialog->check();
234       if(count($msgs)){
235         foreach($msgs as $msg){
236           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
237         }
238       }else{
239         $this->plugins[$this->current] = $this->dialog;
240         $tmp  = get_object_vars($this->dialog);
241         foreach($tmp as $name => $value){
242           $this->plugins[$this->current]->$name = $value;
243         }
244         $this->current = "";
245         $this->dialog = FALSE;
246         $this->backup = NULL;
247       }
248     }
251     /* Abort dialog */
252     if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
253       $serv = $_POST['ServiceName'];    
254       $this->plugins[$serv]->is_account = true;
255       $this->dialog  = $this->plugins[$serv];
256       $this->current = $serv;
257     }
260     /* There is currently a subdialog open, display this dialog */
261     if(is_object($this->dialog)){
262       $add ="";
263       if(isset($this->dialog->krb_host_key) && $this->dialog->krb_host_key instanceof krbHostKeys){
264         $add = $this->dialog->krb_host_key->save_object_by_prefix($this->dialog->krb_service_prefix);
265         $add = $this->dialog->krb_host_key->execute_by_prefix($this->dialog->krb_service_prefix,TRUE);
266       }
267       return($add.$this->dialog->execute());
268     }
271     /* Dispaly services overview */
272     $this->divList->execute();
273     $list = array();
275     foreach($this->plugins as $name => $obj){
276       if($obj->is_account){
277         $list[$name] = $this->plugins[$name]->getListEntry(); 
278       }
279     }
280     $this->divList -> setEntries($list);
281     return("<table style='width:100%;'><tr><td>".$this->divList->Draw()."</td></tr></table>");
282   }
285   /* Get all used services 
286       CLASSNAME => _($this->plugins[*]->DisplayName);   */
287   function getAllUsedServices()
288   {
289     $ret = array();
290     foreach($this->plugins as $name => $obj){
291       if($obj->is_account){
292         if(isset($obj->DisplayName)){
293           $ret[$name] = $obj->DisplayName;
294         }else{
295           $ret[$name] = $name;
296         }
297       }
298     }
299     return($ret);
300   }
303   /* Get all unused services 
304       CLASSNAME => _($this->plugins[*]->DisplayName);  */
305   function getAllUnusedServices()
306   {
307     $tmp = $this->getAllUsedServices();
308     $pool_of_ocs =array();
309     foreach($tmp as $name => $value){
310       if(isset($this->plugins[$name]->conflicts)){
311         $pool_of_ocs[]= get_class($this->plugins[$name]);
312       }
313     }
314    
315     $ret = array();
316     foreach($this->plugins as $name => $obj){
318       /* Skip all pluigns that will lead into conflicts */
319       $skip = false;
320       if(isset($obj->conflicts)){
321         foreach($obj->conflicts as $oc){
322           if(in_array_ics($oc,$pool_of_ocs)){
323             $skip = true;
324           }
325         }
326       }
328       /* Only show createable services */
329       if(!$obj->acl_is_createable()){
330         $skip = true;
331       }
333       if(!$skip){
334         if(isset($obj->DisplayName)){
335           $ret[$name] = $obj->DisplayName;
336         }else{
337           $ret[$name] = $name;
338         }
339       }
340     }
341     return($ret);
342   }
343  
344  
345   /* This function sets the status var for each used 
346      service && calls an external hook if specified in gosa.conf*/
347   function ServiceStatusUpdate($method , $service)
348   {
349     /* Skip if this is a new server */
350     if($this->dn == "new"){
351       msg_dialog::display(_("Information"), _("Cannot update service status until it has been saved!"), INFO_DIALOG);
352       return;
353     }
355     $action = "";
356     if($method == START_SERVICE){
357       $action = SERVICE_STARTED;
358     }elseif($method== STOP_SERVICE){
359       $action = SERVICE_STOPPED;
360     }elseif($method == RESTART_SERVICE){
361       $action = SERVICE_RESTARTED;
362     }else{
363       msg_dialog::display(_("Error"), _("Cannot update service status!"), ERROR_DIALOG);
364       return;
365     }
366     
367     $caseVars = array("cn","dn");
368     if($service == ALL_SERVICES){
369       foreach($this->plugins as $name => $obj){
370         foreach($caseVars as $var){
371           if(isset($this->$var)){
372             $this->plugins[$name]->$var = $this->$var;  
373           }
374         }
376         /* check if services can be restarted */
377         $map =array(SERVICE_STARTED=> "AllowStart" ,
378                     SERVICE_STOPPED => "AllowStop",
379                     SERVICE_RESTARTED => "AllowRestart");
381         /* get plugins informations, restart/start/stop actions allowed ?*/
382         $tmp = $this->plugins[$name]->getListEntry();
384         /* Check if given action is allowed for this service */
385         if($tmp[$map[$action]] && !empty($this->plugins[$name]->StatusFlag)){
386           if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
387             $this->plugins[$name]->setStatus($action);
388           }
389         }
390       }
391     }else{
392       foreach($caseVars as $var){
393         if(isset($this->$var)){
394           $this->plugins[$service]->$var = $this->$var;  
395         }
396       }
397       if($this->plugins[$service]->is_account){
398         $this->plugins[$service]->setStatus($action);
399       }
400     }
401   }
404   function check()
405   {
406     $message = plugin::check();
407     return $message;
408   }
411   function save_object()
412   {
413     foreach($this->plugins as $name => $obj){
414       if($obj->is_account){
415         $this->plugins[$name]->save_object();
416       }
417     }
418   }
421   function remove_from_parent()
422   {
423     $caseVars = array("cn","dn");
424     foreach($this->plugins as $name => $obj){
425       foreach($caseVars as $var){
426         if(isset($this->$var)){
427           $this->plugins[$name]->$var = $this->$var;  
428         }
429       }
430       if($this->plugins[$name]->initially_was_account){
431         if(isset($this->plugins[$name]->krb_host_key) && $this->plugins[$name]->krb_host_key instanceof krbHostKeys){
432           $this->plugins[$name]->krb_host_key->remove_from_parent_by_prefix($this->plugins[$name]->krb_service_prefix);
433         }
434         $this->plugins[$name]->remove_from_parent();
435       }
436     }
437   }
440   function save()
441   {
442     $caseVars = array("cn","dn");
443     foreach($this->plugins as $name => $obj){
445       foreach($caseVars as $var){
446         if(isset($this->$var)){
447           $this->plugins[$name]->$var = $this->$var;
448         }
449       }
451       if($this->plugins[$name]->is_account){
452         $this->plugins[$name]->save();
453       }else{
454         if($this->plugins[$name]->initially_was_account){
455           $this->plugins[$name]->remove_from_parent();
456         }
457       }
458     }
459   }
461   
462   function PrepareForCopyPaste($source)
463   {
464     plugin::PrepareForCopyPaste($source);
466     foreach($this->plugins as $name => $plugin){
467       $this->plugins[$name]->PrepareForCopyPaste($source);
468     }
469   }
472   /* Check if all plugins allow a remove ..  */
473   function allow_remove()
474   {
475     foreach($this->plugins as $name => $obj){
476       $str = $obj->allow_remove();
477       if(!empty($str)){
478         return($str);
479       }
480     }
481   }
482   
484 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
485 ?>