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 /* Capture all service objectClases, necessary for acl handling */
71 if(isset($this->plugins[$name]->objectclasses)){
72 foreach($this->plugins[$name]->objectclasses as $oc){
73 $this->objectclasses[] = $oc;
74 }
75 }
76 }else{
77 trigger_error("Service class missing: ".$plug['CLASS']);
78 }
79 }
80 $this->divList = new divListSystemService($config,$this);
81 }
83 function set_acl_base($base)
84 {
85 plugin::set_acl_base($base);
86 foreach($this->plugins as $name => $obj){
87 $this->plugins[$name]->set_acl_base($base);
88 }
89 }
91 function set_acl_category($category)
92 {
93 plugin::set_acl_category($category);
94 foreach($this->plugins as $name => $obj){
95 $this->plugins[$name]->set_acl_category($category);
96 }
97 }
99 function execute()
100 {
101 /* Variable initialisation */
102 $s_action = "";
103 $s_entry = "";
105 /* Walk through posts and check if there are some introductions for us */
106 $PossiblePosts = array("addNewService" => "",
107 "startServices" => "",
108 "stopServices" => "",
109 "restartServices" => "",
110 "removeServices" => "",
112 "StartSingleService" => "/^StartSingleService_(.*)_[xy]$/",
113 "StopSingleService" => "/^StopSingleService_(.*)_[xy]$/",
114 "RestartSingleService" => "/^RestartSingleService_(.*)_[xy]$/",
115 "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
116 "EditSingleService" => "/^EditSingleService_(.*)_[xy]$/");
119 $once = true;
120 foreach($_POST as $name => $value){
121 foreach($PossiblePosts as $pregCheck => $idPreg) {
122 if(preg_match("/^".$pregCheck."/",$name) && $once){
123 $once = false;
124 $s_action = $pregCheck;
126 if(!empty($idPreg)){
127 $s_entry = preg_replace($idPreg,"\\1",$name);
128 }
129 }
130 }
131 }
134 /* Handle state changes for services */
135 $map = array( "startServices" => array("type" => START_SERVICE , "service" => ALL_SERVICES),
136 "stopServices" => array("type" => STOP_SERVICE , "service" => ALL_SERVICES),
137 "restartServices" => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES),
138 "StartSingleService" => array("type" => START_SERVICE , "service" => $s_entry),
139 "StopSingleService" => array("type" => STOP_SERVICE , "service" => $s_entry),
140 "RestartSingleService" => array("type" => RESTART_SERVICE , "service" => $s_entry));
141 if(isset($map[$s_action])){
142 $type = $map[$s_action]['type'];
143 $service = $map[$s_action]['service'];
144 $this->ServiceStatusUpdate($type,$service);
145 }
147 /* Handle actions linked via href */
148 if(isset($_GET['act']) && $_GET['act'] == "open" && isset($_GET['id'])){
149 $id = $_GET['id'];
150 if(isset($this->plugins[$id])){
151 $s_entry = $id;
152 $s_action = "EditSingleService";
153 }
154 }
156 /* Open service add dialog */
157 if($s_action == "addNewService"){
158 $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
159 }
162 /* Remove service */
163 if($s_action == "RemoveSingleService"){
165 /* Create resetted obj */
166 $new_obj = new $s_entry($this->config,$this->dn,&$this);
167 $new_obj -> set_acl_base($this->acl_base);
168 $new_obj -> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
169 $tmp = $new_obj->getListEntry();
171 if($tmp['AllowRemove']){
173 /* Check if we are allowed to remove this service
174 */
175 $str = $this->plugins[$s_entry]->allow_remove();
177 if(empty($str)){
178 $this->plugins[$s_entry] = $new_obj;
179 $this->plugins[$s_entry]->is_account = false;
180 }else{
181 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
182 }
183 }
184 }
187 /* Edit a service and make a backup from all attributes,
188 to be able to restore old values after aborting dialog */
189 if($s_action == "EditSingleService"){
190 $this->backup = get_object_vars($this->plugins[$s_entry]);
191 $this->dialog = $this->plugins[$s_entry];
192 $this->current = $s_entry;
193 }
196 /* Abort service add */
197 if(isset($_POST['CancelServiceAdd'])){
198 $this->dialog = FALSE;
199 $this->backup = NULL;
200 $this->current = "";
201 }
204 /* Abort dialog
205 Restore vars with values before editing */
206 if(isset($_POST['CancelService']) && !empty($this->current)){
207 if($this->backup == NULL){
208 $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
209 $this->plugins[$this->current]-> set_acl_base($this->acl_base);
210 $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
212 }else{
213 foreach($this->backup as $name => $value){
214 $this->plugins[$this->current]->$name = $value;
215 }
216 }
217 $this->dialog = FALSE;
218 $this->backup = NULL;
219 $this->current = "";
220 }
223 /* Abort dialog */
224 if(isset($_POST['SaveService']) && is_object($this->dialog)){
225 # $this->dialog->save_object();
226 $msgs = $this->dialog->check();
227 if(count($msgs)){
228 foreach($msgs as $msg){
229 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
230 }
231 }else{
232 $this->plugins[$this->current] = $this->dialog;
233 $tmp = get_object_vars($this->dialog);
234 foreach($tmp as $name => $value){
235 $this->plugins[$this->current]->$name = $value;
236 }
237 $this->current = "";
238 $this->dialog = FALSE;
239 $this->backup = NULL;
240 }
241 }
244 /* Abort dialog */
245 if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
246 $serv = $_POST['ServiceName'];
247 $this->plugins[$serv]->is_account = true;
248 $this->dialog = $this->plugins[$serv];
249 $this->current = $serv;
250 }
253 /* There is currently a subdialog open, display this dialog */
254 if(is_object($this->dialog)){
255 # $this->dialog->save_object();
256 return($this->dialog->execute());
257 }
260 /* Dispaly services overview */
261 $this->divList->execute();
262 $list = array();
264 foreach($this->plugins as $name => $obj){
265 if($obj->is_account){
266 $list[$name] = $this->plugins[$name]->getListEntry();
267 }
268 }
269 $this->divList -> setEntries($list);
270 return("<table style='width:100%;'><tr><td>".$this->divList->Draw()."</td></tr></table>");
271 }
274 /* Get all used services
275 CLASSNAME => _($this->plugins[*]->DisplayName); */
276 function getAllUsedServices()
277 {
278 $ret = array();
279 foreach($this->plugins as $name => $obj){
280 if($obj->is_account){
281 if(isset($obj->DisplayName)){
282 $ret[$name] = $obj->DisplayName;
283 }else{
284 $ret[$name] = $name;
285 }
286 }
287 }
288 return($ret);
289 }
292 /* Get all unused services
293 CLASSNAME => _($this->plugins[*]->DisplayName); */
294 function getAllUnusedServices()
295 {
296 $tmp = $this->getAllUsedServices();
297 $pool_of_ocs =array();
298 foreach($tmp as $name => $value){
299 if(isset($this->plugins[$name]->conflicts)){
300 $pool_of_ocs[]= get_class($this->plugins[$name]);
301 }
302 }
304 $ret = array();
305 foreach($this->plugins as $name => $obj){
307 /* Skip all pluigns that will lead into conflicts */
308 $skip = false;
309 if(isset($obj->conflicts)){
310 foreach($obj->conflicts as $oc){
311 if(in_array_ics($oc,$pool_of_ocs)){
312 $skip = true;
313 }
314 }
315 }
317 /* Only show createable services */
318 if(!$obj->acl_is_createable()){
319 $skip = true;
320 }
322 if(!$skip){
323 if(isset($obj->DisplayName)){
324 $ret[$name] = $obj->DisplayName;
325 }else{
326 $ret[$name] = $name;
327 }
328 }
329 }
330 return($ret);
331 }
334 /* This function sets the status var for each used
335 service && calls an external hook if specified in gosa.conf*/
336 function ServiceStatusUpdate($method , $service)
337 {
338 /* Skip if this is a new server */
339 if($this->dn == "new"){
340 msg_dialog::display(_("Information"), _("Cannot update service status until it has been saved!"), INFO_DIALOG);
341 return;
342 }
344 $action = "";
345 if($method == START_SERVICE){
346 $action = SERVICE_STARTED;
347 }elseif($method== STOP_SERVICE){
348 $action = SERVICE_STOPPED;
349 }elseif($method == RESTART_SERVICE){
350 $action = SERVICE_RESTARTED;
351 }else{
352 msg_dialog::display(_("Error"), _("Cannot update service status!"), ERROR_DIALOG);
353 return;
354 }
356 $caseVars = array("cn","dn");
357 if($service == ALL_SERVICES){
358 foreach($this->plugins as $name => $obj){
359 foreach($caseVars as $var){
360 if(isset($this->$var)){
361 $this->plugins[$name]->$var = $this->$var;
362 }
363 }
365 /* check if services can be restarted */
366 $map =array(SERVICE_STARTED=> "AllowStart" ,
367 SERVICE_STOPPED => "AllowStop",
368 SERVICE_RESTARTED => "AllowRestart");
370 /* get plugins informations, restart/start/stop actions allowed ?*/
371 $tmp = $this->plugins[$name]->getListEntry();
373 /* Check if given action is allowed for this service */
374 if($tmp[$map[$action]]){
375 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
376 $this->plugins[$name]->setStatus($action);
377 }
378 }
379 }
380 }else{
381 foreach($caseVars as $var){
382 if(isset($this->$var)){
383 $this->plugins[$service]->$var = $this->$var;
384 }
385 }
386 if($this->plugins[$service]->is_account){
387 $this->plugins[$service]->setStatus($action);
388 }
389 }
390 }
393 function check()
394 {
395 $message = plugin::check();
396 return $message;
397 }
400 function save_object()
401 {
402 foreach($this->plugins as $name => $obj){
403 if($obj->is_account){
404 $this->plugins[$name]->save_object();
405 }
406 }
407 }
410 function remove_from_parent()
411 {
412 $caseVars = array("cn","dn");
413 foreach($this->plugins as $name => $obj){
414 foreach($caseVars as $var){
415 if(isset($this->$var)){
416 $this->plugins[$name]->$var = $this->$var;
417 }
418 }
419 if($this->plugins[$name]->initially_was_account){
420 $this->plugins[$name]->remove_from_parent();
421 }
422 }
423 }
426 function save()
427 {
428 $caseVars = array("cn","dn");
429 foreach($this->plugins as $name => $obj){
431 foreach($caseVars as $var){
432 if(isset($this->$var)){
433 $this->plugins[$name]->$var = $this->$var;
434 }
435 }
437 if($this->plugins[$name]->is_account){
438 $this->plugins[$name]->save();
439 }else{
440 if($this->plugins[$name]->initially_was_account){
441 $this->plugins[$name]->remove_from_parent();
442 }
443 }
444 }
445 }
448 function PrepareForCopyPaste($source)
449 {
450 plugin::PrepareForCopyPaste($source);
452 foreach($this->plugins as $name => $plugin){
453 $this->plugins[$name]->PrepareForCopyPaste($source);
454 }
455 }
458 /* Check if all plugins allow a remove .. */
459 function allow_remove()
460 {
461 foreach($this->plugins as $name => $obj){
462 $str = $obj->allow_remove();
463 if(!empty($str)){
464 return($str);
465 }
466 }
467 }
469 }
470 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
471 ?>