Code

Fixed undefined index in template adaption
[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     /* Get Boolean value */
55     $this->unrestrictedMailSize = FALSE;
56     if(isset($this->attrs['unrestrictedMailSize'][0]) && preg_match("/true/i",$this->attrs['unrestrictedMailSize'][0])){
57       $this->unrestrictedMailSize = TRUE;
58     }
60     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
61     if (count ($this->kolabInvitationPolicy) == 0){
62        $this->kolabInvitationPolicy= array("ACT_MANUAL");
63     }
65     /* Check is account state */
66     $this->is_account = false;
67     if(count($this->kolabDelegate)){
68       $this->is_account = true;
69     }
70     foreach(array("calFBURL") as $attr){
71       if(!empty($this->$attr)){
72         $this->is_account = true;
73       }
74     } 
76     /* Transfer account states for this union */
77     if (isset($this->parent->by_object['mailAccount']) && $this->parent->by_object['mailAccount']->is_account){
78       $this->mail_Account = true;
79      }elseif( isset($this->attrs) && isset($this->attrs['kolabHomeServer'])){
80        $this->mail_Account= true;
81      }else{
82        $this->is_account  = false;
83       $this->mail_Account = false;
84     }
85   }
87     
88   /*! \brief Create thml output 
89    */
90   function execute()
91   {
92           /* Call parent execute */
93         plugin::execute();
95     /* Log view */
96     if($this->is_account && !$this->view_logged){
97       $this->view_logged = TRUE;
98       new log("view","users/".get_class($this),$this->dn);
99     }
101     /* Show tab dialog headers */
102     $display= "";
104     /* Show main page */
105     $smarty= get_smarty();
107     /* Load attributes */
108     foreach($this->attributes as $val){
109       $smarty->assign("$val", $this->$val);
110     }
112     $tmp = $this->plInfo();
113     foreach($tmp['plProvidedAcls'] as $acl => $description){
114       $smarty->assign($acl."ACL",$this->getacl($acl,$this->ReadOnly));
115       $smarty->assign($acl."_W", $this->acl_is_writeable($acl,$this->ReadOnly));
116     }
117     $smarty->assign("is_account" ,  $this->is_account); 
119     if($this->ReadOnly){
120       $smarty->assign("is_removeable",false); 
121       $smarty->assign("is_createable",false); 
122     }else{
123       $smarty->assign("is_removeable",$this->acl_is_removeable()); 
124       $smarty->assign("is_createable",$this->acl_is_createable()); 
125     }
127     /* Check for invitation action */
128     $nr= 0;
129   
130     foreach($_POST as $name => $value){
131       if(preg_match("/add_inv_/",$name)){
132         $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
133       }
134     }
135     foreach($_POST as $name => $value){
136       if(preg_match("/del_inv_/",$name)){
138         $id = preg_replace("/del_inv_/","",$name);
139         $id = preg_replace("/_(x|y)$/","",$id);
140   
141         $new= array();
142         foreach ($this->kolabInvitationPolicy as $entry){
143           if (!preg_match("/^".$this->imapping[$id].":/", $entry)){
144             $new[]= $entry;
145           }
146         }
147         $this->kolabInvitationPolicy= $new;
148       }
149     }
151     /* Unify addresses */
152     $new= array();
153     foreach($this->kolabInvitationPolicy as $value){
154       $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
155       $new[$address]= $value;
156     }
157     $this->kolabInvitationPolicy= array();
158     foreach($new as $value){
159       $this->kolabInvitationPolicy[]= $value;
160     }
162     /* Add delegation */
163     if (isset($_POST['add_delegation'])){
164       if ($_POST['delegate_address'] != ""){
166         /* Valid email address specified? */
167         $address= $_POST['delegate_address'];
168         $valid= FALSE;
169         if (!tests::is_email($address)){
170           if (!tests::is_email($address, TRUE)){
171             msg_dialog::display(_("Error"), msgPool::invalid(_("Mail address"), "","", "your-domain@your-domain.com"));
172           }
173         } else {
175           $ldap= $this->config->get_ldap_link();
176           $ldap->cd ($this->config->current['BASE']);
177           $ldap->search('(mail='.$address.')',array("mail"));
178           if ($ldap->count() == 0){
179             msg_dialog::display(_("Error"), msgPool::duplicated(_("Primary mail address")));
180           } else {
181             $valid= TRUE;
182           }
183         }
185         if ($valid){
186           /* Add it */
187           if ($this->acl_is_writeable("kolabDelegate")){
188             $this->addDelegate ($address);
189             $this->is_modified= TRUE;
190           }
192         }
193       }
194     }
196     /* Delete forward email addresses */
197     if ((isset($_POST['delete_delegation'])) && (isset($_POST['delegate_list']))){
198       if (count($_POST['delegate_list']) && $this->acl_is_writeable("kolabDelegate")){
199         $this->delDelegate ($_POST['delegate_list']);
200       }
201     }
203     /* Assemble policies */
204     $policies= array( 'ACT_ALWAYS_ACCEPT'       => _("Always accept"),
205                       'ACT_ALWAYS_REJECT'       => _("Always reject"),
206                       'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
207                       'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
208                       'ACT_MANUAL'              => _("Manual"));
209     $smarty->assign('policies', $policies);
211     /* Adjust checkbox */
212     if ($this->unrestrictedMailSize){
213       $smarty->assign('unrestrictedMailSizeState', "checked");
214     } else {
215       $smarty->assign('unrestrictedMailSizeState', "");
216     }
218     /* Transfer delegation list */
219     if (!count($this->kolabDelegate)){
220       /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
221       $smarty->assign("kolabDelegate", array());
222     } else {
223       $smarty->assign("kolabDelegate", $this->kolabDelegate);
224     }
226     $smarty->assign("mail_account",$this->mail_Account);
228     /* Create InvitationPolicy table */
229     $invitation= "";
230     $this->imapping= array();
231     $nr= 0;
232     $changeState  = "";
233     foreach ($this->kolabInvitationPolicy as $entry){
235       if($this->acl_is_writeable("kolabInvitationPolicy")){
236         $changeState .= "changeState('address".$nr."'); \n changeState('policy".$nr."'); \n
237           changeState('add".$nr."'); \n changeState('remove".$nr."'); \n";
238       }
240       if(!$this->acl_is_writeable("kolabInvitationPolicy") && !$this->multiple_support_active){
241         $dis = " disabled ";
242       }else{
243         if($this->is_account || $this->multiple_support_active){
244           $dis = " ";
245         }else{
246           $dis = " disabled ";
247         }
248       }
249       $invitation.= "<tr><td>";
251       if(!$this->acl_is_readable("kolabInvitationPolicy"))    {
252       }
254       /* The default entry does not have colons... */
255       if (!preg_match('/:/', $entry)){
256         $invitation.= _("Anonymous");
257         $name= "";
258         $mode= $entry;
259       } else {
260         $name= preg_replace('/:.*$/', '', $entry);
261         $mode= preg_replace('/^[^:]*: */', '', $entry);
263         if(!$this->acl_is_readable("kolabInvitationPolicy")){
264           $name='';
265         }
266         $invitation.= "<input name=\"address$nr\" size=16 maxlength=60  value=\"$name\" id='address".$nr."' ".$dis.">";
267       }
268       $invitation.= "</td>";
270       /* Add mode switch */
271       $invitation.= "<td><select size=\"1\" name=\"policy$nr\" id='policy".$nr."' ".$dis.">";
272       foreach($policies as $key => $value){
273         if ($key == $mode){
274           $invitation.= "<option value=\"$key\" selected>$value</option>";
275         } else {
276           $invitation.= "<option value=\"$key\">$value</option>";
277         }
278       }
279       
280       /* Assign buttons */
281       $button= "";
282       if ($nr == count($this->kolabInvitationPolicy)-1){
283         $button= "<input type='image' name=\"add_inv_".$nr."\" src='images/lists/new.png' id='add".$nr."' ".$dis.">";
284       }
285       if ($nr != 0) {
286         $button.= "<input type='image' name=\"del_inv_".$nr."\" src='images/lists/trash.png' id='remove".$nr."' ".$dis.">";
287       }
288       
289       $invitation.= "</select>&nbsp;</td>
290                     <td>$button</td>
291                   </tr>\n";
292       $this->imapping[$nr]= $name;
293       $nr++;
294     }
295     $smarty->assign("invitation", $invitation);
296     $smarty->assign("changeState", $changeState);
297     $smarty->assign("kolabState",$this->is_account);
299   
300     $smarty->assign("multiple_support",$this->multiple_support_active);
301     foreach($this->attributes as $attr){
302       $smarty->assign("use_".$attr,in_array($attr,$this->multi_boxes));
303     }
304     foreach(array("kolabState") as $attr){
305       $smarty->assign("use_".$attr,in_array($attr,$this->multi_boxes));
306     }
308     if($this->multiple_support_active){
309     }
312     $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
313     return ($display);
314   }
316   function remove_from_parent()
317   {
318     if(!$this->initially_was_account){
319       return;
320     }
321   
322     /* Optionally execute a command after we're done */
323     plugin::remove_from_parent();
324     
325     if(!in_array("kolabInetOrgPerson",$this->attrs['objectClass'])){
326       $this->attrs['objectClass'][] = "kolabInetOrgPerson";
327     }
329     $ldap = $this->config->get_ldap_linK(); 
330     $ldap->cd($this->config->current['BASE']);
331     $ldap->cd ($this->dn);
332     $ldap->modify($this->attrs);
334     /* Log last action */
335     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
337     $this->handle_post_events('remove',array("uid" => $this->uid));
338     if (!$ldap->success()){
339       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
340     }
341   }
344   function check()
345   {
346     /* Call common method to give check the hook */
347     $message= plugin::check();
349     /* FBFuture is in days... */
350     if ($this->kolabFreeBusyFuture != ""){
351       $message[]= msgPool::required(_("Free Busy future"));
352     }
353     if(!preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
354       $message[]= msgPool::invalid(_("Free Busy future"), $this->kolabFreeBusyFuture, "/^[0-9]+$/");
355     }
357     /* Check for URL scheme... */
358     if(!empty($this->calFBURL)){
359       $message[]= msgPool::required(_("Free Busy url"));
360     }
361     if(!preg_match("/^http+(s)*:\/\//",$this->calFBURL)){
362       $message[]= msgPool::invalid(_("Free Busy url"), $this->calFBURL, "/^http+(s)*:\/\//");
363     }
365     /* Check invitation policy for existing mail addresses */
366     foreach($this->kolabInvitationPolicy as $policy){
367       
368       /* Ignore anonymous string */
369       if (!preg_match('/:/', $policy)){
370         continue;
371       }
372       
373       $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
374       if (!tests::is_email($address)){
375         if (!tests::is_email($address, TRUE)){
376           $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
377         }
378       } else {
380         $ldap= $this->config->get_ldap_link();
381         $ldap->cd ($this->config->current['BASE']);
382         $ldap->search('(mail='.$address.')',array("mail"));
383         if ($ldap->count() == 0){
384           $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
385         } else {
386           $valid= TRUE;
387         }
388       }
389     }
391     return ($message);
392   }
394   /* Save data to object */
395   function save_object()
396   {
397     /* Do we need to flip is_account state? */
398     if (isset($_POST['connectivityTab'])){
400       if(isset($_POST["kolabState"])){
401         if($this->acl_is_createable()){
402           $this->is_account = true;
403         }
404       }else{
405         if($this->acl_is_removeable()){
406           $this->is_account = false;
407         }
408       }
409       if ($this->acl_is_writeable("unrestrictedMailSize")){
410         if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
411           $this->unrestrictedMailSize= 1;
412         } else {
413           $this->unrestrictedMailSize= 0;
414         }
415       }
416     }
418     plugin::save_object();
420     /* Save changes done in invitation policies */
421     if($this->acl_is_writeable("kolabInvitationPolicy")){
423       $nr= 0;
424       $this->kolabInvitationPolicy= array();
425       while (isset($_POST["policy$nr"])){
427         /* Anonymous? */
428         if (!isset($_POST["address$nr"])){
429           $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
430         } else {
431           $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
432         }
434         $nr++;
435       }
437       /* If this one is empty, preset with ACT_MANUAL for anonymous users */
438       if (count ($this->kolabInvitationPolicy) == 0){
439         $this->kolabInvitationPolicy= array("ACT_MANUAL");
440       }
441     }
442   }
445   /* Save to LDAP */
446   function save()
447   {
448     /* Check mailmethod before doing something useful */
449     plugin::save();
451     /* Transfer arrays */
452     $this->attrs['kolabDelegate']= $this->kolabDelegate;
453     $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
455     /* unrestrictedMailSize is boolean */
456     if($this->attrs['unrestrictedMailSize']){
457       $this->attrs['unrestrictedMailSize'] = "TRUE";
458     }else{
459       $this->attrs['unrestrictedMailSize'] = "FALSE";
460     }
461   
462     /* Write back to ldap */
463     $ldap= $this->config->get_ldap_link();
464     $ldap->cd($this->dn);
465     $this->cleanup();
466     $ldap->modify ($this->attrs); 
468     /* Log last action */
469     if($this->initially_was_account){
470       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
471     }else{
472       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
473     }
475     if (!$ldap->success()){
476       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
477     }
479     /* Optionally execute a command after we're done */
480     if ($this->initially_was_account == $this->is_account){
481       if ($this->is_modified){
482         $this->handle_post_events("modify",array("uid" => $this->uid));
483       }
484     } else {
485       $this->handle_post_events("add",array("uid" => $this->uid));
486     }
487   }
490   /* Add entry to delegation list */
491   function addDelegate($address)
492   {
493     $this->kolabDelegate[]= $address;
494     $this->kolabDelegate= array_unique ($this->kolabDelegate);
496     sort ($this->kolabDelegate);
497     reset ($this->kolabDelegate);
498     $this->is_modified= TRUE;
499   }
501   function delDelegate($addresses)
502   {
503     $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
504     $this->is_modified= TRUE;
505   }
508   /* Return plugin informations for acl handling  */
509   static function plInfo()
510   {
511     return (array(      
512           "plShortName"     => _("Kolab"),
513           "plDescription"   => _("Kolab account settings")."&nbsp;:&nbsp;<u>"._("Connectivity addon")."</u>",
514           "plSelfModify"    => TRUE,
515           "plDepends"       => array("user"),
516           "plPriority"      => 20,                                 // Position in tabs
517           "plSection"     => array("personal" => _("My account")),
518           "plCategory"    => array("users"),
519           "plOptions"       => array(),
521           "plProvidedAcls"  => array(
522             "kolabFreeBusyFuture"   => _("Free busy future"),
523             "unrestrictedMailSize"  => _("Mail size restriction"),
524             "calFBURL"              => _("Free busy information"),
525             "kolabDelegate"         => _("Delegations"),
526             "kolabInvitationPolicy" => _("Invitation policy"))
527           ));
528   }
531   /*! \brief  Enable multiple edit support. \
532               Also sets $this->mail_account to TRUE, which simulates \
533               a valid mail extension.
534    */
535   public function enable_multiple_support()
536   {
537     plugin::enable_multiple_support();
538     $this->mail_Account = TRUE;
539   }
542   /*! \brief  Save html POSTs in multiple edit.
543    */
544   public function multiple_save_object()
545   {
546     if (isset($_POST['connectivityTab'])){
547       plugin::multiple_save_object();
548       if(isset($_POST['use_kolabState'])){
549         $this->multi_boxes[] = "kolabState";
550       }
551       $this->save_object();
552     }
553   }
556   /*! \brief  Returns all modified values. \
557               All selected an modified values will be returned \
558               in an array.
559       @return array   Returns an array containing all attribute modifications  
560    */
561   public function get_multi_edit_values()
562   {
563     $ret = plugin::get_multi_edit_values();
564     if(in_array("kolabState",$this->multi_boxes)){
565       $ret['is_account'] = $this->is_account;
566     }
568     print_a($ret);
569   
570     return($ret);
571   }
573   /*! \brief  Sets modified attributes in mutliple edit. \
574               All collected values from "get_multi_edit_values()" \
575               will be applied to this plugin.
576       @param  array   An array containing modified attributes returned by get_multi_edit_values();
577    */
578   public function set_multi_edit_values($values)
579   {
580     plugin::set_multi_edit_values($values);
581     if(isset($values['is_account'])){
582       $this->is_account = $values['is_account'];
583     }
584   }
587   /*! \brief  Initialize multiple edit ui for this plugin. \
588               This function sets plugin defaults in multiple edit.
589       @param  array   Attributes used in all object 
590       @param  array   All used attributes.
591    */
592   public function init_multiple_support($attrs,$all)
593   {
594     plugin::init_multiple_support($attrs,$all);
595     if(isset($attrs['objectClass']) && in_array("kolabInetOrgPerson",$attrs['objectClass'])){
596       $this->is_account = TRUE;
597     }
599     /* Pull arrays */
600     foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
601       if (isset($this->multi_attrs["$attr"]["count"])){
602         $tmp = array();
603         for ($i= 0; $i<$this->multi_attrs["$attr"]["count"]; $i++){
604           $tmp[]=$this->multi_attrs["$attr"][$i];
605         }
606         $this->$attr = $tmp;
607       }
608     }
610     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
611     if (count ($this->kolabInvitationPolicy) == 0){
612        $this->kolabInvitationPolicy= array("ACT_MANUAL");
613     }
614   }
616   /* Adapt from template, using 'dn' */
617   function adapt_from_template($dn, $skip= array())
618   {
619     plugin::adapt_from_template($dn, $skip);
621     /* Setting uid to default */
622     if(isset($this->attrs['uid'][0]) && !in_array("uid", $skip)){
623       $this->uid = $this->attrs['uid'][0];
624     }
626     /* Pull arrays */
627     foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
629       if (in_array($attr, $skip)){
630         continue;
631       }
633       if (isset($this->attrs["$attr"]["count"])){
634         $tmp = array();
635         for ($i= 0; $i<$this->attrs["$attr"]["count"]; $i++){
636           $tmp[]=$this->attrs["$attr"][$i];
637         }
638         $this->$attr = $tmp;
639       }
640     }
642     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
643     if (count ($this->kolabInvitationPolicy) == 0){
644       $this->kolabInvitationPolicy= array("ACT_MANUAL");
645     }
647     /* Get Boolean value */
648     $this->unrestrictedMailSize = FALSE;
649     if(isset($this->attrs['unrestrictedMailSize'][0]) && preg_match("/true/i",$this->attrs['unrestrictedMailSize'][0])){
650       $this->unrestrictedMailSize = TRUE;
651     }
653     /* Check is account state */
654     $this->is_account = false;
655     if(count($this->kolabDelegate)){
656       $this->is_account = true;
657     }
658     foreach(array("calFBURL") as $attr){
659       if(!empty($this->$attr)){
660         $this->is_account = true;
661       }
662     }
663   }
668 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
669 ?>