6d4e25bc79449849a299a29c62723b6bebefe22c
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 $additionaloc= array("goImapServer" => array("goImapName", "goImapConnect",
43 "goImapAdmin", "goImapPassword",
44 "goImapSieveServer",
45 "goImapSievePort"),
46 "goKrbServer" => array("goKrbRealm", "goKrbAdmin",
47 "goKrbPassword"),
48 "goFaxServer" => array("goFaxAdmin", "goFaxPassword"),
49 "goLogDBServer" => array("goLogAdmin", "goLogPassword"),
50 "goGlpiServer" => array("goGlpiAdmin","goGlpiDatabase","goGlpiPassword"),
51 "goFonServer" => array("goFonAdmin", "goFonPassword",
52 "goFonAreaCode", "goFonCountryCode"));
54 function servdb ($config, $dn= NULL)
55 {
56 plugin::plugin ($config, $dn);
58 /* Make dynamic list of objectClasses */
59 foreach ($this->additionaloc as $oc => $dummy){
60 if (isset($this->attrs['objectClass']) && in_array($oc, $this->attrs['objectClass'])){
61 $this->objectclasses[$oc]= $oc;
62 }
63 }
65 /* We need to save the historical state of goImapName for later checking */
66 $this->goImapName_old= $this->goImapName;
68 /* Always is account... */
69 $this->is_account= TRUE;
70 }
72 function execute()
73 {
74 /* Call parent execute */
75 plugin::execute();
77 /* Fill templating stuff */
78 $smarty= get_smarty();
80 /* Attributes... */
81 foreach ($this->attributes as $attr){
82 $smarty->assign("$attr", $this->$attr);
83 $smarty->assign("$attr"."ACL", chkacl($this->acl, $attr));
84 }
86 /* Classes... */
87 foreach ($this->additionaloc as $oc => $dummy){
88 if (isset($this->objectclasses[$oc])){
89 $smarty->assign("$oc", "checked");
90 $smarty->assign("$oc"."State", "");
91 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
93 } else {
94 $smarty->assign("$oc", "");
95 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
96 $smarty->assign("$oc"."State", "disabled");
97 }
98 }
100 return($smarty->fetch (get_template_path('servdb.tpl', TRUE)));
101 }
103 function remove_from_parent()
104 {
105 /* This cannot be removed... */
106 }
109 /* Save data to object */
110 function save_object()
111 {
112 if (isset($_POST['dbtab'])){
113 plugin::save_object();
115 /* Save checkbox state */
116 foreach ($this->additionaloc as $oc => $dummy){
117 if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
118 $this->objectclasses[$oc]= $oc;
119 } else {
120 unset($this->objectclasses[$oc]);
121 }
122 }
123 }
124 }
127 /* Check supplied data */
128 function check()
129 {
130 /* Call common method to give check the hook */
131 $message= plugin::check();
133 if(in_array("goGlpiServer",$this->objectclasses)){
134 foreach(array("goGlpiAdmin","goGlpiDatabase") as $attr){
135 if(empty($this->$attr)){
136 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
137 }
138 }
139 }
141 /* All fields are marked as *must* */
142 if (in_array("goImapServer", $this->objectclasses)){
143 foreach (array("goImapAdmin", "goImapName") as $attr){
144 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
145 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
146 }
147 }
150 /* Check connect string */
151 if (!preg_match('/^\{[^:]+:[0-9]+.*\}$/', $this->goImapConnect)){
152 $message[]= sprintf(_("The imap connect string needs to be in the form '%s'."),
153 '{server-name:port/options}');
154 }
155 if (!preg_match('/^[0-9]+$/', $this->goImapSievePort)){
156 $message[]= _("The sieve port needs to be numeric.");
157 }
158 }
159 if (in_array("goKrbServer", $this->objectclasses)){
160 foreach (array("goKrbAdmin", "goKrbRealm") as $attr){
161 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
162 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
163 }
164 }
165 /* goKrbPassword is a must field, if goKrbServer is used as objectClass */
166 if ($this->goKrbPassword == "" ){
167 $message[]= sprintf(_("The specified kerberos password is empty."), $attr);
168 }
169 }
170 if (in_array("goFaxServer", $this->objectclasses)){
171 if ($this->goFaxAdmin == "" || preg_match("/ /", $this->goFaxAdmin)){
172 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), "goFaxAdmin");
173 }
174 }
175 if (in_array("goLogServer", $this->objectclasses)){
176 if ($this->goLogAdmin == "" || preg_match("/ /", $this->goLogAdmin)){
177 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), "goLogAdmin");
178 }
179 }
180 if (in_array("goFonServer", $this->objectclasses)){
181 foreach (array("goFonAdmin", "goFonAreaCode", "goFonCountryCode") as $attr){
182 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
183 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
184 }
185 }
186 }
187 if (in_array("goImapServer", $this->objectclasses) &&
188 $this->goImapName != $this->goImapName_old) {
189 // Attribute has changed
190 if(!preg_match("/^[a-z0-9.-]+$/", $this->goImapName)) {
191 $message[]= sprintf(_("The imap name string needs to be a hostname or an IP-address."));
192 }
193 }
195 return ($message);
196 }
199 /* Save to LDAP */
200 function save()
201 {
202 /* Normalize lazy objectclass arrays */
203 $objectclasses= array();
204 foreach($this->objectclasses as $oc){
205 $objectclasses[]= $oc;
206 }
208 /* Move cn to sieve server */
209 $this->goImapSieveServer= $this->cn;
211 plugin::save();
213 $test = $this->objectclasses;
214 $tmp = array_flip($this->attrs['objectClass']);
216 foreach($this->additionaloc as $key=>$val) {
217 unset($tmp[$key]);
218 }
220 $classes = (array_flip(array_merge(array_flip($test),$tmp)));
222 unset($this->attrs['objectClass']);
224 foreach($classes as $class){
225 $this->attrs['objectClass'][]=$class;
226 }
229 /* Remove unneeded attributes */
230 foreach ($this->additionaloc as $oc => $attrs){
231 if (!in_array($oc, $this->attrs['objectClass'])){
232 foreach ($attrs as $attr){
233 $this->attrs[$attr]= array();
234 }
235 }
236 }
237 $this->attrs = array_reverse($this->attrs);
239 /* Write to LDAP */
240 $ldap= $this->config->get_ldap_link();
241 $ldap->cd($this->dn);
242 $this->cleanup();
243 $ldap->modify ($this->attrs);
245 show_ldap_error($ldap->get_error(), _("Saving server db settings failed"));
247 /* Optionally execute a command after we're done */
248 if ($this->initially_was_account == $this->is_account){
249 if ($this->is_modified){
250 $this->handle_post_events("mofify");
251 }
252 } else {
253 $this->handle_post_events("add");
254 }
255 }
257 }
259 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
260 ?>