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