1 <?php
3 define("START_SERVICE",1);
4 define("STOP_SERVICE",2);
5 define("RESTART_SERVICE",3);
7 define("SERVICE_STOPPED",_("Stop"));
8 define("SERVICE_STARTED",_("Start"));
9 define("SERVICE_RESTARTED",_("Restart"));
11 define("ALL_SERVICES",100);
13 class ServerService extends plugin
14 {
15 /* CLI vars */
16 var $cli_summary = "Manage server services";
17 var $cli_description = "Managing services by adding,removing and configuring services. Allows to start/stop used services.";
18 var $cli_parameters = array("config"=>"Config object" , "dn"=>"Object dn");
20 /* attribute list for save action */
21 var $ignore_account = TRUE;
22 var $attributes = array();
23 var $objectclasses = array();
25 var $divList = NULL;
26 var $dialog = NULL; // Contains dialog object if a dialog is opened
28 var $plugins = array();
29 var $pluign_names = array();
31 var $current = "";
32 var $backup = NULL;
33 var $acl ;
34 var $cn;
36 function ServerService ($config, $dn)
37 {
38 plugin::plugin($config);
39 $this->dn= $dn;
40 foreach ($config->data['TABS']['SERVERSERVICE'] as $plug){
41 $name= $plug['CLASS'];
42 $this->plugin_names[]= $name;
43 $this->plugins[$name]= new $name($config, $dn);
44 }
45 $this->divList = new divListSystemService($config,$this);
46 }
48 function set_acl_base($base)
49 {
50 plugin::set_acl_base($base);
51 foreach($this->plugins as $name => $obj){
52 $this->plugins[$name]->set_acl_base($base);
53 }
54 }
56 function set_acl_category($category)
57 {
58 plugin::set_acl_category($category);
59 foreach($this->plugins as $name => $obj){
60 $this->plugins[$name]->set_acl_category($category);
61 }
62 }
64 function execute()
65 {
66 /* Variable initialisation */
67 $s_action = "";
68 $s_entry = "";
70 /* Walk through posts and check if there are some introductions for us */
71 $PossiblePosts = array("addNewService" => "",
72 "startServices" => "",
73 "stopServices" => "",
74 "restartServices" => "",
75 "removeServices" => "",
77 "StartSingleService" => "/^StartSingleService_(.*)_[xy]$/",
78 "StopSingleService" => "/^StopSingleService_(.*)_[xy]$/",
79 "RestartSingleService" => "/^RestartSingleService_(.*)_[xy]$/",
80 "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
81 "EditSingleService" => "/^EditSingleService_(.*)_[xy]$/");
84 $once = true;
85 foreach($_POST as $name => $value){
86 foreach($PossiblePosts as $pregCheck => $idPreg) {
87 if(preg_match("/^".$pregCheck."/",$name) && $once){
88 $once = false;
89 $s_action = $pregCheck;
91 if(!empty($idPreg)){
92 $s_entry = preg_replace($idPreg,"\\1",$name);
93 }
94 }
95 }
96 }
99 /* Handle state changes for services */
100 $map = array( "startServices" => array("type" => START_SERVICE , "service" => ALL_SERVICES),
101 "stopServices" => array("type" => STOP_SERVICE , "service" => ALL_SERVICES),
102 "restartServices" => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES),
103 "StartSingleService" => array("type" => START_SERVICE , "service" => $s_entry),
104 "StopSingleService" => array("type" => STOP_SERVICE , "service" => $s_entry),
105 "RestartSingleService" => array("type" => RESTART_SERVICE , "service" => $s_entry));
106 if(isset($map[$s_action])){
107 $type = $map[$s_action]['type'];
108 $service = $map[$s_action]['service'];
109 $this->ServiceStatusUpdate($type,$service);
110 }
113 /* Open service add dialog */
114 if($s_action == "addNewService"){
115 $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
116 }
119 /* Remove service */
120 if($s_action == "RemoveSingleService"){
122 /* Create resetted obj */
123 $new_obj = new $s_entry($this->config,$this->dn);
124 $new_obj -> set_acl_base($this->acl_base);
125 $new_obj -> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
126 $tmp = $new_obj->getListEntry();
128 if($tmp['AllowRemove']){
130 /* Check if we are allowed to remove this service
131 */
132 $str = $this->plugins[$s_entry]->allow_remove();
134 if(empty($str)){
135 $this->plugins[$s_entry] = $new_obj;
136 $this->plugins[$s_entry]->is_account = false;
137 }else{
138 print_red($str);
139 }
140 }
141 }
144 /* Edit a service and make a backup from all attributes,
145 to be able to restore old values after aborting dialog */
146 if($s_action == "EditSingleService"){
147 $this->backup = get_object_vars($this->plugins[$s_entry]);
148 $this->dialog = $this->plugins[$s_entry];
149 $this->current = $s_entry;
150 }
153 /* Abort service add */
154 if(isset($_POST['CancelServiceAdd'])){
155 $this->dialog = NULL;
156 $this->backup = NULL;
157 $this->current = "";
158 }
161 /* Abort dialog
162 Restore vars with values before editing */
163 if(isset($_POST['CancelService'])){
164 if($this->backup == NULL){
165 $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
166 $this->plugins[$this->current]-> set_acl_base($this->acl_base);
167 $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
169 }else{
170 foreach($this->backup as $name => $value){
171 $this->plugins[$this->current]->$name = $value;
172 }
173 }
174 $this->dialog = NULL;
175 $this->backup = NULL;
176 $this->current = "";
177 }
180 /* Abort dialog */
181 if(isset($_POST['SaveService'])){
182 $this->dialog->save_object();
183 $msgs = $this->dialog->check();
184 if(count($msgs)){
185 foreach($msgs as $msg){
186 print_red($msg);
187 }
188 }else{
189 $this->plugins[$this->current] = $this->dialog;
190 $tmp = get_object_vars($this->dialog);
191 foreach($tmp as $name => $value){
192 $this->plugins[$this->current]->$name = $value;
193 }
194 $this->current = "";
195 $this->dialog = NULL;
196 $this->backup = NULL;
197 }
198 }
201 /* Abort dialog */
202 if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
203 $serv = $_POST['ServiceName'];
204 $this->plugins[$serv]->is_account = true;
205 $this->dialog = $this->plugins[$serv];
206 $this->current = $serv;
207 }
210 /* There is currently a subdialog open, display this dialog */
211 if($this->dialog != NULL){
212 $this->dialog->save_object();
213 return($this->dialog->execute());
214 }
217 /* Dispaly services overview */
218 $this->divList->execute();
219 $list = array();
221 foreach($this->plugins as $name => $obj){
222 if($obj->is_account){
223 $list[$name] = $this->plugins[$name]->getListEntry();
224 }
225 }
226 $this->divList -> setEntries($list);
227 return($this->divList->Draw());
228 }
231 /* Get all used services
232 CLASSNAME => _($this->plugins[*]->DisplayName); */
233 function getAllUsedServices()
234 {
235 $ret = array();
236 foreach($this->plugins as $name => $obj){
237 if($obj->is_account){
238 if(isset($obj->DisplayName)){
239 $ret[$name] = $obj->DisplayName;
240 }else{
241 $ret[$name] = $name;
242 }
243 }
244 }
245 return($ret);
246 }
249 /* Get all unused services
250 CLASSNAME => _($this->plugins[*]->DisplayName); */
251 function getAllUnusedServices()
252 {
253 $tmp = $this->getAllUsedServices();
254 $pool_of_ocs =array();
255 foreach($tmp as $name => $value){
256 if(isset($this->plugins[$name]->conflicts)){
257 $pool_of_ocs[]= get_class($this->plugins[$name]);
258 }
259 }
261 $ret = array();
262 foreach($this->plugins as $name => $obj){
264 /* Skip all pluigns that will lead into conflicts */
265 $skip = false;
266 if(isset($obj->conflicts)){
267 foreach($obj->conflicts as $oc){
268 if(in_array_ics($oc,$pool_of_ocs)){
269 $skip = true;
270 }
271 }
272 }
274 /* Only show createable services */
275 if(!$obj->acl_is_createable()){
276 $skip = true;
277 }
279 if(!$skip){
280 if(isset($obj->DisplayName)){
281 $ret[$name] = $obj->DisplayName;
282 }else{
283 $ret[$name] = $name;
284 }
285 }
286 }
287 return($ret);
288 }
291 /* This function sets the status var for each used
292 service && calls an external hook if specified in gosa.conf*/
293 function ServiceStatusUpdate($method , $service)
294 {
295 /* Skip if this is a new server */
296 if($this->dn == "new"){
297 print_red(_("Can't set status while this server is not saved."));
298 return;
299 }
301 $action = "";
302 if($method == START_SERVICE){
303 $action = SERVICE_STARTED;
304 }elseif($method== STOP_SERVICE){
305 $action = SERVICE_STOPPED;
306 }elseif($method == RESTART_SERVICE){
307 $action = SERVICE_RESTARTED;
308 }else{
309 print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
310 return;
311 }
313 $caseVars = array("cn","dn");
314 if($service == ALL_SERVICES){
315 foreach($this->plugins as $name => $obj){
316 foreach($caseVars as $var){
317 if(isset($this->$var)){
318 $this->plugins[$name]->$var = $this->$var;
319 }
320 }
322 /* check if services can be restarted */
323 $map =array(SERVICE_STARTED=> "AllowStart" ,
324 SERVICE_STOPPED => "AllowStop",
325 SERVICE_RESTARTED => "AllowRestart");
327 /* get plugins informations, restart/start/stop actions allowed ?*/
328 $tmp = $this->plugins[$name]->getListEntry();
330 /* Check if given action is allowed for this service */
331 if($tmp[$map[$action]]){
332 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
333 $this->plugins[$name]->setStatus($action);
334 }
335 }
336 }
337 }else{
338 foreach($caseVars as $var){
339 if(isset($this->$var)){
340 $this->plugins[$service]->$var = $this->$var;
341 }
342 }
343 if($this->plugins[$service]->is_account){
344 $this->plugins[$service]->setStatus($action);
345 }
346 }
347 }
350 function check()
351 {
352 $message = plugin::check();
353 return $message;
354 }
357 function save_object()
358 {
359 foreach($this->plugins as $name => $obj){
360 if($obj->is_account){
361 $this->plugins[$name]->save_object();
362 }
363 }
364 }
367 function remove_from_parent()
368 {
369 $caseVars = array("cn","dn");
370 foreach($this->plugins as $name => $obj){
371 foreach($caseVars as $var){
372 if(isset($this->$var)){
373 $this->plugins[$name]->$var = $this->$var;
374 }
375 }
376 if($this->plugins[$name]->initially_was_account){
377 $this->plugins[$name]->remove_from_parent();
378 }
379 }
380 }
383 function save()
384 {
385 $caseVars = array("cn","dn");
386 foreach($this->plugins as $name => $obj){
388 foreach($caseVars as $var){
389 if(isset($this->$var)){
390 $this->plugins[$name]->$var = $this->$var;
391 }
392 }
394 if($this->plugins[$name]->is_account){
395 $this->plugins[$name]->save();
396 }else{
397 if($this->plugins[$name]->initially_was_account){
398 $this->plugins[$name]->remove_from_parent();
399 }
400 }
401 }
402 }
405 /* Check if all plugins allow a remove .. */
406 function allow_remove()
407 {
408 foreach($this->plugins as $name => $obj){
409 $str = $obj->allow_remove();
410 if(!empty($str)){
411 return($str);
412 }
413 }
414 }
416 }
417 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
418 ?>