*/ class Net_NNTP_Message { // {{{ properties /* * Contains the message's header object * * @var object * @access public */ var $header; /** * Contains the body part of the message * * @var string * @access public */ var $body; // }}} // {{{ constructor /** * Constructor. * * @access public */ function Net_NNTP_Message() { $this->reset(); } // }}} // {{{ reset() /** * Resets the message object * * @access public */ function reset() { $this->header = new Net_NNTP_Header(); $this->body = null; } // }}} // {{{ create() /** * Create a new instance of Net_NNTP_Message * * @param optional mixed $input Can be any of the following: * (string) RFC2822 message lines (RCLF included) * (array) RFC2822 message lines (RCLF not included) * (object) Net_NNTP_Header object * (object) Net_NNTP_Message object * @param optional mixed $input2 If given, $input will only be use for the message's * header, while $input2 will be used for the body. * (Disallowed when $input is a Net_NNTP_Message) * * @access public * @since 0.1 */ function & create($input = null, $input2 = null) { $Object = new Net_NNTP_Message(); switch (true) { // Null case (is_null($input) && is_null($input2)): return $Object; break; // Object case is_object($input): switch (true) { // Header case is_a($input, 'net_nntp_header'): $Object->setHeader($input); $Object->setBody($input2); return $Object; break; // Message case is_a($input, 'net_nntp_message'): if ($input2 != null) { return PEAR::throwError('Second parameter not allowed!', null); } return $input; break; // Unknown object/class default: return PEAR::throwError('Unsupported object/class: '.get_class($input), null); } break; // Array & String (only 1st parameter) case ((is_string($input) || is_array($input)) && (is_null($input2))): $Object->setMessage($input); return $Object; break; // Array & String (also 2nd parameter) case ((is_string($input) || is_array($input)) && (is_string($input2) || is_array($input2))): $Object->setHeader($input); if (is_array($input2)) { $Object->body = implode("\r\n", $input2); } else { $Object->body = $input2; } return $Object; break; // Unknown type default: return PEAR::throwError('Unsupported object/class: '.get_class($input), null); } } // }}} // {{{ setMessage() /** * Sets the header and body grom the given $message * * @param mixed $message Can be any of the following: * (string) RFC2822 message lines (RCLF included) * (array) RFC2822 message lines (RCLF not included) * (object) Net_NNTP_Message object * * @access public */ function setMessage($message) { switch (true) { // Object case is_object($message); switch (true) { // Message case is_a($input, 'net_nntp_message'): $this =& $message; break; // Unknown object/class default: return PEAR::throwError('Unsupported object/class: '.get_class($message), null); } break; // Array & String case is_array($message): case is_string($message): $array = $this->splitMessage($message); $this->setHeader($array['header']); $this->setBody($array['body']); break; // Unknown type default: return PEAR::throwError('Unsupported type: '.gettype($message), null); } } // }}} // {{{ getMessageString() /** * Get the complete transport-ready message as a string * * @return string * @access public */ function getMessageString() { return $this->header->getFieldsString()."\r\n\r\n".$this->getBody(); } // }}} // {{{ getMessageArray() /** * Get the complete transport-ready message as an array * * @return string * @access public */ function getMessageArray() { // Get the header fields $header = $this->header->getFieldsArray(); // Append null line $header[] = ''; // Merge with body, and return return array_merge($header, explode("\r\n", $this->getBody())); } // }}} // {{{ setHeader() /** * Sets the header's fields from the given $input * * @param mixed $input Can be any of the following: * (string) RFC2822 message lines (RCLF included) * (array) RFC2822 message lines (RCLF not included) * (object) Net_NNTP_Header object * * @access public */ function setHeader($input) { switch (true) { // Object case is_object($input): switch (true) { // Header case is_a($input, 'net_nntp_header'): $this->header = $input; break; // Unknown object/class default: return PEAR::throwError('Unsupported object/class: '. get_class($input), null); } break; // Array & String case is_array($input): case is_string($input): $this->header->setFields($input); break; // Unknown type default: return PEAR::throwError('Unsupported type: '. gettype($input), null); } } // }}} // {{{ getHeader() /** * Gets the header object * * @return object * @access public */ function getHeader() { return $this->header; } // }}} // {{{ setBody() /** * Sets the body * * @param mixed $body Array or string * * @access public */ function setBody($body) { if (is_array($body)) { $this->body = implode("\r\n", $body); } else { $this->body = $body; } } // }}} // {{{ getBody() /** * Gets the body * * @return string * @access public */ function getBody() { return $this->body; } // }}} // {{{ splitMessage() /** * Splits the header and body given in $input apart (at the first * blank line) and return them (in an array) with the same type as $input. * * @param mixed $input Message in form of eiter string or array * * @return array Contains separated header and body sections in same type as $input * @access public */ function splitMessage($input) { switch (true) { // String case is_string($input); if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $input, $matches)) { return array('header' => $matches[1], 'body' => $matches[2]); } else { return PEAR::throwError('Could not split header and body'); } break; // Array case is_array($input); $header = array(); while (($line = array_shift($input)) != '') { $header[] = $line; } return array('header' => &$header, 'body' => $input); break; // Unknown type default: return PEAR::throwError('Unsupported type: '.gettype($input)); } } // }}} } ?>