1 <?php
3 class goService extends plugin{
5 var $cli_summary = "This plugin is used within the ServerService plugin.";
6 var $cli_description = "Some longer text\nfor help";
7 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9 /* This plugin only writes its objectClass */
10 var $objectclasses = array();
11 var $attributes = array();
12 var $StatusFlag = "";
14 /* This class can't be assigned twice so it conflicts with itsself */
15 var $conflicts = array();
16 var $dn = NULL;
17 var $cn = "";
18 var $DisplayName = "";
19 var $view_logged =FALSE;
22 /* Construcktion */
23 function goService($config,$dn)
24 {
25 plugin::plugin($config,$dn);
26 $this->DisplayName = _("Empty service");
27 }
30 /* Create content */
31 function execute()
32 {
33 if($this->is_account && !$this->view_logged){
34 $this->view_logged = TRUE;
35 new log("view","server/".get_class($this),$this->dn);
36 }
38 $str ="<div style='width:100%; text-align:right;'>".
39 " <input type='submit' name='SaveService' value='"._("Save")."'> ".
40 " <input type='submit' name='CancelService' value='"._("Cancel")."'>".
41 "</div>";
42 return($str);
43 }
46 /* Get service information for serverService plugin */
47 function getListEntry()
48 {
50 $this->updateStatusState();
52 /* Assign status flag */
53 if(!empty($this->StatusFlag)){
54 $flag = $this->StatusFlag;
55 $fields['Status'] = $this->$flag;
56 }else{
57 $fields['Status'] = "";
58 }
60 /* Name displayed in service overview */
61 $fields['Message'] = _("Empty service");
63 /* Allow/disallow some functions */
64 $fields['AllowStart'] = $this->acl_is_writeable("start");
65 $fields['AllowStop'] = $this->acl_is_writeable("stop");
66 $fields['AllowRestart'] = $this->acl_is_writeable("restart");
67 $fields['AllowRemove'] = $this->acl_is_removeable();
68 $fields['AllowEdit'] = true;
69 return($fields);
70 }
73 /* Remove service */
74 function remove_from_parent()
75 {
76 if(!$this->initially_was_account || !$this->acl_is_removeable()){
77 return;
78 }
80 plugin::remove_from_parent();
82 /* Remove status flag, it is not a memeber of
83 this->attributes, so ensure that it is deleted too */
84 if(!empty($this->StatusFlag)){
85 $this->attrs[$this->StatusFlag] = array();
86 }
88 /* Check if this is a new entry ... add/modify */
89 $ldap = $this->config->get_ldap_link();
90 $ldap->cat($this->dn,array("objectClass"));
91 if($ldap->count()){
92 $ldap->cd($this->dn);
93 $ldap->modify($this->attrs);
94 }else{
95 $ldap->cd($this->dn);
96 $ldap->add($this->attrs);
98 }
100 new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
102 show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
103 $this->handle_post_events("remove");
104 }
107 /* Save service */
108 function save()
109 {
110 plugin::save();
111 /* Check if this is a new entry ... add/modify */
112 $ldap = $this->config->get_ldap_link();
113 $ldap->cat($this->dn,array("objectClass"));
114 if($ldap->count()){
115 $ldap->cd($this->dn);
116 $ldap->modify($this->attrs);
117 }else{
118 $ldap->cd($this->dn);
119 $ldap->add($this->attrs);
120 }
121 if($this->initially_was_account){
122 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
123 $this->handle_post_events("modify");
124 }else{
125 $this->handle_post_events("add");
126 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
127 }
128 show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
129 }
132 /* Directly save new status flag */
133 function setStatus($value)
134 {
135 if($value == "none") return;
137 /* Can't set status flag for new services (Object doesn't exists in ldap tree) */
138 if(!$this->initially_was_account) return;
140 /* Can't set status flag, if no flag is specified */
141 if(empty($this->StatusFlag)){
142 return;
143 }
145 /* Get object (server), update status flag and save changes */
146 $ldap = $this->config->get_ldap_link();
147 $ldap->cd($this->dn);
148 $ldap->cat($this->dn,array("objectClass"));
149 if($ldap->count()){
151 $tmp = $ldap->fetch();
152 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
153 $attrs['objectClass'][] = $tmp['objectClass'][$i];
154 }
155 $flag = $this->StatusFlag;
156 $attrs[$flag] = $value;
157 $this->$flag = $value;
158 $ldap->modify($attrs);
159 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
160 $this->action_hook();
161 }
162 }
165 function check()
166 {
167 $message = plugin::check();
168 return($message);
169 }
172 function save_object()
173 {
174 plugin::save_object();
175 }
178 function action_hook($add_attrs= array())
179 {
180 /* Find postcreate entries for this class */
181 $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
182 if ($command == "" && isset($this->config->data['TABS'])){
183 $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
184 }
186 if ($command != ""){
188 /* Walk through attribute list */
189 foreach ($this->attributes as $attr){
190 if (!is_array($this->$attr)){
191 $command= preg_replace("/%$attr/", $this->$attr, $command);
192 }
193 }
194 $command= preg_replace("/%dn/", $this->dn, $command);
196 /* Additional attributes */
197 foreach ($add_attrs as $name => $value){
198 $command= preg_replace("/%$name/", $value, $command);
199 }
201 /* If there are still some %.. in our command, try to fill these with some other class vars */
202 if(preg_match("/%/",$command)){
203 $attrs = get_object_vars($this);
204 foreach($attrs as $name => $value){
205 if(!is_string($value)) continue;
206 $command= preg_replace("/%$name/", $value, $command);
207 }
208 }
210 if (check_command($command)){
211 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
212 $command, "Execute");
214 exec($command);
215 } else {
216 $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
217 print_red ($message);
218 }
219 }
220 }
223 /* Get updates for status flag */
224 function updateStatusState()
225 {
226 if(empty($this->StatusFlag)) return;
228 $attrs = array();
229 $flag = $this->StatusFlag;
230 $ldap = $this->config->get_ldap_link();
231 $ldap->cd($this->cn);
232 $ldap->cat($this->dn,array($flag));
233 if($ldap->count()){
234 $attrs = $ldap->fetch();
235 }
236 if(isset($attrs[$flag][0])){
237 $this->$flag = $attrs[$flag][0];
238 }
239 }
240 }
241 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
242 ?>