Reindent; add info function and short code
authorAlex Bligh <alex@alex.org.uk>
Sat, 1 Sep 2012 18:45:23 +0000 (19:45 +0100)
committerAlex Bligh <alex@alex.org.uk>
Sat, 1 Sep 2012 18:45:23 +0000 (19:45 +0100)
README
ambdownload.php
download.pl

diff --git a/README b/README
index f1f67b3..e9a53a7 100644 (file)
--- a/README
+++ b/README
@@ -95,4 +95,17 @@ Usually this will do
 
 You will need to do your own log rotation.
 
+You can include information about each download by using a shortcode:
+
+[ambdownloadinfo url='URL' file='FILENAME' dp='DECIMALPLACES']FORMAT[/ambdownloadinfo]
+
+FORMAT is like printf, but
+  $NAME represents the name of the file (per the target above)
+  $SIZE represents its size
+  $MD5SUM represents its md5sum (if expandedfile.md5sum exists)
+  $FSIZE represents the size written in a friendly manner
+
+Example usage (to be entered in the HTML editor)
+  The file is [ambdownloadinfo url='http://www.example.com/download' file='default']named <b>$NAME</b> has an
+  md5sum of <tt>$MD5SUM</tt> and is roughly $FSIZE in size[/ambdownloadinfo]
 
index 139bd64..08d4cc7 100644 (file)
@@ -10,41 +10,90 @@ Author URI: http://blog.alex.org.uk
 \r
 class ambdownload\r
 {\r
-       function getDownloadLink($user, $link, $error, $file="default")\r
-       {\r
-               if (empty($user) && !empty($error))\r
-               {\r
-                       return ($error);\r
-               }\r
-               $time = time();\r
-               $secret = rtrim(file_get_contents("/etc/apache2/download.secret"));\r
-               $id = $user;\r
-               $hash = hash("sha256",$time.":".$id.":".$file.":".$secret);\r
-               return $link.sprintf("?id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash);\r
-       }\r
+  function getDownloadLink($user, $link, $error, $file="default")\r
+  {\r
+    if (empty($user) && !empty($error))\r
+      {\r
+       return ($error);\r
+      }\r
+    $time = time();\r
+    $secret = rtrim(file_get_contents("/etc/apache2/download.secret"));\r
+    $id = $user;\r
+    $hash = hash("sha256",$time.":".$id.":".$file.":".$secret);\r
+    return $link.sprintf("?id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash);\r
+  }\r
 \r
-       function downloadRedirect()\r
-       {       \r
-               global $post;\r
-               if ((is_single() || is_singular() || is_page()))\r
-               {\r
-                       /* Tag of file to download, e.g. 'default' */\r
-                       $download_file = get_post_meta($post->ID, 'download_file', true);  \r
-                       /* EG 'http://server.example.com/download' */\r
-                       $download_url = get_post_meta($post->ID, 'download_url', true);  \r
-                       /* EG URL where redirected if no username exists */\r
-                       $download_error = get_post_meta($post->ID, 'download_error', true);  \r
-                       if ($download_file && $download_url) {  \r
-                               global $current_user;\r
-                               get_currentuserinfo();\r
-                               wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_url, $download_error, $download_file));\r
-                               exit;\r
-                       }\r
-               }\r
+  function downloadRedirect()\r
+  {    \r
+    global $post;\r
+    if ((is_single() || is_singular() || is_page()))\r
+      {\r
+       /* Tag of file to download, e.g. 'default' */\r
+       $download_file = get_post_meta($post->ID, 'download_file', true);  \r
+       /* EG 'http://server.example.com/download' */\r
+       $download_url = get_post_meta($post->ID, 'download_url', true);  \r
+       /* EG URL where redirected if no username exists */\r
+       $download_error = get_post_meta($post->ID, 'download_error', true);  \r
+       if ($download_file && $download_url) {  \r
+         global $current_user;\r
+         get_currentuserinfo();\r
+         wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_url, $download_error, $download_file));\r
+         exit;\r
        }\r
+      }\r
+  }\r
+}\r
+\r
+function ambdownloadinfo_friendlyfilesize ($size, $dp=0)\r
+{\r
+  $suffix = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');\r
+  for ($n=0;\r
+       $size > 1024 && $n<count($suffix)-1;\r
+       $n++)\r
+    $size /= 1024;\r
+  return sprintf("%.".$dp."f%s",$size,$suffix[$n]);\r
 }\r
 \r
