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

Source for file Client.php

Documentation is available at Client.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: Client.php,v 1.2.2.2 2005/01/14 20:03:22 heino Exp $
  58.  
  59. require_once 'Net/NNTP/Protocol/Client.php';
  60.  
  61.  
  62. // {{{ constants
  63.  
  64. /* NNTP Authentication modes */
  65. define('NET_NNTP_CLIENT_AUTH_ORIGINAL''original');
  66. define('NET_NNTP_CLIENT_AUTH_SIMPLE',   'simple');
  67. define('NET_NNTP_CLIENT_AUTH_GENERIC',  'generic');
  68.  
  69. // }}}
  70. // {{{ Net_NNTP_Client
  71.  
  72. /**
  73.  * Implementation of the client side of NNTP (Network News Transfer Protocol)
  74.  *
  75.  * The Net_NNTP_Client class is a frontend class to the Net_NNTP_Protocol_Client class.
  76.  *
  77.  * @category   Net
  78.  * @package    Net_NNTP
  79.  * @author     Heino H. Gehlsen <heino@gehlsen.dk>
  80.  * @version    $Id: Client.php,v 1.2.2.2 2005/01/14 20:03:22 heino Exp $
  81.  * @access     public
  82.  * @see        Net_NNTP_Protocol_Client
  83.  * @since      Class available since Release 0.11.0
  84.  */
  85. {
  86.     // {{{ properties
  87.  
  88.     /**
  89.      * Used for storing information about the currently selected group
  90.      *
  91.      * @var array 
  92.      * @access private
  93.      * @since 0.3
  94.      */
  95.     var $_currentGroup = null;
  96.  
  97.     // }}}
  98.     // {{{ constructor
  99.  
  100.     /**
  101.      * Constructor
  102.      *
  103.      * @access public
  104.      */
  105.     function Net_NNTP_Client()
  106.     {
  107.         parent::Net_NNTP_Protocol_Client();
  108.     }
  109.  
  110.     // }}}
  111.     // {{{ connect()
  112.  
  113.     /**
  114.      * Connect to the NNTP-server.
  115.      *
  116.      * @param optional string $host The adress of the NNTP-server to connect to.
  117.      * @param optional int $port The port to connect to.
  118.      *
  119.      * @return mixed (bool) true on success or (object) pear_error on failure
  120.      * @access public
  121.      * @see Net_NNTP_Client::quit()
  122.      * @see Net_NNTP_Client::authenticate()
  123.      * @see Net_NNTP_Client::connectAuthenticated()
  124.      */
  125.     function connect($host = NET_NNTP_PROTOCOL_CLIENT_DEFAULT_HOST,
  126.                      $port = NET_NNTP_PROTOCOL_CLIENT_DEFAULT_PORT)
  127.     {
  128.         return parent::connect($host$port);
  129.     }
  130.  
  131.     // }}}
  132.     // {{{ quit()
  133.  
  134.     /**
  135.      * Close connection to the newsserver
  136.      *
  137.      * @access public
  138.      * @see Net_NNTP_Client::connect()
  139.      */
  140.     function quit()
  141.     {
  142.         return $this->cmdQuit();
  143.     }
  144.  
  145.     // }}}
  146.     // {{{ authenticate()
  147.  
  148.     /**
  149.      * Authenticate
  150.      * 
  151.      * Auth process (not yet standarized but used any way)
  152.      * http://www.mibsoftware.com/userkt/nntpext/index.html
  153.      *
  154.      * @param string $user The username
  155.      * @param optional string $pass The password
  156.      * @param optional string $mode The authentication mode (original, simple, generic).
  157.      *
  158.      * @return mixed (bool) true on success or (object) pear_error on failure
  159.      * @access public
  160.      * @see Net_NNTP_Client::connect()
  161.      * @see Net_NNTP_Client::connectAuthenticated()
  162.      */
  163.     function authenticate($user$pass$mode = NET_NNTP_CLIENT_AUTH_ORIGINAL)
  164.     {
  165.         // Username is a must...
  166.         if ($user == null{
  167.             return PEAR::throwError('No username supplied'null);
  168.         }
  169.  
  170.         // Use selected authentication method
  171.         switch ($mode{
  172.             case NET_NNTP_CLIENT_AUTH_ORIGINAL:
  173.                 return $this->cmdAuthinfo($user$pass);
  174.                 break;
  175.             case NET_NNTP_CLIENT_AUTH_SIMPLE:
  176.                 return $this->cmdAuthinfoSimple($user$pass);
  177.                 break;
  178.             case NET_NNTP_CLIENT_AUTH_GENERIC:
  179.                 return $this->cmdAuthinfoGeneric($user$pass);
  180.                 break;
  181.             default:
  182.                 return PEAR::throwError("The auth mode: '$mode' is unknown"null);
  183.         }
  184.     }
  185.  
  186.     // }}}
  187.     // {{{ isConnected()
  188.  
  189.     /**
  190.      * Test whether a connection is currently open.
  191.      *
  192.      * @return bool true or false
  193.      * @access public
  194.      * @see Net_NNTP_Client::connect()
  195.      * @see Net_NNTP_Client::quit()
  196.      */
  197.     function isConnected()
  198.     {
  199.         return parent::isConnected();
  200.     }
  201.  
  202.     // }}}
  203.     // {{{ selectGroup()
  204.  
  205.     /**
  206.      * Selects a newsgroup
  207.      *
  208.      * @param string $newsgroup Newsgroup name
  209.      *
  210.      * @return mixed (array) Info about the newsgroup on success or (object) pear_error on failure
  211.      * @access public
  212.      * @see Net_NNTP_Client::group()
  213.      * @see Net_NNTP_Client::first()
  214.      * @see Net_NNTP_Client::last()
  215.      * @see Net_NNTP_Client::count()
  216.      * @see Net_NNTP_Client::getGroups()
  217.      */
  218.     function selectGroup($newsgroup)
  219.     {
  220.         $response_arr $this->cmdGroup($newsgroup);
  221.         if (PEAR::isError($response_arr)) {
  222.             return $response_arr;
  223.         }
  224.  
  225.         // Store group info in the object
  226.         $this->_currentGroup $response_arr;
  227.  
  228.         return $response_arr;
  229.     }
  230.  
  231.     // }}}
  232.     // {{{ getGroups()
  233.  
  234.     /**
  235.      * Fetches a list of all avaible newsgroups
  236.      *
  237.      * @return mixed (array) nested array with informations about existing newsgroups on success or (object) pear_error on failure
  238.      * @access public
  239.      * @see Net_NNTP_Client::selectGroup()
  240.      * @see Net_NNTP_Client::getDescriptions()
  241.      */
  242.     function getGroups()
  243.     {
  244.         // Get groups
  245.         $groups $this->cmdList();
  246.         if (PEAR::isError($groups)) {
  247.             return $groups;
  248.         }
  249.  
  250.         return $groups;
  251.     }
  252.  
  253.     // }}}
  254.     // {{{ getDescriptions()
  255.  
  256.     /**
  257.      * Fetches a list of all avaible newsgroup descriptions.
  258.      *
  259.      * @return mixed (array) nested array with description of existing newsgroups on success or (object) pear_error on failure
  260.      * @access public
  261.      * @see Net_NNTP_Client::getGroups()
  262.      */
  263.     function getDescriptions()
  264.     {
  265.         // Get group descriptions
  266.         $descriptions $this->cmdListNewsgroups();
  267.         if (PEAR::isError($descriptions)) {
  268.             return $descriptions;
  269.         }
  270.     
  271.         return $descriptions;
  272.     }
  273.  
  274.     // }}}
  275.     // {{{ getOverview()
  276.  
  277.     /**
  278.      * Fetch message header fields from message number $first to $last
  279.      *
  280.      * The format of the returned array is:
  281.      * $messages[message_id][header_name]
  282.      *
  283.      * @param integer $first first article to fetch
  284.      * @param integer $last  last article to fetch
  285.      *
  286.      * @return mixed (array) nested array of message and their headers on success or (object) pear_error on failure
  287.      * @access public
  288.      * @see Net_NNTP_Client::getOverviewFormat()
  289.      * @see Net_NNTP_Client::getReferencesOverview()
  290.      */
  291.     function getOverview($first$last)
  292.     {
  293.         $overview $this->cmdXOver($first.'-'.$last);
  294.         if (PEAR::isError($overview)) {
  295.             return $overview;
  296.         }
  297.     
  298.         return $overview;
  299.     }
  300.  
  301.     // }}}
  302.     // {{{ getOverviewFmt()
  303.  
  304.     /**
  305.      * Returns a list of avaible headers which are send from NNTP-server to the client for every news message
  306.      *
  307.      * @return mixed (array) header names on success or (object) pear_error on failure
  308.      * @access public
  309.      * @see Net_NNTP_Client::getOverview()
  310.      */
  311.     function getOverviewFormat()
  312.     {
  313.         return $this->cmdListOverviewFmt();
  314.     }
  315.  
  316.     // }}}
  317.     // {{{ post()
  318.  
  319.     /**
  320.      * Post an article to a number of newsgroups.
  321.      *
  322.      * (Among the aditional headers you might think of adding could be:
  323.      * "NNTP-Posting-Host: <ip-of-author>", which should contain the IP-address
  324.      * of the author of the post, so the message can be traced back to him.
  325.      * Or "Organization: <org>" which contain the name of the organization
  326.      * the post originates from)
  327.      *
  328.      * @param string $newsgroups The newsgroup to post to.
  329.      * @param string $subject The subject of the post.
  330.      * @param string $body The body of the post itself.
  331.      * @param string $from Name + email-adress of sender.
  332.      * @param optional string $aditional Aditional headers to send.
  333.      *
  334.      * @return mixed (string) server response on success or (object) pear_error on failure
  335.      * @access public
  336.      */
  337.     function post($newsgroups$subject$body$from$aditional = null)
  338.     {
  339.         return $this->cmdPost($newsgroups$subject$body$from$aditional);
  340.     }
  341.  
  342.     // }}}
  343.     // {{{ getArticleRaw()
  344.  
  345.     /**
  346.      * Get a article (raw data)
  347.      *
  348.      * @param mixed $article Either the message-id or the message-number on the server of the article to fetch.
  349.      * @param optional bool  $implode When true the result array is imploded to a string, defaults to false.
  350.      *
  351.      * @return mixed (array/string) The article on success or (object) pear_error on failure
  352.      * @access public
  353.      * @see Net_NNTP_Client::getArticle()
  354.      * @see Net_NNTP_Client::getHeaderRaw()
  355.      * @see Net_NNTP_Client::getBodyRaw()
  356.      */
  357.     function getArticleRaw($article$implode = false)
  358.     {
  359.         $data $this->cmdArticle($article);
  360.         if (PEAR::isError($data)) {
  361.             return $data;
  362.         }
  363.  
  364.         if ($implode == true{
  365.             $data implode("\r\n"$data);
  366.         }
  367.  
  368.         return $data;
  369.     }
  370.  
  371.     // }}}
  372.     // {{{ getHeaderRaw()
  373.  
  374.     /**
  375.      * Get the header of an article (raw data)
  376.      *
  377.      * @param mixed $article Either the (string) message-id or the (int) message-number on the server of the article to fetch.
  378.      * @param optional bool $implode When true the result array is imploded to a string, defaults to false.
  379.      *
  380.      * @return mixed (array/string) header fields on success or (object) pear_error on failure
  381.      * @access public
  382.      * @see Net_NNTP_Client::getHeader()
  383.      * @see Net_NNTP_Client::getArticleRaw()
  384.      * @see Net_NNTP_Client::getBodyRaw()
  385.      */
  386.     function getHeaderRaw($article$implode = false)
  387.     {
  388.         $data $this->cmdHead($article);
  389.         if (PEAR::isError($data)) {
  390.             return $data;
  391.         }
  392.  
  393.         if ($implode == true{
  394.             $data implode("\r\n"$data);
  395.         }
  396.  
  397.         return $data;
  398.     }
  399.  
  400.     // }}}
  401.     // {{{ getBodyRaw()
  402.  
  403.     /**
  404.      * Get the body of an article (raw data)
  405.      *
  406.      * @param mixed $article Either the message-id or the message-number on the server of the article to fetch.
  407.      * @param optional bool $implode When true the result array is imploded to a string, defaults to false.
  408.      *
  409.      * @return mixed (array/string) body on success or (object) pear_error on failure
  410.      * @access public
  411.      * @see Net_NNTP_Client::getBody()
  412.      * @see Net_NNTP_Client::getHeaderRaw()
  413.      * @see Net_NNTP_Client::getArticleRaw()
  414.      */
  415.     function getBodyRaw($article$implode = false)
  416.     {
  417.         $data $this->cmdBody($article);
  418.         if (PEAR::isError($data)) {
  419.             return $data;
  420.         }
  421.     
  422.         if ($implode == true{
  423.             $data implode("\r\n"$data);
  424.         }
  425.     
  426.         return $data;
  427.     }
  428.  
  429.     // }}}
  430.     // {{{ getDate()
  431.  
  432.     /**
  433.      * Get the NNTP-server's internal date
  434.      *
  435.      * Get the date from the newsserver format of returned date:
  436.      *
  437.      * @param optional int $format
  438.      *   - 0: $date - timestamp
  439.      *   - 1: $date['y'] - year
  440.      *        $date['m'] - month
  441.      *        $date['d'] - day
  442.      *
  443.      * @return mixed (mixed) date on success or (object) pear_error on failure
  444.      * @access public
  445.      * @since 0.3
  446.      */
  447.     function getDate($format = 1)
  448.     {
  449.         $date $this->cmdDate();
  450.         if (PEAR::isError($date)) {
  451.             return $date;
  452.         }
  453.  
  454.         switch ($format{
  455.             case 1:
  456.                 return array('y' => substr($date04)'m' => substr($date42)'d' => substr($date62));
  457.                 break;
  458.  
  459.             case 0:
  460.             default:
  461.                 return $date;
  462.                 break;
  463.         }
  464.     }
  465.  
  466.     // }}}
  467.     // {{{ count()
  468.  
  469.     /**
  470.      * Number of articles in currently selected group
  471.      *
  472.      * @return integer number of article in group
  473.      * @access public
  474.      * @since 0.3
  475.      * @see Net_NNTP_Client::group()
  476.      * @see Net_NNTP_Client::first()
  477.      * @see Net_NNTP_Client::last()
  478.      * @see Net_NNTP_Client::selectGroup()
  479.      */
  480.     function count()
  481.     {
  482.         return $this->_currentGroup['count'];
  483.     }
  484.  
  485.     // }}}
  486.     // {{{ last()
  487.  
  488.     /**
  489.      * Maximum article number in currently selected group
  490.      *
  491.      * @return integer number of last article
  492.      * @access public
  493.      * @since 0.3
  494.      * @see Net_NNTP_Client::first()
  495.      * @see Net_NNTP_Client::group()
  496.      * @see Net_NNTP_Client::count()
  497.      * @see Net_NNTP_Client::selectGroup()
  498.      */
  499.     function last()
  500.     {
  501.         return $this->_currentGroup['last'];
  502.     }
  503.  
  504.     // }}}
  505.     // {{{ first()
  506.  
  507.     /**
  508.      * Minimum article number in currently selected group
  509.      *
  510.      * @return integer number of first article
  511.      * @access public
  512.      * @since 0.3
  513.      * @see Net_NNTP_Client::last()
  514.      * @see Net_NNTP_Client::group()
  515.      * @see Net_NNTP_Client::count()
  516.      * @see Net_NNTP_Client::selectGroup()
  517.      */
  518.     function first()
  519.     {
  520.         return $this->_currentGroup['first'];
  521.     }
  522.  
  523.     // }}}
  524.     // {{{ group()
  525.  
  526.     /**
  527.      * Currently selected group
  528.      *
  529.      * @return string group name
  530.      * @access public
  531.      * @since 0.3
  532.      * @see Net_NNTP_Client::first()
  533.      * @see Net_NNTP_Client::last()
  534.      * @see Net_NNTP_Client::count()
  535.      * @see Net_NNTP_Client::selectGroup()
  536.      */
  537.     function group()
  538.     {
  539.         return $this->_currentGroup['group'];
  540.     }
  541.  
  542.     // }}}
  543.  
  544. }
  545.  
  546. // }}}
  547.  
  548. ?>

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