Example #1
0
 /**
  * Download an article body (an article without the header).
  *
  * @param string $groupName The name of the group the article is in.
  * @param mixed $identifier (string) The message-id of the article to download.
  *                          (int)    The article number.
  *
  * @return mixed On success : (string) The article's body.
  *               On failure : (object) PEAR_Error.
  *
  * @access protected
  */
 protected function _getMessage($groupName, $identifier)
 {
     // Make sure the requested group is already selected, if not select it.
     if (parent::group() !== $groupName) {
         // Select the group.
         $summary = $this->selectGroup($groupName);
         // If there was an error selecting the group, return PEAR error object.
         if ($this->isError($summary)) {
             if ($this->_debugBool) {
                 $this->_debugging->log(get_class(), __FUNCTION__, $summary->getMessage(), Logger::LOG_WARNING);
             }
             return $summary;
         }
     }
     // Check if this is an article number or message-id.
     if (!is_numeric($identifier)) {
         // It's a message-id so check if it has the triangular brackets.
         $identifier = $this->_formatMessageID($identifier);
     }
     // Tell the news server we want the body of an article.
     $response = $this->_sendCommand('BODY ' . $identifier);
     if ($this->isError($response)) {
         return $response;
     }
     $body = '';
     switch ($response) {
         // 222, RFC977: 'n <a> article retrieved - body follows'
         case NET_NNTP_PROTOCOL_RESPONSECODE_BODY_FOLLOWS:
             // Continue until connection is lost
             while (!feof($this->_socket)) {
                 // Retrieve and append up to 1024 characters from the server.
                 $line = fgets($this->_socket, 1024);
                 // If the socket is empty/ an error occurs, false is returned.
                 // Since the socket is blocking, the socket should not be empty, so it's definitely an error.
                 if ($line === false) {
                     return $this->throwError('Failed to read line from socket.', null);
                 }
                 // Check if the line terminates the text response.
                 if ($line === ".\r\n") {
                     if ($this->_debugBool) {
                         $this->_debugging->log(get_class(), __FUNCTION__, 'Fetched body for article ' . $identifier, Logger::LOG_INFO);
                     }
                     // Attempt to yEnc decode and return the body.
                     return $this->_decodeIgnoreYEnc($body);
                 }
                 // Check for line that starts with double period, remove one.
                 if ($line[0] === '.' && $line[1] === '.') {
                     $line = substr($line, 1);
                 }
                 // Add the line to the rest of the lines.
                 $body .= $line;
             }
             return $this->throwError('End of stream! Connection lost?', null);
         default:
             return $this->_handleErrorResponse($response);
     }
 }