Message.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. // +-----------------------------------------------------------------------+
  3. // | |
  4. // | Copyright © 2003 Heino H. Gehlsen. All Rights Reserved. |
  5. // | http://www.heino.gehlsen.dk/software/license |
  6. // | |
  7. // +-----------------------------------------------------------------------+
  8. // | |
  9. // | This work (including software, documents, or other related items) is |
  10. // | being provided by the copyright holders under the following license. |
  11. // | By obtaining, using and/or copying this work, you (the licensee) |
  12. // | agree that you have read, understood, and will comply with the |
  13. // | following terms and conditions: |
  14. // | |
  15. // | Permission to use, copy, modify, and distribute this software and |
  16. // | its documentation, with or without modification, for any purpose and |
  17. // | without fee or royalty is hereby granted, provided that you include |
  18. // | the following on ALL copies of the software and documentation or |
  19. // | portions thereof, including modifications, that you make: |
  20. // | |
  21. // | 1. The full text of this NOTICE in a location viewable to users of |
  22. // | the redistributed or derivative work. |
  23. // | |
  24. // | 2. Any pre-existing intellectual property disclaimers, notices, or |
  25. // | terms and conditions. If none exist, a short notice of the |
  26. // | following form (hypertext is preferred, text is permitted) should |
  27. // | be used within the body of any redistributed or derivative code: |
  28. // | "Copyright © 2003 Heino H. Gehlsen. All Rights Reserved. |
  29. // | http://www.heino.gehlsen.dk/software/license" |
  30. // | |
  31. // | 3. Notice of any changes or modifications to the files, including |
  32. // | the date changes were made. (We recommend you provide URIs to |
  33. // | the location from which the code is derived.) |
  34. // | |
  35. // | THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT |
  36. // | HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, |
  37. // | INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR |
  38. // | FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE |
  39. // | OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, |
  40. // | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. |
  41. // | |
  42. // | COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, |
  43. // | SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE |
  44. // | SOFTWARE OR DOCUMENTATION. |
  45. // | |
  46. // | The name and trademarks of copyright holders may NOT be used in |
  47. // | advertising or publicity pertaining to the software without specific, |
  48. // | written prior permission. Title to copyright in this software and any |
  49. // | associated documentation will at all times remain with copyright |
  50. // | holders. |
  51. // | |
  52. // +-----------------------------------------------------------------------+
  53. // | |
  54. // | This license is based on the "W3C® SOFTWARE NOTICE AND LICENSE". |
  55. // | No changes have been made to the "W3C® SOFTWARE NOTICE AND LICENSE", |
  56. // | except for the references to the copyright holder, which has either |
  57. // | been changes or removed. |
  58. // | |
  59. // +-----------------------------------------------------------------------+
  60. // $Id: Message.php,v 1.7 2003/08/16 19:15:04 heino Exp $
  61. require_once 'PEAR.php';
  62. require_once 'Net/NNTP/Header.php';
  63. /**
  64. * The Net_NNTP_Message class
  65. *
  66. * @version $Revision: 1.7 $
  67. * @package Net_NNTP
  68. *
  69. * @author Heino H. Gehlsen <heino@gehlsen.dk>
  70. */
  71. class Net_NNTP_Message
  72. {
  73. // {{{ properties
  74. /*
  75. * Contains the message's header object
  76. *
  77. * @var object
  78. * @access public
  79. */
  80. var $header;
  81. /**
  82. * Contains the body part of the message
  83. *
  84. * @var string
  85. * @access public
  86. */
  87. var $body;
  88. // }}}
  89. // {{{ constructor
  90. /**
  91. * Constructor.
  92. *
  93. * @access public
  94. */
  95. function Net_NNTP_Message()
  96. {
  97. $this->reset();
  98. }
  99. // }}}
  100. // {{{ reset()
  101. /**
  102. * Resets the message object
  103. *
  104. * @access public
  105. */
  106. function reset()
  107. {
  108. $this->header = new Net_NNTP_Header();
  109. $this->body = null;
  110. }
  111. // }}}
  112. // {{{ create()
  113. /**
  114. * Create a new instance of Net_NNTP_Message
  115. *
  116. * @param optional mixed $input Can be any of the following:
  117. * (string) RFC2822 message lines (RCLF included)
  118. * (array) RFC2822 message lines (RCLF not included)
  119. * (object) Net_NNTP_Header object
  120. * (object) Net_NNTP_Message object
  121. * @param optional mixed $input2 If given, $input will only be use for the message's
  122. * header, while $input2 will be used for the body.
  123. * (Disallowed when $input is a Net_NNTP_Message)
  124. *
  125. * @access public
  126. * @since 0.1
  127. */
  128. function & create($input = null, $input2 = null)
  129. {
  130. $Object = new Net_NNTP_Message();
  131. switch (true) {
  132. // Null
  133. case (is_null($input) && is_null($input2)):
  134. return $Object;
  135. break;
  136. // Object
  137. case is_object($input):
  138. switch (true) {
  139. // Header
  140. case is_a($input, 'net_nntp_header'):
  141. $Object->setHeader($input);
  142. $Object->setBody($input2);
  143. return $Object;
  144. break;
  145. // Message
  146. case is_a($input, 'net_nntp_message'):
  147. if ($input2 != null) {
  148. return PEAR::throwError('Second parameter not allowed!', null);
  149. }
  150. return $input;
  151. break;
  152. // Unknown object/class
  153. default:
  154. return PEAR::throwError('Unsupported object/class: '.get_class($input), null);
  155. }
  156. break;
  157. // Array & String (only 1st parameter)
  158. case ((is_string($input) || is_array($input)) && (is_null($input2))):
  159. $Object->setMessage($input);
  160. return $Object;
  161. break;
  162. // Array & String (also 2nd parameter)
  163. case ((is_string($input) || is_array($input)) && (is_string($input2) || is_array($input2))):
  164. $Object->setHeader($input);
  165. if (is_array($input2)) {
  166. $Object->body = implode("\r\n", $input2);
  167. } else {
  168. $Object->body = $input2;
  169. }
  170. return $Object;
  171. break;
  172. // Unknown type
  173. default:
  174. return PEAR::throwError('Unsupported object/class: '.get_class($input), null);
  175. }
  176. }
  177. // }}}
  178. // {{{ setMessage()
  179. /**
  180. * Sets the header and body grom the given $message
  181. *
  182. * @param mixed $message Can be any of the following:
  183. * (string) RFC2822 message lines (RCLF included)
  184. * (array) RFC2822 message lines (RCLF not included)
  185. * (object) Net_NNTP_Message object
  186. *
  187. * @access public
  188. */
  189. function setMessage($message)
  190. {
  191. switch (true) {
  192. // Object
  193. case is_object($message);
  194. switch (true) {
  195. // Message
  196. case is_a($input, 'net_nntp_message'):
  197. $this =& $message;
  198. break;
  199. // Unknown object/class
  200. default:
  201. return PEAR::throwError('Unsupported object/class: '.get_class($message), null);
  202. }
  203. break;
  204. // Array & String
  205. case is_array($message):
  206. case is_string($message):
  207. $array = $this->splitMessage($message);
  208. $this->setHeader($array['header']);
  209. $this->setBody($array['body']);
  210. break;
  211. // Unknown type
  212. default:
  213. return PEAR::throwError('Unsupported type: '.gettype($message), null);
  214. }
  215. }
  216. // }}}
  217. // {{{ getMessageString()
  218. /**
  219. * Get the complete transport-ready message as a string
  220. *
  221. * @return string
  222. * @access public
  223. */
  224. function getMessageString()
  225. {
  226. return $this->header->getFieldsString()."\r\n\r\n".$this->getBody();
  227. }
  228. // }}}
  229. // {{{ getMessageArray()
  230. /**
  231. * Get the complete transport-ready message as an array
  232. *
  233. * @return string
  234. * @access public
  235. */
  236. function getMessageArray()
  237. {
  238. // Get the header fields
  239. $header = $this->header->getFieldsArray();
  240. // Append null line
  241. $header[] = '';
  242. // Merge with body, and return
  243. return array_merge($header, explode("\r\n", $this->getBody()));
  244. }
  245. // }}}
  246. // {{{ setHeader()
  247. /**
  248. * Sets the header's fields from the given $input
  249. *
  250. * @param mixed $input Can be any of the following:
  251. * (string) RFC2822 message lines (RCLF included)
  252. * (array) RFC2822 message lines (RCLF not included)
  253. * (object) Net_NNTP_Header object
  254. *
  255. * @access public
  256. */
  257. function setHeader($input)
  258. {
  259. switch (true) {
  260. // Object
  261. case is_object($input):
  262. switch (true) {
  263. // Header
  264. case is_a($input, 'net_nntp_header'):
  265. $this->header = $input;
  266. break;
  267. // Unknown object/class
  268. default:
  269. return PEAR::throwError('Unsupported object/class: '. get_class($input), null);
  270. }
  271. break;
  272. // Array & String
  273. case is_array($input):
  274. case is_string($input):
  275. $this->header->setFields($input);
  276. break;
  277. // Unknown type
  278. default:
  279. return PEAR::throwError('Unsupported type: '. gettype($input), null);
  280. }
  281. }
  282. // }}}
  283. // {{{ getHeader()
  284. /**
  285. * Gets the header object
  286. *
  287. * @return object
  288. * @access public
  289. */
  290. function getHeader()
  291. {
  292. return $this->header;
  293. }
  294. // }}}
  295. // {{{ setBody()
  296. /**
  297. * Sets the body
  298. *
  299. * @param mixed $body Array or string
  300. *
  301. * @access public
  302. */
  303. function setBody($body)
  304. {
  305. if (is_array($body)) {
  306. $this->body = implode("\r\n", $body);
  307. } else {
  308. $this->body = $body;
  309. }
  310. }
  311. // }}}
  312. // {{{ getBody()
  313. /**
  314. * Gets the body
  315. *
  316. * @return string
  317. * @access public
  318. */
  319. function getBody()
  320. {
  321. return $this->body;
  322. }
  323. // }}}
  324. // {{{ splitMessage()
  325. /**
  326. * Splits the header and body given in $input apart (at the first
  327. * blank line) and return them (in an array) with the same type as $input.
  328. *
  329. * @param mixed $input Message in form of eiter string or array
  330. *
  331. * @return array Contains separated header and body sections in same type as $input
  332. * @access public
  333. */
  334. function splitMessage($input)
  335. {
  336. switch (true) {
  337. // String
  338. case is_string($input);
  339. if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $input, $matches)) {
  340. return array('header' => $matches[1], 'body' => $matches[2]);
  341. } else {
  342. return PEAR::throwError('Could not split header and body');
  343. }
  344. break;
  345. // Array
  346. case is_array($input);
  347. $header = array();
  348. while (($line = array_shift($input)) != '') {
  349. $header[] = $line;
  350. }
  351. return array('header' => &$header, 'body' => $input);
  352. break;
  353. // Unknown type
  354. default:
  355. return PEAR::throwError('Unsupported type: '.gettype($input));
  356. }
  357. }
  358. // }}}
  359. }
  360. ?>