Commit:    3579c9d6ae4200abfc712a99a69b6c562f72a72a
Author:    Ben Ramsey <[email protected]>         Sat, 23 Jan 2016 00:39:00 
-0500
Parents:   151949772442ed3b9a1b5202a000ae88de1bc468
Branches:  master

Link:       
http://git.php.net/?p=web/news.git;a=commitdiff;h=3579c9d6ae4200abfc712a99a69b6c562f72a72a

Log:
Use the new Nntp and fMailbox classes to download attachments

Changed paths:
  M  getpart.php


Diff:
diff --git a/getpart.php b/getpart.php
index 815b4cd..986d04e 100644
--- a/getpart.php
+++ b/getpart.php
@@ -20,134 +20,40 @@ if (isset($_GET['part'])) {
        error("No part specified");
 }
 
-$s = nntp_connect(NNTP_HOST);
-if (!$s) {
-       error("Failed to connect to news server");
-}
+try {
+       $nntpClient = new \Web\News\Nntp(NNTP_HOST);
+       $message = $nntpClient->readArticle($article, $group);
 
-if ($group) {
-       $res = nntp_cmd($s,"GROUP $group",211);
-       if (!$res) {
-               error("Failed to select group");
+       if ($message === null) {
+               error('No article found');
        }
-}
 
-$res = nntp_cmd($s, "ARTICLE $article",220);
-if (!$res) {
-       error("Failed to get article ". $article);
+       $mail = fMailbox::parseMessage($message);
+} catch (Exception $e) {
+       error($e->getMessage());
 }
 
-$emit = false;
-
-$inheaders = 1; $headers = array();
-$boundary = $charset = $encoding = "";
-$mimecount = 0; // number of mime parts
-$boundaries = array();
-$lk = '';
-while (!feof($s)) {
-       $line = fgets($s, 4096);
-       if ($line == ".\r\n") {
-               break;
-       }
-       if ($inheaders && ($line == "\n" || $line == "\r\n")) {
-               $inheaders = 0;
-               if (!empty($headers['content-type'])
-               && preg_match("/charset=(\"|'|)(.+)\\1/is", 
$headers['content-type'], $m)) {
-                       $charset = trim($m[2]);
-               }
-               if (!empty($headers['content-type'])
-               && preg_match("/boundary=(\"|'|)(.+)\\1/is", 
$headers['content-type'], $m)) {
-                       $boundaries[] = trim($m[2]);
-                       $boundary = end($boundaries);
-               }
-               if (!empty($headers['content-type'])
-               && preg_match("/([^;]+)(;|\$)/", $headers['content-type'], $m)) 
{
-                       ++$mimecount;
-               }
-
-               $emit = ($mimecount == $part);
+if (!empty($mail['attachment'][$part])) {
+       $attachment = $mail['attachment'][$part];
 
-               if (!empty($headers['content-transfer-encoding'])) {
-                       $encoding = 
strtolower(trim($headers['content-transfer-encoding']));
-               }
-               if ($emit) {
-                       /* check if content-type exist is made above */
-                       header('Content-Type: ' . $headers['content-type']);
-                       /* Do not rely on user-provided content-deposition 
header, generate own one to */
-                       /* make the content downloadable, do NOT use inline, we 
can't trust the attachment*/
-                       /* Downside of this approach: images should be 
downloaded before use */
-                       /* this is safer though, and prevents doing evil things 
on php.net domain */
-                       $contentdisposition = 'attachment';
-                       if (isset($headers['content-disposition'])
-                       && preg_match('/filename=([\'"]?).+?\1/', 
$headers['content-disposition'], $m)) {
-                               $contentdisposition .= '; ' . $m[0];
-                       }
-                       header('Content-Disposition: ' . $contentdisposition);
-                       // if (isset($headers['content-disposition'])) {
-                       //      header('Content-Disposition: ' . 
$headers['content-disposition']);
-                       //}
-                       if (isset($headers['content-description'])) {
-                               header('Content-Description: ' . 
$headers['content-description']);
-                       }
-               }
+       /* Do not rely on user-provided content-deposition header, generate own 
one to */
+       /* make the content downloadable, do NOT use inline, we can't trust the 
attachment*/
+       /* Downside of this approach: images should be downloaded before use */
+       /* this is safer though, and prevents doing evil things on php.net 
domain */
+       $contentdisposition = 'attachment';
 
-               continue;
-       }
-       # fix lines that started with a period and got escaped
-       if (substr($line,0,2) == "..") {
-               $line = substr($line,1);
+       if (!empty($attachment['filename'])) {
+               $contentdisposition .= '; filename="' . $attachment['filename'] 
. '"';
        }
-       if ($inheaders) {
-               /* header fields can be split across lines: CRLF WSP where WSP 
*/
-               /* is a space (ASCII 32) or tab (ASCII 9) */
-               if ($lk && ($line[0] == ' ' || $line[0] == "\t")) {
-                       $headers[$lk] .= $line;
-               } else {
-                       @list($k,$v) = explode(": ", $line, 2);
-                       if ($k && $v) {
-                               $headers[strtolower($k)] = $v;
-                               $lk = strtolower($k);
-                       } // else not a header field
-               }
-       } else {
-
-               if ($boundary
-               && substr($line,0,2) == '--'
-               && substr($line,2,strlen($boundary)) == $boundary) {
-
-                       $inheaders = 1;
 
-                       if (substr($line,2+strlen($boundary)) == '--') {
-                               # end of this container
-                               array_pop($boundaries);
-                               $boundary = end($boundaries);
-                       } else {
-                               /* next section starts with no headers */
-                               $headers = null;
-                       }
+       header('Content-Type: ' . $attachment['mimetype']);
+       header('Content-Disposition: ' . $contentdisposition);
 
-                       continue;
-               }
-
-               if (!$emit) {
-                       continue;
-               }
-
-               switch($encoding) {
-                       case "quoted-printable":
-                       $line = quoted_printable_decode($line);
-                       break;
-                       case "base64":
-                       $line = base64_decode($line);
-                       break;
-               }
-
-               echo $line;
-
-               /* done with attachment, no need to continue */
-               if ($emit) {
-                       break;
-               }
+       if (isset($attachment['description'])) {
+               header('Content-Description: ' . $attachment['description']);
        }
-}
 
+       echo $attachment['data'];
+} else {
+       error('Part not found');
+}


--
PHP Webmaster List Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to