From: Alex Bligh Date: Sat, 1 Sep 2012 18:45:23 +0000 (+0100) Subject: Reindent; add info function and short code X-Git-Url: http://git.alex.org.uk Reindent; add info function and short code --- diff --git a/README b/README index f1f67b3..e9a53a7 100644 --- 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 $NAME has an + md5sum of $MD5SUM and is roughly $FSIZE in size[/ambdownloadinfo] diff --git a/ambdownload.php b/ambdownload.php index 139bd64..08d4cc7 100644 --- a/ambdownload.php +++ b/ambdownload.php @@ -10,41 +10,90 @@ Author URI: http://blog.alex.org.uk class ambdownload { - function getDownloadLink($user, $link, $error, $file="default") - { - if (empty($user) && !empty($error)) - { - return ($error); - } - $time = time(); - $secret = rtrim(file_get_contents("/etc/apache2/download.secret")); - $id = $user; - $hash = hash("sha256",$time.":".$id.":".$file.":".$secret); - return $link.sprintf("?id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash); - } + function getDownloadLink($user, $link, $error, $file="default") + { + if (empty($user) && !empty($error)) + { + return ($error); + } + $time = time(); + $secret = rtrim(file_get_contents("/etc/apache2/download.secret")); + $id = $user; + $hash = hash("sha256",$time.":".$id.":".$file.":".$secret); + return $link.sprintf("?id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash); + } - function downloadRedirect() - { - global $post; - if ((is_single() || is_singular() || is_page())) - { - /* Tag of file to download, e.g. 'default' */ - $download_file = get_post_meta($post->ID, 'download_file', true); - /* EG 'http://server.example.com/download' */ - $download_url = get_post_meta($post->ID, 'download_url', true); - /* EG URL where redirected if no username exists */ - $download_error = get_post_meta($post->ID, 'download_error', true); - if ($download_file && $download_url) { - global $current_user; - get_currentuserinfo(); - wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_url, $download_error, $download_file)); - exit; - } - } + function downloadRedirect() + { + global $post; + if ((is_single() || is_singular() || is_page())) + { + /* Tag of file to download, e.g. 'default' */ + $download_file = get_post_meta($post->ID, 'download_file', true); + /* EG 'http://server.example.com/download' */ + $download_url = get_post_meta($post->ID, 'download_url', true); + /* EG URL where redirected if no username exists */ + $download_error = get_post_meta($post->ID, 'download_error', true); + if ($download_file && $download_url) { + global $current_user; + get_currentuserinfo(); + wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_url, $download_error, $download_file)); + exit; } + } + } +} + +function ambdownloadinfo_friendlyfilesize ($size, $dp=0) +{ + $suffix = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); + for ($n=0; + $size > 1024 && $n$NAME has an +// md5sum of $MD5SUM and is roughly $FSIZE in size[/ambdownloadinfo] + +function ambdownloadinfo_shortcode ($atts, $content = null) +{ + extract( shortcode_atts( array( + 'url' => null, + 'file' => 'default', + 'dp' => 0, + ), $atts ) ); + if (empty($url)) + return ""; + $info = file_get_contents($url."?info=".$file); + if (empty($info)) + return ""; + if (empty($content)) + $content='$NAME ($FSIZE)'; + $items=explode(" ", $info); + $name = $items[0]; + $size = $items[1]; + $md5sum = $items[2]; + // replace percentage signs + $content = preg_replace('/%/','%%',$content); + $content = preg_replace('/\$NAME\b/','%1$s',$content); + $content = preg_replace('/\$SIZE\b/','%2$s',$content); + $content = preg_replace('/\$MD5SUM\b/','%3$s',$content); + $content = preg_replace('/\$FSIZE\b/','%4$s',$content); + $friendlysize = ambdownloadinfo_friendlyfilesize($size, $dp); + return sprintf($content, $name, $size, $md5sum, $friendlysize); +} +add_shortcode( 'ambdownloadinfo', 'ambdownloadinfo_shortcode' ); add_action( 'template_redirect', array('ambdownload', 'downloadRedirect'), 1, 2); diff --git a/download.pl b/download.pl index 859d5d8..3f4c2ad 100755 --- a/download.pl +++ b/download.pl @@ -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);