Mail
[ ]
[ ]
[ ]
[ ]
[ ]
Source for file parser.php
Documentation is available at parser.php
1. <?php
2. /**
3. * File containing the ezcMailParser class
4. *
5. * @package Mail
6. * @version 1.4
7. * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
8. * @license http://ez.no/licenses/new_bsd New BSD License
9. */
10.
11. /**
12. * Parses a mail in RFC822 format to an ezcMail structure.
13. *
14. * If you want to use your own mail class (extended from {@link ezcMail}),
15. * use {@link ezcMailParserOption}. Example:
16. * <code>
17. * $options = new ezcMailParserOptions();
18. * $options->mailClass = 'MyMailClass';
19. *
20. * $parser = new ezcMailParser( $options );
21. * // if you want to use MyMailClass which extends ezcMail
22. * </code>
23. *
24. * File attachments will be written to disk in a temporary directory.
25. * This temporary directory and the file attachment will be removed
26. * when PHP ends execution. If you want to keep the file you should move it
27. * to another directory.
28. *
29. * @property ezcMailParserOptions $options
30. * Holds the options you can set to the mail parser.
31. *
32. * @package Mail
33. * @version 1.4
34. * @mainclass
35. */
36. class ezcMailParser
37. {
38. /**
39. * Holds the parser of the current mail.
40. *
41. * @var ezcMailPartParser
42. */
43. private $partParser = null;
44.
45. /**
46. * Holds the directory where parsed mail should store temporary files.
47. *
48. * @var string
49. */
50. private static $tmpDir = null;
51.
52. /**
53. * Holds options you can be set to the mail parser.
54. *
55. * @var ezcMailParserOptions
56. */
57. private $options;
58.
59. /**
60. * Constructs a new mail parser.
61. *
62. * For options you can set to the mail parser see {@link ezcMailParserOptions}.
63. *
64. * @throws ezcBasePropertyNotFoundException
65. * if $options contains a property not defined
66. * @throws ezcBaseValueException
67. * if $options contains a property with a value not allowed
68. * @param ezcMailParserOptions|array(string=>mixed)$options
69. */
70. public function __construct( $options = array() )
71. {
72. if ( $options instanceof ezcMailParserOptions )
73. {
74. $this->options = $options;
75. }
76. else if ( is_array( $options ) )
77. {
78. $this->options = new ezcMailParserOptions( $options );
79. }
80. else
81. {
82. throw new ezcBaseValueException( "options", $options, "ezcMailParserOptions|array" );
83. }
84. }
85.
86. /**
87. * Sets the property $name to $value.
88. *
89. * @throws ezcBasePropertyNotFoundException
90. * if the property $name does not exist
91. * @throws ezcBaseValueException
92. * if $value is not accepted for the property $name
93. * @param string $name
94. * @param mixed $value
95. * @ignore
96. */
97. public function __set( $name, $value )
98. {
99. switch ( $name )
100. {
101. case 'options':
102. if ( !( $value instanceof ezcMailParserOptions ) )
103. {
104. throw new ezcBaseValueException( 'options', $value, 'instanceof ezcMailParserOptions' );
105. }
106. $this->options = $value;
107. break;
108.
109. default:
110. throw new ezcBasePropertyNotFoundException( $name );
111. }
112. }
113.
114. /**
115. * Returns the value of the property $name.
116. *
117. * @throws ezcBasePropertyNotFoundException
118. * if the property $name does not exist
119. * @param string $name
120. * @ignore
121. */
122. public function __get( $name )
123. {
124. switch ( $name )
125. {
126. case 'options':
127. return $this->options;
128.
129. default:
130. throw new ezcBasePropertyNotFoundException( $name );
131. }
132. }
133.
134. /**
135. * Returns true if the property $name is set, otherwise false.
136. *
137. * @param string $name
138. * @return bool
139. * @ignore
140. */
141. public function __isset( $name )
142. {
143. switch ( $name )
144. {
145. case 'options':
146. return true;
147.
148. default:
149. return false;
150. }
151. }
152.
153. /**
154. * Returns an array of ezcMail objects parsed from the mail set $set.
155. *
156. * You can optionally use ezcMailParserOptions to provide an alternate class
157. * name which will be instantiated instead of ezcMail, if you need to extend
158. * ezcMail.
159. *
160. * Example:
161. * <code>
162. * $options = new ezcMailParserOptions();
163. * $options->mailClass = 'MyMailClass';
164. *
165. * $parser = new ezcMailParser( $options );
166. * // if you want to use MyMailClass which extends ezcMail
167. * </code>
168. *
169. * @apichange Remove second parameter
170. *
171. * @throws ezcBaseFileNotFoundException
172. * if a neccessary temporary file could not be openened.
173. * @param ezcMailParserSet $set
174. * @param string $class Deprecated. Use $mailClass in ezcMailParserOptions class instead.
175. * @return array(ezcMail)
176. */
177. public function parseMail( ezcMailParserSet $set, $class = null )
178. {
179. $mail = array();
180. if ( !$set->hasData() )
181. {
182. return $mail;
183. }
184. if ( $class === null )
185. {
186. $class = $this->options->mailClass;
187. }
188. do
189. {
190. $this->partParser = new ezcMailRfc822Parser();
191. $data = "";
192. $size = 0;
193. while ( ( $data = $set->getNextLine() ) !== null )
194. {
195. $this->partParser->parseBody( $data );
196. $size += strlen( $data );
197. }
198. $part = $this->partParser->finish( $class );
199. $part->size = $size;
200. $mail[] = $part;
201. } while ( $set->nextMail() );
202. return $mail;
203. }
204.
205. /**
206. * Sets the temporary directory.
207. *
208. * The temporary directory must be writeable by PHP. It will be used to store
209. * file attachments.
210. *
211. * @todo throw if the directory is not writeable.
212. * @param string $dir
213. */
214. public static function setTmpDir( $dir )
215. {
216. self::$tmpDir = $dir;
217. }
218.
219. /**
220. * Returns the temporary directory.
221. *
222. * If no temporary directory has been set this method defaults to
223. * /tmp/ for linux and c:\tmp\ for windows.
224. *
225. * @return string
226. */
227. public static function getTmpDir()
228. {
229. if ( self::$tmpDir === null )
230. {
231. $uname = php_uname();
232. if ( strtoupper( substr( $uname, 0, 3 ) == "WIN" ) )
233. {
234. self::$tmpDir = "c:\\tmp\\";
235. }
236. else
237. {
238. self::$tmpDir = "/tmp/";
239. }
240.
241. }
242. return self::$tmpDir;
243. }
244. }
245. ?>
Last updated: Mon, 17 Dec 2007