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