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'])){
152 $this->dialog->save_object();
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 $tmp = get_object_vars($this->dialog);
161 foreach($tmp as $name => $value){
162 $this->plugins[$this->current]->$name = $value;
163 }
164 $this->current = "";
165 $this->dialog = NULL;
166 $this->backup = NULL;
167 }
168 }
171 /* Abort dialog */
172 if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
173 $serv = $_POST['ServiceName'];
174 $this->plugins[$serv]->is_account = true;
175 $this->dialog = $this->plugins[$serv];
176 $this->current = $serv;
177 }
180 /* There is currently a subdialog open, display this dialog */
181 if($this->dialog != NULL){
182 $this->dialog->save_object();
183 return($this->dialog->execute());
184 }
187 /* Dispaly services overview */
188 $this->divList->execute();
189 $list = array();
190 foreach($this->plugins as $name => $obj){
191 if($obj->is_account){
192 $list[$name] = $obj->getListEntry();
193 }
194 }
195 $this->divList -> setEntries($list);
196 return($this->divList->Draw());
197 }
200 /* Get all used services
201 CLASSNAME => _($this->plugins[*]->DisplayName); */
202 function getAllUsedServices()
203 {
204 $ret = array();
205 foreach($this->plugins as $name => $obj){
206 if($obj->is_account){
207 if(isset($obj->DisplayName)){
208 $ret[$name] = $obj->DisplayName;
209 }else{
210 $ret[$name] = $name;
211 }
212 }
213 }
214 return($ret);
215 }
218 /* Get all unused services
219 CLASSNAME => _($this->plugins[*]->DisplayName); */
220 function getAllUnusedServices()
221 {
222 $tmp = $this->getAllUsedServices();
223 $pool_of_ocs =array();
224 foreach($tmp as $name => $value){
225 if(isset($this->plugins[$name]->conflicts)){
226 $pool_of_ocs = array_merge($pool_of_ocs,$this->plugins[$name]->conflicts);
227 }
228 }
230 $ret = array();
231 foreach($this->plugins as $name => $obj){
233 /* Skip all pluigns that will lead into conflicts */
234 $skip = false;
235 if(isset($obj->conflicts)){
236 foreach($obj->conflicts as $oc){
237 if(in_array_ics($oc,$pool_of_ocs)){
238 $skip = true;
239 }
240 }
241 }
242 if(!$skip){
243 if(isset($obj->DisplayName)){
244 $ret[$name] = $obj->DisplayName;
245 }else{
246 $ret[$name] = $name;
247 }
248 }
249 }
250 return($ret);
251 }
254 /* This function sets the status var for each used
255 service && calls an external hook if specified in gosa.conf*/
256 function ServiceStatusUpdate($method , $service)
257 {
258 /* Skip if this is a new server */
259 if($this->dn == "new"){
260 print_red(_("Can't set status while this server is not saved."));
261 return;
262 }
264 $action = "";
265 if($method == START_SERVICE){
266 $action = SERVICE_STARTED;
267 }elseif($method== STOP_SERVICE){
268 $action = SERVICE_STOPPED;
269 }elseif($method == RESTART_SERVICE){
270 $action = SERVICE_RESTARTED;
271 }else{
272 print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
273 return;
274 }
276 if($service == ALL_SERVICES){
277 foreach($this->plugins as $name => $obj){
278 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
279 $this->plugins[$name]->setStatus($action);
280 }
281 }
282 }else{
283 if($this->plugins[$service]->is_account){
284 $this->plugins[$service]->setStatus($action);
285 }
286 }
287 }
290 function check()
291 {
292 $message = plugin::check();
293 return $message;
294 }
297 function save_object()
298 {
299 foreach($this->plugins as $name => $obj){
300 if($obj->is_account){
301 $this->plugins[$name]->save_object();
302 }
303 }
304 }
307 function remove_from_parent()
308 {
309 $caseVars = array("cn","dn");
310 foreach($this->plugins as $name => $obj){
311 foreach($caseVars as $var){
312 if(isset($this->$var)){
313 $this->plugins[$name]->$var = $this->$var;
314 }
315 }
316 if($this->plugins[$name]->initially_was_account){
317 $this->plugins[$name]->remove_from_parent();
318 }
319 }
320 }
323 function save()
324 {
325 $caseVars = array("cn","dn");
326 foreach($this->plugins as $name => $obj){
328 foreach($caseVars as $var){
329 if(isset($this->$var)){
330 $this->plugins[$name]->$var = $this->$var;
331 }
332 }
334 if($this->plugins[$name]->is_account){
335 $this->plugins[$name]->save();
336 }else{
337 if($this->plugins[$name]->initially_was_account){
338 $this->plugins[$name]->remove_from_parent();
339 }
340 }
341 }
342 }
344 }
345 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
346 ?>