1 <?php
3 class addressbook extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "Addressbook";
7 var $plDescription= "This does something";
9 /* Phonelist attributes */
10 var $telephone_list = array();
11 var $start= 0;
12 var $search_for= "*";
13 var $search_base= "";
14 var $search_type= "";
15 var $new_dn= "";
16 var $orig_cn= "";
17 var $storage_base= "";
18 var $orig_storage_base= "";
20 var $range = 20;
22 var $sn= "";
23 var $cn= "";
24 var $givenName= "";
25 var $mail= "";
26 var $title= "";
27 var $personalTitle= "";
28 var $initials= "";
29 var $homePostalAddress= "";
30 var $homePhone= "";
31 var $mobile= "";
32 var $o= "";
33 var $postalAddress= "";
34 var $l= "";
35 var $postalCode= "";
36 var $st= "";
37 var $ou= "";
38 var $telephoneNumber= "";
39 var $facsimileTelephoneNumber= "";
40 var $pager= "";
42 /* attribute list for save action */
43 var $attributes= array("sn", "givenName", "mail", "title",
44 "initials", "homePostalAddress", "displayName",
45 "homePhone", "mobile", "o", "postalAddress", "l",
46 "postalCode", "st", "ou", "telephoneNumber",
47 "facsimileTelephoneNumber", "pager");
48 var $objectclasses= array("top", "person", "organizationalPerson", "inetOrgPerson");
50 function addressbook ($config, $dn= NULL)
51 {
52 /* Include config object */
53 $this->config= $config;
55 /* Get global filter config */
56 if (!is_global("phonefilter")){
57 $ui= get_userinfo();
58 $base= get_base_from_people($ui->dn);
59 $phonefilter= array("search_base" => $base,
60 "organizational" => "checked",
61 "global" => "checked",
62 "search_for" => "*",
63 "object_type" => "*");
64 register_global("phonefilter", $phonefilter);
65 }
66 }
68 function execute()
69 {
70 /* Call parent execute */
71 plugin::execute();
73 $smarty= get_smarty();
74 foreach($this->attributes as $attr){
75 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
76 }
79 #! Hickert
80 /*prevent empty variables for smarty*/
81 foreach($this->attributes as $atr) $smarty->assign($atr,"");
83 /* Save formular information */
84 $phonefilter= get_global("phonefilter");
85 foreach( array("search_for", "search_base", "object_type") as $type){
86 if (isset($_POST[$type])){
87 $phonefilter[$type]= $_POST[$type];
88 }
89 $this->$type= $phonefilter[$type];
90 }
91 if (isset($_POST['search_base'])){
92 foreach( array("organizational", "global") as $type){
93 if (isset($_POST[$type])){
94 $phonefilter[$type]= "checked";
95 } else {
96 $phonefilter[$type]= "";
97 }
98 }
99 }
101 /* Search string */
102 $s= $phonefilter['search_for'];
103 if ($s == "") {
104 $s= "*";
105 }
106 if (isset($_GET['search'])){
107 $s= validate(mb_substr($_GET['search'], 0, 1, "UTF8"))."*";
108 if ($s == "**"){
109 $s= "*";
110 }
111 $this->search_for= $s;
112 $phonefilter['search_for']= $s;
113 }
114 register_global("phonefilter", $phonefilter);
116 /* Perform actions with CTI hook */
117 if (isset($_GET['target'])
118 && isset($_GET['dial'])
119 && isset($this->config->current['CTIHOOK'])){
121 $dialmode= $_GET['dial'];
122 if ($dialmode == "telephoneNumber" ||
123 $dialmode == "mobile" ||
124 $dialmode == "homePhone"){
126 /* Get target */
127 $ldap= $this->config->get_ldap_link();
128 $ldap->cat(base64_decode($_GET['target']));
129 $attrs= $ldap->fetch();
130 if (isset($attrs["$dialmode"])){
131 $target= $attrs[$dialmode][0];
132 } else {
133 $target= "";
134 }
136 /* Get source */
137 $ui= get_userinfo();
138 $ldap->cat($ui->dn);
139 $attrs= $ldap->fetch();
140 if (isset($attrs["telephoneNumber"])){
141 $source= $attrs['telephoneNumber'][0];
142 } else {
143 $source= "";
144 }
146 /* Save to session */
147 $_SESSION['source']= $source;
148 $_SESSION['target']= $target;
150 /* Perform call */
151 if ($target != "" && $source != ""){
152 $smarty->assign("phone_image", get_template_path('images/phone.png'));
153 $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>"));
154 return($smarty->fetch(get_template_path('dial.tpl', TRUE)));
155 return;
156 } else {
157 print_red (_("You have no personal phone number set. Please change that in order to perform direct dials."));
158 }
159 }
161 }
163 /* Finally dial */
164 if (isset($_POST['dial']) && isset($_SESSION['source']) && isset($_SESSION['target'])){
165 exec ($this->config->current['CTIHOOK']." '".$_SESSION['source']."' '".$_SESSION['target']."'", $dummy, $retval);
166 unset($_SESSION['source']);
167 unset($_SESSION['target']);
168 }
170 /* Delete entry? */
171 if (isset($_POST['delete_entry_confirm'])){
173 /* Some nice guy may send this as POST, so we've to check
174 for the permissions again. */
175 if (chkacl($this->acl, "delete") == ""){
177 /* Delete request is permitted, perform LDAP action */
178 $ldap= $this->config->get_ldap_link();
179 $ldap->rmdir ($this->dn);
180 gosa_log ("Address book object'".$this->dn."' has been removed");
182 } else {
184 /* Normally this shouldn't be reached, send some extra
185 logs to notify the administrator */
186 print_red (_("You are not allowed to delete this entry!"));
187 gosa_log ("Warning: '".$this->ui->uid."' tried to trick address book deletion.");
188 }
190 /* Remove lock file after successfull deletion */
191 del_lock ($this->dn);
193 /* Clean up */
194 if (isset($_SESSION['saved_start'])){
195 $_GET['start']= $_SESSION['saved_start'];
196 }
197 unset($_SESSION['show_info']);
198 unset($_SESSION['saved_start']);
199 }
201 /* Delete entry? */
202 if (isset($_POST['delete_cancel'])){
203 del_lock ($this->dn);
204 }
206 /* Save address entry? */
207 if (isset($_POST['save'])){
208 $this->save_object();
209 $this->storage_base= $_POST['storage_base'];
211 /* Perform checks */
212 $message= $this->check ();
214 /* No errors, save object */
215 if (count ($message) == 0){
216 $this->save();
217 gosa_log ("Addressbook object '".$this->dn."' has been saved");
219 /* Clean up */
220 if (isset($_SESSION['saved_start'])){
221 $_GET['start']= $_SESSION['saved_start'];
222 }
223 $_SESSION['show_info']= $this->dn;
224 unset($_SESSION['saved_start']);
225 } else {
226 /* Errors found, show message */
227 show_errors ($message);
228 }
229 }
231 /* Close info window */
232 if (isset($_GET['close']) || isset($_POST['cancel'])){
233 if (isset($_SESSION['saved_start'])){
234 $_GET['start']= $_SESSION['saved_start'];
235 }
236 unset($_SESSION['show_info']);
237 unset($_SESSION['saved_start']);
238 }
240 /* Start address book edit mode? */
241 if (isset($_GET['global'])){
242 if (!isset($_SESSION['saved_start']) && isset($_GET['start'])){
243 $_SESSION['saved_start']= $_GET['start'];
244 }
245 switch ($_GET['global']){
246 case "add":
247 $this->dn= "new";
248 $this->orig_cn= "";
250 /* Clean values */
251 foreach ($this->attributes as $name){
252 $this->$name= "";
253 }
254 $this->storage_base= $this->config->current["BASE"];
255 break;
256 case "edit":
257 /* Clean values */
258 foreach ($this->attributes as $name){
259 $this->$name= "";
260 }
261 $this->dn= $_SESSION['show_info'];
262 $this->load();
263 $this->orig_cn= $this->cn;
264 break;
265 case "remove":
266 $this->dn= $_SESSION['show_info'];
267 $this->load();
268 /* Load permissions for selected 'dn' and check if
269 we're allowed to remove this 'dn' */
270 if (chkacl($this->acl, "delete") == ""){
272 /* Check locking, save current plugin in 'back_plugin', so
273 the dialog knows where to return. */
274 if (($user= get_lock($this->dn)) != ""){
275 return(gen_locked_message ($user, $this->dn));
276 }
278 /* Lock the current entry, so nobody will edit it during deletion */
279 $ui= get_userinfo();
280 add_lock ($this->dn, $ui->dn);
281 $smarty->assign("info", sprintf(_("You're about to delete the entry %s."), $this->dn));
282 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
283 } else {
285 /* Obviously the user isn't allowed to delete. Show message and
286 clean session. */
287 print_red (_("You are not allowed to delete this entry!"));
288 }
289 }
290 $_SESSION['show_info']= "ADD";
291 }
293 /* Open info window */
294 if (isset($_GET['show'])){
295 if (!isset($_SESSION['saved_start'])){
296 $_SESSION['saved_start']= $_GET['start'];
297 }
298 $_SESSION['show_info']= base64_decode($_GET['show']);
299 }
301 /* Get ldap link / build filter */
302 $ldap= $this->config->get_ldap_link();
303 $this->telephone_list= array ();
305 /* Assemble bases */
306 $bases= array();
307 $filter= "";
308 if ($phonefilter['global'] == "checked"){
309 $bases[]= preg_replace("/".$this->config->current['BASE']."/", "dc=addressbook,".$this->config->current['BASE'], $this->search_base);
310 } else {
311 $filter= '(objectClass=gosaAccount)';
312 }
313 if ($phonefilter['organizational'] == "checked"){
314 $bases[]= $this->search_base;
315 }
316 foreach ($bases as $base){
317 $ldap->cd ($base);
318 if ($phonefilter['object_type'] == '*'){
319 $ldap->search ("(&(objectClass=person)$filter(!(objectClass=gosaUserTemplate))(!(uid=*$))". //array
320 "(|(uid=$s)(homePhone=$s)(telephoneNumber=$s)".
321 "(facsimileTelephoneNumber=$s)(mobile=$s)(givenName=$s)(sn=$s)))", array("sn", "givenName", "telephoneNumber", "facsimileTelephoneNumber", "mobile", "homePhone", "uid", "mail", "cn"));
322 } else {
323 $ldap->search ("(&$filter(!(uid=*$))(!(objectClass=gosaUserTemplate))". //array
324 "(".$phonefilter['object_type']."=$s))", array("sn", "givenName", "telephoneNumber", "facsimileTelephoneNumber", "mobile", "homePhone", "uid", "mail", "cn"));
325 }
327 /* Build current list, error reporting is off, because many of the
328 objects may not be defined after LDAP queries. Asking for presence
329 first is too much overhead. */
330 error_reporting(0);
335 /* Walk through LDAP results */
336 while ($attrs= $ldap->fetch()){
338 #! hickert
339 /* prevent empty vaiables */
340 foreach($this->attributes as $atr) {
341 if(!isset($attrs[$atr][0])) {
342 $attrs[$atr][0] = "";
343 }
344 }
345 if(!isset($_GET['start'])) $_GET['start']="";
348 /* Only show lines that have set any mail or phone informations */
349 if (isset($attrs['telephoneNumber'][0]) ||
350 isset($attrs['facsimileTelephoneNumber'][0]) ||
351 isset($attrs['mobile'][0]) ||
352 isset($attrs['homePhone'][0]) ||
353 isset($attrs['mail'][0])){
355 $this->telephone_list[$attrs['sn'][0].$attrs['dn']]=
357 "<td class=\"phonelist\" title=\"".$attrs['sn'][0].", ".$attrs['givenName'][0]."\" onClick='location.href=\"main.php?plug=".validate($_GET['plug'])."&start=".validate($_GET['start'])."&show=".base64_encode($attrs['dn'])."\"'><a style='vertical-align:middle;' href=\"main.php?plug=".validate($_GET['plug'])."&start=".validate($_GET['start'])."&show=".base64_encode($attrs['dn'])."\">".$attrs['sn'][0].", ".$attrs['givenName'][0].
358 "</a>
359 </td>
360 <td title=\""._("Dial")." ".$attrs['telephoneNumber'][0]."\">
361 <a style='vertical-align:middle;' href=\"main.php?plug=".validate($_GET['plug'])."&dial=telephoneNumber&start=".validate($_GET['start'])."&target=".base64_encode($attrs['dn'])."\">".$attrs['telephoneNumber'][0]."
362 </a>
363 </td>
364 <td title=\"".$attrs['facsimileTelephoneNumber'][0]."\">
365 ".$attrs['facsimileTelephoneNumber'][0]."
366 </td>
367 <td title=\""._("Dial")." ".$attrs['mobile'][0]."\">
368 <a style='vertical-align:middle;' href=\"main.php?plug=".validate($_GET['plug'])."&dial=mobile&start=".validate($_GET['start'])."&target=".base64_encode($attrs['dn'])."\">".$attrs['mobile'][0]."
369 </a>
370 </td>
371 <td title=\""._("Dial")." ".$attrs['homePhone'][0]."\">
372 <a style='vertical-align:middle;' href=\"main.php?plug=".validate($_GET['plug'])."&dial=homePhone&start=".validate($_GET['start'])."&target=".base64_encode($attrs['dn'])."\">".$attrs['homePhone'][0]."
373 </a>
374 </td>
375 <td>
376 <a href=\"getvcard.php?dn=".base64_encode($attrs['dn'])."\">
377 <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])."\">
378 </a>";
380 if (isset($attrs['mail'])){
381 $dest= sprintf(_("Send mail to %s"), $attrs['mail'][0]);
382 $this->telephone_list[$attrs['sn'][0].$attrs['dn']].=
384 "<a href=\"mailto:".htmlentities($attrs['mail'][0])."\">".
385 "<img align=\"top\" border=0 src=\"images/mailto.png\" ".
386 "alt=\"vcf\" title=\"$dest\"></a>";
387 }
388 $this->telephone_list[$attrs['sn'][0].$attrs['dn']].= "</td>";
389 }
390 }
391 error_reporting(E_ALL);
392 }
394 /* Sort up list */
395 ksort ($this->telephone_list);
396 reset ($this->telephone_list);
398 /* Fill template variables */
399 $smarty->assign("search_for", $this->search_for);
400 $smarty->assign("object_type", $this->object_type);
401 $smarty->assign("deplist", $this->config->idepartments);
402 $smarty->assign("depselect", $this->search_base);
403 $smarty->assign("global", $phonefilter['global']);
404 $smarty->assign("organizational", $phonefilter['organizational']);
405 $smarty->assign("search_image", get_template_path('images/search.png'));
406 $smarty->assign("obj_image", get_template_path('images/list_ogroup.png'));
407 $smarty->assign("tree_image", get_template_path('images/tree.png'));
408 $smarty->assign("infoimage", get_template_path('images/info.png'));
409 $smarty->assign("actionimage", get_template_path('images/action.png'));
410 $smarty->assign("launchimage", get_template_path('images/launch.png'));
412 /* Generate alphabet */
413 $alphabet= generate_alphabet();
415 /* Build list output */
416 $output= "";
417 $mod= 0;
419 #! hickert
420 if(!isset($_SESSION['show_info'])) $smarty->assign("show_info", "");;
422 if (isset($_SESSION['show_info'])){
423 $range= 4;
424 $smarty->assign("show_info", "1");
425 $smarty->assign("url", "main.php?plug=".validate($_GET['plug'])."&close=1");
427 switch ($_SESSION['show_info']){
429 case "ADD":
430 $smarty->assign ('storage_base', $this->storage_base);
431 $smarty->assign ('address_info',
432 get_template_path('address_edit.tpl', TRUE));
433 break;
435 default:
436 $smarty->assign ('address_info',
437 get_template_path('address_info.tpl', TRUE));
438 break;
439 }
441 /* Fill variables from LDAP */
442 if ($_SESSION['show_info'] != "ADD"){
443 $ldap->cat($_SESSION['show_info']);
444 $info= $ldap->fetch();
445 }
446 foreach ($this->attributes as $name){
447 if ($_SESSION['show_info'] != "ADD" && isset($info["$name"][0])){
448 error_reporting(0);
449 /* Special treatment for phone attributes */
450 if ($name == "mobile" ||
451 $name == "homePhone" ||
452 $name == "telephoneNumber"){
453 $smarty->assign("info_$name",
454 "<a title=\""._("Dial")." ".$info["$name"][0]."\" href=\"main.php?plug=".validate($_GET['plug'])."&dial=$name&start=".validate($_GET['start'])."&target=".base64_encode($_SESSION['show_info'])."\">".$info["$name"][0]."</a>");
455 } else {
456 $smarty->assign("info_$name", preg_replace("/\n/", "<br>", $info["$name"][0]));
457 }
458 error_reporting(E_ALL);
459 } elseif ($_SESSION['show_info'] == "ADD" && isset($this->$name)) {
460 $smarty->assign("info_$name", $this->$name);
461 } else {
462 $smarty->assign("info_$name", "-");
463 }
464 }
465 if (preg_match("/,dc=addressbook,/", $_SESSION['show_info'])){
466 $storage= _("global addressbook");
467 $smarty->assign("internal", 0);
468 } else {
469 $storage= _("user database");
470 $smarty->assign("internal", 1);
471 }
472 if ($_SESSION['show_info'] != "ADD"){
473 $smarty->assign("storage_info", sprintf(_("Contact stored in '%s'"), $storage));
474 } else {
475 $smarty->assign("storage_info", _("Creating new entry in"));
476 }
477 } else {
480 if(isset($_POST['EntryPerPage'])){
481 $this->range = $_POST['EntryPerPage'];
482 }
483 $range = $this->range;
484 $smarty->assign("internal", 1);
485 }
486 if (isset($_GET['start'])){
487 $this->start= validate($_GET['start']);
488 }
489 foreach ($this->telephone_list as $val){
490 if ($mod < $this->start) {
491 $mod++;
492 continue;
493 }
494 if ($mod >= ($this->start + $range)){
495 $mod++;
496 break;
497 }
498 if ( ($mod++) & 1){
499 $col= "style=\"background-color: #ECECEC;\"";
500 } else {
501 $col= "style=\"background-color: #F5F5F5;\"";
502 }
503 $output.= "<tr $col>\n$val</tr>\n";
504 }
506 $smarty->assign("search_result", $output);
507 $smarty->assign("apply", apply_filter());
508 $smarty->assign("alphabet", $alphabet);
509 if($range < 20){
510 $smarty->assign("range_selector", range_selector(count($this->telephone_list), $this->start, $range));
511 }else{
512 $smarty->assign("range_selector", range_selector(count($this->telephone_list), $this->start, $range, "EntryPerPage"));
513 }
514 $tmp= array("*" => _("All"), "sn" => _("Name"), "givenName" => _("Given name"),
515 "telephoneNumber" => _("Work phone"), "mobile" => _("Cell phone"),
516 "homePhone" => _("Home phone"), "uid" => _("User ID"));
517 natsort($tmp);
518 $smarty->assign("objlist", $tmp);
520 /* Show main page */
521 $smarty->assign ('personal_image', get_template_path('images/addr_personal.png'));
522 $smarty->assign ('home_image', get_template_path('images/addr_home.png'));
523 $smarty->assign ('company_image', get_template_path('images/addr_company.png'));
524 $smarty->assign ('add_image', get_template_path('images/editpaste.png'));
525 $smarty->assign ('edit_image', get_template_path('images/edit.png'));
526 $smarty->assign ('delete_image', get_template_path('images/editdelete.png'));
527 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
528 }
530 function save_object()
531 {
532 plugin::save_object();
533 foreach($this->attributes as $attr){
534 if(isset($_POST[$attr])){
535 $this->$attr = $_POST[$attr];
536 }
537 }
538 }
540 function check()
541 {
542 $message= array();
544 /* must: sn, givenName */
545 if ($this->sn == ""){
546 $message[]= _("The required field 'Name' is not set.");
547 return ($message);
548 }
549 if ($this->givenName == ""){
550 $message[]= _("The required field 'Given name' is not set.");
551 return ($message);
552 }
554 /* Check for valid name definition */
555 if (preg_match ("/[\\\\]/", $this->sn)){
556 $message[]= _("The field 'Name' contains invalid characters.");
557 }
558 if (preg_match ("/[\\\\]/", $this->givenName)){
559 $message[]= _("The field 'Given name' contains invalid characters.");
560 }
562 /* Check phone numbers */
563 if (!is_phone_nr($this->homePhone)){
564 $message[]= _("The field 'Phone' contains an invalid phone number.");
565 }
566 if (!is_phone_nr($this->telephoneNumber)){
567 $message[]= _("The field 'Phone' contains an invalid phone number.");
568 }
569 if (!is_phone_nr($this->facsimileTelephoneNumber)){
570 $message[]= _("The field 'Fax' contains an invalid phone number.");
571 }
572 if (!is_phone_nr($this->mobile)){
573 $message[]= _("The field 'Mobile' contains an invalid phone number.");
574 }
575 if (!is_phone_nr($this->pager)){
576 $message[]= _("The field 'Pager' contains an invalid phone number.");
577 }
579 /* Check for reserved characers */
580 if (preg_match ('/[,+"<>;]/', $this->givenName)){
581 $message[]= _("The field 'Given name' contains invalid characters.");
582 }
583 if (preg_match ('/[,+"<>;]/', $this->sn)){
584 $message[]= _("The field 'Name' contains invalid characters.");
585 }
587 /* Check mail */
588 if (!is_email($this->mail)){
589 $message[]= _("Please enter a valid email address in 'Primary address' field.");
590 }
592 /* Assemble cn/dn */
593 $this->cn= $this->givenName." ".$this->sn;
594 if ($this->orig_cn != $this->cn || $this->storage_base != $this->orig_storage_base){
595 $this->new_dn= $this->create_unique_dn("cn", preg_replace("/,*".$this->config->current['BASE']."$/", "", $this->storage_base).",dc=addressbook,".$this->config->current['BASE']);
596 if ($this->new_dn == "none"){
597 $message[]= _("Cannot create a unique DN for your entry. Please fill more formular fields.");
598 return ($message);
599 }
600 } else {
601 $this->new_dn= $this->dn;
602 }
604 if ($_SESSION['show_info'] == "ADD" && chkacl($this->acl, "add") != ""){
605 $message[]= _("You have no permissions to create or modify a global address book entry.");
606 }
608 return ($message);
609 }
612 function load()
613 {
614 /* Load base attributes */
615 plugin::plugin ($this->config, $this->dn);
616 $this->storage_base= preg_replace('/^[^,]+,/', '', preg_replace('/dc=addressbook,/', '', $this->dn));
617 }
620 function save()
621 {
622 /* First use parents methods to do some basic fillup in $this->attrs */
623 plugin::save ();
625 $this->attrs['cn']= $this->cn;
626 $this->attrs['displayName']= $this->givenName." ".$this->sn;
628 /* Move entry if it got another name... */
629 if ($this->dn != "new" && $this->dn != $this->new_dn){
630 $this->move($this->dn, $this->new_dn);
631 }
632 $this->dn= $this->new_dn;
634 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
635 new entries. So do a check first... */
636 $ldap= $this->config->get_ldap_link();
637 $ldap->cat ($this->dn);
638 if ($ldap->fetch()){
639 $mode= "modify";
640 } else {
641 $mode= "add";
642 $ldap->cd($this->config->current['BASE']);
643 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
644 }
646 /* Finally write data with selected 'mode' */
647 $ldap->cd ($this->dn);
648 $ldap->$mode ($this->attrs);
649 if (show_ldap_error($ldap->get_error())){
650 return (1);
651 }
652 }
654 }
655 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
656 ?>