Code

Removed show_ldap_error() calls
[gosa.git] / gosa-plugins / kolab / personal / connectivity / kolab / 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;
23   var $view_logged  = FALSE;
24   var $uid          = "";
26   var $multiple_support = TRUE;
29   /*! \brief Initialize Plugin 
30       @param  $config   GOsa configuration object
31       @param  $dn       DN of the currently edited object
32       @param  $parent   The parent object, used to fetch Attributes.
33    */
34   function kolabAccount (&$config, $dn= NULL,$parent = NULL)
35   {
36     plugin::plugin ($config, $dn, $parent);
38     /* Setting uid to default */
39     if(isset($this->attrs['uid'][0])){
40       $this->uid = $this->attrs['uid'][0];
41     }
43     /* Pull arrays */
44     foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
45       if (isset($this->attrs["$attr"]["count"])){
46         $tmp = array();
47         for ($i= 0; $i<$this->attrs["$attr"]["count"]; $i++){
48           $tmp[]=$this->attrs["$attr"][$i];
49         }
50         $this->$attr = $tmp;
51       }
52     }
54     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
55     if (count ($this->kolabInvitationPolicy) == 0){
56        $this->kolabInvitationPolicy= array("ACT_MANUAL");
57     }
59     /* Check is account state */
60     $this->is_account = false;
61     if(count($this->kolabDelegate)){
62       $this->is_account = true;
63     }
64     foreach(array("calFBURL","unrestrictedMailSize") as $attr){
65       if(!empty($this->$attr)){
66         $this->is_account = true;
67       }
68     } 
70     /* Transfer account states for this union */
71     if (isset($this->parent->by_object['mailAccount']) && $this->parent->by_object['mailAccount']->is_account){
72       $this->mail_Account = true;
73      }elseif( isset($this->attrs) && isset($this->attrs['kolabHomeServer'])){
74        $this->mail_Account= true;
75      }else{
76        $this->is_account  = false;
77       $this->mail_Account = false;
78     }
79   }
81     
82   /*! \brief Create thml output 
83    */
84   function execute()
85   {
86           /* Call parent execute */
87         plugin::execute();
89     /* Log view */
90     if($this->is_account && !$this->view_logged){
91       $this->view_logged = TRUE;
92       new log("view","users/".get_class($this),$this->dn);
93     }
95     /* Show tab dialog headers */
96     $display= "";
98     /* Show main page */
99     $smarty= get_smarty();
101     /* Load attributes */
102     foreach($this->attributes as $val){
103       $smarty->assign("$val", $this->$val);
104     }
106     $tmp = $this->plInfo();
107     foreach($tmp['plProvidedAcls'] as $acl => $description){
108       $smarty->assign($acl."ACL",$this->getacl($acl,$this->ReadOnly));
109       $smarty->assign($acl."_W", $this->acl_is_writeable($acl,$this->ReadOnly));
110     }
111     $smarty->assign("is_account" ,  $this->is_account); 
113     if($this->ReadOnly){
114       $smarty->assign("is_removeable",false); 
115       $smarty->assign("is_createable",false); 
116     }else{
117       $smarty->assign("is_removeable",$this->acl_is_removeable()); 
118       $smarty->assign("is_createable",$this->acl_is_createable()); 
119     }
121     /* Check for invitation action */
122     $nr= 0;
123   
124     foreach($_POST as $name => $value){
125       if(preg_match("/add_inv_/",$name)){
126         $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
127       }
128     }
129     foreach($_POST as $name => $value){
130       if(preg_match("/del_inv_/",$name)){
132         $id = preg_replace("/del_inv_/","",$name);
133         $id = preg_replace("/_(x|y)$/","",$id);
134   
135         $new= array();
136         foreach ($this->kolabInvitationPolicy as $entry){
137           if (!preg_match("/^".$this->imapping[$id].":/", $entry)){
138             $new[]= $entry;
139           }
140         }
141         $this->kolabInvitationPolicy= $new;
142       }
143     }
145     /* Unify addresses */
146     $new= array();
147     foreach($this->kolabInvitationPolicy as $value){
148       $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
149       $new[$address]= $value;
150     }
151     $this->kolabInvitationPolicy= array();
152     foreach($new as $value){
153       $this->kolabInvitationPolicy[]= $value;
154     }
156     /* Add delegation */
157     if (isset($_POST['add_delegation'])){
158       if ($_POST['delegate_address'] != ""){
160         /* Valid email address specified? */
161         $address= $_POST['delegate_address'];
162         $valid= FALSE;
163         if (!tests::is_email($address)){
164           if (!tests::is_email($address, TRUE)){
165             msg_dialog::display(_("Error"), msgPool::invalid(_("Mail address"), "","", "example@your-domain.com"));
166           }
167         } else {
169           $ldap= $this->config->get_ldap_link();
170           $ldap->cd ($this->config->current['BASE']);
171           $ldap->search('(mail='.$address.')',array("mail"));
172           if ($ldap->count() == 0){
173             msg_dialog::display(_("Error"), msgPool::duplicated(_("Primary mail address")));
174           } else {
175             $valid= TRUE;
176           }
177         }
179         if ($valid){
180           /* Add it */
181           if ($this->acl_is_writeable("kolabDelegate")){
182             $this->addDelegate ($address);
183             $this->is_modified= TRUE;
184           }
186         }
187       }
188     }
190     /* Delete forward email addresses */
191     if ((isset($_POST['delete_delegation'])) && (isset($_POST['delegate_list']))){
192       if (count($_POST['delegate_list']) && $this->acl_is_writeable("kolabDelegate")){
193         $this->delDelegate ($_POST['delegate_list']);
194       }
195     }
197     /* Assemble policies */
198     $policies= array( 'ACT_ALWAYS_ACCEPT'       => _("Always accept"),
199                       'ACT_ALWAYS_REJECT'       => _("Always reject"),
200                       'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
201                       'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
202                       'ACT_MANUAL'              => _("Manual"));
203     $smarty->assign('policies', $policies);
205     /* Adjust checkbox */
206     if ($this->unrestrictedMailSize){
207       $smarty->assign('unrestrictedMailSizeState', "checked");
208     } else {
209       $smarty->assign('unrestrictedMailSizeState', "");
210     }
212     /* Transfer delegation list */
213     if (!count($this->kolabDelegate)){
214       /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
215       $smarty->assign("kolabDelegate", array());
216     } else {
217       $smarty->assign("kolabDelegate", $this->kolabDelegate);
218     }
220     $smarty->assign("mail_account",$this->mail_Account);
222     /* Create InvitationPolicy table */
223     $invitation= "";
224     $this->imapping= array();
225     $nr= 0;
226     $changeState  = "";
227     foreach ($this->kolabInvitationPolicy as $entry){
229       if($this->acl_is_writeable("kolabInvitationPolicy")){
230         $changeState .= "changeState('address".$nr."'); \n changeState('policy".$nr."'); \n
231           changeState('add".$nr."'); \n changeState('remove".$nr."'); \n";
232       }
234       if(!$this->acl_is_writeable("kolabInvitationPolicy") && !$this->multiple_support_active){
235         $dis = " disabled ";
236       }else{
237         if($this->is_account || $this->multiple_support_active){
238           $dis = " ";
239         }else{
240           $dis = " disabled ";
241         }
242       }
243       $invitation.= "<tr><td>";
245       if(!$this->acl_is_readable("kolabInvitationPolicy"))    {
246       }
248       /* The default entry does not have colons... */
249       if (!preg_match('/:/', $entry)){
250         $invitation.= _("Anonymous");
251         $name= "";
252         $mode= $entry;
253       } else {
254         $name= preg_replace('/:.*$/', '', $entry);
255         $mode= preg_replace('/^[^:]*: */', '', $entry);
257         if(!$this->acl_is_readable("kolabInvitationPolicy")){
258           $name='';
259         }
260         $invitation.= "<input name=\"address$nr\" size=16 maxlength=60  value=\"$name\" id='address".$nr."' ".$dis.">";
261       }
262       $invitation.= "</td>";
264       /* Add mode switch */
265       $invitation.= "<td><select size=\"1\" name=\"policy$nr\" id='policy".$nr."' ".$dis.">";
266       foreach($policies as $key => $value){
267         if ($key == $mode){
268           $invitation.= "<option value=\"$key\" selected>$value</option>";
269         } else {
270           $invitation.= "<option value=\"$key\">$value</option>";
271         }
272       }
273       
274       /* Assign buttons */
275       $button= "";
276       if ($nr == count($this->kolabInvitationPolicy)-1){
277         $button= "<input type='image' name=\"add_inv_".$nr."\" src='images/list_new.png' id='add".$nr."' ".$dis.">";
278       }
279       if ($nr != 0) {
280         $button.= "<input type='image' name=\"del_inv_".$nr."\" src='images/edittrash.png' id='remove".$nr."' ".$dis.">";
281       }
282       
283       $invitation.= "</select>&nbsp;</td>
284                     <td>$button</td>
285                   </tr>\n";
286       $this->imapping[$nr]= $name;
287       $nr++;
288     }
289     $smarty->assign("invitation", $invitation);
290     $smarty->assign("changeState", $changeState);
291     $smarty->assign("kolabState",$this->is_account);
293   
294     $smarty->assign("multiple_support",$this->multiple_support_active);
295     foreach($this->attributes as $attr){
296       $smarty->assign("use_".$attr,in_array($attr,$this->multi_boxes));
297     }
298     foreach(array("kolabState") as $attr){
299       $smarty->assign("use_".$attr,in_array($attr,$this->multi_boxes));
300     }
302     if($this->multiple_support_active){
303     }
306     $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
307     return ($display);
308   }
310   function remove_from_parent()
311   {
312     if(!$this->initially_was_account){
313       return;
314     }
315   
316     /* Optionally execute a command after we're done */
317     plugin::remove_from_parent();
318     
319     if(!in_array("kolabInetOrgPerson",$this->attrs['objectClass'])){
320       $this->attrs['objectClass'][] = "kolabInetOrgPerson";
321     }
323     $ldap = $this->config->get_ldap_linK(); 
324     $ldap->cd($this->config->current['BASE']);
325     $ldap->cd ($this->dn);
326     $ldap->modify($this->attrs);
328     /* Log last action */
329     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
331     $this->handle_post_events('remove',array("uid" => $this->uid));
332     if (!$ldap->success()){
333       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
334     }
335   }
338   function check()
339   {
340     /* Call common method to give check the hook */
341     $message= plugin::check();
343     /* FBFuture is in days... */
344     if ($this->kolabFreeBusyFuture != ""){
345       $message[]= msgPool::required(_("Free Busy future"));
346     }
347     if(!preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
348       $message[]= msgPool::invalid(_("Free Busy future"), $this->kolabFreeBusyFuture, "/^[0-9]+$/");
349     }
351     /* Check for URL scheme... */
352     if(!empty($this->calFBURL)){
353       $message[]= msgPool::required(_("Free Busy url"));
354     }
355     if(!preg_match("/^http+(s)*:\/\//",$this->calFBURL)){
356       $message[]= msgPool::invalid(_("Free Busy url"), $this->calFBURL, "/^http+(s)*:\/\//");
357     }
359     /* Check invitation policy for existing mail addresses */
360     foreach($this->kolabInvitationPolicy as $policy){
361       
362       /* Ignore anonymous string */
363       if (!preg_match('/:/', $policy)){
364         continue;
365       }
366       
367       $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
368       if (!tests::is_email($address)){
369         if (!tests::is_email($address, TRUE)){
370           $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
371         }
372       } else {
374         $ldap= $this->config->get_ldap_link();
375         $ldap->cd ($this->config->current['BASE']);
376         $ldap->search('(mail='.$address.')',array("mail"));
377         if ($ldap->count() == 0){
378           $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
379         } else {
380           $valid= TRUE;
381         }
382       }
383     }
385     return ($message);
386   }
388   /* Save data to object */
389   function save_object()
390   {
391     /* Do we need to flip is_account state? */
392     if (isset($_POST['connectivityTab'])){
394       if(isset($_POST["kolabState"])){
395         if($this->acl_is_createable()){
396           $this->is_account = true;
397         }
398       }else{
399         if($this->acl_is_removeable()){
400           $this->is_account = false;
401         }
402       }
403       if ($this->acl_is_writeable("unrestrictedMailSize")){
404         if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
405           $this->unrestrictedMailSize= 1;
406         } else {
407           $this->unrestrictedMailSize= 0;
408         }
409       }
410     }
412     plugin::save_object();
414     /* Save changes done in invitation policies */
415     if($this->acl_is_writeable("kolabInvitationPolicy")){
417       $nr= 0;
418       $this->kolabInvitationPolicy= array();
419       while (isset($_POST["policy$nr"])){
421         /* Anonymous? */
422         if (!isset($_POST["address$nr"])){
423           $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
424         } else {
425           $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
426         }
428         $nr++;
429       }
431       /* If this one is empty, preset with ACT_MANUAL for anonymous users */
432       if (count ($this->kolabInvitationPolicy) == 0){
433         $this->kolabInvitationPolicy= array("ACT_MANUAL");
434       }
435     }
436   }
439   /* Save to LDAP */
440   function save()
441   {
442     /* Check mailmethod before doing something useful */
443     plugin::save();
445     /* Transfer arrays */
446     $this->attrs['kolabDelegate']= $this->kolabDelegate;
447     $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
449     /* unrestrictedMailSize is boolean */
450     if($this->attrs['unrestrictedMailSize']){
451       $this->attrs['unrestrictedMailSize'] = "TRUE";
452     }else{
453       $this->attrs['unrestrictedMailSize'] = "FALSE";
454     }
455   
456     /* Write back to ldap */
457     $ldap= $this->config->get_ldap_link();
458     $ldap->cd($this->dn);
459     $this->cleanup();
460     $ldap->modify ($this->attrs); 
462     /* Log last action */
463     if($this->initially_was_account){
464       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
465     }else{
466       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
467     }
469     if (!$ldap->success()){
470       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
471     }
473     /* Optionally execute a command after we're done */
474     if ($this->initially_was_account == $this->is_account){
475       if ($this->is_modified){
476         $this->handle_post_events("modify",array("uid" => $this->uid));
477       }
478     } else {
479       $this->handle_post_events("add",array("uid" => $this->uid));
480     }
481   }
484   /* Add entry to delegation list */
485   function addDelegate($address)
486   {
487     $this->kolabDelegate[]= $address;
488     $this->kolabDelegate= array_unique ($this->kolabDelegate);
490     sort ($this->kolabDelegate);
491     reset ($this->kolabDelegate);
492     $this->is_modified= TRUE;
493   }
495   function delDelegate($addresses)
496   {
497     $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
498     $this->is_modified= TRUE;
499   }
502   /* Return plugin informations for acl handling  */
503   static function plInfo()
504   {
505     return (array(      
506           "plShortName"     => _("Kolab"),
507           "plDescription"   => _("Kolab account settings")."&nbsp;:&nbsp;<u>"._("Connectivity addon")."</u>",
508           "plSelfModify"    => TRUE,
509           "plDepends"       => array("user"),
510           "plPriority"      => 20,                                 // Position in tabs
511           "plSection"     => array("personal" => _("My account")),
512           "plCategory"    => array("users"),
513           "plOptions"       => array(),
515           "plProvidedAcls"  => array(
516             "kolabFreeBusyFuture"   => _("Free busy future"),
517             "unrestrictedMailSize"  => _("Mail size restriction"),
518             "calFBURL"              => _("Free busy information"),
519             "kolabDelegate"         => _("Delegations"),
520             "kolabInvitationPolicy" => _("Invitation policy"))
521           ));
522   }
525   /*! \brief  Enable multiple edit support. \
526               Also sets $this->mail_account to TRUE, which simulates \
527               a valid mail extension.
528    */
529   public function enable_multiple_support()
530   {
531     plugin::enable_multiple_support();
532     $this->mail_Account = TRUE;
533   }
536   /*! \brief  Save html POSTs in multiple edit.
537    */
538   public function multiple_save_object()
539   {
540     if (isset($_POST['connectivityTab'])){
541       plugin::multiple_save_object();
542       if(isset($_POST['use_kolabState'])){
543         $this->multi_boxes[] = "kolabState";
544       }
545       $this->save_object();
546     }
547   }
550   /*! \brief  Returns all modified values. \
551               All selected an modified values will be returned \
552               in an array.
553       @return array   Returns an array containing all attribute modifications  
554    */
555   public function get_multi_edit_values()
556   {
557     $ret = plugin::get_multi_edit_values();
558     if(in_array("kolabState",$this->multi_boxes)){
559       $ret['is_account'] = $this->is_account;
560     }
562     print_a($ret);
563   
564     return($ret);
565   }
567   /*! \brief  Sets modified attributes in mutliple edit. \
568               All collected values from "get_multi_edit_values()" \
569               will be applied to this plugin.
570       @param  array   An array containing modified attributes returned by get_multi_edit_values();
571    */
572   public function set_multi_edit_values($values)
573   {
574     plugin::set_multi_edit_values($values);
575     if(isset($values['is_account'])){
576       $this->is_account = $values['is_account'];
577     }
578   }
581   /*! \brief  Initialize multiple edit ui for this plugin. \
582               This function sets plugin defaults in multiple edit.
583       @param  array   Attributes used in all object 
584       @param  array   All used attributes.
585    */
586   public function init_multiple_support($attrs,$all)
587   {
588     plugin::init_multiple_support($attrs,$all);
589     if(isset($attrs['objectClass']) && in_array("kolabInetOrgPerson",$attrs['objectClass'])){
590       $this->is_account = TRUE;
591     }
593     /* Pull arrays */
594     foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
595       if (isset($this->multi_attrs["$attr"]["count"])){
596         $tmp = array();
597         for ($i= 0; $i<$this->multi_attrs["$attr"]["count"]; $i++){
598           $tmp[]=$this->multi_attrs["$attr"][$i];
599         }
600         $this->$attr = $tmp;
601       }
602     }
604     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
605     if (count ($this->kolabInvitationPolicy) == 0){
606        $this->kolabInvitationPolicy= array("ACT_MANUAL");
607     }
608   }
611 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
612 ?>