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 management
34 {
35 /* attribute list for save action */
36 var $ignore_account = TRUE;
37 var $skipFooter = TRUE;
38 var $skipHeader = TRUE;
39 var $plugins = array();
40 var $pluign_names = array();
41 var $current = "";
42 var $dialog = FALSE;
43 var $read_only = FALSE;
44 var $backup = NULL;
45 var $acl ;
46 var $cn;
47 var $parent ;
48 var $pl_notify = FALSE;
49 var $acl_category = "";
50 var $acl_base = "";
52 var $is_account;
54 function __construct (&$config, $dn,$parent)
55 {
56 $this->config = $config;
57 $this->parent = $parent;
58 $this->ui = get_userinfo();
59 $this->dn= $dn;
60 if(isset($this->parent->attrs)){
61 $this->attrs = $this->parent->attrs;
62 }
64 // Set storage points - We do not have any - We just create a fake list which lists all services
65 $this->storagePoints = array();
67 // Build filter
68 if (session::global_is_set(get_class($this)."_filter")){
69 $filter= session::global_get(get_class($this)."_filter");
70 } else {
71 $filter = new filter(get_template_path("serverService-filter.xml", true));
72 $filter->setObjectStorage($this->storagePoints);
73 }
74 $this->setFilter($filter);
76 // Build headpage
77 $headpage = new listing(get_template_path("serverService-list.xml", true));
78 $headpage->registerElementFilter("filterServiceStatus", "ServerService::filterServiceStatus");
79 $headpage->setFilter($filter);
82 // Initialize list of used and useable services.
83 foreach ($config->data['TABS']['SERVERSERVICE'] as $plug){
84 if(class_available($plug['CLASS'])){
86 $name= $plug['CLASS'];
87 $this->plugin_names[]= $name;
88 $this->plugins[$name]= new $name($config, $dn, $this);
90 /* Initialize kerberos key support */
91 if(isset($this->plugins[$name]->krb_service_prefix) && !empty($this->plugins[$name]->krb_service_prefix)){
92 $this->plugins[$name]->krb_host_key = &$this->parent->kerberos_key_service;
93 }
95 /* Capture all service objectClases, necessary for acl handling */
96 if(isset($this->plugins[$name]->objectclasses)){
97 foreach($this->plugins[$name]->objectclasses as $oc){
98 $this->objectclasses[] = $oc;
99 }
100 }
101 }
102 }
104 parent::__construct($config, $this->ui, "services", $headpage);
105 $this->registerAction("new", "newService");
106 $this->registerAction("remove", "removeService");
107 $this->registerAction("newServiceSelected", "newServiceSelected");
108 $this->registerAction("saveService", "saveService");
109 $this->registerAction("cancelService", "cancelEdit");
110 $this->registerAction("newServiceCancel", "closeDialogs");
111 $this->registerAction("restart", "updateServiceStatus");
112 $this->registerAction("stop", "updateServiceStatus");
113 $this->registerAction("start", "updateServiceStatus");
114 }
117 /*! \brief Filter extra POST and GET variables for this plugin.
118 */
119 function detectPostActions()
120 {
121 $action = management::detectPostActions();
122 if(isset($_POST['SaveServiceAdd'])) $action['action'] = "newServiceSelected";
123 if(isset($_POST['CancelServiceAdd'])) $action['action'] = "newServiceCancel";
124 if(isset($_POST['SaveService'])) $action['action'] = "saveService";
125 if(isset($_POST['CancelService'])) $action['action'] = "cancelService";
126 return($action);
127 }
130 /*! \brief Edit an existing service here.
131 * Somebody clicked on the paper and pencil icon.
132 */
133 function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
134 {
135 $this->dialog = TRUE;
136 $s_entry = array_pop($target);
137 if($this->plugins[$s_entry]->acl_is_readable("")){
138 $this->backup = get_object_vars($this->plugins[$s_entry]);
139 $this->dialogObject = $this->plugins[$s_entry];
140 $this->current = $s_entry;
141 }
142 }
145 /*! \brief Editing an object was caneled.
146 * Close dialogs/tabs and remove locks.
147 */
148 protected function cancelEdit()
149 {
150 if($this->backup == NULL && $this->current){
151 $this->plugins[$this->current] = new $this->current($this->config,$this->dn,$this);
152 $this->plugins[$this->current]-> set_acl_base($this->acl_base);
153 $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
155 }elseif(is_array($this->backup)){
156 foreach($this->backup as $name => $value){
157 $this->plugins[$this->current]->$name = $value;
158 }
159 }
160 $this->backup = NULL;
161 $this->current = "";
162 $this->closeDialogs();
163 }
166 /*! \brief Let the user choose the new service to create.
167 */
168 function newService()
169 {
170 $this->dialog = TRUE;
171 $this->dialogObject = new ServiceAddDialog($this->config,$this->dn,$this);
172 }
175 /*! \brief Create the selected service.
176 */
177 function newServiceSelected()
178 {
179 $this->closeDialogs();
180 $serv = $_POST['ServiceName'];
181 $this->plugins[$serv]->is_account = true;
182 $this->dialogObject = $this->plugins[$serv];
183 $this->current = $serv;
184 $this->dialog = TRUE;
185 }
188 /*! \brief Save the currently edited service.
189 */
190 function saveService()
191 {
192 $this->dialogObject->save_object();
193 $msgs = $this->dialogObject->check();
194 if(count($msgs)){
195 foreach($msgs as $msg){
196 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
197 }
198 }else{
199 $this->plugins[$this->current] = $this->dialogObject;
200 $tmp = get_object_vars($this->dialogObject);
201 foreach($tmp as $name => $value){
202 $this->plugins[$this->current]->$name = $value;
203 }
204 $this->closeDialogs();
205 $this->backup = NULL;
206 }
207 }
210 /*!\brief Close all opened dialogs
211 * And reset "dialog open" flags to display bottom buttons again.
212 */
213 function closeDialogs()
214 {
215 management::closeDialogs();
216 $this->dialog = FALSE;
217 }
220 /*! \brief Remove the selected service(s)
221 */
222 protected function removeService($action="",$target=array(),$all=array())
223 {
224 foreach($target as $s_entry){
225 $new_obj = new $s_entry($this->config,$this->dn, $this);
226 $new_obj->set_acl_base($this->acl_base);
227 $new_obj->set_acl_category(preg_replace("/\/$/","",$this->acl_category));
228 $tmp = $new_obj->getListEntry();
230 /* Check if we are allowed to remove this service
231 */
232 if($tmp['AllowRemove']){
233 $str = $this->plugins[$s_entry]->allow_remove();
234 if(empty($str)){
235 $this->plugins[$s_entry] = $new_obj;
236 $this->plugins[$s_entry]->is_account = false;
237 }else{
238 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
239 }
240 }
241 }
242 }
245 /*! \brief Returns the list of of services, active services.
246 * Used in the filter class for services.
247 * class_filterServerService.inc
248 */
249 static function getServiceList()
250 {
251 return(session::get('ServerService'));
252 }
255 /*! \brief Returns an image for the service status.
256 * Used in the service list class.
257 */
258 static function filterServiceStatus($a,$b,$c,$d)
259 {
260 $img ="";
261 if(isset($d['0'])){
262 switch($d[0]){
263 case 'Restart' : $img = ('images/status_restarting.png'); break;
264 case 'Stop' : $img = ('images/lists/off.png'); break;
265 case 'Start' : $img = ('images/lists/on.png'); break;
266 }
267 }
268 if(!empty($img)){
269 return('<img src=\''.$img.'\'>');
270 }
271 return(" ");
272 }
275 /*! \brief Execute this plugin
276 * Handle actions/events, locking, snapshots, dialogs, tabs,...
277 */
278 function execute()
279 {
280 // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
281 $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
282 session::set('LOCK_VARS_TO_USE',$vars);
284 /* Display the copy & paste dialog, if it is currently open */
285 $ret = $this->copyPasteHandler("",array());
286 if($ret){
287 return($this->getHeader().$ret);
288 }
290 // Update filter
291 $filter = $this->getFilter();
292 if ($filter) {
293 $filter->update();
294 session::global_set(get_class($this)."_filter", $filter);
295 session::set('autocomplete', $filter);
296 if (!$filter->isValid()){
297 msg_dialog::display(_("Filter error"), _("The filter is incomplete!"), ERROR_DIALOG);
298 }
299 }
302 // Handle actions (POSTs and GETs)
303 $str = $this->handleActions($this->detectPostActions());
304 if($str) return($this->getHeader().$str);
306 // Open single dialog objects
307 if(is_object($this->dialogObject)){
308 if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object();
309 if(method_exists($this->dialogObject,'execute')){
311 /* There is currently a subdialog open, display this dialog */
312 $add ="";
313 if(isset($this->dialogObject->krb_host_key) && $this->dialogObject->krb_host_key instanceof krbHostKeys){
314 $add = $this->dialogObject->krb_host_key->save_object_by_prefix($this->dialogObject->krb_service_prefix);
315 $add = $this->dialogObject->krb_host_key->execute_by_prefix($this->dialogObject->krb_service_prefix,TRUE);
316 }
318 $display = $add.$this->dialogObject->execute();
319 $display.= $this->_getTabFooter();
320 return($this->getHeader().$display);
321 }
322 }
324 // Display tab object.
325 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
326 # $this->tabObject->save_object();
327 $display = $this->tabObject->execute();
328 $display.= $this->_getTabFooter();
329 return($this->getHeader().$display);
330 }
332 // Set current restore base for snapshot handling.
333 if(is_object($this->snapHandler)){
334 $bases = array();
335 foreach($this->storagePoints as $sp){
336 $bases[] = $sp.$this->headpage->getBase();
337 }
339 // No bases specified? Try base
340 if(!count($bases)) $bases[] = $this->headpage->getBase();
342 $this->snapHandler->setSnapshotBases($bases);
343 }
346 // Display list
347 session::set('ServerService', $this->plugins);
348 return($this->renderList());
349 }
352 /*! \brief Updates the status for a list of services.
353 */
354 function updateServiceStatus($action, $target,$all)
355 {
356 /* Handle state changes for services */
357 $map = array(
358 "start" => START_SERVICE ,
359 "stop" => STOP_SERVICE ,
360 "restart" => RESTART_SERVICE );
361 foreach($target as $s_entry){
362 if(isset($map[$action])){
363 $type = $map[$action];
364 $service = $s_entry;
365 $this->ServiceStatusUpdate($type,$service);
366 }
367 }
368 }
371 /*! \brief Returns a list of all used services
372 * CLASSNAME => _($this->plugins[*]->DisplayName);
373 */
374 function getAllUsedServices()
375 {
376 $ret = array();
377 foreach($this->plugins as $name => $obj){
378 if($obj->is_account){
379 if(isset($obj->DisplayName)){
380 $ret[$name] = $obj->DisplayName;
381 }else{
382 $ret[$name] = $name;
383 }
384 }
385 }
386 return($ret);
387 }
390 /*! \brief Returns a list of all unused services.
391 */
392 function getAllUnusedServices()
393 {
394 $tmp = $this->getAllUsedServices();
395 $pool_of_ocs =array();
396 foreach($tmp as $name => $value){
397 if(isset($this->plugins[$name]->conflicts)){
398 $pool_of_ocs[]= get_class($this->plugins[$name]);
399 }
400 }
402 $ret = array();
403 foreach($this->plugins as $name => $obj){
405 /* Skip all pluigns that will lead into conflicts */
406 $skip = false;
407 if(isset($obj->conflicts)){
408 foreach($obj->conflicts as $oc){
409 if(in_array_ics($oc,$pool_of_ocs)){
410 $skip = true;
411 }
412 }
413 }
415 if(!$obj->acl_is_createable()){
416 $skip = true;
417 }
419 if(!$skip){
420 if(isset($obj->DisplayName)){
421 $ret[$name] = $obj->DisplayName;
422 }else{
423 $ret[$name] = $name;
424 }
425 }
426 }
427 return($ret);
428 }
431 /*! \brief Updates the status of a service and
432 * calls an external hook if specified in gosa.conf
433 */
434 function ServiceStatusUpdate($method , $service)
435 {
436 /* Skip if this is a new server */
437 if($this->dn == "new"){
438 msg_dialog::display(_("Information"), _("Cannot update service status until it has been saved!"), INFO_DIALOG);
439 return;
440 }
442 $action = "";
443 if($method == START_SERVICE){
444 $action = SERVICE_STARTED;
445 }elseif($method== STOP_SERVICE){
446 $action = SERVICE_STOPPED;
447 }elseif($method == RESTART_SERVICE){
448 $action = SERVICE_RESTARTED;
449 }else{
450 msg_dialog::display(_("Error"), _("Cannot update service status!"), ERROR_DIALOG);
451 return;
452 }
454 $caseVars = array("cn","dn");
455 if($service == ALL_SERVICES){
456 foreach($this->plugins as $name => $obj){
457 foreach($caseVars as $var){
458 if(isset($this->$var)){
459 $this->plugins[$name]->$var = $this->$var;
460 }
461 }
463 /* check if services can be restarted */
464 $map =array(SERVICE_STARTED=> "AllowStart" ,
465 SERVICE_STOPPED => "AllowStop",
466 SERVICE_RESTARTED => "AllowRestart");
468 /* get plugins informations, restart/start/stop actions allowed ?*/
469 $tmp = $this->plugins[$name]->getListEntry();
471 /* Check if given action is allowed for this service */
472 if($tmp[$map[$action]] && !empty($this->plugins[$name]->StatusFlag)){
473 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
474 $this->plugins[$name]->setStatus($action);
475 }
476 }
477 }
478 }else{
479 foreach($caseVars as $var){
480 if(isset($this->$var)){
481 $this->plugins[$service]->$var = $this->$var;
482 }
483 }
484 if($this->plugins[$service]->is_account){
485 $this->plugins[$service]->setStatus($action);
486 }
487 }
488 }
491 /*! \brief No checks here.
492 */
493 function check()
494 {
495 return(array());
496 }
499 /*! \brief Keep posted form values in opened dialogs
500 */
501 function save_object()
502 {
503 foreach($this->plugins as $name => $obj){
504 if($obj->is_account){
505 $this->plugins[$name]->save_object();
506 }
507 }
508 }
511 /*! \brief Remove all active services
512 */
513 function remove_from_parent()
514 {
515 $caseVars = array("cn","dn");
516 foreach($this->plugins as $name => $obj){
517 foreach($caseVars as $var){
518 if(isset($this->$var)){
519 $this->plugins[$name]->$var = $this->$var;
520 }
521 }
522 if($this->plugins[$name]->initially_was_account){
523 if(isset($this->plugins[$name]->krb_host_key) && $this->plugins[$name]->krb_host_key instanceof krbHostKeys){
524 $this->plugins[$name]->krb_host_key->remove_from_parent_by_prefix($this->plugins[$name]->krb_service_prefix);
525 }
526 $this->plugins[$name]->remove_from_parent();
527 }
528 }
529 }
532 /*! \brief Save all active services
533 */
534 function save()
535 {
536 $caseVars = array("cn","dn");
537 foreach($this->plugins as $name => $obj){
539 foreach($caseVars as $var){
540 if(isset($this->$var)){
541 $this->plugins[$name]->$var = $this->$var;
542 }
543 }
545 if($this->plugins[$name]->is_account){
546 $this->plugins[$name]->save();
547 }else{
548 if($this->plugins[$name]->initially_was_account){
549 $this->plugins[$name]->remove_from_parent();
550 }
551 }
552 }
553 }
556 /*! \brief Prepare active services to be copied.
557 */
558 function PrepareForCopyPaste($source)
559 {
560 plugin::PrepareForCopyPaste($source);
562 foreach($this->plugins as $name => $plugin){
563 $this->plugins[$name]->PrepareForCopyPaste($source);
564 }
565 }
568 /*! \brief Check if there is an active service which prevents
569 * complete service removal.
570 */
571 function allow_remove()
572 {
573 foreach($this->plugins as $name => $obj){
574 $str = $obj->allow_remove();
575 if(!empty($str)){
576 return($str);
577 }
578 }
579 }
582 /*! \brief Forward plugin acls
583 */
584 function set_acl_base($base)
585 {
586 $this->acl_base = $base;
587 foreach($this->plugins as $name => $obj){
588 $this->plugins[$name]->set_acl_base($base);
589 }
590 }
593 /*! \brief Forward plugin acls
594 */
595 function set_acl_category($category)
596 {
597 $this->acl_category = $category;
598 foreach($this->plugins as $name => $obj){
599 $this->plugins[$name]->set_acl_category($category);
600 }
601 }
602 }
603 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
604 ?>