cvs: pear /Contact_AddressBook/Contact AddressBook.php /Contact_AddressBook/Contact/AddressBook Builder.php CSV.php Converter.php Parser.php
/Contact_AddressBook/Contact/AddressBook/Builder CSV.php Eudora.php /Contact_AddressBook/Contact/AddressBook/Parser CSV.php Eudora.php
| From: | Firman Wandayandi | Date: | Tue, 16 Aug 2005 20:43:02 +0000 |
| Subject: | cvs: pear /Contact_AddressBook/Contact AddressBook.php /Contact_AddressBook/Contact/AddressBook Builder.php CSV.php Converter.php Parser.php /Contact_AddressBook/Contact/AddressBook/Builder CSV.php Eudora.php /Contact_AddressBook/Contact/AddressBook/Parser CSV.php Eudora.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-34031@lists.php.net to get a copy of this message | ||
firman Tue Aug 16 16:43:02 2005 EDT
Added files:
/pear/Contact_AddressBook/Contact/AddressBook/Builder CSV.php
Eudora.php
/pear/Contact_AddressBook/Contact/AddressBook/Parser CSV.php
Eudora.php
Modified files:
/pear/Contact_AddressBook/Contact AddressBook.php
/pear/Contact_AddressBook/Contact/AddressBook Builder.php CSV.php
Converter.php
Parser.php
Log:
New API for alpha release state
http://cvs.php.net/diff.php/pear/Contact_AddressBook/Contact/AddressBook.php?r1=1.1&r2=1.2&ty=u Index: pear/Contact_AddressBook/Contact/AddressBook.php diff -u pear/Contact_AddressBook/Contact/AddressBook.php:1.1 pear/Contact_AddressBook/Contact/AddressBook.php:1.2 --- pear/Contact_AddressBook/Contact/AddressBook.php:1.1 Sat Mar 26 13:24:34 2005 +++ pear/Contact_AddressBook/Contact/AddressBook.php Tue Aug 16 16:43:01 2005 @@ -1,70 +1,135 @@ <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ -// +----------------------------------------------------------------------+ -// | PHP version 4 | -// +----------------------------------------------------------------------+ -// | PEAR, the PHP Extension and Application Repository | -// +----------------------------------------------------------------------+ -// | Copyright (C) 2004 Firman Wandayandi | -// | All rights reserved. | -// +----------------------------------------------------------------------+ -// | This LICENSE is in the BSD license style. | -// | http://www.opensource.org/licenses/bsd-license.php | -// | | -// | Redistribution and use in source and binary forms, with or without | -// | modification, are permitted provided that the following conditions | -// | are met: | -// | | -// | Redistributions of source code must retain the above copyright | -// | notice, this list of conditions and the following disclaimer. | -// | | -// | Redistributions in binary form must reproduce the above | -// | copyright notice, this list of conditions and the following | -// | disclaimer in the documentation and/or other materials provided | -// | with the distribution. | -// | | -// | Neither the name of Firman Wandayandi nor the names of | -// | contributors may be used to endorse or promote products derived | -// | from this software without specific prior written permission. | -// | | -// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | -// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | -// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | -// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | -// | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | -// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | -// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | -// | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | -// | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | -// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | -// | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | -// | POSSIBILITY OF SUCH DAMAGE. | -// +----------------------------------------------------------------------+ -// | Authors: Firman Wandayandi <firman@php.net> | -// +----------------------------------------------------------------------+ -// -// $Id: AddressBook.php,v 1.1 2005/03/26 18:24:34 firman Exp $ + +// {{{ Header /** - * File contains Contact_AddressBook class. + * File contains main class of Contact_AddressBook. * - * @author Firman Wandayandi <firman@php.net> + * PHP versions 4 and 5 + * + * LICENSE: + * + * BSD License + * + * Copyright (c) 2004-2005 Firman Wandayandi + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. Neither the name of Firman Wandayandi nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * @category File Formats * @package Contact_AddressBook - * @category FileFormats + * @author Firman Wandayandi <firman@php.net> + * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License + * @version CVS: $Id: AddressBook.php,v 1.2 2005/08/16 20:43:01 firman Exp $ */ +// }}} +// {{{ Dependencies + /** - * Use PEAR for handling error. + * Load PEAR as main framework. */ require_once 'PEAR.php'; +// }}} +// {{{ Global Variables + +/** + * Classes file path informations. + * + * @global array $GLOBALS['_Contact_AddressBook_paths'] + * @name $_Contact_AddressBook_paths + */ +$GLOBALS['_Contact_AddressBook_paths'] = array( + 'csv' => array( + 'parser' => array( + 'Contact/AddressBook/Parser/CSV.php', + 'Contact_AddressBook_Parser_CSV' + ), + 'converter' => array( + 'Contact/AddressBook/Converter/CSV.php', + 'Contact_AddressBook_Converter_CSV' + ), + 'builder' => array( + 'Contact/AddressBook/Builder/CSV.php', + 'Contact_AddressBook_Builder_CSV' + ) + ), + 'eudora' => array( + 'parser' => array( + 'Contact/AddressBook/Parser/Eudora.php', + 'Contact_AddressBook_Parser_Eudora' + ), + 'converter' => array( + 'Contact/AddressBook/Converter.php', + 'Contact_AddressBook_Converter' + ), + 'builder' => array( + 'Contact/AddressBook/Builder/Eudora.php', + 'Contact_AddressBook_Builder_Eudora' + ) + ) +); + +// }}} +// {{{ Initial Codes + +// PHP_EOL Available since PHP 4.3.10 and PHP 5.0.2 +if (!defined('PHP_EOL')) { + // Fix this for MAC eol enable. + if (PEAR_OS == 'Windows') { + define('PHP_EOL', "\r\n"); + } else { + define('PHP_EOL', "\n"); + } +} + +// }}} +// {{{ Class: Contact_AddressBook + /** - * Main Address_Book class. + * Main Contact_AddressBook class. * + * @category File Formats + * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> - * @package Contact_Address_Book + * @copyright Copyright (c) 2004-2005 Firman Wandayandi + * @license http://www.opensource.org/licenses/bsd-license.php + * BSD License + * @version Release: @package_version@ + * + * @todo Add Vcard, LDIF and WAB (Binary) support. + * @todo Mac format complain. + * @todo PHPUnit tests. + * @todo Add storage backend (is it necessary needed?) */ class Contact_AddressBook { @@ -76,44 +141,57 @@ * @var array * @access protected */ - var $options = array( - 'def_dir' => '', // Definition directory. - 'languange' => 'en', // Language. - 'line_break' => "\n" // Line break character for writing a file. + var $_options = array( + 'def_dir' => '', // Definitions directory. + 'language' => 'en' // Language. ); - // }}} - // {{{ Constructor - /** - * PHP4 compatible constructor. + * Supported format names. + * Formats (case-insensitive): + * <pre> + * csv_wab Ms Windows Address Book CSV. + * csv_outlookexpress Ms Windows Outlook Express CSV (equal with WAB). + * csv_outlook Ms Outlook CSV. + * csv_mozilla Mozilla Mailer CSV. + * csv_thunderbird Mozilla Thunderbird CSV (equal with Mozilla). + * csv_netscape Netscape Mailer CSV (equal with Mozilla). + * csv_yahoo Yahoo! CSV. + * csv_palm Palm CSV. + * eudora Eudora address book. + * </pre> * - * @param array $options Options. - * - * @access public + * @var array + * @access private */ - function Contact_AddressBook($options = null) - { - $this->__construct($options); - } + var $_supportedFormats = array( + 'csv_wab', + 'csv_outlookexpress', + 'csv_outlook', + 'csv_mozilla', + 'csv_thunderbird', + 'csv_netscape', + 'csv_yahoo', + 'csv_palm', + 'csv_kmail', + 'eudora' + ); + + // }}} + // {{{ Constructor /** - * PHP5 compatible constructor. + * Constructor. * - * @param array $options Options. + * @param array $options (optional) The options. * * @access public */ - function __construct($options = null) + function Contact_AddressBook($options = null) { - if (!isset($options['def_dir'])){ - require_once 'PEAR/Config.php'; - $config = new PEAR_Config; - $options['def_dir'] = $config->get('data_dir') . - '/Contact_AddressBook/definitions/'; + if (is_array($options)) { + $this->set($options); } - - $this->set($options); } // }}} @@ -144,9 +222,9 @@ } } } elseif (is_string($option)) { - if (isset($this->options[$option])) { - if (gettype($this->options[$option]) == gettype($value)) { - $this->options[$option] = $value; + if (isset($this->_options[$option])) { + if (gettype($this->_options[$option]) == gettype($value)) { + $this->_options[$option] = $value; } else { return PEAR::raiseError("Type mismatch for option '$option' value"); } @@ -155,79 +233,96 @@ } // }}} - // {{{ createObject() + // {{{ get() /** - * Create AddressBook component object. + * Get single or all options. * - * @param string $file Filename. - * @param string $class Classname. + * @param string $option (optional) Option name. * - * @return object Component object on success or PEAR_Error on failure. + * @return mixed Value of option or an array of all options. * @access public - * @static */ - function &createObject($file, $class) + function get($option = null) { - include_once $file; - - if (!class_exists($class)) { - return PEAR::raiseError("Undefined class '$class'"); + if ($option !== null) { + if (isset($this->_options[$option])) { + return $this->_options[$option]; + } else { + return PEAR::raiseError('Unknown option "' . $option . '"'); + } } + return $this->_options; + } - $obj =& new $class; - return $obj; + // }}} + // {{{ getDataDir() + + /** + * Get PEAR data directory. + * + * @return string PEAR data directory. + * @access public + * @static + */ + function getDataDir() + { + require_once 'PEAR/Config.php'; + $config = new PEAR_Config; + return $config->get('data_dir') . '/Contact_AddressBook/data'; } // }}} - // {{{ createParser() + // {{{ isSupported() /** - * Create parser object for specified driver. + * Find the whether address book format is supported or not. * - * @param string $driver Driver name. + * @param string $format Address book format. * - * @return object Parser object on success or PEAR_Error on failure. + * @return bool TRUE if supported, otherwise FALSE. * @access public */ - function &createParser($driver) + function isSupported($format) { - if (substr($driver, 0, 3) == 'csv') { - $class = 'Contact_AddressBook_Parser_csv'; - $file = 'Contact/AddressBook/Parser/csv.php'; - } else { - $class = 'Contact_AddressBook_Parser_' . $driver; - $file = "Contact/AddressBook/Parser/{$driver}.php"; - } - - $obj =& Contact_AddressBook::createObject($file, $class); - return $obj; + return in_array($format, $this->_supportedFormats); } // }}} - // {{{ createBuilder() + // {{{ createParser() /** - * Create builder object for specified driver. + * Create parser object for the specified address book format. * - * @param string $driver Driver name. + * @param string $format The address book format name. * - * @return object Builder object on success or PEAR_Error on failure. + * @return object Parser object on success or PEAR_Error on failure. * @access public */ - function &createBuilder($driver, $options = null) + function createParser($format) { - $class = 'Contact_AddressBook_Builder_' . $driver; - if (substr($driver, 0, 3) == 'csv') { - $driver = str_replace('csv_', '', $driver); - $file = "Contact/AddressBook/Builder/csv/{$driver}.php"; + $format = strtolower($format); + if (!$this->isSupported($format)) { + return PEAR::raiseError('Not supported format \'' . $format .'\''); + } + + if (substr($format, 0, 3) == 'csv') { + $format = substr($format, 4); + $file = $GLOBALS['_Contact_AddressBook_paths']['csv']['parser'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths']['csv']['parser'][1]; + + include_once $file; + $obj = new $class($format); + $res = $obj->setFormat($format); + if (PEAR::isError($res)) { + return $res; + } } else { - $file = "Contact/AddressBook/Builder/{$driver}.php"; - } + $file = $GLOBALS['_Contact_AddressBook_paths'][$format]['parser'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths'][$format]['parser'][1]; - $obj =& Contact_AddressBook::createObject($file, $class); - if (is_a($obj, 'Contact_AddressBook_Builder')) { - $obj->set($this->options); + include_once $file; + $obj = new $class; } return $obj; @@ -237,88 +332,146 @@ // {{{ createConverter() /** - * Create converter object for specified driver. + * Create converter object for the specified address book format. * - * @param string $driver Driver name. + * @param string $format The address book format name. * * @return object Converter object on success or PEAR_Error on failure. * @access public */ - function &createConverter($driver) + function createConverter($format) { - $class = 'Contact_AddressBook_Converter'; - $file = 'Contact/AddressBook/Converter.php'; + $format = strtolower($format); + if (!$this->isSupported($format)) { + return PEAR::raiseError('Not supported format \'' . $format .'\''); + } + + if (substr($format, 0, 3) == 'csv') { + $format = substr($format, 4); + $file = $GLOBALS['_Contact_AddressBook_paths']['csv']['converter'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths']['csv']['converter'][1]; + + include_once $file; + $obj = new $class; + + if (!empty($this->_options['def_dir'])) { + $obj->setDefinitionDir($this->_options['def_dir']); + } + + $res = $obj->setFormat($format); + if (PEAR::isError($res)) { + return $res; + } + + $obj->setLanguage($this->_options['language']); + } else { + $file = $GLOBALS['_Contact_AddressBook_paths'][$format]['converter'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths'][$format]['converter'][1]; + + include_once $file; + $obj = new $class; - $obj =& Contact_AddressBook::createObject($file, $class); - if (is_a($obj, 'Contact_AddressBook_Converter')) { - $obj->setDefinitionDir($this->options['def_dir']); - $obj->loadDefinitionFile($driver . '.def'); + $res = $obj->setFormat($format); + if (PEAR::isError($res)) { + return $res; + } } return $obj; } // }}} - // {{{ isExportable() + // {{{ createBuilder() /** - * Find out the whether address book format is exportable. + * Create builder object for the specified address book format. * - * @param string $format Address book format name. + * @param string $format The address book format name. * - * @return bool TRUE if exportable, otherwise FALSE. + * @return object Builder object on success or PEAR_Error on failure. * @access public */ - function isExportable($format) + function createBuilder($format) { - if (substr($driver, 0, 3) == 'csv') { - $format = str_replace('csv_', '', $format); - $file = "Contact/AddressBook/Builder/csv/{$format}.php"; + $format = strtolower($format); + if (!$this->isSupported($format)) { + return PEAR::raiseError('Not supported format \'' . $format .'\''); + } + + if (substr($format, 0, 3) == 'csv') { + $format = substr($format, 4); + $file = $GLOBALS['_Contact_AddressBook_paths']['csv']['builder'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths']['csv']['builder'][1]; + + include_once $file; + $obj = new $class; + + $res = $obj->setFormat($format); + if (PEAR::isError($res)) { + return $res; + } + + $obj->setLanguage($this->_options['language']); } else { - $file = "Contact/AddressBook/Builder/{$format}.php"; + $file = $GLOBALS['_Contact_AddressBook_paths'][$format]['builder'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths'][$format]['builder'][1]; + + include_once $file; + $obj = new $class; } - return file_exists($file); + return $obj; } // }}} - // {{{ isImportable() + // {{{ import() /** - * Find out the whether address book format is importable. + * Import data from specified format structure to internal structure. * - * @param string $format Address book format name. + * @param array $data Data. + * @param string $format Structure format. * - * @return bool TRUE if importable, otherwise FALSE. + * @return array|PEAR_Error An array result on success or PEAR_Error on + * failure. * @access public */ - function isImportable($format) + function import($data, $format) { - $file = $this->options['def_dir'] . $format . '.def'; - return file_exists($file); + $conv = $this->createConverter($format); + if (PEAR::isError($conv)) { + return $conv; + } + + return $conv->convertFrom($data); } // }}} - // {{{ import() + // {{{ importFromFile() /** - * Import data from specified format structure to internal structure. + * Import data from specified format structure saved in the file to + * internal structure. * - * @param array $data Data. - * @param string $format Structure format. + * @param string $filename Filename. + * @param string $format File format. * * @return array|PEAR_Error An array result on success or PEAR_Error on * failure. * @access public */ - function import($data, $format) + function importFromFile($filename, $format) { - $conv =& $this->createConverter($format); - if (PEAR::isError($conv)) { - return $conv; + $parser =& $this->createParser($format); + if (PEAR::isError($parser)) { + return $parser; } - return $conv->convertFrom($data); + $parser->setFile($filename); + $parser->parse(); + $res = $parser->getResult(); + + return $this->import($res, $format); } // }}} @@ -346,51 +499,57 @@ } // }}} - // {{{ importFromFile() + // {{{ exportToFile() /** - * Import data from specified format structure saved in the file to - * internal structure. + * Export data from internal format to external data structure and save + * it to file. * * @param string $filename Filename. * @param string $format File format. + * @param array $data Address book contacts data. + * @param bool $via_browser (optional) Tell the whether to output via + * browser (downloading) or not. * * @return array|PEAR_Error An array result on success or PEAR_Error on * failure. * @access public */ - function importFromFile($filename, $format) + function exportToFile($filename, $format, $data, $via_browser = true) { - $parser =& $this->createParser($format); - if (PEAR::isError($parser)) { - return $parser; + $data = $this->export($data, $format); + if (PEAR::isError($data)) { + return $data; } - $parser->setInput($filename); - $parser->parse(); - $res = $parser->getResult(); + $builder =& $this->createBuilder($format); + if (PEAR::isError($builder)) { + return $builder; + } - return $this->import($res, $format); + $builder->setData($data); + $builder->build(); + + if ($via_browser) { + return $builder->download($filename); + } else { + return $builder->save($filename); + } } // }}} - // {{{ exportToFile() + // {{{ exportToPrint() /** - * Export data from internal format to external data structure and save - * it to file. + * Export data from internal format to external data structure and print it. * - * @param string $filename Filename. * @param string $format File format. * @param array $data Address book contacts data. - * @param bool $via_browser Tell the whether to output via browser - * (downloading) or not. * - * @return array|PEAR_Error An array result on success or PEAR_Error on - * failure. + * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. * @access public */ - function exportToFile($filename, $format, $data, $via_browser = true) + function exportToPrint($format, $data) { $data = $this->export($data, $format); if (PEAR::isError($data)) { @@ -405,16 +564,15 @@ $builder->setData($data); $builder->build(); - if ($via_browser) { - return $builder->download($filename); - } else { - return $builder->save($filename); - } + $builder->show(); + return true; } // }}} } +// }}} + /* * Local variables: * mode: php http://cvs.php.net/diff.php/pear/Contact_AddressBook/Contact/AddressBook/Builder.php?r1=1.2&r2=1.3&ty=u Index: pear/Contact_AddressBook/Contact/AddressBook/Builder.php diff -u pear/Contact_AddressBook/Contact/AddressBook/Builder.php:1.2 pear/Contact_AddressBook/Contact/AddressBook/Builder.php:1.3 --- pear/Contact_AddressBook/Contact/AddressBook/Builder.php:1.2 Sun Mar 27 14:25:38 2005 +++ pear/Contact_AddressBook/Contact/AddressBook/Builder.php Tue Aug 16 16:43:01 2005 @@ -4,7 +4,7 @@ // {{{ Header /** - * File contains Contact_AddressBook_Converter class. + * File contains Contact_AddressBook_Builder class. * * PHP versions 4 and 5 * @@ -42,64 +42,55 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License - * @version CVS: $Id: Builder.php,v 1.2 2005/03/27 19:25:38 firman Exp $ + * @version CVS: $Id: Builder.php,v 1.3 2005/08/16 20:43:01 firman Exp $ */ // }}} // {{{ Dependencies /** - * Require File for handling files. + * Load File for handling files. */ require_once 'File.php'; /** - * Require Net_UserAgent_Detect for send the right header. + * Load Net_UserAgent_Detect for send the right header. */ require_once "Net/UserAgent/Detect.php"; // }}} -// {{{ Class: Contact_AddressBook_Converter +// {{{ Class: Contact_AddressBook_Builder /** - * Base class for build external formatted structure that may save to a file. + * Abstract class for building address book. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ + * @abstract */ class Contact_AddressBook_Builder { // {{{ Properties /** - * Options. - * - * @var array - * @access protected - */ - var $options = array( - 'line_break' => "\n" - ); - - /** * Cache for data. * * @var array * @access protected */ var $data = array(); - + /** * Cache for result. * @@ -108,91 +99,29 @@ */ var $result = ''; - // }}} - // {{{ Constructor - /** - * PHP 4 compatible constructor. + * MIME Type when send the output to the browser. * - * @param array $data (optional) Data. - * @param array $options (optional) Options. - * - * @access public - */ - function Contact_AddressBook_Builder($data = null, $options = null) - { - $this->__construct($data, $options); - } - - /** - * PHP 5 compatible constructor. - * - * @param array $data (optional) Data. - * @param array $options (optional) Options. - * - * @access public + * @var string + * @access protected */ - function __construct($data = null, $options = null) - { - if ($data !== null && is_array($data)) { - $this->setData($data); - } - - if ($options !== null) { - $this->set($options); - } - } - - // }}} - // {{{ __toString() + var $mime = 'text/plain'; /** - * PHP5 __toString magic method. + * File extension. * - * @return string - * @access public + * @var string + * @access protected */ - function __toString() - { - return var_export($this, true); - } + var $extension = 'txt'; // }}} - // {{{ set() + // {{{ Constructor - /** - * Set the option(s). - * - * Set a single option or multiple options. - * - * @param mixed $option A string with option name as value for - * single option or An associative array contains - * options array('<option>' => <value>) for multiple - * options. - * @param mixed $value (optional) Option value. Require when $option is - * string (single option mode). - * - * @return bool|PEAR_Error TRUE on success, PEAR_Error on failure. - * @access public - */ - function set($option, $value = null) + function Contact_AddressBook_Builder($data = null) { - if (is_array($option)) { - foreach ($option as $key => $val) { - $err = $this->set($key, $val); - if (PEAR::isError($err)) { - return $err; - } - } - } elseif (is_string($option)) { - if (isset($this->options[$option])) { - if (gettype($this->options[$option]) == gettype($value)) { - $this->options[$option] = $value; - } else { - return PEAR::raiseError('Type mismatch for option '. - "'$option' value"); - } - } + if (is_array($data)) { + $this->setData($data); } } @@ -210,7 +139,7 @@ function setData($data) { if (!is_array($data)) { - return PEAR::raiseError('Type mismatch for map only array allowed'); + return PEAR::raiseError('Type mismatch for data, only array allowed'); } $this->data = $data; @@ -228,7 +157,7 @@ */ function build() { - return PEAR::raiseError('unsupported'); + return PEAR::raiseError('Not implemented'); } // }}} @@ -241,7 +170,7 @@ */ function show() { - print $this->result; + print htmlspecialchars($this->result); } // }}} @@ -257,6 +186,7 @@ */ function save($filename) { + $filename .= '.' . $this->extension; return File::write($filename, $this->result, FILE_MODE_WRITE); } @@ -266,43 +196,17 @@ /** * Download the result via browser. * - * @param string $filename (optional) Filename. + * @param string $filename Filename. * * @access public */ - function download($filename = null) + function download($filename) { - if ($filename === null) { - $filename = 'addressbook'; - - // Add specific extension depending classname. - if (substr(get_class($this), 28, 3) == 'csv') { - $filename .= '.csv'; - } elseif (substr(get_class($this), 28, 4) == 'ldif') { - $filename .= '.ldif'; - } else { - $filename .= '.txt'; - } - } - - $extension = ''; - if (($pos = strrpos($filename, '.')) !== false) { - $extension = substr($filename, $pos + 1); - } - - switch (strtolower($extension)) { - case 'csv': - $mimetype = 'text/x-csv'; - break; - default: - $mimetype = 'text/plain'; - break; - } - if (headers_sent()) { return PEAR::raiseError('Cannot process headers, headers already sent'); } + $filename .= '.' . $this->extension; if (Net_UserAgent_Detect::isIE()) { // IE need specific headers @@ -316,12 +220,29 @@ header('Pragma: no-cache'); } - header('Content-Type: ' . $mimetype); - print $this->result; + header('Content-Type: ' . $this->mime); + print $this->show(); exit; } // }}} + // {{{ stripLineBreak() + + /** + * Strip any line break characters then replace it with space. + * + * @param string $str Value. + * + * @return string Result string. + * @access public + * @static + */ + function stripLineBreak($str) + { + return preg_replace('/\r\n|\r|\n/', ' ', $str); + } + + // }}} } // }}} http://cvs.php.net/diff.php/pear/Contact_AddressBook/Contact/AddressBook/CSV.php?r1=1.1&r2=1.2&ty=u Index: pear/Contact_AddressBook/Contact/AddressBook/CSV.php diff -u pear/Contact_AddressBook/Contact/AddressBook/CSV.php:1.1 pear/Contact_AddressBook/Contact/AddressBook/CSV.php:1.2 --- pear/Contact_AddressBook/Contact/AddressBook/CSV.php:1.1 Sun Mar 27 14:36:39 2005 +++ pear/Contact_AddressBook/Contact/AddressBook/CSV.php Tue Aug 16 16:43:01 2005 @@ -4,17 +4,15 @@ // {{{ Header /** - * $ShortFileDescription$ + * File contains Contact_AddressBook_CSV class. * - * $LongFileDescription$ - * - * PHP version $PHPVersion$ + * PHP versions 4 and 5 * * LICENSE: * * BSD License * - * Copyright (c) $CopyrightYear$ Firman Wandayandi + * Copyright (c) 2004-2005 Firman Wandayandi * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -44,206 +42,147 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * @category $CategoryName$ - * @package $PackageName$ + * @category File Formats + * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> - * @copyright Copyright (c) $CopyrightYear$ Firman Wandayandi + * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License - * @version CVS: $Id: CSV.php,v 1.1 2005/03/27 19:36:39 firman Exp $ - * @since File available since Release 0.2.0dev1 + * @version CVS: $Id: CSV.php,v 1.2 2005/08/16 20:43:01 firman Exp $ + * @since File available since Release 0.4.0alpha1 */ // }}} -// {{{ Dependencies - -/** - * Load PEAR for error handling. - */ -require_once 'PEAR.php'; - -// }}} // {{{ Global Variables /** * CSV Formats informations. * - * @global array $GLOBALS['Contact_AddressBook_CSV_configs'] - * @name $Contact_AddressBook_CSV_configs + * @global array $GLOBALS['_Contact_AddressBook_CSV_configs'] + * @name $_Contact_AddressBook_CSV_configs */ -$GLOBALS['Contact_AddressBook_CSV_configs'] = array( - 'wab' => 'Contact/AddressBook/CSV/Config/WAB.php', - 'outlookexpress' => 'Contact/AddressBook/CSV/Config/WAB.php', - 'outlook' => 'Contact/AddressBook/CSV/Config/Outlook.php', - 'mozilla' => 'Contact/AddressBook/CSV/Config/Mozilla.php', - 'nestcape' => 'Contact/AddressBook/CSV/Config/Mozilla.php', - 'thunderbird' => 'Contact/AddressBook/CSV/Config/Mozilla.php', - 'yahoo' => 'Contact/AddressBook/CSV/Config/Yahoo.php', - 'palm' => 'Contact/AddressBook/CSV/Config/Palm.php' +$GLOBALS['_Contact_AddressBook_CSV_configs'] = array( + 'wab' => 'WAB', + 'outlookexpress' => 'WAB', + 'outlook' => 'Outlook', + 'mozilla' => 'Mozilla', + 'nestcape' => 'Mozilla', + 'thunderbird' => 'Mozilla', + 'yahoo' => 'Yahoo', + 'palm' => 'Palm', + 'kmail' => 'KMail' ); /** * CSV Formats informations. * - * @global array $GLOBALS['Contact_AddressBook_CSV_headers'] - * @name $Contact_AddressBook_CSV_headers + * @global array $GLOBALS['_Contact_AddressBook_CSV_headers'] + * @name $_Contact_AddressBook_CSV_headers */ -$GLOBALS['Contact_AddressBook_CSV_headers'] = array( +$GLOBALS['_Contact_AddressBook_CSV_headers'] = array( 'wab' => 'WAB', 'outlookexpress' => 'WAB', 'outlook' => 'Outlook', - 'yahoo' => 'Yahoo' -); - -/** - * Field definitions. - * - * @global array $GLOBALS['Contact_AddressBook_CSV_definitions'] - * @name $Contact_AddressBook_CSV_definitions - */ -$GLOBALS['Contact_AddressBook_CSV_definitions'] = array( - 'wab' => 'WAB.def', - 'outlookexpress' => 'WAB.def', - 'outlook' => 'Outlook.def', - 'mozilla' => 'Mozilla.def', - 'nestcape' => 'Mozilla.def', - 'thunderbird' => 'Mozilla.def', - 'yahoo' => 'Yahoo.def', - 'palm' => 'Palm.def' + 'yahoo' => 'Yahoo', + 'kmail' => 'KMail' ); // }}} -// {{{ Class: $ClassName$ +// {{{ Class: Contact_AddressBook_CSV /** - * $ShortClassDescription$ + * Global class for CSV class. * - * $LongClassDescription$ - * - * @category $CategoryName$ - * @package $PackageName$ + * @category File Formats + * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> - * @copyright Copyright (c) $CopyrightYear$ Firman Wandayandi + * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ - * @since Class available since Release 0.2.0dev1 + * @since Class available since Release 0.4.0alpha1 */ class Contact_AddressBook_CSV { - // {{{ loadConfig() + // {{{ getConfig() /** - * Load CSV file format configuration for the specified CSV address book. + * Load CSV file format configuration for the specified CSV address book, + * then return it. * - * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. - * @access protected + * @param string $format Address book format. + * + * @return array|PEAR_Error An array of config on success or + * PEAR_Error on failure. + * @access public + * @static */ - function loadConfig($format) + function getConfig($format) { - if (!isset($GLOBALS['Contact_AddressBook_CSV_configs'][$format])) - { - return PEAR::raiseError('Uknown format \'' . $format .'\''); + if (!isset($GLOBALS['_Contact_AddressBook_CSV_configs'][$format])) { + return PEAR::raiseError('No such config for \'' . $format .'\''); } - include_once $GLOBALS['Contact_AddressBook_CSV_configs'][$format]; - if (!isset($GLOBALS['Contact_AddressBook_CSV_config'])) - { - return PEAR::raiseError('Undefined configuration'); - } + $dir = Contact_AddressBook::getDataDir() . '/CSV/Config/'; - return $GLOBALS['Contact_AddressBook_CSV_config']; - } + $file = $dir . + $GLOBALS['_Contact_AddressBook_CSV_configs'][$format] . '.php'; + include_once $file; - // }}} - // {{{ getConfig() + if (!isset($GLOBALS['_Contact_AddressBook_CSV_config'])) + { + return PEAR::raiseError('Undefined config for \'' . $format .'\''); + } - function getConfig($format) - { $default = array( 'quote' => '"', 'linebreak' => 'auto', 'selectablefields' => false ); - $config = Contact_AddressBook_CSV::loadConfig($format); - if (PEAR::isError($config)) - { - return $config; - } - - return array_merge($default, $config); - } - - // }}} - // {{{ getDefaultDefinitionFile() - - function getDefaultDefinitionFile($format) - { - if (!isset($GLOBALS['Contact_AddressBook_CSV_definitions'][$format])) - { - return PEAR::raiseError('Uknown format \'' . $format .'\''); - } - - include_once 'PEAR/Config.php'; - $config = new PEAR_Config; - $dir = $config->get('data_dir') . - '/Contact_AddressBook/definitions/CSV/'; - $file = $dir . $GLOBALS['Contact_AddressBook_CSV_definitions'][$format]; - return $file; + return array_merge($default, + $GLOBALS['_Contact_AddressBook_CSV_config']); } // }}} - // {{{ loadDefinition() - - function loadDefinition($file) - { - ini_set('track_error', true); - $res = parse_ini_file($file); - if (empty($res)) - { - return PEAR::raiseError($php_errormsg); - } - - return $res; - } - - // }}} - // {{{ loadHeader() + // {{{ getHeader() /** - * Load CSV file format configuration for the specified CSV address book. + * Load CSV file format configuration for the specified CSV address book, + * then return it. + * + * @param string $format Address book format. + * @param string $language (optional) Address book languange + * (use language code), default is "en". * - * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. - * @access protected + * @return array|PEAR_Error An array of header on success or + * PEAR_Error on failure. + * @access public + * @static */ - function loadHeader($format, $languange = null) + function getHeader($format, $language = null) { - if (!isset($GLOBALS['Contact_AddressBook_CSV_headers'][$format])) - { - return PEAR::raiseError('Uknown format \'' . $format .'\''); + if (!isset($GLOBALS['_Contact_AddressBook_CSV_headers'][$format])) { + return PEAR::raiseError('No such header for \'' . $format .'\''); } - if ($languange !== null) - { - $languange = 'en'; - } - else - { - $languange = strtolower($languange); + if ($language === null) { + $language = 'en'; + } else { + $language = strtolower($language); } - $dir = 'Contact/AddressBook/CSV/Header/'; - $file = $GLOBALS['Contact_AddressBook_CSV_headers'][$format] . '/' . - $languange . '.php'; + $dir = Contact_AddressBook::getDataDir() . '/CSV/Header/'; + $file = $dir . + $GLOBALS['_Contact_AddressBook_CSV_headers'][$format] . '/' . + $language . '.php'; + include_once $file; - include_once $dir . $file; - if (!isset($GLOBALS['Contact_AddressBook_CSV_header'])) - { - return PEAR::raiseError('Undefined header'); + if (!isset($GLOBALS['_Contact_AddressBook_CSV_header'])) { + return PEAR::raiseError('Undefined header for \'' . $format .'\''); } - return $GLOBALS['Contact_AddressBook_CSV_header']; + return $GLOBALS['_Contact_AddressBook_CSV_header']; } // }}} @@ -251,8 +190,6 @@ // }}} -print_r(Contact_AddressBook_CSV::loadHeader('yahoo', 'en')); - /* * Local variables: * mode: php http://cvs.php.net/diff.php/pear/Contact_AddressBook/Contact/AddressBook/Converter.php?r1=1.2&r2=1.3&ty=u Index: pear/Contact_AddressBook/Contact/AddressBook/Converter.php diff -u pear/Contact_AddressBook/Contact/AddressBook/Converter.php:1.2 pear/Contact_AddressBook/Contact/AddressBook/Converter.php:1.3 --- pear/Contact_AddressBook/Contact/AddressBook/Converter.php:1.2 Sun Mar 27 14:28:59 2005 +++ pear/Contact_AddressBook/Contact/AddressBook/Converter.php Tue Aug 16 16:43:01 2005 @@ -42,22 +42,36 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License - * @version CVS: $Id: Converter.php,v 1.2 2005/03/27 19:28:59 firman Exp $ + * @version CVS: $Id: Converter.php,v 1.3 2005/08/16 20:43:01 firman Exp $ */ // }}} +// {{{ Global Variables + +/** + * Definition aliases. + * + * @global array $GLOBALS['_Contact_AddressBook_definitions'] + * @name $_Contact_AddressBook_definitions + * @ignore + */ +$GLOBALS['_Contact_AddressBook_definitions'] = array( + 'eudora' => 'Eudora' +); + +// }}} // {{{ Class: Contact_AddressBook_Converter /** * Class for handling address book data structure converting. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi @@ -69,7 +83,13 @@ { // {{{ Properties - var $definitionFile = ''; + /** + * Directory contains definition files. + * + * @var string + * @access protected + */ + var $definitionDir = ''; /** * Address Book fields definition. @@ -103,79 +123,54 @@ */ var $isPrepared = false; - var $format = ''; - var $languange = 'en'; - // }}} // {{{ Constructor - /** - * PHP4 Compatible contructor. - * - * @param string $definitionFile Definition filename. - * - * @access public - * @see __construct() - */ - function Contact_AddressBook_Converter($format, $languange = null) - { - $this->__construct($format, $languange); - } - - /** - * PHP5 Compatible contructor. - * - * @param string $definitionFile Definition filename. - * - * @access public - */ - function __construct($format, $language = null) + function Contact_AddressBook_Converter() { - $this->format = $format; - if ($language !== null) - { - $this->languange = $language; - } + $this->setDefinitionDir(Contact_AddressBook::getDataDir() . '/Defs/'); } // }}} - // {{{ setDefinitionFile() + // {{{ setDefinitionDir() - function setDefinitionFile($file) + function setDefinitionDir($dir) { - $this->definitionFile = $file; - return $this->loadDefinition(); + $this->definitionDir = $dir; } // }}} - // {{{ loadDefinitionFile() + // {{{ setFormat() /** - * Load the definition file. - * The bundle default definition files was placed at PEAR data dir. + * Set the address book format to convert. * - * @param string $file Definition filename. + * @param string $format The address book format. * - * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. + * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. * @access public + * + * @see Contact_AddressBook_Converter::setDefinitionFile() */ - function loadDefinition() + function setFormat($format) { - $res = Contact_AddressBook_CSV::loadDefinition($this->definitionFile); - if (empty($res)) - { - return PEAR::raiseError($php_errormsg); + $format = strtolower($format); + + if (!isset($GLOBALS['_Contact_AddressBook_definitions'][$format])) { + return PEAR::raiseError('No such definition for \'' . $format .'\''); } - $this->setDefinition($res); - return true; + $file = $this->definitionDir . + $GLOBALS['_Contact_AddressBook_definitions'][$format] . '.def'; + + return $this->setDefinitionFile($file); } // }}} // {{{ setDefinition() /** - * Set custom definition on runtime. + * Set converting definition. * * @param array $definition An array of definition structure. * @@ -193,16 +188,49 @@ // Set isPrepared flag to false, we need to reset the maps with new // definition. - if ($this->isPrepared) - { - $this->isPrepared = false; + if ($this->isPrepared) { + $this->isPrepare = false; + } + + return true; + } + + // }}} + // {{{ setDefinitionFile() + + /** + * Load the definition file. + * The bundle default definition files was placed at PEAR data dir. + * + * @param string $file Definition filename. + * + * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. + * @access public + * + * @see Contact_AddressBook_Converter::setDefinition() + */ + function setDefinitionFile($file) + { + if (!is_file($file) || !file_exists($file)) { + return PEAR::raiseError('No such file \'' . $file . '\''); + } + + ini_set('track_error', true); + $res = @parse_ini_file($file); + if (empty($res)) { + return PEAR::raiseError($php_errormsg); + } + + $res = $this->setDefinition($res); + if (PEAR::isError($res)) { + return $res; } return true; } // }}} - // {{{ _prepare() + // {{{ prepare() /** * Prepare map for converting. @@ -210,17 +238,10 @@ * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. * @access private */ - function _prepare() + function prepare() { if (empty($this->definition)) { - $res = $this->setDefinitionFile( - Contact_AddressBook_CSV::getDefaultDefinitionFile($this->format) - ); - - if (PEAR::isError($res)) - { - return $res; - } + return PEAR::raiseError('Definition never set'); } $this->mapFrom =& $this->definition; @@ -286,7 +307,10 @@ function convertTo($data) { if (!$this->isPrepared) { - $this->_prepare(); + $res = $this->prepare(); + if (PEAR::isError($res)) { + return $res; + } } return $this->performConvert($this->mapTo, $data); @@ -308,14 +332,16 @@ function convertFrom($data) { if (!$this->isPrepared) { - $this->_prepare(); + $res = $this->prepare(); + if (PEAR::isError($res)) { + return $res; + } } return $this->performConvert($this->mapFrom, $data); } // }}} - } // }}} http://cvs.php.net/diff.php/pear/Contact_AddressBook/Contact/AddressBook/Parser.php?r1=1.2&r2=1.3&ty=u Index: pear/Contact_AddressBook/Contact/AddressBook/Parser.php diff -u pear/Contact_AddressBook/Contact/AddressBook/Parser.php:1.2 pear/Contact_AddressBook/Contact/AddressBook/Parser.php:1.3 --- pear/Contact_AddressBook/Contact/AddressBook/Parser.php:1.2 Sun Mar 27 14:34:17 2005 +++ pear/Contact_AddressBook/Contact/AddressBook/Parser.php Tue Aug 16 16:43:01 2005 @@ -4,7 +4,7 @@ // {{{ Header /** - * File contains Contact_AddressBook_CSV_Parser class. + * File contains Contact_AddressBook_Parser class. * * PHP versions 4 and 5 * @@ -42,231 +42,158 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License - * @version CVS: $Id: Parser.php,v 1.2 2005/03/27 19:34:17 firman Exp $ - * @since File available since Release 0.2.0dev1 + * @version CVS: $Id: Parser.php,v 1.3 2005/08/16 20:43:01 firman Exp $ */ // }}} // {{{ Dependencies /** - * Load Contact_AddressBook_Parser as the base class. + * Require File for handling files. */ -require_once 'Contact/AddressBook/Parser.php'; - -/** - * Load Contact_AddressBook_CSV for common functions. - */ -require_once 'Contact/AddressBook/CSV.php'; - -/** - * Load File_CSV for handling CSV parser. - */ -require_once 'File/CSV.php'; +require_once 'File.php'; // }}} -// {{{ Class: Contact_AddressBook_CSV_Parser +// {{{ Class: Contact_AddressBook_Parser /** - * Class for parse CSV address book format. + * Base class for Contact_AddressBook parser classes. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ - * @see File_CSV - * @since Class available since Release 0.2.0dev1 */ -class Contact_AddressBook_CSV_Parser extends Contact_AddressBook_Parser +class Contact_AddressBook_Parser { // {{{ Properties /** - * Configuration mapping to File_CSV. + * Parse result. * * @var array - * @access private + * @access protected + */ + var $result = array(); + + /** + * File to parse. + * + * @var string + * @access protected */ - var $_configMap = array( - 'fields' => 'fields', - 'quote' => 'quote', - 'header' => 'header', - 'crlf' => 'linebreak' - ); + var $file = ''; // }}} - // {{{ Constructor + // {{{ setFile() /** - * PHP4 compatible contructor. + * Set the input file to parse. * - * @param string $format CSV address book format. One of supported formats - * WAB, OutlookExpress, Outlook, Mozilla, Netscape, - * Thunderbird, Palm, Yahoo (case-insensitive and - * only alphanumeric letters). + * @param string $file Input filename. * + * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. * @access public - * @see __contruct() */ - function Contact_AddressBook_CSV_Parser($format) + function setFile($file) { - $this->__construct($format); + if (!file_exists($file)) { + return PEAR::raiseError('No such file \'' . $file . '\''); + } + + $this->file = $file; + return true; } + // }}} + // {{{ parse() + /** - * PHP5 compatible contructor. - * - * @param string $format CSV address book format. One of supported formats - * WAB, OutlookExpress, Outlook, Mozilla, Netscape, - * Thunderbird, Palm, Yahoo (case-insensitive and - * only alphanumeric letters). + * Parse input file to gets address book data. * + * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. * @access public - * @see __contruct() */ - function __construct($format) + function parse() { - $this->_format = strtolower($format); + return PEAR::raiseError('Not implemented'); } // }}} - // {{{ _parseConfig() + // {{{ getResult() /** - * Parse configuration for use in File_CSV. + * Get parse result data. * - * @access private - * @return array An array of configuration. + * @return array The result array. + * @access public */ - function _parseConfig() + function getResult() { - $sysconfig = Contact_AddressBook_CSV::getConfig($this->_format); - if (PEAR::isError($sysconfig)) - { - return $sysconfig; - } - - $config = array( - 'sep' => ',' - ); - - foreach ($this->_configMap as $key => $name) { - if (isset($sysconfig[$name])) { - if ($name == 'linebreak' && $sysconfig[$name] == 'auto') { - continue; - } - - $config[$key] = $sysconfig[$name]; - } - } - return $config; + return $this->result; } // }}} - // {{{ parse() + // {{{ numRows() /** - * Parse input file to gets address book data. + * Returns the number of rows in a result. * - * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. + * @return int The number of rows. * @access public */ - function parse() + function numRows() { - // File_CSV detected config. - $config = File_CSV::discoverFormat($this->file); - if (PEAR::isError($config)) - { - return $config; - } - - // Contact_AddressBook default CSV config. - $sysconfig = Contact_AddressBook_CSV::getConfig($this->_format); - if (PEAR::isError($sysconfig)) - { - return $sysconfig; - } - - // File_CSV config. - $csvconfig = $this->_parseConfig(); - if (PEAR::isError($csvconfig)) - { - return $csvconfig; - } - - // Force override fields count with system configuration, there's - // some bugs in File_CSV::discoverFormat(). - if (isset($csvconfig['fields'])) - { - unset($config['fields']); - } - - $config = array_merge($config, $csvconfig); - - if (isset($sysconfig['selectablefields']) && - $sysconfig['selectablefields']) - { - $config['header'] = false; - } - - $data = array(); - while ($res = File_CSV::readQuoted($this->file, $config)) { - $data[] = $res; - } - - if (!empty($data)) { - // Finalize header for address book selectable fields such - // WAB instead of Outlook Express. - if (isset($sysconfig['selectablefields']) && - $sysconfig['selectablefields']) - { - $data = Contact_AddressBook_CSV_Parser::finalizeHeader($data); - } + return count($this->result); + } - $this->result = $data; - } + // }}} + // {{{ reset() - return true; + /** + * Reset the parser. + * This method set the result into empty array. + * + * @access public + */ + function reset() + { + $this->result = array(); } // }}} - // {{{ finalizeHeader() + // {{{ getFileContents() /** - * Change the array of data from sequential array into associative array, - * using header elements as keys. + * Read the input file to gets file contents. * - * This function only called if configuration 'selectablefields' is TRUE. - * - * @return array() An associative array of result. - * @access public + * @return string|PEAR_Error String file contents on succeed or + * PEAR_Error on failure. + * @access protected + * @see File::read() * @static */ - function finalizeHeader($data) + function getFileContents() { - $header = array_shift($data); - $res = array(); - if (!is_array($header)) { - return $data; - } - - foreach ($header as $i => $field) { - foreach ($data as $j => $values) { - if (isset($values[$i])) { - $res[$j][$field] = $values[$i]; - } + $contents = ''; + while($res = File::read($this->file)) { + if (PEAR::isError($res)) { + return $res; } + + $contents .= $res; } - return $res; + + return $contents; } // }}} @@ -274,16 +201,6 @@ // }}} -$obj = new Contact_AddressBook_CSV_Parser('wab'); -$obj->setFile('files/WAB-selectable.csv'); -$obj->parse(); -$data = $obj->getResult(); - -require_once 'Contact/AddressBook/CSV/Converter.php'; -$conv = new Contact_AddressBook_CSV_Converter('wab'); -$res = $conv->convertFrom($data); -print_r($res); - /* * Local variables: * mode: php http://cvs.php.net/co.php/pear/Contact_AddressBook/Contact/AddressBook/Builder/CSV.php?r=1.1&p=1 Index: pear/Contact_AddressBook/Contact/AddressBook/Builder/CSV.php +++ pear/Contact_AddressBook/Contact/AddressBook/Builder/CSV.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ // {{{ Header /** * File contains Contact_AddressBook_Builder_CSV class. * * PHP versions 4 and 5 * * LICENSE: * * BSD License * * Copyright (c) 2004-2005 Firman Wandayandi * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * 3. Neither the name of Firman Wandayandi nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version CVS: $Id: CSV.php,v 1.1 2005/08/16 20:43:01 firman Exp $ */ // }}} // {{{ Dependencies /** * Load Contact_AddressBook_Builder as the base class. */ require_once 'Contact/AddressBook/Builder.php'; // }}} // {{{ Class: Contact_AddressBook_Builder_CSV /** * Class for building CSV address book format. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ */ class Contact_AddressBook_Builder_CSV extends Contact_AddressBook_Builder { // {{{ Properties /** * MIME Type when send the output to the browser. * * @var string * @access protected */ var $mime = 'text/x-csv'; /** * File extension. * * @var string * @access protected */ var $extension = 'csv'; /** * CSV address book format. * * @var string * @access private */ var $_format = ''; /** * Header language, this header use only when the address book format * show the header. * * @var string * @access private */ var $_language = 'en'; // }}} // {{{ setFormat() /** * Set the address book format. * * @param string $format Address book format. * * @access public */ function setFormat($format) { $this->_format = $format; } // }}} // {{{ setLanguage() /** * Set the language, only use address book show the header. * * @param string $language Language. * * @access public */ function setLanguage($language) { $this->_language = $language; } // }}} // {{{ build() /** * Build the structure format. * * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. * @access public */ function build() { $config = Contact_AddressBook_CSV::getConfig($this->_format); if (PEAR::isError($config)) { return $config; } if (isset($config['header']) && $config['header'] == true) { $header = Contact_AddressBook_CSV::getHeader($this->_format, $this->_language); if (PEAR::isError($header)) { return $header; } // Insert the header into data. array_unshift($this->data, $header); } $escapes = array(',', "\r", "\n", "\r\n", $config['quote']); // Fill values with empty, to deal with csv rule // (same fields count for all fields). $default = array_fill(0, $config['fields'], ''); $this->result = ''; foreach ($this->data as $record) { $record = $record + $default; ksort($record, SORT_NUMERIC); if (isset($config['quoted_field']) && $config['quoted_field'] == true) { $this->result .= $config['quote'] . implode($config['quote'] . ',' . $config['quote'], $record) . $config['quote']; } else { foreach ($record as $key => $value) { // Replace any line break if multilines support doesn't capable. if (!isset($config['multilines']) || $config['multilines'] === false) { $value = Contact_AddressBook_Builder::stripLineBreak($value); } // Quote any escape characters values. if (preg_match('/[' . implode($escapes) . ']+/', $value)) { $value = '"' . $value . '"'; } $record[$key] = $value; } $this->result .= implode(',', $record); } $this->result .= ($config['line_break'] == 'auto') ? PHP_EOL : $config['line_break']; } } // }}} } // }}} /* * Local variables: * mode: php * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/Contact_AddressBook/Contact/AddressBook/Builder/Eudora.php?r=1.1&p=1 Index: pear/Contact_AddressBook/Contact/AddressBook/Builder/Eudora.php +++ pear/Contact_AddressBook/Contact/AddressBook/Builder/Eudora.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ // {{{ Header /** * File contains Contact_AddressBook_Builder_Eudora class. * * PHP versions 4 and 5 * * LICENSE: * * BSD License * * Copyright (c) 2004-2005 Firman Wandayandi * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * 3. Neither the name of Firman Wandayandi nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version CVS: $Id: Eudora.php,v 1.1 2005/08/16 20:43:01 firman Exp $ */ // }}} // {{{ Dependencies /** * Load Contact_AddressBook_Builder as the base class. */ require_once 'Contact/AddressBook/Builder.php'; // }}} // {{{ Class: Contact_AddressBook_Builder_Eudora /** * Class for building the Eudora address book. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ */ class Contact_AddressBook_Builder_Eudora extends Contact_AddressBook_Builder { // {{{ build() /** * Build the structure format. * * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. * @access public */ function build() { $this->result = ''; foreach ($this->data as $record) { $alias = isset($record['nickname']) ? $record['nickname'] : ''; $this->result = 'alias ' . $alias . ' ' . $record['email'] . PHP_EOL; $this->result .= 'note ' . $alias . ' '; if (isset($record['nickname'])) { unset($record['nickname']); } foreach ($record as $field => $value) { if ($value == '') { continue; } if ($field == 'notes') { $this->result .= Contact_AddressBook_Builder::stripLineBreak($value); } else { $this->result .= '<' . $field . ':' . Contact_AddressBook_Builder::stripLineBreak($value) . '>'; } } $this->result .= PHP_EOL; } return true; } // }}} } // }}} /* * Local variables: * mode: php * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/Contact_AddressBook/Contact/AddressBook/Parser/CSV.php?r=1.1&p=1 Index: pear/Contact_AddressBook/Contact/AddressBook/Parser/CSV.php +++ pear/Contact_AddressBook/Contact/AddressBook/Parser/CSV.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ // {{{ Header /** * File contains Contact_AddressBook_Parser_CSV class. * * PHP versions 4 and 5 * * LICENSE: * * BSD License * * Copyright (c) 2004-2005 Firman Wandayandi * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * 3. Neither the name of Firman Wandayandi nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version CVS: $Id: CSV.php,v 1.1 2005/08/16 20:43:02 firman Exp $ */ // }}} // {{{ Dependencies /** * Load Contact_AddressBook_Parser as the base class. */ require_once 'Contact/AddressBook/Parser.php'; /** * Load Contact_AddressBook_CSV for common functions. */ require_once 'Contact/AddressBook/CSV.php'; /** * Load File_CSV for handling CSV parser. */ require_once 'File/CSV.php'; // }}} // {{{ Class: Contact_AddressBook_Parser_CSV /** * Class for parse CSV address book format. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ * @see File_CSV */ class Contact_AddressBook_Parser_CSV extends Contact_AddressBook_Parser { // {{{ Properties /** * Configuration mapping to File_CSV. * * @var array * @access private */ var $_configMap = array( 'fields' => 'fields', 'quote' => 'quote', 'header' => 'header', 'crlf' => 'line_break' ); // }}} // {{{ Constructor /** * Constructor. * * @param string $format CSV address book format. One of supported formats * WAB, OutlookExpress, Outlook, Mozilla, Netscape, * Thunderbird, Palm, Yahoo (case-insensitive and * only alphanumeric letters). * * @access public * @see Contact_AddressBook_Parser_CSV::setFormat() *//* function Contact_AddressBook_Parser_CSV($format) { $this->setFormat($format); } */ // }}} // {{{ setFormat() /** * Set the address book format. * * @param string $format CSV address book format. One of supported formats * WAB, OutlookExpress, Outlook, Mozilla, Netscape, * Thunderbird, Palm, Yahoo (case-insensitive and * only alphanumeric letters). * * @access public */ function setFormat($format) { $this->_format = strtolower($format); } // }}} // {{{ _parseConfig() /** * Parse configuration for File_CSV usage. * * @access private * @return array An array of configuration. */ function _parseConfig($sysconfig) { if (PEAR::isError($sysconfig)) { return $sysconfig; } // Separator are the commas. $config = array( 'sep' => ',' ); foreach ($this->_configMap as $key => $name) { if (isset($sysconfig[$name])) { // Ignore when line break set to "auto". if ($name == 'line_break' && $sysconfig[$name] == 'auto') { continue; } $config[$key] = $sysconfig[$name]; } } return $config; } // }}} // {{{ parse() /** * Parse input file to gets address book data. * * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. * @access public * * @see File_CSV::discoverFormat() * @see Contact_AddressBook_CSV::getConfig() * @see Contact_AddressBook_Parser_CSV::_parseConfig() * @see Contact_AddressBook_Parser_CSV::finalizeHeader() * @see File_CSV::readQuoted() */ function parse() { // Attempt to discover config format by File_CSV. $config = File_CSV::discoverFormat($this->file); if (PEAR::isError($config)) { return $config; } // Get Contact_AddressBook default CSV config. $sysconfig = Contact_AddressBook_CSV::getConfig($this->_format); if (PEAR::isError($sysconfig)) { return $sysconfig; } // Synchronize the config for File_CSV usage. $csvconfig = $this->_parseConfig($sysconfig); if (PEAR::isError($csvconfig)) { return $csvconfig; } // Force override fields count with system configuration, there's // some bugs in File_CSV::discoverFormat(). if (isset($csvconfig['fields'])) { unset($config['fields']); } $config = array_merge($config, $csvconfig); // Set the header into FALSE if selectablefields TRUE, // so File_CSV will be read this the header as a record. // This header required when finalizing and will be removed // automatically. if (isset($sysconfig['selectable_fields']) && $sysconfig['selectable_fields']) { $config['header'] = false; } $data = array(); while ($res = File_CSV::readQuoted($this->file, $config)) { $data[] = $res; } if (!empty($data)) { // Finalize header for address book selectable fields such // WAB instead of Outlook Express. if (isset($sysconfig['selectable_fields']) && $sysconfig['selectable_fields']) { $data = Contact_AddressBook_Parser_CSV::finalizeHeader($data); } $this->result = $data; } return true; } // }}} // {{{ finalizeHeader() /** * Change the array of data from sequential array into associative array, * using header elements as keys. * * This function only called if configuration 'selectablefields' is TRUE. * * @param array $data The return value from * Contact_AddressBook_Parser_CSV::parse() method. * * @return array() An associative array of result. * @access public * @static */ function finalizeHeader($data) { $header = array_shift($data); $res = array(); if (!is_array($header)) { return $data; } foreach ($header as $i => $field) { foreach ($data as $j => $values) { if (isset($values[$i])) { $res[$j][$field] = $values[$i]; } } } return $res; } // }}} // {{{ getResult() /** * Get the parse result. * * @return array An array of address book parse result. * @access public */ function getResult() { return $this->result; } // }}} } // }}} /* * Local variables: * mode: php * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/Contact_AddressBook/Contact/AddressBook/Parser/Eudora.php?r=1.1&p=1 Index: pear/Contact_AddressBook/Contact/AddressBook/Parser/Eudora.php +++ pear/Contact_AddressBook/Contact/AddressBook/Parser/Eudora.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ // {{{ Header /** * File contains Contact_AddressBook_Parser_Eudora class. * * PHP versions 4 and 5 * * LICENSE: * * BSD License * * Copyright (c) 2004-2005 Firman Wandayandi * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * 3. Neither the name of Firman Wandayandi nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version CVS: $Id: Eudora.php,v 1.1 2005/08/16 20:43:02 firman Exp $ */ // }}} // {{{ Dependencies /** * Load Contact_AddressBook_Parser as the base class. */ require_once 'Contact/AddressBook/Parser.php'; // }}} // {{{ Class: Contact_AddressBook_Parser_Eudora /** * Class for handling Eudora address book format parse. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ */ class Contact_AddressBook_Parser_Eudora extends Contact_AddressBook_Parser { // {{{ parse() /** * Parse input file to gets address book data. * * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. * @access public */ function parse() { $contents = Contact_AddressBook_Parser::getFileContents(); if ($contents == '') { return PEAR::raiseError("File '{$this->file}' is empty"); } $data = array(); // Grab all alias keyword. if (preg_match_all('/alias\s+(\w+)\s*(.*)\n/', $contents, $matches, PREG_SET_ORDER)) { $i = 0; foreach ($matches as $raw) { $emails = explode(',', $raw[2]); $data[$i]['nickname'] = $raw[1]; foreach ($emails as $j => $email) { if ($j == 0) { $data[$i]['email'] = $email; } else { $data[$i]['email' . ($j + 1)] = $email; } } $i++; } } // For count alias, grab the note keyword for nickname. // Where detail informations are place here. foreach ($data as $i => $val) { $regex = '/note\s+' . $val['nickname'] . '\s+(<.*\>)(.*)\n/'; if (preg_match_all($regex, $contents, $matches, PREG_SET_ORDER)) { foreach ($matches as $detail) { $data[$i]['notes'] = $detail[2]; if (preg_match_all('|<([^>]+)>|U', $detail[1], $matches_a)) { foreach ($matches_a[1] as $data_a) { list($field, $value) = explode(':', $data_a, 2); if ($field == 'otheremail') { $emails = explode(chr(3), $value); foreach ($emails as $j => $email) { if ($j == 0) { $data[$i]['otheremail'] = $email; } else { $data[$i]['otheremail' . ($j + 1)] = $email; } } continue; } $data[$i][$field] = $value; } } } } } $this->result = $data; return true; } } // }}} /* * Local variables: * mode: php * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?>
http://cvs.php.net/diff.php/pear/Contact_AddressBook/Contact/AddressBook.php?r1=1.1&r2=1.2&ty=u Index: pear/Contact_AddressBook/Contact/AddressBook.php diff -u pear/Contact_AddressBook/Contact/AddressBook.php:1.1 pear/Contact_AddressBook/Contact/AddressBook.php:1.2 --- pear/Contact_AddressBook/Contact/AddressBook.php:1.1 Sat Mar 26 13:24:34 2005 +++ pear/Contact_AddressBook/Contact/AddressBook.php Tue Aug 16 16:43:01 2005 @@ -1,70 +1,135 @@ <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ -// +----------------------------------------------------------------------+ -// | PHP version 4 | -// +----------------------------------------------------------------------+ -// | PEAR, the PHP Extension and Application Repository | -// +----------------------------------------------------------------------+ -// | Copyright (C) 2004 Firman Wandayandi | -// | All rights reserved. | -// +----------------------------------------------------------------------+ -// | This LICENSE is in the BSD license style. | -// | http://www.opensource.org/licenses/bsd-license.php | -// | | -// | Redistribution and use in source and binary forms, with or without | -// | modification, are permitted provided that the following conditions | -// | are met: | -// | | -// | Redistributions of source code must retain the above copyright | -// | notice, this list of conditions and the following disclaimer. | -// | | -// | Redistributions in binary form must reproduce the above | -// | copyright notice, this list of conditions and the following | -// | disclaimer in the documentation and/or other materials provided | -// | with the distribution. | -// | | -// | Neither the name of Firman Wandayandi nor the names of | -// | contributors may be used to endorse or promote products derived | -// | from this software without specific prior written permission. | -// | | -// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | -// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | -// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | -// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | -// | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | -// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | -// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | -// | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | -// | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | -// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | -// | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | -// | POSSIBILITY OF SUCH DAMAGE. | -// +----------------------------------------------------------------------+ -// | Authors: Firman Wandayandi <firman@php.net> | -// +----------------------------------------------------------------------+ -// -// $Id: AddressBook.php,v 1.1 2005/03/26 18:24:34 firman Exp $ + +// {{{ Header /** - * File contains Contact_AddressBook class. + * File contains main class of Contact_AddressBook. * - * @author Firman Wandayandi <firman@php.net> + * PHP versions 4 and 5 + * + * LICENSE: + * + * BSD License + * + * Copyright (c) 2004-2005 Firman Wandayandi + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. Neither the name of Firman Wandayandi nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * @category File Formats * @package Contact_AddressBook - * @category FileFormats + * @author Firman Wandayandi <firman@php.net> + * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License + * @version CVS: $Id: AddressBook.php,v 1.2 2005/08/16 20:43:01 firman Exp $ */ +// }}} +// {{{ Dependencies + /** - * Use PEAR for handling error. + * Load PEAR as main framework. */ require_once 'PEAR.php'; +// }}} +// {{{ Global Variables + +/** + * Classes file path informations. + * + * @global array $GLOBALS['_Contact_AddressBook_paths'] + * @name $_Contact_AddressBook_paths + */ +$GLOBALS['_Contact_AddressBook_paths'] = array( + 'csv' => array( + 'parser' => array( + 'Contact/AddressBook/Parser/CSV.php', + 'Contact_AddressBook_Parser_CSV' + ), + 'converter' => array( + 'Contact/AddressBook/Converter/CSV.php', + 'Contact_AddressBook_Converter_CSV' + ), + 'builder' => array( + 'Contact/AddressBook/Builder/CSV.php', + 'Contact_AddressBook_Builder_CSV' + ) + ), + 'eudora' => array( + 'parser' => array( + 'Contact/AddressBook/Parser/Eudora.php', + 'Contact_AddressBook_Parser_Eudora' + ), + 'converter' => array( + 'Contact/AddressBook/Converter.php', + 'Contact_AddressBook_Converter' + ), + 'builder' => array( + 'Contact/AddressBook/Builder/Eudora.php', + 'Contact_AddressBook_Builder_Eudora' + ) + ) +); + +// }}} +// {{{ Initial Codes + +// PHP_EOL Available since PHP 4.3.10 and PHP 5.0.2 +if (!defined('PHP_EOL')) { + // Fix this for MAC eol enable. + if (PEAR_OS == 'Windows') { + define('PHP_EOL', "\r\n"); + } else { + define('PHP_EOL', "\n"); + } +} + +// }}} +// {{{ Class: Contact_AddressBook + /** - * Main Address_Book class. + * Main Contact_AddressBook class. * + * @category File Formats + * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> - * @package Contact_Address_Book + * @copyright Copyright (c) 2004-2005 Firman Wandayandi + * @license http://www.opensource.org/licenses/bsd-license.php + * BSD License + * @version Release: @package_version@ + * + * @todo Add Vcard, LDIF and WAB (Binary) support. + * @todo Mac format complain. + * @todo PHPUnit tests. + * @todo Add storage backend (is it necessary needed?) */ class Contact_AddressBook { @@ -76,44 +141,57 @@ * @var array * @access protected */ - var $options = array( - 'def_dir' => '', // Definition directory. - 'languange' => 'en', // Language. - 'line_break' => "\n" // Line break character for writing a file. + var $_options = array( + 'def_dir' => '', // Definitions directory. + 'language' => 'en' // Language. ); - // }}} - // {{{ Constructor - /** - * PHP4 compatible constructor. + * Supported format names. + * Formats (case-insensitive): + * <pre> + * csv_wab Ms Windows Address Book CSV. + * csv_outlookexpress Ms Windows Outlook Express CSV (equal with WAB). + * csv_outlook Ms Outlook CSV. + * csv_mozilla Mozilla Mailer CSV. + * csv_thunderbird Mozilla Thunderbird CSV (equal with Mozilla). + * csv_netscape Netscape Mailer CSV (equal with Mozilla). + * csv_yahoo Yahoo! CSV. + * csv_palm Palm CSV. + * eudora Eudora address book. + * </pre> * - * @param array $options Options. - * - * @access public + * @var array + * @access private */ - function Contact_AddressBook($options = null) - { - $this->__construct($options); - } + var $_supportedFormats = array( + 'csv_wab', + 'csv_outlookexpress', + 'csv_outlook', + 'csv_mozilla', + 'csv_thunderbird', + 'csv_netscape', + 'csv_yahoo', + 'csv_palm', + 'csv_kmail', + 'eudora' + ); + + // }}} + // {{{ Constructor /** - * PHP5 compatible constructor. + * Constructor. * - * @param array $options Options. + * @param array $options (optional) The options. * * @access public */ - function __construct($options = null) + function Contact_AddressBook($options = null) { - if (!isset($options['def_dir'])){ - require_once 'PEAR/Config.php'; - $config = new PEAR_Config; - $options['def_dir'] = $config->get('data_dir') . - '/Contact_AddressBook/definitions/'; + if (is_array($options)) { + $this->set($options); } - - $this->set($options); } // }}} @@ -144,9 +222,9 @@ } } } elseif (is_string($option)) { - if (isset($this->options[$option])) { - if (gettype($this->options[$option]) == gettype($value)) { - $this->options[$option] = $value; + if (isset($this->_options[$option])) { + if (gettype($this->_options[$option]) == gettype($value)) { + $this->_options[$option] = $value; } else { return PEAR::raiseError("Type mismatch for option '$option' value"); } @@ -155,79 +233,96 @@ } // }}} - // {{{ createObject() + // {{{ get() /** - * Create AddressBook component object. + * Get single or all options. * - * @param string $file Filename. - * @param string $class Classname. + * @param string $option (optional) Option name. * - * @return object Component object on success or PEAR_Error on failure. + * @return mixed Value of option or an array of all options. * @access public - * @static */ - function &createObject($file, $class) + function get($option = null) { - include_once $file; - - if (!class_exists($class)) { - return PEAR::raiseError("Undefined class '$class'"); + if ($option !== null) { + if (isset($this->_options[$option])) { + return $this->_options[$option]; + } else { + return PEAR::raiseError('Unknown option "' . $option . '"'); + } } + return $this->_options; + } - $obj =& new $class; - return $obj; + // }}} + // {{{ getDataDir() + + /** + * Get PEAR data directory. + * + * @return string PEAR data directory. + * @access public + * @static + */ + function getDataDir() + { + require_once 'PEAR/Config.php'; + $config = new PEAR_Config; + return $config->get('data_dir') . '/Contact_AddressBook/data'; } // }}} - // {{{ createParser() + // {{{ isSupported() /** - * Create parser object for specified driver. + * Find the whether address book format is supported or not. * - * @param string $driver Driver name. + * @param string $format Address book format. * - * @return object Parser object on success or PEAR_Error on failure. + * @return bool TRUE if supported, otherwise FALSE. * @access public */ - function &createParser($driver) + function isSupported($format) { - if (substr($driver, 0, 3) == 'csv') { - $class = 'Contact_AddressBook_Parser_csv'; - $file = 'Contact/AddressBook/Parser/csv.php'; - } else { - $class = 'Contact_AddressBook_Parser_' . $driver; - $file = "Contact/AddressBook/Parser/{$driver}.php"; - } - - $obj =& Contact_AddressBook::createObject($file, $class); - return $obj; + return in_array($format, $this->_supportedFormats); } // }}} - // {{{ createBuilder() + // {{{ createParser() /** - * Create builder object for specified driver. + * Create parser object for the specified address book format. * - * @param string $driver Driver name. + * @param string $format The address book format name. * - * @return object Builder object on success or PEAR_Error on failure. + * @return object Parser object on success or PEAR_Error on failure. * @access public */ - function &createBuilder($driver, $options = null) + function createParser($format) { - $class = 'Contact_AddressBook_Builder_' . $driver; - if (substr($driver, 0, 3) == 'csv') { - $driver = str_replace('csv_', '', $driver); - $file = "Contact/AddressBook/Builder/csv/{$driver}.php"; + $format = strtolower($format); + if (!$this->isSupported($format)) { + return PEAR::raiseError('Not supported format \'' . $format .'\''); + } + + if (substr($format, 0, 3) == 'csv') { + $format = substr($format, 4); + $file = $GLOBALS['_Contact_AddressBook_paths']['csv']['parser'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths']['csv']['parser'][1]; + + include_once $file; + $obj = new $class($format); + $res = $obj->setFormat($format); + if (PEAR::isError($res)) { + return $res; + } } else { - $file = "Contact/AddressBook/Builder/{$driver}.php"; - } + $file = $GLOBALS['_Contact_AddressBook_paths'][$format]['parser'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths'][$format]['parser'][1]; - $obj =& Contact_AddressBook::createObject($file, $class); - if (is_a($obj, 'Contact_AddressBook_Builder')) { - $obj->set($this->options); + include_once $file; + $obj = new $class; } return $obj; @@ -237,88 +332,146 @@ // {{{ createConverter() /** - * Create converter object for specified driver. + * Create converter object for the specified address book format. * - * @param string $driver Driver name. + * @param string $format The address book format name. * * @return object Converter object on success or PEAR_Error on failure. * @access public */ - function &createConverter($driver) + function createConverter($format) { - $class = 'Contact_AddressBook_Converter'; - $file = 'Contact/AddressBook/Converter.php'; + $format = strtolower($format); + if (!$this->isSupported($format)) { + return PEAR::raiseError('Not supported format \'' . $format .'\''); + } + + if (substr($format, 0, 3) == 'csv') { + $format = substr($format, 4); + $file = $GLOBALS['_Contact_AddressBook_paths']['csv']['converter'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths']['csv']['converter'][1]; + + include_once $file; + $obj = new $class; + + if (!empty($this->_options['def_dir'])) { + $obj->setDefinitionDir($this->_options['def_dir']); + } + + $res = $obj->setFormat($format); + if (PEAR::isError($res)) { + return $res; + } + + $obj->setLanguage($this->_options['language']); + } else { + $file = $GLOBALS['_Contact_AddressBook_paths'][$format]['converter'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths'][$format]['converter'][1]; + + include_once $file; + $obj = new $class; - $obj =& Contact_AddressBook::createObject($file, $class); - if (is_a($obj, 'Contact_AddressBook_Converter')) { - $obj->setDefinitionDir($this->options['def_dir']); - $obj->loadDefinitionFile($driver . '.def'); + $res = $obj->setFormat($format); + if (PEAR::isError($res)) { + return $res; + } } return $obj; } // }}} - // {{{ isExportable() + // {{{ createBuilder() /** - * Find out the whether address book format is exportable. + * Create builder object for the specified address book format. * - * @param string $format Address book format name. + * @param string $format The address book format name. * - * @return bool TRUE if exportable, otherwise FALSE. + * @return object Builder object on success or PEAR_Error on failure. * @access public */ - function isExportable($format) + function createBuilder($format) { - if (substr($driver, 0, 3) == 'csv') { - $format = str_replace('csv_', '', $format); - $file = "Contact/AddressBook/Builder/csv/{$format}.php"; + $format = strtolower($format); + if (!$this->isSupported($format)) { + return PEAR::raiseError('Not supported format \'' . $format .'\''); + } + + if (substr($format, 0, 3) == 'csv') { + $format = substr($format, 4); + $file = $GLOBALS['_Contact_AddressBook_paths']['csv']['builder'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths']['csv']['builder'][1]; + + include_once $file; + $obj = new $class; + + $res = $obj->setFormat($format); + if (PEAR::isError($res)) { + return $res; + } + + $obj->setLanguage($this->_options['language']); } else { - $file = "Contact/AddressBook/Builder/{$format}.php"; + $file = $GLOBALS['_Contact_AddressBook_paths'][$format]['builder'][0]; + $class = $GLOBALS['_Contact_AddressBook_paths'][$format]['builder'][1]; + + include_once $file; + $obj = new $class; } - return file_exists($file); + return $obj; } // }}} - // {{{ isImportable() + // {{{ import() /** - * Find out the whether address book format is importable. + * Import data from specified format structure to internal structure. * - * @param string $format Address book format name. + * @param array $data Data. + * @param string $format Structure format. * - * @return bool TRUE if importable, otherwise FALSE. + * @return array|PEAR_Error An array result on success or PEAR_Error on + * failure. * @access public */ - function isImportable($format) + function import($data, $format) { - $file = $this->options['def_dir'] . $format . '.def'; - return file_exists($file); + $conv = $this->createConverter($format); + if (PEAR::isError($conv)) { + return $conv; + } + + return $conv->convertFrom($data); } // }}} - // {{{ import() + // {{{ importFromFile() /** - * Import data from specified format structure to internal structure. + * Import data from specified format structure saved in the file to + * internal structure. * - * @param array $data Data. - * @param string $format Structure format. + * @param string $filename Filename. + * @param string $format File format. * * @return array|PEAR_Error An array result on success or PEAR_Error on * failure. * @access public */ - function import($data, $format) + function importFromFile($filename, $format) { - $conv =& $this->createConverter($format); - if (PEAR::isError($conv)) { - return $conv; + $parser =& $this->createParser($format); + if (PEAR::isError($parser)) { + return $parser; } - return $conv->convertFrom($data); + $parser->setFile($filename); + $parser->parse(); + $res = $parser->getResult(); + + return $this->import($res, $format); } // }}} @@ -346,51 +499,57 @@ } // }}} - // {{{ importFromFile() + // {{{ exportToFile() /** - * Import data from specified format structure saved in the file to - * internal structure. + * Export data from internal format to external data structure and save + * it to file. * * @param string $filename Filename. * @param string $format File format. + * @param array $data Address book contacts data. + * @param bool $via_browser (optional) Tell the whether to output via + * browser (downloading) or not. * * @return array|PEAR_Error An array result on success or PEAR_Error on * failure. * @access public */ - function importFromFile($filename, $format) + function exportToFile($filename, $format, $data, $via_browser = true) { - $parser =& $this->createParser($format); - if (PEAR::isError($parser)) { - return $parser; + $data = $this->export($data, $format); + if (PEAR::isError($data)) { + return $data; } - $parser->setInput($filename); - $parser->parse(); - $res = $parser->getResult(); + $builder =& $this->createBuilder($format); + if (PEAR::isError($builder)) { + return $builder; + } - return $this->import($res, $format); + $builder->setData($data); + $builder->build(); + + if ($via_browser) { + return $builder->download($filename); + } else { + return $builder->save($filename); + } } // }}} - // {{{ exportToFile() + // {{{ exportToPrint() /** - * Export data from internal format to external data structure and save - * it to file. + * Export data from internal format to external data structure and print it. * - * @param string $filename Filename. * @param string $format File format. * @param array $data Address book contacts data. - * @param bool $via_browser Tell the whether to output via browser - * (downloading) or not. * - * @return array|PEAR_Error An array result on success or PEAR_Error on - * failure. + * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. * @access public */ - function exportToFile($filename, $format, $data, $via_browser = true) + function exportToPrint($format, $data) { $data = $this->export($data, $format); if (PEAR::isError($data)) { @@ -405,16 +564,15 @@ $builder->setData($data); $builder->build(); - if ($via_browser) { - return $builder->download($filename); - } else { - return $builder->save($filename); - } + $builder->show(); + return true; } // }}} } +// }}} + /* * Local variables: * mode: php http://cvs.php.net/diff.php/pear/Contact_AddressBook/Contact/AddressBook/Builder.php?r1=1.2&r2=1.3&ty=u Index: pear/Contact_AddressBook/Contact/AddressBook/Builder.php diff -u pear/Contact_AddressBook/Contact/AddressBook/Builder.php:1.2 pear/Contact_AddressBook/Contact/AddressBook/Builder.php:1.3 --- pear/Contact_AddressBook/Contact/AddressBook/Builder.php:1.2 Sun Mar 27 14:25:38 2005 +++ pear/Contact_AddressBook/Contact/AddressBook/Builder.php Tue Aug 16 16:43:01 2005 @@ -4,7 +4,7 @@ // {{{ Header /** - * File contains Contact_AddressBook_Converter class. + * File contains Contact_AddressBook_Builder class. * * PHP versions 4 and 5 * @@ -42,64 +42,55 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License - * @version CVS: $Id: Builder.php,v 1.2 2005/03/27 19:25:38 firman Exp $ + * @version CVS: $Id: Builder.php,v 1.3 2005/08/16 20:43:01 firman Exp $ */ // }}} // {{{ Dependencies /** - * Require File for handling files. + * Load File for handling files. */ require_once 'File.php'; /** - * Require Net_UserAgent_Detect for send the right header. + * Load Net_UserAgent_Detect for send the right header. */ require_once "Net/UserAgent/Detect.php"; // }}} -// {{{ Class: Contact_AddressBook_Converter +// {{{ Class: Contact_AddressBook_Builder /** - * Base class for build external formatted structure that may save to a file. + * Abstract class for building address book. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ + * @abstract */ class Contact_AddressBook_Builder { // {{{ Properties /** - * Options. - * - * @var array - * @access protected - */ - var $options = array( - 'line_break' => "\n" - ); - - /** * Cache for data. * * @var array * @access protected */ var $data = array(); - + /** * Cache for result. * @@ -108,91 +99,29 @@ */ var $result = ''; - // }}} - // {{{ Constructor - /** - * PHP 4 compatible constructor. + * MIME Type when send the output to the browser. * - * @param array $data (optional) Data. - * @param array $options (optional) Options. - * - * @access public - */ - function Contact_AddressBook_Builder($data = null, $options = null) - { - $this->__construct($data, $options); - } - - /** - * PHP 5 compatible constructor. - * - * @param array $data (optional) Data. - * @param array $options (optional) Options. - * - * @access public + * @var string + * @access protected */ - function __construct($data = null, $options = null) - { - if ($data !== null && is_array($data)) { - $this->setData($data); - } - - if ($options !== null) { - $this->set($options); - } - } - - // }}} - // {{{ __toString() + var $mime = 'text/plain'; /** - * PHP5 __toString magic method. + * File extension. * - * @return string - * @access public + * @var string + * @access protected */ - function __toString() - { - return var_export($this, true); - } + var $extension = 'txt'; // }}} - // {{{ set() + // {{{ Constructor - /** - * Set the option(s). - * - * Set a single option or multiple options. - * - * @param mixed $option A string with option name as value for - * single option or An associative array contains - * options array('<option>' => <value>) for multiple - * options. - * @param mixed $value (optional) Option value. Require when $option is - * string (single option mode). - * - * @return bool|PEAR_Error TRUE on success, PEAR_Error on failure. - * @access public - */ - function set($option, $value = null) + function Contact_AddressBook_Builder($data = null) { - if (is_array($option)) { - foreach ($option as $key => $val) { - $err = $this->set($key, $val); - if (PEAR::isError($err)) { - return $err; - } - } - } elseif (is_string($option)) { - if (isset($this->options[$option])) { - if (gettype($this->options[$option]) == gettype($value)) { - $this->options[$option] = $value; - } else { - return PEAR::raiseError('Type mismatch for option '. - "'$option' value"); - } - } + if (is_array($data)) { + $this->setData($data); } } @@ -210,7 +139,7 @@ function setData($data) { if (!is_array($data)) { - return PEAR::raiseError('Type mismatch for map only array allowed'); + return PEAR::raiseError('Type mismatch for data, only array allowed'); } $this->data = $data; @@ -228,7 +157,7 @@ */ function build() { - return PEAR::raiseError('unsupported'); + return PEAR::raiseError('Not implemented'); } // }}} @@ -241,7 +170,7 @@ */ function show() { - print $this->result; + print htmlspecialchars($this->result); } // }}} @@ -257,6 +186,7 @@ */ function save($filename) { + $filename .= '.' . $this->extension; return File::write($filename, $this->result, FILE_MODE_WRITE); } @@ -266,43 +196,17 @@ /** * Download the result via browser. * - * @param string $filename (optional) Filename. + * @param string $filename Filename. * * @access public */ - function download($filename = null) + function download($filename) { - if ($filename === null) { - $filename = 'addressbook'; - - // Add specific extension depending classname. - if (substr(get_class($this), 28, 3) == 'csv') { - $filename .= '.csv'; - } elseif (substr(get_class($this), 28, 4) == 'ldif') { - $filename .= '.ldif'; - } else { - $filename .= '.txt'; - } - } - - $extension = ''; - if (($pos = strrpos($filename, '.')) !== false) { - $extension = substr($filename, $pos + 1); - } - - switch (strtolower($extension)) { - case 'csv': - $mimetype = 'text/x-csv'; - break; - default: - $mimetype = 'text/plain'; - break; - } - if (headers_sent()) { return PEAR::raiseError('Cannot process headers, headers already sent'); } + $filename .= '.' . $this->extension; if (Net_UserAgent_Detect::isIE()) { // IE need specific headers @@ -316,12 +220,29 @@ header('Pragma: no-cache'); } - header('Content-Type: ' . $mimetype); - print $this->result; + header('Content-Type: ' . $this->mime); + print $this->show(); exit; } // }}} + // {{{ stripLineBreak() + + /** + * Strip any line break characters then replace it with space. + * + * @param string $str Value. + * + * @return string Result string. + * @access public + * @static + */ + function stripLineBreak($str) + { + return preg_replace('/\r\n|\r|\n/', ' ', $str); + } + + // }}} } // }}} http://cvs.php.net/diff.php/pear/Contact_AddressBook/Contact/AddressBook/CSV.php?r1=1.1&r2=1.2&ty=u Index: pear/Contact_AddressBook/Contact/AddressBook/CSV.php diff -u pear/Contact_AddressBook/Contact/AddressBook/CSV.php:1.1 pear/Contact_AddressBook/Contact/AddressBook/CSV.php:1.2 --- pear/Contact_AddressBook/Contact/AddressBook/CSV.php:1.1 Sun Mar 27 14:36:39 2005 +++ pear/Contact_AddressBook/Contact/AddressBook/CSV.php Tue Aug 16 16:43:01 2005 @@ -4,17 +4,15 @@ // {{{ Header /** - * $ShortFileDescription$ + * File contains Contact_AddressBook_CSV class. * - * $LongFileDescription$ - * - * PHP version $PHPVersion$ + * PHP versions 4 and 5 * * LICENSE: * * BSD License * - * Copyright (c) $CopyrightYear$ Firman Wandayandi + * Copyright (c) 2004-2005 Firman Wandayandi * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -44,206 +42,147 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * @category $CategoryName$ - * @package $PackageName$ + * @category File Formats + * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> - * @copyright Copyright (c) $CopyrightYear$ Firman Wandayandi + * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License - * @version CVS: $Id: CSV.php,v 1.1 2005/03/27 19:36:39 firman Exp $ - * @since File available since Release 0.2.0dev1 + * @version CVS: $Id: CSV.php,v 1.2 2005/08/16 20:43:01 firman Exp $ + * @since File available since Release 0.4.0alpha1 */ // }}} -// {{{ Dependencies - -/** - * Load PEAR for error handling. - */ -require_once 'PEAR.php'; - -// }}} // {{{ Global Variables /** * CSV Formats informations. * - * @global array $GLOBALS['Contact_AddressBook_CSV_configs'] - * @name $Contact_AddressBook_CSV_configs + * @global array $GLOBALS['_Contact_AddressBook_CSV_configs'] + * @name $_Contact_AddressBook_CSV_configs */ -$GLOBALS['Contact_AddressBook_CSV_configs'] = array( - 'wab' => 'Contact/AddressBook/CSV/Config/WAB.php', - 'outlookexpress' => 'Contact/AddressBook/CSV/Config/WAB.php', - 'outlook' => 'Contact/AddressBook/CSV/Config/Outlook.php', - 'mozilla' => 'Contact/AddressBook/CSV/Config/Mozilla.php', - 'nestcape' => 'Contact/AddressBook/CSV/Config/Mozilla.php', - 'thunderbird' => 'Contact/AddressBook/CSV/Config/Mozilla.php', - 'yahoo' => 'Contact/AddressBook/CSV/Config/Yahoo.php', - 'palm' => 'Contact/AddressBook/CSV/Config/Palm.php' +$GLOBALS['_Contact_AddressBook_CSV_configs'] = array( + 'wab' => 'WAB', + 'outlookexpress' => 'WAB', + 'outlook' => 'Outlook', + 'mozilla' => 'Mozilla', + 'nestcape' => 'Mozilla', + 'thunderbird' => 'Mozilla', + 'yahoo' => 'Yahoo', + 'palm' => 'Palm', + 'kmail' => 'KMail' ); /** * CSV Formats informations. * - * @global array $GLOBALS['Contact_AddressBook_CSV_headers'] - * @name $Contact_AddressBook_CSV_headers + * @global array $GLOBALS['_Contact_AddressBook_CSV_headers'] + * @name $_Contact_AddressBook_CSV_headers */ -$GLOBALS['Contact_AddressBook_CSV_headers'] = array( +$GLOBALS['_Contact_AddressBook_CSV_headers'] = array( 'wab' => 'WAB', 'outlookexpress' => 'WAB', 'outlook' => 'Outlook', - 'yahoo' => 'Yahoo' -); - -/** - * Field definitions. - * - * @global array $GLOBALS['Contact_AddressBook_CSV_definitions'] - * @name $Contact_AddressBook_CSV_definitions - */ -$GLOBALS['Contact_AddressBook_CSV_definitions'] = array( - 'wab' => 'WAB.def', - 'outlookexpress' => 'WAB.def', - 'outlook' => 'Outlook.def', - 'mozilla' => 'Mozilla.def', - 'nestcape' => 'Mozilla.def', - 'thunderbird' => 'Mozilla.def', - 'yahoo' => 'Yahoo.def', - 'palm' => 'Palm.def' + 'yahoo' => 'Yahoo', + 'kmail' => 'KMail' ); // }}} -// {{{ Class: $ClassName$ +// {{{ Class: Contact_AddressBook_CSV /** - * $ShortClassDescription$ + * Global class for CSV class. * - * $LongClassDescription$ - * - * @category $CategoryName$ - * @package $PackageName$ + * @category File Formats + * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> - * @copyright Copyright (c) $CopyrightYear$ Firman Wandayandi + * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ - * @since Class available since Release 0.2.0dev1 + * @since Class available since Release 0.4.0alpha1 */ class Contact_AddressBook_CSV { - // {{{ loadConfig() + // {{{ getConfig() /** - * Load CSV file format configuration for the specified CSV address book. + * Load CSV file format configuration for the specified CSV address book, + * then return it. * - * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. - * @access protected + * @param string $format Address book format. + * + * @return array|PEAR_Error An array of config on success or + * PEAR_Error on failure. + * @access public + * @static */ - function loadConfig($format) + function getConfig($format) { - if (!isset($GLOBALS['Contact_AddressBook_CSV_configs'][$format])) - { - return PEAR::raiseError('Uknown format \'' . $format .'\''); + if (!isset($GLOBALS['_Contact_AddressBook_CSV_configs'][$format])) { + return PEAR::raiseError('No such config for \'' . $format .'\''); } - include_once $GLOBALS['Contact_AddressBook_CSV_configs'][$format]; - if (!isset($GLOBALS['Contact_AddressBook_CSV_config'])) - { - return PEAR::raiseError('Undefined configuration'); - } + $dir = Contact_AddressBook::getDataDir() . '/CSV/Config/'; - return $GLOBALS['Contact_AddressBook_CSV_config']; - } + $file = $dir . + $GLOBALS['_Contact_AddressBook_CSV_configs'][$format] . '.php'; + include_once $file; - // }}} - // {{{ getConfig() + if (!isset($GLOBALS['_Contact_AddressBook_CSV_config'])) + { + return PEAR::raiseError('Undefined config for \'' . $format .'\''); + } - function getConfig($format) - { $default = array( 'quote' => '"', 'linebreak' => 'auto', 'selectablefields' => false ); - $config = Contact_AddressBook_CSV::loadConfig($format); - if (PEAR::isError($config)) - { - return $config; - } - - return array_merge($default, $config); - } - - // }}} - // {{{ getDefaultDefinitionFile() - - function getDefaultDefinitionFile($format) - { - if (!isset($GLOBALS['Contact_AddressBook_CSV_definitions'][$format])) - { - return PEAR::raiseError('Uknown format \'' . $format .'\''); - } - - include_once 'PEAR/Config.php'; - $config = new PEAR_Config; - $dir = $config->get('data_dir') . - '/Contact_AddressBook/definitions/CSV/'; - $file = $dir . $GLOBALS['Contact_AddressBook_CSV_definitions'][$format]; - return $file; + return array_merge($default, + $GLOBALS['_Contact_AddressBook_CSV_config']); } // }}} - // {{{ loadDefinition() - - function loadDefinition($file) - { - ini_set('track_error', true); - $res = parse_ini_file($file); - if (empty($res)) - { - return PEAR::raiseError($php_errormsg); - } - - return $res; - } - - // }}} - // {{{ loadHeader() + // {{{ getHeader() /** - * Load CSV file format configuration for the specified CSV address book. + * Load CSV file format configuration for the specified CSV address book, + * then return it. + * + * @param string $format Address book format. + * @param string $language (optional) Address book languange + * (use language code), default is "en". * - * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. - * @access protected + * @return array|PEAR_Error An array of header on success or + * PEAR_Error on failure. + * @access public + * @static */ - function loadHeader($format, $languange = null) + function getHeader($format, $language = null) { - if (!isset($GLOBALS['Contact_AddressBook_CSV_headers'][$format])) - { - return PEAR::raiseError('Uknown format \'' . $format .'\''); + if (!isset($GLOBALS['_Contact_AddressBook_CSV_headers'][$format])) { + return PEAR::raiseError('No such header for \'' . $format .'\''); } - if ($languange !== null) - { - $languange = 'en'; - } - else - { - $languange = strtolower($languange); + if ($language === null) { + $language = 'en'; + } else { + $language = strtolower($language); } - $dir = 'Contact/AddressBook/CSV/Header/'; - $file = $GLOBALS['Contact_AddressBook_CSV_headers'][$format] . '/' . - $languange . '.php'; + $dir = Contact_AddressBook::getDataDir() . '/CSV/Header/'; + $file = $dir . + $GLOBALS['_Contact_AddressBook_CSV_headers'][$format] . '/' . + $language . '.php'; + include_once $file; - include_once $dir . $file; - if (!isset($GLOBALS['Contact_AddressBook_CSV_header'])) - { - return PEAR::raiseError('Undefined header'); + if (!isset($GLOBALS['_Contact_AddressBook_CSV_header'])) { + return PEAR::raiseError('Undefined header for \'' . $format .'\''); } - return $GLOBALS['Contact_AddressBook_CSV_header']; + return $GLOBALS['_Contact_AddressBook_CSV_header']; } // }}} @@ -251,8 +190,6 @@ // }}} -print_r(Contact_AddressBook_CSV::loadHeader('yahoo', 'en')); - /* * Local variables: * mode: php http://cvs.php.net/diff.php/pear/Contact_AddressBook/Contact/AddressBook/Converter.php?r1=1.2&r2=1.3&ty=u Index: pear/Contact_AddressBook/Contact/AddressBook/Converter.php diff -u pear/Contact_AddressBook/Contact/AddressBook/Converter.php:1.2 pear/Contact_AddressBook/Contact/AddressBook/Converter.php:1.3 --- pear/Contact_AddressBook/Contact/AddressBook/Converter.php:1.2 Sun Mar 27 14:28:59 2005 +++ pear/Contact_AddressBook/Contact/AddressBook/Converter.php Tue Aug 16 16:43:01 2005 @@ -42,22 +42,36 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License - * @version CVS: $Id: Converter.php,v 1.2 2005/03/27 19:28:59 firman Exp $ + * @version CVS: $Id: Converter.php,v 1.3 2005/08/16 20:43:01 firman Exp $ */ // }}} +// {{{ Global Variables + +/** + * Definition aliases. + * + * @global array $GLOBALS['_Contact_AddressBook_definitions'] + * @name $_Contact_AddressBook_definitions + * @ignore + */ +$GLOBALS['_Contact_AddressBook_definitions'] = array( + 'eudora' => 'Eudora' +); + +// }}} // {{{ Class: Contact_AddressBook_Converter /** * Class for handling address book data structure converting. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi @@ -69,7 +83,13 @@ { // {{{ Properties - var $definitionFile = ''; + /** + * Directory contains definition files. + * + * @var string + * @access protected + */ + var $definitionDir = ''; /** * Address Book fields definition. @@ -103,79 +123,54 @@ */ var $isPrepared = false; - var $format = ''; - var $languange = 'en'; - // }}} // {{{ Constructor - /** - * PHP4 Compatible contructor. - * - * @param string $definitionFile Definition filename. - * - * @access public - * @see __construct() - */ - function Contact_AddressBook_Converter($format, $languange = null) - { - $this->__construct($format, $languange); - } - - /** - * PHP5 Compatible contructor. - * - * @param string $definitionFile Definition filename. - * - * @access public - */ - function __construct($format, $language = null) + function Contact_AddressBook_Converter() { - $this->format = $format; - if ($language !== null) - { - $this->languange = $language; - } + $this->setDefinitionDir(Contact_AddressBook::getDataDir() . '/Defs/'); } // }}} - // {{{ setDefinitionFile() + // {{{ setDefinitionDir() - function setDefinitionFile($file) + function setDefinitionDir($dir) { - $this->definitionFile = $file; - return $this->loadDefinition(); + $this->definitionDir = $dir; } // }}} - // {{{ loadDefinitionFile() + // {{{ setFormat() /** - * Load the definition file. - * The bundle default definition files was placed at PEAR data dir. + * Set the address book format to convert. * - * @param string $file Definition filename. + * @param string $format The address book format. * - * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. + * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. * @access public + * + * @see Contact_AddressBook_Converter::setDefinitionFile() */ - function loadDefinition() + function setFormat($format) { - $res = Contact_AddressBook_CSV::loadDefinition($this->definitionFile); - if (empty($res)) - { - return PEAR::raiseError($php_errormsg); + $format = strtolower($format); + + if (!isset($GLOBALS['_Contact_AddressBook_definitions'][$format])) { + return PEAR::raiseError('No such definition for \'' . $format .'\''); } - $this->setDefinition($res); - return true; + $file = $this->definitionDir . + $GLOBALS['_Contact_AddressBook_definitions'][$format] . '.def'; + + return $this->setDefinitionFile($file); } // }}} // {{{ setDefinition() /** - * Set custom definition on runtime. + * Set converting definition. * * @param array $definition An array of definition structure. * @@ -193,16 +188,49 @@ // Set isPrepared flag to false, we need to reset the maps with new // definition. - if ($this->isPrepared) - { - $this->isPrepared = false; + if ($this->isPrepared) { + $this->isPrepare = false; + } + + return true; + } + + // }}} + // {{{ setDefinitionFile() + + /** + * Load the definition file. + * The bundle default definition files was placed at PEAR data dir. + * + * @param string $file Definition filename. + * + * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. + * @access public + * + * @see Contact_AddressBook_Converter::setDefinition() + */ + function setDefinitionFile($file) + { + if (!is_file($file) || !file_exists($file)) { + return PEAR::raiseError('No such file \'' . $file . '\''); + } + + ini_set('track_error', true); + $res = @parse_ini_file($file); + if (empty($res)) { + return PEAR::raiseError($php_errormsg); + } + + $res = $this->setDefinition($res); + if (PEAR::isError($res)) { + return $res; } return true; } // }}} - // {{{ _prepare() + // {{{ prepare() /** * Prepare map for converting. @@ -210,17 +238,10 @@ * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. * @access private */ - function _prepare() + function prepare() { if (empty($this->definition)) { - $res = $this->setDefinitionFile( - Contact_AddressBook_CSV::getDefaultDefinitionFile($this->format) - ); - - if (PEAR::isError($res)) - { - return $res; - } + return PEAR::raiseError('Definition never set'); } $this->mapFrom =& $this->definition; @@ -286,7 +307,10 @@ function convertTo($data) { if (!$this->isPrepared) { - $this->_prepare(); + $res = $this->prepare(); + if (PEAR::isError($res)) { + return $res; + } } return $this->performConvert($this->mapTo, $data); @@ -308,14 +332,16 @@ function convertFrom($data) { if (!$this->isPrepared) { - $this->_prepare(); + $res = $this->prepare(); + if (PEAR::isError($res)) { + return $res; + } } return $this->performConvert($this->mapFrom, $data); } // }}} - } // }}} http://cvs.php.net/diff.php/pear/Contact_AddressBook/Contact/AddressBook/Parser.php?r1=1.2&r2=1.3&ty=u Index: pear/Contact_AddressBook/Contact/AddressBook/Parser.php diff -u pear/Contact_AddressBook/Contact/AddressBook/Parser.php:1.2 pear/Contact_AddressBook/Contact/AddressBook/Parser.php:1.3 --- pear/Contact_AddressBook/Contact/AddressBook/Parser.php:1.2 Sun Mar 27 14:34:17 2005 +++ pear/Contact_AddressBook/Contact/AddressBook/Parser.php Tue Aug 16 16:43:01 2005 @@ -4,7 +4,7 @@ // {{{ Header /** - * File contains Contact_AddressBook_CSV_Parser class. + * File contains Contact_AddressBook_Parser class. * * PHP versions 4 and 5 * @@ -42,231 +42,158 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License - * @version CVS: $Id: Parser.php,v 1.2 2005/03/27 19:34:17 firman Exp $ - * @since File available since Release 0.2.0dev1 + * @version CVS: $Id: Parser.php,v 1.3 2005/08/16 20:43:01 firman Exp $ */ // }}} // {{{ Dependencies /** - * Load Contact_AddressBook_Parser as the base class. + * Require File for handling files. */ -require_once 'Contact/AddressBook/Parser.php'; - -/** - * Load Contact_AddressBook_CSV for common functions. - */ -require_once 'Contact/AddressBook/CSV.php'; - -/** - * Load File_CSV for handling CSV parser. - */ -require_once 'File/CSV.php'; +require_once 'File.php'; // }}} -// {{{ Class: Contact_AddressBook_CSV_Parser +// {{{ Class: Contact_AddressBook_Parser /** - * Class for parse CSV address book format. + * Base class for Contact_AddressBook parser classes. * - * @category FileFormats + * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ - * @see File_CSV - * @since Class available since Release 0.2.0dev1 */ -class Contact_AddressBook_CSV_Parser extends Contact_AddressBook_Parser +class Contact_AddressBook_Parser { // {{{ Properties /** - * Configuration mapping to File_CSV. + * Parse result. * * @var array - * @access private + * @access protected + */ + var $result = array(); + + /** + * File to parse. + * + * @var string + * @access protected */ - var $_configMap = array( - 'fields' => 'fields', - 'quote' => 'quote', - 'header' => 'header', - 'crlf' => 'linebreak' - ); + var $file = ''; // }}} - // {{{ Constructor + // {{{ setFile() /** - * PHP4 compatible contructor. + * Set the input file to parse. * - * @param string $format CSV address book format. One of supported formats - * WAB, OutlookExpress, Outlook, Mozilla, Netscape, - * Thunderbird, Palm, Yahoo (case-insensitive and - * only alphanumeric letters). + * @param string $file Input filename. * + * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. * @access public - * @see __contruct() */ - function Contact_AddressBook_CSV_Parser($format) + function setFile($file) { - $this->__construct($format); + if (!file_exists($file)) { + return PEAR::raiseError('No such file \'' . $file . '\''); + } + + $this->file = $file; + return true; } + // }}} + // {{{ parse() + /** - * PHP5 compatible contructor. - * - * @param string $format CSV address book format. One of supported formats - * WAB, OutlookExpress, Outlook, Mozilla, Netscape, - * Thunderbird, Palm, Yahoo (case-insensitive and - * only alphanumeric letters). + * Parse input file to gets address book data. * + * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. * @access public - * @see __contruct() */ - function __construct($format) + function parse() { - $this->_format = strtolower($format); + return PEAR::raiseError('Not implemented'); } // }}} - // {{{ _parseConfig() + // {{{ getResult() /** - * Parse configuration for use in File_CSV. + * Get parse result data. * - * @access private - * @return array An array of configuration. + * @return array The result array. + * @access public */ - function _parseConfig() + function getResult() { - $sysconfig = Contact_AddressBook_CSV::getConfig($this->_format); - if (PEAR::isError($sysconfig)) - { - return $sysconfig; - } - - $config = array( - 'sep' => ',' - ); - - foreach ($this->_configMap as $key => $name) { - if (isset($sysconfig[$name])) { - if ($name == 'linebreak' && $sysconfig[$name] == 'auto') { - continue; - } - - $config[$key] = $sysconfig[$name]; - } - } - return $config; + return $this->result; } // }}} - // {{{ parse() + // {{{ numRows() /** - * Parse input file to gets address book data. + * Returns the number of rows in a result. * - * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. + * @return int The number of rows. * @access public */ - function parse() + function numRows() { - // File_CSV detected config. - $config = File_CSV::discoverFormat($this->file); - if (PEAR::isError($config)) - { - return $config; - } - - // Contact_AddressBook default CSV config. - $sysconfig = Contact_AddressBook_CSV::getConfig($this->_format); - if (PEAR::isError($sysconfig)) - { - return $sysconfig; - } - - // File_CSV config. - $csvconfig = $this->_parseConfig(); - if (PEAR::isError($csvconfig)) - { - return $csvconfig; - } - - // Force override fields count with system configuration, there's - // some bugs in File_CSV::discoverFormat(). - if (isset($csvconfig['fields'])) - { - unset($config['fields']); - } - - $config = array_merge($config, $csvconfig); - - if (isset($sysconfig['selectablefields']) && - $sysconfig['selectablefields']) - { - $config['header'] = false; - } - - $data = array(); - while ($res = File_CSV::readQuoted($this->file, $config)) { - $data[] = $res; - } - - if (!empty($data)) { - // Finalize header for address book selectable fields such - // WAB instead of Outlook Express. - if (isset($sysconfig['selectablefields']) && - $sysconfig['selectablefields']) - { - $data = Contact_AddressBook_CSV_Parser::finalizeHeader($data); - } + return count($this->result); + } - $this->result = $data; - } + // }}} + // {{{ reset() - return true; + /** + * Reset the parser. + * This method set the result into empty array. + * + * @access public + */ + function reset() + { + $this->result = array(); } // }}} - // {{{ finalizeHeader() + // {{{ getFileContents() /** - * Change the array of data from sequential array into associative array, - * using header elements as keys. + * Read the input file to gets file contents. * - * This function only called if configuration 'selectablefields' is TRUE. - * - * @return array() An associative array of result. - * @access public + * @return string|PEAR_Error String file contents on succeed or + * PEAR_Error on failure. + * @access protected + * @see File::read() * @static */ - function finalizeHeader($data) + function getFileContents() { - $header = array_shift($data); - $res = array(); - if (!is_array($header)) { - return $data; - } - - foreach ($header as $i => $field) { - foreach ($data as $j => $values) { - if (isset($values[$i])) { - $res[$j][$field] = $values[$i]; - } + $contents = ''; + while($res = File::read($this->file)) { + if (PEAR::isError($res)) { + return $res; } + + $contents .= $res; } - return $res; + + return $contents; } // }}} @@ -274,16 +201,6 @@ // }}} -$obj = new Contact_AddressBook_CSV_Parser('wab'); -$obj->setFile('files/WAB-selectable.csv'); -$obj->parse(); -$data = $obj->getResult(); - -require_once 'Contact/AddressBook/CSV/Converter.php'; -$conv = new Contact_AddressBook_CSV_Converter('wab'); -$res = $conv->convertFrom($data); -print_r($res); - /* * Local variables: * mode: php http://cvs.php.net/co.php/pear/Contact_AddressBook/Contact/AddressBook/Builder/CSV.php?r=1.1&p=1 Index: pear/Contact_AddressBook/Contact/AddressBook/Builder/CSV.php +++ pear/Contact_AddressBook/Contact/AddressBook/Builder/CSV.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ // {{{ Header /** * File contains Contact_AddressBook_Builder_CSV class. * * PHP versions 4 and 5 * * LICENSE: * * BSD License * * Copyright (c) 2004-2005 Firman Wandayandi * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * 3. Neither the name of Firman Wandayandi nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version CVS: $Id: CSV.php,v 1.1 2005/08/16 20:43:01 firman Exp $ */ // }}} // {{{ Dependencies /** * Load Contact_AddressBook_Builder as the base class. */ require_once 'Contact/AddressBook/Builder.php'; // }}} // {{{ Class: Contact_AddressBook_Builder_CSV /** * Class for building CSV address book format. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ */ class Contact_AddressBook_Builder_CSV extends Contact_AddressBook_Builder { // {{{ Properties /** * MIME Type when send the output to the browser. * * @var string * @access protected */ var $mime = 'text/x-csv'; /** * File extension. * * @var string * @access protected */ var $extension = 'csv'; /** * CSV address book format. * * @var string * @access private */ var $_format = ''; /** * Header language, this header use only when the address book format * show the header. * * @var string * @access private */ var $_language = 'en'; // }}} // {{{ setFormat() /** * Set the address book format. * * @param string $format Address book format. * * @access public */ function setFormat($format) { $this->_format = $format; } // }}} // {{{ setLanguage() /** * Set the language, only use address book show the header. * * @param string $language Language. * * @access public */ function setLanguage($language) { $this->_language = $language; } // }}} // {{{ build() /** * Build the structure format. * * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. * @access public */ function build() { $config = Contact_AddressBook_CSV::getConfig($this->_format); if (PEAR::isError($config)) { return $config; } if (isset($config['header']) && $config['header'] == true) { $header = Contact_AddressBook_CSV::getHeader($this->_format, $this->_language); if (PEAR::isError($header)) { return $header; } // Insert the header into data. array_unshift($this->data, $header); } $escapes = array(',', "\r", "\n", "\r\n", $config['quote']); // Fill values with empty, to deal with csv rule // (same fields count for all fields). $default = array_fill(0, $config['fields'], ''); $this->result = ''; foreach ($this->data as $record) { $record = $record + $default; ksort($record, SORT_NUMERIC); if (isset($config['quoted_field']) && $config['quoted_field'] == true) { $this->result .= $config['quote'] . implode($config['quote'] . ',' . $config['quote'], $record) . $config['quote']; } else { foreach ($record as $key => $value) { // Replace any line break if multilines support doesn't capable. if (!isset($config['multilines']) || $config['multilines'] === false) { $value = Contact_AddressBook_Builder::stripLineBreak($value); } // Quote any escape characters values. if (preg_match('/[' . implode($escapes) . ']+/', $value)) { $value = '"' . $value . '"'; } $record[$key] = $value; } $this->result .= implode(',', $record); } $this->result .= ($config['line_break'] == 'auto') ? PHP_EOL : $config['line_break']; } } // }}} } // }}} /* * Local variables: * mode: php * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/Contact_AddressBook/Contact/AddressBook/Builder/Eudora.php?r=1.1&p=1 Index: pear/Contact_AddressBook/Contact/AddressBook/Builder/Eudora.php +++ pear/Contact_AddressBook/Contact/AddressBook/Builder/Eudora.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ // {{{ Header /** * File contains Contact_AddressBook_Builder_Eudora class. * * PHP versions 4 and 5 * * LICENSE: * * BSD License * * Copyright (c) 2004-2005 Firman Wandayandi * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * 3. Neither the name of Firman Wandayandi nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version CVS: $Id: Eudora.php,v 1.1 2005/08/16 20:43:01 firman Exp $ */ // }}} // {{{ Dependencies /** * Load Contact_AddressBook_Builder as the base class. */ require_once 'Contact/AddressBook/Builder.php'; // }}} // {{{ Class: Contact_AddressBook_Builder_Eudora /** * Class for building the Eudora address book. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ */ class Contact_AddressBook_Builder_Eudora extends Contact_AddressBook_Builder { // {{{ build() /** * Build the structure format. * * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. * @access public */ function build() { $this->result = ''; foreach ($this->data as $record) { $alias = isset($record['nickname']) ? $record['nickname'] : ''; $this->result = 'alias ' . $alias . ' ' . $record['email'] . PHP_EOL; $this->result .= 'note ' . $alias . ' '; if (isset($record['nickname'])) { unset($record['nickname']); } foreach ($record as $field => $value) { if ($value == '') { continue; } if ($field == 'notes') { $this->result .= Contact_AddressBook_Builder::stripLineBreak($value); } else { $this->result .= '<' . $field . ':' . Contact_AddressBook_Builder::stripLineBreak($value) . '>'; } } $this->result .= PHP_EOL; } return true; } // }}} } // }}} /* * Local variables: * mode: php * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/Contact_AddressBook/Contact/AddressBook/Parser/CSV.php?r=1.1&p=1 Index: pear/Contact_AddressBook/Contact/AddressBook/Parser/CSV.php +++ pear/Contact_AddressBook/Contact/AddressBook/Parser/CSV.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ // {{{ Header /** * File contains Contact_AddressBook_Parser_CSV class. * * PHP versions 4 and 5 * * LICENSE: * * BSD License * * Copyright (c) 2004-2005 Firman Wandayandi * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * 3. Neither the name of Firman Wandayandi nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version CVS: $Id: CSV.php,v 1.1 2005/08/16 20:43:02 firman Exp $ */ // }}} // {{{ Dependencies /** * Load Contact_AddressBook_Parser as the base class. */ require_once 'Contact/AddressBook/Parser.php'; /** * Load Contact_AddressBook_CSV for common functions. */ require_once 'Contact/AddressBook/CSV.php'; /** * Load File_CSV for handling CSV parser. */ require_once 'File/CSV.php'; // }}} // {{{ Class: Contact_AddressBook_Parser_CSV /** * Class for parse CSV address book format. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ * @see File_CSV */ class Contact_AddressBook_Parser_CSV extends Contact_AddressBook_Parser { // {{{ Properties /** * Configuration mapping to File_CSV. * * @var array * @access private */ var $_configMap = array( 'fields' => 'fields', 'quote' => 'quote', 'header' => 'header', 'crlf' => 'line_break' ); // }}} // {{{ Constructor /** * Constructor. * * @param string $format CSV address book format. One of supported formats * WAB, OutlookExpress, Outlook, Mozilla, Netscape, * Thunderbird, Palm, Yahoo (case-insensitive and * only alphanumeric letters). * * @access public * @see Contact_AddressBook_Parser_CSV::setFormat() *//* function Contact_AddressBook_Parser_CSV($format) { $this->setFormat($format); } */ // }}} // {{{ setFormat() /** * Set the address book format. * * @param string $format CSV address book format. One of supported formats * WAB, OutlookExpress, Outlook, Mozilla, Netscape, * Thunderbird, Palm, Yahoo (case-insensitive and * only alphanumeric letters). * * @access public */ function setFormat($format) { $this->_format = strtolower($format); } // }}} // {{{ _parseConfig() /** * Parse configuration for File_CSV usage. * * @access private * @return array An array of configuration. */ function _parseConfig($sysconfig) { if (PEAR::isError($sysconfig)) { return $sysconfig; } // Separator are the commas. $config = array( 'sep' => ',' ); foreach ($this->_configMap as $key => $name) { if (isset($sysconfig[$name])) { // Ignore when line break set to "auto". if ($name == 'line_break' && $sysconfig[$name] == 'auto') { continue; } $config[$key] = $sysconfig[$name]; } } return $config; } // }}} // {{{ parse() /** * Parse input file to gets address book data. * * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. * @access public * * @see File_CSV::discoverFormat() * @see Contact_AddressBook_CSV::getConfig() * @see Contact_AddressBook_Parser_CSV::_parseConfig() * @see Contact_AddressBook_Parser_CSV::finalizeHeader() * @see File_CSV::readQuoted() */ function parse() { // Attempt to discover config format by File_CSV. $config = File_CSV::discoverFormat($this->file); if (PEAR::isError($config)) { return $config; } // Get Contact_AddressBook default CSV config. $sysconfig = Contact_AddressBook_CSV::getConfig($this->_format); if (PEAR::isError($sysconfig)) { return $sysconfig; } // Synchronize the config for File_CSV usage. $csvconfig = $this->_parseConfig($sysconfig); if (PEAR::isError($csvconfig)) { return $csvconfig; } // Force override fields count with system configuration, there's // some bugs in File_CSV::discoverFormat(). if (isset($csvconfig['fields'])) { unset($config['fields']); } $config = array_merge($config, $csvconfig); // Set the header into FALSE if selectablefields TRUE, // so File_CSV will be read this the header as a record. // This header required when finalizing and will be removed // automatically. if (isset($sysconfig['selectable_fields']) && $sysconfig['selectable_fields']) { $config['header'] = false; } $data = array(); while ($res = File_CSV::readQuoted($this->file, $config)) { $data[] = $res; } if (!empty($data)) { // Finalize header for address book selectable fields such // WAB instead of Outlook Express. if (isset($sysconfig['selectable_fields']) && $sysconfig['selectable_fields']) { $data = Contact_AddressBook_Parser_CSV::finalizeHeader($data); } $this->result = $data; } return true; } // }}} // {{{ finalizeHeader() /** * Change the array of data from sequential array into associative array, * using header elements as keys. * * This function only called if configuration 'selectablefields' is TRUE. * * @param array $data The return value from * Contact_AddressBook_Parser_CSV::parse() method. * * @return array() An associative array of result. * @access public * @static */ function finalizeHeader($data) { $header = array_shift($data); $res = array(); if (!is_array($header)) { return $data; } foreach ($header as $i => $field) { foreach ($data as $j => $values) { if (isset($values[$i])) { $res[$j][$field] = $values[$i]; } } } return $res; } // }}} // {{{ getResult() /** * Get the parse result. * * @return array An array of address book parse result. * @access public */ function getResult() { return $this->result; } // }}} } // }}} /* * Local variables: * mode: php * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/Contact_AddressBook/Contact/AddressBook/Parser/Eudora.php?r=1.1&p=1 Index: pear/Contact_AddressBook/Contact/AddressBook/Parser/Eudora.php +++ pear/Contact_AddressBook/Contact/AddressBook/Parser/Eudora.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ // {{{ Header /** * File contains Contact_AddressBook_Parser_Eudora class. * * PHP versions 4 and 5 * * LICENSE: * * BSD License * * Copyright (c) 2004-2005 Firman Wandayandi * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * 3. Neither the name of Firman Wandayandi nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version CVS: $Id: Eudora.php,v 1.1 2005/08/16 20:43:02 firman Exp $ */ // }}} // {{{ Dependencies /** * Load Contact_AddressBook_Parser as the base class. */ require_once 'Contact/AddressBook/Parser.php'; // }}} // {{{ Class: Contact_AddressBook_Parser_Eudora /** * Class for handling Eudora address book format parse. * * @category File Formats * @package Contact_AddressBook * @author Firman Wandayandi <firman@php.net> * @copyright Copyright (c) 2004-2005 Firman Wandayandi * @license http://www.opensource.org/licenses/bsd-license.php * BSD License * @version Release: @package_version@ */ class Contact_AddressBook_Parser_Eudora extends Contact_AddressBook_Parser { // {{{ parse() /** * Parse input file to gets address book data. * * @return bool|PEAR_Error TRUE on succeed or PEAR_Error on failure. * @access public */ function parse() { $contents = Contact_AddressBook_Parser::getFileContents(); if ($contents == '') { return PEAR::raiseError("File '{$this->file}' is empty"); } $data = array(); // Grab all alias keyword. if (preg_match_all('/alias\s+(\w+)\s*(.*)\n/', $contents, $matches, PREG_SET_ORDER)) { $i = 0; foreach ($matches as $raw) { $emails = explode(',', $raw[2]); $data[$i]['nickname'] = $raw[1]; foreach ($emails as $j => $email) { if ($j == 0) { $data[$i]['email'] = $email; } else { $data[$i]['email' . ($j + 1)] = $email; } } $i++; } } // For count alias, grab the note keyword for nickname. // Where detail informations are place here. foreach ($data as $i => $val) { $regex = '/note\s+' . $val['nickname'] . '\s+(<.*\>)(.*)\n/'; if (preg_match_all($regex, $contents, $matches, PREG_SET_ORDER)) { foreach ($matches as $detail) { $data[$i]['notes'] = $detail[2]; if (preg_match_all('|<([^>]+)>|U', $detail[1], $matches_a)) { foreach ($matches_a[1] as $data_a) { list($field, $value) = explode(':', $data_a, 2); if ($field == 'otheremail') { $emails = explode(chr(3), $value); foreach ($emails as $j => $email) { if ($j == 0) { $data[$i]['otheremail'] = $email; } else { $data[$i]['otheremail' . ($j + 1)] = $email; } } continue; } $data[$i][$field] = $value; } } } } } $this->result = $data; return true; } } // }}} /* * Local variables: * mode: php * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?>