1 <?php
3 class servdb extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server basic objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* Object information */
11 var $goImapName= "";
12 var $goImapName_old= "";
13 var $goImapConnect= "";
14 var $goImapAdmin= "";
15 var $goImapPassword= "";
16 var $goImapSieveServer= "";
17 var $goImapSievePort= "";
18 var $goKrbRealm= "";
19 var $goKrbAdmin= "";
20 var $goKrbPassword= "";
21 var $goFaxAdmin= "";
22 var $goFaxPassword= "";
23 var $goLogAdmin= "";
24 var $goLogPassword= "";
25 var $goFonAdmin= "";
26 var $goFonPassword= "";
27 var $goFonAreaCode= "";
28 var $goFonCountryCode= "";
29 var $ignore_account= TRUE;
30 var $goGlpiAdmin="";
31 var $goGlpiDatabase = "";
32 var $goGlpiPassword ="";
35 /* attribute list for save action */
36 var $attributes= array("goImapName", "goImapConnect", "goImapAdmin", "goImapPassword",
37 "goImapSieveServer", "goImapSievePort", "goKrbRealm",
38 "goKrbAdmin", "goKrbPassword", "goFaxAdmin", "goFaxPassword",
39 "goLogAdmin", "goLogPassword", "goFonAdmin", "goFonPassword",
40 "goFonAreaCode", "goFonCountryCode","goGlpiAdmin","goGlpiDatabase","goGlpiPassword");
41 var $objectclasses = array("top", "goServer");
42 var $init_objectclasses = array();
43 var $additionaloc= array("goImapServer" => array("goImapName", "goImapConnect",
44 "goImapAdmin", "goImapPassword",
45 "goImapSieveServer",
46 "goImapSievePort"),
47 "goKrbServer" => array("goKrbRealm", "goKrbAdmin",
48 "goKrbPassword"),
49 "goFaxServer" => array("goFaxAdmin", "goFaxPassword"),
50 "goLogDBServer" => array("goLogAdmin", "goLogPassword"),
51 "goGlpiServer" => array("goGlpiAdmin","goGlpiDatabase","goGlpiPassword"),
52 "goFonServer" => array("goFonAdmin", "goFonPassword",
53 "goFonAreaCode", "goFonCountryCode"));
55 function servdb ($config, $dn= NULL, $parent= NULL)
56 {
57 plugin::plugin ($config, $dn, $parent);
59 /* Make dynamic list of objectClasses */
60 foreach ($this->additionaloc as $oc => $dummy){
61 if (isset($this->attrs['objectClass']) && in_array($oc, $this->attrs['objectClass'])){
62 $this->objectclasses[$oc]= $oc;
63 }
64 }
66 /* Remember used databases */
67 $this->init_objectclasses = $this->objectclasses;
69 /* We need to save the historical state of goImapName for later checking */
70 $this->goImapName_old= $this->goImapName;
72 /* Always is account... */
73 $this->is_account= TRUE;
74 }
76 function execute()
77 {
78 /* Call parent execute */
79 plugin::execute();
81 /* Fill templating stuff */
82 $smarty= get_smarty();
84 /* Attributes... */
85 foreach ($this->attributes as $attr){
86 $smarty->assign("$attr", $this->$attr);
87 $smarty->assign("$attr"."ACL", chkacl($this->acl, $attr));
88 }
90 /* Classes... */
91 foreach ($this->additionaloc as $oc => $dummy){
92 if (isset($this->objectclasses[$oc])){
93 $smarty->assign("$oc", "checked");
94 $smarty->assign("$oc"."State", "");
95 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
97 } else {
98 $smarty->assign("$oc", "");
99 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
100 $smarty->assign("$oc"."State", "disabled");
101 }
102 }
104 return($smarty->fetch (get_template_path('servdb.tpl', TRUE)));
105 }
107 function remove_from_parent()
108 {
109 /* This cannot be removed... */
110 }
113 /* Save data to object */
114 function save_object()
115 {
116 if (isset($_POST['dbtab'])){
117 plugin::save_object();
119 /* Save checkbox state */
120 foreach ($this->additionaloc as $oc => $dummy){
122 if(chkacl($this->acl,$oc) == ""){
123 if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
124 $this->objectclasses[$oc]= $oc;
125 } else {
126 unset($this->objectclasses[$oc]);
127 }
128 }
129 }
130 }
131 }
134 /* Check supplied data */
135 function check()
136 {
137 /* Call common method to give check the hook */
138 $message= plugin::check();
140 if(in_array("goGlpiServer",$this->objectclasses)){
141 foreach(array("goGlpiAdmin","goGlpiDatabase") as $attr){
142 if(empty($this->$attr)){
143 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
144 }
145 }
146 }
148 /* All fields are marked as *must* */
149 if (in_array("goImapServer", $this->objectclasses)){
150 foreach (array("goImapAdmin", "goImapName") as $attr){
151 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
152 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
153 }
154 }
157 /* Check connect string */
158 if (!preg_match('/^\{[^:]+:[0-9]+.*\}$/', $this->goImapConnect)){
159 $message[]= sprintf(_("The imap connect string needs to be in the form '%s'."),
160 '{server-name:port/options}');
161 }
162 if (!preg_match('/^[0-9]+$/', $this->goImapSievePort)){
163 $message[]= _("The sieve port needs to be numeric.");
164 }
165 }
166 if (in_array("goKrbServer", $this->objectclasses)){
167 foreach (array("goKrbAdmin", "goKrbRealm") as $attr){
168 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
169 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
170 }
171 }
172 /* goKrbPassword is a must field, if goKrbServer is used as objectClass */
173 if ($this->goKrbPassword == "" ){
174 $message[]= sprintf(_("The specified kerberos password is empty."), $attr);
175 }
176 }
177 if (in_array("goFaxServer", $this->objectclasses)){
178 if ($this->goFaxAdmin == "" || preg_match("/ /", $this->goFaxAdmin)){
179 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), "goFaxAdmin");
180 }
181 }
182 if (in_array("goLogServer", $this->objectclasses)){
183 if ($this->goLogAdmin == "" || preg_match("/ /", $this->goLogAdmin)){
184 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), "goLogAdmin");
185 }
186 }
187 if (in_array("goFonServer", $this->objectclasses)){
188 foreach (array("goFonAdmin", "goFonAreaCode", "goFonCountryCode") as $attr){
189 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
190 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
191 }
192 }
193 }
195 /* Check if we are able to remove the asterisk database. If the database is still in use skip */
196 if (!in_array("goFonServer", $this->objectclasses) && in_array("goFonServer", $this->init_objectclasses)){
197 $ldap = $this->config->get_ldap_link();
198 $ldap->cd($this->config->current['BASE']);
199 $ldap->search("(&(goFonHomeServer=".$this->dn.")(|(uid=*)(cn=*)))",array("dn","uid","cn","goFonHomeServer"));
200 if($ldap->count()){
202 /* Number of entries shown in warning */
203 $i = 3;
204 $str = "";
205 while(($attrs = $ldap->fetch()) && $i >= 0){
206 $i --;
207 if(isset($attrs['uid'][0])){
208 $str .= $attrs['uid'][0]." ";
209 }else{
210 $str .= $attrs['cn'][0]." ";
211 }
212 }
214 /* Some entries found */
215 if($i != 3){
216 $message[] = sprintf(_("You can't remove the asterisk database extension, it is still in use by these objects '%s'."),trim($str));
217 }
218 }
219 }
221 if (in_array("goImapServer", $this->objectclasses) &&
222 $this->goImapName != $this->goImapName_old) {
223 // Attribute has changed
224 if(!preg_match("/^[a-z0-9.-]+$/", $this->goImapName)) {
225 $message[]= sprintf(_("The imap name string needs to be a hostname or an IP-address."));
226 }
227 }
229 return ($message);
230 }
233 /* Save to LDAP */
234 function save()
235 {
236 /* Normalize lazy objectclass arrays */
237 $objectclasses= array();
238 foreach($this->objectclasses as $oc){
239 $objectclasses[]= $oc;
240 }
242 /* Move cn to sieve server */
243 $this->goImapSieveServer= $this->cn;
245 plugin::save();
247 $test = $this->objectclasses;
248 $tmp = array_flip($this->attrs['objectClass']);
250 foreach($this->additionaloc as $key=>$val) {
251 unset($tmp[$key]);
252 }
254 $classes = (array_flip(array_merge(array_flip($test),$tmp)));
256 unset($this->attrs['objectClass']);
258 foreach($classes as $class){
259 $this->attrs['objectClass'][]=$class;
260 }
263 /* Remove unneeded attributes */
264 foreach ($this->additionaloc as $oc => $attrs){
265 if (!in_array($oc, $this->attrs['objectClass'])){
266 foreach ($attrs as $attr){
267 $this->attrs[$attr]= array();
268 }
269 }
270 }
271 $this->attrs = array_reverse($this->attrs);
273 /* Write to LDAP */
274 $ldap= $this->config->get_ldap_link();
275 $ldap->cd($this->dn);
276 $this->cleanup();
277 $ldap->modify ($this->attrs);
279 show_ldap_error($ldap->get_error(), _("Saving server db settings failed"));
281 /* Optionally execute a command after we're done */
282 if ($this->initially_was_account == $this->is_account){
283 if ($this->is_modified){
284 $this->handle_post_events("modify");
285 }
286 } else {
287 $this->handle_post_events("add");
288 }
289 }
291 function allow_remove()
292 {
293 /* Check if we are able to remove the asterisk database. If the database is still in use skip */
294 if (in_array_ics("goFonServer", $this->init_objectclasses)){
295 $ldap = $this->config->get_ldap_link();
296 $ldap->cd($this->config->current['BASE']);
297 $ldap->search("(&(goFonHomeServer=".$this->dn.")(|(uid=*)(cn=*)))",array("dn","uid","cn","goFonHomeServer"));
298 if($ldap->count()){
300 /* Number of entries shown in warning */
301 $i = 3;
302 $str = "";
303 while(($attrs = $ldap->fetch()) && $i >= 0){
304 $i --;
305 if(isset($attrs['uid'][0])){
306 $str .= $attrs['uid'][0]." ";
307 }else{
308 $str .= $attrs['cn'][0]." ";
309 }
310 }
312 /* Some entries found */
313 if($i != 3){
314 return(sprintf(_("You can't remove the asterisk database extension, it is still in use by these objects '%s'."),trim($str)));
315 }
316 }
317 }
318 }
321 }
323 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
324 ?>