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 /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
87 $smarty->assign("staticAddress", "");
89 /* Classes... */
90 foreach ($this->additionaloc as $oc => $dummy){
91 if (isset($this->objectclasses[$oc])){
92 $smarty->assign("$oc", "checked");
93 $smarty->assign("$oc"."State", "");
94 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
96 } else {
97 $smarty->assign("$oc", "");
98 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
99 $smarty->assign("$oc"."State", "disabled");
100 }
101 }
103 return($smarty->fetch (get_template_path('servdb.tpl', TRUE)));
104 }
106 function remove_from_parent()
107 {
108 /* This cannot be removed... */
109 }
112 /* Save data to object */
113 function save_object()
114 {
115 if (isset($_POST['dbtab'])){
116 plugin::save_object();
118 /* Save checkbox state */
119 foreach ($this->additionaloc as $oc => $dummy){
120 if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
121 $this->objectclasses[$oc]= $oc;
122 } else {
123 unset($this->objectclasses[$oc]);
124 }
125 }
126 }
127 }
130 /* Check supplied data */
131 function check()
132 {
133 $message= array();
135 if(in_array("goGlpiServer",$this->objectclasses)){
136 foreach(array("goGlpiAdmin","goGlpiDatabase") as $attr){
137 if(empty($this->$attr)){
138 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
139 }
140 }
141 }
143 /* All fields are marked as *must* */
144 if (in_array("goImapServer", $this->objectclasses)){
145 foreach (array("goImapAdmin", "goImapName") as $attr){
146 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
147 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
148 }
149 }
152 /* Check connect string */
153 if (!preg_match('/^\{[^:]+:[0-9]+.*\}$/', $this->goImapConnect)){
154 $message[]= sprintf(_("The imap connect string needs to be in the form '%s'."),
155 '{server-name:port/options}');
156 }
157 if (!preg_match('/^[0-9]+$/', $this->goImapSievePort)){
158 $message[]= _("The sieve port needs to be numeric.");
159 }
160 }
161 if (in_array("goKrbServer", $this->objectclasses)){
162 foreach (array("goKrbAdmin", "goKrbRealm") as $attr){
163 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
164 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
165 }
166 }
167 /* goKrbPassword is a must field, if goKrbServer is used as objectClass */
168 if ($this->goKrbPassword == "" ){
169 $message[]= sprintf(_("The specified kerberos password is empty."), $attr);
170 }
171 }
172 if (in_array("goFaxServer", $this->objectclasses)){
173 if ($this->goFaxAdmin == "" || preg_match("/ /", $this->goFaxAdmin)){
174 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), "goFaxAdmin");
175 }
176 }
177 if (in_array("goLogServer", $this->objectclasses)){
178 if ($this->goLogAdmin == "" || preg_match("/ /", $this->goLogAdmin)){
179 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), "goLogAdmin");
180 }
181 }
182 if (in_array("goFonServer", $this->objectclasses)){
183 foreach (array("goFonAdmin", "goFonAreaCode", "goFonCountryCode") as $attr){
184 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
185 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
186 }
187 }
188 }
189 if (in_array("goImapServer", $this->objectclasses) &&
190 $this->goImapName != $this->goImapName_old) {
191 // Attribute has changed
192 if(!preg_match("/^[a-z0-9.-]+$/", $this->goImapName)) {
193 $message[]= sprintf(_("The imap name string needs to be a hostname or an IP-address."));
194 }
195 }
197 return ($message);
198 }
201 /* Save to LDAP */
202 function save()
203 {
204 /* Normalize lazy objectclass arrays */
205 $objectclasses= array();
206 foreach($this->objectclasses as $oc){
207 $objectclasses[]= $oc;
208 }
210 /* Move cn to sieve server */
211 $this->goImapSieveServer= $this->cn;
213 plugin::save();
215 $test = $this->objectclasses;
216 $tmp = array_flip($this->attrs['objectClass']);
218 foreach($this->additionaloc as $key=>$val) {
219 unset($tmp[$key]);
220 }
222 $classes = (array_flip(array_merge(array_flip($test),$tmp)));
224 unset($this->attrs['objectClass']);
226 foreach($classes as $class){
227 $this->attrs['objectClass'][]=$class;
228 }
231 /* Remove unneeded attributes */
232 foreach ($this->additionaloc as $oc => $attrs){
233 if (!in_array($oc, $this->attrs['objectClass'])){
234 foreach ($attrs as $attr){
235 $this->attrs[$attr]= array();
236 }
237 }
238 }
239 $this->attrs = array_reverse($this->attrs);
241 /* Write to LDAP */
242 $ldap= $this->config->get_ldap_link();
243 $ldap->cd($this->dn);
244 $ldap->modify($this->attrs);
245 show_ldap_error($ldap->get_error());
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 ?>