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