Code

Headers, msgPools
[gosa.git] / gosa-plugins / systems / admin / systems / services / class_goService.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id: class_plugin.inc 9256 2008-03-03 16:09:13Z cajus $$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class goService extends plugin{
24         
25   /* This plugin only writes its objectClass */
26   var $objectclasses    = array();
27   var $attributes       = array();
28   var $StatusFlag       = "";
29  
30   /* This class can't be assigned twice so it conflicts with itsself */
31   var $conflicts            = array();
32   var $dn                   = NULL;
33   var $cn                   = "";
34   var $DisplayName          = "";
35   var $view_logged  =FALSE;
37    
38   /* Construcktion */ 
39   function goService(&$config,$dn)
40   {
41     plugin::plugin($config,$dn);
42     $this->DisplayName = _("Empty service");
43   }
45   
46   /* Create content */
47   function execute()
48   {
49     if($this->is_account && !$this->view_logged){
50       $this->view_logged = TRUE;
51       new log("view","server/".get_class($this),$this->dn);
52     }
54     $str ="<div style='width:100%; text-align:right;'>".
55           "  <input type='submit' name='SaveService' value='"._("Save")."'>&nbsp;".
56           "  <input type='submit' name='CancelService' value='"._("Cancel")."'>".
57           "</div>";
58     return($str);
59   }
62   /* Get service information for serverService plugin */
63   function getListEntry()
64   {
65     
66     $this->updateStatusState();
68     /* Assign status flag */
69     if(!empty($this->StatusFlag)){
70       $flag                   = $this->StatusFlag;
71       $fields['Status']       = $this->$flag;
72     }else{
73       $fields['Status']       = "";
74     }
76     /* Name displayed in service overview */
77     $fields['Message']      = _("Empty service");
79     /* Allow/disallow some functions */
80     $fields['AllowStart']   = $this->acl_is_writeable("start");
81     $fields['AllowStop']    = $this->acl_is_writeable("stop");
82     $fields['AllowRestart'] = $this->acl_is_writeable("restart");
83     $fields['AllowRemove']  = $this->acl_is_removeable();
84     $fields['AllowEdit']    = true;
85     return($fields);
86   }
89   /* Remove service */
90   function remove_from_parent()
91   {
92     if(!$this->initially_was_account || !$this->acl_is_removeable()){
93       return;
94     }
95     
96     plugin::remove_from_parent();
98     /* Remove status flag, it is not a memeber of 
99         this->attributes, so ensure that it is deleted too */
100     if(!empty($this->StatusFlag)){
101       $this->attrs[$this->StatusFlag] = array();
102     }
104     /* Check if this is a new entry ... add/modify */
105     $ldap = $this->config->get_ldap_link();
106     $ldap->cat($this->dn,array("objectClass"));
107     if($ldap->count()){
108       $ldap->cd($this->dn);
109       $ldap->modify($this->attrs);
110     }else{
111       $ldap->cd($this->dn);
112       $ldap->add($this->attrs);
113       
114     }
116     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
118     show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
119     $this->handle_post_events("remove");
120   }
123   /* Save service */
124   function save()
125   {
126     plugin::save();
127     /* Check if this is a new entry ... add/modify */
128     $ldap = $this->config->get_ldap_link();
129     $ldap->cat($this->dn,array("objectClass"));
130     if($ldap->count()){
131       $ldap->cd($this->dn);
132       $ldap->modify($this->attrs);
133     }else{
134       $ldap->cd($this->dn);
135       $ldap->add($this->attrs);
136     }
137     if($this->initially_was_account){
138       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
139       $this->handle_post_events("modify");
140     }else{
141       $this->handle_post_events("add");
142       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
143     }
144     show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
145   }
148   /* Directly save new status flag */
149   function setStatus($value)
150   {
151     if($value == "none") return;
153     /* Can't set status flag for new services (Object doesn't exists in ldap tree) */
154     if(!$this->initially_was_account) return;
156     /* Can't set status flag, if no flag is specified  */
157     if(empty($this->StatusFlag)){
158       return;
159     }
161     /* Get object (server), update status flag and save changes */
162     $ldap = $this->config->get_ldap_link();
163     $ldap->cd($this->dn);
164     $ldap->cat($this->dn,array("objectClass"));
165     if($ldap->count()){
167       $tmp = $ldap->fetch();
168       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
169         $attrs['objectClass'][] = $tmp['objectClass'][$i];
170       }
171       $flag = $this->StatusFlag;
172       $attrs[$flag] = $value;
173       $this->$flag = $value;
174       $ldap->modify($attrs);
175       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
176       $this->action_hook();
177     }
178   }
181   function check()
182   { 
183     $message = plugin::check();
184     return($message);
185   }
186   
188   function save_object()
189   {
190     plugin::save_object();
191   }  
193   
194   function action_hook($add_attrs= array())
195   {
196     /* Find postcreate entries for this class */
197     $command= $this->config->search(get_class($this), "ACTION_HOOK",array('menu','tabs'));
198     if ($command != ""){
200       /* Walk through attribute list */
201       foreach ($this->attributes as $attr){
202         if (!is_array($this->$attr)){
203           $command= preg_replace("/%$attr/", $this->$attr, $command);
204         }
205       }
206       $command= preg_replace("/%dn/", $this->dn, $command);
208       /* Additional attributes */
209       foreach ($add_attrs as $name => $value){
210         $command= preg_replace("/%$name/", $value, $command);
211       }
213       /* If there are still some %.. in our command, try to fill these with some other class vars */
214       if(preg_match("/%/",$command)){
215         $attrs = get_object_vars($this);
216         foreach($attrs as $name => $value){
217           if(!is_string($value)) continue;
218           $command= preg_replace("/%$name/", $value, $command);
219         }
220       }
222       if (check_command($command)){
223         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
224             $command, "Execute");
226         exec($command);
227       } else {
228         msg_dialog::display(_("Configuration error"), msgPool::cmdnotfound("ACTION_HOOK", get_class($this)), ERROR_DIALOG);
229       }
230     }
231   }
234   /* Get updates for status flag */
235   function updateStatusState()
236   {
237     if(empty($this->StatusFlag)) return;
239     $attrs = array();
240     $flag = $this->StatusFlag;
241     $ldap = $this->config->get_ldap_link();
242     $ldap->cd($this->cn);
243     $ldap->cat($this->dn,array($flag));
244     if($ldap->count()){
245       $attrs = $ldap->fetch();
246     }
247     if(isset($attrs[$flag][0])){
248       $this->$flag = $attrs[$flag][0];
249     }
250   }
252 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
253 ?>