Code

420ce026b600f1a2014a6b67a5ce779ba7303029
[gosa.git] / gosa-plugins / addressbook / addons / addressbook / class_addressbook.inc
1 <?php
4 class addressbookgosa extends plugin{
6   static function plInfo()
7   {
8     /* Adapt addressbook settings and mofiy required settings. 
9      */
10     $attrs = addressbook::plInfo(); 
11     $attrs['plShortName']   = _("Addressbook")." - "._("GOsa");
12     $attrs['plDescription'] = _("Addressbook")." - "._("GOsa related objects");
13     $attrs['plSelfModify']  = TRUE;
14     $attrs['plPriority']    = 2;
15     $attrs['plCategory']    = array("addressbook");
16     return($attrs);
17   }
18 }
20 class addressbook extends plugin
21 {
22         /* Definitions */
23         var $plHeadline   = "Addressbook";
24         var $plDescription= "This does something";
26   /* Generic */
27   var $ui;  
29         /* Phonelist attributes */
30         var $telephone_list   = array();
31         var $new_dn           = "";
32         var $orig_cn          = "";
33         var $storage_base     = "";
34         var $orig_storage_base= "";
36   /* Filter attributes */
37         var $start            = 0;
38         var $search_for       = "*";
39         var $search_base      = "";
40         var $search_type      = "";
41         var $range            = 20;
43   /* Currently edited/added entry attributes */
44         var $sn                       = "";
45         var $cn                       = "";
46         var $givenName                = "";
47         var $mail                     = "";
48         var $title                    = "";
49         var $personalTitle            = "";
50         var $initials                 = "";
51         var $homePostalAddress        = "";
52         var $homePhone                = "";
53         var $mobile                   = "";
54         var $o                        = "";
55         var $postalAddress            = "";
56         var $l                        = "";
57         var $postalCode               = "";
58         var $st                       = "";
59         var $ou                       = "";
60         var $telephoneNumber          = "";
61         var $facsimileTelephoneNumber = "";
62         var $pager                    = "";
63   var $view_logged              = FALSE;
65         /* attribute list for save action */
66         var $attributes= array("sn", "givenName", "mail", "title",
67                         "initials", "homePostalAddress", "displayName",
68                         "homePhone", "mobile", "o", "postalAddress", "l",
69                         "postalCode", "st", "ou", "telephoneNumber",
70                         "facsimileTelephoneNumber", "pager");
72         var $objectclasses= array("top", "person", "organizationalPerson", "inetOrgPerson");
74         var $abobjectclass= "dc=addressbook";
76   function addressbook (&$config, $dn= NULL)
77   {
78     /* Include config object */
79     $this->config= &$config;
81     /* Check if there is a special ldap-sub-tree specified, instead of dc=addressbook, */
82     $aoc = $this->config->search("addressbook", "addressbookBaseDN",array('menu'));
83     if ($aoc != ""){
84       $this->abobjectclass  = $aoc;
85     }
87     /* Get global filter config */
88     if (!session::is_set("phonefilter")){
89       $ui         = get_userinfo();
90       $base       = get_base_from_people($ui->dn);
91       $phonefilter= array(
92           "search_base"       => $base,
93           "organizational"    => "checked",
94           "global"            => "checked",
95           "search_for"        => "*",
96           "object_type"       => "*");
97       session::set("phonefilter", $phonefilter);
98     }
99      
100     $this->ui = get_userinfo();
101   }
103   function execute()
104   {
105     /* Call parent execute */
106     plugin::execute();
108     $smarty= get_smarty();
110     /* Prevent empty variables for smarty */
111     foreach($this->attributes as $atr) {
112       $smarty->assign($atr,"");
113     }
115     /* Save formular information */
116     $phonefilter= session::get("phonefilter");
117     foreach( array("search_for", "search_base", "object_type") as $type){
118       if (isset($_POST[$type])){
119         $phonefilter[$type]= $_POST[$type];
120       }
121       $this->$type= $phonefilter[$type];
122     }
123     if (isset($_POST['search_base'])){
124       foreach( array("organizational", "global") as $type){
125         if (isset($_POST[$type])){
126           $phonefilter[$type]= "checked";
127         } else {
128           $phonefilter[$type]= "";
129         }
130       }
131     }
133     /* Search string */
134     $s= $phonefilter['search_for'];
135     if ($s == "") {
136       $s= "*";
137     }
138     if (isset($_GET['search'])){
139       $s= validate(mb_substr($_GET['search'], 0, 1, "UTF8"))."*";
140       if ($s == "**"){
141         $s= "*";
142       }
143       $this->search_for= $s;
144       $phonefilter['search_for']= $s;
145     }
146     session::set("phonefilter", $phonefilter);
148     /* Assign create acl */
149     $acl = $this->get_entry_acls($this->abobjectclass.",".$phonefilter['search_base']);
150     $smarty->assign("internal_createable", preg_match("/c/",$acl));
151     $smarty->assign("internal_removeable", preg_match("/d/",$acl));
152     $smarty->assign("internal_editable",   preg_match("/w/",$acl));
154     /* Perform actions with CTI hook */
155     if (isset($_GET['target'])
156         && isset($_GET['dial'])
157         && $this->config->get_cfg_value("ctiHook") != "" ){
159       $dialmode= $_GET['dial'];
160       if ($dialmode == "telephoneNumber" ||
161           $dialmode == "mobile" ||
162           $dialmode == "homePhone"){
164         /* Get target */
165         $ldap= $this->config->get_ldap_link();
166         $ldap->cat(base64_decode($_GET['target']), array('telephoneNumber', 'mobile', 'homePhone'));
167         $attrs= $ldap->fetch();
168         if (isset($attrs["$dialmode"])){
169           $target= $attrs[$dialmode][0];
170         } else {
171           $target= "";
172         }
174         /* Get source */
175         $ui= get_userinfo();
176         $ldap->cat($ui->dn, array('telephoneNumber'));
177         $attrs= $ldap->fetch();
178         if (isset($attrs["telephoneNumber"])){
179           $source= $attrs['telephoneNumber'][0];
180         } else {
181           $source= "";
182         }
184         /* Save to session */
185         session::set('source',$source);
186         session::set('target',$target);
188         /* Perform call */
189         if ($target != "" && $source != ""){
190           $smarty->assign("phone_image", get_template_path('plugins/addressbook/images/phone.png'));
191           $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>"));
192           return($smarty->fetch(get_template_path('dial.tpl', TRUE)));
193           return;
194         } else {
195           msg_dialog::display(_("Error"), _("You need to set your personal phone number in order to perform direct dials."), ERROR_DIALOG);
196         }
197       }
199     }
201     /* Finally dial */
202     if (isset($_POST['dial']) && session::is_set('source') && session::is_set('target')){
203       exec ($this->config->get_cfg_value("ctiHook")." '".session::get('source')."' '".session::get('target')."'", $dummy, $retval);
204       session::un_set('source');
205       session::un_set('target');
206     }
209     /* Delete entry? */
210     if (isset($_POST['delete_entry_confirm'])){
212       /* Some nice guy may send this as POST, so we've to check
213          for the permissions again. */
214       
215       $acl = $this->get_entry_acls($this->dn);
216       if(preg_match("/d/",$acl)){
218         /* Delete request is permitted, perform LDAP action */
219         $ldap= $this->config->get_ldap_link();
220         $ldap->rmdir ($this->dn);
221         new log("remove","addressbook/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
222         if (!$ldap->success()){
223           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
224         }
225         new log("remove","addressbook/".get_class($this),$this->dn,array(),"Addressbook object'".$this->dn."' has been removed");
226   
227       } else {
229         /* Normally this shouldn't be reached, send some extra
230            logs to notify the administrator */
231               msg_dialog::display(_("Permission"),permDelete($this->dn),ERROR_DIALOG);
232         new log("remove","addressbook/".get_class($this),$this->dn,array(),"Warning: '".$this->ui->uid."' tried to trick address book deletion.");
233       }
235       /* Remove lock file after successfull deletion */
236       del_lock ($this->dn);
238       /* Clean up */
239       if (session::is_set('saved_start')){
240         $_GET['start']= session::get('saved_start');
241       }
242       session::un_set('show_info');
243       session::un_set('saved_start');
244     }
247     /* Delete entry? */
248     if (isset($_POST['delete_cancel'])){
249       del_lock ($this->dn);
250     }
253     /* Save address entry? */
254     if (isset($_POST['save'])){
255       $this->save_object();
256       $this->storage_base= $_POST['storage_base'];
258       /* Perform checks */
259       $message= $this->check ();
261       /* No errors, save object */
262       if (count ($message) == 0){
263         $this->save();
265         /* Clean up */
266         if (session::is_set('saved_start')){
267           $_GET['start']= session::get('saved_start');
268         }
269         session::set('show_info',$this->dn);
270         session::un_set('saved_start');
271       } else {
272         /* Errors found, show message */
273         msg_dialog::displayChecks($message);
274       }
275     }
278     /* Close info window */
279     if (isset($_GET['close']) || isset($_POST['cancel'])){
280       if (session::is_set('saved_start')){
281         $_GET['start']= session::get('saved_start');
282       }
283       session::un_set('show_info');
284       session::un_set('saved_start');
285     }
288     /* Start address book edit mode? */
289     if (isset($_GET['global'])){
290       if (!session::is_set('saved_start') && isset($_GET['start'])){
291         session::set('saved_start',$_GET['start']);
292       }
293       switch ($_GET['global']){
294         case "add":
295           $this->dn= "new";
296         $this->orig_cn= "";
298         /* Clean values */
299         foreach ($this->attributes as $name){
300           $this->$name= "";
301         }
302         $this->saved_attributes= array();
303         $this->storage_base= $this->config->current["BASE"];
304         break;
306         case "edit":
307           /* Clean values */
308           foreach ($this->attributes as $name){
309             $this->$name= "";
310           }
311         $this->dn= session::get('show_info'); 
312         $this->load();
313         $this->orig_cn= $this->cn;
314         break;
315         case "remove":
316           $this->dn= session::get('show_info'); 
317         $this->load();
319         /* Load permissions for selected 'dn' and check if
320            we're allowed to remove this 'dn' */
321         $acl = $this->get_entry_acls($this->dn);
322         if(preg_match("/d/",$acl)){
324           /* Check locking, save current plugin in 'back_plugin', so
325              the dialog knows where to return. */
326           if (($user= get_lock($this->dn)) != ""){
327             return(gen_locked_message ($user, $this->dn));
328           }
330           /* Lock the current entry, so nobody will edit it during deletion */
331           $ui= get_userinfo();
332           add_lock ($this->dn, $ui->dn);
333           $smarty->assign("info", msgPool::deleteInfo($this->dn));
334           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
335         } else {
337           /* Obviously the user isn't allowed to delete. Show message and
338              clean session. */
339                 msg_dialog::display(_("Permission"),permDelete($this->dn),ERROR_DIALOG);
340         }
341       }
342       session::set('show_info',"ADD");
343     }
346     /* Open info window */
347     if (isset($_GET['show'])){
348       if (!session::is_set('saved_start')){
349         session::set('saved_start',$_GET['start']);
350       }
351       $this->dn = base64_decode($_GET['show']);
352       $this->view_logged =FALSE;
353       if(!$this->view_logged){
354         $this->view_logged = TRUE;
355         new log("view","addressbook/".get_class($this),$this->dn);
356       }
357       session::set('show_info',base64_decode($_GET['show']));
358     }
361     /* Get ldap link / build filter */
362     $ldap= $this->config->get_ldap_link();
363     $this->telephone_list= array ();
366     /* Assemble bases 
367         (Depending on checkboxes, we search for organisational entries or seperated 
368         adressbook entries within dc=adressbook, ) */
369     $bases= array();
370     $filter= "";
371     if ($phonefilter['global'] == "checked"){
372       $bases[]= preg_replace("/".$this->config->current['BASE']."/", $this->abobjectclass.",".$this->config->current['BASE'], $this->search_base);
373     } else {
374       $filter= '(objectClass=gosaAccount)';
375     }
376     if ($phonefilter['organizational'] == "checked"){
377       $bases[]= $this->search_base;
378     }
381     /* Only display those entries that have at least on of this attributes set */
382     $must_have_this = array("telephoneNumber","facsimileTelephoneNumber","mobile","homePhone","mail");
384     /* Requested attributes in ldap search */
385     $attributes = array("sn", "givenName", "telephoneNumber", "facsimileTelephoneNumber", "mobile", "homePhone", "uid", "mail", "cn");
387     /* Create attribute filter part */
388     $attribute_filter = "";
389     foreach($attributes as $att){
390       $attribute_filter .= "(".$att."=".$s.")";
391     }
393     /* Walk through bases an check for usable entries */
394     foreach ($bases as $base){
396       $ldap->cd ($base);
398       if ($phonefilter['object_type'] == '*'){
399         $ldap->search (
400             "(&(objectClass=person)$filter(!(objectClass=gosaUserTemplate))".   // Skip templates etc ..
401             "(!(uid=*$))".                                                      // Skip entries with ...$ as uid 
402             "(|".$attribute_filter."))"                                         
403           ,$attributes); 
404       } else {
405         $ldap->search ("(&$filter(!(uid=*$))(!(objectClass=gosaUserTemplate))".                     //array
406             "(".$phonefilter['object_type']."=$s))", $attributes);
407       }
409       /* Walk through LDAP results */
410       while ($attrs= $ldap->fetch()){
412         /* prevent empty vaiables */
413         foreach($this->attributes as $atr)          {
414           if(!isset($attrs[$atr][0])) {
415             $attrs[$atr][0] = "";
416           }
417         }
419         /* Check if page number was posted */
420         if(!isset($_GET['start'])) {
421           $_GET['start']="";
422         }
424         /* Check if at least one attribute is specified */
425         $skip = false;
427         foreach($must_have_this as $attr)      {
428           if(isset($attrs[$attr][0]) && !empty($attrs[$attr][0])){
429             $skip =false;
430             break;
431           }
432         }
434         /* Skip all attributes that we are not allowed to read */
435         $any = false;
436         foreach($attributes as $attr){
437     
438           $acls = $this->get_entry_acls($attrs['dn'],$attr);  
439           if(!preg_match("/r/",$acls)){
440             $attrs[$attr][0] = "";
441           }else{
442             $any = true;
443           }
444         }
445  
446         /* Only show lines that have set any mail or phone informations */
447         if(!$skip && $any){      
448   
449           $this->telephone_list[$attrs['sn'][0].$attrs['dn']]=
451             "<td class=\"phonelist\" title=\"".$attrs['sn'][0].", ".$attrs['givenName'][0].
452               "\"onClick='location.href=\"main.php?plug=".validate($_GET['plug']).
453                           "&amp;start=".validate($_GET['start']).
454                           "&amp;show=".urlencode(base64_encode($attrs['dn']))."\"'>
455               <a style='vertical-align:middle;' href=\"main.php?plug=".validate($_GET['plug']).
456                           "&amp;start=".validate($_GET['start']).
457                           "&amp;show=".urlencode(base64_encode($attrs['dn']))."\">".
458                $attrs['sn'][0].", ".$attrs['givenName'][0].
459             "</a>
460             &nbsp;</td>
461             <td class=\"phonelist\" title=\""._("Dial")." ".$attrs['telephoneNumber'][0]."\">
462             <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]."
463             </a>
464             &nbsp;</td>
465             <td class=\"phonelist\" title=\"".$attrs['facsimileTelephoneNumber'][0]."\">
466             ".$attrs['facsimileTelephoneNumber'][0]."
467             &nbsp;</td>
468             <td class=\"phonelist\" title=\""._("Dial")." ".$attrs['mobile'][0]."\">
469             <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]."
470             </a>
471             &nbsp;</td>
472             <td class=\"phonelist\" title=\""._("Dial")." ".$attrs['homePhone'][0]."\">
473             <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]."
474             </a>
475             &nbsp;</td>
476             <td>
477             <a href=\"plugins/addressbook/getvcard.php?dn=".base64_encode($attrs['dn'])."\">
478             <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])."\">
479             </a>";
482           if(preg_match("/r/",$this->get_entry_acls($attrs['dn'],"mail"))){
483             if (isset($attrs['mail'][0]) && !empty($attrs['mail'][0])){
484               $dest= sprintf(_("Send mail to %s"), $attrs['mail'][0]);
485               $this->telephone_list[$attrs['sn'][0].$attrs['dn']].=
487                 "<a href=\"mailto:".htmlentities($attrs['mail'][0])."\">".
488                 "<img  align=\"top\" border=0 src=\"images/mailto.png\" alt=\"vcf\" title=\"$dest\"></a>";
489             }
490           }
491           $this->telephone_list[$attrs['sn'][0].$attrs['dn']].= "&nbsp;</td>";
492         }
493       }
494       error_reporting(E_ALL | E_STRICT);
495     }
497     /* Sort up list */
498     ksort ($this->telephone_list);
499     reset ($this->telephone_list);
501     /* Fill template variables */
502     $smarty->assign("search_for", $this->search_for);
503     $smarty->assign("object_type", $this->object_type);
505     /* Create list of readable departments */
506     $deps = array();
507     foreach($this->config->idepartments as $dn => $name){
508       if( $this->acl_is_readable($dn)){
509         $deps[$dn] = $name;
510       }
511     }
514   
515     $this->base = $phonefilter['search_base'];
516     $smarty->assign("deplist", $deps);
517     $smarty->assign("depselect", $this->search_base);
518     $smarty->assign("global", $phonefilter['global']);
519     $smarty->assign("organizational", $phonefilter['organizational']);
520     $smarty->assign("search_image", get_template_path('images/lists/search.png'));
521     $smarty->assign("obj_image", get_template_path('plugins/addressbook/images/objects.png'));
522     $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
523     $smarty->assign("infoimage", get_template_path('images/info.png'));
524     $smarty->assign("actionimage", get_template_path('images/action.png'));
525     $smarty->assign("launchimage", get_template_path('images/launch.png'));
527     /* Generate alphabet */
528     $alphabet= generate_alphabet();
530     /* Build list output */
531     $output= "";
532     $mod= 0;
535     /* View detailed infos */
536     $smarty->assign("show_info", "");
537     if (session::is_set('show_info')){
539       $range= 4;
540       $smarty->assign("show_info", "1");
541       $smarty->assign("url", "main.php?plug=".validate($_GET['plug'])."&amp;close=1");
543       $tmp = $this->plInfo();
545       if(isset($_POST['storage_base'])){
546         $this->storage_base = $_POST['storage_base'];  
547       }
549       switch (session::get('show_info')){
551         case "ADD":
553           $a_bases = $this->get_allowed_bases();
554   
555           if(!isset($a_bases[$this->storage_base])){
556             $base = key($this->get_allowed_bases());
557             $this->storage_base = $base;
558           } 
560           $smarty->assign ('storage_base', $this->storage_base);
561           $smarty->assign ('address_info', get_template_path('address_edit.tpl', TRUE));
563           foreach($tmp['plProvidedAcls'] as $name => $translated){
564             $smarty->assign($name."ACL",$this->get_entry_acls($this->abobjectclass.",".$base,$name));
565           }
566         break;
568         default:
569           $smarty->assign ('address_info', get_template_path('address_info.tpl', TRUE));
570           foreach($tmp['plProvidedAcls'] as $name => $translated){
571             $smarty->assign($name."ACL",$this->get_entry_acls($this->dn,$name));
572           }
573         break;
574       }
576       /* Fill variables from LDAP */
577       if (session::get('show_info') != "ADD"){
578         $ldap->cat(session::get('show_info'), $this->attributes);
579         $info= $ldap->fetch();
580       }
581       foreach ($this->attributes as $name){
583         $dn = $this->dn;
584         if($dn == "new") $dn = $this->base;
586         /* Skip entries we are not allowed to read */
587         if(!preg_match("/r/",$this->get_entry_acls($dn,$name))){
588           $smarty->assign("info_$name", "");
589         }else
591         if (session::get('show_info') != "ADD" && isset($info["$name"][0])){
592           error_reporting(0);
593           /* Special treatment for phone attributes */
594           if ($name == "mobile" ||
595               $name == "homePhone" ||
596               $name == "telephoneNumber"){
597             $smarty->assign("info_$name",
598                 "<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::get('show_info'))."\">".$info["$name"][0]."</a>");
599           } else {
600             $smarty->assign("info_$name", preg_replace("/\n/", "<br>", $info["$name"][0]));
601           }
602           error_reporting(E_ALL | E_STRICT);
603         } elseif (session::get('show_info') == "ADD" && isset($this->$name)) {
604           $smarty->assign("info_$name", $this->$name);
605         } else {
606           $smarty->assign("info_$name", "-");
607         }
608       }
609       if (preg_match("/,".$this->abobjectclass.",/", session::get('show_info'))){
610         $storage= _("global addressbook");
611         $smarty->assign("internal", 0);
612       } else {
613         $storage= _("user database");
614         $smarty->assign("internal", 1);
615       }
616       if (session::get('show_info') != "ADD"){
617         $smarty->assign("storage_info", sprintf(_("Contact stored in '%s'"), $storage));
618       } else {
619         $smarty->assign("storage_info", _("Creating new entry in"));
620       }
621     } else {
624       if(isset($_POST['EntryPerPage'])){
625         $this->range = $_POST['EntryPerPage'];
626       }
627       $range = $this->range;    
628       $smarty->assign("internal", 1);
629     }
630     if (isset($_GET['start'])){
631       $this->start= validate($_GET['start']);
632     }
633     foreach ($this->telephone_list as $val){
634       if ($mod < $this->start) {
635         $mod++;
636         continue;
637       }
638       if ($mod >= ($this->start + $range)){
639         $mod++;
640         break;
641       }
642       if ( ($mod++) & 1){
643         $col= "style=\"background-color: #ECECEC;\"";
644       } else {
645         $col= "style=\"background-color: #F5F5F5;\"";
646       }
647       $output.= "<tr $col>\n$val</tr>\n";
648     }
650     $smarty->assign("search_result", $output);
651     $smarty->assign("apply", apply_filter());
652     $smarty->assign("alphabet", $alphabet);
653     if($range < 20){
654       $smarty->assign("range_selector", range_selector(count($this->telephone_list), $this->start, $range));
655     }else{
656       $smarty->assign("range_selector", range_selector(count($this->telephone_list), $this->start, $range, "EntryPerPage"));
657     }
658     $tmp= array("*" => _("All"), "sn" => _("Name"), "givenName" => _("Given name"),
659         "telephoneNumber" => _("Work phone"), "mobile" => _("Cell phone"),
660         "homePhone" => _("Home phone"), "uid" => _("User ID"));
661     natsort($tmp);
662     $smarty->assign("objlist", $tmp);
664     /* Show main page */
665     $smarty->assign ('personal_image', get_template_path('plugins/addressbook/images/addr_personal.png'));
666     $smarty->assign ('home_image', get_template_path('plugins/addressbook/images/addr_home.png'));
667     $smarty->assign ('company_image', get_template_path('plugins/addressbook/images/addr_company.png'));
668     $smarty->assign ('add_image', get_template_path('images/lists/paste.png'));
669     $smarty->assign ('edit_image', get_template_path('images/lists/edit.png'));
670     $smarty->assign ('delete_image', get_template_path('images/lists/delete.png'));
671     return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
672   }
674   function save_object()
675   {
676     plugin::save_object();
677     foreach($this->attributes as $attr){
679       /* save attributes depending on acls */
680       $acl = $this->get_entry_acls($this->dn,$attr);
682       if(preg_match("/w/",$acl)){
683         if(isset($_POST[$attr])){
684           $this->$attr = $_POST[$attr];
685         }
686       }
688     }
689   }
691   function check()
692   {
693     /* Call common method to give check the hook */
694     $message= plugin::check();
696     /* must: sn, givenName */
697     if ($this->sn == ""){
698       $message[] = msgPool::required(_("Name"));
699       return ($message);
700     }
701     if ($this->givenName == ""){
702       $message[] = msgPool::required(_("Given name"));
703       return ($message);
704     }
706     /* Check for valid name definition */
707     if (preg_match ("/[\\\\]/", $this->sn)){
708       $message[] = msgPool::invalid(_("Name"),$this->sn,"/[\\\\]");
709     }
710     if (preg_match ("/[\\\\]/", $this->givenName)){
711       $message[] = msgPool::invalid(_("Given name"),$this->givenName,"/[\\\\]");
712     }
714     /* Check phone numbers */
715     if (!tests::is_phone_nr($this->homePhone)){
716       $message[] = msgPool::invalid(_("Phone"),$this->homePhone);
717     }
718     if (!tests::is_phone_nr($this->telephoneNumber)){
719       $message[] = msgPool::invalid(_("Telephone number"),$this->telephoneNumber);
720     }
721     if (!tests::is_phone_nr($this->facsimileTelephoneNumber)){
722       $message[] = msgPool::invalid(_("Fax"),$this->facsimileTelephoneNumber);
723     }
724     if (!tests::is_phone_nr($this->mobile)){
725       $message[] = msgPool::invalid(_("Mobile"),$this->mobile);
726     }
727     if (!tests::is_phone_nr($this->pager)){
728       $message[] = msgPool::invalid(_("Pager"),$this->pager);
729     }
730     /* Check for reserved characers */
731     if (preg_match ('/[,+"<>;]/', $this->givenName)){
732       $message[] = msgPool::invalid(_("Given name"),$this->givenName,'/[,+"<>;]/');
733   }
734   if (preg_match ('/[,+"<>;]/', $this->sn)){
735     $message[] = msgPool::invalid(_("Name"),$this->sn,'/[,+"<>;]/');
736   }
738   /* Check mail */
739   if (!tests::is_email($this->mail)){
740     $message[] = msgPool::invalid(_("Email"),"","","your-domain@your-domain.com");
741   }
743   /* Assemble cn/dn */
744   $this->cn= $this->givenName." ".$this->sn;
745   if ($this->orig_cn != $this->cn || $this->storage_base != $this->orig_storage_base){
746     $this->new_dn= $this->create_unique_dn("cn", preg_replace("/,*".$this->config->current['BASE']."$/", "", $this->storage_base).",".$this->abobjectclass.",".$this->config->current['BASE']);
747     if ($this->new_dn == "none"){
748       $message[]= _("Cannot create a unique DN for your entry. Please fill more formular fields.");
749       return ($message);
750     }
751   } else {
752     $this->new_dn= $this->dn;
753   }
755   return ($message);
756   }
759   function load()
760   {
761     /* Load base attributes */
762     plugin::plugin ($this->config, $this->dn);
763     $this->view_logged = FALSE;
764     $this->storage_base= preg_replace('/^[^,]+,/', '', preg_replace('/'.$this->abobjectclass.',/', '', $this->dn));
765   }
768   function save()
769   {
770     /* First use parents methods to do some basic fillup in $this->attrs */
771     plugin::save ();
773     $this->attrs['cn']= $this->cn;
774     $this->attrs['displayName']= $this->givenName." ".$this->sn;
776     /* Move entry if it got another name... */
777     if ($this->dn != "new" && $this->dn != $this->new_dn){
778       $this->move($this->dn, $this->new_dn);
779     }
780     $this->dn= $this->new_dn;
782     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
783        new entries. So do a check first... */
784     $ldap= $this->config->get_ldap_link();
785     $ldap->cat ($this->dn,array('dn'));
786     if ($ldap->fetch()){
787       $mode= "modify";
788     } else {
789       $mode= "add";
790       $ldap->cd($this->config->current['BASE']);
791       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
792     }
794     /* Finally write data with selected 'mode' */
795     $ldap->cd ($this->dn);
796     $this->cleanup();
797     $ldap->$mode ($this->attrs);
798     if (!$ldap->success()){
799       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
800       return (1);
801     }
803     if($mode == "add"){
804       new log("create","addressbook/".get_class($this),$this->dn, array_keys($this->attrs),$ldap->get_error());
805     }else{
806       new log("modify","addressbook/".get_class($this),$this->dn, array_keys($this->attrs),$ldap->get_error());
807     }
808   }
810  
811   /* Return entry acls */
812   function get_entry_acls($dn,$attr = "")
813   {
814     $acls = "";
815     $combineACLs = FALSE;
817     /* If combineACLs is set to false:
818        Differentiate between user and addressbook acls, thus leads into two object categories.
819         - real GOsa users
820         - and addressbook entries
821        To view addressbook entries (e.g. ou=addressbook,) you just require permissions on the ldap base for 'addressbook'.
822        To view real GOsa users you have to additionally add user permissions.
824        You can enable this option, to use only addressbook acls.
825      */
826     if($combineACLs){
827       $dn = preg_replace("/".preg_quote($this->abobjectclass, '/').",/","",$dn);
828       $acls = $this->ui->get_permissions($dn,"addressbook/addressbook",$attr);
829     }else{
831       /* Use addressbook acls */
832       if(preg_match("/".preg_quote($this->abobjectclass, '/')."/",$dn))  {
833         $acls = "";
834         foreach($this->ui->ACLperPath as $path => $data){
835           $acls.= $this->ui->get_permissions($path,"addressbook/addressbook",$attr);
836         }
837       }
839       /* Use Organizational Person acls */
840       else{
841         $acls = $this->ui->get_permissions($dn,"addressbook/addressbookgosa",$attr);
842       }
843     }
845     return($acls);
846   }
849   /* Return plugin informations for acl handling  */
850   static function plInfo()
851   {
852     return (array(
853           "plShortName" => _("Addressbook")." - "._("Addressbook entries"),
854           "plDescription" => _("Addressbook")." - "._("Addressbook related objects"),
855           "plSelfModify"  => FALSE,
856           "plDepends"     => array(),
857           "plPriority"    => 0,
858           "plSection"     => array("addon" => _("Addons")),
859           "plCategory"    => array("addressbook" => array("objectClass" => "inetOrgPerson", "description" => _("Addressbook"))),
861           "plProvidedAcls"    => array(
862             "sn"                        => _("Surename"),         
863             "givenName"                 => _("Given name"), 
864             "telephoneNumber"           => _("Telefon number"), 
865             "facsimileTelephoneNumber"  => _("Fax number"), 
866             "mobile"                    => _("Mobile number"), 
867             "homePhone"                 => _("Home phone number"), 
868             "uid"                       => _("User identification"), 
869             "mail"                      => _("Mail address"), 
870             "pager"                     => _("Pager"),
871             "o"                         => _("Organization"),
872             "ou"                        => _("Department"),
873             "l"                         => _("Location"),
874             "postalAddress"             => _("Postal address"),
875             "postalCode"                => _("Postal address"),
876             "st"                        => _("State"),
877             "initials"                  => _("Initials"), 
878             "title"                     => _("Title"), 
879             "homePostalAddress"         => _("Home postal address"), 
880             "cn"                        => _("Common name"))
881             ));
882   }
884 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
885 ?>