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 $smarty= get_smarty();
75 #! Hickert
76 /*prevent empty variables for smarty*/
77 foreach($this->attributes as $atr) $smarty->assign($atr,"");
79 /* Save formular information */
80 $phonefilter= get_global("phonefilter");
81 foreach( array("search_for", "search_base", "object_type") as $type){
82 if (isset($_POST[$type])){
83 $phonefilter[$type]= $_POST[$type];
84 }
85 $this->$type= $phonefilter[$type];
86 }
87 if (isset($_POST['search_base'])){
88 foreach( array("organizational", "global") as $type){
89 if (isset($_POST[$type])){
90 $phonefilter[$type]= "checked";
91 } else {
92 $phonefilter[$type]= "";
93 }
94 }
95 }
97 /* Search string */
98 $s= $phonefilter['search_for'];
99 if ($s == "") {
100 $s= "*";
101 }
102 if (isset($_GET['search'])){
103 $s= validate(mb_substr($_GET['search'], 0, 1, "UTF8"))."*";
104 if ($s == "**"){
105 $s= "*";
106 }
107 $this->search_for= $s;
108 $phonefilter['search_for']= $s;
109 }
110 register_global("phonefilter", $phonefilter);
112 /* Perform actions with CTI hook */
113 if (isset($_GET['target'])
114 && isset($_GET['dial'])
115 && isset($this->config->current['CTIHOOK'])){
117 $dialmode= $_GET['dial'];
118 if ($dialmode == "telephoneNumber" ||
119 $dialmode == "mobile" ||
120 $dialmode == "homePhone"){
122 /* Get target */
123 $ldap= $this->config->get_ldap_link();
124 $ldap->cat(base64_decode($_GET['target']));
125 $attrs= $ldap->fetch();
126 if (isset($attrs["$dialmode"])){
127 $target= $attrs[$dialmode][0];
128 } else {
129 $target= "";
130 }
132 /* Get source */
133 $ui= get_userinfo();
134 $ldap->cat($ui->dn);
135 $attrs= $ldap->fetch();
136 if (isset($attrs["telephoneNumber"])){
137 $source= $attrs['telephoneNumber'][0];
138 } else {
139 $source= "";
140 }
142 /* Save to session */
143 $_SESSION['source']= $source;
144 $_SESSION['target']= $target;
146 /* Perform call */
147 if ($target != "" && $source != ""){
148 $smarty->assign("phone_image", get_template_path('images/phone.png'));
149 $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>"));
150 return($smarty->fetch(get_template_path('dial.tpl', TRUE)));
151 return;
152 } else {
153 print_red (_("You have no personal phone number set. Please change that in order to perform direct dials."));
154 }
155 }
157 }
159 /* Finally dial */
160 if (isset($_POST['dial']) && isset($_SESSION['source']) && isset($_SESSION['target'])){
161 exec ($this->config->current['CTIHOOK']." '".$_SESSION['source']."' '".$_SESSION['target']."'", $dummy, $retval);
162 unset($_SESSION['source']);
163 unset($_SESSION['target']);
164 }
166 /* Delete entry? */
167 if (isset($_POST['delete_entry_confirm'])){
169 /* Some nice guy may send this as POST, so we've to check
170 for the permissions again. */
171 if (chkacl($this->acl, "delete") == ""){
173 /* Delete request is permitted, perform LDAP action */
174 $ldap= $this->config->get_ldap_link();
175 $ldap->rmdir ($this->dn);
176 gosa_log ("Address book object'".$this->dn."' has been removed");
178 } else {
180 /* Normally this shouldn't be reached, send some extra
181 logs to notify the administrator */
182 print_red (_("You are not allowed to delete this entry!"));
183 gosa_log ("Warning: '".$this->ui->uid."' tried to trick address book deletion.");
184 }
186 /* Remove lock file after successfull deletion */
187 del_lock ($this->dn);
189 /* Clean up */
190 if (isset($_SESSION['saved_start'])){
191 $_GET['start']= $_SESSION['saved_start'];
192 }
193 unset($_SESSION['show_info']);
194 unset($_SESSION['saved_start']);
195 }
197 /* Delete entry? */
198 if (isset($_POST['delete_cancel'])){
199 del_lock ($this->dn);
200 }
202 /* Save address entry? */
203 if (isset($_POST['save'])){
204 $this->save_object();
205 $this->storage_base= $_POST['storage_base'];
207 /* Perform checks */
208 $message= $this->check ();
210 /* No errors, save object */
211 if (count ($message) == 0){
212 $this->save();
213 gosa_log ("Addressbook object '".$this->dn."' has been saved");
215 /* Clean up */
216 if (isset($_SESSION['saved_start'])){
217 $_GET['start']= $_SESSION['saved_start'];
218 }
219 $_SESSION['show_info']= $this->dn;
220 unset($_SESSION['saved_start']);
221 } else {
222 /* Errors found, show message */
223 show_errors ($message);
224 }
225 }
227 /* Close info window */
228 if (isset($_GET['close']) || isset($_POST['cancel'])){
229 if (isset($_SESSION['saved_start'])){
230 $_GET['start']= $_SESSION['saved_start'];
231 }
232 unset($_SESSION['show_info']);
233 unset($_SESSION['saved_start']);
234 }
236 /* Start address book edit mode? */
237 if (isset($_GET['global'])){
238 if (!isset($_SESSION['saved_start']) && isset($_GET['start'])){
239 $_SESSION['saved_start']= $_GET['start'];
240 }
241 switch ($_GET['global']){
242 case "add":
243 $this->dn= "new";
244 $this->orig_cn= "";
246 /* Clean values */
247 foreach ($this->attributes as $name){
248 $this->$name= "";
249 }
250 $this->storage_base= $this->config->current["BASE"];
251 break;
252 case "edit":
253 /* Clean values */
254 foreach ($this->attributes as $name){
255 $this->$name= "";
256 }
257 $this->dn= $_SESSION['show_info'];
258 $this->load();
259 $this->orig_cn= $this->cn;
260 break;
261 case "remove":
262 $this->dn= $_SESSION['show_info'];
263 $this->load();
264 /* Load permissions for selected 'dn' and check if
265 we're allowed to remove this 'dn' */
266 $ui= get_userinfo();
267 $dn= preg_replace("/,dc=addressbook,/", "", $this->dn);
268 $acl= get_permissions ($dn, $ui->subtreeACL);
269 $this->acl= get_module_permission($acl, "global-addressbook", $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 add_lock ($this->dn, $ui->dn);
280 $smarty->assign("info", sprintf(_("You're about to delete the entry %s."), $this->dn));
281 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
282 } else {
284 /* Obviously the user isn't allowed to delete. Show message and
285 clean session. */
286 print_red (_("You are not allowed to delete this entry!"));
287 }
288 }
289 $_SESSION['show_info']= "ADD";
290 }
292 /* Open info window */
293 if (isset($_GET['show'])){
294 if (!isset($_SESSION['saved_start'])){
295 $_SESSION['saved_start']= $_GET['start'];
296 }
297 $_SESSION['show_info']= base64_decode($_GET['show']);
298 }
300 /* Get ldap link / build filter */
301 $ldap= $this->config->get_ldap_link();
302 $this->telephone_list= array ();
304 /* Assemble bases */
305 $bases= array();
306 $filter= "";
307 if ($phonefilter['global'] == "checked"){
308 $bases[]= preg_replace("/".$this->config->current['BASE']."/", "dc=addressbook,".$this->config->current['BASE'], $this->search_base);
309 } else {
310 $filter= '(objectClass=gosaAccount)';
311 }
312 if ($phonefilter['organizational'] == "checked"){
313 $bases[]= $this->search_base;
314 }
315 foreach ($bases as $base){
316 $ldap->cd ($base);
317 if ($phonefilter['object_type'] == '*'){
318 $ldap->search ("(&(objectClass=person)$filter(!(objectClass=gosaUserTemplate))(!(uid=*$))".
319 "(|(uid=$s)(homePhone=$s)(telephoneNumber=$s)".
320 "(facsimileTelephoneNumber=$s)(mobile=$s)(givenName=$s)(sn=$s)))", array("sn", "givenName", "telephoneNumber", "facsimileTelephoneNumber", "mobile", "homePhone", "uid", "mail", "cn"));
321 } else {
322 $ldap->search ("(&$filter(!(uid=*$))(!(objectClass=gosaUserTemplate))".
323 "(".$phonefilter['object_type']."=$s))", array("sn", "givenName", "telephoneNumber", "facsimileTelephoneNumber", "mobile", "homePhone", "uid", "mail", "cn"));
324 }
326 /* Build current list, error reporting is off, because many of the
327 objects may not be defined after LDAP queries. Asking for presence
328 first is too much overhead. */
329 error_reporting(0);
334 /* Walk through LDAP results */
335 while ($attrs= $ldap->fetch()){
337 #! hickert
338 /* prevent empty vaiables */
339 foreach($this->attributes as $atr) {
340 if(!isset($attrs[$atr][0])) {
341 $attrs[$atr][0] = "";
342 }
343 }
344 if(!isset($_GET['start'])) $_GET['start']="";
347 /* Only show lines that have set any mail or phone informations */
348 if (isset($attrs['telephoneNumber'][0]) ||
349 isset($attrs['facsimileTelephoneNumber'][0]) ||
350 isset($attrs['mobile'][0]) ||
351 isset($attrs['homePhone'][0]) ||
352 isset($attrs['mail'][0])){
354 $this->telephone_list[$attrs['sn'][0].$attrs['dn']]=
356 "<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].
357 "</a>
358 </td>
359 <td title=\""._("Dial")." ".$attrs['telephoneNumber'][0]."\">
360 <a href=\"main.php?plug=".validate($_GET['plug'])."&dial=telephoneNumber&start=".validate($_GET['start'])."&target=".base64_encode($attrs['dn'])."\">".$attrs['telephoneNumber'][0]."
361 </a>
362 </td>
363 <td title=\"".$attrs['facsimileTelephoneNumber'][0]."\">
364 ".$attrs['facsimileTelephoneNumber'][0]."
365 </td>
366 <td title=\""._("Dial")." ".$attrs['mobile'][0]."\">
367 <a href=\"main.php?plug=".validate($_GET['plug'])."&dial=mobile&start=".validate($_GET['start'])."&target=".base64_encode($attrs['dn'])."\">".$attrs['mobile'][0]."
368 </a>
369 </td>
370 <td title=\""._("Dial")." ".$attrs['homePhone'][0]."\">
371 <a href=\"main.php?plug=".validate($_GET['plug'])."&dial=homePhone&start=".validate($_GET['start'])."&target=".base64_encode($attrs['dn'])."\">".$attrs['homePhone'][0]."
372 </a>
373 </td>
374 <td>
375 <a href=\"getvcard.php?dn=".base64_encode($attrs['dn'])."\">
376 <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])."\">
377 </a>";
379 if (isset($attrs['mail'])){
380 $dest= sprintf(_("Send mail to %s"), $attrs['mail'][0]);
381 $this->telephone_list[$attrs['sn'][0].$attrs['dn']].=
383 "<a href=\"mailto:".$attrs['mail'][0]."\">".
384 "<img align=\"top\" border=0 src=\"images/mailto.png\" ".
385 "alt=\"vcf\" title=\"$dest\"></a>";
386 }
387 $this->telephone_list[$attrs['sn'][0].$attrs['dn']].= "</td>";
388 }
389 }
390 error_reporting(E_ALL);
391 }
393 /* Sort up list */
394 ksort ($this->telephone_list);
395 reset ($this->telephone_list);
397 /* Fill template variables */
398 $smarty->assign("search_for", $this->search_for);
399 $smarty->assign("object_type", $this->object_type);
400 $smarty->assign("deplist", $this->config->idepartments);
401 $smarty->assign("depselect", $this->search_base);
402 $smarty->assign("global", $phonefilter['global']);
403 $smarty->assign("organizational", $phonefilter['organizational']);
404 $smarty->assign("search_image", get_template_path('images/search.png'));
405 $smarty->assign("obj_image", get_template_path('images/list_ogroup.png'));
406 $smarty->assign("tree_image", get_template_path('images/tree.png'));
407 $smarty->assign("infoimage", get_template_path('images/info.png'));
408 $smarty->assign("actionimage", get_template_path('images/action.png'));
409 $smarty->assign("launchimage", get_template_path('images/launch.png'));
411 /* Generate alphabet */
412 $alphabet= generate_alphabet();
414 /* Build list output */
415 $output= "";
416 $mod= 0;
418 #! hickert
419 if(!isset($_SESSION['show_info'])) $smarty->assign("show_info", "");;
421 if (isset($_SESSION['show_info'])){
422 $range= 4;
423 $smarty->assign("show_info", "1");
424 $smarty->assign("url", "main.php?plug=".validate($_GET['plug'])."&close=1");
426 switch ($_SESSION['show_info']){
428 case "ADD":
429 $smarty->assign ('storage_base', $this->storage_base);
430 $smarty->assign ('address_info',
431 get_template_path('address_edit.tpl', TRUE));
432 break;
434 default:
435 $smarty->assign ('address_info',
436 get_template_path('address_info.tpl', TRUE));
437 break;
438 }
440 /* Fill variables from LDAP */
441 # FIXME: Missing ACL support for addressbook yet
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 {
478 $range= 20;
479 $smarty->assign("internal", 1);
480 }
481 if (isset($_GET['start'])){
482 $this->start= validate($_GET['start']);
483 }
484 foreach ($this->telephone_list as $val){
485 if ($mod < $this->start) {
486 $mod++;
487 continue;
488 }
489 if ($mod >= ($this->start + $range)){
490 $mod++;
491 break;
492 }
493 if ( ($mod++) & 1){
494 $col= "style=\"background-color: #ECECEC;\"";
495 } else {
496 $col= "style=\"background-color: #F5F5F5;\"";
497 }
498 $output.= "<tr $col>\n$val</tr>\n";
499 }
500 $smarty->assign("search_result", $output);
501 $smarty->assign("apply", apply_filter());
502 $smarty->assign("alphabet", $alphabet);
503 $smarty->assign("range_selector", range_selector(count($this->telephone_list), $this->start, $range));
504 $tmp= array("*" => _("All"), "sn" => _("Name"), "givenName" => _("Given name"),
505 "telephoneNumber" => _("Work phone"), "mobile" => _("Cell phone"),
506 "homePhone" => _("Home phone"), "uid" => _("User ID"));
507 natsort($tmp);
508 $smarty->assign("objlist", $tmp);
510 /* Show main page */
511 $smarty->assign ('personal_image', get_template_path('images/addr_personal.png'));
512 $smarty->assign ('home_image', get_template_path('images/addr_home.png'));
513 $smarty->assign ('company_image', get_template_path('images/addr_company.png'));
514 $smarty->assign ('add_image', get_template_path('images/editpaste.png'));
515 $smarty->assign ('edit_image', get_template_path('images/edit.png'));
516 $smarty->assign ('delete_image', get_template_path('images/editdelete.png'));
517 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
518 }
521 function check()
522 {
523 $message= array();
525 /* must: sn, givenName */
526 if ($this->sn == ""){
527 $message[]= _("The required field 'Name' is not set.");
528 return ($message);
529 }
530 if ($this->givenName == ""){
531 $message[]= _("The required field 'Given name' is not set.");
532 return ($message);
533 }
535 /* Check for valid name definition */
536 if (preg_match ("/[\\\\]/", $this->sn)){
537 $message[]= _("The field 'Name' contains invalid characters.");
538 }
539 if (preg_match ("/[\\\\]/", $this->givenName)){
540 $message[]= _("The field 'Given name' contains invalid characters.");
541 }
543 /* Check phone numbers */
544 if (!is_phone_nr($this->homePhone)){
545 $message[]= _("The field 'Phone' contains an invalid phone number.");
546 }
547 if (!is_phone_nr($this->telephoneNumber)){
548 $message[]= _("The field 'Phone' contains an invalid phone number.");
549 }
550 if (!is_phone_nr($this->facsimileTelephoneNumber)){
551 $message[]= _("The field 'Fax' contains an invalid phone number.");
552 }
553 if (!is_phone_nr($this->mobile)){
554 $message[]= _("The field 'Mobile' contains an invalid phone number.");
555 }
556 if (!is_phone_nr($this->pager)){
557 $message[]= _("The field 'Pager' contains an invalid phone number.");
558 }
560 /* Check for reserved characers */
561 if (preg_match ('/[,+"<>;]/', $this->givenName)){
562 $message[]= _("The field 'Given name' contains invalid characters.");
563 }
564 if (preg_match ('/[,+"<>;]/', $this->sn)){
565 $message[]= _("The field 'Name' contains invalid characters.");
566 }
568 /* Check mail */
569 if (!is_email($this->mail)){
570 $message[]= _("Please enter a valid email address in 'Primary address' field.");
571 }
573 /* Assemble cn/dn */
574 $this->cn= $this->givenName." ".$this->sn;
575 if ($this->orig_cn != $this->cn || $this->storage_base != $this->orig_storage_base){
576 $this->new_dn= $this->create_unique_dn("cn", preg_replace("/,*".$this->config->current['BASE']."$/", "", $this->storage_base).",dc=addressbook,".$this->config->current['BASE']);
577 if ($this->new_dn == "none"){
578 $message[]= _("Cannot create a unique DN for your entry. Please fill more formular fields.");
579 return ($message);
580 }
581 } else {
582 $this->new_dn= $this->dn;
583 }
585 $ui= get_userinfo();
586 $dn= preg_replace("/,dc=addressbook,/", "", $this->new_dn);
587 $acl= get_permissions ($dn, $ui->subtreeACL);
588 $acl= get_module_permission($acl, "global-addressbook", $this->dn);
589 if ($_SESSION['show_info'] == "ADD" && chkacl($acl, "create") != ""){
590 $message[]= _("You have no permissions to create or modify a global address book entry.");
591 }
593 return ($message);
594 }
597 function load()
598 {
599 /* Load base attributes */
600 plugin::plugin ($this->config, $this->dn);
601 $this->storage_base= preg_replace('/^[^,]+,/', '', preg_replace('/dc=addressbook,/', '', $this->dn));
602 }
605 function save()
606 {
607 /* First use parents methods to do some basic fillup in $this->attrs */
608 plugin::save ();
610 $this->attrs['cn']= $this->cn;
611 $this->attrs['displayName']= $this->givenName." ".$this->sn;
613 /* Move entry if it got another name... */
614 if ($this->dn != "new" && $this->dn != $this->new_dn){
615 $this->move($this->dn, $this->new_dn);
616 }
617 $this->dn= $this->new_dn;
619 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
620 new entries. So do a check first... */
621 $ldap= $this->config->get_ldap_link();
622 $ldap->cat ($this->dn);
623 if ($ldap->fetch()){
624 $mode= "modify";
625 } else {
626 $mode= "add";
627 $ldap->cd($this->config->current['BASE']);
628 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
629 }
631 /* Finally write data with selected 'mode' */
632 $ldap->cd ($this->dn);
633 $ldap->$mode ($this->attrs);
634 if (show_ldap_error($ldap->get_error())){
635 return (1);
636 }
637 }
639 }
641 ?>