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