65e0f9df5419260db5f885977bc81db0f9dc628a
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 $message= array();
132 if(in_array("goGlpiServer",$this->objectclasses)){
133 foreach(array("goGlpiAdmin","goGlpiDatabase") as $attr){
134 if(empty($this->$attr)){
135 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
136 }
137 }
138 }
140 /* All fields are marked as *must* */
141 if (in_array("goImapServer", $this->objectclasses)){
142 foreach (array("goImapAdmin", "goImapName") as $attr){
143 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
144 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
145 }
146 }
149 /* Check connect string */
150 if (!preg_match('/^\{[^:]+:[0-9]+.*\}$/', $this->goImapConnect)){
151 $message[]= sprintf(_("The imap connect string needs to be in the form '%s'."),
152 '{server-name:port/options}');
153 }
154 if (!preg_match('/^[0-9]+$/', $this->goImapSievePort)){
155 $message[]= _("The sieve port needs to be numeric.");
156 }
157 }
158 if (in_array("goKrbServer", $this->objectclasses)){
159 foreach (array("goKrbAdmin", "goKrbRealm") as $attr){
160 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
161 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
162 }
163 }
164 /* goKrbPassword is a must field, if goKrbServer is used as objectClass */
165 if ($this->goKrbPassword == "" ){
166 $message[]= sprintf(_("The specified kerberos password is empty."), $attr);
167 }
168 }
169 if (in_array("goFaxServer", $this->objectclasses)){
170 if ($this->goFaxAdmin == "" || preg_match("/ /", $this->goFaxAdmin)){
171 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), "goFaxAdmin");
172 }
173 }
174 if (in_array("goLogServer", $this->objectclasses)){
175 if ($this->goLogAdmin == "" || preg_match("/ /", $this->goLogAdmin)){
176 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), "goLogAdmin");
177 }
178 }
179 if (in_array("goFonServer", $this->objectclasses)){
180 foreach (array("goFonAdmin", "goFonAreaCode", "goFonCountryCode") as $attr){
181 if ($this->$attr == "" || preg_match("/ /", $this->$attr)){
182 $message[]= sprintf(_("The attribute '%s' is empty or contains invalid characters."), $attr);
183 }
184 }
185 }
186 if (in_array("goImapServer", $this->objectclasses) &&
187 $this->goImapName != $this->goImapName_old) {
188 // Attribute has changed
189 if(!preg_match("/^[a-z0-9.-]+$/", $this->goImapName)) {
190 $message[]= sprintf(_("The imap name string needs to be a hostname or an IP-address."));
191 }
192 }
194 return ($message);
195 }
198 /* Save to LDAP */
199 function save()
200 {
201 /* Normalize lazy objectclass arrays */
202 $objectclasses= array();
203 foreach($this->objectclasses as $oc){
204 $objectclasses[]= $oc;
205 }
207 /* Move cn to sieve server */
208 $this->goImapSieveServer= $this->cn;
210 plugin::save();
212 $test = $this->objectclasses;
213 $tmp = array_flip($this->attrs['objectClass']);
215 foreach($this->additionaloc as $key=>$val) {
216 unset($tmp[$key]);
217 }
219 $classes = (array_flip(array_merge(array_flip($test),$tmp)));
221 unset($this->attrs['objectClass']);
223 foreach($classes as $class){
224 $this->attrs['objectClass'][]=$class;
225 }
228 /* Remove unneeded attributes */
229 foreach ($this->additionaloc as $oc => $attrs){
230 if (!in_array($oc, $this->attrs['objectClass'])){
231 foreach ($attrs as $attr){
232 $this->attrs[$attr]= array();
233 }
234 }
235 }
236 $this->attrs = array_reverse($this->attrs);
238 /* Write to LDAP */
239 $ldap= $this->config->get_ldap_link();
240 $ldap->cd($this->dn);
241 $this->cleanup();
242 $ldap->modify ($this->attrs);
244 show_ldap_error($ldap->get_error());
246 /* Optionally execute a command after we're done */
247 if ($this->initially_was_account == $this->is_account){
248 if ($this->is_modified){
249 $this->handle_post_events("mofify");
250 }
251 } else {
252 $this->handle_post_events("add");
253 }
254 }
256 }
258 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
259 ?>