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]$/");
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;
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 $this->backup = get_object_vars($this->plugins[$s_entry]);
196 $this->dialog = $this->plugins[$s_entry];
197 $this->current = $s_entry;
198 }
201 /* Abort service add */
202 if(isset($_POST['CancelServiceAdd'])){
203 $this->dialog = FALSE;
204 $this->backup = NULL;
205 $this->current = "";
206 }
209 /* Abort dialog
210 Restore vars with values before editing */
211 if(isset($_POST['CancelService']) && !empty($this->current)){
212 if($this->backup == NULL){
213 $this->plugins[$this->current] = new $this->current($this->config,$this->dn,$this);
214 $this->plugins[$this->current]-> set_acl_base($this->acl_base);
215 $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
217 }else{
218 foreach($this->backup as $name => $value){
219 $this->plugins[$this->current]->$name = $value;
220 }
221 }
222 $this->dialog = FALSE;
223 $this->backup = NULL;
224 $this->current = "";
225 }
228 /* Abort dialog */
229 if(isset($_POST['SaveService']) && is_object($this->dialog)){
230 # $this->dialog->save_object();
231 $msgs = $this->dialog->check();
232 if(count($msgs)){
233 foreach($msgs as $msg){
234 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
235 }
236 }else{
237 $this->plugins[$this->current] = $this->dialog;
238 $tmp = get_object_vars($this->dialog);
239 foreach($tmp as $name => $value){
240 $this->plugins[$this->current]->$name = $value;
241 }
242 $this->current = "";
243 $this->dialog = FALSE;
244 $this->backup = NULL;
245 }
246 }
249 /* Abort dialog */
250 if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
251 $serv = $_POST['ServiceName'];
252 $this->plugins[$serv]->is_account = true;
253 $this->dialog = $this->plugins[$serv];
254 $this->current = $serv;
255 }
258 /* There is currently a subdialog open, display this dialog */
259 if(is_object($this->dialog)){
260 $add ="";
261 if(isset($this->dialog->krb_host_key) && $this->dialog->krb_host_key instanceof krb_host_keys){
262 $add = $this->dialog->krb_host_key->save_object_by_prefix($this->dialog->krb_service_prefix);
263 $add = $this->dialog->krb_host_key->execute_by_prefix($this->dialog->krb_service_prefix,TRUE);
264 }
265 return($add.$this->dialog->execute());
266 }
269 /* Dispaly services overview */
270 $this->divList->execute();
271 $list = array();
273 foreach($this->plugins as $name => $obj){
274 if($obj->is_account){
275 $list[$name] = $this->plugins[$name]->getListEntry();
276 }
277 }
278 $this->divList -> setEntries($list);
279 return("<table style='width:100%;'><tr><td>".$this->divList->Draw()."</td></tr></table>");
280 }
283 /* Get all used services
284 CLASSNAME => _($this->plugins[*]->DisplayName); */
285 function getAllUsedServices()
286 {
287 $ret = array();
288 foreach($this->plugins as $name => $obj){
289 if($obj->is_account){
290 if(isset($obj->DisplayName)){
291 $ret[$name] = $obj->DisplayName;
292 }else{
293 $ret[$name] = $name;
294 }
295 }
296 }
297 return($ret);
298 }
301 /* Get all unused services
302 CLASSNAME => _($this->plugins[*]->DisplayName); */
303 function getAllUnusedServices()
304 {
305 $tmp = $this->getAllUsedServices();
306 $pool_of_ocs =array();
307 foreach($tmp as $name => $value){
308 if(isset($this->plugins[$name]->conflicts)){
309 $pool_of_ocs[]= get_class($this->plugins[$name]);
310 }
311 }
313 $ret = array();
314 foreach($this->plugins as $name => $obj){
316 /* Skip all pluigns that will lead into conflicts */
317 $skip = false;
318 if(isset($obj->conflicts)){
319 foreach($obj->conflicts as $oc){
320 if(in_array_ics($oc,$pool_of_ocs)){
321 $skip = true;
322 }
323 }
324 }
326 /* Only show createable services */
327 if(!$obj->acl_is_createable()){
328 $skip = true;
329 }
331 if(!$skip){
332 if(isset($obj->DisplayName)){
333 $ret[$name] = $obj->DisplayName;
334 }else{
335 $ret[$name] = $name;
336 }
337 }
338 }
339 return($ret);
340 }
343 /* This function sets the status var for each used
344 service && calls an external hook if specified in gosa.conf*/
345 function ServiceStatusUpdate($method , $service)
346 {
347 /* Skip if this is a new server */
348 if($this->dn == "new"){
349 msg_dialog::display(_("Information"), _("Cannot update service status until it has been saved!"), INFO_DIALOG);
350 return;
351 }
353 $action = "";
354 if($method == START_SERVICE){
355 $action = SERVICE_STARTED;
356 }elseif($method== STOP_SERVICE){
357 $action = SERVICE_STOPPED;
358 }elseif($method == RESTART_SERVICE){
359 $action = SERVICE_RESTARTED;
360 }else{
361 msg_dialog::display(_("Error"), _("Cannot update service status!"), ERROR_DIALOG);
362 return;
363 }
365 $caseVars = array("cn","dn");
366 if($service == ALL_SERVICES){
367 foreach($this->plugins as $name => $obj){
368 foreach($caseVars as $var){
369 if(isset($this->$var)){
370 $this->plugins[$name]->$var = $this->$var;
371 }
372 }
374 /* check if services can be restarted */
375 $map =array(SERVICE_STARTED=> "AllowStart" ,
376 SERVICE_STOPPED => "AllowStop",
377 SERVICE_RESTARTED => "AllowRestart");
379 /* get plugins informations, restart/start/stop actions allowed ?*/
380 $tmp = $this->plugins[$name]->getListEntry();
382 /* Check if given action is allowed for this service */
383 if($tmp[$map[$action]]){
384 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
385 $this->plugins[$name]->setStatus($action);
386 }
387 }
388 }
389 }else{
390 foreach($caseVars as $var){
391 if(isset($this->$var)){
392 $this->plugins[$service]->$var = $this->$var;
393 }
394 }
395 if($this->plugins[$service]->is_account){
396 $this->plugins[$service]->setStatus($action);
397 }
398 }
399 }
402 function check()
403 {
404 $message = plugin::check();
405 return $message;
406 }
409 function save_object()
410 {
411 foreach($this->plugins as $name => $obj){
412 if($obj->is_account){
413 $this->plugins[$name]->save_object();
414 }
415 }
416 }
419 function remove_from_parent()
420 {
421 $caseVars = array("cn","dn");
422 foreach($this->plugins as $name => $obj){
423 foreach($caseVars as $var){
424 if(isset($this->$var)){
425 $this->plugins[$name]->$var = $this->$var;
426 }
427 }
428 if($this->plugins[$name]->initially_was_account){
429 if(isset($this->plugins[$name]->krb_host_key) && $this->plugins[$name]->krb_host_key instanceof krb_host_keys){
430 $this->plugins[$name]->krb_host_key->remove_from_parent_by_prefix($this->plugins[$name]->krb_host_key);
431 }
432 $this->plugins[$name]->remove_from_parent();
433 }
434 }
435 }
438 function save()
439 {
440 $caseVars = array("cn","dn");
441 foreach($this->plugins as $name => $obj){
443 foreach($caseVars as $var){
444 if(isset($this->$var)){
445 $this->plugins[$name]->$var = $this->$var;
446 }
447 }
449 if($this->plugins[$name]->is_account){
450 $this->plugins[$name]->save();
451 }else{
452 if($this->plugins[$name]->initially_was_account){
453 $this->plugins[$name]->remove_from_parent();
454 }
455 }
456 }
457 }
460 function PrepareForCopyPaste($source)
461 {
462 plugin::PrepareForCopyPaste($source);
464 foreach($this->plugins as $name => $plugin){
465 $this->plugins[$name]->PrepareForCopyPaste($source);
466 }
467 }
470 /* Check if all plugins allow a remove .. */
471 function allow_remove()
472 {
473 foreach($this->plugins as $name => $obj){
474 $str = $obj->allow_remove();
475 if(!empty($str)){
476 return($str);
477 }
478 }
479 }
481 }
482 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
483 ?>