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 $ui= get_userinfo();
41 $this->acl= get_permissions ($ui->dn, $ui->subtreeACL);
43 foreach ($config->data['TABS']['SERVERSERVICE'] as $plug){
44 $name= $plug['CLASS'];
45 $this->plugin_names[]= $name;
46 $this->plugins[$name]= new $name($config, $dn);
47 $this->plugins[$name]->acl= get_module_permission($this->acl, "$name", $ui->dn);
48 }
50 $this->divList = new divListSystemService($config,$this);
51 }
54 function execute()
55 {
56 /* Variable initialisation */
57 $s_action = "";
58 $s_entry = "";
60 /* Walk through posts and check if there are some introductions for us */
61 $PossiblePosts = array("addNewService" => "",
62 "startServices" => "",
63 "stopServices" => "",
64 "restartServices" => "",
65 "removeServices" => "",
67 "StartSingleService" => "/^StartSingleService_(.*)_[xy]$/",
68 "StopSingleService" => "/^StopSingleService_(.*)_[xy]$/",
69 "RestartSingleService" => "/^RestartSingleService_(.*)_[xy]$/",
70 "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
71 "EditSingleService" => "/^EditSingleService_(.*)_[xy]$/");
74 $once = true;
75 foreach($_POST as $name => $value){
76 foreach($PossiblePosts as $pregCheck => $idPreg) {
77 if(preg_match("/^".$pregCheck."/",$name) && $once){
78 $once = false;
79 $s_action = $pregCheck;
81 if(!empty($idPreg)){
82 $s_entry = preg_replace($idPreg,"\\1",$name);
83 }
84 }
85 }
86 }
89 /* Handle state changes for services */
90 $map = array( "startServices" => array("type" => START_SERVICE , "service" => ALL_SERVICES),
91 "stopServices" => array("type" => STOP_SERVICE , "service" => ALL_SERVICES),
92 "restartServices" => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES), "StartSingleService" => array("type" => START_SERVICE , "service" => $s_entry),
93 "StopSingleService" => array("type" => STOP_SERVICE , "service" => $s_entry),
94 "RestartSingleService" => array("type" => RESTART_SERVICE , "service" => $s_entry));
95 if(isset($map[$s_action])){
96 $type = $map[$s_action]['type'];
97 $service = $map[$s_action]['service'];
98 $this->ServiceStatusUpdate($type,$service);
99 }
102 /* Open service add dialog */
103 if($s_action == "addNewService"){
104 $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
105 }
108 /* Remove service */
109 if($s_action == "RemoveSingleService"){
110 $this->plugins[$s_entry]->is_account= false;
111 $this->plugins[$s_entry] = NULL;
112 $this->plugins[$s_entry] = new $s_entry($this->config,$this->dn);
113 $this->plugins[$s_entry]->acl = $this->acl;
114 $this->plugins[$s_entry]->is_account = false;
115 }
118 /* Edit a service and make a backup from all attributes,
119 to be able to restore old values after aborting dialog */
120 if($s_action == "EditSingleService"){
121 $this->backup = get_object_vars($this->plugins[$s_entry]);
122 $this->dialog = $this->plugins[$s_entry];
123 $this->current = $s_entry;
124 }
127 /* Abort service add */
128 if(isset($_POST['CancelServiceAdd'])){
129 $this->dialog = NULL;
130 $this->backup = NULL;
131 $this->current = "";
132 }
135 /* Abort dialog
136 Restore vars with values before editing */
137 if(isset($_POST['CancelService'])){
138 if($this->backup == NULL){
139 $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
140 }else{
141 foreach($this->backup as $name => $value){
142 $this->plugins[$this->current]->$name = $value;
143 }
144 }
145 $this->dialog = NULL;
146 $this->backup = NULL;
147 $this->current = "";
148 }
151 /* Abort dialog */
152 if(isset($_POST['SaveService'])){
153 $this->dialog->save_object();
154 $msgs = $this->dialog->check();
155 if(count($msgs)){
156 foreach($msgs as $msg){
157 print_red($msg);
158 }
159 }else{
160 $this->plugins[$this->current] = $this->dialog;
161 $tmp = get_object_vars($this->dialog);
162 foreach($tmp as $name => $value){
163 $this->plugins[$this->current]->$name = $value;
164 }
165 $this->current = "";
166 $this->dialog = NULL;
167 $this->backup = NULL;
168 }
169 }
172 /* Abort dialog */
173 if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
174 $serv = $_POST['ServiceName'];
175 $this->plugins[$serv]->is_account = true;
176 $this->dialog = $this->plugins[$serv];
177 $this->current = $serv;
178 }
181 /* There is currently a subdialog open, display this dialog */
182 if($this->dialog != NULL){
183 $this->dialog->save_object();
184 return($this->dialog->execute());
185 }
188 /* Dispaly services overview */
189 $this->divList->execute();
190 $list = array();
192 foreach($this->plugins as $name => $obj){
193 if($obj->is_account){
194 $list[$name] = $this->plugins[$name]->getListEntry();
195 }
196 }
197 $this->divList -> setEntries($list);
198 return($this->divList->Draw());
199 }
202 /* Get all used services
203 CLASSNAME => _($this->plugins[*]->DisplayName); */
204 function getAllUsedServices()
205 {
206 $ret = array();
207 foreach($this->plugins as $name => $obj){
208 if($obj->is_account){
209 if(isset($obj->DisplayName)){
210 $ret[$name] = $obj->DisplayName;
211 }else{
212 $ret[$name] = $name;
213 }
214 }
215 }
216 return($ret);
217 }
220 /* Get all unused services
221 CLASSNAME => _($this->plugins[*]->DisplayName); */
222 function getAllUnusedServices()
223 {
224 $tmp = $this->getAllUsedServices();
225 $pool_of_ocs =array();
226 foreach($tmp as $name => $value){
227 if(isset($this->plugins[$name]->conflicts)){
228 $pool_of_ocs = array_merge($pool_of_ocs,$this->plugins[$name]->conflicts);
229 }
230 }
232 $ret = array();
233 foreach($this->plugins as $name => $obj){
235 /* Skip all pluigns that will lead into conflicts */
236 $skip = false;
237 if(isset($obj->conflicts)){
238 foreach($obj->conflicts as $oc){
239 if(in_array_ics($oc,$pool_of_ocs)){
240 $skip = true;
241 }
242 }
243 }
244 if(!$skip){
245 if(isset($obj->DisplayName)){
246 $ret[$name] = $obj->DisplayName;
247 }else{
248 $ret[$name] = $name;
249 }
250 }
251 }
252 return($ret);
253 }
256 /* This function sets the status var for each used
257 service && calls an external hook if specified in gosa.conf*/
258 function ServiceStatusUpdate($method , $service)
259 {
260 /* Skip if this is a new server */
261 if($this->dn == "new"){
262 print_red(_("Can't set status while this server is not saved."));
263 return;
264 }
266 $action = "";
267 if($method == START_SERVICE){
268 $action = SERVICE_STARTED;
269 }elseif($method== STOP_SERVICE){
270 $action = SERVICE_STOPPED;
271 }elseif($method == RESTART_SERVICE){
272 $action = SERVICE_RESTARTED;
273 }else{
274 print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
275 return;
276 }
278 $caseVars = array("cn","dn");
279 if($service == ALL_SERVICES){
280 foreach($this->plugins as $name => $obj){
281 foreach($caseVars as $var){
282 if(isset($this->$var)){
283 $this->plugins[$name]->$var = $this->$var;
284 }
285 }
286 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
287 $this->plugins[$name]->setStatus($action);
288 }
289 }
290 }else{
291 foreach($caseVars as $var){
292 if(isset($this->$var)){
293 $this->plugins[$service]->$var = $this->$var;
294 }
295 }
296 if($this->plugins[$service]->is_account){
297 $this->plugins[$service]->setStatus($action);
298 }
299 }
300 }
303 function check()
304 {
305 $message = plugin::check();
306 return $message;
307 }
310 function save_object()
311 {
312 foreach($this->plugins as $name => $obj){
313 if($obj->is_account){
314 $this->plugins[$name]->save_object();
315 }
316 }
317 }
320 function remove_from_parent()
321 {
322 $caseVars = array("cn","dn");
323 foreach($this->plugins as $name => $obj){
324 foreach($caseVars as $var){
325 if(isset($this->$var)){
326 $this->plugins[$name]->$var = $this->$var;
327 }
328 }
329 if($this->plugins[$name]->initially_was_account){
330 $this->plugins[$name]->remove_from_parent();
331 }
332 }
333 }
336 function save()
337 {
338 $caseVars = array("cn","dn");
339 foreach($this->plugins as $name => $obj){
341 foreach($caseVars as $var){
342 if(isset($this->$var)){
343 $this->plugins[$name]->$var = $this->$var;
344 }
345 }
347 if($this->plugins[$name]->is_account){
348 $this->plugins[$name]->save();
349 }else{
350 if($this->plugins[$name]->initially_was_account){
351 $this->plugins[$name]->remove_from_parent();
352 }
353 }
354 }
355 }
357 }
358 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
359 ?>