X-Git-Url: http://git.alex.org.uk diff --git a/ambdownload.php b/ambdownload.php index c32d715..08d4cc7 100644 --- a/ambdownload.php +++ b/ambdownload.php @@ -10,33 +10,90 @@ Author URI: http://blog.alex.org.uk class ambdownload { - function getDownloadLink($user, $file="default") - { - $time = time(); - $secret = rtrim(file_get_contents("/etc/apache2/download.secret")); - $id = $user; - $hash = hash("sha256",$time.":".$id.":".$file.":".$secret); - $link = "http://server.example.com/download?"; - 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())) - { - $download_file = get_post_meta($post->ID, 'download_file', true); - if ($download_file) { - global $current_user; - get_currentuserinfo(); - wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $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);