Merge commit 'origin/master' into hruid.
[platal.git] / include / vcard.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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('user.func.inc.php');
23
24 class VCardIterator implements PlIterator
25 {
26 private $user_list = array();
27 private $count = 0;
28 private $freetext = null;
29 private $photos = true;
30
31 public function __construct($photos, $freetext)
32 {
33 $this->freetext = $freetext;
34 $this->photos = $photos;
35 }
36
37 public function add_user($user)
38 {
39 $this->user_list[] = User::get($user);
40 $this->count++;
41 }
42
43 public function first()
44 {
45 return count($this->user_list) == $this->count - 1;
46 }
47
48 public function last()
49 {
50 return count($this->user_list) == 0;
51 }
52
53 public function total()
54 {
55 return $this->count;
56 }
57
58 public function next()
59 {
60 if (!$this->user_list) {
61 return null;
62 }
63 global $globals;
64 $login = array_shift($this->user_list);
65 $user = get_user_details($login->login());
66
67 if (strlen(trim($user['freetext']))) {
68 $user['freetext'] = pl_entity_decode($user['freetext']);
69 }
70 if ($this->freetext) {
71 if (strlen(trim($user['freetext']))) {
72 $user['freetext'] = $this->freetext . "\n" . $user['freetext'];
73 } else {
74 $user['freetext'] = $this->freetext;
75 }
76 }
77
78 // alias virtual
79 $res = XDB::query(
80 "SELECT alias
81 FROM virtual
82 INNER JOIN virtual_redirect USING(vid)
83 INNER JOIN auth_user_quick ON ( user_id = {?} AND emails_alias_pub = 'public' )
84 WHERE ( redirect={?} OR redirect={?} )
85 AND alias LIKE '%@{$globals->mail->alias_dom}'",
86 $user['user_id'],
87 $user['forlife'].'@'.$globals->mail->domain,
88 $user['forlife'].'@'.$globals->mail->domain2);
89
90 $user['virtualalias'] = $res->fetchOneCell();
91 $user['gpxs_vcardjoin'] = join(',', array_map(array('VCard', 'text_encode'), $user['gpxs_name']));
92 $user['binets_vcardjoin'] = join(',', array_map(array('VCard', 'text_encode'), $user['binets']));
93 // get photo
94 if ($this->photos) {
95 $res = XDB::query(
96 "SELECT attach, attachmime
97 FROM photo AS p
98 WHERE u.user_id = {?}", $login->id());
99 if ($res->numRows()) {
100 $user['photo'] = $res->fetchOneAssoc();
101 }
102 }
103 return $user;
104 }
105 }
106
107 class VCard
108 {
109 static private $windows = false;
110 private $iterator = null;
111
112 public function __construct($users, $photos = true, $freetext = null)
113 {
114 $this->iterator = new VCardIterator($photos, $freetext);
115 VCard::$windows = (strpos($_SERVER['HTTP_USER_AGENT'], 'Windows') !== false);
116 if (is_array($users)) {
117 foreach ($users as $user) {
118 $this->iterator->add_user($user);
119 }
120 } else {
121 $this->iterator->add_user($users);
122 }
123 }
124
125 public static function escape($text)
126 {
127 if (VCard::$windows) {
128 return str_replace(';', '\\\\;', $text);
129 } else {
130 return str_replace(array(';', ','), array('\\\\;', '\\\\,'), $text);
131 }
132 }
133
134 public static function format_adr($params, &$smarty)
135 {
136 // $adr1, $adr2, $adr3, $postcode, $city, $region, $country
137 extract($params['adr']);
138 $adr = trim($adr1);
139 $adr = trim("$adr\n$adr2");
140 $adr = trim("$adr\n$adr3");
141 return VCard::text_encode(';;'
142 . (VCard::$windows ? VCard::escape($adr) : $adr) . ';'
143 . (VCard::$windows ? VCard::escape($city) : $city) . ';'
144 . (VCard::$windows ? VCard::escape($region) : $region) . ';'
145 . (VCard::$windows ? VCard::escape($postcode) : $postcode) . ';'
146 . (VCard::$windows ? VCard::escape($country) : $country), false);
147 }
148
149 public static function text_encode($text, $escape = true)
150 {
151 if (is_array($text)) {
152 return implode(',', array_map(array('VCard', 'text_encode'), $text));
153 }
154 if ($escape) {
155 $text = VCard::escape($text);
156 }
157 if (VCard::$windows) {
158 $text = utf8_decode($text);
159 }
160 return str_replace(array("\r\n", "\n", "\r"), '\n', $text);
161 }
162
163 public function do_page(&$page)
164 {
165 $page->changeTpl('core/vcard.tpl', NO_SKIN);
166 $page->register_modifier('vcard_enc', array($this, 'text_encode'));
167 $page->register_function('format_adr', array($this, 'format_adr'));
168 $page->assign_by_ref('users', $this->iterator);
169
170 header("Pragma: ");
171 header("Cache-Control: ");
172 header("Content-type: text/x-vcard; charset=UTF-8");
173 header("Content-Transfer-Encoding: 8bit");
174 }
175 }
176
177 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
178 ?>