1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 Cajus Pollmeier
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 /* Basic setup, remove eventually registered sessions */
22 @require_once ("../include/php_setup.inc");
23 @require_once ("functions.inc");
24 error_reporting (0);
25 session::start();
26 session::set('errorsAlreadyPosted',array());
28 /* Logged in? Simple security check */
29 if (!session::is_set('ui')){
30 new log("security","all/all","",array(),"Error: getvcard.php called without session") ;
31 header ("Location: index.php");
32 exit;
33 }
35 /* Uid parameter set? */
36 if (!isset($_GET['dn']) || $_GET['dn'] == ""){
37 msg_dialog::display(_("Internal error"), _("Missing parameters!"), FATAL_ERROR_DIALOG);
38 exit;
39 }
41 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
42 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
43 header("Cache-Control: no-cache");
44 header("Pragma: no-cache");
45 header("Cache-Control: post-check=0, pre-check=0");
46 header("Content-type: text/x-vcard; charset=utf-8");
47 header("Content-type: text/plain");
48 if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
49 preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
51 header('Content-Disposition: filename="'.$name.'.vcf"');
52 } else {
53 header('Content-Disposition: attachment; filename="'.$name.'.vcf"');
54 }
56 /* Get entry */
57 $config= session::get('config');
58 $ldap= $config->get_ldap_link();
59 $ldap->cat(base64_decode(validate($_GET['dn'])));
61 /*
62 * Generate vcard for specified IDs
63 */
64 while ($attrs= $ldap->fetch()){
65 /* Header / Name */
66 echo "BEGIN:VCARD\n";
67 echo "VERSION:3.0\n";
68 echo "FN:".preg_replace('/,/', '\\,', $attrs['cn'][0])."\n";
70 /* Assemble titles for N attribute */
71 $titles= "";
72 if (isset($attrs['personalTitle'])){
73 $titles= $attrs['personalTitle'][0];
74 }
75 if (isset($attrs['academicTitle'])){
76 if ($titles != ""){
77 $titles.= ",".$attrs['academicTitle'][0];
78 } else {
79 $titles= $attrs['academicTitle'][0];
80 }
81 }
82 echo "N:".$attrs['sn'][0].";".$attrs['givenName'][0].";;;$titles\n";
84 /* Generate random UID */
85 $uid= "";
86 srand(make_seed());
87 $chars= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
88 for ($i= 0; $i<16; $i++){
89 $uid.= $chars[rand(0, strlen($chars)-1)];
90 }
91 echo "UID:$uid\n";
93 /* Mail addresses */
94 if (isset($attrs['mail'][0])){
95 echo "EMAIL;TYPE=internet,pref:".$attrs['mail'][0]."\n";
96 if (isset($attrs['gosaMailAlternateAddress'])){
97 for ($i= 0; $i<$attrs['gosaMailAlternateAddress']['count']; $i++){
98 echo "EMAIL;TYPE=internet:".
99 $attrs['gosaMailAlternateAddress'][$i]."\n";
100 }
101 }
102 }
104 /* Export date */
105 echo "REV:".date("Y-m-d")."\n";
106 echo "CLASS:PUBLIC\n";
108 /* Fill address */
109 if (isset($attrs['homePostalAddress'])){
110 @list($street,$town,$country)= preg_split('/\n/', $attrs['homePostalAddress'][0]);
111 echo "ADR;TYPE=home:;;".trim($street).";".trim($town).";;;".
112 trim($country)."\n";
113 }
114 if (isset($attrs['postalAddress'])){
115 @list($street,$town,$country)= preg_split('/\n/', $attrs['postalAddress'][0]);
116 echo "ADR;TYPE=work,pref:;;".trim($street).";".trim($town).";;;".
117 trim($country)."\n";
118 }
120 /* Telephone numbers */
121 if (isset($attrs['homePhone'])){
122 echo "TEL;TYPE=home:".$attrs['homePhone'][0]."\n";
123 }
124 if (isset($attrs['telephoneNumber'])){
125 echo "TEL;TYPE=work,pref:".$attrs['telephoneNumber'][0]."\n";
126 }
127 if (isset($attrs['mobile'])){
128 echo "TEL;TYPE=cell:".$attrs['mobile'][0]."\n";
129 }
130 if (isset($attrs['pager'])){
131 echo "TEL;TYPE=pager:".$attrs['pager'][0]."\n";
132 }
134 /* Set organization */
135 if (isset($attrs['o'])){
136 echo "ORG:".$attrs['o'][0]."\n";
137 }
139 echo "NOTE:Exported by GOsa - https://gosa.gonicus.de\n";
140 echo "SORT-STRING:".$attrs['sn'][0]."\n";
141 if (isset($attrs['labeledURI'][0])){
142 echo "URL:".$attrs['labeledURI'][0]."\n";
143 }
145 /* Add user certificate */
146 #if (isset($attrs['userCertificate;binary'])){
147 # $cert= $ldap->get_attribute($ldap->getDN(), "userCertificate;binary");
148 # $cert= preg_replace('/\r\n/', chr(10).' ', chunk_split(base64_encode($cert)));
149 # echo "KEY;ENCODING=b:".preg_replace('/\n $/', '', $cert)."\n";
150 #}
152 /* Add picture */
153 if (isset($attrs['jpegPhoto'])){
154 $photodata= $ldap->get_attribute($ldap->getDN(), "jpegPhoto");
155 $photodata= preg_replace('/\r\n/', chr(10).' ', chunk_split(base64_encode($photodata)));
156 echo "PHOTO;ENCODING=b;TYPE=JPEG:".preg_replace('/\n $/', '', $photodata)."\n";
157 }
159 /* Day of birth */
160 if (isset($attrs['dayOfBirth'][0])){
161 echo "BDAY:".$attrs['dayOfBirth'][0]."\n";
162 }
164 echo "END:VCARD\n\n";
165 }
167 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
168 ?>