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