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

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