7741a1cb046183d393c6811ff0f6655bb46fc937
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$$
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{
25 /* This plugin only writes its objectClass */
26 var $objectclasses = array();
27 var $attributes = array();
28 var $StatusFlag = "";
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;
38 /* Construcktion */
39 function goService(&$config,$dn)
40 {
41 plugin::plugin($config,$dn);
42 $this->DisplayName = _("Empty service");
43 }
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")."'> ".
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 {
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 }
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);
114 }
116 new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
118 if (!$ldap->success()){
119 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
120 }
121 $this->handle_post_events("remove");
122 }
125 /* Save service */
126 function save()
127 {
128 plugin::save();
129 /* Check if this is a new entry ... add/modify */
130 $ldap = $this->config->get_ldap_link();
131 $ldap->cat($this->dn,array("objectClass"));
132 if($ldap->count()){
133 $ldap->cd($this->dn);
134 $ldap->modify($this->attrs);
135 }else{
136 $ldap->cd($this->dn);
137 $ldap->add($this->attrs);
138 }
139 if($this->initially_was_account){
140 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
141 $this->handle_post_events("modify");
142 }else{
143 $this->handle_post_events("add");
144 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
145 }
146 if (!$ldap->success()){
147 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
148 }
149 }
152 /* Directly save new status flag */
153 function setStatus($value)
154 {
155 if($value == "none") return;
157 /* Can't set status flag for new services (Object doesn't exists in ldap tree) */
158 if(!$this->initially_was_account) return;
160 /* Can't set status flag, if no flag is specified */
161 if(empty($this->StatusFlag)){
162 return;
163 }
165 /* Get object (server), update status flag and save changes */
166 $ldap = $this->config->get_ldap_link();
167 $ldap->cd($this->dn);
168 $ldap->cat($this->dn,array("objectClass"));
169 if($ldap->count()){
171 $tmp = $ldap->fetch();
172 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
173 $attrs['objectClass'][] = $tmp['objectClass'][$i];
174 }
175 $flag = $this->StatusFlag;
176 $attrs[$flag] = $value;
177 $this->$flag = $value;
178 $ldap->modify($attrs);
179 if (!$ldap->success()){
180 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
181 }
182 $this->action_hook();
183 }
184 }
187 function check()
188 {
189 $message = plugin::check();
190 return($message);
191 }
194 function save_object()
195 {
196 plugin::save_object();
197 }
200 function action_hook($add_attrs= array())
201 {
202 /* Find postcreate entries for this class */
203 $command= $this->config->search(get_class($this), "ACTION_HOOK",array('menu','tabs'));
204 if ($command != ""){
206 /* Walk through attribute list */
207 foreach ($this->attributes as $attr){
208 if (!is_array($this->$attr)){
209 $command= preg_replace("/%$attr/", $this->$attr, $command);
210 }
211 }
212 $command= preg_replace("/%dn/", $this->dn, $command);
214 /* Additional attributes */
215 foreach ($add_attrs as $name => $value){
216 $command= preg_replace("/%$name/", $value, $command);
217 }
219 /* If there are still some %.. in our command, try to fill these with some other class vars */
220 if(preg_match("/%/",$command)){
221 $attrs = get_object_vars($this);
222 foreach($attrs as $name => $value){
223 if(!is_string($value)) continue;
224 $command= preg_replace("/%$name/", $value, $command);
225 }
226 }
228 if (check_command($command)){
229 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
230 $command, "Execute");
232 exec($command);
233 } else {
234 msg_dialog::display(_("Configuration error"), msgPool::cmdnotfound("ACTION_HOOK", get_class($this)), ERROR_DIALOG);
235 }
236 }
237 }
240 /* Get updates for status flag */
241 function updateStatusState()
242 {
243 if(empty($this->StatusFlag)) return;
245 $attrs = array();
246 $flag = $this->StatusFlag;
247 $ldap = $this->config->get_ldap_link();
248 $ldap->cd($this->cn);
249 $ldap->cat($this->dn,array($flag));
250 if($ldap->count()){
251 $attrs = $ldap->fetch();
252 }
253 if(isset($attrs[$flag][0])){
254 $this->$flag = $attrs[$flag][0];
255 }
256 }
257 }
258 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
259 ?>