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.4.2.5 2005/03/28 16:33:17 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.4.2.5 2005/03/28 16:33:17 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.      */
  124.     function connect($host = NET_NNTP_PROTOCOL_CLIENT_DEFAULT_HOST,
  125.                      $port = NET_NNTP_PROTOCOL_CLIENT_DEFAULT_PORT)
  126.     {
  127.         return parent::connect($host$port);
  128.     }
  129.  
  130.     // }}}
  131.     // {{{ quit()
  132.  
  133.     /**
  134.      * Close connection to the newsserver
  135.      *
  136.      * @access public
  137.      * @see Net_NNTP_Client::connect()
  138.      */
  139.     function quit()
  140.     {
  141.         return $this->cmdQuit();
  142.     }
  143.  
  144.     // }}}
  145.     // {{{ authenticate()
  146.  
  147.     /**
  148.      * Authenticate
  149.      * 
  150.      * Auth process (not yet standarized but used any way)
  151.      * http://www.mibsoftware.com/userkt/nntpext/index.html
  152.      *
  153.      * @param string $user The username
  154.      * @param optional string $pass The password
  155.      * @param optional string $mode The authentication mode (original, simple, generic).
  156.      *
  157.      * @return mixed (bool) true on success or (object) pear_error on failure
  158.      * @access public
  159.      * @see Net_NNTP_Client::connect()
  160.      */
  161.     function authenticate($user$pass$mode = NET_NNTP_CLIENT_AUTH_ORIGINAL)
  162.     {
  163.         // Username is a must...
  164.         if ($user == null{
  165.             return PEAR::throwError('No username supplied'null);
  166.         }
  167.  
  168.         // Use selected authentication method
  169.         switch ($mode{
  170.             case NET_NNTP_CLIENT_AUTH_ORIGINAL:
  171.                 return $this->cmdAuthinfo($user$pass);
  172.                 break;
  173.             case NET_NNTP_CLIENT_AUTH_SIMPLE:
  174.                 return $this->cmdAuthinfoSimple($user$pass);
  175.                 break;
  176.             case NET_NNTP_CLIENT_AUTH_GENERIC:
  177.                 return $this->cmdAuthinfoGeneric($user$pass);
  178.                 break;
  179.             default:
  180.                 return PEAR::throwError("The auth mode: '$mode' is unknown"null);
  181.         }
  182.     }
  183.  
  184.     // }}}
  185.     // {{{ isConnected()
  186.  
  187.     /**
  188.      * Test whether a connection is currently open.
  189.      *
  190.      * @return bool true or false
  191.      * @access public
  192.      * @see Net_NNTP_Client::connect()
  193.      * @see Net_NNTP_Client::quit()
  194.      */
  195.     function isConnected()
  196.     {
  197.         return parent::isConnected();
  198.     }
  199.  
  200.     // }}}
  201.     // {{{ selectGroup()
  202.  
  203.     /**
  204.      * Selects a newsgroup
  205.      *
  206.      * @param string $newsgroup Newsgroup name
  207.      *
  208.      * @return mixed (array) Info about the newsgroup on success or (object) pear_error on failure
  209.      * @access public
  210.      * @see Net_NNTP_Client::group()
  211.      * @see Net_NNTP_Client::first()
  212.      * @see Net_NNTP_Client::last()
  213.      * @see Net_NNTP_Client::count()
  214.      * @see Net_NNTP_Client::getGroups()
  215.      */
  216.     function selectGroup($newsgroup)
  217.     {
  218.         $response_arr $this->cmdGroup($newsgroup);
  219.         if (PEAR::isError($response_arr)) {
  220.             return $response_arr;
  221.         }
  222.  
  223.         // Store group info in the object
  224.         $this->_currentGroup $response_arr;
  225.  
  226.         return $response_arr;
  227.     }
  228.  
  229.     // }}}
  230.     // {{{ getGroups()
  231.  
  232.     /**
  233.      * Fetches a list of all avaible newsgroups
  234.      *
  235.      * @return mixed (array) nested array with informations about existing newsgroups on success or (object) pear_error on failure
  236.      * @access public
  237.      * @see Net_NNTP_Client::selectGroup()
  238.      * @see Net_NNTP_Client::getDescriptions()
  239.      */
  240.     function getGroups()
  241.     {
  242.         // Get groups
  243.         $groups $this->cmdList();
  244.         if (PEAR::isError($groups)) {
  245.             return $groups;
  246.         }
  247.  
  248.         return $groups;
  249.     }
  250.  
  251.     // }}}
  252.     // {{{ getDescriptions()
  253.  
  254.     /**
  255.      * Fetches a list of all avaible newsgroup descriptions.
  256.      *
  257.      * @return mixed (array) nested array with description of existing newsgroups on success or (object) pear_error on failure
  258.      * @access public
  259.      * @see Net_NNTP_Client::getGroups()
  260.      */
  261.     function getDescriptions()
  262.     {
  263.         // Get group descriptions
  264.         $descriptions $this->cmdListNewsgroups();
  265.         if (PEAR::isError($descriptions)) {
  266.             return $descriptions;
  267.         }
  268.     
  269.         return $descriptions;
  270.     }
  271.  
  272.     // }}}
  273.     // {{{ getOverview()
  274.  
  275.     /**
  276.      * Fetch message header fields from message number $first to $last
  277.      *
  278.      * The format of the returned array is:
  279.      * $messages[message_id][header_name]
  280.      *
  281.      * @param integer $first first article to fetch
  282.      * @param integer $last  last article to fetch
  283.      *
  284.      * @return mixed (array) nested array of message and their headers on success or (object) pear_error on failure
  285.      * @access public
  286.      * @see Net_NNTP_Client::getOverviewFormat()
  287.      * @see Net_NNTP_Client::getReferencesOverview()
  288.      */
  289.     function getOverview($first$last)
  290.     {
  291.         $overview $this->cmdXOver($first.'-'.$last);
  292.         if (PEAR::isError($overview)) {
  293.             return $overview;
  294.         }
  295.     
  296.         return $overview;
  297.     }
  298.  
  299.     // }}}
  300.     // {{{ getOverviewFmt()
  301.  
  302.     /**
  303.      * Returns a list of avaible headers which are send from NNTP-server to the client for every news message
  304.      *
  305.      * @return mixed (array) header names on success or (object) pear_error on failure
  306.      * @access public
  307.      * @see Net_NNTP_Client::getOverview()
  308.      */
  309.     function getOverviewFormat()
  310.     {
  311.         return $this->cmdListOverviewFmt();
  312.     }
  313.  
  314.     // }}}
  315.     // {{{ getReferencesOverview()
  316.  
  317.     /**
  318.      * Fetch a list of each message's reference header.
  319.      *
  320.      * @param integer $first first article to fetch
  321.      * @param integer $last  last article to fetch
  322.      *
  323.      * @return mixed (array) nested array of references on success or (object) pear_error on failure
  324.      * @access public
  325.      * @see Net_NNTP_Client::getOverview()
  326.      */
  327.     function getReferencesOverview($first$last)
  328.     {
  329.         $overview $this->cmdXROver($first.'-'.$last);
  330.         if (PEAR::isError($overview)) {
  331.             return $overview;
  332.         }
  333.     
  334.         return $overview;
  335.     }
  336.  
  337.     // }}}
  338.     // {{{ post()
  339.  
  340.     /**
  341.      * Post an article to a number of newsgroups.
  342.      *
  343.      * (Among the aditional headers you might think of adding could be:
  344.      * "NNTP-Posting-Host: <ip-of-author>", which should contain the IP-address
  345.      * of the author of the post, so the message can be traced back to him.
  346.      * Or "Organization: <org>" which contain the name of the organization
  347.      * the post originates from)
  348.      *
  349.      * @param string $newsgroups The newsgroup to post to.
  350.      * @param string $subject The subject of the post.
  351.      * @param string $body The body of the post itself.
  352.      * @param string $from Name + email-adress of sender.
  353.      * @param optional string $aditional Aditional headers to send.
  354.      *
  355.      * @return mixed (string) server response on success or (object) pear_error on failure
  356.      * @access public
  357.      */
  358.     function post($newsgroups$subject$body$from$aditional = null)
  359.     {
  360.         return $this->cmdPost($newsgroups$subject$body$from$aditional);
  361.     }
  362.  
  363.     // }}}
  364.     // {{{ getArticleRaw()
  365.  
  366.     /**
  367.      * Get a article (raw data)
  368.      *
  369.      * @param mixed $article Either the message-id or the message-number on the server of the article to fetch.
  370.      * @param optional bool  $implode When true the result array is imploded to a string, defaults to false.
  371.      *
  372.      * @return mixed (array/string) The article on success or (object) pear_error on failure
  373.      * @access public
  374.      * @see Net_NNTP_Client::getArticle()
  375.      * @see Net_NNTP_Client::getHeaderRaw()
  376.      * @see Net_NNTP_Client::getBodyRaw()
  377.      */
  378.     function getArticleRaw($article$implode = false)
  379.     {
  380.         $data $this->cmdArticle($article);
  381.         if (PEAR::isError($data)) {
  382.             return $data;
  383.         }
  384.  
  385.         if ($implode == true{
  386.             $data implode("\r\n"$data);
  387.         }
  388.  
  389.         return $data;
  390.     }
  391.  
  392.     // }}}
  393.     // {{{ getHeaderRaw()
  394.  
  395.     /**
  396.      * Get the header of an article (raw data)
  397.      *
  398.      * @param mixed $article Either the (string) message-id or the (int) message-number on the server of the article to fetch.
  399.      * @param optional bool $implode When true the result array is imploded to a string, defaults to false.
  400.      *
  401.      * @return mixed (array/string) header fields on success or (object) pear_error on failure
  402.      * @access public
  403.      * @see Net_NNTP_Client::getHeader()
  404.      * @see Net_NNTP_Client::getArticleRaw()
  405.      * @see Net_NNTP_Client::getBodyRaw()
  406.      */
  407.     function getHeaderRaw($article$implode = false)
  408.     {
  409.         $data $this->cmdHead($article);
  410.         if (PEAR::isError($data)) {
  411.             return $data;
  412.         }
  413.  
  414.         if ($implode == true{
  415.             $data implode("\r\n"$data);
  416.         }
  417.  
  418.         return $data;
  419.     }
  420.  
  421.     // }}}
  422.     // {{{ getBody()
  423.  
  424.     // Not written yet...
  425.  
  426.     // }}}
  427.     // {{{ getBodyRaw()
  428.  
  429.     /**
  430.      * Get the body of an article (raw data)
  431.      *
  432.      * @param mixed $article Either the message-id or the message-number on the server of the article to fetch.
  433.      * @param optional bool $implode When true the result array is imploded to a string, defaults to false.
  434.      *
  435.      * @return mixed (array/string) body on success or (object) pear_error on failure
  436.      * @access public
  437.      * @see Net_NNTP_Client::getBody()
  438.      * @see Net_NNTP_Client::getHeaderRaw()
  439.      * @see Net_NNTP_Client::getArticleRaw()
  440.      */
  441.     function getBodyRaw($article$implode = false)
  442.     {
  443.         $data $this->cmdBody($article);
  444.         if (PEAR::isError($data)) {
  445.             return $data;
  446.         }
  447.     
  448.         if ($implode == true{
  449.             $data implode("\r\n"$data);
  450.         }
  451.     
  452.         return $data;
  453.     }
  454.  
  455.     // }}}
  456.     // {{{ getGroupArticles()
  457.  
  458.     /**
  459.      * Experimental
  460.      *
  461.      * @access public
  462.      * @since 0.3
  463.      */
  464.     function getGroupArticles($newsgroup)
  465.     {
  466.         return $this->cmdListgroup($newsgroup);
  467.     }
  468.  
  469.     // }}}
  470.     // {{{ getNewGroups()
  471.  
  472.     /**
  473.      * Experimental
  474.      *
  475.      * @access public
  476.      * @since 0.3
  477.      */
  478.     function getNewGroups($time$distributions = null)
  479.     {
  480.         switch (gettype($time)) {
  481.             case 'integer':
  482.                 break;
  483.             case 'string':
  484.                 $time = (int) strtotime($time);
  485.                 break;
  486.             default:
  487.                 return PEAR::throwError('UPS...');
  488.         }
  489.  
  490.         return $this->cmdNewgroups($time$distributions);
  491.     }
  492.  
  493.     // }}}
  494.     // {{{ getNewNews()
  495.  
  496.     /**
  497.      * Experimental
  498.      *
  499.      * @access public
  500.      * @since 0.3
  501.      */
  502.     function getNewNews($time$newsgroups '*')
  503.     {
  504.         switch (gettype($time)) {
  505.             case 'integer':
  506.                 break;
  507.             case 'string':
  508.                 $time = (int) strtotime($time);
  509.                 break;
  510.             default:
  511.                 return PEAR::throwError('UPS...');
  512.         }
  513.  
  514.         return $this->cmdNewnews($time$newsgroups);
  515.     }
  516.  
  517.     // }}}
  518.     // {{{ getDate()
  519.  
  520.     /**
  521.      * Get the NNTP-server's internal date
  522.      *
  523.      * Get the date from the newsserver format of returned date:
  524.      *
  525.      * @param optional int $format
  526.      *   - 0: $date - timestamp
  527.      *   - 1: $date['y'] - year
  528.      *        $date['m'] - month
  529.      *        $date['d'] - day
  530.      *
  531.      * @return mixed (mixed) date on success or (object) pear_error on failure
  532.      * @access public
  533.      * @since 0.3
  534.      */
  535.     function getDate($format = 1)
  536.     {
  537.         $date $this->cmdDate();
  538.         if (PEAR::isError($date)) {
  539.             return $date;
  540.         }
  541.  
  542.         switch ($format{
  543.             case 1:
  544.                 return array('y' => substr($date04)'m' => substr($date42)'d' => substr($date62));
  545.                 break;
  546.  
  547.             case 0:
  548.             default:
  549.                 return $date;
  550.                 break;
  551.         }
  552.     }
  553.  
  554.     // }}}
  555.     // {{{ count()
  556.  
  557.     /**
  558.      * Number of articles in currently selected group
  559.      *
  560.      * @return integer number of article in group
  561.      * @access public
  562.      * @since 0.3
  563.      * @see Net_NNTP_Client::group()
  564.      * @see Net_NNTP_Client::first()
  565.      * @see Net_NNTP_Client::last()
  566.      * @see Net_NNTP_Client::selectGroup()
  567.      */
  568.     function count()
  569.     {
  570.         return $this->_currentGroup['count'];
  571.     }
  572.  
  573.     // }}}
  574.     // {{{ last()
  575.  
  576.     /**
  577.      * Maximum article number in currently selected group
  578.      *
  579.      * @return integer number of last article
  580.      * @access public
  581.      * @since 0.3
  582.      * @see Net_NNTP_Client::first()
  583.      * @see Net_NNTP_Client::group()
  584.      * @see Net_NNTP_Client::count()
  585.      * @see Net_NNTP_Client::selectGroup()
  586.      */
  587.     function last()
  588.     {
  589.         return $this->_currentGroup['last'];
  590.     }
  591.  
  592.     // }}}
  593.     // {{{ first()
  594.  
  595.     /**
  596.      * Minimum article number in currently selected group
  597.      *
  598.      * @return integer number of first article
  599.      * @access public
  600.      * @since 0.3
  601.      * @see Net_NNTP_Client::last()
  602.      * @see Net_NNTP_Client::group()
  603.      * @see Net_NNTP_Client::count()
  604.      * @see Net_NNTP_Client::selectGroup()
  605.      */
  606.     function first()
  607.     {
  608.         return $this->_currentGroup['first'];
  609.     }
  610.  
  611.     // }}}
  612.     // {{{ group()
  613.  
  614.     /**
  615.      * Currently selected group
  616.      *
  617.      * @return string group name
  618.      * @access public
  619.      * @since 0.3
  620.      * @see Net_NNTP_Client::first()
  621.      * @see Net_NNTP_Client::last()
  622.      * @see Net_NNTP_Client::count()
  623.      * @see Net_NNTP_Client::selectGroup()
  624.      */
  625.     function group()
  626.     {
  627.         return $this->_currentGroup['group'];
  628.     }
  629.  
  630.     // }}}
  631. }
  632.  
  633. // }}}
  634.  
  635. ?>

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