Automation of the management of associations' mailing lists (Closes #817), Updates...
[platal.git] / include / vcard.inc.php
index 749af3d..a4366fc 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
 require_once('xorg.misc.inc.php');
 require_once('user.func.inc.php');
 
-class VCard
+class VCardIterator implements PlIterator
 {
-    var $users = array();
+    private $user_list = array();
+    private $count     = 0;
+    private $freetext  = null;
+    private $photos    = true;
 
-    function VCard($users, $freetext = null)
+    public function __construct($photos, $freetext)
     {
-        if (is_array($users)) {
-            foreach ($users as $user) {
-                $this->add_user($user, $freetext);
-            }
-        } else {
-            $this->add_user($users, $freetext);
-        }
+        $this->freetext = $freetext;
+        $this->photos   = $photos;
     }
 
-    function escape($text)
+    public function add_user($user)
     {
-        return preg_replace('/[,;:]/', '\\\\$0', $text);
+        $this->user_list[] = get_user_forlife($user);
+        $this->count++;
     }
 
-    function format_adr($params, &$smarty)
+    public function first()
     {
-        // $adr1, $adr2, $adr3, $postcode, $city, $region, $country
-        extract($params['adr']);
-        $adr = trim($adr1);
-        $adr = trim("$adr\n$adr2");
-        $adr = trim("$adr\n$adr3");
-        return $this->text_encode(';;'
-                . $this->escape($adr) . ';'
-                . $this->escape($city) . ';'
-                . $this->escape($region) . ';'
-                . $this->escape($postcode) . ';'
-                . $this->escape($country), false);
+        return count($this->user_list) == $this->count - 1;
     }
 
-    function text_encode($text, $escape = true)
+    public function last()
     {
-        if ($escape) {
-            $text = $this->escape($text);
-        }
-        return preg_replace("/(\r\n|\n|\r)/", '\n', $text);
+        return count($this->user_list) == 0;
     }
 
-    function add_user($x, $freetext)
+    public function total()
     {
-        global $globals;
+        return $this->count;
+    }
 
-        $login = get_user_forlife($x);
+    public function next()
+    {
+        if (!$this->user_list) {
+            return null;
+        }
+        global $globals;
+        $login = array_shift($this->user_list);
         $user  = get_user_details($login);
 
         if (strlen(trim($user['freetext']))) {
-            $user['freetext'] = html_entity_decode($user['freetext']);
+            $user['freetext'] = pl_entity_decode($user['freetext']);
         }
-        if (!is_null($freetext)) {
+        if ($this->freetext) {
             if (strlen(trim($user['freetext']))) {
-                $user['freetext'] = $freetext . "\n" . $user['freetext'];
+                $user['freetext'] = $this->freetext . "\n" . $user['freetext'];
             } else {
-                $user['freetext'] = $freetext;
+                $user['freetext'] = $this->freetext;
             }
         }
 
@@ -91,37 +84,97 @@ class VCard
              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']));
         // get photo
-        $res = XDB::query(
-                "SELECT attach, attachmime
-                   FROM photo   AS p
-             INNER JOIN aliases AS a ON (a.id = p.uid AND a.type = 'a_vie')
-                  WHERE a.alias = {?}", $login);
-        if ($res->numRows()) {
-            $user['photo'] = $res->fetchOneAssoc();
+        if ($this->photos) {
+            $res = XDB::query(
+                    "SELECT attach, attachmime
+                       FROM photo   AS p
+                 INNER JOIN aliases AS a ON (a.id = p.uid AND a.type = 'a_vie')
+                      WHERE a.alias = {?}", $login);
+            if ($res->numRows()) {
+                $user['photo'] = $res->fetchOneAssoc();
+            }
+        }
+        return $user;
+    }
+}
+
+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);
+            }
+        } else {
+            $this->iterator->add_user($users);
+        }
+    }
+
+    public static function escape($text)
+    {
+        if (VCard::$windows) {
+            return str_replace(';', '\\\\;', $text);
+        } else {
+            return str_replace(array(';', ','), array('\\\\;', '\\\\,'), $text);
         }
-        $this->users[] = $user;
     }
 
-    function do_page(&$page)
+    public static function format_adr($params, &$smarty)
     {
-        $page->changeTpl('vcard.tpl', NO_SKIN);
+        // $adr1, $adr2, $adr3, $postcode, $city, $region, $country
+        extract($params['adr']);
+        $adr = trim($adr1);
+        $adr = trim("$adr\n$adr2");
+        $adr = trim("$adr\n$adr3");
+        return VCard::text_encode(';;'
+                . (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)
+    {
+        if (is_array($text)) {
+            return implode(',', array_map(array('VCard', 'text_encode'), $text));
+        }
+        if ($escape) {
+            $text = VCard::escape($text);
+        }
+        if (VCard::$windows) {
+            $text = utf8_decode($text);
+        }
+        return str_replace(array("\r\n", "\n", "\r"), '\n', $text);
+    }
+
+    public function do_page(&$page)
+    {
+        $page->changeTpl('core/vcard.tpl', NO_SKIN);
         $page->register_modifier('vcard_enc',  array($this, 'text_encode'));
         $page->register_function('format_adr', array($this, 'format_adr'));
-        $page->assign_by_ref('users', $this->users);
+        $page->assign_by_ref('users', $this->iterator);
 
         header("Pragma: ");
         header("Cache-Control: ");
-        header("Content-type: text/x-vcard; charset=iso-8859-15");
+        header("Content-type: text/x-vcard; charset=UTF-8");
         header("Content-Transfer-Encoding: 8bit");
-    }
+  }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>