Add file time/speed; make URLs configurable in Wordpress
[ambdownload.git] / ambdownload.php
index c32d715..139bd64 100644 (file)
@@ -10,14 +10,17 @@ Author URI: http://blog.alex.org.uk
 \r
 class ambdownload\r
 {\r
-       function getDownloadLink($user, $file="default")\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
-               $link = "http://server.example.com/download?";\r
-               return $link.sprintf("id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash);\r
+               return $link.sprintf("?id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash);\r
        }\r
 \r
        function downloadRedirect()\r
@@ -25,11 +28,16 @@ class ambdownload
                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
-                       if ($download_file) {  \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_file));\r
+                               wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_url, $download_error, $download_file));\r
                                exit;\r
                        }\r
                }\r