| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2006 Polytechnique.org * |
| 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 | |
| 22 | require_once('xorg.misc.inc.php'); |
| 23 | require_once('user.func.inc.php'); |
| 24 | |
| 25 | class VCard |
| 26 | { |
| 27 | var $users = array(); |
| 28 | var $photos; |
| 29 | |
| 30 | function VCard($users, $photos = true, $freetext = null) |
| 31 | { |
| 32 | $this->photos = $photos; |
| 33 | if (is_array($users)) { |
| 34 | foreach ($users as $user) { |
| 35 | $this->add_user($user, $freetext); |
| 36 | } |
| 37 | } else { |
| 38 | $this->add_user($users, $freetext); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | function escape($text) |
| 43 | { |
| 44 | return preg_replace('/[,;:]/', '\\\\$0', $text); |
| 45 | } |
| 46 | |
| 47 | function format_adr($params, &$smarty) |
| 48 | { |
| 49 | // $adr1, $adr2, $adr3, $postcode, $city, $region, $country |
| 50 | extract($params['adr']); |
| 51 | $adr = trim($adr1); |
| 52 | $adr = trim("$adr\n$adr2"); |
| 53 | $adr = trim("$adr\n$adr3"); |
| 54 | return $this->text_encode(';;' |
| 55 | . $this->escape($adr) . ';' |
| 56 | . $this->escape($city) . ';' |
| 57 | . $this->escape($region) . ';' |
| 58 | . $this->escape($postcode) . ';' |
| 59 | . $this->escape($country), false); |
| 60 | } |
| 61 | |
| 62 | function text_encode($text, $escape = true) |
| 63 | { |
| 64 | if (is_array($text)) { |
| 65 | return implode(',', array_map(array($this, 'text_encode'), $text)); |
| 66 | } |
| 67 | if ($escape) { |
| 68 | $text = $this->escape($text); |
| 69 | } |
| 70 | return preg_replace("/(\r\n|\n|\r)/", '\n', $text); |
| 71 | } |
| 72 | |
| 73 | function add_user($x, $freetext) |
| 74 | { |
| 75 | global $globals; |
| 76 | |
| 77 | $login = get_user_forlife($x); |
| 78 | $user = get_user_details($login); |
| 79 | |
| 80 | if (strlen(trim($user['freetext']))) { |
| 81 | $user['freetext'] = html_entity_decode($user['freetext']); |
| 82 | } |
| 83 | if (!is_null($freetext)) { |
| 84 | if (strlen(trim($user['freetext']))) { |
| 85 | $user['freetext'] = $freetext . "\n" . $user['freetext']; |
| 86 | } else { |
| 87 | $user['freetext'] = $freetext; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // alias virtual |
| 92 | $res = XDB::query( |
| 93 | "SELECT alias |
| 94 | FROM virtual |
| 95 | INNER JOIN virtual_redirect USING(vid) |
| 96 | INNER JOIN auth_user_quick ON ( user_id = {?} AND emails_alias_pub = 'public' ) |
| 97 | WHERE ( redirect={?} OR redirect={?} ) |
| 98 | AND alias LIKE '%@{$globals->mail->alias_dom}'", |
| 99 | S::v('uid'), |
| 100 | $user['forlife'].'@'.$globals->mail->domain, |
| 101 | $user['forlife'].'@'.$globals->mail->domain2); |
| 102 | |
| 103 | $user['virtualalias'] = $res->fetchOneCell(); |
| 104 | $user['gpxs_vcardjoin'] = join(',', array_map(array($this, 'text_encode'), $user['gpxs_name'])); |
| 105 | $user['binets_vcardjoin'] = join(',', array_map(array($this, 'text_encode'), $user['binets'])); |
| 106 | // get photo |
| 107 | if ($this->photos) { |
| 108 | $res = XDB::query( |
| 109 | "SELECT attach, attachmime |
| 110 | FROM photo AS p |
| 111 | INNER JOIN aliases AS a ON (a.id = p.uid AND a.type = 'a_vie') |
| 112 | WHERE a.alias = {?}", $login); |
| 113 | if ($res->numRows()) { |
| 114 | $user['photo'] = $res->fetchOneAssoc(); |
| 115 | } |
| 116 | } |
| 117 | $this->users[] = $user; |
| 118 | } |
| 119 | |
| 120 | function do_page(&$page) |
| 121 | { |
| 122 | $page->changeTpl('vcard.tpl', NO_SKIN); |
| 123 | $page->register_modifier('vcard_enc', array($this, 'text_encode')); |
| 124 | $page->register_function('format_adr', array($this, 'format_adr')); |
| 125 | $page->assign_by_ref('users', $this->users); |
| 126 | |
| 127 | header("Pragma: "); |
| 128 | header("Cache-Control: "); |
| 129 | header("Content-type: text/x-vcard; charset=iso-8859-15"); |
| 130 | header("Content-Transfer-Encoding: 8bit"); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: |
| 135 | ?> |