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 }
298 // Handle actions (POSTs and GETs)
299 $str = $this->handleActions($this->detectPostActions());
300 if($str) return($this->getHeader().$str);
302 // Open single dialog objects
303 if(is_object($this->dialogObject)){
304 if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object();
305 if(method_exists($this->dialogObject,'execute')){
307 /* There is currently a subdialog open, display this dialog */
308 $add ="";
309 if(isset($this->dialogObject->krb_host_key) && $this->dialogObject->krb_host_key instanceof krbHostKeys){
310 $add = $this->dialogObject->krb_host_key->save_object_by_prefix($this->dialogObject->krb_service_prefix);
311 $add = $this->dialogObject->krb_host_key->execute_by_prefix($this->dialogObject->krb_service_prefix,TRUE);
312 }
314 $display = $add.$this->dialogObject->execute();
315 $display.= $this->_getTabFooter();
316 return($this->getHeader().$display);
317 }
318 }
320 // Display tab object.
321 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
322 # $this->tabObject->save_object();
323 $display = $this->tabObject->execute();
324 $display.= $this->_getTabFooter();
325 return($this->getHeader().$display);
326 }
328 // Set current restore base for snapshot handling.
329 if(is_object($this->snapHandler)){
330 $bases = array();
331 foreach($this->storagePoints as $sp){
332 $bases[] = $sp.$this->headpage->getBase();
333 }
335 // No bases specified? Try base
336 if(!count($bases)) $bases[] = $this->headpage->getBase();
338 $this->snapHandler->setSnapshotBases($bases);
339 }
342 // Display list
343 session::set('ServerService', $this->plugins);
344 return($this->renderList());
345 }
348 /*! \brief Updates the status for a list of services.
349 */
350 function updateServiceStatus($action, $target,$all)
351 {
352 /* Handle state changes for services */
353 $map = array(
354 "start" => START_SERVICE ,
355 "stop" => STOP_SERVICE ,
356 "restart" => RESTART_SERVICE );
357 foreach($target as $s_entry){
358 if(isset($map[$action])){
359 $type = $map[$action];
360 $service = $s_entry;
361 $this->ServiceStatusUpdate($type,$service);
362 }
363 }
364 }
367 /*! \brief Returns a list of all used services
368 * CLASSNAME => _($this->plugins[*]->DisplayName);
369 */
370 function getAllUsedServices()
371 {
372 $ret = array();
373 foreach($this->plugins as $name => $obj){
374 if($obj->is_account){
375 if(isset($obj->DisplayName)){
376 $ret[$name] = $obj->DisplayName;
377 }else{
378 $ret[$name] = $name;
379 }
380 }
381 }
382 return($ret);
383 }
386 /*! \brief Returns a list of all unused services.
387 */
388 function getAllUnusedServices()
389 {
390 $tmp = $this->getAllUsedServices();
391 $pool_of_ocs =array();
392 foreach($tmp as $name => $value){
393 if(isset($this->plugins[$name]->conflicts)){
394 $pool_of_ocs[]= get_class($this->plugins[$name]);
395 }
396 }
398 $ret = array();
399 foreach($this->plugins as $name => $obj){
401 /* Skip all pluigns that will lead into conflicts */
402 $skip = false;
403 if(isset($obj->conflicts)){
404 foreach($obj->conflicts as $oc){
405 if(in_array_ics($oc,$pool_of_ocs)){
406 $skip = true;
407 }
408 }
409 }
411 if(!$obj->acl_is_createable()){
412 $skip = true;
413 }
415 if(!$skip){
416 if(isset($obj->DisplayName)){
417 $ret[$name] = $obj->DisplayName;
418 }else{
419 $ret[$name] = $name;
420 }
421 }
422 }
423 return($ret);
424 }
427 /*! \brief Updates the status of a service and
428 * calls an external hook if specified in gosa.conf
429 */
430 function ServiceStatusUpdate($method , $service)
431 {
432 /* Skip if this is a new server */
433 if($this->dn == "new"){
434 msg_dialog::display(_("Information"), _("Cannot update service status until it has been saved!"), INFO_DIALOG);
435 return;
436 }
438 $action = "";
439 if($method == START_SERVICE){
440 $action = SERVICE_STARTED;
441 }elseif($method== STOP_SERVICE){
442 $action = SERVICE_STOPPED;
443 }elseif($method == RESTART_SERVICE){
444 $action = SERVICE_RESTARTED;
445 }else{
446 msg_dialog::display(_("Error"), _("Cannot update service status!"), ERROR_DIALOG);
447 return;
448 }
450 $caseVars = array("cn","dn");
451 if($service == ALL_SERVICES){
452 foreach($this->plugins as $name => $obj){
453 foreach($caseVars as $var){
454 if(isset($this->$var)){
455 $this->plugins[$name]->$var = $this->$var;
456 }
457 }
459 /* check if services can be restarted */
460 $map =array(SERVICE_STARTED=> "AllowStart" ,
461 SERVICE_STOPPED => "AllowStop",
462 SERVICE_RESTARTED => "AllowRestart");
464 /* get plugins informations, restart/start/stop actions allowed ?*/
465 $tmp = $this->plugins[$name]->getListEntry();
467 /* Check if given action is allowed for this service */
468 if($tmp[$map[$action]] && !empty($this->plugins[$name]->StatusFlag)){
469 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
470 $this->plugins[$name]->setStatus($action);
471 }
472 }
473 }
474 }else{
475 foreach($caseVars as $var){
476 if(isset($this->$var)){
477 $this->plugins[$service]->$var = $this->$var;
478 }
479 }
480 if($this->plugins[$service]->is_account){
481 $this->plugins[$service]->setStatus($action);
482 }
483 }
484 }
487 /*! \brief No checks here.
488 */
489 function check()
490 {
491 return(array());
492 }
495 /*! \brief Keep posted form values in opened dialogs
496 */
497 function save_object()
498 {
499 foreach($this->plugins as $name => $obj){
500 if($obj->is_account){
501 $this->plugins[$name]->save_object();
502 }
503 }
504 }
507 /*! \brief Remove all active services
508 */
509 function remove_from_parent()
510 {
511 $caseVars = array("cn","dn");
512 foreach($this->plugins as $name => $obj){
513 foreach($caseVars as $var){
514 if(isset($this->$var)){
515 $this->plugins[$name]->$var = $this->$var;
516 }
517 }
518 if($this->plugins[$name]->initially_was_account){
519 if(isset($this->plugins[$name]->krb_host_key) && $this->plugins[$name]->krb_host_key instanceof krbHostKeys){
520 $this->plugins[$name]->krb_host_key->remove_from_parent_by_prefix($this->plugins[$name]->krb_service_prefix);
521 }
522 $this->plugins[$name]->remove_from_parent();
523 }
524 }
525 }
528 /*! \brief Save all active services
529 */
530 function save()
531 {
532 $caseVars = array("cn","dn");
533 foreach($this->plugins as $name => $obj){
535 foreach($caseVars as $var){
536 if(isset($this->$var)){
537 $this->plugins[$name]->$var = $this->$var;
538 }
539 }
541 if($this->plugins[$name]->is_account){
542 $this->plugins[$name]->save();
543 }else{
544 if($this->plugins[$name]->initially_was_account){
545 $this->plugins[$name]->remove_from_parent();
546 }
547 }
548 }
549 }
552 /*! \brief Prepare active services to be copied.
553 */
554 function PrepareForCopyPaste($source)
555 {
556 plugin::PrepareForCopyPaste($source);
558 foreach($this->plugins as $name => $plugin){
559 $this->plugins[$name]->PrepareForCopyPaste($source);
560 }
561 }
564 /*! \brief Check if there is an active service which prevents
565 * complete service removal.
566 */
567 function allow_remove()
568 {
569 foreach($this->plugins as $name => $obj){
570 $str = $obj->allow_remove();
571 if(!empty($str)){
572 return($str);
573 }
574 }
575 }
578 /*! \brief Forward plugin acls
579 */
580 function set_acl_base($base)
581 {
582 $this->acl_base = $base;
583 foreach($this->plugins as $name => $obj){
584 $this->plugins[$name]->set_acl_base($base);
585 }
586 }
589 /*! \brief Forward plugin acls
590 */
591 function set_acl_category($category)
592 {
593 $this->acl_category = $category;
594 foreach($this->plugins as $name => $obj){
595 $this->plugins[$name]->set_acl_category($category);
596 }
597 }
598 }
599 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
600 ?>