Code

Added logging to ogroup
[gosa.git] / plugins / personal / connectivity / class_kolabAccount.inc
1 <?php
2 class kolabAccount extends plugin
3 {
4   /* Definitions */
5   var $plHeadline       = "Kolab";
6   var $plDescription    = "This does something";
8   /* Kolab attributes */
9   var $kolabInvitationPolicy  = array();
10   var $kolabFreeBusyFuture    = 60;
11   var $unrestrictedMailSize   = 0;
12   var $calFBURL               = "";
13   var $kolabDelegate          = array();
15   /* attribute list for save action */
16   var $attributes     = array("kolabFreeBusyFuture", "unrestrictedMailSize", "calFBURL","kolabDelegate","kolabInvitationPolicy");
17   var $objectclasses  = array("kolabInetOrgPerson");
19   /* Helper */
20   var $imapping= array();
21   var $mail_Account   = false;
22   var $ReadOnly = false;
24   var $uid = "";
26   function kolabAccount ($config, $dn= NULL,$parent = NULL)
27   {
28     plugin::plugin ($config, $dn, $parent);
30     /* Setting uid to default */
31     if(isset($this->attrs['uid'][0])){
32       $this->uid = $this->attrs['uid'][0];
33     }
35     /* Pull arrays */
36     foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
37       if (isset($this->attrs["$attr"]["count"])){
38         $tmp = array();
39         for ($i= 0; $i<$this->attrs["$attr"]["count"]; $i++){
40           $tmp[]=$this->attrs["$attr"][$i];
41         }
42         $this->$attr = $tmp;
43       }
44     }
46     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
47     if (count ($this->kolabInvitationPolicy) == 0){
48        $this->kolabInvitationPolicy= array("ACT_MANUAL");
49     }
51     /* Check is account state */
52     $this->is_account = false;
53     if(count($this->kolabDelegate)){
54       $this->is_account = true;
55     }
56     foreach(array("calFBURL","unrestrictedMailSize") as $attr){
57       if(!empty($this->$attr)){
58         $this->is_account = true;
59       }
60     } 
62     /* Transfer account states for this union */
63     if (isset($this->parent) && $this->parent->by_object['mailAccount']->is_account){
64       $this->mail_Account = true;
65      }elseif( isset($this->attrs) && isset($this->attrs['kolabHomeServer'])){
66        $this->mail_Account= true;
67      }else{
68        $this->is_account  = false;
69       $this->mail_Account = false;
70     }
72     /* Log view */
73     if($this->is_account){
74       @log::log("view","users/".get_class($this),$this->dn);
75     }
76   }
79   function execute()
80   {
81         /* Call parent execute */
82         plugin::execute();
84     /* Show tab dialog headers */
85     $display= "";
87     /* Show main page */
88     $smarty= get_smarty();
90     /* Load attributes */
91     foreach($this->attributes as $val){
92       $smarty->assign("$val", $this->$val);
93     }
95     $tmp = $this->plInfo();
96     foreach($tmp['plProvidedAcls'] as $acl => $description){
97       $smarty->assign($acl."ACL",$this->getacl($acl,$this->ReadOnly));
98       $smarty->assign($acl."_W", $this->acl_is_writeable($acl,$this->ReadOnly));
99     }
100     $smarty->assign("is_account" ,  $this->is_account); 
102     if($this->ReadOnly){
103       $smarty->assign("is_removeable",false); 
104       $smarty->assign("is_createable",false); 
105     }else{
106       $smarty->assign("is_removeable",$this->acl_is_removeable()); 
107       $smarty->assign("is_createable",$this->acl_is_createable()); 
108     }
110     /* Check for invitation action */
111     $nr= 0;
112     while (isset($_POST["policy$nr"])){
113       if (isset($_POST["add$nr"])){
114         $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
115       }
116       if (isset($_POST["remove$nr"])){
117         $new= array();
118         foreach ($this->kolabInvitationPolicy as $entry){
119           if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
120             $new[]= $entry;
121           }
122         }
123         $this->kolabInvitationPolicy= $new;
124       }
125       $nr++;
126     }
128     /* Unify addresses */
129     $new= array();
130     foreach($this->kolabInvitationPolicy as $value){
131       $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
132       $new[$address]= $value;
133     }
134     $this->kolabInvitationPolicy= array();
135     foreach($new as $value){
136       $this->kolabInvitationPolicy[]= $value;
137     }
139     /* Add delegation */
140     if (isset($_POST['add_delegation'])){
141       if ($_POST['delegate_address'] != ""){
143         /* Valid email address specified? */
144         $address= $_POST['delegate_address'];
145         $valid= FALSE;
146         if (!is_email($address)){
147           if (!is_email($address, TRUE)){
148               print_red (_("You're trying to add an invalid email address to the list of delegations."));
149           }
150         } else {
152           $ldap= $this->config->get_ldap_link();
153           $ldap->cd ($this->config->current['BASE']);
154           $ldap->search('(mail='.$address.')',array("mail"));
155           if ($ldap->count() == 0){
156             print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
157           } else {
158             $valid= TRUE;
159           }
160         }
162         if ($valid){
163           /* Add it */
164           if ($this->acl_is_writeable("kolabDelegate")){
165             $this->addDelegate ($address);
166             $this->is_modified= TRUE;
167           }
169         }
170       }
171     }
173     /* Delete forward email addresses */
174     if ((isset($_POST['delete_delegation'])) && (isset($_POST['delegate_list']))){
175       if (count($_POST['delegate_list']) && $this->acl_is_writeable("kolabDelegate")){
176         $this->delDelegate ($_POST['delegate_list']);
177       }
178     }
180     /* Assemble policies */
181     $policies= array( 'ACT_ALWAYS_ACCEPT'       => _("Always accept"),
182                       'ACT_ALWAYS_REJECT'       => _("Always reject"),
183                       'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
184                       'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
185                       'ACT_MANUAL'              => _("Manual"));
186     $smarty->assign('policies', $policies);
188     /* Adjust checkbox */
189     if ($this->unrestrictedMailSize){
190       $smarty->assign('unrestrictedMailSizeState', "checked");
191     } else {
192       $smarty->assign('unrestrictedMailSizeState', "");
193     }
195     /* Transfer delegation list */
196     if (!count($this->kolabDelegate)){
197       /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
198       $smarty->assign("kolabDelegate", array());
199     } else {
200       $smarty->assign("kolabDelegate", $this->kolabDelegate);
201     }
203     $smarty->assign("mail_account",$this->mail_Account);
205     /* Create InvitationPolicy table */
206     $invitation= "";
207     $this->imapping= array();
208     $nr= 0;
209     $changeState  = "";
210     foreach ($this->kolabInvitationPolicy as $entry){
212       if($this->acl_is_writeable("kolabInvitationPolicy")){
213         $changeState .= "changeState('address".$nr."'); \n changeState('policy".$nr."'); \n
214           changeState('add".$nr."'); \n changeState('remove".$nr."'); \n";
215       }
217       if(!$this->acl_is_writeable("kolabInvitationPolicy")){
218         $dis = " disabled ";
219       }else{
220         if($this->is_account){
221           $dis = " ";
222         }else{
223           $dis = " disabled ";
224         }
225       }
226       $invitation.= "<tr><td>";
228       if(!$this->acl_is_readable("kolabInvitationPolicy"))    {
229       }
231       /* The default entry does not have colons... */
232       if (!preg_match('/:/', $entry)){
233         $invitation.= _("Anonymous");
234         $name= "";
235         $mode= $entry;
236       } else {
237         $name= preg_replace('/:.*$/', '', $entry);
238         $mode= preg_replace('/^[^:]*: */', '', $entry);
240         if(!$this->acl_is_readable("kolabInvitationPolicy")){
241           $name='';
242         }
243         $invitation.= "<input name=\"address$nr\" size=16 maxlength=60  value=\"$name\" id='address".$nr."' ".$dis.">";
244       }
245       $invitation.= "</td>";
247       /* Add mode switch */
248       $invitation.= "<td><select size=\"1\" name=\"policy$nr\" id='policy".$nr."' ".$dis.">";
249       foreach($policies as $key => $value){
250         if ($key == $mode){
251           $invitation.= "<option value=\"$key\" selected>$value</option>";
252         } else {
253           $invitation.= "<option value=\"$key\">$value</option>";
254         }
255       }
256       
257       /* Assign buttons */
258       $button= "";
259       if ($nr == count($this->kolabInvitationPolicy)-1){
260         $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\" id='add".$nr."' ".$dis.">";
261       }
262       if ($nr != 0) {
263         $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\" id='remove".$nr."' ".$dis.">";
264       }
265       
266       $invitation.= "</select>&nbsp;$button</td></tr>\n";
267       $this->imapping[$nr]= $name;
268       $nr++;
269     }
270     $smarty->assign("invitation", $invitation);
271     $smarty->assign("changeState", $changeState);
272     $smarty->assign("kolabState",$this->is_account);
273     $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
276     return ($display);
277   }
279   function remove_from_parent()
280   {
281     if(!$this->initially_was_account){
282       return;
283     }
284   
285     /* Optionally execute a command after we're done */
286     plugin::remove_from_parent();
287     
288     if(!in_array("kolabInetOrgPerson",$this->attrs['objectClass'])){
289       $this->attrs['objectClass'][] = "kolabInetOrgPerson";
290     }
292     $ldap = $this->config->get_ldap_linK(); 
293     $ldap->cd($this->config->current['BASE']);
294     $ldap->cd ($this->dn);
295     $ldap->modify($this->attrs);
297     /* Log last action */
298     @log::log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
300     $this->handle_post_events('remove',array("uid" => $this->uid));
301     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/kolab account with dn '%s' failed."),$this->dn));
302   }
305   function check()
306   {
307     /* Call common method to give check the hook */
308     $message= plugin::check();
310     /* FBFuture is in days... */
311     if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
312       $message[]= _("The value specified as Free Busy future needs to be an integer.");
313     }
315     /* Check for URL scheme... */
316     if(!empty($this->calFBURL) && !preg_match("/http+(s)*:\/\//",$this->calFBURL)){
317       $message[]= _("The value specified as Free Busy Information URL is invalid.");
318     }
320     /* Check invitation policy for existing mail addresses */
321     foreach($this->kolabInvitationPolicy as $policy){
322       
323       /* Ignore anonymous string */
324       if (!preg_match('/:/', $policy)){
325         continue;
326       }
327       
328       $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
329       if (!is_email($address)){
330         if (!is_email($address, TRUE)){
331           $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
332         }
333       } else {
335         $ldap= $this->config->get_ldap_link();
336         $ldap->cd ($this->config->current['BASE']);
337         $ldap->search('(mail='.$address.')',array("mail"));
338         if ($ldap->count() == 0){
339           $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
340         } else {
341           $valid= TRUE;
342         }
343       }
344     }
346     return ($message);
347   }
349   /* Save data to object */
350   function save_object()
351   {
352     /* Do we need to flip is_account state? */
353     if (isset($_POST['connectivityTab'])){
355       if(isset($_POST["kolabState"])){
356         if($this->acl_is_createable()){
357           $this->is_account = true;
358         }
359       }else{
360         if($this->acl_is_removeable()){
361           $this->is_account = false;
362         }
363       }
364       
365       if ($this->acl_is_writeable("unrestrictedMailSize")){
366         if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
367           $this->unrestrictedMailSize= 1;
368         } else {
369           $this->unrestrictedMailSize= 0;
370         }
371       }
372     }
374     plugin::save_object();
376     /* Save changes done in invitation policies */
377     if($this->acl_is_writeable("kolabInvitationPolicy")){
378       $nr= 0;
379       $this->kolabInvitationPolicy= array();
380       while (isset($_POST["policy$nr"])){
382         /* Anonymous? */
383         if (!isset($_POST["address$nr"])){
384           $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
385         } else {
386           $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
387         }
389         $nr++;
390       }
392       /* If this one is empty, preset with ACT_MANUAL for anonymous users */
393       if (count ($this->kolabInvitationPolicy) == 0){
394         $this->kolabInvitationPolicy= array("ACT_MANUAL");
395       }
396     }
397   }
400   /* Save to LDAP */
401   function save()
402   {
403     /* Check mailmethod before doing something useful */
404     plugin::save();
406     /* Transfer arrays */
407     $this->attrs['kolabDelegate']= $this->kolabDelegate;
408     $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
410     /* unrestrictedMailSize is boolean */
411     if($this->attrs['unrestrictedMailSize']){
412       $this->attrs['unrestrictedMailSize'] = "TRUE";
413     }else{
414       $this->attrs['unrestrictedMailSize'] = "FALSE";
415     }
416   
417     /* Write back to ldap */
418     $ldap= $this->config->get_ldap_link();
419     $ldap->cd($this->dn);
420     $this->cleanup();
421     $ldap->modify ($this->attrs); 
423     /* Log last action */
424     if($this->initially_was_account){
425       @log::log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
426     }else{
427       @log::log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
428     }
430     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/kolab account with dn '%s' failed."),$this->dn));
432     /* Optionally execute a command after we're done */
433     if ($this->initially_was_account == $this->is_account){
434       if ($this->is_modified){
435         $this->handle_post_events("modify",array("uid" => $this->uid));
436       }
437     } else {
438       $this->handle_post_events("add",array("uid" => $this->uid));
439     }
440   }
443   /* Add entry to delegation list */
444   function addDelegate($address)
445   {
446     $this->kolabDelegate[]= $address;
447     $this->kolabDelegate= array_unique ($this->kolabDelegate);
449     sort ($this->kolabDelegate);
450     reset ($this->kolabDelegate);
451     $this->is_modified= TRUE;
452   }
454   function delDelegate($addresses)
455   {
456     $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
457     $this->is_modified= TRUE;
458   }
461   /* Return plugin informations for acl handling  */
462   function plInfo()
463   {
464     return (array(      
465           "plShortName"     => _("Kolab"),
466           "plDescription"   => _("Kolab account settings")."&nbsp;:&nbsp;<u>"._("Connectivity addon")."</u>",
467           "plSelfModify"    => TRUE,
468           "plDepends"       => array("user"),
469           "plPriority"      => 20,                                 // Position in tabs
470           "plSection"     => array("personal" => _("My account")),
471           "plCategory"    => array("users"),
472           "plOptions"       => array(),
474           "plProvidedAcls"  => array(
475             "kolabFreeBusyFuture"   => _("Free busy future"),
476             "unrestrictedMailSize"  => _("Mail size restriction"),
477             "calFBURL"              => _("Free busy information"),
478             "kolabDelegate"         => _("Delegations"),
479             "kolabInvitationPolicy" => _("Invitation policy"))
480           ));
481   }
484 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
485 ?>