Merge branch 'master' of /home/git/platal into profile_edit
[platal.git] / include / vcard.inc.php
CommitLineData
89460d9c 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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 {
29b12e6e 40 $this->user_list[] = get_user_forlife($user);
41 $this->count++;
89460d9c 42 }
43
29b12e6e 44 public function first()
89460d9c 45 {
29b12e6e 46 return count($this->user_list) == $this->count - 1;
89460d9c 47 }
48
29b12e6e 49 public function last()
89460d9c 50 {
29b12e6e 51 return count($this->user_list) == 0;
89460d9c 52 }
53
29b12e6e 54 public function total()
89460d9c 55 {
29b12e6e 56 return $this->count;
57 }
89460d9c 58
29b12e6e 59 public function next()
60 {
61 if (!$this->user_list) {
62 return null;
63 }
64 global $globals;
65 $login = array_shift($this->user_list);
89460d9c 66 $user = get_user_details($login);
67
68 if (strlen(trim($user['freetext']))) {
29b12e6e 69 $user['freetext'] = pl_entity_decode($user['freetext']);
89460d9c 70 }
29b12e6e 71 if ($this->freetext) {
89460d9c 72 if (strlen(trim($user['freetext']))) {
29b12e6e 73 $user['freetext'] = $this->freetext . "\n" . $user['freetext'];
89460d9c 74 } else {
29b12e6e 75 $user['freetext'] = $this->freetext;
89460d9c 76 }
77 }
78
79 // alias virtual
80 $res = XDB::query(
81 "SELECT alias
82 FROM virtual
83 INNER JOIN virtual_redirect USING(vid)
84 INNER JOIN auth_user_quick ON ( user_id = {?} AND emails_alias_pub = 'public' )
85 WHERE ( redirect={?} OR redirect={?} )
86 AND alias LIKE '%@{$globals->mail->alias_dom}'",
87 S::v('uid'),
88 $user['forlife'].'@'.$globals->mail->domain,
89 $user['forlife'].'@'.$globals->mail->domain2);
787bb3d7 90
89460d9c 91 $user['virtualalias'] = $res->fetchOneCell();
29b12e6e 92 $user['gpxs_vcardjoin'] = join(',', array_map(array('VCard', 'text_encode'), $user['gpxs_name']));
93 $user['binets_vcardjoin'] = join(',', array_map(array('VCard', 'text_encode'), $user['binets']));
89460d9c 94 // get photo
917c4d11 95 if ($this->photos) {
96 $res = XDB::query(
97 "SELECT attach, attachmime
98 FROM photo AS p
99 INNER JOIN aliases AS a ON (a.id = p.uid AND a.type = 'a_vie')
100 WHERE a.alias = {?}", $login);
101 if ($res->numRows()) {
102 $user['photo'] = $res->fetchOneAssoc();
103 }
89460d9c 104 }
29b12e6e 105 return $user;
106 }
107}
108
109class VCard
110{
111 private $iterator = null;
112
113 public function __construct($users, $photos = true, $freetext = null)
114 {
115 $this->iterator = new VCardIterator($photos, $freetext);
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 return preg_replace('/[,;]/', '\\\\$0', $text);
128 }
129
130 public static function format_adr($params, &$smarty)
131 {
132 // $adr1, $adr2, $adr3, $postcode, $city, $region, $country
133 extract($params['adr']);
134 $adr = trim($adr1);
135 $adr = trim("$adr\n$adr2");
136 $adr = trim("$adr\n$adr3");
137 return VCard::text_encode(';;'
138 . VCard::escape($adr) . ';'
139 . VCard::escape($city) . ';'
140 . VCard::escape($region) . ';'
141 . VCard::escape($postcode) . ';'
142 . VCard::escape($country), false);
143 }
144
145 public static function text_encode($text, $escape = true)
146 {
147 if (is_array($text)) {
148 return implode(',', array_map(array('VCard', 'text_encode'), $text));
149 }
150 if ($escape) {
151 $text = VCard::escape($text);
152 }
153 return preg_replace("/(\r\n|\n|\r)/", '\n', $text);
89460d9c 154 }
155
29b12e6e 156 public function do_page(&$page)
89460d9c 157 {
8b1f8e12 158 $page->changeTpl('core/vcard.tpl', NO_SKIN);
89460d9c 159 $page->register_modifier('vcard_enc', array($this, 'text_encode'));
160 $page->register_function('format_adr', array($this, 'format_adr'));
29b12e6e 161 $page->assign_by_ref('users', $this->iterator);
89460d9c 162
163 header("Pragma: ");
164 header("Cache-Control: ");
493b6abe 165 header("Content-type: text/x-vcard; charset=UTF-8");
89460d9c 166 header("Content-Transfer-Encoding: 8bit");
29b12e6e 167 }
89460d9c 168}
169
a7de4ef7 170// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
89460d9c 171?>