centralise the "From"-header construction in functions/identities.php.
[squirrelmail.git] / src / vcard.php
1 <?php
2
3 /**
4 * vcard.php
5 *
6 * This file shows an attched vcard
7 *
8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 */
13
14 /**
15 * Include the SquirrelMail initialization file.
16 */
17 require('../include/init.php');
18
19 /* SquirrelMail required files. */
20
21 /** imap functions depend on date functions */
22 include_once(SM_PATH . 'functions/date.php');
23 /** form functions */
24 include_once(SM_PATH . 'functions/forms.php');
25 /** mime decoding */
26 include_once(SM_PATH . 'functions/mime.php');
27 /** url parser */
28 include_once(SM_PATH . 'functions/url_parser.php');
29 /** imap functions used to retrieve vcard */
30 include_once(SM_PATH . 'functions/imap_general.php');
31 include_once(SM_PATH . 'functions/imap_messages.php');
32
33 /* globals */
34
35 sqgetGlobalVar('passed_id', $passed_id, SQ_GET);
36 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
37 sqgetGlobalVar('ent_id', $ent_id, SQ_GET);
38 sqgetGlobalVar('startMessage', $startMessage, SQ_GET);
39 /* end globals */
40
41 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
42 sqimap_mailbox_select($imapConnection, $mailbox);
43
44 displayPageHeader($color);
45
46 $msg_url = 'read_body.php?mailbox='.urlencode($mailbox).
47 '&amp;startMessage='.urlencode($startMessage).
48 '&amp;passed_id='.urlencode($passed_id);
49 $msg_url = set_url_var($msg_url, 'ent_id', 0);
50
51 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
52
53 $entity_vcard = getEntity($message,$ent_id);
54
55 $vcard = mime_fetch_body($imapConnection, $passed_id, $ent_id);
56 $vcard = decodeBody($vcard, $entity_vcard->header->encoding);
57 $vcard = explode ("\n",$vcard);
58 foreach ($vcard as $l) {
59 $k = substr($l, 0, strpos($l, ':'));
60 $v = substr($l, strpos($l, ':') + 1);
61 $attributes = explode(';', $k);
62 $k = strtolower(array_shift($attributes));
63 foreach ($attributes as $attr) {
64 if ($attr == 'quoted-printable')
65 $v = quoted_printable_decode($v);
66 else
67 $k .= ';' . strtolower($attr);
68 }
69
70 $v = str_replace(';', "\n", $v);
71 $vcard_nice[$k] = $v;
72 }
73
74 if ($vcard_nice['version'] == '2.1') {
75 // get firstname and lastname for sm addressbook
76 $vcard_nice['firstname'] = substr($vcard_nice['n'],
77 strpos($vcard_nice['n'], "\n") + 1, strlen($vcard_nice['n']));
78 $vcard_nice['lastname'] = substr($vcard_nice['n'], 0,
79 strpos($vcard_nice['n'], "\n"));
80 // workaround for Outlook, should be fixed in a better way,
81 // maybe in new 'vCard' class.
82 if (isset($vcard_nice['email;pref;internet'])) {
83 $vcard_nice['email;internet'] = $vcard_nice['email;pref;internet'];
84 }
85 } else {
86 $oTemplate->assign('note', sprintf(_("vCard Version %s is not supported. Some information might not be converted correctly."), htmlspecialchars($vcard_nice['version'])));
87 $oTemplate->display('note.tpl');
88
89 $vcard_nice['firstname'] = '';
90 $vcard_nice['lastname'] = '';
91 }
92
93 foreach ($vcard_nice as $k => $v) {
94 $v = htmlspecialchars($v);
95 $v = trim($v);
96 $vcard_safe[$k] = trim(nl2br($v));
97 }
98
99 $ShowValues = array(
100 'fn' => _("Name"),
101 'title' => _("Title"),
102 'email;internet' => _("E-mail"),
103 'url' => _("Web Page"),
104 'org' => _("Organization / Department"),
105 'adr' => _("Address"),
106 'tel;work' => _("Work Phone"),
107 'tel;home' => _("Home Phone"),
108 'tel;cell' => _("Cellular Phone"),
109 'tel;fax' => _("Fax"),
110 'note' => _("Note"));
111
112 if (isset($vcard_safe['email;internet'])) {
113 $vcard_safe['email;internet'] = makeComposeLink('src/compose.php?send_to='.urlencode($vcard_safe['email;internet']),
114 $vcard_safe['email;internet']);
115 }
116
117 if (isset($vcard_safe['url'])) {
118 $vcard_safe['url'] = '<a href="' . $vcard_safe['url'] . '" target="_blank">' .
119 $vcard_safe['url'] . '</a>';
120 }
121
122 $vcard = array();
123 foreach ($ShowValues as $k => $v) {
124 if (isset($vcard_safe[$k]) && $vcard_safe[$k]) {
125 $vcard[$v] = $vcard_safe[$k];
126 }
127 }
128
129 $dl = '../src/download.php?absolute_dl=true&amp;passed_id=' .
130 urlencode($passed_id) . '&amp;mailbox=' . urlencode($mailbox) .
131 '&amp;ent_id=' . urlencode($ent_id);
132
133 if (isset($vcard_nice['email;internet'])) {
134 $email = $vcard_nice['email;internet'];
135 } else {
136 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
137 $header = $message->rfc822_header;
138 $from_name = $header->getAddr_s('from');
139
140 $email = getEmail(decodeHeader($from_name));
141 }
142
143 $opts = array();
144 if (isset($vcard_nice['url'])) {
145 $opts[$vcard_nice['url']] = _("Web Page");
146 }
147 if (isset($vcard_nice['adr'])) {
148 $opts[$vcard_nice['adr']] = _("Address");
149 }
150 if (isset($vcard_nice['title'])) {
151 $opts[$vcard_nice['title']] = _("Title");
152 }
153 if (isset($vcard_nice['org'])) {
154 $opts[$vcard_nice['org']] = _("Organization / Department");
155 }
156 if (isset($vcard_nice['title'])) {
157 $opts[$vcard_nice['title'].'; '.$vcard_nice['org']] = _("Title &amp; Org. / Dept.");
158 }
159 if (isset($vcard_nice['tel;work'])) {
160 $opts[$vcard_nice['tel;work']] = _("Work Phone");
161 }
162 if (isset($vcard_nice['tel;home'])) {
163 $opts[$vcard_nice['tel;home']] = _("Home Phone");
164 }
165 if (isset($vcard_nice['tel;cell'])) {
166 $opts[$vcard_nice['tel;cell']] = _("Cellular Phone");
167 }
168 if (isset($vcard_nice['tel;fax'])) {
169 $opts[$vcard_nice['tel;fax']] = _("Fax");
170 }
171 if (isset($vcard_nice['note'])) {
172 $opts[$vcard_nice['note']] = _("Note");
173 }
174
175 $oTemplate->assign('view_message_link', $msg_url);
176 $oTemplate->assign('download_link', $dl);
177 $oTemplate->assign('vcard', $vcard);
178
179 $oTemplate->assign('nickname', $vcard_nice['firstname'].'-'.$vcard_safe['lastname']);
180 $oTemplate->assign('firstname', $vcard_safe['firstname']);
181 $oTemplate->assign('lastname', $vcard_safe['lastname']);
182 $oTemplate->assign('email', $email);
183 $oTemplate->assign('info', $opts);
184
185 $oTemplate->display('vcard.tpl');
186
187 $oTemplate->display('footer.tpl');
188 ?>