\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