Net_NNTP
[ class tree: Net_NNTP ] [ index: Net_NNTP ] [ all elements ]

Source for file Message.php

Documentation is available at Message.php

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

Documentation generated on Mon, 11 Mar 2019 13:52:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.