Bye xorg.misc.inc.php
[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 {
29b12e6e 39 $this->user_list[] = get_user_forlife($user);
40 $this->count++;
89460d9c 41 }
42
29b12e6e 43 public function first()
89460d9c 44 {
29b12e6e 45 return count($this->user_list) == $this->count - 1;
89460d9c 46 }
47
29b12e6e 48 public function last()
89460d9c 49 {
29b12e6e 50 return count($this->user_list) == 0;
89460d9c 51 }
52
29b12e6e 53 public function total()
89460d9c 54 {
29b12e6e 55 return $this->count;
56 }
89460d9c 57
29b12e6e 58 public function next()
59 {
60 if (!$this->user_list) {
61 return null;
62 }
63 global $globals;
64 $login = array_shift($this->user_list);
89460d9c 65 $user = get_user_details($login);
66
67 if (strlen(trim($user['freetext']))) {
29b12e6e 68 $user['freetext'] = pl_entity_decode($user['freetext']);
89460d9c 69 }
29b12e6e 70 if ($this->freetext) {
89460d9c 71 if (strlen(trim($user['freetext']))) {
29b12e6e 72 $user['freetext'] = $this->freetext . "\n" . $user['freetext'];
89460d9c 73 } else {
29b12e6e 74 $user['freetext'] = $this->freetext;
89460d9c 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}'",
50bf984b 86 $user['user_id'],
89460d9c 87 $user['forlife'].'@'.$globals->mail->domain,
88 $user['forlife'].'@'.$globals->mail->domain2);
eaf30d86 89
89460d9c 90 $user['virtualalias'] = $res->fetchOneCell();
29b12e6e 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']));
89460d9c 93 // get photo
917c4d11 94 if ($this->photos) {
95 $res = XDB::query(
96 "SELECT attach, attachmime
97 FROM photo AS p
98 INNER JOIN aliases AS a ON (a.id = p.uid AND a.type = 'a_vie')
99 WHERE a.alias = {?}", $login);
100 if ($res->numRows()) {
101 $user['photo'] = $res->fetchOneAssoc();
102 }
89460d9c 103 }
29b12e6e 104 return $user;
105 }
106}
107
108class VCard
109{
5175057e 110 static private $windows = false;
29b12e6e 111 private $iterator = null;
112
113 public function __construct($users, $photos = true, $freetext = null)
114 {
115 $this->iterator = new VCardIterator($photos, $freetext);
5175057e 116 VCard::$windows = (strpos($_SERVER['HTTP_USER_AGENT'], 'Windows') !== false);
29b12e6e 117 if (is_array($users)) {
118 foreach ($users as $user) {
119 $this->iterator->add_user($user);
120 }
121 } else {
122 $this->iterator->add_user($users);
123 }
124 }
125
126 public static function escape($text)
127 {
5175057e 128 if (VCard::$windows) {
833a7481 129 return str_replace(';', '\\\\;', $text);
5175057e 130 } else {
833a7481 131 return str_replace(array(';', ','), array('\\\\;', '\\\\,'), $text);
5175057e 132 }
29b12e6e 133 }
134
135 public static function format_adr($params, &$smarty)
136 {
137 // $adr1, $adr2, $adr3, $postcode, $city, $region, $country
138 extract($params['adr']);
139 $adr = trim($adr1);
140 $adr = trim("$adr\n$adr2");
141 $adr = trim("$adr\n$adr3");
142 return VCard::text_encode(';;'
62c93c73
OLF
143 . (VCard::$windows ? VCard::escape($adr) : $adr) . ';'
144 . (VCard::$windows ? VCard::escape($city) : $city) . ';'
145 . (VCard::$windows ? VCard::escape($region) : $region) . ';'
146 . (VCard::$windows ? VCard::escape($postcode) : $postcode) . ';'
147 . (VCard::$windows ? VCard::escape($country) : $country), false);
29b12e6e 148 }
149
150 public static function text_encode($text, $escape = true)
151 {
152 if (is_array($text)) {
153 return implode(',', array_map(array('VCard', 'text_encode'), $text));
154 }
155 if ($escape) {
156 $text = VCard::escape($text);
157 }
5175057e
FB
158 if (VCard::$windows) {
159 $text = utf8_decode($text);
160 }
833a7481 161 return str_replace(array("\r\n", "\n", "\r"), '\n', $text);
89460d9c 162 }
163
29b12e6e 164 public function do_page(&$page)
89460d9c 165 {
8b1f8e12 166 $page->changeTpl('core/vcard.tpl', NO_SKIN);
89460d9c 167 $page->register_modifier('vcard_enc', array($this, 'text_encode'));
168 $page->register_function('format_adr', array($this, 'format_adr'));
29b12e6e 169 $page->assign_by_ref('users', $this->iterator);
89460d9c 170
171 header("Pragma: ");
172 header("Cache-Control: ");
493b6abe 173 header("Content-type: text/x-vcard; charset=UTF-8");
89460d9c 174 header("Content-Transfer-Encoding: 8bit");
29b12e6e 175 }
89460d9c 176}
177
a7de4ef7 178// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
89460d9c 179?>