Skip to content
This repository
Browse code

Added function to get a temporary, secure download URL for private fi…

…les.
  • Loading branch information...
commit 11f46ec2714d34907009e37ad3b97f4421aefed3 1 parent bbe1aed
Brad Touesnard authored September 09, 2012

Showing 1 changed file with 55 additions and 0 deletions. Show diff stats Hide diff stats

  1. 55  wordpress-s3/class-plugin-public.php
55  wordpress-s3/class-plugin-public.php
@@ -36,4 +36,59 @@ function wp_get_attachment_url($url, $postID) {
36 36
 
37 37
         return $url;
38 38
     }
  39
+
  40
+
  41
+	/**
  42
+	* Generate a link to download a file from Amazon S3 using query string
  43
+	* authentication. This link is only valid for a limited amount of time.
  44
+	*
  45
+	* @param $bucket The name of the bucket in which the file is stored.
  46
+	* @param $filekey The key of the file, excluding the leading slash.
  47
+	* @param $expires The amount of time the link is valid (in seconds).
  48
+	* @param $operation The type of HTTP operation. Either GET or HEAD.
  49
+	*/
  50
+	function get_secure_attachment_url($postID, $expires = 900, $operation = 'GET') {
  51
+        if (!$this->options) $this->options = get_option('tantan_wordpress_s3');
  52
+
  53
+        if (
  54
+			!$this->options['wp-uploads'] || !$this->options['key'] || !$this->options['secret']
  55
+			|| !$this->options['bucket'] || !($amazon = get_post_meta($postID, 'amazonS3_info', true))
  56
+		) {
  57
+			return false;
  58
+		}
  59
+
  60
+		$accessDomain = $this->options['virtual-host'] ? $amazon['bucket'] : $amazon['bucket'].'.s3.amazonaws.com';
  61
+		
  62
+		$expire_time = time() + $expires;
  63
+		$filekey = rawurlencode($amazon['key']);
  64
+		$filekey = str_replace('%2F', '/', $filekey);
  65
+		$path = $amazon['bucket'] .'/'. $filekey;
  66
+
  67
+		/**
  68
+		* StringToSign = HTTP-VERB + "\n" +
  69
+		* Content-MD5 + "\n" +
  70
+		* Content-Type + "\n" +
  71
+		* Expires + "\n" +
  72
+		* CanonicalizedAmzHeaders +
  73
+		* CanonicalizedResource;
  74
+		*/
  75
+		
  76
+		$stringtosign =
  77
+			$operation ."\n". // type of HTTP request (GET/HEAD)
  78
+			"\n". // Content-MD5 is meaningless for GET
  79
+			"\n". // Content-Type is meaningless for GET
  80
+			$expire_time ."\n". // set the expire date of this link
  81
+			"/$path"; // full path (incl bucket), starting with a /
  82
+
  83
+		require_once(dirname(__FILE__).'/lib.s3.php');
  84
+		$s3 = new TanTanS3($this->options['key'], $this->options['secret']);
  85
+		$signature = urlencode($s3->constructSig($stringtosign));
  86
+		
  87
+		return sprintf('http://%s/%s?AWSAccessKeyId=%s&Expires=%u&Signature=%s', $accessDomain, $filekey, $this->options['key'], $expire_time, $signature);
  88
+	}
  89
+}
  90
+
  91
+function wps3_get_secure_attachment_url($postID, $expires = 900, $operation = 'GET') {
  92
+	global $TanTanWordPressS3Plugin;
  93
+	return $TanTanWordPressS3Plugin->get_secure_attachment_url($postID, $expires, $operation);
39 94
 }

0 notes on commit 11f46ec

Please sign in to comment.
Something went wrong with that request. Please try again.