|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * +----------------------------------------------------------------------+ |
| 4 | + * | Copyright (c) 1997-2008 The PHP Group | |
| 5 | + * +----------------------------------------------------------------------+ |
| 6 | + * | All rights reserved. | |
| 7 | + * | | |
| 8 | + * | Redistribution and use in source and binary forms, with or without | |
| 9 | + * | modification, are permitted provided that the following conditions | |
| 10 | + * | are met: | |
| 11 | + * | | |
| 12 | + * | - Redistributions of source code must retain the above copyright | |
| 13 | + * | notice, this list of conditions and the following disclaimer. | |
| 14 | + * | - Redistributions in binary form must reproduce the above copyright | |
| 15 | + * | notice, this list of conditions and the following disclaimer in the | |
| 16 | + * | documentation and/or other materials provided with the distribution. | |
| 17 | + * | - Neither the name of the The PEAR Group nor the names of its | |
| 18 | + * | contributors may be used to endorse or promote products derived from | |
| 19 | + * | this software without specific prior written permission. | |
| 20 | + * | | |
| 21 | + * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 22 | + * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 23 | + * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 24 | + * | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 25 | + * | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 26 | + * | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 27 | + * | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 28 | + * | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| 29 | + * | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 30 | + * | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
| 31 | + * | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 32 | + * | POSSIBILITY OF SUCH DAMAGE. | |
| 33 | + * +----------------------------------------------------------------------+ |
| 34 | + * |
| 35 | + * PHP Version 5 |
| 36 | + * |
| 37 | + * @category File_Formats |
| 38 | + * @package File_IMC |
| 39 | + * @author Paul M. Jones <pmjones@ciaweb.net> |
| 40 | + * @author Marshall Roch <mroch@php.net> |
| 41 | + * @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
| 42 | + * @version SVN: $Id$ |
| 43 | + * @link http://pear.php.net/package/File_IMC |
| 44 | + */ |
| 45 | + |
| 46 | +/** |
| 47 | +* This class handles vCard and vCalendar files, formats designed by the |
| 48 | +* Internet Mail Consortium (IMC). |
| 49 | +* |
| 50 | +* vCard automates the exchange of personal information typically found |
| 51 | +* on a traditional business card. vCard is used in applications such as |
| 52 | +* Internet mail, voice mail, Web browsers, telephony applications, call |
| 53 | +* centers, video conferencing, PIMs (Personal Information Managers), |
| 54 | +* PDAs (Personal Data Assistants), pagers, fax, office equipment, and |
| 55 | +* smart cards. |
| 56 | +* |
| 57 | +* vCalendar defines a transport and platform-independent format for |
| 58 | +* exchanging calendaring and scheduling information in an easy, automated, |
| 59 | +* and consistent manner. It captures information about event and "to-do" |
| 60 | +* items that are normally used by applications such as a personal |
| 61 | +* information managers (PIMs) and group schedulers. Programs that use |
| 62 | +* vCalendar can exchange important data about events so that you can |
| 63 | +* schedule meetings with anyone who has a vCalendar-aware program. |
| 64 | +* |
| 65 | +* This class is capable of building and parsing vCard 2.1, vCard 3.0, and |
| 66 | +* vCalendar files. The vCard code was moved from Contact_Vcard_Build |
| 67 | +* and Contact_Vcard_Parse, and the API remains basically the same. |
| 68 | +* The only difference is that this package uses a factory pattern: |
| 69 | +* |
| 70 | +* <code> |
| 71 | +* $parse =& File_IMC::parse('vCard'); |
| 72 | +* $build =& File_IMC::build('vCard', '3.0'); |
| 73 | +* </code> |
| 74 | +* instead of |
| 75 | +* <code> |
| 76 | +* $parse = new Contact_Vcard_Parse(); |
| 77 | +* $build = new Contact_Vcard_Build('3.0'); |
| 78 | +* </code> |
| 79 | +* |
| 80 | +* @category File_Formats |
| 81 | +* @package File_IMC |
| 82 | +* @author Paul M. Jones <pmjones@ciaweb.net> |
| 83 | +* @author Marshall Roch <mroch@php.net> |
| 84 | +* @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
| 85 | +* @version Release: @package_version@ |
| 86 | +* @link http://pear.php.net/package/File_IMC |
| 87 | +* @link http://www.imc.org/pdi/ IMC's vCard and vCalendar info page |
| 88 | +*/ |
| 89 | +class File_IMC |
| 90 | +{ |
| 91 | + /** |
| 92 | + * Constants for File_IMC errors. |
| 93 | + * @global |
| 94 | + */ |
| 95 | + const ERROR = 100; |
| 96 | + const ERROR_INVALID_DRIVER = 101; |
| 97 | + const ERROR_INVALID_PARAM = 102; |
| 98 | + const ERROR_INVALID_VCARD_VERSION = 103; |
| 99 | + const ERROR_PARAM_NOT_SET = 104; |
| 100 | + const ERROR_INVALID_ITERATION = 105; |
| 101 | + |
| 102 | + /** |
| 103 | + * Constants for File_IMC vCard "N" component positions. |
| 104 | + * @global |
| 105 | + */ |
| 106 | + const VCARD_N_FAMILY = 0; |
| 107 | + const VCARD_N_GIVEN = 1; |
| 108 | + const VCARD_N_ADDL = 2; |
| 109 | + const VCARD_N_PREFIX = 3; |
| 110 | + const VCARD_N_SUFFIX = 4; |
| 111 | + |
| 112 | + /** |
| 113 | + * Constants for File_IMC vCard "ADR" component positions. |
| 114 | + * @global |
| 115 | + */ |
| 116 | + const VCARD_ADR_POB = 0; |
| 117 | + const VCARD_ADR_EXTEND = 1; |
| 118 | + const VCARD_ADR_STREET = 2; |
| 119 | + const VCARD_ADR_LOCALITY = 3; |
| 120 | + const VCARD_ADR_REGION = 4; |
| 121 | + const VCARD_ADR_POSTCODE = 5; |
| 122 | + const VCARD_ADR_COUNTRY = 6; |
| 123 | + |
| 124 | + /** |
| 125 | + * Constants for File_IMC vCard "GEO" component positions. |
| 126 | + * @global |
| 127 | + */ |
| 128 | + const VCARD_GEO_LAT = 0; |
| 129 | + const VCARD_GEO_LON = 1; |
| 130 | + |
| 131 | + /** |
| 132 | + * SPL-compatible autoloader. |
| 133 | + * |
| 134 | + * @param string $className Name of the class to load. |
| 135 | + * |
| 136 | + * @return boolean |
| 137 | + */ |
| 138 | + public static function autoload($className) |
| 139 | + { |
| 140 | + if (strpos($className, 'File_IMC') === false) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + return include str_replace('_', '/', $className) . '.php'; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Builder factory |
| 148 | + * |
| 149 | + * Creates an instance of the correct parser class, based on the |
| 150 | + * parameter passed. For example, File_IMC::parse('vCard') creates |
| 151 | + * a new object to parse a vCard file. |
| 152 | + * |
| 153 | + * @param string $format Type of file to parse, vCard or vCalendar |
| 154 | + * @param mixed $version Optionally, the version. |
| 155 | + * |
| 156 | + * @return mixed |
| 157 | + * @throws File_IMC_Exception In case the driver is not found/available. |
| 158 | + */ |
| 159 | + public static function build($format, $version = null) |
| 160 | + { |
| 161 | + $format = trim($format); |
| 162 | + if (empty($format)) { |
| 163 | + throw new File_IMC_Exception('No driver.', self::ERROR_INVALID_DRIVER); |
| 164 | + } |
| 165 | + |
| 166 | + // fix^H^H^H^force case |
| 167 | + $format = ucfirst(strtolower($format)); |
| 168 | + |
| 169 | + $fileName = 'File/IMC/Build/'. $format . '.php'; |
| 170 | + $className = 'File_IMC_Build_'. $format; |
| 171 | + |
| 172 | + if (!class_exists($className, false)) { |
| 173 | + @include_once $fileName; |
| 174 | + } |
| 175 | + |
| 176 | + if (!class_exists($className, false)) { |
| 177 | + throw new File_IMC_Exception( |
| 178 | + 'No builder driver exists for format: ' . $format, |
| 179 | + self::ERROR_INVALID_DRIVER); |
| 180 | + } |
| 181 | + |
| 182 | + if ($version !== null) { |
| 183 | + $class = new $className($version); |
| 184 | + } else { |
| 185 | + $class = new $className; |
| 186 | + } |
| 187 | + return $class; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Parser factory |
| 192 | + * |
| 193 | + * Creates an instance of the correct parser class, based on the |
| 194 | + * parameter passed. For example, File_IMC::parse('vCard') creates |
| 195 | + * a new object to parse a vCard file. |
| 196 | + * |
| 197 | + * @param string $format Type of file to parse, vCard or vCalendar |
| 198 | + * |
| 199 | + * @return mixed |
| 200 | + * @throws File_IMC_Exception If no parse is found/available. |
| 201 | + */ |
| 202 | + public static function parse($format) |
| 203 | + { |
| 204 | + $format = trim($format); |
| 205 | + if (empty($format)) { |
| 206 | + throw new File_IMC_Exception('No driver.', self::ERROR_INVALID_DRIVER); |
| 207 | + } |
| 208 | + |
| 209 | + // fix^H^H^H^force case |
| 210 | + $format = ucfirst(strtolower($format)); |
| 211 | + |
| 212 | + $fileName = 'File/IMC/Parse/'. $format . '.php'; |
| 213 | + $className = 'File_IMC_Parse_'. $format; |
| 214 | + |
| 215 | + if (!class_exists($className, false)) { |
| 216 | + @include_once $fileName; |
| 217 | + } |
| 218 | + |
| 219 | + if (!class_exists($className, false)) { |
| 220 | + throw new File_IMC_Exception( |
| 221 | + 'No parser driver exists for format: ' . $format, |
| 222 | + self::ERROR_INVALID_DRIVER); |
| 223 | + } |
| 224 | + return new $className; |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +spl_autoload_register(array('File_IMC', 'autoload')); |
0 commit comments