Code

Fixed several errors
[gosa.git] / html / getvcard.php
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 ();
27 /* Logged in? Simple security check */
28 if (!isset($_SESSION['ui'])){
29   gosa_log ("Error: getvcard.php called without session");
30   header ("Location: ../index.php");
31   exit;
32 }
34 /* Uid parameter set? */
35 if (!isset($_GET['dn']) || $_GET['dn'] == ""){
36   print_red (_("Error: getcvard.php needs a parameter to export a vcard!"));
37   exit;
38 }
40 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
41 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
42 header("Cache-Control: no-cache");
43 header("Pragma: no-cache");
44 header("Cache-Control: post-check=0, pre-check=0");
45 header("Content-type: text/x-vcard; charset=utf-8");
46 header("Content-type: text/plain");
47 if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
48     preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
50   header('Content-Disposition: filename="'.$name.'.vcf"');
51 } else {
52   header('Content-Disposition: attachment; filename="'.$name.'.vcf"');
53 }
55 /* Get entry */
56 $config= $_SESSION['config'];
57 $ldap= $config->get_ldap_link();
58 $ldap->cat(base64_decode(validate($_GET['dn'])));
60 /* 
61  * Generate vcard for specified IDs
62  */
63 while ($attrs= $ldap->fetch()){
64   /* Header / Name */
65   echo "BEGIN:VCARD\n";
66   echo "VERSION:3.0\n";
67   echo "FN:".preg_replace('/,/', '\\,', $attrs['cn'][0])."\n";
69   /* Assemble titles for N attribute */
70   $titles= "";
71   if (isset($attrs['personalTitle'])){
72     $titles= $attrs['personalTitle'][0];
73   }
74   if (isset($attrs['academicTitle'])){
75     if ($titles != ""){
76       $titles.= ",".$attrs['academicTitle'][0];
77     } else {
78       $titles= $attrs['academicTitle'][0];
79     }
80   }
81   echo "N:".$attrs['sn'][0].";".$attrs['givenName'][0].";;;$titles\n";
83   /* Generate random UID */
84   $uid= "";
85   srand(make_seed());
86   $chars= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
87   for ($i= 0; $i<16; $i++){
88     $uid.= $chars[rand(0, strlen($chars)-1)];
89   }
90   echo "UID:$uid\n";
92   /* Mail addresses */
93   if (isset($attrs['mail'][0])){
94     echo "EMAIL;TYPE=internet,pref:".$attrs['mail'][0]."\n";
95     if (isset($attrs['gosaMailAlternateAddress'])){
96       for ($i= 0; $i<$attrs['gosaMailAlternateAddress']['count']; $i++){
97         echo "EMAIL;TYPE=internet:".
98           $attrs['gosaMailAlternateAddress'][$i]."\n";
99       }
100     }
101   }
103   /* Export date */
104   echo "REV:".date("Y-m-d")."\n";
105   echo "CLASS:PUBLIC\n";
107   /* Fill address */
108   if (isset($attrs['homePostalAddress'])){
109     @list($street,$town,$country)= preg_split('/\n/', $attrs['homePostalAddress'][0]);
110     echo "ADR;TYPE=home:;;".trim($street).";".trim($town).";;;".
111       trim($country)."\n";
112   }
113   if (isset($attrs['postalAddress'])){
114     @list($street,$town,$country)= preg_split('/\n/', $attrs['postalAddress'][0]);
115     echo "ADR;TYPE=work,pref:;;".trim($street).";".trim($town).";;;".
116       trim($country)."\n";
117   }
119   /* Telephone numbers */
120   if (isset($attrs['homePhone'])){
121     echo "TEL;TYPE=home:".$attrs['homePhone'][0]."\n";
122   }
123   if (isset($attrs['telephoneNumber'])){
124     echo "TEL;TYPE=work,pref:".$attrs['telephoneNumber'][0]."\n";
125   }
126   if (isset($attrs['mobile'])){
127     echo "TEL;TYPE=cell:".$attrs['mobile'][0]."\n";
128   }
129   if (isset($attrs['pager'])){
130     echo "TEL;TYPE=pager:".$attrs['pager'][0]."\n";
131   }
133   /* Set organization */
134   if (isset($attrs['o'])){
135     echo "ORG:".$attrs['o'][0]."\n";
136   }
138   echo "NOTE:Exported by GOsa - https://gosa.gonicus.de\n";
139   echo "SORT-STRING:".$attrs['sn'][0]."\n";
140   if (isset($attrs['labeledURI'][0])){
141     echo "URL:".$attrs['labeledURI'][0]."\n";
142   }
144   /* Add user certificate */
145 #if (isset($attrs['userCertificate;binary'])){
146 #       $cert= $ldap->get_attribute($ldap->getDN(), "userCertificate;binary");
147 #       $cert= preg_replace('/\r\n/', chr(10).' ', chunk_split(base64_encode($cert)));
148 #       echo "KEY;ENCODING=b:".preg_replace('/\n $/', '', $cert)."\n";
149 #}
151   /* Add picture */
152   if (isset($attrs['jpegPhoto'])){
153     $photodata= $ldap->get_attribute($ldap->getDN(), "jpegPhoto");
154     $photodata= preg_replace('/\r\n/', chr(10).' ', chunk_split(base64_encode($photodata)));
155     echo "PHOTO;ENCODING=b;TYPE=JPEG:".preg_replace('/\n $/', '', $photodata)."\n";
156   }     
158   /* Day of birth */
159   if (isset($attrs['dayOfBirth'][0])){
160     echo "BDAY:".$attrs['dayOfBirth'][0]."\n";
161   }
163   echo "END:VCARD\n\n";
166 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
167 ?>