+// Implements [ambdownloadinfo url file='FILENAME'dp='DECIMALPLACES']FORMAT[/ambdownloadinfo]\r
+// Format is like printf, but\r
+//    $NAME is the name of the file\r
+//    $SIZE is the size of the file in bytes\r
+//    $MD5SUM is the md5sum of the file\r
+//    $FSIZE is the size of the file in friendly notation\r
+//\r
+// Example usage (to be entered in the HTML editor)\r
+//    The file is [ambdownloadinfo url='http://www.example.com/download' file='default']named <b>$NAME</b> has an\r
+//    md5sum of <tt>$MD5SUM</tt> and is roughly $FSIZE in size[/ambdownloadinfo]\r
+\r
+function ambdownloadinfo_shortcode ($atts, $content = null)\r
+{\r
+  extract( shortcode_atts( array(\r
+                                'url' => null,\r
+                                'file' => 'default',\r
+                                'dp' => 0,\r
+                                ), $atts ) );\r
+  if (empty($url))\r
+    return "";\r
+  $info = file_get_contents($url."?info=".$file);\r
+  if (empty($info))\r
+      return "";\r
+  if (empty($content))\r
+    $content='$NAME ($FSIZE)';\r
+  $items=explode(" ", $info);\r
+  $name = $items[0];\r
+  $size = $items[1];\r
+  $md5sum = $items[2];\r
+  // replace percentage signs\r
+  $content = preg_replace('/%/','%%',$content);\r
+  $content = preg_replace('/\$NAME\b/','%1$s',$content);\r
+  $content = preg_replace('/\$SIZE\b/','%2$s',$content);\r
+  $content = preg_replace('/\$MD5SUM\b/','%3$s',$content);\r
+  $content = preg_replace('/\$FSIZE\b/','%4$s',$content);\r
+  $friendlysize = ambdownloadinfo_friendlyfilesize($size, $dp);\r
+  return sprintf($content, $name, $size, $md5sum, $friendlysize);\r
+}\r
 \r
+add_shortcode( 'ambdownloadinfo', 'ambdownloadinfo_shortcode' );\r
 add_action( 'template_redirect', array('ambdownload', 'downloadRedirect'), 1, 2);\r
 \r
 \r
index 859d5d8..3f4c2ad 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/perl
 
+use File::stat;
 use strict;
 use warnings;
 
@@ -11,6 +12,7 @@ use File::stat;
 use Digest::SHA qw(sha256_hex sha1);
 use MIME::Base64;
 use File::Spec;
+use File::Spec::Functions qw(rel2abs);
 use CGI;
 use HTML::Entities;
 use IO::Handle;
@@ -20,7 +22,7 @@ my $log;
 my $transaction="unknown";
 
 my $logfile = "/var/log/download.log";
-my $datadir = "/var/www/server.example.com/public_html/download/";
+my $datadir = dirname(rel2abs($0))."/";
 my $secretfile="/etc/apache2/download.secret";
 my $secret;
 my $sentheader = 0;
@@ -102,6 +104,14 @@ sub gethash
     return sha256_hex(shift @_);
 }
 
+sub getfile
+{
+    my $fn = $datadir.(shift @_);
+    $fn = File::Spec->rel2abs( readlink($fn) ) if (-l $fn);
+    qdie ("File not found") unless ( -f $fn);
+    return $fn;
+}
+
 sub decodeparams
 {
     my $query = CGI::url(-absolute=>1);
@@ -119,14 +129,40 @@ sub decodeparams
 
     my $hash = gethash($clienttime.":".$clientid.":".$clientfile.":".$secret);
     qdie ("Bad hash") unless ($hash eq $clienthash);
-    my $fn = $datadir.$clientfile;
-    $fn = File::Spec->rel2abs( readlink($fn) ) if (-l $fn);
-    qdie ("File not found") unless ( -f $fn);
+    my $fn = getfile($clientfile);
     $clientfile = basename ($fn);
     $transaction=$hash." ".$clientfile." ".$clientid;
     return $fn;
 }
 
+sub doinfo
+{
+    my $clientfile = shift @_;
+    my $fn = getfile($clientfile);
+    $clientfile = basename ($fn);
+    my $size = "unknown";
+    my $sb = stat($fn);
+    $size = $sb->size if (defined($sb) && defined($sb->size));
+    my $md5sum = "unknown";
+    my $md5fn = $fn.".md5sum";
+    if ( -r $md5fn )
+    {
+       my $md5;
+       open $md5, "<", $md5fn || qdie ("Can't read md5sum");
+       while (<$md5>)
+       {
+           chomp;
+           $md5sum = $1 if (/^([a-f0-9]+)\b/);
+       }
+       close $md5;
+    }
+    $sentheader = 1;
+    print CGI::header(
+       -type => 'text/plain' );
+
+    print "$clientfile $size $md5sum\n";
+}
+
 open (my $sfh, "<", $secretfile) || qdie("Can't open secret file $secretfile: $!");
 chomp($secret=join("",<$sfh>));
 close ($sfh);
@@ -140,6 +176,13 @@ if (!defined($ENV{DOCUMENT_ROOT}) && !defined($ENV{SERVER_NAME}))
 }
 else
 {
+    my $info = CGI::url_param('info');
+    if (defined($info))
+    {
+       doinfo($info);
+       exit 0;
+    }
+
     openlog;
     my $file = decodeparams;
     my $sb = stat($file);