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