2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 require_once('user.func.inc.php');
24 class VCardIterator
implements PlIterator
26 private $user_list = array();
28 private $freetext = null
;
29 private $photos = true
;
31 public function __construct($photos, $freetext)
33 $this->freetext
= $freetext;
34 $this->photos
= $photos;
37 public function add_user($user)
39 $forlife = get_user_forlife($user, '_silent_user_callback');
41 $this->user_list
[] = get_user_forlife($user);
46 public function first()
48 return count($this->user_list
) == $this->count
- 1;
51 public function last()
53 return count($this->user_list
) == 0;
56 public function total()
61 public function next()
63 if (!$this->user_list
) {
67 $login = array_shift($this->user_list
);
68 $user = get_user_details($login);
70 if (strlen(trim($user['freetext']))) {
71 $user['freetext'] = pl_entity_decode($user['freetext']);
73 if ($this->freetext
) {
74 if (strlen(trim($user['freetext']))) {
75 $user['freetext'] = $this->freetext
. "\n" . $user['freetext'];
77 $user['freetext'] = $this->freetext
;
85 INNER JOIN virtual_redirect USING(vid)
86 INNER JOIN auth_user_quick ON ( user_id = {?} AND emails_alias_pub = 'public' )
87 WHERE ( redirect={?} OR redirect={?} )
88 AND alias LIKE '%@{$globals->mail->alias_dom}'",
90 $user['forlife'].'@'.$globals->mail
->domain
,
91 $user['forlife'].'@'.$globals->mail
->domain2
);
93 $user['virtualalias'] = $res->fetchOneCell();
94 $user['gpxs_vcardjoin'] = join(', ', array_map(array('VCard', 'text_encode'), $user['gpxs_name']));
95 $user['binets_vcardjoin'] = join(', ', array_map(array('VCard', 'text_encode'), $user['binets']));
99 "SELECT attach, attachmime
101 INNER JOIN aliases AS a ON (a.id = p.uid AND a.type = 'a_vie')
102 WHERE a.alias = {?}", $login);
103 if ($res->numRows()) {
104 $user['photo'] = $res->fetchOneAssoc();
113 static private $windows = false
;
114 private $iterator = null
;
116 public function __construct($users, $photos = true
, $freetext = null
)
118 $this->iterator
= new VCardIterator($photos, $freetext);
119 VCard
::$windows = (strpos($_SERVER['HTTP_USER_AGENT'], 'Windows') !== false
);
120 if (is_array($users)) {
121 foreach ($users as $user) {
122 $this->iterator
->add_user($user);
125 $this->iterator
->add_user($users);
129 public static function escape($text)
131 if (VCard
::$windows) {
132 return str_replace(';', '\\\\;', $text);
134 return str_replace(array(';', ','), array('\\\\;', '\\\\,'), $text);
138 public static function format_adr($params, &$smarty)
140 // $adr1, $adr2, $adr3, $postcode, $city, $region, $country
141 extract($params['adr']);
143 $adr = trim("$adr\n$adr2");
144 $adr = trim("$adr\n$adr3");
145 return VCard
::text_encode(';;'
146 . (VCard
::$windows ? VCard
::escape($adr) : $adr) . ';'
147 . (VCard
::$windows ? VCard
::escape($city) : $city) . ';'
148 . (VCard
::$windows ? VCard
::escape($region) : $region) . ';'
149 . (VCard
::$windows ? VCard
::escape($postcode) : $postcode) . ';'
150 . (VCard
::$windows ? VCard
::escape($country) : $country), false
);
153 public static function text_encode($text, $escape = true
)
155 if (is_array($text)) {
156 return implode(',', array_map(array('VCard', 'text_encode'), $text));
159 $text = VCard
::escape($text);
161 if (VCard
::$windows) {
162 $text = utf8_decode($text);
164 return str_replace(array("\r\n", "\n", "\r"), '\n', $text);
167 public function do_page(&$page)
169 $page->changeTpl('core/vcard.tpl', NO_SKIN
);
170 $page->register_modifier('vcard_enc', array($this, 'text_encode'));
171 $page->register_function('format_adr', array($this, 'format_adr'));
172 $page->assign_by_ref('users', $this->iterator
);
175 header("Cache-Control: ");
176 header("Content-type: text/x-vcard; charset=UTF-8");
180 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: