public function getArticle($msgId) { SpotDebug::msg(SpotDebug::TRACE, __CLASS__ . "->getArticle(" . $msgId . ")"); $this->connect(); $result = array('header' => array(), 'body' => array()); try { $this->registerTryCommand(); # Fetch the article $art = $this->_nntp->getArticle($msgId); } catch (Exception $x) { $this->registerError($x); echo PHP_EOL . 'getArticle(): Failed to retrieve article: ' . $msgId . PHP_EOL; /** * Try this operation again, but make sure we are not overloading * the NNTP server with useless requests */ if ($this->tooManyErrors()) { throw $x; } else { return $this->getArticle($msgId); } # else } # catch /* * Now we will split it in both a body and an array, this * way it is much easier to work with */ $i = 0; $lnCount = count($art); while ($i < $lnCount && $art[$i] != '') { $result['header'][] = $art[$i]; $i++; } # while $i++; while ($i < $lnCount) { $result['body'][] = $art[$i]; $i++; } # while return $result; }
/** * Download a full article, the body and the header, return an array with named keys and their * associated values, optionally decode the body using yEnc. * * @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. * @param bool $yEnc Attempt to yEnc decode the body. * * @return mixed On success : (array) The article. * On failure : (object) PEAR_Error. * * @access public */ public function get_Article($groupName, $identifier, $yEnc = false) { $connected = $this->_checkConnection(); if ($connected !== true) { return $connected; } // 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_NOTICE); } return $summary; } } // Check if it's an article number or message-id. if (!is_numeric($identifier)) { // If it's a message-id, check if it has the required triangular brackets. $identifier = $this->_formatMessageID($identifier); } // Download the article. $article = parent::getArticle($identifier); // If there was an error downloading the article, return a PEAR error object. if ($this->isError($article)) { if ($this->_debugBool) { $this->_debugging->log(get_class(), __FUNCTION__, $article->getMessage(), Logger::LOG_NOTICE); } return $article; } $ret = $article; // Make sure the article is an array and has more than 1 element. if (count($article) > 0) { $ret = []; $body = ''; $emptyLine = false; foreach ($article as $line) { // If we found the empty line it means we are done reading the header and we will start reading the body. if (!$emptyLine) { if ($line === '') { $emptyLine = True; continue; } // Use the line type of the article as the array key (From, Subject, etc..). if (preg_match('/([A-Z-]+?): (.*)/i', $line, $matches)) { // If the line type takes more than 1 line, append the rest of the content to the same key. if (array_key_exists($matches[1], $ret)) { $ret[$matches[1]] .= $matches[2]; } else { $ret[$matches[1]] = $matches[2]; } } // Now we have the header, so get the body from the rest of the lines. } else { $body .= $line; } } // Finally we decode the message using yEnc. $ret['Message'] = $yEnc ? $this->_decodeIgnoreYEnc($body) : $body; } return $ret; }