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 if(!is_object($this->dialogObject)) return;
193 $this->dialogObject->save_object();
194 $msgs = $this->dialogObject->check();
195 if(count($msgs)){
196 foreach($msgs as $msg){
197 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
198 }
199 }else{
200 $this->plugins[$this->current] = $this->dialogObject;
201 $tmp = get_object_vars($this->dialogObject);
202 foreach($tmp as $name => $value){
203 $this->plugins[$this->current]->$name = $value;
204 }
205 $this->closeDialogs();
206 $this->backup = NULL;
207 }
208 }
211 /*!\brief Close all opened dialogs
212 * And reset "dialog open" flags to display bottom buttons again.
213 */
214 function closeDialogs()
215 {
216 management::closeDialogs();
217 $this->dialog = FALSE;
218 }
221 /*! \brief Remove the selected service(s)
222 */
223 protected function removeService($action="",$target=array(),$all=array())
224 {
225 foreach($target as $s_entry){
226 $new_obj = new $s_entry($this->config,$this->dn, $this);
227 $new_obj->set_acl_base($this->acl_base);
228 $new_obj->set_acl_category(preg_replace("/\/$/","",$this->acl_category));
229 $tmp = $new_obj->getListEntry();
231 /* Check if we are allowed to remove this service
232 */
233 if($tmp['AllowRemove']){
234 $str = $this->plugins[$s_entry]->allow_remove();
235 if(empty($str)){
236 $this->plugins[$s_entry] = $new_obj;
237 $this->plugins[$s_entry]->is_account = false;
238 }else{
239 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
240 }
241 }
242 }
243 }
246 /*! \brief Returns the list of of services, active services.
247 * Used in the filter class for services.
248 * class_filterServerService.inc
249 */
250 static function getServiceList()
251 {
252 return(session::get('ServerService'));
253 }
256 /*! \brief Returns an image for the service status.
257 * Used in the service list class.
258 */
259 static function filterServiceStatus($a,$b,$c,$d)
260 {
261 $img ="";
262 if(isset($d['0'])){
263 switch($d[0]){
264 case 'Restart' : $img = ('images/status_restarting.png'); break;
265 case 'Stop' : $img = ('images/lists/off.png'); break;
266 case 'Start' : $img = ('images/lists/on.png'); break;
267 }
268 }
269 if(!empty($img)){
270 return('<img src=\''.$img.'\'>');
271 }
272 return(" ");
273 }
276 /*! \brief Execute this plugin
277 * Handle actions/events, locking, snapshots, dialogs, tabs,...
278 */
279 function execute()
280 {
281 // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
282 $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
283 session::set('LOCK_VARS_TO_USE',$vars);
285 /* Display the copy & paste dialog, if it is currently open */
286 $ret = $this->copyPasteHandler("",array());
287 if($ret){
288 return($this->getHeader().$ret);
289 }
291 // Update filter
292 $filter = $this->getFilter();
293 if ($filter) {
294 $filter->update();
295 session::global_set(get_class($this)."_filter", $filter);
296 session::set('autocomplete', $filter);
297 }
299 // Handle actions (POSTs and GETs)
300 $str = $this->handleActions($this->detectPostActions());
301 if($str) return($this->getHeader().$str);
303 // Open single dialog objects
304 if(is_object($this->dialogObject)){
305 if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object();
306 if(method_exists($this->dialogObject,'execute')){
308 /* There is currently a subdialog open, display this dialog */
309 $add ="";
310 if(isset($this->dialogObject->krb_host_key) && $this->dialogObject->krb_host_key instanceof krbHostKeys){
311 $add = $this->dialogObject->krb_host_key->save_object_by_prefix($this->dialogObject->krb_service_prefix);
312 $add = $this->dialogObject->krb_host_key->execute_by_prefix($this->dialogObject->krb_service_prefix,TRUE);
313 }
315 $display = $add.$this->dialogObject->execute();
316 $display.= $this->_getTabFooter();
317 return($this->getHeader().$display);
318 }
319 }
321 // Display tab object.
322 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
323 # $this->tabObject->save_object();
324 $display = $this->tabObject->execute();
325 $display.= $this->_getTabFooter();
326 return($this->getHeader().$display);
327 }
329 // Set current restore base for snapshot handling.
330 if(is_object($this->snapHandler)){
331 $bases = array();
332 foreach($this->storagePoints as $sp){
333 $bases[] = $sp.$this->headpage->getBase();
334 }
336 // No bases specified? Try base
337 if(!count($bases)) $bases[] = $this->headpage->getBase();
339 $this->snapHandler->setSnapshotBases($bases);
340 }
343 // Display list
344 session::set('ServerService', $this->plugins);
345 return($this->renderList());
346 }
349 /*! \brief Updates the status for a list of services.
350 */
351 function updateServiceStatus($action, $target,$all)
352 {
353 /* Handle state changes for services */
354 $map = array(
355 "start" => START_SERVICE ,
356 "stop" => STOP_SERVICE ,
357 "restart" => RESTART_SERVICE );
358 foreach($target as $s_entry){
359 if(isset($map[$action])){
360 $type = $map[$action];
361 $service = $s_entry;
362 $this->ServiceStatusUpdate($type,$service);
363 }
364 }
365 }
368 /*! \brief Returns a list of all used services
369 * CLASSNAME => _($this->plugins[*]->DisplayName);
370 */
371 function getAllUsedServices()
372 {
373 $ret = array();
374 foreach($this->plugins as $name => $obj){
375 if($obj->is_account){
376 if(isset($obj->DisplayName)){
377 $ret[$name] = $obj->DisplayName;
378 }else{
379 $ret[$name] = $name;
380 }
381 }
382 }
383 return($ret);
384 }
387 /*! \brief Returns a list of all unused services.
388 */
389 function getAllUnusedServices()
390 {
391 $tmp = $this->getAllUsedServices();
392 $pool_of_ocs =array();
393 foreach($tmp as $name => $value){
394 if(isset($this->plugins[$name]->conflicts)){
395 $pool_of_ocs[]= get_class($this->plugins[$name]);
396 }
397 }
399 $ret = array();
400 foreach($this->plugins as $name => $obj){
402 /* Skip all pluigns that will lead into conflicts */
403 $skip = false;
404 if(isset($obj->conflicts)){
405 foreach($obj->conflicts as $oc){
406 if(in_array_ics($oc,$pool_of_ocs)){
407 $skip = true;
408 }
409 }
410 }
412 if(!$obj->acl_is_createable()){
413 $skip = true;
414 }
416 if(!$skip){
417 if(isset($obj->DisplayName)){
418 $ret[$name] = $obj->DisplayName;
419 }else{
420 $ret[$name] = $name;
421 }
422 }
423 }
424 return($ret);
425 }
428 /*! \brief Updates the status of a service and
429 * calls an external hook if specified in gosa.conf
430 */
431 function ServiceStatusUpdate($method , $service)
432 {
433 /* Skip if this is a new server */
434 if($this->dn == "new"){
435 msg_dialog::display(_("Information"), _("Cannot update service status until it has been saved!"), INFO_DIALOG);
436 return;
437 }
439 $action = "";
440 if($method == START_SERVICE){
441 $action = SERVICE_STARTED;
442 }elseif($method== STOP_SERVICE){
443 $action = SERVICE_STOPPED;
444 }elseif($method == RESTART_SERVICE){
445 $action = SERVICE_RESTARTED;
446 }else{
447 msg_dialog::display(_("Error"), _("Cannot update service status!"), ERROR_DIALOG);
448 return;
449 }
451 $caseVars = array("cn","dn");
452 if($service == ALL_SERVICES){
453 foreach($this->plugins as $name => $obj){
454 foreach($caseVars as $var){
455 if(isset($this->$var)){
456 $this->plugins[$name]->$var = $this->$var;
457 }
458 }
460 /* check if services can be restarted */
461 $map =array(SERVICE_STARTED=> "AllowStart" ,
462 SERVICE_STOPPED => "AllowStop",
463 SERVICE_RESTARTED => "AllowRestart");
465 /* get plugins informations, restart/start/stop actions allowed ?*/
466 $tmp = $this->plugins[$name]->getListEntry();
468 /* Check if given action is allowed for this service */
469 if($tmp[$map[$action]] && !empty($this->plugins[$name]->StatusFlag)){
470 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
471 $this->plugins[$name]->setStatus($action);
472 }
473 }
474 }
475 }else{
476 foreach($caseVars as $var){
477 if(isset($this->$var)){
478 $this->plugins[$service]->$var = $this->$var;
479 }
480 }
481 if($this->plugins[$service]->is_account){
482 $this->plugins[$service]->setStatus($action);
483 }
484 }
485 }
488 /*! \brief No checks here.
489 */
490 function check()
491 {
492 return(array());
493 }
496 /*! \brief Keep posted form values in opened dialogs
497 */
498 function save_object()
499 {
500 foreach($this->plugins as $name => $obj){
501 if($obj->is_account){
502 $this->plugins[$name]->save_object();
503 }
504 }
505 }
508 /*! \brief Remove all active services
509 */
510 function remove_from_parent()
511 {
512 $caseVars = array("cn","dn");
513 foreach($this->plugins as $name => $obj){
514 foreach($caseVars as $var){
515 if(isset($this->$var)){
516 $this->plugins[$name]->$var = $this->$var;
517 }
518 }
519 if($this->plugins[$name]->initially_was_account){
520 if(isset($this->plugins[$name]->krb_host_key) && $this->plugins[$name]->krb_host_key instanceof krbHostKeys){
521 $this->plugins[$name]->krb_host_key->remove_from_parent_by_prefix($this->plugins[$name]->krb_service_prefix);
522 }
523 $this->plugins[$name]->remove_from_parent();
524 }
525 }
526 }
529 /*! \brief Save all active services
530 */
531 function save()
532 {
533 $caseVars = array("cn","dn");
534 foreach($this->plugins as $name => $obj){
536 foreach($caseVars as $var){
537 if(isset($this->$var)){
538 $this->plugins[$name]->$var = $this->$var;
539 }
540 }
542 if($this->plugins[$name]->is_account){
543 $this->plugins[$name]->save();
544 }else{
545 if($this->plugins[$name]->initially_was_account){
546 $this->plugins[$name]->remove_from_parent();
547 }
548 }
549 }
550 }
553 /*! \brief Prepare active services to be copied.
554 */
555 function PrepareForCopyPaste($source)
556 {
557 plugin::PrepareForCopyPaste($source);
559 foreach($this->plugins as $name => $plugin){
560 $this->plugins[$name]->PrepareForCopyPaste($source);
561 }
562 }
565 /*! \brief Check if there is an active service which prevents
566 * complete service removal.
567 */
568 function allow_remove()
569 {
570 foreach($this->plugins as $name => $obj){
571 $str = $obj->allow_remove();
572 if(!empty($str)){
573 return($str);
574 }
575 }
576 }
579 /*! \brief Forward plugin acls
580 */
581 function set_acl_base($base)
582 {
583 $this->acl_base = $base;
584 foreach($this->plugins as $name => $obj){
585 $this->plugins[$name]->set_acl_base($base);
586 }
587 }
590 /*! \brief Forward plugin acls
591 */
592 function set_acl_category($category)
593 {
594 $this->acl_category = $category;
595 foreach($this->plugins as $name => $obj){
596 $this->plugins[$name]->set_acl_category($category);
597 }
598 }
601 function saveCopyDialog(){
602 }
605 function getCopyDialog(){
606 return(array("string"=>"","status"=>""));
607 }
610 }
611 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
612 ?>