Code

Switched log call
[gosa.git] / plugins / addons / addressbook / class_addressbook.inc
1 <?php
3 class addressbook extends plugin
4 {
5         /* Definitions */
6         var $plHeadline   = "Addressbook";
7         var $plDescription= "This does something";
9   /* Generic */
10   var $ui;  
12         /* Phonelist attributes */
13         var $telephone_list   = array();
14         var $new_dn           = "";
15         var $orig_cn          = "";
16         var $storage_base     = "";
17         var $orig_storage_base= "";
19   /* Filter attributes */
20         var $start            = 0;
21         var $search_for       = "*";
22         var $search_base      = "";
23         var $search_type      = "";
24         var $range            = 20;
26   /* Currently edited/added entry attributes */
27         var $sn                       = "";
28         var $cn                       = "";
29         var $givenName                = "";
30         var $mail                     = "";
31         var $title                    = "";
32         var $personalTitle            = "";
33         var $initials                 = "";
34         var $homePostalAddress        = "";
35         var $homePhone                = "";
36         var $mobile                   = "";
37         var $o                        = "";
38         var $postalAddress            = "";
39         var $l                        = "";
40         var $postalCode               = "";
41         var $st                       = "";
42         var $ou                       = "";
43         var $telephoneNumber          = "";
44         var $facsimileTelephoneNumber = "";
45         var $pager                    = "";
46   var $view_logged              = FALSE;
48         /* attribute list for save action */
49         var $attributes= array("sn", "givenName", "mail", "title",
50                         "initials", "homePostalAddress", "displayName",
51                         "homePhone", "mobile", "o", "postalAddress", "l",
52                         "postalCode", "st", "ou", "telephoneNumber",
53                         "facsimileTelephoneNumber", "pager");
55         var $objectclasses= array("top", "person", "organizationalPerson", "inetOrgPerson");
57         var $abobjectclass= "dc=addressbook";
59   function addressbook ($config, $dn= NULL)
60   {
61     /* Include config object */
62     $this->config= $config;
64     /* Check if there is a special ldap-sub-tree specified, instead of dc=addressbook, */
65     $aoc = search_config($this->config->data['MENU'], "addressbook", "LDAP_OBJECT_CLASS");
66     if ($aoc != ""){
67       $this->abobjectclass  = $aoc;
68     }
70     /* Get global filter config */
71     if (!is_global("phonefilter")){
72       $ui         = get_userinfo();
73       $base       = get_base_from_people($ui->dn);
74       $phonefilter= array(
75           "search_base"       => $base,
76           "organizational"    => "checked",
77           "global"            => "checked",
78           "search_for"        => "*",
79           "object_type"       => "*");
80       register_global("phonefilter", $phonefilter);
81     }
82      
83     $this->ui = get_userinfo();
84   }
86   function execute()
87   {
88     /* Call parent execute */
89     plugin::execute();
91     $smarty= get_smarty();
93     /* Prevent empty variables for smarty */
94     foreach($this->attributes as $atr) {
95       $smarty->assign($atr,"");
96     }
98     /* Save formular information */
99     $phonefilter= get_global("phonefilter");
100     foreach( array("search_for", "search_base", "object_type") as $type){
101       if (isset($_POST[$type])){
102         $phonefilter[$type]= $_POST[$type];
103       }
104       $this->$type= $phonefilter[$type];
105     }
106     if (isset($_POST['search_base'])){
107       foreach( array("organizational", "global") as $type){
108         if (isset($_POST[$type])){
109           $phonefilter[$type]= "checked";
110         } else {
111           $phonefilter[$type]= "";
112         }
113       }
114     }
116     /* Search string */
117     $s= $phonefilter['search_for'];
118     if ($s == "") {
119       $s= "*";
120     }
121     if (isset($_GET['search'])){
122       $s= validate(mb_substr($_GET['search'], 0, 1, "UTF8"))."*";
123       if ($s == "**"){
124         $s= "*";
125       }
126       $this->search_for= $s;
127       $phonefilter['search_for']= $s;
128     }
129     register_global("phonefilter", $phonefilter);
131     /* Assign create acl */
132     $acl = $this->get_entry_acls($this->abobjectclass.",".$phonefilter['search_base']);
133     $smarty->assign("internal_createable", preg_match("/c/",$acl));
134     $smarty->assign("internal_removeable", preg_match("/d/",$acl));
135     $smarty->assign("internal_editable",   preg_match("/w/",$acl));
137     /* Perform actions with CTI hook */
138     if (isset($_GET['target'])
139         && isset($_GET['dial'])
140         && isset($this->config->current['CTIHOOK'])){
142       $dialmode= $_GET['dial'];
143       if ($dialmode == "telephoneNumber" ||
144           $dialmode == "mobile" ||
145           $dialmode == "homePhone"){
147         /* Get target */
148         $ldap= $this->config->get_ldap_link();
149         $ldap->cat(base64_decode($_GET['target']), array('telephoneNumber', 'mobile', 'homePhone'));
150         $attrs= $ldap->fetch();
151         if (isset($attrs["$dialmode"])){
152           $target= $attrs[$dialmode][0];
153         } else {
154           $target= "";
155         }
157         /* Get source */
158         $ui= get_userinfo();
159         $ldap->cat($ui->dn, array('telephoneNumber'));
160         $attrs= $ldap->fetch();
161         if (isset($attrs["telephoneNumber"])){
162           $source= $attrs['telephoneNumber'][0];
163         } else {
164           $source= "";
165         }
167         /* Save to session */
168         $_SESSION['source']= $source;
169         $_SESSION['target']= $target;
171         /* Perform call */
172         if ($target != "" && $source != ""){
173           $smarty->assign("phone_image", get_template_path('images/phone.png'));
174           $smarty->assign("dial_info", sprintf(_("Dial from %s to %s now?"), "<b style='font-size:22px; color:red'>".$source."</b>", "<b style='font-size:22px;color:red'>".$target."</b>"));
175           return($smarty->fetch(get_template_path('dial.tpl', TRUE)));
176           return;
177         } else {
178           print_red (_("You have no personal phone number set. Please change that in order to perform direct dials."));
179         }
180       }
182     }
184     /* Finally dial */
185     if (isset($_POST['dial']) && isset($_SESSION['source']) && isset($_SESSION['target'])){
186       exec ($this->config->current['CTIHOOK']." '".$_SESSION['source']."' '".$_SESSION['target']."'", $dummy, $retval);
187       unset($_SESSION['source']);
188       unset($_SESSION['target']);
189     }
192     /* Delete entry? */
193     if (isset($_POST['delete_entry_confirm'])){
195       /* Some nice guy may send this as POST, so we've to check
196          for the permissions again. */
197       
198       $acl = $this->get_entry_acls($this->dn);
199       if(preg_match("/d/",$acl)){
201         /* Delete request is permitted, perform LDAP action */
202         $ldap= $this->config->get_ldap_link();
203         $ldap->rmdir ($this->dn);
204         new log("remove","addressbook/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
205         show_ldap_error($ldap->get_error(), sprintf(_("Removing of addressbook entry '%s' failed."),$this->dn));
206         new log("remove","addressbook/".get_class($this),$this->dn,array(),"Address book object'".$this->dn."' has been removed");
207   
208       } else {
210         /* Normally this shouldn't be reached, send some extra
211            logs to notify the administrator */
212         print_red (_("You are not allowed to delete this entry!"));
213         new log("remove","addressbook/".get_class($this),$this->dn,array(),"Warning: '".$this->ui->uid."' tried to trick address book deletion.");
214       }
216       /* Remove lock file after successfull deletion */
217       del_lock ($this->dn);
219       /* Clean up */
220       if (isset($_SESSION['saved_start'])){
221         $_GET['start']= $_SESSION['saved_start'];
222       }
223       unset($_SESSION['show_info']);
224       unset($_SESSION['saved_start']);
225     }
228     /* Delete entry? */
229     if (isset($_POST['delete_cancel'])){
230       del_lock ($this->dn);
231     }
234     /* Save address entry? */
235     if (isset($_POST['save'])){
236       $this->save_object();
237       $this->storage_base= $_POST['storage_base'];
239       /* Perform checks */
240       $message= $this->check ();
242       /* No errors, save object */
243       if (count ($message) == 0){
244         $this->save();
246         /* Clean up */
247         if (isset($_SESSION['saved_start'])){
248           $_GET['start']= $_SESSION['saved_start'];
249         }
250         $_SESSION['show_info']= $this->dn;
251         unset($_SESSION['saved_start']);
252       } else {
253         /* Errors found, show message */
254         show_errors ($message);
255       }
256     }
259     /* Close info window */
260     if (isset($_GET['close']) || isset($_POST['cancel'])){
261       if (isset($_SESSION['saved_start'])){
262         $_GET['start']= $_SESSION['saved_start'];
263       }
264       unset($_SESSION['show_info']);
265       unset($_SESSION['saved_start']);
266     }
269     /* Start address book edit mode? */
270     if (isset($_GET['global'])){
271       if (!isset($_SESSION['saved_start']) && isset($_GET['start'])){
272         $_SESSION['saved_start']= $_GET['start'];
273       }
274       switch ($_GET['global']){
275         case "add":
276           $this->dn= "new";
277         $this->orig_cn= "";
279         /* Clean values */
280         foreach ($this->attributes as $name){
281           $this->$name= "";
282         }
283         $this->saved_attributes= array();
284         $this->storage_base= $this->config->current["BASE"];
285         break;
287         case "edit":
288           /* Clean values */
289           foreach ($this->attributes as $name){
290             $this->$name= "";
291           }
292         $this->dn= $_SESSION['show_info']; 
293         $this->load();
294         $this->orig_cn= $this->cn;
295         break;
296         case "remove":
297           $this->dn= $_SESSION['show_info']; 
298         $this->load();
300         /* Load permissions for selected 'dn' and check if
301            we're allowed to remove this 'dn' */
302         $acl = $this->get_entry_acls($this->dn);
303         if(preg_match("/d/",$acl)){
305           /* Check locking, save current plugin in 'back_plugin', so
306              the dialog knows where to return. */
307           if (($user= get_lock($this->dn)) != ""){
308             return(gen_locked_message ($user, $this->dn));
309           }
311           /* Lock the current entry, so nobody will edit it during deletion */
312           $ui= get_userinfo();
313           add_lock ($this->dn, $ui->dn);
314           $smarty->assign("info", sprintf(_("You're about to delete the entry %s."), $this->dn));
315           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
316         } else {
318           /* Obviously the user isn't allowed to delete. Show message and
319              clean session. */
320           print_red (_("You are not allowed to delete this entry!"));
321         }
322       }
323       $_SESSION['show_info']= "ADD";
324     }
327     /* Open info window */
328     if (isset($_GET['show'])){
329       if (!isset($_SESSION['saved_start'])){
330         $_SESSION['saved_start']= $_GET['start'];
331       }
332       $this->dn = base64_decode($_GET['show']);
333       $this->view_logged =FALSE;
334       if(!$this->view_logged){
335         $this->view_logged = TRUE;
336         new log("view","addressbook/".get_class($this),$this->dn);
337       }
338       $_SESSION['show_info']= base64_decode($_GET['show']);
339     }
342     /* Get ldap link / build filter */
343     $ldap= $this->config->get_ldap_link();
344     $this->telephone_list= array ();
347     /* Assemble bases 
348         (Depending on checkboxes, we search for organisational entries or seperated 
349         adressbook entries within dc=adressbook, ) */
350     $bases= array();
351     $filter= "";
352     if ($phonefilter['global'] == "checked"){
353       $bases[]= preg_replace("/".$this->config->current['BASE']."/", $this->abobjectclass.",".$this->config->current['BASE'], $this->search_base);
354     } else {
355       $filter= '(objectClass=gosaAccount)';
356     }
357     if ($phonefilter['organizational'] == "checked"){
358       $bases[]= $this->search_base;
359     }
362     /* Only display those entries that have at least on of this attributes set */
363     $must_have_this = array("telephoneNumber","facsimileTelephoneNumber","mobile","homePhone","mail");
365     /* Requested attributes in ldap search */
366     $attributes = array("sn", "givenName", "telephoneNumber", "facsimileTelephoneNumber", "mobile", "homePhone", "uid", "mail", "cn");
368     /* Create attribute filter part */
369     $attribute_filter = "";
370     foreach($attributes as $att){
371       $attribute_filter .= "(".$att."=".$s.")";
372     }
374     /* Walk through bases an check for usable entries */
375     foreach ($bases as $base){
377       $ldap->cd ($base);
379       if ($phonefilter['object_type'] == '*'){
380         $ldap->search (
381             "(&(objectClass=person)$filter(!(objectClass=gosaUserTemplate))".   // Skip templates etc ..
382             "(!(uid=*$))".                                                      // Skip entries with ...$ as uid 
383             "(|".$attribute_filter."))"                                         
384           ,$attributes); 
385       } else {
386         $ldap->search ("(&$filter(!(uid=*$))(!(objectClass=gosaUserTemplate))".                     //array
387             "(".$phonefilter['object_type']."=$s))", $attributes);
388       }
390       /* Walk through LDAP results */
391       while ($attrs= $ldap->fetch()){
393         /* prevent empty vaiables */
394         foreach($this->attributes as $atr)          {
395           if(!isset($attrs[$atr][0])) {
396             $attrs[$atr][0] = "";
397           }
398         }
400         /* Check if page number was posted */
401         if(!isset($_GET['start'])) {
402           $_GET['start']="";
403         }
405         /* Check if at least one attribute is specified */
406         $skip = false;
408         foreach($must_have_this as $attr)      {
409           if(isset($attrs[$attr][0]) && !empty($attrs[$attr][0])){
410             $skip =false;
411             break;
412           }
413         }
415         /* Skip all attributes that we are not allowed to read */
416         $any = false;
417         foreach($attributes as $attr){
418     
419           $acls = $this->get_entry_acls($attrs['dn'],$attr);  
420           if(!preg_match("/r/",$acls)){
421             $attrs[$attr][0] = "";
422           }else{
423             $any = true;
424           }
425         }
426  
427         /* Only show lines that have set any mail or phone informations */
428         if(!$skip && $any){      
429   
430           $this->telephone_list[$attrs['sn'][0].$attrs['dn']]=
432             "<td class=\"phonelist\" title=\"".$attrs['sn'][0].", ".$attrs['givenName'][0]."\" onClick='location.href=\"main.php?plug=".validate($_GET['plug'])."&amp;start=".validate($_GET['start'])."&amp;show=".base64_encode($attrs['dn'])."\"'><a style='vertical-align:middle;' href=\"main.php?plug=".validate($_GET['plug'])."&amp;start=".validate($_GET['start'])."&amp;show=".base64_encode($attrs['dn'])."\">".$attrs['sn'][0].", ".$attrs['givenName'][0].
433             "</a>
434             </td>
435             <td class=\"phonelist\" title=\""._("Dial")." ".$attrs['telephoneNumber'][0]."\">
436             <a style='vertical-align:middle;' href=\"main.php?plug=".validate($_GET['plug'])."&amp;dial=telephoneNumber&amp;start=".validate($_GET['start'])."&amp;target=".base64_encode($attrs['dn'])."\">".$attrs['telephoneNumber'][0]."
437             </a>
438             </td>
439             <td class=\"phonelist\" title=\"".$attrs['facsimileTelephoneNumber'][0]."\">
440             ".$attrs['facsimileTelephoneNumber'][0]."
441             </td>
442             <td class=\"phonelist\" title=\""._("Dial")." ".$attrs['mobile'][0]."\">
443             <a style='vertical-align:middle;' href=\"main.php?plug=".validate($_GET['plug'])."&amp;dial=mobile&amp;start=".validate($_GET['start'])."&amp;target=".base64_encode($attrs['dn'])."\">".$attrs['mobile'][0]."
444             </a>
445             </td>
446             <td class=\"phonelist\" title=\""._("Dial")." ".$attrs['homePhone'][0]."\">
447             <a style='vertical-align:middle;' href=\"main.php?plug=".validate($_GET['plug'])."&amp;dial=homePhone&amp;start=".validate($_GET['start'])."&amp;target=".base64_encode($attrs['dn'])."\">".$attrs['homePhone'][0]."
448             </a>
449             </td>
450             <td>
451             <a href=\"getvcard.php?dn=".base64_encode($attrs['dn'])."\">
452             <img align=\"top\" border=0 src=\"images/save.png\" alt=\"vcf\" title=\"".sprintf(_("Save contact for %s as vcard"), $attrs['givenName'][0]." ".$attrs['sn'][0])."\">
453             </a>";
456           if(preg_match("/r/",$this->get_entry_acls($attrs['dn'],"mail"))){
457             if (isset($attrs['mail'][0]) && !empty($attrs['mail'][0])){
458               $dest= sprintf(_("Send mail to %s"), $attrs['mail'][0]);
459               $this->telephone_list[$attrs['sn'][0].$attrs['dn']].=
461                 "<a href=\"mailto:".htmlentities($attrs['mail'][0])."\">".
462                 "<img  align=\"top\" border=0 src=\"images/mailto.png\" alt=\"vcf\" title=\"$dest\"></a>";
463             }
464           }
465           $this->telephone_list[$attrs['sn'][0].$attrs['dn']].= "</td>";
466         }
467       }
468       error_reporting(E_ALL);
469     }
471     /* Sort up list */
472     ksort ($this->telephone_list);
473     reset ($this->telephone_list);
475     /* Fill template variables */
476     $smarty->assign("search_for", $this->search_for);
477     $smarty->assign("object_type", $this->object_type);
479     $this->base = $phonefilter['search_base'];
480     $smarty->assign("deplist", $this->get_allowed_bases());
481     $smarty->assign("depselect", $this->search_base);
482     $smarty->assign("global", $phonefilter['global']);
483     $smarty->assign("organizational", $phonefilter['organizational']);
484     $smarty->assign("search_image", get_template_path('images/search.png'));
485     $smarty->assign("obj_image", get_template_path('images/list_ogroup.png'));
486     $smarty->assign("tree_image", get_template_path('images/tree.png'));
487     $smarty->assign("infoimage", get_template_path('images/info.png'));
488     $smarty->assign("actionimage", get_template_path('images/action.png'));
489     $smarty->assign("launchimage", get_template_path('images/launch.png'));
491     /* Generate alphabet */
492     $alphabet= generate_alphabet();
494     /* Build list output */
495     $output= "";
496     $mod= 0;
499     /* View detailed infos */
500     $smarty->assign("show_info", "");
501     if (isset($_SESSION['show_info'])){
503       $range= 4;
504       $smarty->assign("show_info", "1");
505       $smarty->assign("url", "main.php?plug=".validate($_GET['plug'])."&amp;close=1");
507       $tmp = $this->plInfo();
509       if(isset($_POST['storage_base'])){
510         $this->storage_base = $_POST['storage_base'];  
511       }
513       switch ($_SESSION['show_info']){
515         case "ADD":
517           $a_bases = $this->get_allowed_bases();
518   
519           if(!isset($a_bases[$this->storage_base])){
520             $base = key($this->get_allowed_bases());
521             $this->storage_base = $base;
522           } 
524           $smarty->assign ('storage_base', $this->storage_base);
525           $smarty->assign ('address_info', get_template_path('address_edit.tpl', TRUE));
527           foreach($tmp['plProvidedAcls'] as $name => $translated){
528             $smarty->assign($name."ACL",$this->get_entry_acls($this->abobjectclass.",".$base,$name));
529           }
530         break;
532         default:
533           $smarty->assign ('address_info', get_template_path('address_info.tpl', TRUE));
534           foreach($tmp['plProvidedAcls'] as $name => $translated){
535             $smarty->assign($name."ACL",$this->get_entry_acls($this->dn,$name));
536           }
537         break;
538       }
540       /* Fill variables from LDAP */
541       if ($_SESSION['show_info'] != "ADD"){
542         $ldap->cat($_SESSION['show_info'], $this->attributes);
543         $info= $ldap->fetch();
544       }
545       foreach ($this->attributes as $name){
547         /* Skip entries we are not allowed to read */
548         if(!preg_match("/r/",$this->get_entry_acls($this->dn,$name))){
549           $smarty->assign("info_$name", "");
550         }else
552         if ($_SESSION['show_info'] != "ADD" && isset($info["$name"][0])){
553           error_reporting(0);
554           /* Special treatment for phone attributes */
555           if ($name == "mobile" ||
556               $name == "homePhone" ||
557               $name == "telephoneNumber"){
558             $smarty->assign("info_$name",
559                 "<a title=\""._("Dial")." ".$info["$name"][0]."\" href=\"main.php?plug=".validate($_GET['plug'])."&amp;dial=$name&amp;start=".validate($_GET['start'])."&amp;target=".base64_encode($_SESSION['show_info'])."\">".$info["$name"][0]."</a>");
560           } else {
561             $smarty->assign("info_$name", preg_replace("/\n/", "<br>", $info["$name"][0]));
562           }
563           error_reporting(E_ALL);
564         } elseif ($_SESSION['show_info'] == "ADD" && isset($this->$name)) {
565           $smarty->assign("info_$name", $this->$name);
566         } else {
567           $smarty->assign("info_$name", "-");
568         }
569       }
570       if (preg_match("/,".$this->abobjectclass.",/", $_SESSION['show_info'])){
571         $storage= _("global addressbook");
572         $smarty->assign("internal", 0);
573       } else {
574         $storage= _("user database");
575         $smarty->assign("internal", 1);
576       }
577       if ($_SESSION['show_info'] != "ADD"){
578         $smarty->assign("storage_info", sprintf(_("Contact stored in '%s'"), $storage));
579       } else {
580         $smarty->assign("storage_info", _("Creating new entry in"));
581       }
582     } else {
585       if(isset($_POST['EntryPerPage'])){
586         $this->range = $_POST['EntryPerPage'];
587       }
588       $range = $this->range;    
589       $smarty->assign("internal", 1);
590     }
591     if (isset($_GET['start'])){
592       $this->start= validate($_GET['start']);
593     }
594     foreach ($this->telephone_list as $val){
595       if ($mod < $this->start) {
596         $mod++;
597         continue;
598       }
599       if ($mod >= ($this->start + $range)){
600         $mod++;
601         break;
602       }
603       if ( ($mod++) & 1){
604         $col= "style=\"background-color: #ECECEC;\"";
605       } else {
606         $col= "style=\"background-color: #F5F5F5;\"";
607       }
608       $output.= "<tr $col>\n$val</tr>\n";
609     }
611     $smarty->assign("search_result", $output);
612     $smarty->assign("apply", apply_filter());
613     $smarty->assign("alphabet", $alphabet);
614     if($range < 20){
615       $smarty->assign("range_selector", range_selector(count($this->telephone_list), $this->start, $range));
616     }else{
617       $smarty->assign("range_selector", range_selector(count($this->telephone_list), $this->start, $range, "EntryPerPage"));
618     }
619     $tmp= array("*" => _("All"), "sn" => _("Name"), "givenName" => _("Given name"),
620         "telephoneNumber" => _("Work phone"), "mobile" => _("Cell phone"),
621         "homePhone" => _("Home phone"), "uid" => _("User ID"));
622     natsort($tmp);
623     $smarty->assign("objlist", $tmp);
625     /* Show main page */
626     $smarty->assign ('personal_image', get_template_path('images/addr_personal.png'));
627     $smarty->assign ('home_image', get_template_path('images/addr_home.png'));
628     $smarty->assign ('company_image', get_template_path('images/addr_company.png'));
629     $smarty->assign ('add_image', get_template_path('images/editpaste.png'));
630     $smarty->assign ('edit_image', get_template_path('images/edit.png'));
631     $smarty->assign ('delete_image', get_template_path('images/editdelete.png'));
632     return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
633   }
635   function save_object()
636   {
637     plugin::save_object();
638     foreach($this->attributes as $attr){
640       /* save attributes depending on acls */
641       $acl = $this->get_entry_acls($this->dn,$attr);
643       if(preg_match("/w/",$acl)){
644         if(isset($_POST[$attr])){
645           $this->$attr = $_POST[$attr];
646         }
647       }
649     }
650   }
652   function check()
653   {
654     /* Call common method to give check the hook */
655     $message= plugin::check();
657     /* must: sn, givenName */
658     if ($this->sn == ""){
659       $message[]= _("The required field 'Name' is not set.");
660       return ($message);
661     }
662     if ($this->givenName == ""){
663       $message[]= _("The required field 'Given name' is not set.");
664       return ($message);
665     }
667     /* Check for valid name definition */
668     if (preg_match ("/[\\\\]/", $this->sn)){
669       $message[]= _("The field 'Name' contains invalid characters.");
670     }
671     if (preg_match ("/[\\\\]/", $this->givenName)){
672       $message[]= _("The field 'Given name' contains invalid characters.");
673     }
675     /* Check phone numbers */
676     if (!is_phone_nr($this->homePhone)){
677       $message[]= _("The field 'Phone' contains an invalid phone number.");
678     }
679     if (!is_phone_nr($this->telephoneNumber)){
680       $message[]= _("The field 'Phone' contains an invalid phone number.");
681     }
682     if (!is_phone_nr($this->facsimileTelephoneNumber)){
683       $message[]= _("The field 'Fax' contains an invalid phone number.");
684     }
685     if (!is_phone_nr($this->mobile)){
686       $message[]= _("The field 'Mobile' contains an invalid phone number.");
687     }
688     if (!is_phone_nr($this->pager)){
689       $message[]= _("The field 'Pager' contains an invalid phone number.");
690     }
692     /* Check for reserved characers */
693     if (preg_match ('/[,+"<>;]/', $this->givenName)){
694       $message[]= _("The field 'Given name' contains invalid characters.");
695   }
696   if (preg_match ('/[,+"<>;]/', $this->sn)){
697     $message[]= _("The field 'Name' contains invalid characters.");
698   }
700   /* Check mail */
701   if (!is_email($this->mail)){
702     $message[]= _("Please enter a valid email address in 'Primary address' field.");
703   }
705   /* Assemble cn/dn */
706   $this->cn= $this->givenName." ".$this->sn;
707   if ($this->orig_cn != $this->cn || $this->storage_base != $this->orig_storage_base){
708     $this->new_dn= $this->create_unique_dn("cn", preg_replace("/,*".$this->config->current['BASE']."$/", "", $this->storage_base).",".$this->abobjectclass.",".$this->config->current['BASE']);
709     if ($this->new_dn == "none"){
710       $message[]= _("Cannot create a unique DN for your entry. Please fill more formular fields.");
711       return ($message);
712     }
713   } else {
714     $this->new_dn= $this->dn;
715   }
717   return ($message);
718   }
721   function load()
722   {
723     /* Load base attributes */
724     plugin::plugin ($this->config, $this->dn);
725     $this->view_logged = FALSE;
726     $this->storage_base= preg_replace('/^[^,]+,/', '', preg_replace('/'.$this->abobjectclass.',/', '', $this->dn));
727   }
730   function save()
731   {
732     /* First use parents methods to do some basic fillup in $this->attrs */
733     plugin::save ();
735     $this->attrs['cn']= $this->cn;
736     $this->attrs['displayName']= $this->givenName." ".$this->sn;
738     /* Move entry if it got another name... */
739     if ($this->dn != "new" && $this->dn != $this->new_dn){
740       $this->move($this->dn, $this->new_dn);
741     }
742     $this->dn= $this->new_dn;
744     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
745        new entries. So do a check first... */
746     $ldap= $this->config->get_ldap_link();
747     $ldap->cat ($this->dn,array('dn'));
748     if ($ldap->fetch()){
749       $mode= "modify";
750     } else {
751       $mode= "add";
752       $ldap->cd($this->config->current['BASE']);
753       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
754     }
756     /* Finally write data with selected 'mode' */
757     $ldap->cd ($this->dn);
758     $this->cleanup();
759     $ldap->$mode ($this->attrs);
760     if (show_ldap_error($ldap->get_error(), sprintf(_("Removing of addressbook entry '%s' failed."),$this->dn))){
761       return (1);
762     }
764     if($mode == "add"){
765       new log("create","addressbook/".get_class($this),$this->dn, array_keys($this->attrs),$ldap->get_error());
766     }else{
767       new log("modify","addressbook/".get_class($this),$this->dn, array_keys($this->attrs),$ldap->get_error());
768     }
769   }
771   
772   /* Return entry acls */
773   function get_entry_acls($dn,$attr = "")
774   {
775     $acls = "";
776   
777     /* Use addressbook acls */
778     if(preg_match("/".normalizePreg($this->abobjectclass)."/",$dn))  {
779       $dn = preg_replace("/".normalizePreg($this->abobjectclass).",/","",$dn);
780       $acls = $this->ui->get_permissions($dn,"addressbook/addressbook",$attr);
781     }
782     
783     /* Use Organizational Person acls */
784     else{
785       $acls = $this->ui->get_permissions($dn,"users/user",$attr);
786     }
788     return($acls);
789   }
792   /* Return plugin informations for acl handling  */
793   function plInfo()
794   {
795     return (array(
796           "plShortName" => _("Addressbook"),
797           "plDescription" => _("Addressbook entry acls"),
798           "plSelfModify"  => FALSE,
799           "plDepends"     => array(),
800           "plPriority"    => 0,
801           "plSection"     => array("addons" => _("Addons")),
802           "plCategory"    => array("addressbook" => array("objectClass" => "inetOrgPerson", "description" => _("Addressbook"))),
804           "plProvidedAcls"    => array(
805             "sn"                        => _("Surename"),         
806             "givenName"                 => _("Given name"), 
807             "telephoneNumber"           => _("Telefon number"), 
808             "facsimileTelephoneNumber"  => _("Fax number"), 
809             "mobile"                    => _("Mobile number"), 
810             "homePhone"                 => _("Home phone number"), 
811             "uid"                       => _("User identification"), 
812             "mail"                      => _("Mail address"), 
813             "pager"                     => _("Pager"),
814             "o"                         => _("Organization"),
815             "ou"                        => _("Department"),
816             "l"                         => _("Location"),
817             "postalAddress"             => _("Postal address"),
818             "postalCode"                => _("Postal address"),
819             "st"                        => _("State"),
820             "initials"                  => _("Initials"), 
821             "title"                     => _("Title"), 
822             "homePostalAddress"         => _("Home postal address"), 
823             "cn"                        => _("Common name"))
824             ));
825   }
827 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
828 ?>