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);
45 /* Capture all service objectClases, necessary for acl handling */
46 if(isset($this->plugins[$name]->objectclasses)){
47 foreach($this->plugins[$name]->objectclasses as $oc){
48 $this->objectclasses[] = $oc;
49 }
50 }
51 }
52 $this->divList = new divListSystemService($config,$this);
56 }
58 function set_acl_base($base)
59 {
60 plugin::set_acl_base($base);
61 foreach($this->plugins as $name => $obj){
62 $this->plugins[$name]->set_acl_base($base);
63 }
64 }
66 function set_acl_category($category)
67 {
68 plugin::set_acl_category($category);
69 foreach($this->plugins as $name => $obj){
70 $this->plugins[$name]->set_acl_category($category);
71 }
72 }
74 function execute()
75 {
76 /* Variable initialisation */
77 $s_action = "";
78 $s_entry = "";
80 /* Walk through posts and check if there are some introductions for us */
81 $PossiblePosts = array("addNewService" => "",
82 "startServices" => "",
83 "stopServices" => "",
84 "restartServices" => "",
85 "removeServices" => "",
87 "StartSingleService" => "/^StartSingleService_(.*)_[xy]$/",
88 "StopSingleService" => "/^StopSingleService_(.*)_[xy]$/",
89 "RestartSingleService" => "/^RestartSingleService_(.*)_[xy]$/",
90 "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
91 "EditSingleService" => "/^EditSingleService_(.*)_[xy]$/");
94 $once = true;
95 foreach($_POST as $name => $value){
96 foreach($PossiblePosts as $pregCheck => $idPreg) {
97 if(preg_match("/^".$pregCheck."/",$name) && $once){
98 $once = false;
99 $s_action = $pregCheck;
101 if(!empty($idPreg)){
102 $s_entry = preg_replace($idPreg,"\\1",$name);
103 }
104 }
105 }
106 }
109 /* Handle state changes for services */
110 $map = array( "startServices" => array("type" => START_SERVICE , "service" => ALL_SERVICES),
111 "stopServices" => array("type" => STOP_SERVICE , "service" => ALL_SERVICES),
112 "restartServices" => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES),
113 "StartSingleService" => array("type" => START_SERVICE , "service" => $s_entry),
114 "StopSingleService" => array("type" => STOP_SERVICE , "service" => $s_entry),
115 "RestartSingleService" => array("type" => RESTART_SERVICE , "service" => $s_entry));
116 if(isset($map[$s_action])){
117 $type = $map[$s_action]['type'];
118 $service = $map[$s_action]['service'];
119 $this->ServiceStatusUpdate($type,$service);
120 }
123 /* Open service add dialog */
124 if($s_action == "addNewService"){
125 $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
126 }
129 /* Remove service */
130 if($s_action == "RemoveSingleService"){
132 /* Create resetted obj */
133 $new_obj = new $s_entry($this->config,$this->dn);
134 $new_obj -> set_acl_base($this->acl_base);
135 $new_obj -> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
136 $tmp = $new_obj->getListEntry();
138 if($tmp['AllowRemove']){
140 /* Check if we are allowed to remove this service
141 */
142 $str = $this->plugins[$s_entry]->allow_remove();
144 if(empty($str)){
145 $this->plugins[$s_entry] = $new_obj;
146 $this->plugins[$s_entry]->is_account = false;
147 }else{
148 print_red($str);
149 }
150 }
151 }
154 /* Edit a service and make a backup from all attributes,
155 to be able to restore old values after aborting dialog */
156 if($s_action == "EditSingleService"){
157 $this->backup = get_object_vars($this->plugins[$s_entry]);
158 $this->dialog = $this->plugins[$s_entry];
159 $this->current = $s_entry;
160 }
163 /* Abort service add */
164 if(isset($_POST['CancelServiceAdd'])){
165 $this->dialog = NULL;
166 $this->backup = NULL;
167 $this->current = "";
168 }
171 /* Abort dialog
172 Restore vars with values before editing */
173 if(isset($_POST['CancelService'])){
174 if($this->backup == NULL){
175 $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
176 $this->plugins[$this->current]-> set_acl_base($this->acl_base);
177 $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
179 }else{
180 foreach($this->backup as $name => $value){
181 $this->plugins[$this->current]->$name = $value;
182 }
183 }
184 $this->dialog = NULL;
185 $this->backup = NULL;
186 $this->current = "";
187 }
190 /* Abort dialog */
191 if(isset($_POST['SaveService'])){
192 $this->dialog->save_object();
193 $msgs = $this->dialog->check();
194 if(count($msgs)){
195 foreach($msgs as $msg){
196 print_red($msg);
197 }
198 }else{
199 $this->plugins[$this->current] = $this->dialog;
200 $tmp = get_object_vars($this->dialog);
201 foreach($tmp as $name => $value){
202 $this->plugins[$this->current]->$name = $value;
203 }
204 $this->current = "";
205 $this->dialog = NULL;
206 $this->backup = NULL;
207 }
208 }
211 /* Abort dialog */
212 if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
213 $serv = $_POST['ServiceName'];
214 $this->plugins[$serv]->is_account = true;
215 $this->dialog = $this->plugins[$serv];
216 $this->current = $serv;
217 }
220 /* There is currently a subdialog open, display this dialog */
221 if($this->dialog != NULL){
222 $this->dialog->save_object();
223 return($this->dialog->execute());
224 }
227 /* Dispaly services overview */
228 $this->divList->execute();
229 $list = array();
231 foreach($this->plugins as $name => $obj){
232 if($obj->is_account){
233 $list[$name] = $this->plugins[$name]->getListEntry();
234 }
235 }
236 $this->divList -> setEntries($list);
237 return($this->divList->Draw());
238 }
241 /* Get all used services
242 CLASSNAME => _($this->plugins[*]->DisplayName); */
243 function getAllUsedServices()
244 {
245 $ret = array();
246 foreach($this->plugins as $name => $obj){
247 if($obj->is_account){
248 if(isset($obj->DisplayName)){
249 $ret[$name] = $obj->DisplayName;
250 }else{
251 $ret[$name] = $name;
252 }
253 }
254 }
255 return($ret);
256 }
259 /* Get all unused services
260 CLASSNAME => _($this->plugins[*]->DisplayName); */
261 function getAllUnusedServices()
262 {
263 $tmp = $this->getAllUsedServices();
264 $pool_of_ocs =array();
265 foreach($tmp as $name => $value){
266 if(isset($this->plugins[$name]->conflicts)){
267 $pool_of_ocs[]= get_class($this->plugins[$name]);
268 }
269 }
271 $ret = array();
272 foreach($this->plugins as $name => $obj){
274 /* Skip all pluigns that will lead into conflicts */
275 $skip = false;
276 if(isset($obj->conflicts)){
277 foreach($obj->conflicts as $oc){
278 if(in_array_ics($oc,$pool_of_ocs)){
279 $skip = true;
280 }
281 }
282 }
284 /* Only show createable services */
285 if(!$obj->acl_is_createable()){
286 $skip = true;
287 }
289 if(!$skip){
290 if(isset($obj->DisplayName)){
291 $ret[$name] = $obj->DisplayName;
292 }else{
293 $ret[$name] = $name;
294 }
295 }
296 }
297 return($ret);
298 }
301 /* This function sets the status var for each used
302 service && calls an external hook if specified in gosa.conf*/
303 function ServiceStatusUpdate($method , $service)
304 {
305 /* Skip if this is a new server */
306 if($this->dn == "new"){
307 print_red(_("Can't set status while this server is not saved."));
308 return;
309 }
311 $action = "";
312 if($method == START_SERVICE){
313 $action = SERVICE_STARTED;
314 }elseif($method== STOP_SERVICE){
315 $action = SERVICE_STOPPED;
316 }elseif($method == RESTART_SERVICE){
317 $action = SERVICE_RESTARTED;
318 }else{
319 print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
320 return;
321 }
323 $caseVars = array("cn","dn");
324 if($service == ALL_SERVICES){
325 foreach($this->plugins as $name => $obj){
326 foreach($caseVars as $var){
327 if(isset($this->$var)){
328 $this->plugins[$name]->$var = $this->$var;
329 }
330 }
332 /* check if services can be restarted */
333 $map =array(SERVICE_STARTED=> "AllowStart" ,
334 SERVICE_STOPPED => "AllowStop",
335 SERVICE_RESTARTED => "AllowRestart");
337 /* get plugins informations, restart/start/stop actions allowed ?*/
338 $tmp = $this->plugins[$name]->getListEntry();
340 /* Check if given action is allowed for this service */
341 if($tmp[$map[$action]]){
342 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
343 $this->plugins[$name]->setStatus($action);
344 }
345 }
346 }
347 }else{
348 foreach($caseVars as $var){
349 if(isset($this->$var)){
350 $this->plugins[$service]->$var = $this->$var;
351 }
352 }
353 if($this->plugins[$service]->is_account){
354 $this->plugins[$service]->setStatus($action);
355 }
356 }
357 }
360 function check()
361 {
362 $message = plugin::check();
363 return $message;
364 }
367 function save_object()
368 {
369 foreach($this->plugins as $name => $obj){
370 if($obj->is_account){
371 $this->plugins[$name]->save_object();
372 }
373 }
374 }
377 function remove_from_parent()
378 {
379 $caseVars = array("cn","dn");
380 foreach($this->plugins as $name => $obj){
381 foreach($caseVars as $var){
382 if(isset($this->$var)){
383 $this->plugins[$name]->$var = $this->$var;
384 }
385 }
386 if($this->plugins[$name]->initially_was_account){
387 $this->plugins[$name]->remove_from_parent();
388 }
389 }
390 }
393 function save()
394 {
395 $caseVars = array("cn","dn");
396 foreach($this->plugins as $name => $obj){
398 foreach($caseVars as $var){
399 if(isset($this->$var)){
400 $this->plugins[$name]->$var = $this->$var;
401 }
402 }
404 if($this->plugins[$name]->is_account){
405 $this->plugins[$name]->save();
406 }else{
407 if($this->plugins[$name]->initially_was_account){
408 $this->plugins[$name]->remove_from_parent();
409 }
410 }
411 }
412 }
415 /* Check if all plugins allow a remove .. */
416 function allow_remove()
417 {
418 foreach($this->plugins as $name => $obj){
419 $str = $obj->allow_remove();
420 if(!empty($str)){
421 return($str);
422 }
423 }
424 }
426 }
427 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
428 ?>