1 <?php
2 class kolabAccount extends plugin
3 {
4 /* Definitions */
5 var $plHeadline = "Kolab";
6 var $plDescription = "This does something";
8 /* CLI vars */
9 var $cli_summary = "Manage users Kolab account";
10 var $cli_description = "Some longer text\nfor help";
11 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
13 /* Kolab attributes */
14 var $kolabInvitationPolicy = array();
15 var $kolabFreeBusyFuture = 60;
16 var $unrestrictedMailSize = 0;
17 var $calFBURL = "";
18 var $kolabDelegate = array();
20 /* attribute list for save action */
21 var $attributes = array("kolabFreeBusyFuture", "unrestrictedMailSize", "calFBURL","kolabDelegate");
22 var $objectclasses = array("kolabInetOrgPerson");
24 /* Helper */
25 var $imapping= array();
28 function kolabAccount ($config, $dn= NULL)
29 {
30 plugin::plugin ($config, $dn);
32 /* Pull arrays */
33 foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
34 if (isset($this->attrs["$attr"]["count"])){
35 for ($i= 0; $i<$this->attrs["$attr"]["count"]; $i++){
36 array_push($this->$attr, $this->attrs["$attr"][$i]);
37 }
38 }
39 }
41 /* If this one is empty, preset with ACT_MANUAL for anonymous users */
42 if (count ($this->kolabInvitationPolicy) == 0){
43 $this->kolabInvitationPolicy= array("ACT_MANUAL");
44 }
46 /* Check is account state */
47 $this->is_account = false;
48 if(count($this->kolabDelegate)){
49 $this->is_account = true;
50 }
51 foreach(array("calFBURL","unrestrictedMailSize") as $attr){
52 if(!empty($this->$attr)){
53 $this->is_account = true;
54 }
55 }
56 }
59 function execute()
60 {
61 /* Call parent execute */
62 plugin::execute();
64 /* Show tab dialog headers */
65 $display= "";
67 /* Show main page */
68 $smarty= get_smarty();
70 /* Load attributes */
71 foreach($this->attributes as $val){
72 $smarty->assign("$val", $this->$val);
73 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
74 }
76 /* Check for invitation action */
77 $nr= 0;
78 while (isset($_POST["policy$nr"])){
79 if (isset($_POST["add$nr"])){
80 $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
81 }
82 if (isset($_POST["remove$nr"])){
83 $new= array();
84 foreach ($this->kolabInvitationPolicy as $entry){
85 if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
86 $new[]= $entry;
87 }
88 }
89 $this->kolabInvitationPolicy= $new;
90 }
91 $nr++;
92 }
94 /* Unify addresses */
95 $new= array();
96 foreach($this->kolabInvitationPolicy as $value){
97 if (preg_match('/^:/', $value)){
98 continue;
99 }
100 $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
101 $new[$address]= $value;
102 }
103 $this->kolabInvitationPolicy= array();
104 foreach($new as $value){
105 $this->kolabInvitationPolicy[]= $value;
106 }
108 /* Add delegation */
109 if (isset($_POST['add_delegation'])){
110 if ($_POST['delegate_address'] != ""){
112 /* Valid email address specified? */
113 $address= $_POST['delegate_address'];
114 $valid= FALSE;
115 if (!is_email($address)){
116 if (!is_email($address, TRUE)){
117 print_red (_("You're trying to add an invalid email address to the list of delegations."));
118 }
119 } else {
121 $ldap= $this->config->get_ldap_link();
122 $ldap->cd ($this->config->current['BASE']);
123 $ldap->search('(mail='.$address.')',array("mail"));
124 if ($ldap->count() == 0){
125 print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
126 } else {
127 $valid= TRUE;
128 }
129 }
131 if ($valid){
132 /* Add it */
133 if (chkacl ($this->acl, "kolabDelegate") == ""){
134 $this->addDelegate ($address);
135 $this->is_modified= TRUE;
136 }
138 }
139 }
140 }
142 /* Delete forward email addresses */
143 if (isset($_POST['delete_delegation'])){
144 if (count($_POST['delegate_list'])
145 && chkacl ($this->acl, "kolabDelegate") == ""){
147 $this->delDelegate ($_POST['delegate_list']);
148 }
149 }
151 /* Assemble policies */
152 $policies= array( 'ACT_ALWAYS_ACCEPT' => _("Always accept"),
153 'ACT_ALWAYS_REJECT' => _("Always reject"),
154 'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
155 'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
156 'ACT_MANUAL' => _("Manual"));
157 $smarty->assign('policies', $policies);
159 /* Adjust checkbox */
160 if ($this->unrestrictedMailSize){
161 $smarty->assign('unrestrictedMailSizeState', "checked");
162 } else {
163 $smarty->assign('unrestrictedMailSizeState', "");
164 }
166 /* Transfer account states for this union */
167 if (isset($this->parent) && $this->parent->by_object['mailAccount']->is_account){
168 $smarty->assign('mail_account', 'true');
169 } else {
170 $smarty->assign('mail_account', '');
171 }
173 /* Transfer delegation list */
174 if (!count($this->kolabDelegate)){
175 /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
176 $smarty->assign("kolabDelegate", array());
177 } else {
178 $smarty->assign("kolabDelegate", $this->kolabDelegate);
179 }
180 $smarty->assign("kolabDelegateACL", chkacl($this->acl, $this->kolabDelegate));
182 /* Create InvitationPolicy table */
183 $invitation= "";
184 $this->imapping= array();
185 $nr= 0;
186 $acl= chkacl($this->acl, "kolabInvitationPolicy");
187 foreach ($this->kolabInvitationPolicy as $entry){
188 $invitation.= "<tr><td>";
190 /* The default entry does not have colons... */
191 if (!preg_match('/:/', $entry)){
192 $invitation.= _("Anonymous");
193 $name= "";
194 $mode= $entry;
195 } else {
196 $name= preg_replace('/:.*$/', '', $entry);
197 $mode= preg_replace('/^[^:]*: */', '', $entry);
198 $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 $acl value=\"$name\">";
199 }
200 $invitation.= "</td>";
202 /* Add mode switch */
203 $invitation.= "<td><select size=\"1\" name=\"policy$nr\" $acl>";
204 foreach($policies as $key => $value){
205 if ($key == $mode){
206 $invitation.= "<option value=\"$key\" selected>$value</option>";
207 } else {
208 $invitation.= "<option value=\"$key\">$value</option>";
209 }
210 }
212 /* Assign buttons */
213 $button= "";
214 if ($nr == count($this->kolabInvitationPolicy)-1){
215 $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\">";
216 }
217 if ($nr != 0) {
218 $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\">";
219 }
221 $invitation.= "</select> $button</td></tr>\n";
222 $this->imapping[$nr]= $name;
223 $nr++;
224 }
225 $smarty->assign("invitation", $invitation);
227 $smarty->assign("kolabState",$this->is_account);
228 $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
231 return ($display);
232 }
234 function remove_from_parent()
235 {
236 /* Optionally execute a command after we're done */
237 plugin::remove_from_parent();
239 if(!in_array("kolabInetOrgPerson",$this->attrs['objectClass'])){
240 $this->attrs['objectClass'][] = "kolabInetOrgPerson";
241 }
243 $ldap = $this->config->get_ldap_linK();
244 $ldap->cd($this->config->current['BASE']);
245 $ldap->cd ($this->dn);
246 $ldap->modify($this->attrs);
248 print_a($this->attrs);
250 $this->handle_post_events('remove');
251 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/kolab account with dn '%s' failed."),$this->dn));
252 }
255 function check()
256 {
257 /* Call common method to give check the hook */
258 $message= plugin::check();
260 /* FBFuture is in days... */
261 if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
262 $message[]= _("The value specified as Free Busy future needs to be an integer.");
263 }
265 /* Check for URL scheme... */
266 if ($this->calFBURL != "" && !@preg_match('/^[^:/]+://[a-z0-9_/.+~-]+$/i', $this->calFBURL)){
267 $message[]= _("The value specified as Free Busy Information URL is invalid.");
268 }
270 /* Check invitation policy for existing mail addresses */
271 foreach($this->kolabInvitationPolicy as $policy){
273 /* Ignore anonymous string */
274 if (!preg_match('/:/', $policy)){
275 continue;
276 }
278 $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
279 if (!is_email($address)){
280 if (!is_email($address, TRUE)){
281 $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
282 }
283 } else {
285 $ldap= $this->config->get_ldap_link();
286 $ldap->cd ($this->config->current['BASE']);
287 $ldap->search('(mail='.$address.')',array("mail"));
288 if ($ldap->count() == 0){
289 $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
290 } else {
291 $valid= TRUE;
292 }
293 }
294 }
296 return ($message);
297 }
299 /* Save data to object */
300 function save_object()
301 {
302 /* Do we need to flip is_account state? */
303 if (isset($_POST['connectivityTab'])){
305 if(isset($_POST["kolabState"])){
306 $this->is_account = true;
307 }else{
308 $this->is_account = false;
309 }
311 if (chkacl('unrestrictedMailSize', $this->acl == "")){
312 if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
313 $this->unrestrictedMailSize= 1;
314 } else {
315 $this->unrestrictedMailSize= 0;
316 }
317 }
318 }
320 plugin::save_object();
322 /* Save changes done in invitation policies */
323 $nr= 0;
324 $this->kolabInvitationPolicy= array();
325 while (isset($_POST["policy$nr"])){
327 /* Anonymous? */
328 if (!isset($_POST["address$nr"])){
329 $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
330 } else {
331 $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
332 }
334 $nr++;
335 }
337 /* If this one is empty, preset with ACT_MANUAL for anonymous users */
338 if (count ($this->kolabInvitationPolicy) == 0){
339 $this->kolabInvitationPolicy= array("ACT_MANUAL");
340 }
342 }
345 /* Save to LDAP */
346 function save()
347 {
348 /* Check mailmethod before doing something useful */
349 plugin::save();
351 /* Transfer arrays */
352 $this->attrs['kolabDelegate']= $this->kolabDelegate;
353 $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
355 /* Write back to ldap */
356 $ldap= $this->config->get_ldap_link();
357 $ldap->cd($this->dn);
358 $this->cleanup();
359 $ldap->modify ($this->attrs);
361 print_a($this->attrs);
363 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/kolab account with dn '%s' failed."),$this->dn));
365 /* Optionally execute a command after we're done */
366 if ($this->initially_was_account == $this->is_account){
367 if ($this->is_modified){
368 $this->handle_post_events("mofify");
369 }
370 } else {
371 $this->handle_post_events("add");
372 }
373 }
376 /* Add entry to delegation list */
377 function addDelegate($address)
378 {
379 $this->kolabDelegate[]= $address;
380 $this->kolabDelegate= array_unique ($this->kolabDelegate);
382 sort ($this->kolabDelegate);
383 reset ($this->kolabDelegate);
384 $this->is_modified= TRUE;
385 }
387 function delDelegate($addresses)
388 {
389 $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
390 $this->is_modified= TRUE;
391 }
393 }
395 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
396 ?>