cvs: php-news-web / article.php getpart.php

From: Date: Thu, 16 Jan 2003 04:24:54 +0000
Subject: cvs: php-news-web / article.php getpart.php
Groups: php.mirrors 
Request: Send a blank email to php-mirrors+get-15045@lists.php.net to get a copy of this message
wez Wed Jan 15 23:24:54 2003 EDT Added files: /php-news-web getpart.php Modified files: /php-news-web article.php Log: Implement simple link to download an attachment. Could do with some testing, particularly for binary files (I didn't have time to find any on news.php.net). Index: php-news-web/article.php diff -u php-news-web/article.php:1.20 php-news-web/article.php:1.21 --- php-news-web/article.php:1.20 Mon Nov 25 01:02:54 2002 +++ php-news-web/article.php Wed Jan 15 23:24:54 2003 @@ -20,6 +20,7 @@ $inheaders = 1; $headers = array(); $masterheaders = null; $mimetype = $boundary = $charset = $encoding = ""; +$mimecount = 0; // number of mime parts $boundaries = array(); $lk = ''; while (!feof($s)) { @@ -39,6 +40,7 @@ if ($headers['content-type'] && preg_match("/([^;]+)(;|\$)/", $headers['content-type'], $m)) { $mimetype = trim(strtolower($m[1])); + ++$mimecount; } if (!$started) { head("$group: ".format_title($headers[subject], $charset)); @@ -63,8 +65,15 @@ $description = trim($headers['content-description']) . " "; else $description = ""; - - echo "Attachment: [$mimetype] ${description}$name<br />\n"; + + $description .= $name; + $link_desc = "[$mimetype]"; + if (strlen($description)) + $link_desc .= " " . $description; + + $dl_link = "getpart.php?group=$group&article=$article&part=$mimecount"; + + echo "Attachment: <a target=\"_blank\" href=\"$dl_link\">${link_desc}</a><br />\n"; } if ($masterheaders == null) Index: php-news-web/getpart.php +++ php-news-web/getpart.php <?php // vim:sw=2:ts=2:et if (!$article) die("no article specified"); if (!$part) die("no part specified"); require 'common.inc'; require 'nntp.inc'; $s = nntp_connect("news.php.net") or die("failed to connect to news server"); if ($group) { $res = nntp_cmd($s,"GROUP $group",211) or die("failed to select group .".htmlspecialchars($group)); } $res = nntp_cmd($s, "ARTICLE $article",220) or die("failed to get article ".htmlspecialchars($article)); $emit = false; $inheaders = 1; $headers = array(); $masterheaders = null; $mimetype = $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 ($headers['content-type'] && preg_match("/charset=(\"|'|)(.+)\\1/is", $headers['content-type'], $m)) { $charset = trim($m[2]); } if ($headers['content-type'] && preg_match("/boundary=(\"|'|)(.+)\\1/is", $headers['content-type'], $m)) { $boundaries[] = trim($m[2]); $boundary = end($boundaries); } if ($headers['content-type'] && preg_match("/([^;]+)(;|\$)/", $headers['content-type'], $m)) { $mimetype = trim(strtolower($m[1])); ++$mimecount; } $emit = ($mimecount == $part); $encoding = strtolower(trim($headers['content-transfer-encoding'])); if ($emit) { if ($headers['content-type']) header('Content-Type: ' . $headers['content-type']); if ($headers['content-disposition']) header('Content-Disposition: ' . $headers['content-disposition']); if ($headers['content-description']) header('Content-Description: ' . $headers['content-description']); } if ($masterheaders == null) $headers = $masterheaders; continue; } # fix lines that started with a period and got escaped if (substr($line,0,2) == "..") $line = substr($line,1); if ($inheaders) { list($k,$v) = explode(": ", $line, 2); if ($k && $v) { $headers[strtolower($k)] = $v; $lk = strtolower($k); } else { $headers[$lk] .= $line; } } 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; start with an inherited set of headers $headers = $masterheaders; $mimetype = ""; } continue; } if (!$emit) continue; switch($encoding) { case "quoted-printable": $line = quoted_printable_decode($line); break; case "base64": $line = base64_decode($line); break; } echo $line; } }

« previous php.mirrors (#15045) next »