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;
50 function ServerService (&$config, $dn)
51 {
52 plugin::plugin($config);
53 $this->dn= $dn;
54 foreach ($config->data['TABS']['SERVERSERVICE'] as $plug){
56 if(class_available($plug['CLASS'])){
58 $name= $plug['CLASS'];
59 $this->plugin_names[]= $name;
60 $this->plugins[$name]= new $name($config, $dn);
62 /* Capture all service objectClases, necessary for acl handling */
63 if(isset($this->plugins[$name]->objectclasses)){
64 foreach($this->plugins[$name]->objectclasses as $oc){
65 $this->objectclasses[] = $oc;
66 }
67 }
68 }else{
69 trigger_error("Service class missing: ".$plug['CLASS']);
70 }
71 }
72 $this->divList = new divListSystemService($config,$this);
73 }
75 function set_acl_base($base)
76 {
77 plugin::set_acl_base($base);
78 foreach($this->plugins as $name => $obj){
79 $this->plugins[$name]->set_acl_base($base);
80 }
81 }
83 function set_acl_category($category)
84 {
85 plugin::set_acl_category($category);
86 foreach($this->plugins as $name => $obj){
87 $this->plugins[$name]->set_acl_category($category);
88 }
89 }
91 function execute()
92 {
93 /* Variable initialisation */
94 $s_action = "";
95 $s_entry = "";
97 /* Walk through posts and check if there are some introductions for us */
98 $PossiblePosts = array("addNewService" => "",
99 "startServices" => "",
100 "stopServices" => "",
101 "restartServices" => "",
102 "removeServices" => "",
104 "StartSingleService" => "/^StartSingleService_(.*)_[xy]$/",
105 "StopSingleService" => "/^StopSingleService_(.*)_[xy]$/",
106 "RestartSingleService" => "/^RestartSingleService_(.*)_[xy]$/",
107 "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
108 "EditSingleService" => "/^EditSingleService_(.*)_[xy]$/");
111 $once = true;
112 foreach($_POST as $name => $value){
113 foreach($PossiblePosts as $pregCheck => $idPreg) {
114 if(preg_match("/^".$pregCheck."/",$name) && $once){
115 $once = false;
116 $s_action = $pregCheck;
118 if(!empty($idPreg)){
119 $s_entry = preg_replace($idPreg,"\\1",$name);
120 }
121 }
122 }
123 }
126 /* Handle state changes for services */
127 $map = array( "startServices" => array("type" => START_SERVICE , "service" => ALL_SERVICES),
128 "stopServices" => array("type" => STOP_SERVICE , "service" => ALL_SERVICES),
129 "restartServices" => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES),
130 "StartSingleService" => array("type" => START_SERVICE , "service" => $s_entry),
131 "StopSingleService" => array("type" => STOP_SERVICE , "service" => $s_entry),
132 "RestartSingleService" => array("type" => RESTART_SERVICE , "service" => $s_entry));
133 if(isset($map[$s_action])){
134 $type = $map[$s_action]['type'];
135 $service = $map[$s_action]['service'];
136 $this->ServiceStatusUpdate($type,$service);
137 }
139 /* Handle actions linked via href */
140 if(isset($_GET['act']) && $_GET['act'] == "open" && isset($_GET['id'])){
141 $id = $_GET['id'];
142 if(isset($this->plugins[$id])){
143 $s_entry = $id;
144 $s_action = "EditSingleService";
145 }
146 }
148 /* Open service add dialog */
149 if($s_action == "addNewService"){
150 $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
151 }
154 /* Remove service */
155 if($s_action == "RemoveSingleService"){
157 /* Create resetted obj */
158 $new_obj = new $s_entry($this->config,$this->dn);
159 $new_obj -> set_acl_base($this->acl_base);
160 $new_obj -> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
161 $tmp = $new_obj->getListEntry();
163 if($tmp['AllowRemove']){
165 /* Check if we are allowed to remove this service
166 */
167 $str = $this->plugins[$s_entry]->allow_remove();
169 if(empty($str)){
170 $this->plugins[$s_entry] = $new_obj;
171 $this->plugins[$s_entry]->is_account = false;
172 }else{
173 print_red($str);
174 }
175 }
176 }
179 /* Edit a service and make a backup from all attributes,
180 to be able to restore old values after aborting dialog */
181 if($s_action == "EditSingleService"){
182 $this->backup = get_object_vars($this->plugins[$s_entry]);
183 $this->dialog = $this->plugins[$s_entry];
184 $this->current = $s_entry;
185 }
188 /* Abort service add */
189 if(isset($_POST['CancelServiceAdd'])){
190 $this->dialog = FALSE;
191 $this->backup = NULL;
192 $this->current = "";
193 }
196 /* Abort dialog
197 Restore vars with values before editing */
198 if(isset($_POST['CancelService']) && !empty($this->current)){
199 if($this->backup == NULL){
200 $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
201 $this->plugins[$this->current]-> set_acl_base($this->acl_base);
202 $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
204 }else{
205 foreach($this->backup as $name => $value){
206 $this->plugins[$this->current]->$name = $value;
207 }
208 }
209 $this->dialog = FALSE;
210 $this->backup = NULL;
211 $this->current = "";
212 }
215 /* Abort dialog */
216 if(isset($_POST['SaveService']) && is_object($this->dialog)){
217 $this->dialog->save_object();
218 $msgs = $this->dialog->check();
219 if(count($msgs)){
220 foreach($msgs as $msg){
221 print_red($msg);
222 }
223 }else{
224 $this->plugins[$this->current] = $this->dialog;
225 $tmp = get_object_vars($this->dialog);
226 foreach($tmp as $name => $value){
227 $this->plugins[$this->current]->$name = $value;
228 }
229 $this->current = "";
230 $this->dialog = FALSE;
231 $this->backup = NULL;
232 }
233 }
236 /* Abort dialog */
237 if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
238 $serv = $_POST['ServiceName'];
239 $this->plugins[$serv]->is_account = true;
240 $this->dialog = $this->plugins[$serv];
241 $this->current = $serv;
242 }
245 /* There is currently a subdialog open, display this dialog */
246 if(is_object($this->dialog)){
247 $this->dialog->save_object();
248 return($this->dialog->execute());
249 }
252 /* Dispaly services overview */
253 $this->divList->execute();
254 $list = array();
256 foreach($this->plugins as $name => $obj){
257 if($obj->is_account){
258 $list[$name] = $this->plugins[$name]->getListEntry();
259 }
260 }
261 $this->divList -> setEntries($list);
262 return("<table style='width:100%;'><tr><td>".$this->divList->Draw()."</td></tr></table>");
263 }
266 /* Get all used services
267 CLASSNAME => _($this->plugins[*]->DisplayName); */
268 function getAllUsedServices()
269 {
270 $ret = array();
271 foreach($this->plugins as $name => $obj){
272 if($obj->is_account){
273 if(isset($obj->DisplayName)){
274 $ret[$name] = $obj->DisplayName;
275 }else{
276 $ret[$name] = $name;
277 }
278 }
279 }
280 return($ret);
281 }
284 /* Get all unused services
285 CLASSNAME => _($this->plugins[*]->DisplayName); */
286 function getAllUnusedServices()
287 {
288 $tmp = $this->getAllUsedServices();
289 $pool_of_ocs =array();
290 foreach($tmp as $name => $value){
291 if(isset($this->plugins[$name]->conflicts)){
292 $pool_of_ocs[]= get_class($this->plugins[$name]);
293 }
294 }
296 $ret = array();
297 foreach($this->plugins as $name => $obj){
299 /* Skip all pluigns that will lead into conflicts */
300 $skip = false;
301 if(isset($obj->conflicts)){
302 foreach($obj->conflicts as $oc){
303 if(in_array_ics($oc,$pool_of_ocs)){
304 $skip = true;
305 }
306 }
307 }
309 /* Only show createable services */
310 if(!$obj->acl_is_createable()){
311 $skip = true;
312 }
314 if(!$skip){
315 if(isset($obj->DisplayName)){
316 $ret[$name] = $obj->DisplayName;
317 }else{
318 $ret[$name] = $name;
319 }
320 }
321 }
322 return($ret);
323 }
326 /* This function sets the status var for each used
327 service && calls an external hook if specified in gosa.conf*/
328 function ServiceStatusUpdate($method , $service)
329 {
330 /* Skip if this is a new server */
331 if($this->dn == "new"){
332 print_red(_("Can't set status while this server is not saved."));
333 return;
334 }
336 $action = "";
337 if($method == START_SERVICE){
338 $action = SERVICE_STARTED;
339 }elseif($method== STOP_SERVICE){
340 $action = SERVICE_STOPPED;
341 }elseif($method == RESTART_SERVICE){
342 $action = SERVICE_RESTARTED;
343 }else{
344 print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
345 return;
346 }
348 $caseVars = array("cn","dn");
349 if($service == ALL_SERVICES){
350 foreach($this->plugins as $name => $obj){
351 foreach($caseVars as $var){
352 if(isset($this->$var)){
353 $this->plugins[$name]->$var = $this->$var;
354 }
355 }
357 /* check if services can be restarted */
358 $map =array(SERVICE_STARTED=> "AllowStart" ,
359 SERVICE_STOPPED => "AllowStop",
360 SERVICE_RESTARTED => "AllowRestart");
362 /* get plugins informations, restart/start/stop actions allowed ?*/
363 $tmp = $this->plugins[$name]->getListEntry();
365 /* Check if given action is allowed for this service */
366 if($tmp[$map[$action]]){
367 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
368 $this->plugins[$name]->setStatus($action);
369 }
370 }
371 }
372 }else{
373 foreach($caseVars as $var){
374 if(isset($this->$var)){
375 $this->plugins[$service]->$var = $this->$var;
376 }
377 }
378 if($this->plugins[$service]->is_account){
379 $this->plugins[$service]->setStatus($action);
380 }
381 }
382 }
385 function check()
386 {
387 $message = plugin::check();
388 return $message;
389 }
392 function save_object()
393 {
394 foreach($this->plugins as $name => $obj){
395 if($obj->is_account){
396 $this->plugins[$name]->save_object();
397 }
398 }
399 }
402 function remove_from_parent()
403 {
404 $caseVars = array("cn","dn");
405 foreach($this->plugins as $name => $obj){
406 foreach($caseVars as $var){
407 if(isset($this->$var)){
408 $this->plugins[$name]->$var = $this->$var;
409 }
410 }
411 if($this->plugins[$name]->initially_was_account){
412 $this->plugins[$name]->remove_from_parent();
413 }
414 }
415 }
418 function save()
419 {
420 $caseVars = array("cn","dn");
421 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 }
429 if($this->plugins[$name]->is_account){
430 $this->plugins[$name]->save();
431 }else{
432 if($this->plugins[$name]->initially_was_account){
433 $this->plugins[$name]->remove_from_parent();
434 }
435 }
436 }
437 }
440 function PrepareForCopyPaste($source)
441 {
442 plugin::PrepareForCopyPaste($source);
444 foreach($this->plugins as $name => $plugin){
445 $this->plugins[$name]->PrepareForCopyPaste($source);
446 }
447 }
450 /* Check if all plugins allow a remove .. */
451 function allow_remove()
452 {
453 foreach($this->plugins as $name => $obj){
454 $str = $obj->allow_remove();
455 if(!empty($str)){
456 return($str);
457 }
458 }
459 }
461 }
462 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
463 ?>