cosmetics.
[platal.git] / include / vcard.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 require_once('xorg.misc.inc.php');
23 require_once('user.func.inc.php');
24
25 class VCard
26 {
27 var $users = array();
28
29 function VCard($users, $freetext = null)
30 {
31 if (is_array($users)) {
32 foreach ($users as $user) {
33 $this->add_user($user, $freetext);
34 }
35 } else {
36 $this->add_user($users, $freetext);
37 }
38 }
39
40 function escape($text)
41 {
42 return preg_replace('/[,;:]/', '\\\\$0', $text);
43 }
44
45 function format_adr($params, &$smarty)
46 {
47 // $adr1, $adr2, $adr3, $postcode, $city, $region, $country
48 extract($params['adr']);
49 $adr = trim($adr1);
50 $adr = trim("$adr\n$adr2");
51 $adr = trim("$adr\n$adr3");
52 return $this->text_encode(';;'
53 . $this->escape($adr) . ';'
54 . $this->escape($city) . ';'
55 . $this->escape($region) . ';'
56 . $this->escape($postcode) . ';'
57 . $this->escape($country), false);
58 }
59
60 function text_encode($text, $escape = true)
61 {
62 if ($escape) {
63 $text = $this->escape($text);
64 }
65 return preg_replace("/(\r\n|\n|\r)/", '\n', $text);
66 }
67
68 function add_user($x, $freetext)
69 {
70 global $globals;
71
72 $login = get_user_forlife($x);
73 $user = get_user_details($login);
74
75 if (strlen(trim($user['freetext']))) {
76 $user['freetext'] = html_entity_decode($user['freetext']);
77 }
78 if (!is_null($freetext)) {
79 if (strlen(trim($user['freetext']))) {
80 $user['freetext'] = $freetext . "\n" . $user['freetext'];
81 } else {
82 $user['freetext'] = $freetext;
83 }
84 }
85
86 // alias virtual
87 $res = XDB::query(
88 "SELECT alias
89 FROM virtual
90 INNER JOIN virtual_redirect USING(vid)
91 INNER JOIN auth_user_quick ON ( user_id = {?} AND emails_alias_pub = 'public' )
92 WHERE ( redirect={?} OR redirect={?} )
93 AND alias LIKE '%@{$globals->mail->alias_dom}'",
94 S::v('uid'),
95 $user['forlife'].'@'.$globals->mail->domain,
96 $user['forlife'].'@'.$globals->mail->domain2);
97
98 $user['virtualalias'] = $res->fetchOneCell();
99
100 // get photo
101 $res = XDB::query(
102 "SELECT attach, attachmime
103 FROM photo AS p
104 INNER JOIN aliases AS a ON (a.id = p.uid AND a.type = 'a_vie')
105 WHERE a.alias = {?}", $login);
106 if ($res->numRows()) {
107 $user['photo'] = $res->fetchOneAssoc();
108 }
109 $this->users[] = $user;
110 }
111
112 function do_page(&$page)
113 {
114 $page->changeTpl('vcard.tpl', NO_SKIN);
115 $page->register_modifier('vcard_enc', array($this, 'text_encode'));
116 $page->register_function('format_adr', array($this, 'format_adr'));
117 $page->assign_by_ref('users', $this->users);
118
119 header("Pragma: ");
120 header("Cache-Control: ");
121 header("Content-type: text/x-vcard; charset=iso-8859-15");
122 header("Content-Transfer-Encoding: 8bit");
123 }
124 }
125
126 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
127 ?>