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 }
264 if(!$skip){
265 if(isset($obj->DisplayName)){
266 $ret[$name] = $obj->DisplayName;
267 }else{
268 $ret[$name] = $name;
269 }
270 }
271 }
272 return($ret);
273 }
276 /* This function sets the status var for each used
277 service && calls an external hook if specified in gosa.conf*/
278 function ServiceStatusUpdate($method , $service)
279 {
280 /* Skip if this is a new server */
281 if($this->dn == "new"){
282 print_red(_("Can't set status while this server is not saved."));
283 return;
284 }
286 $action = "";
287 if($method == START_SERVICE){
288 $action = SERVICE_STARTED;
289 }elseif($method== STOP_SERVICE){
290 $action = SERVICE_STOPPED;
291 }elseif($method == RESTART_SERVICE){
292 $action = SERVICE_RESTARTED;
293 }else{
294 print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
295 return;
296 }
298 $caseVars = array("cn","dn");
299 if($service == ALL_SERVICES){
300 foreach($this->plugins as $name => $obj){
301 foreach($caseVars as $var){
302 if(isset($this->$var)){
303 $this->plugins[$name]->$var = $this->$var;
304 }
305 }
307 /* check if services can be restarted */
308 $map =array(SERVICE_STARTED=> "AllowStart" ,
309 SERVICE_STOPPED => "AllowStop",
310 SERVICE_RESTARTED => "AllowRestart");
312 /* get plugins informations, restart/start/stop actions allowed ?*/
313 $tmp = $this->plugins[$name]->getListEntry();
315 /* Check if given action is allowed for this service */
316 if($tmp[$map[$action]]){
317 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
318 $this->plugins[$name]->setStatus($action);
319 }
320 }
321 }
322 }else{
323 foreach($caseVars as $var){
324 if(isset($this->$var)){
325 $this->plugins[$service]->$var = $this->$var;
326 }
327 }
328 if($this->plugins[$service]->is_account){
329 $this->plugins[$service]->setStatus($action);
330 }
331 }
332 }
335 function check()
336 {
337 $message = plugin::check();
338 return $message;
339 }
342 function save_object()
343 {
344 foreach($this->plugins as $name => $obj){
345 if($obj->is_account){
346 $this->plugins[$name]->save_object();
347 }
348 }
349 }
352 function remove_from_parent()
353 {
354 $caseVars = array("cn","dn");
355 foreach($this->plugins as $name => $obj){
356 foreach($caseVars as $var){
357 if(isset($this->$var)){
358 $this->plugins[$name]->$var = $this->$var;
359 }
360 }
361 if($this->plugins[$name]->initially_was_account){
362 $this->plugins[$name]->remove_from_parent();
363 }
364 }
365 }
368 function save()
369 {
370 $caseVars = array("cn","dn");
371 foreach($this->plugins as $name => $obj){
373 foreach($caseVars as $var){
374 if(isset($this->$var)){
375 $this->plugins[$name]->$var = $this->$var;
376 }
377 }
379 if($this->plugins[$name]->is_account){
380 $this->plugins[$name]->save();
381 }else{
382 if($this->plugins[$name]->initially_was_account){
383 $this->plugins[$name]->remove_from_parent();
384 }
385 }
386 }
387 }
389 }
390 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
391 ?>