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 2005/01/08 20:03:29 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 2005/01/08 20:03:29 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()
  104.     {
  105.     $this->reset();
  106.     }
  107.  
  108.     // }}}
  109.     // {{{ reset()
  110.  
  111.     /**
  112.      * Resets the message object
  113.      *
  114.      * @access public
  115.      */
  116.     function reset()
  117.     {
  118.     $this->header = new Net_NNTP_Header();
  119.     $this->body = null;
  120.     }
  121.  
  122.     // }}}
  123.     // {{{ create()
  124.     
  125.     /**
  126.      * Create a new instance of Net_NNTP_Message
  127.      *
  128.      * @param optional mixed $input  Can be any of the following:
  129.      *                                (string) RFC2822 message lines (RCLF included)
  130.      *                                (array)  RFC2822 message lines (RCLF not included)
  131.      *                                (object) Net_NNTP_Header object
  132.      *                                (object) Net_NNTP_Message object
  133.      * @param optional mixed $input2 If given, $input will only be use for the message's
  134.      *                                header, while $input2 will be used for the body.
  135.      *                                (Disallowed when $input is a Net_NNTP_Message)
  136.      *
  137.      * @access public
  138.      * @since 0.1
  139.      */
  140.     function create($input = null$input2 = null)
  141.     {
  142.     $Object = new Net_NNTP_Message();
  143.     
  144.     switch (true{
  145.  
  146.         // Null
  147.         case (is_null($input&& is_null($input2)):
  148.             return $Object;
  149.         break;
  150.  
  151.  
  152.         // Object 
  153.         case is_object($input):
  154.         switch (true{
  155.             
  156.             // Header
  157.             case is_a($input'net_nntp_header'):
  158.             $Object->setHeader($input);
  159.             $Object->setBody($input2);
  160.             return $Object;
  161.             break;
  162.             
  163.             // Message
  164.             case is_a($input'net_nntp_message'):
  165.             if ($input2 != null{
  166.                 return PEAR::throwError('Second parameter not allowed!'null);
  167.             }            
  168.             return $input;
  169.             break;
  170.             
  171.             // Unknown object/class
  172.             default:
  173.             return PEAR::throwError('Unsupported object/class: '.get_class($input)null);
  174.         }
  175.         break;
  176.  
  177.         // Array & String (only 1st parameter)
  178.         case ((is_string($input|| is_array($input)) && (is_null($input2))):
  179.         $Object->setMessage($input);
  180.         return $Object;
  181.         break;
  182.  
  183.         // Array & String (also 2nd parameter)
  184.         case ((is_string($input|| is_array($input)) && (is_string($input2|| is_array($input2))):
  185.  
  186.         $Object->setHeader($input);
  187.  
  188.         if (is_array($input2)) {
  189.             $Object->body = implode("\r\n"$input2);
  190.         else {
  191.             $Object->body = $input2;
  192.         }
  193.  
  194.         return $Object;
  195.         break;
  196.  
  197.         // Unknown type
  198.         default:
  199.         return PEAR::throwError('Unsupported object/class: '.get_class($input)null);
  200.     }
  201.     }
  202.  
  203.     // }}}
  204.     // {{{ setMessage()
  205.  
  206.     /**
  207.      * Sets the header and body grom the given $message
  208.      *
  209.      * @param mixed $message Can be any of the following:
  210.      *                        (string) RFC2822 message lines (RCLF included)
  211.      *                        (array)  RFC2822 message lines (RCLF not included)
  212.      *                        (object) Net_NNTP_Message object
  213.      *
  214.      * @access public
  215.      */
  216.     function setMessage($message)
  217.     {
  218.     switch (true{
  219.  
  220.         // Object
  221.         case is_object($message);
  222.             switch (true{
  223.  
  224.             // Message
  225.             case is_a($input'net_nntp_message'):
  226.                 $this->setHeader($message->getHeader());
  227.                 $this->setBody($message->getBody());
  228.                 break;
  229.  
  230.             // Unknown object/class
  231.             default:
  232.                 return PEAR::throwError('Unsupported object/class: '.get_class($message)null);
  233.         }
  234.         break;
  235.  
  236.         // Array & String
  237.         case is_array($message):
  238.         case is_string($message):
  239.         $array $this->splitMessage($message);
  240.         $this->setHeader($array['header']);
  241.         $this->setBody($array['body']);
  242.             break;
  243.         
  244.         // Unknown type
  245.         default:
  246.             return PEAR::throwError('Unsupported type: '.gettype($message)null);
  247.     }
  248.     }
  249.  
  250.     // }}}
  251.     // {{{ getMessageString()
  252.  
  253.     /**
  254.      * Get the complete transport-ready message as a string
  255.      *
  256.      * @return string 
  257.      * @access public
  258.      */
  259.     function getMessageString()
  260.     {
  261.     return $this->header->getFieldsString()."\r\n\r\n".$this->getBody();
  262.     }
  263.  
  264.     // }}}
  265.     // {{{ getMessageArray()
  266.  
  267.     /**
  268.      * Get the complete transport-ready message as an array
  269.      *
  270.      * @return string 
  271.      * @access public
  272.      */
  273.     function getMessageArray()
  274.     {
  275.     // Get the header fields
  276.     $header $this->header->getFieldsArray();
  277.     
  278.     // Append null line
  279.     $header['';
  280.     
  281.     // Merge with body, and return
  282.     return array_merge($headerexplode("\r\n"$this->getBody()));
  283.     }
  284.  
  285.     // }}}
  286.     // {{{ setHeader()
  287.  
  288.     /**
  289.      * Sets the header's fields from the given $input
  290.      *
  291.      * @param mixed $input Can be any of the following:
  292.      *                      (string) RFC2822 message lines (RCLF included)
  293.      *                      (array)  RFC2822 message lines (RCLF not included)
  294.      *                      (object) Net_NNTP_Header object
  295.      *
  296.      * @access public
  297.      */
  298.     function setHeader($input)
  299.     {
  300.         switch (true{
  301.  
  302.         // Object
  303.         case is_object($input):
  304.             switch (true{
  305.             
  306.             // Header
  307.             case is_a($input'net_nntp_header'):
  308.                     $this->header = $input;
  309.                 break;
  310.  
  311.             // Unknown object/class
  312.             default:
  313.                 return PEAR::throwError('Unsupported object/class: 'get_class($input)null);
  314.             }
  315.             break;
  316.  
  317.         // Array & String
  318.         case is_array($input):
  319.         case is_string($input):
  320.             $this->header->setFields($input);
  321.             break;
  322.  
  323.         // Unknown type
  324.         default:
  325.             return PEAR::throwError('Unsupported type: 'gettype($input)null);
  326.         }
  327.     }
  328.  
  329.     // }}}
  330.     // {{{ getHeader()
  331.  
  332.     /**
  333.      * Gets the header object
  334.      *
  335.      * @return object 
  336.      * @access public
  337.      */
  338.     function getHeader()
  339.     {
  340.     return $this->header;
  341.     }
  342.  
  343.     // }}}
  344.     // {{{ setBody()
  345.  
  346.     /**
  347.      * Sets the body
  348.      *
  349.      * @param mixed $body Array or string
  350.      *
  351.      * @access public
  352.      */
  353.     function setBody($body)
  354.     {
  355.     if (is_array($body)) {
  356.         $this->body = implode("\r\n"$body);
  357.     else {
  358.         $this->body = $body;
  359.     }
  360.     }
  361.  
  362.     // }}}
  363.     // {{{ getBody()
  364.  
  365.     /**
  366.      * Gets the body
  367.      *
  368.      * @return string 
  369.      * @access public
  370.      */
  371.     function getBody()
  372.     {
  373.     return $this->body;
  374.     }
  375.  
  376.     // }}}
  377.     // {{{ splitMessage()
  378.  
  379.     /**
  380.      * Splits the header and body given in $input apart (at the first
  381.      * blank line) and return them (in an array) with the same type as $input.
  382.      *
  383.      * @param mixed $input Message in form of eiter string or array
  384.      *
  385.      * @return array Contains separated header and body sections in same type as $input
  386.      * @access public
  387.      */
  388.     function splitMessage($input)
  389.     {
  390.     switch (true{
  391.  
  392.         // String
  393.         case is_string($input);
  394.             if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s"$input$matches)) {
  395.                 return array('header' => $matches[1]'body' => $matches[2]);
  396.             else {
  397.                 return PEAR::throwError('Could not split header and body');
  398.         }
  399.         break;
  400.         
  401.         // Array
  402.         case is_array($input);
  403.         $header = array();
  404.         while (($line array_shift($input)) != ''{
  405.             $header[$line;
  406.         }
  407.             return array('header' => &$header'body' => $input);
  408.         break;
  409.  
  410.         // Unknown type
  411.         default:
  412.             return PEAR::throwError('Unsupported type: '.gettype($input));
  413.     }
  414.     }
  415.  
  416.     // }}}
  417.  
  418. }
  419.  
  420. // }}}
  421.  
  422. ?>

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