Add domd5sums.pl
[ambdownload.git] / ambdownload.php
1 <?php\r
2 /*\r
3 Plugin Name: ambdownload\r
4 Plugin URI: http://blog.alex.org.uk/\r
5 Description: Redirect a given page to a download URL\r
6 Version: 2.2\r
7 Author: Alex Bligh\r
8 Author URI: http://blog.alex.org.uk\r
9 */\r
10 \r
11 class ambdownload\r
12 {\r
13   function getDownloadLink($user, $link, $error, $file="default")\r
14   {\r
15     if (empty($user) && !empty($error))\r
16       {\r
17         return ($error);\r
18       }\r
19     $time = time();\r
20     $secret = rtrim(file_get_contents("/etc/apache2/download.secret"));\r
21     $id = $user;\r
22     $hash = hash("sha256",$time.":".$id.":".$file.":".$secret);\r
23     return $link.sprintf("?id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash);\r
24   }\r
25 \r
26   function downloadRedirect()\r
27   {     \r
28     global $post;\r
29     if ((is_single() || is_singular() || is_page()))\r
30       {\r
31         /* Tag of file to download, e.g. 'default' */\r
32         $download_file = get_post_meta($post->ID, 'download_file', true);  \r
33         /* EG 'http://server.example.com/download' */\r
34         $download_url = get_post_meta($post->ID, 'download_url', true);  \r
35         /* EG URL where redirected if no username exists */\r
36         $download_error = get_post_meta($post->ID, 'download_error', true);  \r
37         if ($download_file && $download_url) {  \r
38           global $current_user;\r
39           get_currentuserinfo();\r
40           wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_url, $download_error, $download_file));\r
41           exit;\r
42         }\r
43       }\r
44   }\r
45 }\r
46 \r
47 function ambdownloadinfo_friendlyfilesize ($size, $dp=0)\r
48 {\r
49   $suffix = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');\r
50   for ($n=0;\r
51        $size > 1024 && $n<count($suffix)-1;\r
52        $n++)\r
53     $size /= 1024;\r
54   return sprintf("%.".$dp."f%s",$size,$suffix[$n]);\r
55 }\r
56 \r
57 // Implements [ambdownloadinfo url file='FILENAME'dp='DECIMALPLACES']FORMAT[/ambdownloadinfo]\r
58 // Format is like printf, but\r
59 //    $NAME is the name of the file\r
60 //    $SIZE is the size of the file in bytes\r
61 //    $MD5SUM is the md5sum of the file\r
62 //    $FSIZE is the size of the file in friendly notation\r
63 //\r
64 // Example usage (to be entered in the HTML editor)\r
65 //    The file is [ambdownloadinfo url='http://www.example.com/download' file='default']named <b>$NAME</b> has an\r
66 //    md5sum of <tt>$MD5SUM</tt> and is roughly $FSIZE in size[/ambdownloadinfo]\r
67 \r
68 function ambdownloadinfo_shortcode ($atts, $content = null)\r
69 {\r
70   extract( shortcode_atts( array(\r
71                                  'url' => null,\r
72                                  'file' => 'default',\r
73                                  'dp' => 0,\r
74                                  ), $atts ) );\r
75   if (empty($url))\r
76     return "";\r
77   $info = file_get_contents($url."?info=".$file);\r
78   if (empty($info))\r
79       return "";\r
80   if (empty($content))\r
81     $content='$NAME ($FSIZE)';\r
82   $items=explode(" ", $info);\r
83   $name = $items[0];\r
84   $size = $items[1];\r
85   $md5sum = $items[2];\r
86   // replace percentage signs\r
87   $content = preg_replace('/%/','%%',$content);\r
88   $content = preg_replace('/\$NAME\b/','%1$s',$content);\r
89   $content = preg_replace('/\$SIZE\b/','%2$s',$content);\r
90   $content = preg_replace('/\$MD5SUM\b/','%3$s',$content);\r
91   $content = preg_replace('/\$FSIZE\b/','%4$s',$content);\r
92   $friendlysize = ambdownloadinfo_friendlyfilesize($size, $dp);\r
93   return sprintf($content, $name, $size, $md5sum, $friendlysize);\r
94 }\r
95 \r
96 add_shortcode( 'ambdownloadinfo', 'ambdownloadinfo_shortcode' );\r
97 add_action( 'template_redirect', array('ambdownload', 'downloadRedirect'), 1, 2);\r
98 \r
99 \r
100 \r