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 = "";
21 /* Construcktion */
22 function goService($config,$dn)
23 {
24 plugin::plugin($config,$dn);
25 $this->DisplayName = _("Empty service");
27 if($this->is_account){
28 @log::log("view","server/".get_class($this),$this->dn);
29 }
30 }
33 /* Create content */
34 function execute()
35 {
36 $str ="<div style='width:100%; text-align:right;'>".
37 " <input type='submit' name='SaveService' value='"._("Save")."'> ".
38 " <input type='submit' name='CancelService' value='"._("Cancel")."'>".
39 "</div>";
40 return($str);
41 }
44 /* Get service information for serverService plugin */
45 function getListEntry()
46 {
48 $this->updateStatusState();
50 /* Assign status flag */
51 if(!empty($this->StatusFlag)){
52 $flag = $this->StatusFlag;
53 $fields['Status'] = $this->$flag;
54 }else{
55 $fields['Status'] = "";
56 }
58 /* Name displayed in service overview */
59 $fields['Message'] = _("Empty service");
61 /* Allow/disallow some functions */
62 $fields['AllowStart'] = $this->acl_is_writeable("start");
63 $fields['AllowStop'] = $this->acl_is_writeable("stop");
64 $fields['AllowRestart'] = $this->acl_is_writeable("restart");
65 $fields['AllowRemove'] = $this->acl_is_removeable();
66 $fields['AllowEdit'] = true;
67 return($fields);
68 }
71 /* Remove service */
72 function remove_from_parent()
73 {
74 if(!$this->initially_was_account || !$this->acl_is_removeable()){
75 return;
76 }
78 plugin::remove_from_parent();
80 /* Remove status flag, it is not a memeber of
81 this->attributes, so ensure that it is deleted too */
82 if(!empty($this->StatusFlag)){
83 $this->attrs[$this->StatusFlag] = array();
84 }
86 /* Check if this is a new entry ... add/modify */
87 $ldap = $this->config->get_ldap_link();
88 $ldap->cat($this->dn,array("objectClass"));
89 if($ldap->count()){
90 $ldap->cd($this->dn);
91 $ldap->modify($this->attrs);
92 }else{
93 $ldap->cd($this->dn);
94 $ldap->add($this->attrs);
96 }
98 @log::log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
100 show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
101 $this->handle_post_events("remove");
102 }
105 /* Save service */
106 function save()
107 {
108 plugin::save();
109 /* Check if this is a new entry ... add/modify */
110 $ldap = $this->config->get_ldap_link();
111 $ldap->cat($this->dn,array("objectClass"));
112 if($ldap->count()){
113 $ldap->cd($this->dn);
114 $ldap->modify($this->attrs);
115 }else{
116 $ldap->cd($this->dn);
117 $ldap->add($this->attrs);
118 }
119 if($this->initially_was_account){
120 @log::log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
121 $this->handle_post_events("modify");
122 }else{
123 $this->handle_post_events("add");
124 @log::log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
125 }
126 show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
127 }
130 /* Directly save new status flag */
131 function setStatus($value)
132 {
133 if($value == "none") return;
135 /* Can't set status flag for new services (Object doesn't exists in ldap tree) */
136 if(!$this->initially_was_account) return;
138 /* Can't set status flag, if no flag is specified */
139 if(empty($this->StatusFlag)){
140 return;
141 }
143 /* Get object (server), update status flag and save changes */
144 $ldap = $this->config->get_ldap_link();
145 $ldap->cd($this->dn);
146 $ldap->cat($this->dn,array("objectClass"));
147 if($ldap->count()){
149 $tmp = $ldap->fetch();
150 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
151 $attrs['objectClass'][] = $tmp['objectClass'][$i];
152 }
153 $flag = $this->StatusFlag;
154 $attrs[$flag] = $value;
155 $this->$flag = $value;
156 $ldap->modify($attrs);
157 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/".get_class($this)." - (".$this->DisplayName.") with dn '%s' failed."),$this->dn));
158 $this->action_hook();
159 }
160 }
163 function check()
164 {
165 $message = plugin::check();
166 return($message);
167 }
170 function save_object()
171 {
172 plugin::save_object();
173 }
176 function action_hook($add_attrs= array())
177 {
178 /* Find postcreate entries for this class */
179 $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
180 if ($command == "" && isset($this->config->data['TABS'])){
181 $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
182 }
184 if ($command != ""){
186 /* Walk through attribute list */
187 foreach ($this->attributes as $attr){
188 if (!is_array($this->$attr)){
189 $command= preg_replace("/%$attr/", $this->$attr, $command);
190 }
191 }
192 $command= preg_replace("/%dn/", $this->dn, $command);
194 /* Additional attributes */
195 foreach ($add_attrs as $name => $value){
196 $command= preg_replace("/%$name/", $value, $command);
197 }
199 /* If there are still some %.. in our command, try to fill these with some other class vars */
200 if(preg_match("/%/",$command)){
201 $attrs = get_object_vars($this);
202 foreach($attrs as $name => $value){
203 if(!is_string($value)) continue;
204 $command= preg_replace("/%$name/", $value, $command);
205 }
206 }
208 if (check_command($command)){
209 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
210 $command, "Execute");
212 exec($command);
213 } else {
214 $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
215 print_red ($message);
216 }
217 }
218 }
221 /* Get updates for status flag */
222 function updateStatusState()
223 {
224 if(empty($this->StatusFlag)) return;
226 $attrs = array();
227 $flag = $this->StatusFlag;
228 $ldap = $this->config->get_ldap_link();
229 $ldap->cd($this->cn);
230 $ldap->cat($this->dn,array($flag));
231 if($ldap->count()){
232 $attrs = $ldap->fetch();
233 }
234 if(isset($attrs[$flag][0])){
235 $this->$flag = $attrs[$flag][0];
236 }
237 }
238 }
239 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
240 ?>