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