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

Documentation generated on Mon, 11 Mar 2019 14:12:43 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.