Initial commit
[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, $file="default")\r
14         {\r
15                 $time = time();\r
16                 $secret = rtrim(file_get_contents("/etc/apache2/download.secret"));\r
17                 $id = $user;\r
18                 $hash = hash("sha256",$time.":".$id.":".$file.":".$secret);\r
19                 $link = "http://server.example.com/download?";\r
20                 return $link.sprintf("id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash);\r
21         }\r
22 \r
23         function downloadRedirect()\r
24         {       \r
25                 global $post;\r
26                 if ((is_single() || is_singular() || is_page()))\r
27                 {\r
28                         $download_file = get_post_meta($post->ID, 'download_file', true);  \r
29                         if ($download_file) {  \r
30                                 global $current_user;\r
31                                 get_currentuserinfo();\r
32                                 wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_file));\r
33                                 exit;\r
34                         }\r
35                 }\r
36         }\r
37 }\r
38 \r
39 \r
40 add_action( 'template_redirect', array('ambdownload', 'downloadRedirect'), 1, 2);\r
41 \r
42 \r
43 \r