1 <?php
3 define("START_SERVICE",1);
4 define("STOP_SERVICE",2);
5 define("RESET_SERVICE",3);
7 define("SERVICE_STOPPED","stopped");
8 define("SERVICE_STARTED","started");
9 define("SERVICE_RESETTED","resetted");
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 "resetServices" => "",
64 "removeServices" => "",
66 "StartSingleService" => "/^StartSingleService_(.*)_[xy]$/",
67 "StopSingleService" => "/^StopSingleService_(.*)_[xy]$/",
68 "ResetSingleService" => "/^ResetSingleService_(.*)_[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 "resetServices" => array("type" => RESET_SERVICE , "service" => ALL_SERVICES),
92 "StartSingleService" => array("type" => START_SERVICE , "service" => $s_entry),
93 "StopSingleService" => array("type" => STOP_SERVICE , "service" => $s_entry),
94 "ResetSingleService" => array("type" => RESET_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'])){
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 $this->current = "";
162 $this->dialog = NULL;
163 $this->backup = NULL;
164 }
165 }
168 /* Abort dialog */
169 if(isset($_POST['SaveServiceAdd'])){
170 $serv = $_POST['ServiceName'];
171 $this->plugins[$serv]->is_account = true;
172 $this->dialog = $this->plugins[$serv];
173 $this->current = $serv;
174 }
177 /* There is currently a subdialog open, display this dialog */
178 if($this->dialog != NULL){
179 $this->dialog->save_object();
180 return($this->dialog->execute());
181 }
184 /* Dispaly services overview */
185 $this->divList->execute();
186 $list = array();
187 foreach($this->plugins as $name => $obj){
188 if($obj->is_account){
189 $list[$name] = $obj->getListEntry();
190 }
191 }
192 $this->divList -> setEntries($list);
193 return($this->divList->Draw());
194 }
197 /* Get all used services
198 CLASSNAME => _($this->plugins[*]->DisplayName); */
199 function getAllUsedServices()
200 {
201 $ret = array();
202 foreach($this->plugins as $name => $obj){
203 if($obj->is_account){
204 if(isset($obj->DisplayName)){
205 $ret[$name] = $obj->DisplayName;
206 }else{
207 $ret[$name] = $name;
208 }
209 }
210 }
211 return($ret);
212 }
215 /* Get all unused services
216 CLASSNAME => _($this->plugins[*]->DisplayName); */
217 function getAllUnusedServices()
218 {
219 $tmp = $this->getAllUsedServices();
220 $pool_of_ocs =array();
221 foreach($tmp as $name => $value){
222 if(isset($this->plugins[$name]->conflicts)){
223 $pool_of_ocs = array_merge($pool_of_ocs,$this->plugins[$name]->conflicts);
224 }
225 }
227 $ret = array();
228 foreach($this->plugins as $name => $obj){
230 /* Skip all pluigns that will lead into conflicts */
231 $skip = false;
232 if(isset($obj->conflicts)){
233 foreach($obj->conflicts as $oc){
234 if(in_array_ics($oc,$pool_of_ocs)){
235 $skip = true;
236 }
237 }
238 }
239 if(!$skip){
240 if(isset($obj->DisplayName)){
241 $ret[$name] = $obj->DisplayName;
242 }else{
243 $ret[$name] = $name;
244 }
245 }
246 }
247 return($ret);
248 }
251 /* This function sets the status var for each used
252 service && calls an external hook if specified in gosa.conf*/
253 function ServiceStatusUpdate($method , $service)
254 {
255 /* Skip if this is a new server */
256 if($this->dn == "new"){
257 print_red(_("Can't set status while this server is not saved."));
258 return;
259 }
261 $action = "";
262 if($method == START_SERVICE){
263 $action = SERVICE_STARTED;
264 }elseif($method== STOP_SERVICE){
265 $action = SERVICE_STOPPED;
266 }elseif($method == RESET_SERVICE){
267 $action = SERVICE_RESETTED;
268 }else{
269 print_red(sprintf(_("The specified method '%s' can't executed for services."),$action));
270 return;
271 }
273 if($service == ALL_SERVICES){
274 foreach($this->plugins as $name => $obj){
275 if($this->plugins[$name]->is_account){
276 $this->plugins[$name]->setStatus($action);
277 }
278 }
279 }else{
280 if($this->plugins[$service]->is_account){
281 $this->plugins[$service]->setStatus($action);
282 }
283 }
284 }
287 function check()
288 {
289 $message = plugin::check();
290 return $message;
291 }
294 function save_object()
295 {
296 foreach($this->plugins as $name => $obj){
297 if($obj->is_account){
298 $this->plugins[$name]->save_object();
299 }
300 }
301 }
304 function remove_from_parent()
305 {
306 $caseVars = array("cn","dn");
307 foreach($this->plugins as $name => $obj){
308 foreach($caseVars as $var){
309 if(isset($this->$var)){
310 $this->plugins[$name]->$var = $this->$var;
311 }
312 }
313 if($this->plugins[$name]->initially_was_account){
314 $this->plugins[$name]->remove_from_parent();
315 }
316 }
317 }
320 function save()
321 {
322 $caseVars = array("cn","dn");
323 foreach($this->plugins as $name => $obj){
325 foreach($caseVars as $var){
326 if(isset($this->$var)){
327 $this->plugins[$name]->$var = $this->$var;
328 }
329 }
331 if($this->plugins[$name]->is_account){
332 $this->plugins[$name]->save();
333 }else{
334 if($this->plugins[$name]->initially_was_account){
335 $this->plugins[$name]->remove_from_parent();
336 }
337 }
338 }
339 }
341 }
342 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
343 ?>