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 $ldap = $this->config->get_ldap_link();
193 $ldap->cd($this->dn);
194 $ldap->cat($this->dn);
196 if($ldap->count()){
197 $attrs = $ldap->fetch();
198 }else{
199 $attrs = array();
200 }
202 foreach($this->plugins as $name => $obj){
203 if($obj->is_account){
204 $flag = $this->plugins[$name]->StatusFlag;
205 if(isset($attrs[$flag][0])){
206 $this->plugins[$name]->$flag = $attrs[$flag][0];
207 }
208 $list[$name] = $this->plugins[$name]->getListEntry();
209 }
210 }
211 $this->divList -> setEntries($list);
212 return($this->divList->Draw());
213 }
216 /* Get all used services
217 CLASSNAME => _($this->plugins[*]->DisplayName); */
218 function getAllUsedServices()
219 {
220 $ret = array();
221 foreach($this->plugins as $name => $obj){
222 if($obj->is_account){
223 if(isset($obj->DisplayName)){
224 $ret[$name] = $obj->DisplayName;
225 }else{
226 $ret[$name] = $name;
227 }
228 }
229 }
230 return($ret);
231 }
234 /* Get all unused services
235 CLASSNAME => _($this->plugins[*]->DisplayName); */
236 function getAllUnusedServices()
237 {
238 $tmp = $this->getAllUsedServices();
239 $pool_of_ocs =array();
240 foreach($tmp as $name => $value){
241 if(isset($this->plugins[$name]->conflicts)){
242 $pool_of_ocs = array_merge($pool_of_ocs,$this->plugins[$name]->conflicts);
243 }
244 }
246 $ret = array();
247 foreach($this->plugins as $name => $obj){
249 /* Skip all pluigns that will lead into conflicts */
250 $skip = false;
251 if(isset($obj->conflicts)){
252 foreach($obj->conflicts as $oc){
253 if(in_array_ics($oc,$pool_of_ocs)){
254 $skip = true;
255 }
256 }
257 }
258 if(!$skip){
259 if(isset($obj->DisplayName)){
260 $ret[$name] = $obj->DisplayName;
261 }else{
262 $ret[$name] = $name;
263 }
264 }
265 }
266 return($ret);
267 }
270 /* This function sets the status var for each used
271 service && calls an external hook if specified in gosa.conf*/
272 function ServiceStatusUpdate($method , $service)
273 {
274 /* Skip if this is a new server */
275 if($this->dn == "new"){
276 print_red(_("Can't set status while this server is not saved."));
277 return;
278 }
280 $action = "";
281 if($method == START_SERVICE){
282 $action = SERVICE_STARTED;
283 }elseif($method== STOP_SERVICE){
284 $action = SERVICE_STOPPED;
285 }elseif($method == RESTART_SERVICE){
286 $action = SERVICE_RESTARTED;
287 }else{
288 print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
289 return;
290 }
292 $caseVars = array("cn","dn");
293 if($service == ALL_SERVICES){
294 foreach($this->plugins as $name => $obj){
295 foreach($caseVars as $var){
296 if(isset($this->$var)){
297 $this->plugins[$name]->$var = $this->$var;
298 }
299 }
300 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
301 $this->plugins[$name]->setStatus($action);
302 }
303 }
304 }else{
305 foreach($caseVars as $var){
306 if(isset($this->$var)){
307 $this->plugins[$service]->$var = $this->$var;
308 }
309 }
310 if($this->plugins[$service]->is_account){
311 $this->plugins[$service]->setStatus($action);
312 }
313 }
314 }
317 function check()
318 {
319 $message = plugin::check();
320 return $message;
321 }
324 function save_object()
325 {
326 foreach($this->plugins as $name => $obj){
327 if($obj->is_account){
328 $this->plugins[$name]->save_object();
329 }
330 }
331 }
334 function remove_from_parent()
335 {
336 $caseVars = array("cn","dn");
337 foreach($this->plugins as $name => $obj){
338 foreach($caseVars as $var){
339 if(isset($this->$var)){
340 $this->plugins[$name]->$var = $this->$var;
341 }
342 }
343 if($this->plugins[$name]->initially_was_account){
344 $this->plugins[$name]->remove_from_parent();
345 }
346 }
347 }
350 function save()
351 {
352 $caseVars = array("cn","dn");
353 foreach($this->plugins as $name => $obj){
355 foreach($caseVars as $var){
356 if(isset($this->$var)){
357 $this->plugins[$name]->$var = $this->$var;
358 }
359 }
361 if($this->plugins[$name]->is_account){
362 $this->plugins[$name]->save();
363 }else{
364 if($this->plugins[$name]->initially_was_account){
365 $this->plugins[$name]->remove_from_parent();
366 }
367 }
368 }
369 }
371 }
372 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
373 ?>