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']){
129 $this->plugins[$s_entry] = $new_obj;
130 $this->plugins[$s_entry]->is_account = false;
131 }
132 }
135 /* Edit a service and make a backup from all attributes,
136 to be able to restore old values after aborting dialog */
137 if($s_action == "EditSingleService"){
138 $this->backup = get_object_vars($this->plugins[$s_entry]);
139 $this->dialog = $this->plugins[$s_entry];
140 $this->current = $s_entry;
141 }
144 /* Abort service add */
145 if(isset($_POST['CancelServiceAdd'])){
146 $this->dialog = NULL;
147 $this->backup = NULL;
148 $this->current = "";
149 }
152 /* Abort dialog
153 Restore vars with values before editing */
154 if(isset($_POST['CancelService'])){
155 if($this->backup == NULL){
156 $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
157 $this->plugins[$this->current]-> set_acl_base($this->acl_base);
158 $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
160 }else{
161 foreach($this->backup as $name => $value){
162 $this->plugins[$this->current]->$name = $value;
163 }
164 }
165 $this->dialog = NULL;
166 $this->backup = NULL;
167 $this->current = "";
168 }
171 /* Abort dialog */
172 if(isset($_POST['SaveService'])){
173 $this->dialog->save_object();
174 $msgs = $this->dialog->check();
175 if(count($msgs)){
176 foreach($msgs as $msg){
177 print_red($msg);
178 }
179 }else{
180 $this->plugins[$this->current] = $this->dialog;
181 $tmp = get_object_vars($this->dialog);
182 foreach($tmp as $name => $value){
183 $this->plugins[$this->current]->$name = $value;
184 }
185 $this->current = "";
186 $this->dialog = NULL;
187 $this->backup = NULL;
188 }
189 }
192 /* Abort dialog */
193 if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
194 $serv = $_POST['ServiceName'];
195 $this->plugins[$serv]->is_account = true;
196 $this->dialog = $this->plugins[$serv];
197 $this->current = $serv;
198 }
201 /* There is currently a subdialog open, display this dialog */
202 if($this->dialog != NULL){
203 $this->dialog->save_object();
204 return($this->dialog->execute());
205 }
208 /* Dispaly services overview */
209 $this->divList->execute();
210 $list = array();
212 foreach($this->plugins as $name => $obj){
213 if($obj->is_account){
214 $list[$name] = $this->plugins[$name]->getListEntry();
215 }
216 }
217 $this->divList -> setEntries($list);
218 return($this->divList->Draw());
219 }
222 /* Get all used services
223 CLASSNAME => _($this->plugins[*]->DisplayName); */
224 function getAllUsedServices()
225 {
226 $ret = array();
227 foreach($this->plugins as $name => $obj){
228 if($obj->is_account){
229 if(isset($obj->DisplayName)){
230 $ret[$name] = $obj->DisplayName;
231 }else{
232 $ret[$name] = $name;
233 }
234 }
235 }
236 return($ret);
237 }
240 /* Get all unused services
241 CLASSNAME => _($this->plugins[*]->DisplayName); */
242 function getAllUnusedServices()
243 {
244 $tmp = $this->getAllUsedServices();
245 $pool_of_ocs =array();
246 foreach($tmp as $name => $value){
247 if(isset($this->plugins[$name]->conflicts)){
248 $pool_of_ocs = array_merge($pool_of_ocs,$this->plugins[$name]->conflicts);
249 }
250 }
252 $ret = array();
253 foreach($this->plugins as $name => $obj){
255 /* Skip all pluigns that will lead into conflicts */
256 $skip = false;
257 if(isset($obj->conflicts)){
258 foreach($obj->conflicts as $oc){
259 if(in_array_ics($oc,$pool_of_ocs)){
260 $skip = true;
261 }
262 }
263 }
265 /* Only show createable services */
266 if(!$obj->acl_is_createable()){
267 $skip = true;
268 }
270 if(!$skip){
271 if(isset($obj->DisplayName)){
272 $ret[$name] = $obj->DisplayName;
273 }else{
274 $ret[$name] = $name;
275 }
276 }
277 }
278 return($ret);
279 }
282 /* This function sets the status var for each used
283 service && calls an external hook if specified in gosa.conf*/
284 function ServiceStatusUpdate($method , $service)
285 {
286 /* Skip if this is a new server */
287 if($this->dn == "new"){
288 print_red(_("Can't set status while this server is not saved."));
289 return;
290 }
292 $action = "";
293 if($method == START_SERVICE){
294 $action = SERVICE_STARTED;
295 }elseif($method== STOP_SERVICE){
296 $action = SERVICE_STOPPED;
297 }elseif($method == RESTART_SERVICE){
298 $action = SERVICE_RESTARTED;
299 }else{
300 print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
301 return;
302 }
304 $caseVars = array("cn","dn");
305 if($service == ALL_SERVICES){
306 foreach($this->plugins as $name => $obj){
307 foreach($caseVars as $var){
308 if(isset($this->$var)){
309 $this->plugins[$name]->$var = $this->$var;
310 }
311 }
313 /* check if services can be restarted */
314 $map =array(SERVICE_STARTED=> "AllowStart" ,
315 SERVICE_STOPPED => "AllowStop",
316 SERVICE_RESTARTED => "AllowRestart");
318 /* get plugins informations, restart/start/stop actions allowed ?*/
319 $tmp = $this->plugins[$name]->getListEntry();
321 /* Check if given action is allowed for this service */
322 if($tmp[$map[$action]]){
323 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
324 $this->plugins[$name]->setStatus($action);
325 }
326 }
327 }
328 }else{
329 foreach($caseVars as $var){
330 if(isset($this->$var)){
331 $this->plugins[$service]->$var = $this->$var;
332 }
333 }
334 if($this->plugins[$service]->is_account){
335 $this->plugins[$service]->setStatus($action);
336 }
337 }
338 }
341 function check()
342 {
343 $message = plugin::check();
344 return $message;
345 }
348 function save_object()
349 {
350 foreach($this->plugins as $name => $obj){
351 if($obj->is_account){
352 $this->plugins[$name]->save_object();
353 }
354 }
355 }
358 function remove_from_parent()
359 {
360 $caseVars = array("cn","dn");
361 foreach($this->plugins as $name => $obj){
362 foreach($caseVars as $var){
363 if(isset($this->$var)){
364 $this->plugins[$name]->$var = $this->$var;
365 }
366 }
367 if($this->plugins[$name]->initially_was_account){
368 $this->plugins[$name]->remove_from_parent();
369 }
370 }
371 }
374 function save()
375 {
376 $caseVars = array("cn","dn");
377 foreach($this->plugins as $name => $obj){
379 foreach($caseVars as $var){
380 if(isset($this->$var)){
381 $this->plugins[$name]->$var = $this->$var;
382 }
383 }
385 if($this->plugins[$name]->is_account){
386 $this->plugins[$name]->save();
387 }else{
388 if($this->plugins[$name]->initially_was_account){
389 $this->plugins[$name]->remove_from_parent();
390 }
391 }
392 }
393 }
395 }
396 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
397 ?>