Merge branch 'platal-0.9.17'
[platal.git] / include / vcard.inc.php
index bec45bb..20aa9ce 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-require_once('xorg.misc.inc.php');
 require_once('user.func.inc.php');
 
-class VCardIterator
+class VCardIterator implements PlIterator
 {
     private $user_list = array();
     private $count     = 0;
@@ -37,8 +36,11 @@ class VCardIterator
 
     public function add_user($user)
     {
-        $this->user_list[] = get_user_forlife($user);
-        $this->count++;
+        $forlife = get_user_forlife($user, '_silent_user_callback');
+        if ($forlife) {
+            $this->user_list[] = get_user_forlife($user);
+            $this->count++;
+        }
     }
 
     public function first()
@@ -84,10 +86,10 @@ class VCardIterator
              INNER JOIN auth_user_quick  ON ( user_id = {?} AND emails_alias_pub = 'public' )
                   WHERE ( redirect={?} OR redirect={?} )
                         AND alias LIKE '%@{$globals->mail->alias_dom}'",
-                S::v('uid'),
+                $user['user_id'],
                 $user['forlife'].'@'.$globals->mail->domain,
                 $user['forlife'].'@'.$globals->mail->domain2);
-        
+
         $user['virtualalias'] = $res->fetchOneCell();
         $user['gpxs_vcardjoin'] = join(',', array_map(array('VCard', 'text_encode'), $user['gpxs_name']));
         $user['binets_vcardjoin'] = join(',', array_map(array('VCard', 'text_encode'), $user['binets']));
@@ -108,11 +110,13 @@ class VCardIterator
 
 class VCard
 {
+    static private $windows = false;
     private $iterator = null;
 
     public function __construct($users, $photos = true, $freetext = null)
     {
         $this->iterator = new VCardIterator($photos, $freetext);
+        VCard::$windows  = (strpos($_SERVER['HTTP_USER_AGENT'], 'Windows') !== false);
         if (is_array($users)) {
             foreach ($users as $user) {
                 $this->iterator->add_user($user);
@@ -124,7 +128,11 @@ class VCard
 
     public static function escape($text)
     {
-        return preg_replace('/[,;]/', '\\\\$0', $text);
+        if (VCard::$windows) {
+            return str_replace(';', '\\\\;', $text);
+        } else {
+            return str_replace(array(';', ','), array('\\\\;', '\\\\,'), $text);
+        }
     }
 
     public static function format_adr($params, &$smarty)
@@ -135,11 +143,11 @@ class VCard
         $adr = trim("$adr\n$adr2");
         $adr = trim("$adr\n$adr3");
         return VCard::text_encode(';;'
-                . VCard::escape($adr) . ';'
-                . VCard::escape($city) . ';'
-                . VCard::escape($region) . ';'
-                . VCard::escape($postcode) . ';'
-                . VCard::escape($country), false);
+                . (VCard::$windows ? VCard::escape($adr) : $adr) . ';'
+                . (VCard::$windows ? VCard::escape($city) : $city) . ';'
+                . (VCard::$windows ? VCard::escape($region) : $region) . ';'
+                . (VCard::$windows ? VCard::escape($postcode) : $postcode) . ';'
+                . (VCard::$windows ? VCard::escape($country) : $country), false);
     }
 
     public static function text_encode($text, $escape = true)
@@ -150,7 +158,10 @@ class VCard
         if ($escape) {
             $text = VCard::escape($text);
         }
-        return preg_replace("/(\r\n|\n|\r)/", '\n', $text);
+        if (VCard::$windows) {
+            $text = utf8_decode($text);
+        }
+        return str_replace(array("\r\n", "\n", "\r"), '\n', $text);
     }
 
     public function do_page(&$page)
@@ -163,7 +174,6 @@ class VCard
         header("Pragma: ");
         header("Cache-Control: ");
         header("Content-type: text/x-vcard; charset=UTF-8");
-        header("Content-Transfer-Encoding: 8bit");
   }
 }