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();
191 foreach($this->plugins as $name => $obj){
192 if($obj->is_account){
193 $list[$name] = $obj->getListEntry();
194 }
195 }
196 $this->divList -> setEntries($list);
197 return($this->divList->Draw());
198 }
201 /* Get all used services
202 CLASSNAME => _($this->plugins[*]->DisplayName); */
203 function getAllUsedServices()
204 {
205 $ret = array();
206 foreach($this->plugins as $name => $obj){
207 if($obj->is_account){
208 if(isset($obj->DisplayName)){
209 $ret[$name] = $obj->DisplayName;
210 }else{
211 $ret[$name] = $name;
212 }
213 }
214 }
215 return($ret);
216 }
219 /* Get all unused services
220 CLASSNAME => _($this->plugins[*]->DisplayName); */
221 function getAllUnusedServices()
222 {
223 $tmp = $this->getAllUsedServices();
224 $pool_of_ocs =array();
225 foreach($tmp as $name => $value){
226 if(isset($this->plugins[$name]->conflicts)){
227 $pool_of_ocs = array_merge($pool_of_ocs,$this->plugins[$name]->conflicts);
228 }
229 }
231 $ret = array();
232 foreach($this->plugins as $name => $obj){
234 /* Skip all pluigns that will lead into conflicts */
235 $skip = false;
236 if(isset($obj->conflicts)){
237 foreach($obj->conflicts as $oc){
238 if(in_array_ics($oc,$pool_of_ocs)){
239 $skip = true;
240 }
241 }
242 }
243 if(!$skip){
244 if(isset($obj->DisplayName)){
245 $ret[$name] = $obj->DisplayName;
246 }else{
247 $ret[$name] = $name;
248 }
249 }
250 }
251 return($ret);
252 }
255 /* This function sets the status var for each used
256 service && calls an external hook if specified in gosa.conf*/
257 function ServiceStatusUpdate($method , $service)
258 {
259 /* Skip if this is a new server */
260 if($this->dn == "new"){
261 print_red(_("Can't set status while this server is not saved."));
262 return;
263 }
265 $action = "";
266 if($method == START_SERVICE){
267 $action = SERVICE_STARTED;
268 }elseif($method== STOP_SERVICE){
269 $action = SERVICE_STOPPED;
270 }elseif($method == RESTART_SERVICE){
271 $action = SERVICE_RESTARTED;
272 }else{
273 print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
274 return;
275 }
277 if($service == ALL_SERVICES){
278 foreach($this->plugins as $name => $obj){
279 if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
280 $this->plugins[$name]->setStatus($action);
281 }
282 }
283 }else{
284 if($this->plugins[$service]->is_account){
285 $this->plugins[$service]->setStatus($action);
286 }
287 }
288 }
291 function check()
292 {
293 $message = plugin::check();
294 return $message;
295 }
298 function save_object()
299 {
300 foreach($this->plugins as $name => $obj){
301 if($obj->is_account){
302 $this->plugins[$name]->save_object();
303 }
304 }
305 }
308 function remove_from_parent()
309 {
310 $caseVars = array("cn","dn");
311 foreach($this->plugins as $name => $obj){
312 foreach($caseVars as $var){
313 if(isset($this->$var)){
314 $this->plugins[$name]->$var = $this->$var;
315 }
316 }
317 if($this->plugins[$name]->initially_was_account){
318 $this->plugins[$name]->remove_from_parent();
319 }
320 }
321 }
324 function save()
325 {
326 $caseVars = array("cn","dn");
327 foreach($this->plugins as $name => $obj){
329 foreach($caseVars as $var){
330 if(isset($this->$var)){
331 $this->plugins[$name]->$var = $this->$var;
332 }
333 }
335 if($this->plugins[$name]->is_account){
336 $this->plugins[$name]->save();
337 }else{
338 if($this->plugins[$name]->initially_was_account){
339 $this->plugins[$name]->remove_from_parent();
340 }
341 }
342 }
343 }
345 }
346 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
347 ?>