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 /* Initialize acl_base */
58 $this->set_acl_base($this->dn);
60 /* Adapt parent attributes */
61 if(isset($this->parent->attrs)){
62 $this->attrs = $this->parent->attrs;
63 }
65 foreach ($config->data['TABS']['SERVERSERVICE'] as $plug){
67 if(class_available($plug['CLASS'])){
69 $name= $plug['CLASS'];
70 $this->plugin_names[]= $name;
71 $this->plugins[$name]= new $name($config, $dn, $this);
73 /* Initialize kerberos key support */
74 if(isset($this->plugins[$name]->krb_service_prefix) && !empty($this->plugins[$name]->krb_service_prefix)){
75 $this->plugins[$name]->krb_host_key = &$this->parent->kerberos_key_service;
76 }
78 /* Capture all service objectClases, necessary for acl handling */
79 if(isset($this->plugins[$name]->objectclasses)){
80 foreach($this->plugins[$name]->objectclasses as $oc){
81 $this->objectclasses[] = $oc;
82 }
83 }
84 }else{
85 #trigger_error("Service class missing: ".$plug['CLASS']);
86 }
87 }
88 $this->divList = new divListSystemService($config,$this);
89 }
91 function set_acl_base($base)
92 {
93 plugin::set_acl_base($base);
94 foreach($this->plugins as $name => $obj){
95 $this->plugins[$name]->set_acl_base($base);
96 }
97 }
99 function set_acl_category($category)
100 {
101 plugin::set_acl_category($category);
102 foreach($this->plugins as $name => $obj){
103 $this->plugins[$name]->set_acl_category($category);
104 }
105 }
107 function execute()
108 {
109 /* Variable initialisation */
110 $s_action = "";
111 $s_entry = "";
113 /* Walk through posts and check if there are some introductions for us */
114 $PossiblePosts = array("addNewService" => "",
115 "startServices" => "",
116 "stopServices" => "",
117 "restartServices" => "",
118 "removeServices" => "",
120 "StartSingleService" => "/^StartSingleService_(.*)_[xy]$/",
121 "StopSingleService" => "/^StopSingleService_(.*)_[xy]$/",
122 "RestartSingleService" => "/^RestartSingleService_(.*)_[xy]$/",
123 "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
124 "EditSingleService" => "/^EditSingleService_(.*)_[xy]$/");
127 $once = true;
128 foreach($_POST as $name => $value){
129 foreach($PossiblePosts as $pregCheck => $idPreg) {
130 if(preg_match("/^".$pregCheck."/",$name) && $once){
131 $once = false;
132 $s_action = $pregCheck;
134 if(!empty($idPreg)){
135 $s_entry = preg_replace($idPreg,"\\1",$name);
136 }
137 }
138 }
139 }
142 /* Handle state changes for services */
143 $map = array( "startServices" => array("type" => START_SERVICE , "service" => ALL_SERVICES),
144 "stopServices" => array("type" => STOP_SERVICE , "service" => ALL_SERVICES),
145 "restartServices" => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES),
146 "StartSingleService" => array("type" => START_SERVICE , "service" => $s_entry),
147 "StopSingleService" => array("type" => STOP_SERVICE , "service" => $s_entry),
148 "RestartSingleService" => array("type" => RESTART_SERVICE , "service" => $s_entry));
149 if(isset($map[$s_action])){
150 $type = $map[$s_action]['type'];
151 $service = $map[$s_action]['service'];
152 $this->ServiceStatusUpdate($type,$service);
153 }
155 /* Handle actions linked via href */
156 if(isset($_GET['act']) && $_GET['act'] == "open" && isset($_GET['id'])){
157 $id = $_GET['id'];
158 if(isset($this->plugins[$id])){
159 $s_entry = $id;
160 $s_action = "EditSingleService";
161 }
162 }
164 /* Open service add dialog */
165 if($s_action == "addNewService"){
166 $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
167 }
170 /* Remove service */
171 if($s_action == "RemoveSingleService"){
173 /* Create resetted obj */
174 $new_obj = new $s_entry($this->config,$this->dn, $this);
175 $new_obj -> set_acl_base($this->acl_base);
176 $new_obj -> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
177 $tmp = $new_obj->getListEntry();
179 if($tmp['AllowRemove']){
181 /* Check if we are allowed to remove this service
182 */
183 $str = $this->plugins[$s_entry]->allow_remove();
185 if(empty($str)){
186 $this->plugins[$s_entry] = $new_obj;
187 $this->plugins[$s_entry]->is_account = false;
188 }else{
189 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
190 }
191 }
192 }
195 /* Edit a service and make a backup from all attributes,
196 to be able to restore old values after aborting dialog */
197 if($s_action == "EditSingleService"){
198 if($this->plugins[$s_entry]->acl_is_readable("")){
199 $this->backup = get_object_vars($this->plugins[$s_entry]);
200 $this->dialog = $this->plugins[$s_entry];
201 $this->current = $s_entry;
202 }
203 }
206 /* Abort service add */
207 if(isset($_POST['CancelServiceAdd'])){
208 $this->dialog = FALSE;
209 $this->backup = NULL;
210 $this->current = "";
211 }
214 /* Abort dialog
215 Restore vars with values before editing */
216 if(isset($_POST['CancelService']) && !empty($this->current)){
217 if($this->backup == NULL){
218 $this->plugins[$this->current] = new $this->current($this->config,$this->dn,$this);
219 $this->plugins[$this->current]-> set_acl_base($this->acl_base);
220 $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
222 }else{
223 foreach($this->backup as $name => $value){
224 $this->plugins[$this->current]->$name = $value;
225 }
226 }
227 $this->dialog = FALSE;
228 $this->backup = NULL;
229 $this->current = "";
230 }
233 /* Abort dialog */
234 if(isset($_POST['SaveService']) && is_object($this->dialog)){
235 # $this->dialog->save_object();
236 $msgs = $this->dialog->check();
237 if(count($msgs)){
238 foreach($msgs as $msg){
239 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
240 }
241 }else{
242 $this->plugins[$this->current] = $this->dialog;
243 $tmp = get_object_vars($this->dialog);
244 foreach($tmp as $name => $value){
245 $this->plugins[$this->current]->$name = $value;
246 }
247 $this->current = "";
248 $this->dialog = FALSE;
249 $this->backup = NULL;
250 }
251 }
254 /* Abort dialog */
255 if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
256 $serv = $_POST['ServiceName'];
257 $this->plugins[$serv]->is_account = true;
258 $this->dialog = $this->plugins[$serv];
259 $this->current = $serv;
260 }
263 /* There is currently a subdialog open, display this dialog */
264 if(is_object($this->dialog)){
265 $add ="";
266 if(isset($this->dialog->krb_host_key) && $this->dialog->krb_host_key instanceof krbHostKeys){
267 $add = $this->dialog->krb_host_key->save_object_by_prefix($this->dialog->krb_service_prefix);
268 $add = $this->dialog->krb_host_key->execute_by_prefix($this->dialog->krb_service_prefix,TRUE);
269 }
270 return($add.$this->dialog->execute());
271 }
274 /* Dispaly services overview */
275 $this->divList->execute();
276 $list = array();
278 foreach($this->plugins as $name => $obj){
279 if($obj->is_account){
280 $list[$name] = $this->plugins[$name]->getListEntry();
281 }
282 }
283 $this->divList -> setEntries($list);
284 return("<table style='width:100%;'><tr><td>".$this->divList->Draw()."</td></tr></table>");
285 }
288 /* Get all used services
289 CLASSNAME => _($this->plugins[*]->DisplayName); */
290 function getAllUsedServices()
291 {
292 $ret = array();
293 foreach($this->plugins as $name => $obj){
294 if($obj->is_account){
295 if(isset($obj->DisplayName)){
296 $ret[$name] = $obj->DisplayName;
297 }else{
298 $ret[$name] = $name;
299 }
300 }
301 }
302 return($ret);
303 }
306 /* Get all unused services
307 CLASSNAME => _($this->plugins[*]->DisplayName); */
308 function getAllUnusedServices()
309 {
310 $tmp = $this->getAllUsedServices();
311 $pool_of_ocs =array();
312 foreach($tmp as $name => $value){
313 if(isset($this->plugins[$name]->conflicts)){
314 $pool_of_ocs[]= get_class($this->plugins[$name]);
315 }
316 }
318 $ret = array();
319 foreach($this->plugins as $name => $obj){
321 /* Skip all pluigns that will lead into conflicts */
322 $skip = false;
323 if(isset($obj->conflicts)){
324 foreach($obj->conflicts as $oc){
325 if(in_array_ics($oc,$pool_of_ocs)){
326 $skip = true;
327 }
328 }
329 }
331 /* Only show createable services */
332 if(!$obj->acl_is_createable()){
333 $skip = true;
334 }
336 if(!$skip){
337 if(isset($obj->DisplayName)){
338 $ret[$name] = $obj->DisplayName;
339 }else{
340 $ret[$name] = $name;
341 }
342 }
343 }
344 return($ret);
345 }
348 /* This function sets the status var for each used
349 service && calls an external hook if specified in gosa.conf*/
350 function ServiceStatusUpdate($method , $service)
351 {
352 /* Skip if this is a new server */
353 if($this->dn == "new"){
354 msg_dialog::display(_("Information"), _("Cannot update service status until it has been saved!"), INFO_DIALOG);
355 return;
356 }
358 $action = "";
359 if($method == START_SERVICE){
360 $action = SERVICE_STARTED;
361 }elseif($method== STOP_SERVICE){
362 $action = SERVICE_STOPPED;
363 }elseif($method == RESTART_SERVICE){
364 $action = SERVICE_RESTARTED;
365 }else{
366 msg_dialog::display(_("Error"), _("Cannot update service status!"), ERROR_DIALOG);
367 return;
368 }
370 $caseVars = array("cn","dn");
371 if($service == ALL_SERVICES){
372 foreach($this->plugins as $name => $obj){
373 foreach($caseVars as $var){
374 if(isset($this->$var)){
375 $this->plugins[$name]->$var = $this->$var;
376 }
377 }
379 /* check if services can be restarted */
380 $map =array(SERVICE_STARTED=> "AllowStart" ,
381 SERVICE_STOPPED => "AllowStop",
382 SERVICE_RESTARTED => "AllowRestart");
384 /* get plugins informations, restart/start/stop actions allowed ?*/
385 $tmp = $this->plugins[$name]->getListEntry();
387 /* Check if given action is allowed for this service */
388 if($tmp[$map[$action]] && !empty($this->plugins[$name]->StatusFlag)){
389 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
390 $this->plugins[$name]->setStatus($action);
391 }
392 }
393 }
394 }else{
395 foreach($caseVars as $var){
396 if(isset($this->$var)){
397 $this->plugins[$service]->$var = $this->$var;
398 }
399 }
400 if($this->plugins[$service]->is_account){
401 $this->plugins[$service]->setStatus($action);
402 }
403 }
404 }
407 function check()
408 {
409 $message = plugin::check();
410 return $message;
411 }
414 function save_object()
415 {
416 foreach($this->plugins as $name => $obj){
417 if($obj->is_account){
418 $this->plugins[$name]->save_object();
419 }
420 }
421 }
424 function remove_from_parent()
425 {
426 $caseVars = array("cn","dn");
427 foreach($this->plugins as $name => $obj){
428 foreach($caseVars as $var){
429 if(isset($this->$var)){
430 $this->plugins[$name]->$var = $this->$var;
431 }
432 }
433 if($this->plugins[$name]->initially_was_account){
434 if(isset($this->plugins[$name]->krb_host_key) && $this->plugins[$name]->krb_host_key instanceof krbHostKeys){
435 $this->plugins[$name]->krb_host_key->remove_from_parent_by_prefix($this->plugins[$name]->krb_service_prefix);
436 }
437 $this->plugins[$name]->remove_from_parent();
438 }
439 }
440 }
443 function save()
444 {
445 $caseVars = array("cn","dn");
446 foreach($this->plugins as $name => $obj){
448 foreach($caseVars as $var){
449 if(isset($this->$var)){
450 $this->plugins[$name]->$var = $this->$var;
451 }
452 }
454 if($this->plugins[$name]->is_account){
455 $this->plugins[$name]->save();
456 }else{
457 if($this->plugins[$name]->initially_was_account){
458 $this->plugins[$name]->remove_from_parent();
459 }
460 }
461 }
462 }
465 function PrepareForCopyPaste($source)
466 {
467 plugin::PrepareForCopyPaste($source);
469 foreach($this->plugins as $name => $plugin){
470 $this->plugins[$name]->PrepareForCopyPaste($source);
471 }
472 }
475 /* Check if all plugins allow a remove .. */
476 function allow_remove()
477 {
478 foreach($this->plugins as $name => $obj){
479 $str = $obj->allow_remove();
480 if(!empty($str)){
481 return($str);
482 }
483 }
484 }
486 }
487 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
488 ?>