| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | /**
 | 
        
           |  |  | 3 | * $Id$
 | 
        
           |  |  | 4 | *
 | 
        
           |  |  | 5 | * Copyright (c) 2013, Donovan Schönknecht.  All rights reserved.
 | 
        
           |  |  | 6 | *
 | 
        
           |  |  | 7 | * Redistribution and use in source and binary forms, with or without
 | 
        
           |  |  | 8 | * modification, are permitted provided that the following conditions are met:
 | 
        
           |  |  | 9 | *
 | 
        
           |  |  | 10 | * - Redistributions of source code must retain the above copyright notice,
 | 
        
           |  |  | 11 | *   this list of conditions and the following disclaimer.
 | 
        
           |  |  | 12 | * - Redistributions in binary form must reproduce the above copyright
 | 
        
           |  |  | 13 | *   notice, this list of conditions and the following disclaimer in the
 | 
        
           |  |  | 14 | *   documentation and/or other materials provided with the distribution.
 | 
        
           |  |  | 15 | *
 | 
        
           |  |  | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 | 
        
           |  |  | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 | 
        
           |  |  | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 | 
        
           |  |  | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 | 
        
           |  |  | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 | 
        
           |  |  | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 | 
        
           |  |  | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 | 
        
           |  |  | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 | 
        
           |  |  | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 | 
        
           |  |  | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 | 
        
           |  |  | 26 | * POSSIBILITY OF SUCH DAMAGE.
 | 
        
           |  |  | 27 | *
 | 
        
           |  |  | 28 | * Amazon S3 is a trademark of Amazon.com, Inc. or its affiliates.
 | 
        
           |  |  | 29 | */
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | /**
 | 
        
           |  |  | 32 | * Amazon S3 PHP class
 | 
        
           |  |  | 33 | *
 | 
        
           |  |  | 34 | * @link http://undesigned.org.za/2007/10/22/amazon-s3-php-class
 | 
        
           |  |  | 35 | * @version 0.5.1
 | 
        
           |  |  | 36 | */
 | 
        
           |  |  | 37 | class S3
 | 
        
           |  |  | 38 | {
 | 
        
           |  |  | 39 | 	// ACL flags
 | 
        
           |  |  | 40 | 	const ACL_PRIVATE = 'private';
 | 
        
           |  |  | 41 | 	const ACL_PUBLIC_READ = 'public-read';
 | 
        
           |  |  | 42 | 	const ACL_PUBLIC_READ_WRITE = 'public-read-write';
 | 
        
           |  |  | 43 | 	const ACL_AUTHENTICATED_READ = 'authenticated-read';
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | 	const STORAGE_CLASS_STANDARD = 'STANDARD';
 | 
        
           |  |  | 46 | 	const STORAGE_CLASS_RRS = 'REDUCED_REDUNDANCY';
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 | 	const SSE_NONE = '';
 | 
        
           |  |  | 49 | 	const SSE_AES256 = 'AES256';
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 | 	/**
 | 
        
           |  |  | 52 | 	 * The AWS Access key
 | 
        
           |  |  | 53 | 	 *
 | 
        
           |  |  | 54 | 	 * @var string
 | 
        
           |  |  | 55 | 	 * @access private
 | 
        
           |  |  | 56 | 	 * @static
 | 
        
           |  |  | 57 | 	 */
 | 
        
           |  |  | 58 | 	private static $__accessKey = null;
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 | 	/**
 | 
        
           |  |  | 61 | 	 * AWS Secret Key
 | 
        
           |  |  | 62 | 	 *
 | 
        
           |  |  | 63 | 	 * @var string
 | 
        
           |  |  | 64 | 	 * @access private
 | 
        
           |  |  | 65 | 	 * @static
 | 
        
           |  |  | 66 | 	 */
 | 
        
           |  |  | 67 | 	private static $__secretKey = null;
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 | 	/**
 | 
        
           |  |  | 70 | 	 * SSL Client key
 | 
        
           |  |  | 71 | 	 *
 | 
        
           |  |  | 72 | 	 * @var string
 | 
        
           |  |  | 73 | 	 * @access private
 | 
        
           |  |  | 74 | 	 * @static
 | 
        
           |  |  | 75 | 	 */
 | 
        
           |  |  | 76 | 	private static $__sslKey = null;
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 | 	/**
 | 
        
           |  |  | 79 | 	 * Default delimiter to be used, for example while getBucket().
 | 
        
           |  |  | 80 | 	 * @var string
 | 
        
           |  |  | 81 | 	 * @access public
 | 
        
           |  |  | 82 | 	 * @static
 | 
        
           |  |  | 83 | 	 */
 | 
        
           |  |  | 84 | 	public static $defDelimiter = null;
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 | 	/**
 | 
        
           |  |  | 87 | 	 * AWS URI
 | 
        
           |  |  | 88 | 	 *
 | 
        
           |  |  | 89 | 	 * @var string
 | 
        
           |  |  | 90 | 	 * @acess public
 | 
        
           |  |  | 91 | 	 * @static
 | 
        
           |  |  | 92 | 	 */
 | 
        
           |  |  | 93 | 	public static $endpoint = 's3.amazonaws.com';
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 | 	/**
 | 
        
           |  |  | 96 | 	 * Proxy information
 | 
        
           |  |  | 97 | 	 *
 | 
        
           |  |  | 98 | 	 * @var null|array
 | 
        
           |  |  | 99 | 	 * @access public
 | 
        
           |  |  | 100 | 	 * @static
 | 
        
           |  |  | 101 | 	 */
 | 
        
           |  |  | 102 | 	public static $proxy = null;
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 | 	/**
 | 
        
           |  |  | 105 | 	 * Connect using SSL?
 | 
        
           |  |  | 106 | 	 *
 | 
        
           |  |  | 107 | 	 * @var bool
 | 
        
           |  |  | 108 | 	 * @access public
 | 
        
           |  |  | 109 | 	 * @static
 | 
        
           |  |  | 110 | 	 */
 | 
        
           |  |  | 111 | 	public static $useSSL = false;
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 | 	/**
 | 
        
           |  |  | 114 | 	 * Use SSL validation?
 | 
        
           |  |  | 115 | 	 *
 | 
        
           |  |  | 116 | 	 * @var bool
 | 
        
           |  |  | 117 | 	 * @access public
 | 
        
           |  |  | 118 | 	 * @static
 | 
        
           |  |  | 119 | 	 */
 | 
        
           |  |  | 120 | 	public static $useSSLValidation = true;
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 | 	/**
 | 
        
           |  |  | 123 | 	 * Use SSL version
 | 
        
           |  |  | 124 | 	 *
 | 
        
           |  |  | 125 | 	 * @var const
 | 
        
           |  |  | 126 | 	 * @access public
 | 
        
           |  |  | 127 | 	 * @static
 | 
        
           |  |  | 128 | 	 */
 | 
        
           |  |  | 129 | 	public static $useSSLVersion = CURL_SSLVERSION_TLSv1;
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 | 	/**
 | 
        
           |  |  | 132 | 	 * Use PHP exceptions?
 | 
        
           |  |  | 133 | 	 *
 | 
        
           |  |  | 134 | 	 * @var bool
 | 
        
           |  |  | 135 | 	 * @access public
 | 
        
           |  |  | 136 | 	 * @static
 | 
        
           |  |  | 137 | 	 */
 | 
        
           |  |  | 138 | 	public static $useExceptions = false;
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 | 	/**
 | 
        
           |  |  | 141 | 	 * Time offset applied to time()
 | 
        
           |  |  | 142 | 	 * @access private
 | 
        
           |  |  | 143 | 	 * @static
 | 
        
           |  |  | 144 | 	 */
 | 
        
           |  |  | 145 | 	private static $__timeOffset = 0;
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 | 	/**
 | 
        
           |  |  | 148 | 	 * SSL client key
 | 
        
           |  |  | 149 | 	 *
 | 
        
           |  |  | 150 | 	 * @var bool
 | 
        
           |  |  | 151 | 	 * @access public
 | 
        
           |  |  | 152 | 	 * @static
 | 
        
           |  |  | 153 | 	 */
 | 
        
           |  |  | 154 | 	public static $sslKey = null;
 | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 | 	/**
 | 
        
           |  |  | 157 | 	 * SSL client certfificate
 | 
        
           |  |  | 158 | 	 *
 | 
        
           |  |  | 159 | 	 * @var string
 | 
        
           |  |  | 160 | 	 * @acess public
 | 
        
           |  |  | 161 | 	 * @static
 | 
        
           |  |  | 162 | 	 */
 | 
        
           |  |  | 163 | 	public static $sslCert = null;
 | 
        
           |  |  | 164 |   | 
        
           |  |  | 165 | 	/**
 | 
        
           |  |  | 166 | 	 * SSL CA cert (only required if you are having problems with your system CA cert)
 | 
        
           |  |  | 167 | 	 *
 | 
        
           |  |  | 168 | 	 * @var string
 | 
        
           |  |  | 169 | 	 * @access public
 | 
        
           |  |  | 170 | 	 * @static
 | 
        
           |  |  | 171 | 	 */
 | 
        
           |  |  | 172 | 	public static $sslCACert = null;
 | 
        
           |  |  | 173 |   | 
        
           |  |  | 174 | 	/**
 | 
        
           |  |  | 175 | 	 * AWS Key Pair ID
 | 
        
           |  |  | 176 | 	 *
 | 
        
           |  |  | 177 | 	 * @var string
 | 
        
           |  |  | 178 | 	 * @access private
 | 
        
           |  |  | 179 | 	 * @static
 | 
        
           |  |  | 180 | 	 */
 | 
        
           |  |  | 181 | 	private static $__signingKeyPairId = null;
 | 
        
           |  |  | 182 |   | 
        
           |  |  | 183 | 	/**
 | 
        
           |  |  | 184 | 	 * Key resource, freeSigningKey() must be called to clear it from memory
 | 
        
           |  |  | 185 | 	 *
 | 
        
           |  |  | 186 | 	 * @var bool
 | 
        
           |  |  | 187 | 	 * @access private
 | 
        
           |  |  | 188 | 	 * @static
 | 
        
           |  |  | 189 | 	 */
 | 
        
           |  |  | 190 | 	private static $__signingKeyResource = false;
 | 
        
           |  |  | 191 |   | 
        
           |  |  | 192 |   | 
        
           |  |  | 193 | 	/**
 | 
        
           |  |  | 194 | 	* Constructor - if you're not using the class statically
 | 
        
           |  |  | 195 | 	*
 | 
        
           |  |  | 196 | 	* @param string $accessKey Access key
 | 
        
           |  |  | 197 | 	* @param string $secretKey Secret key
 | 
        
           |  |  | 198 | 	* @param boolean $useSSL Enable SSL
 | 
        
           |  |  | 199 | 	* @param string $endpoint Amazon URI
 | 
        
           |  |  | 200 | 	* @return void
 | 
        
           |  |  | 201 | 	*/
 | 
        
           |  |  | 202 | 	public function __construct($accessKey = null, $secretKey = null, $useSSL = false, $endpoint = 's3.amazonaws.com')
 | 
        
           |  |  | 203 | 	{
 | 
        
           |  |  | 204 | 		if ($accessKey !== null && $secretKey !== null)
 | 
        
           |  |  | 205 | 			self::setAuth($accessKey, $secretKey);
 | 
        
           |  |  | 206 | 		self::$useSSL = $useSSL;
 | 
        
           |  |  | 207 | 		self::$endpoint = $endpoint;
 | 
        
           |  |  | 208 | 	}
 | 
        
           |  |  | 209 |   | 
        
           |  |  | 210 |   | 
        
           |  |  | 211 | 	/**
 | 
        
           |  |  | 212 | 	* Set the service endpoint
 | 
        
           |  |  | 213 | 	*
 | 
        
           |  |  | 214 | 	* @param string $host Hostname
 | 
        
           |  |  | 215 | 	* @return void
 | 
        
           |  |  | 216 | 	*/
 | 
        
           |  |  | 217 | 	public function setEndpoint($host)
 | 
        
           |  |  | 218 | 	{
 | 
        
           |  |  | 219 | 		self::$endpoint = $host;
 | 
        
           |  |  | 220 | 	}
 | 
        
           |  |  | 221 |   | 
        
           |  |  | 222 |   | 
        
           |  |  | 223 | 	/**
 | 
        
           |  |  | 224 | 	* Set AWS access key and secret key
 | 
        
           |  |  | 225 | 	*
 | 
        
           |  |  | 226 | 	* @param string $accessKey Access key
 | 
        
           |  |  | 227 | 	* @param string $secretKey Secret key
 | 
        
           |  |  | 228 | 	* @return void
 | 
        
           |  |  | 229 | 	*/
 | 
        
           |  |  | 230 | 	public static function setAuth($accessKey, $secretKey)
 | 
        
           |  |  | 231 | 	{
 | 
        
           |  |  | 232 | 		self::$__accessKey = $accessKey;
 | 
        
           |  |  | 233 | 		self::$__secretKey = $secretKey;
 | 
        
           |  |  | 234 | 	}
 | 
        
           |  |  | 235 |   | 
        
           |  |  | 236 |   | 
        
           |  |  | 237 | 	/**
 | 
        
           |  |  | 238 | 	* Check if AWS keys have been set
 | 
        
           |  |  | 239 | 	*
 | 
        
           |  |  | 240 | 	* @return boolean
 | 
        
           |  |  | 241 | 	*/
 | 
        
           |  |  | 242 | 	public static function hasAuth() {
 | 
        
           |  |  | 243 | 		return (self::$__accessKey !== null && self::$__secretKey !== null);
 | 
        
           |  |  | 244 | 	}
 | 
        
           |  |  | 245 |   | 
        
           |  |  | 246 |   | 
        
           |  |  | 247 | 	/**
 | 
        
           |  |  | 248 | 	* Set SSL on or off
 | 
        
           |  |  | 249 | 	*
 | 
        
           |  |  | 250 | 	* @param boolean $enabled SSL enabled
 | 
        
           |  |  | 251 | 	* @param boolean $validate SSL certificate validation
 | 
        
           |  |  | 252 | 	* @return void
 | 
        
           |  |  | 253 | 	*/
 | 
        
           |  |  | 254 | 	public static function setSSL($enabled, $validate = true)
 | 
        
           |  |  | 255 | 	{
 | 
        
           |  |  | 256 | 		self::$useSSL = $enabled;
 | 
        
           |  |  | 257 | 		self::$useSSLValidation = $validate;
 | 
        
           |  |  | 258 | 	}
 | 
        
           |  |  | 259 |   | 
        
           |  |  | 260 |   | 
        
           |  |  | 261 | 	/**
 | 
        
           |  |  | 262 | 	* Set SSL client certificates (experimental)
 | 
        
           |  |  | 263 | 	*
 | 
        
           |  |  | 264 | 	* @param string $sslCert SSL client certificate
 | 
        
           |  |  | 265 | 	* @param string $sslKey SSL client key
 | 
        
           |  |  | 266 | 	* @param string $sslCACert SSL CA cert (only required if you are having problems with your system CA cert)
 | 
        
           |  |  | 267 | 	* @return void
 | 
        
           |  |  | 268 | 	*/
 | 
        
           |  |  | 269 | 	public static function setSSLAuth($sslCert = null, $sslKey = null, $sslCACert = null)
 | 
        
           |  |  | 270 | 	{
 | 
        
           |  |  | 271 | 		self::$sslCert = $sslCert;
 | 
        
           |  |  | 272 | 		self::$sslKey = $sslKey;
 | 
        
           |  |  | 273 | 		self::$sslCACert = $sslCACert;
 | 
        
           |  |  | 274 | 	}
 | 
        
           |  |  | 275 |   | 
        
           |  |  | 276 |   | 
        
           |  |  | 277 | 	/**
 | 
        
           |  |  | 278 | 	* Set proxy information
 | 
        
           |  |  | 279 | 	*
 | 
        
           |  |  | 280 | 	* @param string $host Proxy hostname and port (localhost:1234)
 | 
        
           |  |  | 281 | 	* @param string $user Proxy username
 | 
        
           |  |  | 282 | 	* @param string $pass Proxy password
 | 
        
           |  |  | 283 | 	* @param constant $type CURL proxy type
 | 
        
           |  |  | 284 | 	* @return void
 | 
        
           |  |  | 285 | 	*/
 | 
        
           |  |  | 286 | 	public static function setProxy($host, $user = null, $pass = null, $type = CURLPROXY_SOCKS5)
 | 
        
           |  |  | 287 | 	{
 | 
        
           |  |  | 288 | 		self::$proxy = array('host' => $host, 'type' => $type, 'user' => $user, 'pass' => $pass);
 | 
        
           |  |  | 289 | 	}
 | 
        
           |  |  | 290 |   | 
        
           |  |  | 291 |   | 
        
           |  |  | 292 | 	/**
 | 
        
           |  |  | 293 | 	* Set the error mode to exceptions
 | 
        
           |  |  | 294 | 	*
 | 
        
           |  |  | 295 | 	* @param boolean $enabled Enable exceptions
 | 
        
           |  |  | 296 | 	* @return void
 | 
        
           |  |  | 297 | 	*/
 | 
        
           |  |  | 298 | 	public static function setExceptions($enabled = true)
 | 
        
           |  |  | 299 | 	{
 | 
        
           |  |  | 300 | 		self::$useExceptions = $enabled;
 | 
        
           |  |  | 301 | 	}
 | 
        
           |  |  | 302 |   | 
        
           |  |  | 303 |   | 
        
           |  |  | 304 | 	/**
 | 
        
           |  |  | 305 | 	* Set AWS time correction offset (use carefully)
 | 
        
           |  |  | 306 | 	*
 | 
        
           |  |  | 307 | 	* This can be used when an inaccurate system time is generating
 | 
        
           |  |  | 308 | 	* invalid request signatures.  It should only be used as a last
 | 
        
           |  |  | 309 | 	* resort when the system time cannot be changed.
 | 
        
           |  |  | 310 | 	*
 | 
        
           |  |  | 311 | 	* @param string $offset Time offset (set to zero to use AWS server time)
 | 
        
           |  |  | 312 | 	* @return void
 | 
        
           |  |  | 313 | 	*/
 | 
        
           |  |  | 314 | 	public static function setTimeCorrectionOffset($offset = 0)
 | 
        
           |  |  | 315 | 	{
 | 
        
           |  |  | 316 | 		if ($offset == 0)
 | 
        
           |  |  | 317 | 		{
 | 
        
           |  |  | 318 | 			$rest = new S3Request('HEAD');
 | 
        
           |  |  | 319 | 			$rest = $rest->getResponse();
 | 
        
           |  |  | 320 | 			$awstime = $rest->headers['date'];
 | 
        
           |  |  | 321 | 			$systime = time();
 | 
        
           |  |  | 322 | 			$offset = $systime > $awstime ? -($systime - $awstime) : ($awstime - $systime);
 | 
        
           |  |  | 323 | 		}
 | 
        
           |  |  | 324 | 		self::$__timeOffset = $offset;
 | 
        
           |  |  | 325 | 	}
 | 
        
           |  |  | 326 |   | 
        
           |  |  | 327 |   | 
        
           |  |  | 328 | 	/**
 | 
        
           |  |  | 329 | 	* Set signing key
 | 
        
           |  |  | 330 | 	*
 | 
        
           |  |  | 331 | 	* @param string $keyPairId AWS Key Pair ID
 | 
        
           |  |  | 332 | 	* @param string $signingKey Private Key
 | 
        
           |  |  | 333 | 	* @param boolean $isFile Load private key from file, set to false to load string
 | 
        
           |  |  | 334 | 	* @return boolean
 | 
        
           |  |  | 335 | 	*/
 | 
        
           |  |  | 336 | 	public static function setSigningKey($keyPairId, $signingKey, $isFile = true)
 | 
        
           |  |  | 337 | 	{
 | 
        
           |  |  | 338 | 		self::$__signingKeyPairId = $keyPairId;
 | 
        
           |  |  | 339 | 		if ((self::$__signingKeyResource = openssl_pkey_get_private($isFile ?
 | 
        
           |  |  | 340 | 		file_get_contents($signingKey) : $signingKey)) !== false) return true;
 | 
        
           |  |  | 341 | 		self::__triggerError('S3::setSigningKey(): Unable to open load private key: '.$signingKey, __FILE__, __LINE__);
 | 
        
           |  |  | 342 | 		return false;
 | 
        
           |  |  | 343 | 	}
 | 
        
           |  |  | 344 |   | 
        
           |  |  | 345 |   | 
        
           |  |  | 346 | 	/**
 | 
        
           |  |  | 347 | 	* Free signing key from memory, MUST be called if you are using setSigningKey()
 | 
        
           |  |  | 348 | 	*
 | 
        
           |  |  | 349 | 	* @return void
 | 
        
           |  |  | 350 | 	*/
 | 
        
           |  |  | 351 | 	public static function freeSigningKey()
 | 
        
           |  |  | 352 | 	{
 | 
        
           |  |  | 353 | 		if (self::$__signingKeyResource !== false)
 | 
        
           |  |  | 354 | 		{
 | 
        
           |  |  | 355 | 			self::$__signingKeyResource = null;
 | 
        
           |  |  | 356 | 		}
 | 
        
           |  |  | 357 | 	}
 | 
        
           |  |  | 358 |   | 
        
           |  |  | 359 |   | 
        
           |  |  | 360 | 	/**
 | 
        
           |  |  | 361 | 	* Internal error handler
 | 
        
           |  |  | 362 | 	*
 | 
        
           |  |  | 363 | 	* @internal Internal error handler
 | 
        
           |  |  | 364 | 	* @param string $message Error message
 | 
        
           |  |  | 365 | 	* @param string $file Filename
 | 
        
           |  |  | 366 | 	* @param integer $line Line number
 | 
        
           |  |  | 367 | 	* @param integer $code Error code
 | 
        
           |  |  | 368 | 	* @return void
 | 
        
           |  |  | 369 | 	*/
 | 
        
           |  |  | 370 | 	private static function __triggerError($message, $file, $line, $code = 0)
 | 
        
           |  |  | 371 | 	{
 | 
        
           |  |  | 372 | 		if (self::$useExceptions)
 | 
        
           |  |  | 373 | 			throw new S3Exception($message, $file, $line, $code);
 | 
        
           |  |  | 374 | 		else
 | 
        
           |  |  | 375 | 			trigger_error($message, E_USER_WARNING);
 | 
        
           |  |  | 376 | 	}
 | 
        
           |  |  | 377 |   | 
        
           |  |  | 378 |   | 
        
           |  |  | 379 | 	/**
 | 
        
           |  |  | 380 | 	* Get a list of buckets
 | 
        
           |  |  | 381 | 	*
 | 
        
           |  |  | 382 | 	* @param boolean $detailed Returns detailed bucket list when true
 | 
        
           |  |  | 383 | 	* @return array | false
 | 
        
           |  |  | 384 | 	*/
 | 
        
           |  |  | 385 | 	public static function listBuckets($detailed = false)
 | 
        
           |  |  | 386 | 	{
 | 
        
           |  |  | 387 | 		$rest = new S3Request('GET', '', '', self::$endpoint);
 | 
        
           |  |  | 388 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 389 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 390 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 391 | 		if ($rest->error !== false)
 | 
        
           |  |  | 392 | 		{
 | 
        
           |  |  | 393 | 			self::__triggerError(sprintf("S3::listBuckets(): [%s] %s", $rest->error['code'],
 | 
        
           |  |  | 394 | 			$rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 395 | 			return false;
 | 
        
           |  |  | 396 | 		}
 | 
        
           |  |  | 397 | 		$results = array();
 | 
        
           |  |  | 398 | 		if (!isset($rest->body->Buckets)) return $results;
 | 
        
           |  |  | 399 |   | 
        
           |  |  | 400 | 		if ($detailed)
 | 
        
           |  |  | 401 | 		{
 | 
        
           |  |  | 402 | 			if (isset($rest->body->Owner, $rest->body->Owner->ID, $rest->body->Owner->DisplayName))
 | 
        
           |  |  | 403 | 			$results['owner'] = array(
 | 
        
           |  |  | 404 | 				'id' => (string)$rest->body->Owner->ID, 'name' => (string)$rest->body->Owner->DisplayName
 | 
        
           |  |  | 405 | 			);
 | 
        
           |  |  | 406 | 			$results['buckets'] = array();
 | 
        
           |  |  | 407 | 			foreach ($rest->body->Buckets->Bucket as $b)
 | 
        
           |  |  | 408 | 				$results['buckets'][] = array(
 | 
        
           |  |  | 409 | 					'name' => (string)$b->Name, 'time' => strtotime((string)$b->CreationDate)
 | 
        
           |  |  | 410 | 				);
 | 
        
           |  |  | 411 | 		} else
 | 
        
           |  |  | 412 | 			foreach ($rest->body->Buckets->Bucket as $b) $results[] = (string)$b->Name;
 | 
        
           |  |  | 413 |   | 
        
           |  |  | 414 | 		return $results;
 | 
        
           |  |  | 415 | 	}
 | 
        
           |  |  | 416 |   | 
        
           |  |  | 417 |   | 
        
           |  |  | 418 | 	/**
 | 
        
           |  |  | 419 | 	* Get contents for a bucket
 | 
        
           |  |  | 420 | 	*
 | 
        
           |  |  | 421 | 	* If maxKeys is null this method will loop through truncated result sets
 | 
        
           |  |  | 422 | 	*
 | 
        
           |  |  | 423 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 424 | 	* @param string $prefix Prefix
 | 
        
           |  |  | 425 | 	* @param string $marker Marker (last file listed)
 | 
        
           |  |  | 426 | 	* @param string $maxKeys Max keys (maximum number of keys to return)
 | 
        
           |  |  | 427 | 	* @param string $delimiter Delimiter
 | 
        
           |  |  | 428 | 	* @param boolean $returnCommonPrefixes Set to true to return CommonPrefixes
 | 
        
           |  |  | 429 | 	* @return array | false
 | 
        
           |  |  | 430 | 	*/
 | 
        
           |  |  | 431 | 	public static function getBucket($bucket, $prefix = null, $marker = null, $maxKeys = null, $delimiter = null, $returnCommonPrefixes = false)
 | 
        
           |  |  | 432 | 	{
 | 
        
           |  |  | 433 | 		$rest = new S3Request('GET', $bucket, '', self::$endpoint);
 | 
        
           |  |  | 434 | 		if ($maxKeys == 0) $maxKeys = null;
 | 
        
           |  |  | 435 | 		if ($prefix !== null && $prefix !== '') $rest->setParameter('prefix', $prefix);
 | 
        
           |  |  | 436 | 		if ($marker !== null && $marker !== '') $rest->setParameter('marker', $marker);
 | 
        
           |  |  | 437 | 		if ($maxKeys !== null && $maxKeys !== '') $rest->setParameter('max-keys', $maxKeys);
 | 
        
           |  |  | 438 | 		if ($delimiter !== null && $delimiter !== '') $rest->setParameter('delimiter', $delimiter);
 | 
        
           |  |  | 439 | 		else if (!empty(self::$defDelimiter)) $rest->setParameter('delimiter', self::$defDelimiter);
 | 
        
           |  |  | 440 | 		$response = $rest->getResponse();
 | 
        
           |  |  | 441 | 		if ($response->error === false && $response->code !== 200)
 | 
        
           |  |  | 442 | 			$response->error = array('code' => $response->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 443 | 		if ($response->error !== false)
 | 
        
           |  |  | 444 | 		{
 | 
        
           |  |  | 445 | 			self::__triggerError(sprintf("S3::getBucket(): [%s] %s",
 | 
        
           |  |  | 446 | 			$response->error['code'], $response->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 447 | 			return false;
 | 
        
           |  |  | 448 | 		}
 | 
        
           |  |  | 449 |   | 
        
           |  |  | 450 | 		$results = array();
 | 
        
           |  |  | 451 |   | 
        
           |  |  | 452 | 		$nextMarker = null;
 | 
        
           |  |  | 453 | 		if (isset($response->body, $response->body->Contents))
 | 
        
           |  |  | 454 | 		foreach ($response->body->Contents as $c)
 | 
        
           |  |  | 455 | 		{
 | 
        
           |  |  | 456 | 			$results[(string)$c->Key] = array(
 | 
        
           |  |  | 457 | 				'name' => (string)$c->Key,
 | 
        
           |  |  | 458 | 				'time' => strtotime((string)$c->LastModified),
 | 
        
           |  |  | 459 | 				'size' => (int)$c->Size,
 | 
        
           |  |  | 460 | 				'hash' => substr((string)$c->ETag, 1, -1)
 | 
        
           |  |  | 461 | 			);
 | 
        
           |  |  | 462 | 			$nextMarker = (string)$c->Key;
 | 
        
           |  |  | 463 | 		}
 | 
        
           |  |  | 464 |   | 
        
           |  |  | 465 | 		if ($returnCommonPrefixes && isset($response->body, $response->body->CommonPrefixes))
 | 
        
           |  |  | 466 | 			foreach ($response->body->CommonPrefixes as $c)
 | 
        
           |  |  | 467 | 				$results[(string)$c->Prefix] = array('prefix' => (string)$c->Prefix);
 | 
        
           |  |  | 468 |   | 
        
           |  |  | 469 | 		if (isset($response->body, $response->body->IsTruncated) &&
 | 
        
           |  |  | 470 | 		(string)$response->body->IsTruncated == 'false') return $results;
 | 
        
           |  |  | 471 |   | 
        
           |  |  | 472 | 		if (isset($response->body, $response->body->NextMarker))
 | 
        
           |  |  | 473 | 			$nextMarker = (string)$response->body->NextMarker;
 | 
        
           |  |  | 474 |   | 
        
           |  |  | 475 | 		// Loop through truncated results if maxKeys isn't specified
 | 
        
           |  |  | 476 | 		if ($maxKeys == null && $nextMarker !== null && (string)$response->body->IsTruncated == 'true')
 | 
        
           |  |  | 477 | 		do
 | 
        
           |  |  | 478 | 		{
 | 
        
           |  |  | 479 | 			$rest = new S3Request('GET', $bucket, '', self::$endpoint);
 | 
        
           |  |  | 480 | 			if ($prefix !== null && $prefix !== '') $rest->setParameter('prefix', $prefix);
 | 
        
           |  |  | 481 | 			$rest->setParameter('marker', $nextMarker);
 | 
        
           |  |  | 482 | 			if ($delimiter !== null && $delimiter !== '') $rest->setParameter('delimiter', $delimiter);
 | 
        
           |  |  | 483 |   | 
        
           |  |  | 484 | 			if (($response = $rest->getResponse()) == false || $response->code !== 200) break;
 | 
        
           |  |  | 485 |   | 
        
           |  |  | 486 | 			if (isset($response->body, $response->body->Contents))
 | 
        
           |  |  | 487 | 			foreach ($response->body->Contents as $c)
 | 
        
           |  |  | 488 | 			{
 | 
        
           |  |  | 489 | 				$results[(string)$c->Key] = array(
 | 
        
           |  |  | 490 | 					'name' => (string)$c->Key,
 | 
        
           |  |  | 491 | 					'time' => strtotime((string)$c->LastModified),
 | 
        
           |  |  | 492 | 					'size' => (int)$c->Size,
 | 
        
           |  |  | 493 | 					'hash' => substr((string)$c->ETag, 1, -1)
 | 
        
           |  |  | 494 | 				);
 | 
        
           |  |  | 495 | 				$nextMarker = (string)$c->Key;
 | 
        
           |  |  | 496 | 			}
 | 
        
           |  |  | 497 |   | 
        
           |  |  | 498 | 			if ($returnCommonPrefixes && isset($response->body, $response->body->CommonPrefixes))
 | 
        
           |  |  | 499 | 				foreach ($response->body->CommonPrefixes as $c)
 | 
        
           |  |  | 500 | 					$results[(string)$c->Prefix] = array('prefix' => (string)$c->Prefix);
 | 
        
           |  |  | 501 |   | 
        
           |  |  | 502 | 			if (isset($response->body, $response->body->NextMarker))
 | 
        
           |  |  | 503 | 				$nextMarker = (string)$response->body->NextMarker;
 | 
        
           |  |  | 504 |   | 
        
           |  |  | 505 | 		} while ($response !== false && (string)$response->body->IsTruncated == 'true');
 | 
        
           |  |  | 506 |   | 
        
           |  |  | 507 | 		return $results;
 | 
        
           |  |  | 508 | 	}
 | 
        
           |  |  | 509 |   | 
        
           |  |  | 510 |   | 
        
           |  |  | 511 | 	/**
 | 
        
           |  |  | 512 | 	* Put a bucket
 | 
        
           |  |  | 513 | 	*
 | 
        
           |  |  | 514 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 515 | 	* @param constant $acl ACL flag
 | 
        
           |  |  | 516 | 	* @param string $location Set as "EU" to create buckets hosted in Europe
 | 
        
           |  |  | 517 | 	* @return boolean
 | 
        
           |  |  | 518 | 	*/
 | 
        
           |  |  | 519 | 	public static function putBucket($bucket, $acl = self::ACL_PRIVATE, $location = false)
 | 
        
           |  |  | 520 | 	{
 | 
        
           |  |  | 521 | 		$rest = new S3Request('PUT', $bucket, '', self::$endpoint);
 | 
        
           |  |  | 522 | 		$rest->setAmzHeader('x-amz-acl', $acl);
 | 
        
           |  |  | 523 |   | 
        
           |  |  | 524 | 		if ($location !== false)
 | 
        
           |  |  | 525 | 		{
 | 
        
           |  |  | 526 | 			$dom = new DOMDocument;
 | 
        
           |  |  | 527 | 			$createBucketConfiguration = $dom->createElement('CreateBucketConfiguration');
 | 
        
           |  |  | 528 | 			$locationConstraint = $dom->createElement('LocationConstraint', $location);
 | 
        
           |  |  | 529 | 			$createBucketConfiguration->appendChild($locationConstraint);
 | 
        
           |  |  | 530 | 			$dom->appendChild($createBucketConfiguration);
 | 
        
           |  |  | 531 | 			$rest->data = $dom->saveXML();
 | 
        
           |  |  | 532 | 			$rest->size = strlen($rest->data);
 | 
        
           |  |  | 533 | 			$rest->setHeader('Content-Type', 'application/xml');
 | 
        
           |  |  | 534 | 		}
 | 
        
           |  |  | 535 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 536 |   | 
        
           |  |  | 537 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 538 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 539 | 		if ($rest->error !== false)
 | 
        
           |  |  | 540 | 		{
 | 
        
           |  |  | 541 | 			self::__triggerError(sprintf("S3::putBucket({$bucket}, {$acl}, {$location}): [%s] %s",
 | 
        
           |  |  | 542 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 543 | 			return false;
 | 
        
           |  |  | 544 | 		}
 | 
        
           |  |  | 545 | 		return true;
 | 
        
           |  |  | 546 | 	}
 | 
        
           |  |  | 547 |   | 
        
           |  |  | 548 |   | 
        
           |  |  | 549 | 	/**
 | 
        
           |  |  | 550 | 	* Delete an empty bucket
 | 
        
           |  |  | 551 | 	*
 | 
        
           |  |  | 552 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 553 | 	* @return boolean
 | 
        
           |  |  | 554 | 	*/
 | 
        
           |  |  | 555 | 	public static function deleteBucket($bucket)
 | 
        
           |  |  | 556 | 	{
 | 
        
           |  |  | 557 | 		$rest = new S3Request('DELETE', $bucket, '', self::$endpoint);
 | 
        
           |  |  | 558 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 559 | 		if ($rest->error === false && $rest->code !== 204)
 | 
        
           |  |  | 560 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 561 | 		if ($rest->error !== false)
 | 
        
           |  |  | 562 | 		{
 | 
        
           |  |  | 563 | 			self::__triggerError(sprintf("S3::deleteBucket({$bucket}): [%s] %s",
 | 
        
           |  |  | 564 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 565 | 			return false;
 | 
        
           |  |  | 566 | 		}
 | 
        
           |  |  | 567 | 		return true;
 | 
        
           |  |  | 568 | 	}
 | 
        
           |  |  | 569 |   | 
        
           |  |  | 570 |   | 
        
           |  |  | 571 | 	/**
 | 
        
           |  |  | 572 | 	* Create input info array for putObject()
 | 
        
           |  |  | 573 | 	*
 | 
        
           |  |  | 574 | 	* @param string $file Input file
 | 
        
           |  |  | 575 | 	* @param mixed $md5sum Use MD5 hash (supply a string if you want to use your own)
 | 
        
           |  |  | 576 | 	* @return array | false
 | 
        
           |  |  | 577 | 	*/
 | 
        
           |  |  | 578 | 	public static function inputFile($file, $md5sum = true)
 | 
        
           |  |  | 579 | 	{
 | 
        
           |  |  | 580 | 		if (!file_exists($file) || !is_file($file) || !is_readable($file))
 | 
        
           |  |  | 581 | 		{
 | 
        
           |  |  | 582 | 			self::__triggerError('S3::inputFile(): Unable to open input file: '.$file, __FILE__, __LINE__);
 | 
        
           |  |  | 583 | 			return false;
 | 
        
           |  |  | 584 | 		}
 | 
        
           |  |  | 585 | 		clearstatcache(false, $file);
 | 
        
           |  |  | 586 | 		return array('file' => $file, 'size' => filesize($file), 'md5sum' => $md5sum !== false ?
 | 
        
           |  |  | 587 | 		(is_string($md5sum) ? $md5sum : base64_encode(md5_file($file, true))) : '');
 | 
        
           |  |  | 588 | 	}
 | 
        
           |  |  | 589 |   | 
        
           |  |  | 590 |   | 
        
           |  |  | 591 | 	/**
 | 
        
           |  |  | 592 | 	* Create input array info for putObject() with a resource
 | 
        
           |  |  | 593 | 	*
 | 
        
           |  |  | 594 | 	* @param string $resource Input resource to read from
 | 
        
           |  |  | 595 | 	* @param integer $bufferSize Input byte size
 | 
        
           |  |  | 596 | 	* @param string $md5sum MD5 hash to send (optional)
 | 
        
           |  |  | 597 | 	* @return array | false
 | 
        
           |  |  | 598 | 	*/
 | 
        
           |  |  | 599 | 	public static function inputResource(&$resource, $bufferSize = false, $md5sum = '')
 | 
        
           |  |  | 600 | 	{
 | 
        
           |  |  | 601 | 		if (!is_resource($resource) || (int)$bufferSize < 0)
 | 
        
           |  |  | 602 | 		{
 | 
        
           |  |  | 603 | 			self::__triggerError('S3::inputResource(): Invalid resource or buffer size', __FILE__, __LINE__);
 | 
        
           |  |  | 604 | 			return false;
 | 
        
           |  |  | 605 | 		}
 | 
        
           |  |  | 606 |   | 
        
           |  |  | 607 | 		// Try to figure out the bytesize
 | 
        
           |  |  | 608 | 		if ($bufferSize === false)
 | 
        
           |  |  | 609 | 		{
 | 
        
           |  |  | 610 | 			if (fseek($resource, 0, SEEK_END) < 0 || ($bufferSize = ftell($resource)) === false)
 | 
        
           |  |  | 611 | 			{
 | 
        
           |  |  | 612 | 				self::__triggerError('S3::inputResource(): Unable to obtain resource size', __FILE__, __LINE__);
 | 
        
           |  |  | 613 | 				return false;
 | 
        
           |  |  | 614 | 			}
 | 
        
           |  |  | 615 | 			fseek($resource, 0);
 | 
        
           |  |  | 616 | 		}
 | 
        
           |  |  | 617 |   | 
        
           |  |  | 618 | 		$input = array('size' => $bufferSize, 'md5sum' => $md5sum);
 | 
        
           |  |  | 619 | 		$input['fp'] =& $resource;
 | 
        
           |  |  | 620 | 		return $input;
 | 
        
           |  |  | 621 | 	}
 | 
        
           |  |  | 622 |   | 
        
           |  |  | 623 |   | 
        
           |  |  | 624 | 	/**
 | 
        
           |  |  | 625 | 	* Put an object
 | 
        
           |  |  | 626 | 	*
 | 
        
           |  |  | 627 | 	* @param mixed $input Input data
 | 
        
           |  |  | 628 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 629 | 	* @param string $uri Object URI
 | 
        
           |  |  | 630 | 	* @param constant $acl ACL constant
 | 
        
           |  |  | 631 | 	* @param array $metaHeaders Array of x-amz-meta-* headers
 | 
        
           |  |  | 632 | 	* @param array $requestHeaders Array of request headers or content type as a string
 | 
        
           |  |  | 633 | 	* @param constant $storageClass Storage class constant
 | 
        
           |  |  | 634 | 	* @param constant $serverSideEncryption Server-side encryption
 | 
        
           |  |  | 635 | 	* @return boolean
 | 
        
           |  |  | 636 | 	*/
 | 
        
           |  |  | 637 | 	public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $requestHeaders = array(), $storageClass = self::STORAGE_CLASS_STANDARD, $serverSideEncryption = self::SSE_NONE)
 | 
        
           |  |  | 638 | 	{
 | 
        
           |  |  | 639 | 		if ($input === false) return false;
 | 
        
           |  |  | 640 | 		$rest = new S3Request('PUT', $bucket, $uri, self::$endpoint);
 | 
        
           |  |  | 641 |   | 
        
           |  |  | 642 | 		if (!is_array($input)) $input = array(
 | 
        
           |  |  | 643 | 			'data' => $input, 'size' => strlen($input),
 | 
        
           |  |  | 644 | 			'md5sum' => base64_encode(md5($input, true))
 | 
        
           |  |  | 645 | 		);
 | 
        
           |  |  | 646 |   | 
        
           |  |  | 647 | 		// Data
 | 
        
           |  |  | 648 | 		if (isset($input['fp']))
 | 
        
           |  |  | 649 | 			$rest->fp =& $input['fp'];
 | 
        
           |  |  | 650 | 		elseif (isset($input['file']))
 | 
        
           |  |  | 651 | 			$rest->fp = @fopen($input['file'], 'rb');
 | 
        
           |  |  | 652 | 		elseif (isset($input['data']))
 | 
        
           |  |  | 653 | 			$rest->data = $input['data'];
 | 
        
           |  |  | 654 |   | 
        
           |  |  | 655 | 		// Content-Length (required)
 | 
        
           |  |  | 656 | 		if (isset($input['size']) && $input['size'] >= 0)
 | 
        
           |  |  | 657 | 			$rest->size = $input['size'];
 | 
        
           |  |  | 658 | 		else {
 | 
        
           |  |  | 659 | 			if (isset($input['file'])) {
 | 
        
           |  |  | 660 | 				clearstatcache(false, $input['file']);
 | 
        
           |  |  | 661 | 				$rest->size = filesize($input['file']);
 | 
        
           |  |  | 662 | 			}
 | 
        
           |  |  | 663 | 			elseif (isset($input['data']))
 | 
        
           |  |  | 664 | 				$rest->size = strlen($input['data']);
 | 
        
           |  |  | 665 | 		}
 | 
        
           |  |  | 666 |   | 
        
           |  |  | 667 | 		// Custom request headers (Content-Type, Content-Disposition, Content-Encoding)
 | 
        
           |  |  | 668 | 		if (is_array($requestHeaders))
 | 
        
           |  |  | 669 | 			foreach ($requestHeaders as $h => $v)
 | 
        
           |  |  | 670 | 				strpos($h, 'x-amz-') === 0 ? $rest->setAmzHeader($h, $v) : $rest->setHeader($h, $v);
 | 
        
           |  |  | 671 | 		elseif (is_string($requestHeaders)) // Support for legacy contentType parameter
 | 
        
           |  |  | 672 | 			$input['type'] = $requestHeaders;
 | 
        
           |  |  | 673 |   | 
        
           |  |  | 674 | 		// Content-Type
 | 
        
           |  |  | 675 | 		if (!isset($input['type']))
 | 
        
           |  |  | 676 | 		{
 | 
        
           |  |  | 677 | 			if (isset($requestHeaders['Content-Type']))
 | 
        
           |  |  | 678 | 				$input['type'] =& $requestHeaders['Content-Type'];
 | 
        
           |  |  | 679 | 			elseif (isset($input['file']))
 | 
        
           |  |  | 680 | 				$input['type'] = self::__getMIMEType($input['file']);
 | 
        
           |  |  | 681 | 			else
 | 
        
           |  |  | 682 | 				$input['type'] = 'application/octet-stream';
 | 
        
           |  |  | 683 | 		}
 | 
        
           |  |  | 684 |   | 
        
           |  |  | 685 | 		if ($storageClass !== self::STORAGE_CLASS_STANDARD) // Storage class
 | 
        
           |  |  | 686 | 			$rest->setAmzHeader('x-amz-storage-class', $storageClass);
 | 
        
           |  |  | 687 |   | 
        
           |  |  | 688 | 		if ($serverSideEncryption !== self::SSE_NONE) // Server-side encryption
 | 
        
           |  |  | 689 | 			$rest->setAmzHeader('x-amz-server-side-encryption', $serverSideEncryption);
 | 
        
           |  |  | 690 |   | 
        
           |  |  | 691 | 		// We need to post with Content-Length and Content-Type, MD5 is optional
 | 
        
           |  |  | 692 | 		if ($rest->size >= 0 && ($rest->fp !== false || $rest->data !== false))
 | 
        
           |  |  | 693 | 		{
 | 
        
           |  |  | 694 | 			$rest->setHeader('Content-Type', $input['type']);
 | 
        
           |  |  | 695 | 			if (isset($input['md5sum'])) $rest->setHeader('Content-MD5', $input['md5sum']);
 | 
        
           |  |  | 696 |   | 
        
           |  |  | 697 | 			$rest->setAmzHeader('x-amz-acl', $acl);
 | 
        
           |  |  | 698 | 			foreach ($metaHeaders as $h => $v) $rest->setAmzHeader('x-amz-meta-'.$h, $v);
 | 
        
           |  |  | 699 | 			$rest->getResponse();
 | 
        
           |  |  | 700 | 		} else
 | 
        
           |  |  | 701 | 			$rest->response->error = array('code' => 0, 'message' => 'Missing input parameters');
 | 
        
           |  |  | 702 |   | 
        
           |  |  | 703 | 		if ($rest->response->error === false && $rest->response->code !== 200)
 | 
        
           |  |  | 704 | 			$rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 705 | 		if ($rest->response->error !== false)
 | 
        
           |  |  | 706 | 		{
 | 
        
           |  |  | 707 | 			self::__triggerError(sprintf("S3::putObject(): [%s] %s",
 | 
        
           |  |  | 708 | 			$rest->response->error['code'], $rest->response->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 709 | 			return false;
 | 
        
           |  |  | 710 | 		}
 | 
        
           |  |  | 711 | 		return true;
 | 
        
           |  |  | 712 | 	}
 | 
        
           |  |  | 713 |   | 
        
           |  |  | 714 |   | 
        
           |  |  | 715 | 	/**
 | 
        
           |  |  | 716 | 	* Put an object from a file (legacy function)
 | 
        
           |  |  | 717 | 	*
 | 
        
           |  |  | 718 | 	* @param string $file Input file path
 | 
        
           |  |  | 719 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 720 | 	* @param string $uri Object URI
 | 
        
           |  |  | 721 | 	* @param constant $acl ACL constant
 | 
        
           |  |  | 722 | 	* @param array $metaHeaders Array of x-amz-meta-* headers
 | 
        
           |  |  | 723 | 	* @param string $contentType Content type
 | 
        
           |  |  | 724 | 	* @return boolean
 | 
        
           |  |  | 725 | 	*/
 | 
        
           |  |  | 726 | 	public static function putObjectFile($file, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $contentType = null)
 | 
        
           |  |  | 727 | 	{
 | 
        
           |  |  | 728 | 		return self::putObject(self::inputFile($file), $bucket, $uri, $acl, $metaHeaders, $contentType);
 | 
        
           |  |  | 729 | 	}
 | 
        
           |  |  | 730 |   | 
        
           |  |  | 731 |   | 
        
           |  |  | 732 | 	/**
 | 
        
           |  |  | 733 | 	* Put an object from a string (legacy function)
 | 
        
           |  |  | 734 | 	*
 | 
        
           |  |  | 735 | 	* @param string $string Input data
 | 
        
           |  |  | 736 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 737 | 	* @param string $uri Object URI
 | 
        
           |  |  | 738 | 	* @param constant $acl ACL constant
 | 
        
           |  |  | 739 | 	* @param array $metaHeaders Array of x-amz-meta-* headers
 | 
        
           |  |  | 740 | 	* @param string $contentType Content type
 | 
        
           |  |  | 741 | 	* @return boolean
 | 
        
           |  |  | 742 | 	*/
 | 
        
           |  |  | 743 | 	public static function putObjectString($string, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $contentType = 'text/plain')
 | 
        
           |  |  | 744 | 	{
 | 
        
           |  |  | 745 | 		return self::putObject($string, $bucket, $uri, $acl, $metaHeaders, $contentType);
 | 
        
           |  |  | 746 | 	}
 | 
        
           |  |  | 747 |   | 
        
           |  |  | 748 |   | 
        
           |  |  | 749 | 	/**
 | 
        
           |  |  | 750 | 	* Get an object
 | 
        
           |  |  | 751 | 	*
 | 
        
           |  |  | 752 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 753 | 	* @param string $uri Object URI
 | 
        
           |  |  | 754 | 	* @param mixed $saveTo Filename or resource to write to
 | 
        
           |  |  | 755 | 	* @return mixed
 | 
        
           |  |  | 756 | 	*/
 | 
        
           |  |  | 757 | 	public static function getObject($bucket, $uri, $saveTo = false)
 | 
        
           |  |  | 758 | 	{
 | 
        
           |  |  | 759 | 		$rest = new S3Request('GET', $bucket, $uri, self::$endpoint);
 | 
        
           |  |  | 760 | 		if ($saveTo !== false)
 | 
        
           |  |  | 761 | 		{
 | 
        
           |  |  | 762 | 			if (is_resource($saveTo))
 | 
        
           |  |  | 763 | 				$rest->fp =& $saveTo;
 | 
        
           |  |  | 764 | 			else
 | 
        
           |  |  | 765 | 				if (($rest->fp = @fopen($saveTo, 'wb')) !== false)
 | 
        
           |  |  | 766 | 					$rest->file = realpath($saveTo);
 | 
        
           |  |  | 767 | 				else
 | 
        
           |  |  | 768 | 					$rest->response->error = array('code' => 0, 'message' => 'Unable to open save file for writing: '.$saveTo);
 | 
        
           |  |  | 769 | 		}
 | 
        
           |  |  | 770 | 		if ($rest->response->error === false) $rest->getResponse();
 | 
        
           |  |  | 771 |   | 
        
           |  |  | 772 | 		if ($rest->response->error === false && $rest->response->code !== 200)
 | 
        
           |  |  | 773 | 			$rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 774 | 		if ($rest->response->error !== false)
 | 
        
           |  |  | 775 | 		{
 | 
        
           |  |  | 776 | 			self::__triggerError(sprintf("S3::getObject({$bucket}, {$uri}): [%s] %s",
 | 
        
           |  |  | 777 | 			$rest->response->error['code'], $rest->response->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 778 | 			return false;
 | 
        
           |  |  | 779 | 		}
 | 
        
           |  |  | 780 | 		return $rest->response;
 | 
        
           |  |  | 781 | 	}
 | 
        
           |  |  | 782 |   | 
        
           |  |  | 783 |   | 
        
           |  |  | 784 | 	/**
 | 
        
           |  |  | 785 | 	* Get object information
 | 
        
           |  |  | 786 | 	*
 | 
        
           |  |  | 787 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 788 | 	* @param string $uri Object URI
 | 
        
           |  |  | 789 | 	* @param boolean $returnInfo Return response information
 | 
        
           |  |  | 790 | 	* @return mixed | false
 | 
        
           |  |  | 791 | 	*/
 | 
        
           |  |  | 792 | 	public static function getObjectInfo($bucket, $uri, $returnInfo = true)
 | 
        
           |  |  | 793 | 	{
 | 
        
           |  |  | 794 | 		$rest = new S3Request('HEAD', $bucket, $uri, self::$endpoint);
 | 
        
           |  |  | 795 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 796 | 		if ($rest->error === false && ($rest->code !== 200 && $rest->code !== 404))
 | 
        
           |  |  | 797 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 798 | 		if ($rest->error !== false)
 | 
        
           |  |  | 799 | 		{
 | 
        
           |  |  | 800 | 			self::__triggerError(sprintf("S3::getObjectInfo({$bucket}, {$uri}): [%s] %s",
 | 
        
           |  |  | 801 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 802 | 			return false;
 | 
        
           |  |  | 803 | 		}
 | 
        
           |  |  | 804 | 		return $rest->code == 200 ? $returnInfo ? $rest->headers : true : false;
 | 
        
           |  |  | 805 | 	}
 | 
        
           |  |  | 806 |   | 
        
           |  |  | 807 |   | 
        
           |  |  | 808 | 	/**
 | 
        
           |  |  | 809 | 	* Copy an object
 | 
        
           |  |  | 810 | 	*
 | 
        
           |  |  | 811 | 	* @param string $srcBucket Source bucket name
 | 
        
           |  |  | 812 | 	* @param string $srcUri Source object URI
 | 
        
           |  |  | 813 | 	* @param string $bucket Destination bucket name
 | 
        
           |  |  | 814 | 	* @param string $uri Destination object URI
 | 
        
           |  |  | 815 | 	* @param constant $acl ACL constant
 | 
        
           |  |  | 816 | 	* @param array $metaHeaders Optional array of x-amz-meta-* headers
 | 
        
           |  |  | 817 | 	* @param array $requestHeaders Optional array of request headers (content type, disposition, etc.)
 | 
        
           |  |  | 818 | 	* @param constant $storageClass Storage class constant
 | 
        
           |  |  | 819 | 	* @return mixed | false
 | 
        
           |  |  | 820 | 	*/
 | 
        
           |  |  | 821 | 	public static function copyObject($srcBucket, $srcUri, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $requestHeaders = array(), $storageClass = self::STORAGE_CLASS_STANDARD)
 | 
        
           |  |  | 822 | 	{
 | 
        
           |  |  | 823 | 		$rest = new S3Request('PUT', $bucket, $uri, self::$endpoint);
 | 
        
           |  |  | 824 | 		$rest->setHeader('Content-Length', 0);
 | 
        
           |  |  | 825 | 		foreach ($requestHeaders as $h => $v)
 | 
        
           |  |  | 826 | 				strpos($h, 'x-amz-') === 0 ? $rest->setAmzHeader($h, $v) : $rest->setHeader($h, $v);
 | 
        
           |  |  | 827 | 		foreach ($metaHeaders as $h => $v) $rest->setAmzHeader('x-amz-meta-'.$h, $v);
 | 
        
           |  |  | 828 | 		if ($storageClass !== self::STORAGE_CLASS_STANDARD) // Storage class
 | 
        
           |  |  | 829 | 			$rest->setAmzHeader('x-amz-storage-class', $storageClass);
 | 
        
           |  |  | 830 | 		$rest->setAmzHeader('x-amz-acl', $acl);
 | 
        
           |  |  | 831 | 		$rest->setAmzHeader('x-amz-copy-source', sprintf('/%s/%s', $srcBucket, rawurlencode($srcUri)));
 | 
        
           |  |  | 832 | 		if (sizeof($requestHeaders) > 0 || sizeof($metaHeaders) > 0)
 | 
        
           |  |  | 833 | 			$rest->setAmzHeader('x-amz-metadata-directive', 'REPLACE');
 | 
        
           |  |  | 834 |   | 
        
           |  |  | 835 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 836 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 837 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 838 | 		if ($rest->error !== false)
 | 
        
           |  |  | 839 | 		{
 | 
        
           |  |  | 840 | 			self::__triggerError(sprintf("S3::copyObject({$srcBucket}, {$srcUri}, {$bucket}, {$uri}): [%s] %s",
 | 
        
           |  |  | 841 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 842 | 			return false;
 | 
        
           |  |  | 843 | 		}
 | 
        
           |  |  | 844 | 		return isset($rest->body->LastModified, $rest->body->ETag) ? array(
 | 
        
           |  |  | 845 | 			'time' => strtotime((string)$rest->body->LastModified),
 | 
        
           |  |  | 846 | 			'hash' => substr((string)$rest->body->ETag, 1, -1)
 | 
        
           |  |  | 847 | 		) : false;
 | 
        
           |  |  | 848 | 	}
 | 
        
           |  |  | 849 |   | 
        
           |  |  | 850 |   | 
        
           |  |  | 851 | 	/**
 | 
        
           |  |  | 852 | 	* Set up a bucket redirection
 | 
        
           |  |  | 853 | 	*
 | 
        
           |  |  | 854 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 855 | 	* @param string $location Target host name
 | 
        
           |  |  | 856 | 	* @return boolean
 | 
        
           |  |  | 857 | 	*/
 | 
        
           |  |  | 858 | 	public static function setBucketRedirect($bucket = NULL, $location = NULL)
 | 
        
           |  |  | 859 | 	{
 | 
        
           |  |  | 860 | 		$rest = new S3Request('PUT', $bucket, '', self::$endpoint);
 | 
        
           |  |  | 861 |   | 
        
           |  |  | 862 | 		if( empty($bucket) || empty($location) ) {
 | 
        
           |  |  | 863 | 			self::__triggerError("S3::setBucketRedirect({$bucket}, {$location}): Empty parameter.", __FILE__, __LINE__);
 | 
        
           |  |  | 864 | 			return false;
 | 
        
           |  |  | 865 | 		}
 | 
        
           |  |  | 866 |   | 
        
           |  |  | 867 | 		$dom = new DOMDocument;
 | 
        
           |  |  | 868 | 		$websiteConfiguration = $dom->createElement('WebsiteConfiguration');
 | 
        
           |  |  | 869 | 		$redirectAllRequestsTo = $dom->createElement('RedirectAllRequestsTo');
 | 
        
           |  |  | 870 | 		$hostName = $dom->createElement('HostName', $location);
 | 
        
           |  |  | 871 | 		$redirectAllRequestsTo->appendChild($hostName);
 | 
        
           |  |  | 872 | 		$websiteConfiguration->appendChild($redirectAllRequestsTo);
 | 
        
           |  |  | 873 | 		$dom->appendChild($websiteConfiguration);
 | 
        
           |  |  | 874 | 		$rest->setParameter('website', null);
 | 
        
           |  |  | 875 | 		$rest->data = $dom->saveXML();
 | 
        
           |  |  | 876 | 		$rest->size = strlen($rest->data);
 | 
        
           |  |  | 877 | 		$rest->setHeader('Content-Type', 'application/xml');
 | 
        
           |  |  | 878 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 879 |   | 
        
           |  |  | 880 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 881 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 882 | 		if ($rest->error !== false)
 | 
        
           |  |  | 883 | 		{
 | 
        
           |  |  | 884 | 			self::__triggerError(sprintf("S3::setBucketRedirect({$bucket}, {$location}): [%s] %s",
 | 
        
           |  |  | 885 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 886 | 			return false;
 | 
        
           |  |  | 887 | 		}
 | 
        
           |  |  | 888 | 		return true;
 | 
        
           |  |  | 889 | 	}
 | 
        
           |  |  | 890 |   | 
        
           |  |  | 891 |   | 
        
           |  |  | 892 | 	/**
 | 
        
           |  |  | 893 | 	* Set logging for a bucket
 | 
        
           |  |  | 894 | 	*
 | 
        
           |  |  | 895 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 896 | 	* @param string $targetBucket Target bucket (where logs are stored)
 | 
        
           |  |  | 897 | 	* @param string $targetPrefix Log prefix (e,g; domain.com-)
 | 
        
           |  |  | 898 | 	* @return boolean
 | 
        
           |  |  | 899 | 	*/
 | 
        
           |  |  | 900 | 	public static function setBucketLogging($bucket, $targetBucket, $targetPrefix = null)
 | 
        
           |  |  | 901 | 	{
 | 
        
           |  |  | 902 | 		// The S3 log delivery group has to be added to the target bucket's ACP
 | 
        
           |  |  | 903 | 		if ($targetBucket !== null && ($acp = self::getAccessControlPolicy($targetBucket, '')) !== false)
 | 
        
           |  |  | 904 | 		{
 | 
        
           |  |  | 905 | 			// Only add permissions to the target bucket when they do not exist
 | 
        
           |  |  | 906 | 			$aclWriteSet = false;
 | 
        
           |  |  | 907 | 			$aclReadSet = false;
 | 
        
           |  |  | 908 | 			foreach ($acp['acl'] as $acl)
 | 
        
           |  |  | 909 | 			if ($acl['type'] == 'Group' && $acl['uri'] == 'http://acs.amazonaws.com/groups/s3/LogDelivery')
 | 
        
           |  |  | 910 | 			{
 | 
        
           |  |  | 911 | 				if ($acl['permission'] == 'WRITE') $aclWriteSet = true;
 | 
        
           |  |  | 912 | 				elseif ($acl['permission'] == 'READ_ACP') $aclReadSet = true;
 | 
        
           |  |  | 913 | 			}
 | 
        
           |  |  | 914 | 			if (!$aclWriteSet) $acp['acl'][] = array(
 | 
        
           |  |  | 915 | 				'type' => 'Group', 'uri' => 'http://acs.amazonaws.com/groups/s3/LogDelivery', 'permission' => 'WRITE'
 | 
        
           |  |  | 916 | 			);
 | 
        
           |  |  | 917 | 			if (!$aclReadSet) $acp['acl'][] = array(
 | 
        
           |  |  | 918 | 				'type' => 'Group', 'uri' => 'http://acs.amazonaws.com/groups/s3/LogDelivery', 'permission' => 'READ_ACP'
 | 
        
           |  |  | 919 | 			);
 | 
        
           |  |  | 920 | 			if (!$aclReadSet || !$aclWriteSet) self::setAccessControlPolicy($targetBucket, '', $acp);
 | 
        
           |  |  | 921 | 		}
 | 
        
           |  |  | 922 |   | 
        
           |  |  | 923 | 		$dom = new DOMDocument;
 | 
        
           |  |  | 924 | 		$bucketLoggingStatus = $dom->createElement('BucketLoggingStatus');
 | 
        
           |  |  | 925 | 		$bucketLoggingStatus->setAttribute('xmlns', 'http://s3.amazonaws.com/doc/2006-03-01/');
 | 
        
           |  |  | 926 | 		if ($targetBucket !== null)
 | 
        
           |  |  | 927 | 		{
 | 
        
           |  |  | 928 | 			if ($targetPrefix == null) $targetPrefix = $bucket . '-';
 | 
        
           |  |  | 929 | 			$loggingEnabled = $dom->createElement('LoggingEnabled');
 | 
        
           |  |  | 930 | 			$loggingEnabled->appendChild($dom->createElement('TargetBucket', $targetBucket));
 | 
        
           |  |  | 931 | 			$loggingEnabled->appendChild($dom->createElement('TargetPrefix', $targetPrefix));
 | 
        
           |  |  | 932 | 			// TODO: Add TargetGrants?
 | 
        
           |  |  | 933 | 			$bucketLoggingStatus->appendChild($loggingEnabled);
 | 
        
           |  |  | 934 | 		}
 | 
        
           |  |  | 935 | 		$dom->appendChild($bucketLoggingStatus);
 | 
        
           |  |  | 936 |   | 
        
           |  |  | 937 | 		$rest = new S3Request('PUT', $bucket, '', self::$endpoint);
 | 
        
           |  |  | 938 | 		$rest->setParameter('logging', null);
 | 
        
           |  |  | 939 | 		$rest->data = $dom->saveXML();
 | 
        
           |  |  | 940 | 		$rest->size = strlen($rest->data);
 | 
        
           |  |  | 941 | 		$rest->setHeader('Content-Type', 'application/xml');
 | 
        
           |  |  | 942 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 943 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 944 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 945 | 		if ($rest->error !== false)
 | 
        
           |  |  | 946 | 		{
 | 
        
           |  |  | 947 | 			self::__triggerError(sprintf("S3::setBucketLogging({$bucket}, {$targetBucket}): [%s] %s",
 | 
        
           |  |  | 948 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 949 | 			return false;
 | 
        
           |  |  | 950 | 		}
 | 
        
           |  |  | 951 | 		return true;
 | 
        
           |  |  | 952 | 	}
 | 
        
           |  |  | 953 |   | 
        
           |  |  | 954 |   | 
        
           |  |  | 955 | 	/**
 | 
        
           |  |  | 956 | 	* Get logging status for a bucket
 | 
        
           |  |  | 957 | 	*
 | 
        
           |  |  | 958 | 	* This will return false if logging is not enabled.
 | 
        
           |  |  | 959 | 	* Note: To enable logging, you also need to grant write access to the log group
 | 
        
           |  |  | 960 | 	*
 | 
        
           |  |  | 961 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 962 | 	* @return array | false
 | 
        
           |  |  | 963 | 	*/
 | 
        
           |  |  | 964 | 	public static function getBucketLogging($bucket)
 | 
        
           |  |  | 965 | 	{
 | 
        
           |  |  | 966 | 		$rest = new S3Request('GET', $bucket, '', self::$endpoint);
 | 
        
           |  |  | 967 | 		$rest->setParameter('logging', null);
 | 
        
           |  |  | 968 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 969 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 970 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 971 | 		if ($rest->error !== false)
 | 
        
           |  |  | 972 | 		{
 | 
        
           |  |  | 973 | 			self::__triggerError(sprintf("S3::getBucketLogging({$bucket}): [%s] %s",
 | 
        
           |  |  | 974 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 975 | 			return false;
 | 
        
           |  |  | 976 | 		}
 | 
        
           |  |  | 977 | 		if (!isset($rest->body->LoggingEnabled)) return false; // No logging
 | 
        
           |  |  | 978 | 		return array(
 | 
        
           |  |  | 979 | 			'targetBucket' => (string)$rest->body->LoggingEnabled->TargetBucket,
 | 
        
           |  |  | 980 | 			'targetPrefix' => (string)$rest->body->LoggingEnabled->TargetPrefix,
 | 
        
           |  |  | 981 | 		);
 | 
        
           |  |  | 982 | 	}
 | 
        
           |  |  | 983 |   | 
        
           |  |  | 984 |   | 
        
           |  |  | 985 | 	/**
 | 
        
           |  |  | 986 | 	* Disable bucket logging
 | 
        
           |  |  | 987 | 	*
 | 
        
           |  |  | 988 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 989 | 	* @return boolean
 | 
        
           |  |  | 990 | 	*/
 | 
        
           |  |  | 991 | 	public static function disableBucketLogging($bucket)
 | 
        
           |  |  | 992 | 	{
 | 
        
           |  |  | 993 | 		return self::setBucketLogging($bucket, null);
 | 
        
           |  |  | 994 | 	}
 | 
        
           |  |  | 995 |   | 
        
           |  |  | 996 |   | 
        
           |  |  | 997 | 	/**
 | 
        
           |  |  | 998 | 	* Get a bucket's location
 | 
        
           |  |  | 999 | 	*
 | 
        
           |  |  | 1000 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 1001 | 	* @return string | false
 | 
        
           |  |  | 1002 | 	*/
 | 
        
           |  |  | 1003 | 	public static function getBucketLocation($bucket)
 | 
        
           |  |  | 1004 | 	{
 | 
        
           |  |  | 1005 | 		$rest = new S3Request('GET', $bucket, '', self::$endpoint);
 | 
        
           |  |  | 1006 | 		$rest->setParameter('location', null);
 | 
        
           |  |  | 1007 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 1008 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 1009 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1010 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1011 | 		{
 | 
        
           |  |  | 1012 | 			self::__triggerError(sprintf("S3::getBucketLocation({$bucket}): [%s] %s",
 | 
        
           |  |  | 1013 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 1014 | 			return false;
 | 
        
           |  |  | 1015 | 		}
 | 
        
           |  |  | 1016 | 		return (isset($rest->body[0]) && (string)$rest->body[0] !== '') ? (string)$rest->body[0] : 'US';
 | 
        
           |  |  | 1017 | 	}
 | 
        
           |  |  | 1018 |   | 
        
           |  |  | 1019 |   | 
        
           |  |  | 1020 | 	/**
 | 
        
           |  |  | 1021 | 	* Set object or bucket Access Control Policy
 | 
        
           |  |  | 1022 | 	*
 | 
        
           |  |  | 1023 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 1024 | 	* @param string $uri Object URI
 | 
        
           |  |  | 1025 | 	* @param array $acp Access Control Policy Data (same as the data returned from getAccessControlPolicy)
 | 
        
           |  |  | 1026 | 	* @return boolean
 | 
        
           |  |  | 1027 | 	*/
 | 
        
           |  |  | 1028 | 	public static function setAccessControlPolicy($bucket, $uri = '', $acp = array())
 | 
        
           |  |  | 1029 | 	{
 | 
        
           |  |  | 1030 | 		$dom = new DOMDocument;
 | 
        
           |  |  | 1031 | 		$dom->formatOutput = true;
 | 
        
           |  |  | 1032 | 		$accessControlPolicy = $dom->createElement('AccessControlPolicy');
 | 
        
           |  |  | 1033 | 		$accessControlList = $dom->createElement('AccessControlList');
 | 
        
           |  |  | 1034 |   | 
        
           |  |  | 1035 | 		// It seems the owner has to be passed along too
 | 
        
           |  |  | 1036 | 		$owner = $dom->createElement('Owner');
 | 
        
           |  |  | 1037 | 		$owner->appendChild($dom->createElement('ID', $acp['owner']['id']));
 | 
        
           |  |  | 1038 | 		$owner->appendChild($dom->createElement('DisplayName', $acp['owner']['name']));
 | 
        
           |  |  | 1039 | 		$accessControlPolicy->appendChild($owner);
 | 
        
           |  |  | 1040 |   | 
        
           |  |  | 1041 | 		foreach ($acp['acl'] as $g)
 | 
        
           |  |  | 1042 | 		{
 | 
        
           |  |  | 1043 | 			$grant = $dom->createElement('Grant');
 | 
        
           |  |  | 1044 | 			$grantee = $dom->createElement('Grantee');
 | 
        
           |  |  | 1045 | 			$grantee->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
 | 
        
           |  |  | 1046 | 			if (isset($g['id']))
 | 
        
           |  |  | 1047 | 			{ // CanonicalUser (DisplayName is omitted)
 | 
        
           |  |  | 1048 | 				$grantee->setAttribute('xsi:type', 'CanonicalUser');
 | 
        
           |  |  | 1049 | 				$grantee->appendChild($dom->createElement('ID', $g['id']));
 | 
        
           |  |  | 1050 | 			}
 | 
        
           |  |  | 1051 | 			elseif (isset($g['email']))
 | 
        
           |  |  | 1052 | 			{ // AmazonCustomerByEmail
 | 
        
           |  |  | 1053 | 				$grantee->setAttribute('xsi:type', 'AmazonCustomerByEmail');
 | 
        
           |  |  | 1054 | 				$grantee->appendChild($dom->createElement('EmailAddress', $g['email']));
 | 
        
           |  |  | 1055 | 			}
 | 
        
           |  |  | 1056 | 			elseif ($g['type'] == 'Group')
 | 
        
           |  |  | 1057 | 			{ // Group
 | 
        
           |  |  | 1058 | 				$grantee->setAttribute('xsi:type', 'Group');
 | 
        
           |  |  | 1059 | 				$grantee->appendChild($dom->createElement('URI', $g['uri']));
 | 
        
           |  |  | 1060 | 			}
 | 
        
           |  |  | 1061 | 			$grant->appendChild($grantee);
 | 
        
           |  |  | 1062 | 			$grant->appendChild($dom->createElement('Permission', $g['permission']));
 | 
        
           |  |  | 1063 | 			$accessControlList->appendChild($grant);
 | 
        
           |  |  | 1064 | 		}
 | 
        
           |  |  | 1065 |   | 
        
           |  |  | 1066 | 		$accessControlPolicy->appendChild($accessControlList);
 | 
        
           |  |  | 1067 | 		$dom->appendChild($accessControlPolicy);
 | 
        
           |  |  | 1068 |   | 
        
           |  |  | 1069 | 		$rest = new S3Request('PUT', $bucket, $uri, self::$endpoint);
 | 
        
           |  |  | 1070 | 		$rest->setParameter('acl', null);
 | 
        
           |  |  | 1071 | 		$rest->data = $dom->saveXML();
 | 
        
           |  |  | 1072 | 		$rest->size = strlen($rest->data);
 | 
        
           |  |  | 1073 | 		$rest->setHeader('Content-Type', 'application/xml');
 | 
        
           |  |  | 1074 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 1075 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 1076 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1077 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1078 | 		{
 | 
        
           |  |  | 1079 | 			self::__triggerError(sprintf("S3::setAccessControlPolicy({$bucket}, {$uri}): [%s] %s",
 | 
        
           |  |  | 1080 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 1081 | 			return false;
 | 
        
           |  |  | 1082 | 		}
 | 
        
           |  |  | 1083 | 		return true;
 | 
        
           |  |  | 1084 | 	}
 | 
        
           |  |  | 1085 |   | 
        
           |  |  | 1086 |   | 
        
           |  |  | 1087 | 	/**
 | 
        
           |  |  | 1088 | 	* Get object or bucket Access Control Policy
 | 
        
           |  |  | 1089 | 	*
 | 
        
           |  |  | 1090 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 1091 | 	* @param string $uri Object URI
 | 
        
           |  |  | 1092 | 	* @return mixed | false
 | 
        
           |  |  | 1093 | 	*/
 | 
        
           |  |  | 1094 | 	public static function getAccessControlPolicy($bucket, $uri = '')
 | 
        
           |  |  | 1095 | 	{
 | 
        
           |  |  | 1096 | 		$rest = new S3Request('GET', $bucket, $uri, self::$endpoint);
 | 
        
           |  |  | 1097 | 		$rest->setParameter('acl', null);
 | 
        
           |  |  | 1098 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 1099 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 1100 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1101 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1102 | 		{
 | 
        
           |  |  | 1103 | 			self::__triggerError(sprintf("S3::getAccessControlPolicy({$bucket}, {$uri}): [%s] %s",
 | 
        
           |  |  | 1104 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 1105 | 			return false;
 | 
        
           |  |  | 1106 | 		}
 | 
        
           |  |  | 1107 |   | 
        
           |  |  | 1108 | 		$acp = array();
 | 
        
           |  |  | 1109 | 		if (isset($rest->body->Owner, $rest->body->Owner->ID, $rest->body->Owner->DisplayName))
 | 
        
           |  |  | 1110 | 			$acp['owner'] = array(
 | 
        
           |  |  | 1111 | 				'id' => (string)$rest->body->Owner->ID, 'name' => (string)$rest->body->Owner->DisplayName
 | 
        
           |  |  | 1112 | 			);
 | 
        
           |  |  | 1113 |   | 
        
           |  |  | 1114 | 		if (isset($rest->body->AccessControlList))
 | 
        
           |  |  | 1115 | 		{
 | 
        
           |  |  | 1116 | 			$acp['acl'] = array();
 | 
        
           |  |  | 1117 | 			foreach ($rest->body->AccessControlList->Grant as $grant)
 | 
        
           |  |  | 1118 | 			{
 | 
        
           |  |  | 1119 | 				foreach ($grant->Grantee as $grantee)
 | 
        
           |  |  | 1120 | 				{
 | 
        
           |  |  | 1121 | 					if (isset($grantee->ID, $grantee->DisplayName)) // CanonicalUser
 | 
        
           |  |  | 1122 | 						$acp['acl'][] = array(
 | 
        
           |  |  | 1123 | 							'type' => 'CanonicalUser',
 | 
        
           |  |  | 1124 | 							'id' => (string)$grantee->ID,
 | 
        
           |  |  | 1125 | 							'name' => (string)$grantee->DisplayName,
 | 
        
           |  |  | 1126 | 							'permission' => (string)$grant->Permission
 | 
        
           |  |  | 1127 | 						);
 | 
        
           |  |  | 1128 | 					elseif (isset($grantee->EmailAddress)) // AmazonCustomerByEmail
 | 
        
           |  |  | 1129 | 						$acp['acl'][] = array(
 | 
        
           |  |  | 1130 | 							'type' => 'AmazonCustomerByEmail',
 | 
        
           |  |  | 1131 | 							'email' => (string)$grantee->EmailAddress,
 | 
        
           |  |  | 1132 | 							'permission' => (string)$grant->Permission
 | 
        
           |  |  | 1133 | 						);
 | 
        
           |  |  | 1134 | 					elseif (isset($grantee->URI)) // Group
 | 
        
           |  |  | 1135 | 						$acp['acl'][] = array(
 | 
        
           |  |  | 1136 | 							'type' => 'Group',
 | 
        
           |  |  | 1137 | 							'uri' => (string)$grantee->URI,
 | 
        
           |  |  | 1138 | 							'permission' => (string)$grant->Permission
 | 
        
           |  |  | 1139 | 						);
 | 
        
           |  |  | 1140 | 					else continue;
 | 
        
           |  |  | 1141 | 				}
 | 
        
           |  |  | 1142 | 			}
 | 
        
           |  |  | 1143 | 		}
 | 
        
           |  |  | 1144 | 		return $acp;
 | 
        
           |  |  | 1145 | 	}
 | 
        
           |  |  | 1146 |   | 
        
           |  |  | 1147 |   | 
        
           |  |  | 1148 | 	/**
 | 
        
           |  |  | 1149 | 	* Delete an object
 | 
        
           |  |  | 1150 | 	*
 | 
        
           |  |  | 1151 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 1152 | 	* @param string $uri Object URI
 | 
        
           |  |  | 1153 | 	* @return boolean
 | 
        
           |  |  | 1154 | 	*/
 | 
        
           |  |  | 1155 | 	public static function deleteObject($bucket, $uri)
 | 
        
           |  |  | 1156 | 	{
 | 
        
           |  |  | 1157 | 		$rest = new S3Request('DELETE', $bucket, $uri, self::$endpoint);
 | 
        
           |  |  | 1158 | 		$rest = $rest->getResponse();
 | 
        
           |  |  | 1159 | 		if ($rest->error === false && $rest->code !== 204)
 | 
        
           |  |  | 1160 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1161 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1162 | 		{
 | 
        
           |  |  | 1163 | 			self::__triggerError(sprintf("S3::deleteObject(): [%s] %s",
 | 
        
           |  |  | 1164 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 1165 | 			return false;
 | 
        
           |  |  | 1166 | 		}
 | 
        
           |  |  | 1167 | 		return true;
 | 
        
           |  |  | 1168 | 	}
 | 
        
           |  |  | 1169 |   | 
        
           |  |  | 1170 |   | 
        
           |  |  | 1171 | 	/**
 | 
        
           |  |  | 1172 | 	* Get a query string authenticated URL
 | 
        
           |  |  | 1173 | 	*
 | 
        
           |  |  | 1174 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 1175 | 	* @param string $uri Object URI
 | 
        
           |  |  | 1176 | 	* @param integer $lifetime Lifetime in seconds
 | 
        
           |  |  | 1177 | 	* @param boolean $hostBucket Use the bucket name as the hostname
 | 
        
           |  |  | 1178 | 	* @param boolean $https Use HTTPS ($hostBucket should be false for SSL verification)
 | 
        
           |  |  | 1179 | 	* @return string
 | 
        
           |  |  | 1180 | 	*/
 | 
        
           |  |  | 1181 | 	public static function getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false)
 | 
        
           |  |  | 1182 | 	{
 | 
        
           |  |  | 1183 | 		$expires = self::__getTime() + $lifetime;
 | 
        
           |  |  | 1184 | 		$uri = str_replace(array('%2F', '%2B'), array('/', '+'), rawurlencode($uri));
 | 
        
           |  |  | 1185 | 		return sprintf(($https ? 'https' : 'http').'://%s/%s?AWSAccessKeyId=%s&Expires=%u&Signature=%s',
 | 
        
           |  |  | 1186 | 		// $hostBucket ? $bucket : $bucket.'.s3.amazonaws.com', $uri, self::$__accessKey, $expires,
 | 
        
           |  |  | 1187 | 		$hostBucket ? $bucket : self::$endpoint.'/'.$bucket, $uri, self::$__accessKey, $expires,
 | 
        
           |  |  | 1188 | 		urlencode(self::__getHash("GET\n\n\n{$expires}\n/{$bucket}/{$uri}")));
 | 
        
           |  |  | 1189 | 	}
 | 
        
           |  |  | 1190 |   | 
        
           |  |  | 1191 |   | 
        
           |  |  | 1192 | 	/**
 | 
        
           |  |  | 1193 | 	* Get a CloudFront signed policy URL
 | 
        
           |  |  | 1194 | 	*
 | 
        
           |  |  | 1195 | 	* @param array $policy Policy
 | 
        
           |  |  | 1196 | 	* @return string
 | 
        
           |  |  | 1197 | 	*/
 | 
        
           |  |  | 1198 | 	public static function getSignedPolicyURL($policy)
 | 
        
           |  |  | 1199 | 	{
 | 
        
           |  |  | 1200 | 		$data = json_encode($policy);
 | 
        
           |  |  | 1201 | 		$signature = '';
 | 
        
           |  |  | 1202 | 		if (!openssl_sign($data, $signature, self::$__signingKeyResource)) return false;
 | 
        
           |  |  | 1203 |   | 
        
           |  |  | 1204 | 		$encoded = str_replace(array('+', '='), array('-', '_', '~'), base64_encode($data));
 | 
        
           |  |  | 1205 | 		$signature = str_replace(array('+', '='), array('-', '_', '~'), base64_encode($signature));
 | 
        
           |  |  | 1206 |   | 
        
           |  |  | 1207 | 		$url = $policy['Statement'][0]['Resource'] . '?';
 | 
        
           |  |  | 1208 | 		foreach (array('Policy' => $encoded, 'Signature' => $signature, 'Key-Pair-Id' => self::$__signingKeyPairId) as $k => $v)
 | 
        
           |  |  | 1209 | 			$url .= $k.'='.str_replace('%2F', '/', rawurlencode($v)).'&';
 | 
        
           |  |  | 1210 | 		return substr($url, 0, -1);
 | 
        
           |  |  | 1211 | 	}
 | 
        
           |  |  | 1212 |   | 
        
           |  |  | 1213 |   | 
        
           |  |  | 1214 | 	/**
 | 
        
           |  |  | 1215 | 	* Get a CloudFront canned policy URL
 | 
        
           |  |  | 1216 | 	*
 | 
        
           |  |  | 1217 | 	* @param string $url URL to sign
 | 
        
           |  |  | 1218 | 	* @param integer $lifetime URL lifetime
 | 
        
           |  |  | 1219 | 	* @return string
 | 
        
           |  |  | 1220 | 	*/
 | 
        
           |  |  | 1221 | 	public static function getSignedCannedURL($url, $lifetime)
 | 
        
           |  |  | 1222 | 	{
 | 
        
           |  |  | 1223 | 		return self::getSignedPolicyURL(array(
 | 
        
           |  |  | 1224 | 			'Statement' => array(
 | 
        
           |  |  | 1225 | 				array('Resource' => $url, 'Condition' => array(
 | 
        
           |  |  | 1226 | 					'DateLessThan' => array('AWS:EpochTime' => self::__getTime() + $lifetime)
 | 
        
           |  |  | 1227 | 				))
 | 
        
           |  |  | 1228 | 			)
 | 
        
           |  |  | 1229 | 		));
 | 
        
           |  |  | 1230 | 	}
 | 
        
           |  |  | 1231 |   | 
        
           |  |  | 1232 |   | 
        
           |  |  | 1233 | 	/**
 | 
        
           |  |  | 1234 | 	* Get upload POST parameters for form uploads
 | 
        
           |  |  | 1235 | 	*
 | 
        
           |  |  | 1236 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 1237 | 	* @param string $uriPrefix Object URI prefix
 | 
        
           |  |  | 1238 | 	* @param constant $acl ACL constant
 | 
        
           |  |  | 1239 | 	* @param integer $lifetime Lifetime in seconds
 | 
        
           |  |  | 1240 | 	* @param integer $maxFileSize Maximum filesize in bytes (default 5MB)
 | 
        
           |  |  | 1241 | 	* @param string $successRedirect Redirect URL or 200 / 201 status code
 | 
        
           |  |  | 1242 | 	* @param array $amzHeaders Array of x-amz-meta-* headers
 | 
        
           |  |  | 1243 | 	* @param array $headers Array of request headers or content type as a string
 | 
        
           |  |  | 1244 | 	* @param boolean $flashVars Includes additional "Filename" variable posted by Flash
 | 
        
           |  |  | 1245 | 	* @return object
 | 
        
           |  |  | 1246 | 	*/
 | 
        
           |  |  | 1247 | 	public static function getHttpUploadPostParams($bucket, $uriPrefix = '', $acl = self::ACL_PRIVATE, $lifetime = 3600,
 | 
        
           |  |  | 1248 | 	$maxFileSize = 5242880, $successRedirect = "201", $amzHeaders = array(), $headers = array(), $flashVars = false)
 | 
        
           |  |  | 1249 | 	{
 | 
        
           |  |  | 1250 | 		// Create policy object
 | 
        
           |  |  | 1251 | 		$policy = new stdClass;
 | 
        
           |  |  | 1252 | 		$policy->expiration = gmdate('Y-m-d\TH:i:s\Z', (self::__getTime() + $lifetime));
 | 
        
           |  |  | 1253 | 		$policy->conditions = array();
 | 
        
           |  |  | 1254 | 		$obj = new stdClass; $obj->bucket = $bucket; array_push($policy->conditions, $obj);
 | 
        
           |  |  | 1255 | 		$obj = new stdClass; $obj->acl = $acl; array_push($policy->conditions, $obj);
 | 
        
           |  |  | 1256 |   | 
        
           |  |  | 1257 | 		$obj = new stdClass; // 200 for non-redirect uploads
 | 
        
           |  |  | 1258 | 		if (is_numeric($successRedirect) && in_array((int)$successRedirect, array(200, 201)))
 | 
        
           |  |  | 1259 | 			$obj->success_action_status = (string)$successRedirect;
 | 
        
           |  |  | 1260 | 		else // URL
 | 
        
           |  |  | 1261 | 			$obj->success_action_redirect = $successRedirect;
 | 
        
           |  |  | 1262 | 		array_push($policy->conditions, $obj);
 | 
        
           |  |  | 1263 |   | 
        
           |  |  | 1264 | 		if ($acl !== self::ACL_PUBLIC_READ)
 | 
        
           |  |  | 1265 | 			array_push($policy->conditions, array('eq', '$acl', $acl));
 | 
        
           |  |  | 1266 |   | 
        
           |  |  | 1267 | 		array_push($policy->conditions, array('starts-with', '$key', $uriPrefix));
 | 
        
           |  |  | 1268 | 		if ($flashVars) array_push($policy->conditions, array('starts-with', '$Filename', ''));
 | 
        
           |  |  | 1269 | 		foreach (array_keys($headers) as $headerKey)
 | 
        
           |  |  | 1270 | 			array_push($policy->conditions, array('starts-with', '$'.$headerKey, ''));
 | 
        
           |  |  | 1271 | 		foreach ($amzHeaders as $headerKey => $headerVal)
 | 
        
           |  |  | 1272 | 		{
 | 
        
           |  |  | 1273 | 			$obj = new stdClass;
 | 
        
           |  |  | 1274 | 			$obj->{$headerKey} = (string)$headerVal;
 | 
        
           |  |  | 1275 | 			array_push($policy->conditions, $obj);
 | 
        
           |  |  | 1276 | 		}
 | 
        
           |  |  | 1277 | 		array_push($policy->conditions, array('content-length-range', 0, $maxFileSize));
 | 
        
           |  |  | 1278 | 		$policy = base64_encode(str_replace('\/', '/', json_encode($policy)));
 | 
        
           |  |  | 1279 |   | 
        
           |  |  | 1280 | 		// Create parameters
 | 
        
           |  |  | 1281 | 		$params = new stdClass;
 | 
        
           |  |  | 1282 | 		$params->AWSAccessKeyId = self::$__accessKey;
 | 
        
           |  |  | 1283 | 		$params->key = $uriPrefix.'${filename}';
 | 
        
           |  |  | 1284 | 		$params->acl = $acl;
 | 
        
           |  |  | 1285 | 		$params->policy = $policy; unset($policy);
 | 
        
           |  |  | 1286 | 		$params->signature = self::__getHash($params->policy);
 | 
        
           |  |  | 1287 | 		if (is_numeric($successRedirect) && in_array((int)$successRedirect, array(200, 201)))
 | 
        
           |  |  | 1288 | 			$params->success_action_status = (string)$successRedirect;
 | 
        
           |  |  | 1289 | 		else
 | 
        
           |  |  | 1290 | 			$params->success_action_redirect = $successRedirect;
 | 
        
           |  |  | 1291 | 		foreach ($headers as $headerKey => $headerVal) $params->{$headerKey} = (string)$headerVal;
 | 
        
           |  |  | 1292 | 		foreach ($amzHeaders as $headerKey => $headerVal) $params->{$headerKey} = (string)$headerVal;
 | 
        
           |  |  | 1293 | 		return $params;
 | 
        
           |  |  | 1294 | 	}
 | 
        
           |  |  | 1295 |   | 
        
           |  |  | 1296 |   | 
        
           |  |  | 1297 | 	/**
 | 
        
           |  |  | 1298 | 	* Create a CloudFront distribution
 | 
        
           |  |  | 1299 | 	*
 | 
        
           |  |  | 1300 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 1301 | 	* @param boolean $enabled Enabled (true/false)
 | 
        
           |  |  | 1302 | 	* @param array $cnames Array containing CNAME aliases
 | 
        
           |  |  | 1303 | 	* @param string $comment Use the bucket name as the hostname
 | 
        
           |  |  | 1304 | 	* @param string $defaultRootObject Default root object
 | 
        
           |  |  | 1305 | 	* @param string $originAccessIdentity Origin access identity
 | 
        
           |  |  | 1306 | 	* @param array $trustedSigners Array of trusted signers
 | 
        
           |  |  | 1307 | 	* @return array | false
 | 
        
           |  |  | 1308 | 	*/
 | 
        
           |  |  | 1309 | 	public static function createDistribution($bucket, $enabled = true, $cnames = array(), $comment = null, $defaultRootObject = null, $originAccessIdentity = null, $trustedSigners = array())
 | 
        
           |  |  | 1310 | 	{
 | 
        
           |  |  | 1311 | 		if (!extension_loaded('openssl'))
 | 
        
           |  |  | 1312 | 		{
 | 
        
           |  |  | 1313 | 			self::__triggerError(sprintf("S3::createDistribution({$bucket}, ".(int)$enabled.", [], '$comment'): %s",
 | 
        
           |  |  | 1314 | 			"CloudFront functionality requires SSL"), __FILE__, __LINE__);
 | 
        
           |  |  | 1315 | 			return false;
 | 
        
           |  |  | 1316 | 		}
 | 
        
           |  |  | 1317 | 		$useSSL = self::$useSSL;
 | 
        
           |  |  | 1318 |   | 
        
           |  |  | 1319 | 		self::$useSSL = true; // CloudFront requires SSL
 | 
        
           |  |  | 1320 | 		$rest = new S3Request('POST', '', '2010-11-01/distribution', 'cloudfront.amazonaws.com');
 | 
        
           |  |  | 1321 | 		$rest->data = self::__getCloudFrontDistributionConfigXML(
 | 
        
           |  |  | 1322 | 			$bucket.'.s3.amazonaws.com',
 | 
        
           |  |  | 1323 | 			$enabled,
 | 
        
           |  |  | 1324 | 			(string)$comment,
 | 
        
           |  |  | 1325 | 			(string)microtime(true),
 | 
        
           |  |  | 1326 | 			$cnames,
 | 
        
           |  |  | 1327 | 			$defaultRootObject,
 | 
        
           |  |  | 1328 | 			$originAccessIdentity,
 | 
        
           |  |  | 1329 | 			$trustedSigners
 | 
        
           |  |  | 1330 | 		);
 | 
        
           |  |  | 1331 |   | 
        
           |  |  | 1332 | 		$rest->size = strlen($rest->data);
 | 
        
           |  |  | 1333 | 		$rest->setHeader('Content-Type', 'application/xml');
 | 
        
           |  |  | 1334 | 		$rest = self::__getCloudFrontResponse($rest);
 | 
        
           |  |  | 1335 |   | 
        
           |  |  | 1336 | 		self::$useSSL = $useSSL;
 | 
        
           |  |  | 1337 |   | 
        
           |  |  | 1338 | 		if ($rest->error === false && $rest->code !== 201)
 | 
        
           |  |  | 1339 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1340 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1341 | 		{
 | 
        
           |  |  | 1342 | 			self::__triggerError(sprintf("S3::createDistribution({$bucket}, ".(int)$enabled.", [], '$comment'): [%s] %s",
 | 
        
           |  |  | 1343 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 1344 | 			return false;
 | 
        
           |  |  | 1345 | 		} elseif ($rest->body instanceof SimpleXMLElement)
 | 
        
           |  |  | 1346 | 			return self::__parseCloudFrontDistributionConfig($rest->body);
 | 
        
           |  |  | 1347 | 		return false;
 | 
        
           |  |  | 1348 | 	}
 | 
        
           |  |  | 1349 |   | 
        
           |  |  | 1350 |   | 
        
           |  |  | 1351 | 	/**
 | 
        
           |  |  | 1352 | 	* Get CloudFront distribution info
 | 
        
           |  |  | 1353 | 	*
 | 
        
           |  |  | 1354 | 	* @param string $distributionId Distribution ID from listDistributions()
 | 
        
           |  |  | 1355 | 	* @return array | false
 | 
        
           |  |  | 1356 | 	*/
 | 
        
           |  |  | 1357 | 	public static function getDistribution($distributionId)
 | 
        
           |  |  | 1358 | 	{
 | 
        
           |  |  | 1359 | 		if (!extension_loaded('openssl'))
 | 
        
           |  |  | 1360 | 		{
 | 
        
           |  |  | 1361 | 			self::__triggerError(sprintf("S3::getDistribution($distributionId): %s",
 | 
        
           |  |  | 1362 | 			"CloudFront functionality requires SSL"), __FILE__, __LINE__);
 | 
        
           |  |  | 1363 | 			return false;
 | 
        
           |  |  | 1364 | 		}
 | 
        
           |  |  | 1365 | 		$useSSL = self::$useSSL;
 | 
        
           |  |  | 1366 |   | 
        
           |  |  | 1367 | 		self::$useSSL = true; // CloudFront requires SSL
 | 
        
           |  |  | 1368 | 		$rest = new S3Request('GET', '', '2010-11-01/distribution/'.$distributionId, 'cloudfront.amazonaws.com');
 | 
        
           |  |  | 1369 | 		$rest = self::__getCloudFrontResponse($rest);
 | 
        
           |  |  | 1370 |   | 
        
           |  |  | 1371 | 		self::$useSSL = $useSSL;
 | 
        
           |  |  | 1372 |   | 
        
           |  |  | 1373 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 1374 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1375 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1376 | 		{
 | 
        
           |  |  | 1377 | 			self::__triggerError(sprintf("S3::getDistribution($distributionId): [%s] %s",
 | 
        
           |  |  | 1378 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 1379 | 			return false;
 | 
        
           |  |  | 1380 | 		}
 | 
        
           |  |  | 1381 | 		elseif ($rest->body instanceof SimpleXMLElement)
 | 
        
           |  |  | 1382 | 		{
 | 
        
           |  |  | 1383 | 			$dist = self::__parseCloudFrontDistributionConfig($rest->body);
 | 
        
           |  |  | 1384 | 			$dist['hash'] = $rest->headers['hash'];
 | 
        
           |  |  | 1385 | 			$dist['id'] = $distributionId;
 | 
        
           |  |  | 1386 | 			return $dist;
 | 
        
           |  |  | 1387 | 		}
 | 
        
           |  |  | 1388 | 		return false;
 | 
        
           |  |  | 1389 | 	}
 | 
        
           |  |  | 1390 |   | 
        
           |  |  | 1391 |   | 
        
           |  |  | 1392 | 	/**
 | 
        
           |  |  | 1393 | 	* Update a CloudFront distribution
 | 
        
           |  |  | 1394 | 	*
 | 
        
           |  |  | 1395 | 	* @param array $dist Distribution array info identical to output of getDistribution()
 | 
        
           |  |  | 1396 | 	* @return array | false
 | 
        
           |  |  | 1397 | 	*/
 | 
        
           |  |  | 1398 | 	public static function updateDistribution($dist)
 | 
        
           |  |  | 1399 | 	{
 | 
        
           |  |  | 1400 | 		if (!extension_loaded('openssl'))
 | 
        
           |  |  | 1401 | 		{
 | 
        
           |  |  | 1402 | 			self::__triggerError(sprintf("S3::updateDistribution({$dist['id']}): %s",
 | 
        
           |  |  | 1403 | 			"CloudFront functionality requires SSL"), __FILE__, __LINE__);
 | 
        
           |  |  | 1404 | 			return false;
 | 
        
           |  |  | 1405 | 		}
 | 
        
           |  |  | 1406 |   | 
        
           |  |  | 1407 | 		$useSSL = self::$useSSL;
 | 
        
           |  |  | 1408 |   | 
        
           |  |  | 1409 | 		self::$useSSL = true; // CloudFront requires SSL
 | 
        
           |  |  | 1410 | 		$rest = new S3Request('PUT', '', '2010-11-01/distribution/'.$dist['id'].'/config', 'cloudfront.amazonaws.com');
 | 
        
           |  |  | 1411 | 		$rest->data = self::__getCloudFrontDistributionConfigXML(
 | 
        
           |  |  | 1412 | 			$dist['origin'],
 | 
        
           |  |  | 1413 | 			$dist['enabled'],
 | 
        
           |  |  | 1414 | 			$dist['comment'],
 | 
        
           |  |  | 1415 | 			$dist['callerReference'],
 | 
        
           |  |  | 1416 | 			$dist['cnames'],
 | 
        
           |  |  | 1417 | 			$dist['defaultRootObject'],
 | 
        
           |  |  | 1418 | 			$dist['originAccessIdentity'],
 | 
        
           |  |  | 1419 | 			$dist['trustedSigners']
 | 
        
           |  |  | 1420 | 		);
 | 
        
           |  |  | 1421 |   | 
        
           |  |  | 1422 | 		$rest->size = strlen($rest->data);
 | 
        
           |  |  | 1423 | 		$rest->setHeader('If-Match', $dist['hash']);
 | 
        
           |  |  | 1424 | 		$rest = self::__getCloudFrontResponse($rest);
 | 
        
           |  |  | 1425 |   | 
        
           |  |  | 1426 | 		self::$useSSL = $useSSL;
 | 
        
           |  |  | 1427 |   | 
        
           |  |  | 1428 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 1429 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1430 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1431 | 		{
 | 
        
           |  |  | 1432 | 			self::__triggerError(sprintf("S3::updateDistribution({$dist['id']}): [%s] %s",
 | 
        
           |  |  | 1433 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 1434 | 			return false;
 | 
        
           |  |  | 1435 | 		} else {
 | 
        
           |  |  | 1436 | 			$dist = self::__parseCloudFrontDistributionConfig($rest->body);
 | 
        
           |  |  | 1437 | 			$dist['hash'] = $rest->headers['hash'];
 | 
        
           |  |  | 1438 | 			return $dist;
 | 
        
           |  |  | 1439 | 		}
 | 
        
           |  |  | 1440 | 		return false;
 | 
        
           |  |  | 1441 | 	}
 | 
        
           |  |  | 1442 |   | 
        
           |  |  | 1443 |   | 
        
           |  |  | 1444 | 	/**
 | 
        
           |  |  | 1445 | 	* Delete a CloudFront distribution
 | 
        
           |  |  | 1446 | 	*
 | 
        
           |  |  | 1447 | 	* @param array $dist Distribution array info identical to output of getDistribution()
 | 
        
           |  |  | 1448 | 	* @return boolean
 | 
        
           |  |  | 1449 | 	*/
 | 
        
           |  |  | 1450 | 	public static function deleteDistribution($dist)
 | 
        
           |  |  | 1451 | 	{
 | 
        
           |  |  | 1452 | 		if (!extension_loaded('openssl'))
 | 
        
           |  |  | 1453 | 		{
 | 
        
           |  |  | 1454 | 			self::__triggerError(sprintf("S3::deleteDistribution({$dist['id']}): %s",
 | 
        
           |  |  | 1455 | 			"CloudFront functionality requires SSL"), __FILE__, __LINE__);
 | 
        
           |  |  | 1456 | 			return false;
 | 
        
           |  |  | 1457 | 		}
 | 
        
           |  |  | 1458 |   | 
        
           |  |  | 1459 | 		$useSSL = self::$useSSL;
 | 
        
           |  |  | 1460 |   | 
        
           |  |  | 1461 | 		self::$useSSL = true; // CloudFront requires SSL
 | 
        
           |  |  | 1462 | 		$rest = new S3Request('DELETE', '', '2008-06-30/distribution/'.$dist['id'], 'cloudfront.amazonaws.com');
 | 
        
           |  |  | 1463 | 		$rest->setHeader('If-Match', $dist['hash']);
 | 
        
           |  |  | 1464 | 		$rest = self::__getCloudFrontResponse($rest);
 | 
        
           |  |  | 1465 |   | 
        
           |  |  | 1466 | 		self::$useSSL = $useSSL;
 | 
        
           |  |  | 1467 |   | 
        
           |  |  | 1468 | 		if ($rest->error === false && $rest->code !== 204)
 | 
        
           |  |  | 1469 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1470 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1471 | 		{
 | 
        
           |  |  | 1472 | 			self::__triggerError(sprintf("S3::deleteDistribution({$dist['id']}): [%s] %s",
 | 
        
           |  |  | 1473 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 1474 | 			return false;
 | 
        
           |  |  | 1475 | 		}
 | 
        
           |  |  | 1476 | 		return true;
 | 
        
           |  |  | 1477 | 	}
 | 
        
           |  |  | 1478 |   | 
        
           |  |  | 1479 |   | 
        
           |  |  | 1480 | 	/**
 | 
        
           |  |  | 1481 | 	* Get a list of CloudFront distributions
 | 
        
           |  |  | 1482 | 	*
 | 
        
           |  |  | 1483 | 	* @return array
 | 
        
           |  |  | 1484 | 	*/
 | 
        
           |  |  | 1485 | 	public static function listDistributions()
 | 
        
           |  |  | 1486 | 	{
 | 
        
           |  |  | 1487 | 		if (!extension_loaded('openssl'))
 | 
        
           |  |  | 1488 | 		{
 | 
        
           |  |  | 1489 | 			self::__triggerError(sprintf("S3::listDistributions(): [%s] %s",
 | 
        
           |  |  | 1490 | 			"CloudFront functionality requires SSL"), __FILE__, __LINE__);
 | 
        
           |  |  | 1491 | 			return false;
 | 
        
           |  |  | 1492 | 		}
 | 
        
           |  |  | 1493 |   | 
        
           |  |  | 1494 | 		$useSSL = self::$useSSL;
 | 
        
           |  |  | 1495 | 		self::$useSSL = true; // CloudFront requires SSL
 | 
        
           |  |  | 1496 | 		$rest = new S3Request('GET', '', '2010-11-01/distribution', 'cloudfront.amazonaws.com');
 | 
        
           |  |  | 1497 | 		$rest = self::__getCloudFrontResponse($rest);
 | 
        
           |  |  | 1498 | 		self::$useSSL = $useSSL;
 | 
        
           |  |  | 1499 |   | 
        
           |  |  | 1500 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 1501 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1502 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1503 | 		{
 | 
        
           |  |  | 1504 | 			self::__triggerError(sprintf("S3::listDistributions(): [%s] %s",
 | 
        
           |  |  | 1505 | 			$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
 | 
        
           |  |  | 1506 | 			return false;
 | 
        
           |  |  | 1507 | 		}
 | 
        
           |  |  | 1508 | 		elseif ($rest->body instanceof SimpleXMLElement && isset($rest->body->DistributionSummary))
 | 
        
           |  |  | 1509 | 		{
 | 
        
           |  |  | 1510 | 			$list = array();
 | 
        
           |  |  | 1511 | 			if (isset($rest->body->Marker, $rest->body->MaxItems, $rest->body->IsTruncated))
 | 
        
           |  |  | 1512 | 			{
 | 
        
           |  |  | 1513 | 				//$info['marker'] = (string)$rest->body->Marker;
 | 
        
           |  |  | 1514 | 				//$info['maxItems'] = (int)$rest->body->MaxItems;
 | 
        
           |  |  | 1515 | 				//$info['isTruncated'] = (string)$rest->body->IsTruncated == 'true' ? true : false;
 | 
        
           |  |  | 1516 | 			}
 | 
        
           |  |  | 1517 | 			foreach ($rest->body->DistributionSummary as $summary)
 | 
        
           |  |  | 1518 | 				$list[(string)$summary->Id] = self::__parseCloudFrontDistributionConfig($summary);
 | 
        
           |  |  | 1519 |   | 
        
           |  |  | 1520 | 			return $list;
 | 
        
           |  |  | 1521 | 		}
 | 
        
           |  |  | 1522 | 		return array();
 | 
        
           |  |  | 1523 | 	}
 | 
        
           |  |  | 1524 |   | 
        
           |  |  | 1525 | 	/**
 | 
        
           |  |  | 1526 | 	* List CloudFront Origin Access Identities
 | 
        
           |  |  | 1527 | 	*
 | 
        
           |  |  | 1528 | 	* @return array
 | 
        
           |  |  | 1529 | 	*/
 | 
        
           |  |  | 1530 | 	public static function listOriginAccessIdentities()
 | 
        
           |  |  | 1531 | 	{
 | 
        
           |  |  | 1532 | 		if (!extension_loaded('openssl'))
 | 
        
           |  |  | 1533 | 		{
 | 
        
           |  |  | 1534 | 			self::__triggerError(sprintf("S3::listOriginAccessIdentities(): [%s] %s",
 | 
        
           |  |  | 1535 | 			"CloudFront functionality requires SSL"), __FILE__, __LINE__);
 | 
        
           |  |  | 1536 | 			return false;
 | 
        
           |  |  | 1537 | 		}
 | 
        
           |  |  | 1538 |   | 
        
           |  |  | 1539 | 		self::$useSSL = true; // CloudFront requires SSL
 | 
        
           |  |  | 1540 | 		$rest = new S3Request('GET', '', '2010-11-01/origin-access-identity/cloudfront', 'cloudfront.amazonaws.com');
 | 
        
           |  |  | 1541 | 		$rest = self::__getCloudFrontResponse($rest);
 | 
        
           |  |  | 1542 | 		$useSSL = self::$useSSL;
 | 
        
           |  |  | 1543 |   | 
        
           |  |  | 1544 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 1545 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1546 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1547 | 		{
 | 
        
           |  |  | 1548 | 			trigger_error(sprintf("S3::listOriginAccessIdentities(): [%s] %s",
 | 
        
           |  |  | 1549 | 			$rest->error['code'], $rest->error['message']), E_USER_WARNING);
 | 
        
           |  |  | 1550 | 			return false;
 | 
        
           |  |  | 1551 | 		}
 | 
        
           |  |  | 1552 |   | 
        
           |  |  | 1553 | 		if (isset($rest->body->CloudFrontOriginAccessIdentitySummary))
 | 
        
           |  |  | 1554 | 		{
 | 
        
           |  |  | 1555 | 			$identities = array();
 | 
        
           |  |  | 1556 | 			foreach ($rest->body->CloudFrontOriginAccessIdentitySummary as $identity)
 | 
        
           |  |  | 1557 | 				if (isset($identity->S3CanonicalUserId))
 | 
        
           |  |  | 1558 | 					$identities[(string)$identity->Id] = array('id' => (string)$identity->Id, 's3CanonicalUserId' => (string)$identity->S3CanonicalUserId);
 | 
        
           |  |  | 1559 | 			return $identities;
 | 
        
           |  |  | 1560 | 		}
 | 
        
           |  |  | 1561 | 		return false;
 | 
        
           |  |  | 1562 | 	}
 | 
        
           |  |  | 1563 |   | 
        
           |  |  | 1564 |   | 
        
           |  |  | 1565 | 	/**
 | 
        
           |  |  | 1566 | 	* Invalidate objects in a CloudFront distribution
 | 
        
           |  |  | 1567 | 	*
 | 
        
           |  |  | 1568 | 	* Thanks to Martin Lindkvist for S3::invalidateDistribution()
 | 
        
           |  |  | 1569 | 	*
 | 
        
           |  |  | 1570 | 	* @param string $distributionId Distribution ID from listDistributions()
 | 
        
           |  |  | 1571 | 	* @param array $paths Array of object paths to invalidate
 | 
        
           |  |  | 1572 | 	* @return boolean
 | 
        
           |  |  | 1573 | 	*/
 | 
        
           |  |  | 1574 | 	public static function invalidateDistribution($distributionId, $paths)
 | 
        
           |  |  | 1575 | 	{
 | 
        
           |  |  | 1576 | 		if (!extension_loaded('openssl'))
 | 
        
           |  |  | 1577 | 		{
 | 
        
           |  |  | 1578 | 			self::__triggerError(sprintf("S3::invalidateDistribution(): [%s] %s",
 | 
        
           |  |  | 1579 | 			"CloudFront functionality requires SSL"), __FILE__, __LINE__);
 | 
        
           |  |  | 1580 | 			return false;
 | 
        
           |  |  | 1581 | 		}
 | 
        
           |  |  | 1582 |   | 
        
           |  |  | 1583 | 		$useSSL = self::$useSSL;
 | 
        
           |  |  | 1584 | 		self::$useSSL = true; // CloudFront requires SSL
 | 
        
           |  |  | 1585 | 		$rest = new S3Request('POST', '', '2010-08-01/distribution/'.$distributionId.'/invalidation', 'cloudfront.amazonaws.com');
 | 
        
           |  |  | 1586 | 		$rest->data = self::__getCloudFrontInvalidationBatchXML($paths, (string)microtime(true));
 | 
        
           |  |  | 1587 | 		$rest->size = strlen($rest->data);
 | 
        
           |  |  | 1588 | 		$rest = self::__getCloudFrontResponse($rest);
 | 
        
           |  |  | 1589 | 		self::$useSSL = $useSSL;
 | 
        
           |  |  | 1590 |   | 
        
           |  |  | 1591 | 		if ($rest->error === false && $rest->code !== 201)
 | 
        
           |  |  | 1592 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1593 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1594 | 		{
 | 
        
           |  |  | 1595 | 			trigger_error(sprintf("S3::invalidate('{$distributionId}',{$paths}): [%s] %s",
 | 
        
           |  |  | 1596 | 			$rest->error['code'], $rest->error['message']), E_USER_WARNING);
 | 
        
           |  |  | 1597 | 			return false;
 | 
        
           |  |  | 1598 | 		}
 | 
        
           |  |  | 1599 | 		return true;
 | 
        
           |  |  | 1600 | 	}
 | 
        
           |  |  | 1601 |   | 
        
           |  |  | 1602 |   | 
        
           |  |  | 1603 | 	/**
 | 
        
           |  |  | 1604 | 	* Get a InvalidationBatch DOMDocument
 | 
        
           |  |  | 1605 | 	*
 | 
        
           |  |  | 1606 | 	* @internal Used to create XML in invalidateDistribution()
 | 
        
           |  |  | 1607 | 	* @param array $paths Paths to objects to invalidateDistribution
 | 
        
           |  |  | 1608 | 	* @param int $callerReference
 | 
        
           |  |  | 1609 | 	* @return string
 | 
        
           |  |  | 1610 | 	*/
 | 
        
           |  |  | 1611 | 	private static function __getCloudFrontInvalidationBatchXML($paths, $callerReference = '0')
 | 
        
           |  |  | 1612 | 	{
 | 
        
           |  |  | 1613 | 		$dom = new DOMDocument('1.0', 'UTF-8');
 | 
        
           |  |  | 1614 | 		$dom->formatOutput = true;
 | 
        
           |  |  | 1615 | 		$invalidationBatch = $dom->createElement('InvalidationBatch');
 | 
        
           |  |  | 1616 | 		foreach ($paths as $path)
 | 
        
           |  |  | 1617 | 			$invalidationBatch->appendChild($dom->createElement('Path', $path));
 | 
        
           |  |  | 1618 |   | 
        
           |  |  | 1619 | 		$invalidationBatch->appendChild($dom->createElement('CallerReference', $callerReference));
 | 
        
           |  |  | 1620 | 		$dom->appendChild($invalidationBatch);
 | 
        
           |  |  | 1621 | 		return $dom->saveXML();
 | 
        
           |  |  | 1622 | 	}
 | 
        
           |  |  | 1623 |   | 
        
           |  |  | 1624 |   | 
        
           |  |  | 1625 | 	/**
 | 
        
           |  |  | 1626 | 	* List your invalidation batches for invalidateDistribution() in a CloudFront distribution
 | 
        
           |  |  | 1627 | 	*
 | 
        
           |  |  | 1628 | 	* http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/ListInvalidation.html
 | 
        
           |  |  | 1629 | 	* returned array looks like this:
 | 
        
           |  |  | 1630 | 	*	Array
 | 
        
           |  |  | 1631 | 	*	(
 | 
        
           |  |  | 1632 | 	*		[I31TWB0CN9V6XD] => InProgress
 | 
        
           |  |  | 1633 | 	*		[IT3TFE31M0IHZ] => Completed
 | 
        
           |  |  | 1634 | 	*		[I12HK7MPO1UQDA] => Completed
 | 
        
           |  |  | 1635 | 	*		[I1IA7R6JKTC3L2] => Completed
 | 
        
           |  |  | 1636 | 	*	)
 | 
        
           |  |  | 1637 | 	*
 | 
        
           |  |  | 1638 | 	* @param string $distributionId Distribution ID from listDistributions()
 | 
        
           |  |  | 1639 | 	* @return array
 | 
        
           |  |  | 1640 | 	*/
 | 
        
           |  |  | 1641 | 	public static function getDistributionInvalidationList($distributionId)
 | 
        
           |  |  | 1642 | 	{
 | 
        
           |  |  | 1643 | 		if (!extension_loaded('openssl'))
 | 
        
           |  |  | 1644 | 		{
 | 
        
           |  |  | 1645 | 			self::__triggerError(sprintf("S3::getDistributionInvalidationList(): [%s] %s",
 | 
        
           |  |  | 1646 | 			"CloudFront functionality requires SSL"), __FILE__, __LINE__);
 | 
        
           |  |  | 1647 | 			return false;
 | 
        
           |  |  | 1648 | 		}
 | 
        
           |  |  | 1649 |   | 
        
           |  |  | 1650 | 		$useSSL = self::$useSSL;
 | 
        
           |  |  | 1651 | 		self::$useSSL = true; // CloudFront requires SSL
 | 
        
           |  |  | 1652 | 		$rest = new S3Request('GET', '', '2010-11-01/distribution/'.$distributionId.'/invalidation', 'cloudfront.amazonaws.com');
 | 
        
           |  |  | 1653 | 		$rest = self::__getCloudFrontResponse($rest);
 | 
        
           |  |  | 1654 | 		self::$useSSL = $useSSL;
 | 
        
           |  |  | 1655 |   | 
        
           |  |  | 1656 | 		if ($rest->error === false && $rest->code !== 200)
 | 
        
           |  |  | 1657 | 			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
 | 
        
           |  |  | 1658 | 		if ($rest->error !== false)
 | 
        
           |  |  | 1659 | 		{
 | 
        
           |  |  | 1660 | 			trigger_error(sprintf("S3::getDistributionInvalidationList('{$distributionId}'): [%s]",
 | 
        
           |  |  | 1661 | 			$rest->error['code'], $rest->error['message']), E_USER_WARNING);
 | 
        
           |  |  | 1662 | 			return false;
 | 
        
           |  |  | 1663 | 		}
 | 
        
           |  |  | 1664 | 		elseif ($rest->body instanceof SimpleXMLElement && isset($rest->body->InvalidationSummary))
 | 
        
           |  |  | 1665 | 		{
 | 
        
           |  |  | 1666 | 			$list = array();
 | 
        
           |  |  | 1667 | 			foreach ($rest->body->InvalidationSummary as $summary)
 | 
        
           |  |  | 1668 | 				$list[(string)$summary->Id] = (string)$summary->Status;
 | 
        
           |  |  | 1669 |   | 
        
           |  |  | 1670 | 			return $list;
 | 
        
           |  |  | 1671 | 		}
 | 
        
           |  |  | 1672 | 		return array();
 | 
        
           |  |  | 1673 | 	}
 | 
        
           |  |  | 1674 |   | 
        
           |  |  | 1675 |   | 
        
           |  |  | 1676 | 	/**
 | 
        
           |  |  | 1677 | 	* Get a DistributionConfig DOMDocument
 | 
        
           |  |  | 1678 | 	*
 | 
        
           |  |  | 1679 | 	* http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?PutConfig.html
 | 
        
           |  |  | 1680 | 	*
 | 
        
           |  |  | 1681 | 	* @internal Used to create XML in createDistribution() and updateDistribution()
 | 
        
           |  |  | 1682 | 	* @param string $bucket S3 Origin bucket
 | 
        
           |  |  | 1683 | 	* @param boolean $enabled Enabled (true/false)
 | 
        
           |  |  | 1684 | 	* @param string $comment Comment to append
 | 
        
           |  |  | 1685 | 	* @param string $callerReference Caller reference
 | 
        
           |  |  | 1686 | 	* @param array $cnames Array of CNAME aliases
 | 
        
           |  |  | 1687 | 	* @param string $defaultRootObject Default root object
 | 
        
           |  |  | 1688 | 	* @param string $originAccessIdentity Origin access identity
 | 
        
           |  |  | 1689 | 	* @param array $trustedSigners Array of trusted signers
 | 
        
           |  |  | 1690 | 	* @return string
 | 
        
           |  |  | 1691 | 	*/
 | 
        
           |  |  | 1692 | 	private static function __getCloudFrontDistributionConfigXML($bucket, $enabled, $comment, $callerReference = '0', $cnames = array(), $defaultRootObject = null, $originAccessIdentity = null, $trustedSigners = array())
 | 
        
           |  |  | 1693 | 	{
 | 
        
           |  |  | 1694 | 		$dom = new DOMDocument('1.0', 'UTF-8');
 | 
        
           |  |  | 1695 | 		$dom->formatOutput = true;
 | 
        
           |  |  | 1696 | 		$distributionConfig = $dom->createElement('DistributionConfig');
 | 
        
           |  |  | 1697 | 		$distributionConfig->setAttribute('xmlns', 'http://cloudfront.amazonaws.com/doc/2010-11-01/');
 | 
        
           |  |  | 1698 |   | 
        
           |  |  | 1699 | 		$origin = $dom->createElement('S3Origin');
 | 
        
           |  |  | 1700 | 		$origin->appendChild($dom->createElement('DNSName', $bucket));
 | 
        
           |  |  | 1701 | 		if ($originAccessIdentity !== null) $origin->appendChild($dom->createElement('OriginAccessIdentity', $originAccessIdentity));
 | 
        
           |  |  | 1702 | 		$distributionConfig->appendChild($origin);
 | 
        
           |  |  | 1703 |   | 
        
           |  |  | 1704 | 		if ($defaultRootObject !== null) $distributionConfig->appendChild($dom->createElement('DefaultRootObject', $defaultRootObject));
 | 
        
           |  |  | 1705 |   | 
        
           |  |  | 1706 | 		$distributionConfig->appendChild($dom->createElement('CallerReference', $callerReference));
 | 
        
           |  |  | 1707 | 		foreach ($cnames as $cname)
 | 
        
           |  |  | 1708 | 			$distributionConfig->appendChild($dom->createElement('CNAME', $cname));
 | 
        
           |  |  | 1709 | 		if ($comment !== '') $distributionConfig->appendChild($dom->createElement('Comment', $comment));
 | 
        
           |  |  | 1710 | 		$distributionConfig->appendChild($dom->createElement('Enabled', $enabled ? 'true' : 'false'));
 | 
        
           |  |  | 1711 |   | 
        
           |  |  | 1712 | 		$trusted = $dom->createElement('TrustedSigners');
 | 
        
           |  |  | 1713 | 		foreach ($trustedSigners as $id => $type)
 | 
        
           |  |  | 1714 | 			$trusted->appendChild($id !== '' ? $dom->createElement($type, $id) : $dom->createElement($type));
 | 
        
           |  |  | 1715 | 		$distributionConfig->appendChild($trusted);
 | 
        
           |  |  | 1716 |   | 
        
           |  |  | 1717 | 		$dom->appendChild($distributionConfig);
 | 
        
           |  |  | 1718 | 		//var_dump($dom->saveXML());
 | 
        
           |  |  | 1719 | 		return $dom->saveXML();
 | 
        
           |  |  | 1720 | 	}
 | 
        
           |  |  | 1721 |   | 
        
           |  |  | 1722 |   | 
        
           |  |  | 1723 | 	/**
 | 
        
           |  |  | 1724 | 	* Parse a CloudFront distribution config
 | 
        
           |  |  | 1725 | 	*
 | 
        
           |  |  | 1726 | 	* See http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?GetDistribution.html
 | 
        
           |  |  | 1727 | 	*
 | 
        
           |  |  | 1728 | 	* @internal Used to parse the CloudFront DistributionConfig node to an array
 | 
        
           |  |  | 1729 | 	* @param object &$node DOMNode
 | 
        
           |  |  | 1730 | 	* @return array
 | 
        
           |  |  | 1731 | 	*/
 | 
        
           |  |  | 1732 | 	private static function __parseCloudFrontDistributionConfig(&$node)
 | 
        
           |  |  | 1733 | 	{
 | 
        
           |  |  | 1734 | 		if (isset($node->DistributionConfig))
 | 
        
           |  |  | 1735 | 			return self::__parseCloudFrontDistributionConfig($node->DistributionConfig);
 | 
        
           |  |  | 1736 |   | 
        
           |  |  | 1737 | 		$dist = array();
 | 
        
           |  |  | 1738 | 		if (isset($node->Id, $node->Status, $node->LastModifiedTime, $node->DomainName))
 | 
        
           |  |  | 1739 | 		{
 | 
        
           |  |  | 1740 | 			$dist['id'] = (string)$node->Id;
 | 
        
           |  |  | 1741 | 			$dist['status'] = (string)$node->Status;
 | 
        
           |  |  | 1742 | 			$dist['time'] = strtotime((string)$node->LastModifiedTime);
 | 
        
           |  |  | 1743 | 			$dist['domain'] = (string)$node->DomainName;
 | 
        
           |  |  | 1744 | 		}
 | 
        
           |  |  | 1745 |   | 
        
           |  |  | 1746 | 		if (isset($node->CallerReference))
 | 
        
           |  |  | 1747 | 			$dist['callerReference'] = (string)$node->CallerReference;
 | 
        
           |  |  | 1748 |   | 
        
           |  |  | 1749 | 		if (isset($node->Enabled))
 | 
        
           |  |  | 1750 | 			$dist['enabled'] = (string)$node->Enabled == 'true' ? true : false;
 | 
        
           |  |  | 1751 |   | 
        
           |  |  | 1752 | 		if (isset($node->S3Origin))
 | 
        
           |  |  | 1753 | 		{
 | 
        
           |  |  | 1754 | 			if (isset($node->S3Origin->DNSName))
 | 
        
           |  |  | 1755 | 				$dist['origin'] = (string)$node->S3Origin->DNSName;
 | 
        
           |  |  | 1756 |   | 
        
           |  |  | 1757 | 			$dist['originAccessIdentity'] = isset($node->S3Origin->OriginAccessIdentity) ?
 | 
        
           |  |  | 1758 | 			(string)$node->S3Origin->OriginAccessIdentity : null;
 | 
        
           |  |  | 1759 | 		}
 | 
        
           |  |  | 1760 |   | 
        
           |  |  | 1761 | 		$dist['defaultRootObject'] = isset($node->DefaultRootObject) ? (string)$node->DefaultRootObject : null;
 | 
        
           |  |  | 1762 |   | 
        
           |  |  | 1763 | 		$dist['cnames'] = array();
 | 
        
           |  |  | 1764 | 		if (isset($node->CNAME))
 | 
        
           |  |  | 1765 | 			foreach ($node->CNAME as $cname)
 | 
        
           |  |  | 1766 | 				$dist['cnames'][(string)$cname] = (string)$cname;
 | 
        
           |  |  | 1767 |   | 
        
           |  |  | 1768 | 		$dist['trustedSigners'] = array();
 | 
        
           |  |  | 1769 | 		if (isset($node->TrustedSigners))
 | 
        
           |  |  | 1770 | 			foreach ($node->TrustedSigners as $signer)
 | 
        
           |  |  | 1771 | 			{
 | 
        
           |  |  | 1772 | 				if (isset($signer->Self))
 | 
        
           |  |  | 1773 | 					$dist['trustedSigners'][''] = 'Self';
 | 
        
           |  |  | 1774 | 				elseif (isset($signer->KeyPairId))
 | 
        
           |  |  | 1775 | 					$dist['trustedSigners'][(string)$signer->KeyPairId] = 'KeyPairId';
 | 
        
           |  |  | 1776 | 				elseif (isset($signer->AwsAccountNumber))
 | 
        
           |  |  | 1777 | 					$dist['trustedSigners'][(string)$signer->AwsAccountNumber] = 'AwsAccountNumber';
 | 
        
           |  |  | 1778 | 			}
 | 
        
           |  |  | 1779 |   | 
        
           |  |  | 1780 | 		$dist['comment'] = isset($node->Comment) ? (string)$node->Comment : null;
 | 
        
           |  |  | 1781 | 		return $dist;
 | 
        
           |  |  | 1782 | 	}
 | 
        
           |  |  | 1783 |   | 
        
           |  |  | 1784 |   | 
        
           |  |  | 1785 | 	/**
 | 
        
           |  |  | 1786 | 	* Grab CloudFront response
 | 
        
           |  |  | 1787 | 	*
 | 
        
           |  |  | 1788 | 	* @internal Used to parse the CloudFront S3Request::getResponse() output
 | 
        
           |  |  | 1789 | 	* @param object &$rest S3Request instance
 | 
        
           |  |  | 1790 | 	* @return object
 | 
        
           |  |  | 1791 | 	*/
 | 
        
           |  |  | 1792 | 	private static function __getCloudFrontResponse(&$rest)
 | 
        
           |  |  | 1793 | 	{
 | 
        
           |  |  | 1794 | 		$rest->getResponse();
 | 
        
           |  |  | 1795 | 		if ($rest->response->error === false && isset($rest->response->body) &&
 | 
        
           |  |  | 1796 | 		is_string($rest->response->body) && substr($rest->response->body, 0, 5) == '<?xml')
 | 
        
           |  |  | 1797 | 		{
 | 
        
           |  |  | 1798 | 			$rest->response->body = simplexml_load_string($rest->response->body);
 | 
        
           |  |  | 1799 | 			// Grab CloudFront errors
 | 
        
           |  |  | 1800 | 			if (isset($rest->response->body->Error, $rest->response->body->Error->Code,
 | 
        
           |  |  | 1801 | 			$rest->response->body->Error->Message))
 | 
        
           |  |  | 1802 | 			{
 | 
        
           |  |  | 1803 | 				$rest->response->error = array(
 | 
        
           |  |  | 1804 | 					'code' => (string)$rest->response->body->Error->Code,
 | 
        
           |  |  | 1805 | 					'message' => (string)$rest->response->body->Error->Message
 | 
        
           |  |  | 1806 | 				);
 | 
        
           |  |  | 1807 | 				unset($rest->response->body);
 | 
        
           |  |  | 1808 | 			}
 | 
        
           |  |  | 1809 | 		}
 | 
        
           |  |  | 1810 | 		return $rest->response;
 | 
        
           |  |  | 1811 | 	}
 | 
        
           |  |  | 1812 |   | 
        
           |  |  | 1813 |   | 
        
           |  |  | 1814 | 	/**
 | 
        
           |  |  | 1815 | 	* Get MIME type for file
 | 
        
           |  |  | 1816 | 	*
 | 
        
           |  |  | 1817 | 	* To override the putObject() Content-Type, add it to $requestHeaders
 | 
        
           |  |  | 1818 | 	*
 | 
        
           |  |  | 1819 | 	* To use fileinfo, ensure the MAGIC environment variable is set
 | 
        
           |  |  | 1820 | 	*
 | 
        
           |  |  | 1821 | 	* @internal Used to get mime types
 | 
        
           |  |  | 1822 | 	* @param string &$file File path
 | 
        
           |  |  | 1823 | 	* @return string
 | 
        
           |  |  | 1824 | 	*/
 | 
        
           |  |  | 1825 | 	private static function __getMIMEType(&$file)
 | 
        
           |  |  | 1826 | 	{
 | 
        
           |  |  | 1827 | 		static $exts = array(
 | 
        
           |  |  | 1828 | 			'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif',
 | 
        
           |  |  | 1829 | 			'png' => 'image/png', 'ico' => 'image/x-icon', 'pdf' => 'application/pdf',
 | 
        
           |  |  | 1830 | 			'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'svg' => 'image/svg+xml',
 | 
        
           |  |  | 1831 | 			'svgz' => 'image/svg+xml', 'swf' => 'application/x-shockwave-flash',
 | 
        
           |  |  | 1832 | 			'zip' => 'application/zip', 'gz' => 'application/x-gzip',
 | 
        
           |  |  | 1833 | 			'tar' => 'application/x-tar', 'bz' => 'application/x-bzip',
 | 
        
           |  |  | 1834 | 			'bz2' => 'application/x-bzip2',  'rar' => 'application/x-rar-compressed',
 | 
        
           |  |  | 1835 | 			'exe' => 'application/x-msdownload', 'msi' => 'application/x-msdownload',
 | 
        
           |  |  | 1836 | 			'cab' => 'application/vnd.ms-cab-compressed', 'txt' => 'text/plain',
 | 
        
           |  |  | 1837 | 			'asc' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html',
 | 
        
           |  |  | 1838 | 			'css' => 'text/css', 'js' => 'text/javascript',
 | 
        
           |  |  | 1839 | 			'xml' => 'text/xml', 'xsl' => 'application/xsl+xml',
 | 
        
           |  |  | 1840 | 			'ogg' => 'application/ogg', 'mp3' => 'audio/mpeg', 'wav' => 'audio/x-wav',
 | 
        
           |  |  | 1841 | 			'avi' => 'video/x-msvideo', 'mpg' => 'video/mpeg', 'mpeg' => 'video/mpeg',
 | 
        
           |  |  | 1842 | 			'mov' => 'video/quicktime', 'flv' => 'video/x-flv', 'php' => 'text/x-php'
 | 
        
           |  |  | 1843 | 		);
 | 
        
           |  |  | 1844 |   | 
        
           |  |  | 1845 | 		$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
 | 
        
           |  |  | 1846 | 		if (isset($exts[$ext])) return $exts[$ext];
 | 
        
           |  |  | 1847 |   | 
        
           |  |  | 1848 | 		// Use fileinfo if available
 | 
        
           |  |  | 1849 | 		if (extension_loaded('fileinfo') && isset($_ENV['MAGIC']) &&
 | 
        
           |  |  | 1850 | 		($finfo = finfo_open(FILEINFO_MIME, $_ENV['MAGIC'])) !== false)
 | 
        
           |  |  | 1851 | 		{
 | 
        
           |  |  | 1852 | 			if (($type = finfo_file($finfo, $file)) !== false)
 | 
        
           |  |  | 1853 | 			{
 | 
        
           |  |  | 1854 | 				// Remove the charset and grab the last content-type
 | 
        
           |  |  | 1855 | 				$type = explode(' ', str_replace('; charset=', ';charset=', $type));
 | 
        
           |  |  | 1856 | 				$type = array_pop($type);
 | 
        
           |  |  | 1857 | 				$type = explode(';', $type);
 | 
        
           |  |  | 1858 | 				$type = trim(array_shift($type));
 | 
        
           |  |  | 1859 | 			}
 | 
        
           |  |  | 1860 | 			finfo_close($finfo);
 | 
        
           |  |  | 1861 | 			if ($type !== false && strlen($type) > 0) return $type;
 | 
        
           |  |  | 1862 | 		}
 | 
        
           |  |  | 1863 |   | 
        
           |  |  | 1864 | 		return 'application/octet-stream';
 | 
        
           |  |  | 1865 | 	}
 | 
        
           |  |  | 1866 |   | 
        
           |  |  | 1867 |   | 
        
           |  |  | 1868 | 	/**
 | 
        
           |  |  | 1869 | 	* Get the current time
 | 
        
           |  |  | 1870 | 	*
 | 
        
           |  |  | 1871 | 	* @internal Used to apply offsets to sytem time
 | 
        
           |  |  | 1872 | 	* @return integer
 | 
        
           |  |  | 1873 | 	*/
 | 
        
           |  |  | 1874 | 	public static function __getTime()
 | 
        
           |  |  | 1875 | 	{
 | 
        
           |  |  | 1876 | 		return time() + self::$__timeOffset;
 | 
        
           |  |  | 1877 | 	}
 | 
        
           |  |  | 1878 |   | 
        
           |  |  | 1879 |   | 
        
           |  |  | 1880 | 	/**
 | 
        
           |  |  | 1881 | 	* Generate the auth string: "AWS AccessKey:Signature"
 | 
        
           |  |  | 1882 | 	*
 | 
        
           |  |  | 1883 | 	* @internal Used by S3Request::getResponse()
 | 
        
           |  |  | 1884 | 	* @param string $string String to sign
 | 
        
           |  |  | 1885 | 	* @return string
 | 
        
           |  |  | 1886 | 	*/
 | 
        
           |  |  | 1887 | 	public static function __getSignature($string)
 | 
        
           |  |  | 1888 | 	{
 | 
        
           |  |  | 1889 | 		return 'AWS '.self::$__accessKey.':'.self::__getHash($string);
 | 
        
           |  |  | 1890 | 	}
 | 
        
           |  |  | 1891 |   | 
        
           |  |  | 1892 |   | 
        
           |  |  | 1893 | 	/**
 | 
        
           |  |  | 1894 | 	* Creates a HMAC-SHA1 hash
 | 
        
           |  |  | 1895 | 	*
 | 
        
           |  |  | 1896 | 	* This uses the hash extension if loaded
 | 
        
           |  |  | 1897 | 	*
 | 
        
           |  |  | 1898 | 	* @internal Used by __getSignature()
 | 
        
           |  |  | 1899 | 	* @param string $string String to sign
 | 
        
           |  |  | 1900 | 	* @return string
 | 
        
           |  |  | 1901 | 	*/
 | 
        
           |  |  | 1902 | 	private static function __getHash($string)
 | 
        
           |  |  | 1903 | 	{
 | 
        
           |  |  | 1904 | 		return base64_encode(extension_loaded('hash') ?
 | 
        
           |  |  | 1905 | 		hash_hmac('sha1', $string, self::$__secretKey, true) : pack('H*', sha1(
 | 
        
           |  |  | 1906 | 		(str_pad(self::$__secretKey, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) .
 | 
        
           |  |  | 1907 | 		pack('H*', sha1((str_pad(self::$__secretKey, 64, chr(0x00)) ^
 | 
        
           |  |  | 1908 | 		(str_repeat(chr(0x36), 64))) . $string)))));
 | 
        
           |  |  | 1909 | 	}
 | 
        
           |  |  | 1910 |   | 
        
           |  |  | 1911 | }
 | 
        
           |  |  | 1912 |   | 
        
           |  |  | 1913 | /**
 | 
        
           |  |  | 1914 |  * S3 Request class
 | 
        
           |  |  | 1915 |  *
 | 
        
           |  |  | 1916 |  * @link http://undesigned.org.za/2007/10/22/amazon-s3-php-class
 | 
        
           |  |  | 1917 |  * @version 0.5.0-dev
 | 
        
           |  |  | 1918 |  */
 | 
        
           |  |  | 1919 | final class S3Request
 | 
        
           |  |  | 1920 | {
 | 
        
           |  |  | 1921 | 	/**
 | 
        
           |  |  | 1922 | 	 * AWS URI
 | 
        
           |  |  | 1923 | 	 *
 | 
        
           |  |  | 1924 | 	 * @var string
 | 
        
           |  |  | 1925 | 	 * @access pricate
 | 
        
           |  |  | 1926 | 	 */
 | 
        
           |  |  | 1927 | 	private $endpoint;
 | 
        
           |  |  | 1928 |   | 
        
           |  |  | 1929 | 	/**
 | 
        
           |  |  | 1930 | 	 * Verb
 | 
        
           |  |  | 1931 | 	 *
 | 
        
           |  |  | 1932 | 	 * @var string
 | 
        
           |  |  | 1933 | 	 * @access private
 | 
        
           |  |  | 1934 | 	 */
 | 
        
           |  |  | 1935 | 	private $verb;
 | 
        
           |  |  | 1936 |   | 
        
           |  |  | 1937 | 	/**
 | 
        
           |  |  | 1938 | 	 * S3 bucket name
 | 
        
           |  |  | 1939 | 	 *
 | 
        
           |  |  | 1940 | 	 * @var string
 | 
        
           |  |  | 1941 | 	 * @access private
 | 
        
           |  |  | 1942 | 	 */
 | 
        
           |  |  | 1943 | 	private $bucket;
 | 
        
           |  |  | 1944 |   | 
        
           |  |  | 1945 | 	/**
 | 
        
           |  |  | 1946 | 	 * Object URI
 | 
        
           |  |  | 1947 | 	 *
 | 
        
           |  |  | 1948 | 	 * @var string
 | 
        
           |  |  | 1949 | 	 * @access private
 | 
        
           |  |  | 1950 | 	 */
 | 
        
           |  |  | 1951 | 	private $uri;
 | 
        
           |  |  | 1952 |   | 
        
           |  |  | 1953 | 	/**
 | 
        
           |  |  | 1954 | 	 * Final object URI
 | 
        
           |  |  | 1955 | 	 *
 | 
        
           |  |  | 1956 | 	 * @var string
 | 
        
           |  |  | 1957 | 	 * @access private
 | 
        
           |  |  | 1958 | 	 */
 | 
        
           |  |  | 1959 | 	private $resource = '';
 | 
        
           |  |  | 1960 |   | 
        
           |  |  | 1961 | 	/**
 | 
        
           |  |  | 1962 | 	 * Additional request parameters
 | 
        
           |  |  | 1963 | 	 *
 | 
        
           |  |  | 1964 | 	 * @var array
 | 
        
           |  |  | 1965 | 	 * @access private
 | 
        
           |  |  | 1966 | 	 */
 | 
        
           |  |  | 1967 | 	private $parameters = array();
 | 
        
           |  |  | 1968 |   | 
        
           |  |  | 1969 | 	/**
 | 
        
           |  |  | 1970 | 	 * Amazon specific request headers
 | 
        
           |  |  | 1971 | 	 *
 | 
        
           |  |  | 1972 | 	 * @var array
 | 
        
           |  |  | 1973 | 	 * @access private
 | 
        
           |  |  | 1974 | 	 */
 | 
        
           |  |  | 1975 | 	private $amzHeaders = array();
 | 
        
           |  |  | 1976 |   | 
        
           |  |  | 1977 | 	/**
 | 
        
           |  |  | 1978 | 	 * HTTP request headers
 | 
        
           |  |  | 1979 | 	 *
 | 
        
           |  |  | 1980 | 	 * @var array
 | 
        
           |  |  | 1981 | 	 * @access private
 | 
        
           |  |  | 1982 | 	 */
 | 
        
           |  |  | 1983 | 	private $headers = array(
 | 
        
           |  |  | 1984 | 		'Host' => '', 'Date' => '', 'Content-MD5' => '', 'Content-Type' => ''
 | 
        
           |  |  | 1985 | 	);
 | 
        
           |  |  | 1986 |   | 
        
           |  |  | 1987 | 	/**
 | 
        
           |  |  | 1988 | 	 * Use HTTP PUT?
 | 
        
           |  |  | 1989 | 	 *
 | 
        
           |  |  | 1990 | 	 * @var bool
 | 
        
           |  |  | 1991 | 	 * @access public
 | 
        
           |  |  | 1992 | 	 */
 | 
        
           |  |  | 1993 | 	public $fp = false;
 | 
        
           |  |  | 1994 |   | 
        
           |  |  | 1995 | 	/**
 | 
        
           |  |  | 1996 | 	 * PUT file size
 | 
        
           |  |  | 1997 | 	 *
 | 
        
           |  |  | 1998 | 	 * @var int
 | 
        
           |  |  | 1999 | 	 * @access public
 | 
        
           |  |  | 2000 | 	 */
 | 
        
           |  |  | 2001 | 	public $size = 0;
 | 
        
           |  |  | 2002 |   | 
        
           |  |  | 2003 | 	/**
 | 
        
           |  |  | 2004 | 	 * PUT post fields
 | 
        
           |  |  | 2005 | 	 *
 | 
        
           |  |  | 2006 | 	 * @var array
 | 
        
           |  |  | 2007 | 	 * @access public
 | 
        
           |  |  | 2008 | 	 */
 | 
        
           |  |  | 2009 | 	public $data = false;
 | 
        
           |  |  | 2010 |   | 
        
           |  |  | 2011 | 	/**
 | 
        
           |  |  | 2012 | 	 * S3 request respone
 | 
        
           |  |  | 2013 | 	 *
 | 
        
           |  |  | 2014 | 	 * @var object
 | 
        
           |  |  | 2015 | 	 * @access public
 | 
        
           |  |  | 2016 | 	 */
 | 
        
           |  |  | 2017 | 	public $response;
 | 
        
           |  |  | 2018 |   | 
        
           |  |  | 2019 | 	/**
 | 
        
           |  |  | 2020 | 	 * Filename or resource to write to.
 | 
        
           |  |  | 2021 | 	 *
 | 
        
           |  |  | 2022 | 	 * @var string
 | 
        
           |  |  | 2023 | 	 * @access public
 | 
        
           |  |  | 2024 | 	 */
 | 
        
           |  |  | 2025 | 	public $file;
 | 
        
           |  |  | 2026 |   | 
        
           |  |  | 2027 | 	/**
 | 
        
           |  |  | 2028 | 	* Constructor
 | 
        
           |  |  | 2029 | 	*
 | 
        
           |  |  | 2030 | 	* @param string $verb Verb
 | 
        
           |  |  | 2031 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 2032 | 	* @param string $uri Object URI
 | 
        
           |  |  | 2033 | 	* @param string $endpoint AWS endpoint URI
 | 
        
           |  |  | 2034 | 	* @return mixed
 | 
        
           |  |  | 2035 | 	*/
 | 
        
           |  |  | 2036 | 	function __construct($verb, $bucket = '', $uri = '', $endpoint = 's3.amazonaws.com')
 | 
        
           |  |  | 2037 | 	{
 | 
        
           |  |  | 2038 |   | 
        
           |  |  | 2039 | 		$this->endpoint = $endpoint;
 | 
        
           |  |  | 2040 | 		$this->verb = $verb;
 | 
        
           |  |  | 2041 | 		$this->bucket = $bucket;
 | 
        
           |  |  | 2042 | 		$this->uri = $uri !== '' ? '/'.str_replace('%2F', '/', rawurlencode($uri)) : '/';
 | 
        
           |  |  | 2043 |   | 
        
           |  |  | 2044 | 		//if ($this->bucket !== '')
 | 
        
           |  |  | 2045 | 		//	$this->resource = '/'.$this->bucket.$this->uri;
 | 
        
           |  |  | 2046 | 		//else
 | 
        
           |  |  | 2047 | 		//	$this->resource = $this->uri;
 | 
        
           |  |  | 2048 |   | 
        
           |  |  | 2049 | 		if ($this->bucket !== '')
 | 
        
           |  |  | 2050 | 		{
 | 
        
           |  |  | 2051 | 			if ($this->__dnsBucketName($this->bucket))
 | 
        
           |  |  | 2052 | 			{
 | 
        
           |  |  | 2053 | 				$this->headers['Host'] = $this->bucket.'.'.$this->endpoint;
 | 
        
           |  |  | 2054 | 				$this->resource = '/'.$this->bucket.$this->uri;
 | 
        
           |  |  | 2055 | 			}
 | 
        
           |  |  | 2056 | 			else
 | 
        
           |  |  | 2057 | 			{
 | 
        
           |  |  | 2058 | 				$this->headers['Host'] = $this->endpoint;
 | 
        
           |  |  | 2059 | 				$this->uri = $this->uri;
 | 
        
           |  |  | 2060 | 				if ($this->bucket !== '') $this->uri = '/'.$this->bucket.$this->uri;
 | 
        
           |  |  | 2061 | 				$this->bucket = '';
 | 
        
           |  |  | 2062 | 				$this->resource = $this->uri;
 | 
        
           |  |  | 2063 | 			}
 | 
        
           |  |  | 2064 | 		}
 | 
        
           |  |  | 2065 | 		else
 | 
        
           |  |  | 2066 | 		{
 | 
        
           |  |  | 2067 | 			$this->headers['Host'] = $this->endpoint;
 | 
        
           |  |  | 2068 | 			$this->resource = $this->uri;
 | 
        
           |  |  | 2069 | 		}
 | 
        
           |  |  | 2070 |   | 
        
           |  |  | 2071 |   | 
        
           |  |  | 2072 | 		$this->headers['Date'] = gmdate('D, d M Y H:i:s T');
 | 
        
           |  |  | 2073 | 		$this->response = new STDClass;
 | 
        
           |  |  | 2074 | 		$this->response->error = false;
 | 
        
           |  |  | 2075 | 		$this->response->body = null;
 | 
        
           |  |  | 2076 | 		$this->response->headers = array();
 | 
        
           |  |  | 2077 | 	}
 | 
        
           |  |  | 2078 |   | 
        
           |  |  | 2079 |   | 
        
           |  |  | 2080 | 	/**
 | 
        
           |  |  | 2081 | 	* Set request parameter
 | 
        
           |  |  | 2082 | 	*
 | 
        
           |  |  | 2083 | 	* @param string $key Key
 | 
        
           |  |  | 2084 | 	* @param string $value Value
 | 
        
           |  |  | 2085 | 	* @return void
 | 
        
           |  |  | 2086 | 	*/
 | 
        
           |  |  | 2087 | 	public function setParameter($key, $value)
 | 
        
           |  |  | 2088 | 	{
 | 
        
           |  |  | 2089 | 		$this->parameters[$key] = $value;
 | 
        
           |  |  | 2090 | 	}
 | 
        
           |  |  | 2091 |   | 
        
           |  |  | 2092 |   | 
        
           |  |  | 2093 | 	/**
 | 
        
           |  |  | 2094 | 	* Set request header
 | 
        
           |  |  | 2095 | 	*
 | 
        
           |  |  | 2096 | 	* @param string $key Key
 | 
        
           |  |  | 2097 | 	* @param string $value Value
 | 
        
           |  |  | 2098 | 	* @return void
 | 
        
           |  |  | 2099 | 	*/
 | 
        
           |  |  | 2100 | 	public function setHeader($key, $value)
 | 
        
           |  |  | 2101 | 	{
 | 
        
           |  |  | 2102 | 		$this->headers[$key] = $value;
 | 
        
           |  |  | 2103 | 	}
 | 
        
           |  |  | 2104 |   | 
        
           |  |  | 2105 |   | 
        
           |  |  | 2106 | 	/**
 | 
        
           |  |  | 2107 | 	* Set x-amz-meta-* header
 | 
        
           |  |  | 2108 | 	*
 | 
        
           |  |  | 2109 | 	* @param string $key Key
 | 
        
           |  |  | 2110 | 	* @param string $value Value
 | 
        
           |  |  | 2111 | 	* @return void
 | 
        
           |  |  | 2112 | 	*/
 | 
        
           |  |  | 2113 | 	public function setAmzHeader($key, $value)
 | 
        
           |  |  | 2114 | 	{
 | 
        
           |  |  | 2115 | 		$this->amzHeaders[$key] = $value;
 | 
        
           |  |  | 2116 | 	}
 | 
        
           |  |  | 2117 |   | 
        
           |  |  | 2118 |   | 
        
           |  |  | 2119 | 	/**
 | 
        
           |  |  | 2120 | 	* Get the S3 response
 | 
        
           |  |  | 2121 | 	*
 | 
        
           |  |  | 2122 | 	* @return object | false
 | 
        
           |  |  | 2123 | 	*/
 | 
        
           |  |  | 2124 | 	public function getResponse()
 | 
        
           |  |  | 2125 | 	{
 | 
        
           |  |  | 2126 | 		$query = '';
 | 
        
           |  |  | 2127 | 		if (sizeof($this->parameters) > 0)
 | 
        
           |  |  | 2128 | 		{
 | 
        
           |  |  | 2129 | 			$query = substr($this->uri, -1) !== '?' ? '?' : '&';
 | 
        
           |  |  | 2130 | 			foreach ($this->parameters as $var => $value)
 | 
        
           |  |  | 2131 | 				if ($value == null || $value == '') $query .= $var.'&';
 | 
        
           |  |  | 2132 | 				else $query .= $var.'='.rawurlencode($value).'&';
 | 
        
           |  |  | 2133 | 			$query = substr($query, 0, -1);
 | 
        
           |  |  | 2134 | 			$this->uri .= $query;
 | 
        
           |  |  | 2135 |   | 
        
           |  |  | 2136 | 			if (array_key_exists('acl', $this->parameters) ||
 | 
        
           |  |  | 2137 | 			array_key_exists('location', $this->parameters) ||
 | 
        
           |  |  | 2138 | 			array_key_exists('torrent', $this->parameters) ||
 | 
        
           |  |  | 2139 | 			array_key_exists('website', $this->parameters) ||
 | 
        
           |  |  | 2140 | 			array_key_exists('logging', $this->parameters))
 | 
        
           |  |  | 2141 | 				$this->resource .= $query;
 | 
        
           |  |  | 2142 | 		}
 | 
        
           |  |  | 2143 | 		$url = (S3::$useSSL ? 'https://' : 'http://') . ($this->headers['Host'] !== '' ? $this->headers['Host'] : $this->endpoint) . $this->uri;
 | 
        
           |  |  | 2144 |   | 
        
           |  |  | 2145 | 		//var_dump('bucket: ' . $this->bucket, 'uri: ' . $this->uri, 'resource: ' . $this->resource, 'url: ' . $url);
 | 
        
           |  |  | 2146 |   | 
        
           |  |  | 2147 | 		// Basic setup
 | 
        
           |  |  | 2148 | 		$curl = curl_init();
 | 
        
           |  |  | 2149 | 		curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php');
 | 
        
           |  |  | 2150 |   | 
        
           |  |  | 2151 | 		if (S3::$useSSL)
 | 
        
           |  |  | 2152 | 		{
 | 
        
           |  |  | 2153 | 			// Set protocol version
 | 
        
           |  |  | 2154 | 			curl_setopt($curl, CURLOPT_SSLVERSION, S3::$useSSLVersion);
 | 
        
           |  |  | 2155 |   | 
        
           |  |  | 2156 | 			// SSL Validation can now be optional for those with broken OpenSSL installations
 | 
        
           |  |  | 2157 | 			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, S3::$useSSLValidation ? 2 : 0);
 | 
        
           |  |  | 2158 | 			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, S3::$useSSLValidation ? 1 : 0);
 | 
        
           |  |  | 2159 |   | 
        
           |  |  | 2160 | 			if (S3::$sslKey !== null) curl_setopt($curl, CURLOPT_SSLKEY, S3::$sslKey);
 | 
        
           |  |  | 2161 | 			if (S3::$sslCert !== null) curl_setopt($curl, CURLOPT_SSLCERT, S3::$sslCert);
 | 
        
           |  |  | 2162 | 			if (S3::$sslCACert !== null) curl_setopt($curl, CURLOPT_CAINFO, S3::$sslCACert);
 | 
        
           |  |  | 2163 | 		}
 | 
        
           |  |  | 2164 |   | 
        
           |  |  | 2165 | 		curl_setopt($curl, CURLOPT_URL, $url);
 | 
        
           |  |  | 2166 |   | 
        
           |  |  | 2167 | 		if (S3::$proxy != null && isset(S3::$proxy['host']))
 | 
        
           |  |  | 2168 | 		{
 | 
        
           |  |  | 2169 | 			curl_setopt($curl, CURLOPT_PROXY, S3::$proxy['host']);
 | 
        
           |  |  | 2170 | 			curl_setopt($curl, CURLOPT_PROXYTYPE, S3::$proxy['type']);
 | 
        
           |  |  | 2171 | 			if (isset(S3::$proxy['user'], S3::$proxy['pass']) && S3::$proxy['user'] != null && S3::$proxy['pass'] != null)
 | 
        
           |  |  | 2172 | 				curl_setopt($curl, CURLOPT_PROXYUSERPWD, sprintf('%s:%s', S3::$proxy['user'], S3::$proxy['pass']));
 | 
        
           |  |  | 2173 | 		}
 | 
        
           |  |  | 2174 |   | 
        
           |  |  | 2175 | 		// Headers
 | 
        
           |  |  | 2176 | 		$headers = array(); $amz = array();
 | 
        
           |  |  | 2177 | 		foreach ($this->amzHeaders as $header => $value)
 | 
        
           |  |  | 2178 | 			if (strlen($value) > 0) $headers[] = $header.': '.$value;
 | 
        
           |  |  | 2179 | 		foreach ($this->headers as $header => $value)
 | 
        
           |  |  | 2180 | 			if (strlen($value) > 0) $headers[] = $header.': '.$value;
 | 
        
           |  |  | 2181 |   | 
        
           |  |  | 2182 | 		// Collect AMZ headers for signature
 | 
        
           |  |  | 2183 | 		foreach ($this->amzHeaders as $header => $value)
 | 
        
           |  |  | 2184 | 			if (strlen($value) > 0) $amz[] = strtolower($header).':'.$value;
 | 
        
           |  |  | 2185 |   | 
        
           |  |  | 2186 | 		// AMZ headers must be sorted
 | 
        
           |  |  | 2187 | 		if (sizeof($amz) > 0)
 | 
        
           |  |  | 2188 | 		{
 | 
        
           |  |  | 2189 | 			//sort($amz);
 | 
        
           |  |  | 2190 | 			usort($amz, array(&$this, '__sortMetaHeadersCmp'));
 | 
        
           |  |  | 2191 | 			$amz = "\n".implode("\n", $amz);
 | 
        
           |  |  | 2192 | 		} else $amz = '';
 | 
        
           |  |  | 2193 |   | 
        
           |  |  | 2194 | 		if (S3::hasAuth())
 | 
        
           |  |  | 2195 | 		{
 | 
        
           |  |  | 2196 | 			// Authorization string (CloudFront stringToSign should only contain a date)
 | 
        
           |  |  | 2197 | 			if ($this->headers['Host'] == 'cloudfront.amazonaws.com')
 | 
        
           |  |  | 2198 | 				$headers[] = 'Authorization: ' . S3::__getSignature($this->headers['Date']);
 | 
        
           |  |  | 2199 | 			else
 | 
        
           |  |  | 2200 | 			{
 | 
        
           |  |  | 2201 | 				$headers[] = 'Authorization: ' . S3::__getSignature(
 | 
        
           |  |  | 2202 | 					$this->verb."\n".
 | 
        
           |  |  | 2203 | 					$this->headers['Content-MD5']."\n".
 | 
        
           |  |  | 2204 | 					$this->headers['Content-Type']."\n".
 | 
        
           |  |  | 2205 | 					$this->headers['Date'].$amz."\n".
 | 
        
           |  |  | 2206 | 					$this->resource
 | 
        
           |  |  | 2207 | 				);
 | 
        
           |  |  | 2208 | 			}
 | 
        
           |  |  | 2209 | 		}
 | 
        
           |  |  | 2210 |   | 
        
           |  |  | 2211 | 		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
 | 
        
           |  |  | 2212 | 		curl_setopt($curl, CURLOPT_HEADER, false);
 | 
        
           |  |  | 2213 | 		curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
 | 
        
           |  |  | 2214 | 		curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback'));
 | 
        
           |  |  | 2215 | 		curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback'));
 | 
        
           |  |  | 2216 | 		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
 | 
        
           |  |  | 2217 |   | 
        
           |  |  | 2218 | 		// Request types
 | 
        
           |  |  | 2219 | 		switch ($this->verb)
 | 
        
           |  |  | 2220 | 		{
 | 
        
           |  |  | 2221 | 			case 'GET': break;
 | 
        
           |  |  | 2222 | 			case 'PUT': case 'POST': // POST only used for CloudFront
 | 
        
           |  |  | 2223 | 				if ($this->fp !== false)
 | 
        
           |  |  | 2224 | 				{
 | 
        
           |  |  | 2225 | 					curl_setopt($curl, CURLOPT_PUT, true);
 | 
        
           |  |  | 2226 | 					curl_setopt($curl, CURLOPT_INFILE, $this->fp);
 | 
        
           |  |  | 2227 | 					if ($this->size >= 0)
 | 
        
           |  |  | 2228 | 						curl_setopt($curl, CURLOPT_INFILESIZE, $this->size);
 | 
        
           |  |  | 2229 | 				}
 | 
        
           |  |  | 2230 | 				elseif ($this->data !== false)
 | 
        
           |  |  | 2231 | 				{
 | 
        
           |  |  | 2232 | 					curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
 | 
        
           |  |  | 2233 | 					curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
 | 
        
           |  |  | 2234 | 				}
 | 
        
           |  |  | 2235 | 				else
 | 
        
           |  |  | 2236 | 					curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
 | 
        
           |  |  | 2237 | 			break;
 | 
        
           |  |  | 2238 | 			case 'HEAD':
 | 
        
           |  |  | 2239 | 				curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
 | 
        
           |  |  | 2240 | 				curl_setopt($curl, CURLOPT_NOBODY, true);
 | 
        
           |  |  | 2241 | 			break;
 | 
        
           |  |  | 2242 | 			case 'DELETE':
 | 
        
           |  |  | 2243 | 				curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
 | 
        
           |  |  | 2244 | 			break;
 | 
        
           |  |  | 2245 | 			default: break;
 | 
        
           |  |  | 2246 | 		}
 | 
        
           |  |  | 2247 |   | 
        
           |  |  | 2248 | 		// Execute, grab errors
 | 
        
           |  |  | 2249 | 		if (curl_exec($curl))
 | 
        
           |  |  | 2250 | 			$this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
 | 
        
           |  |  | 2251 | 		else
 | 
        
           |  |  | 2252 | 			$this->response->error = array(
 | 
        
           |  |  | 2253 | 				'code' => curl_errno($curl),
 | 
        
           |  |  | 2254 | 				'message' => curl_error($curl),
 | 
        
           |  |  | 2255 | 				'resource' => $this->resource
 | 
        
           |  |  | 2256 | 			);
 | 
        
           |  |  | 2257 |   | 
        
           |  |  | 2258 | 		@curl_close($curl);
 | 
        
           |  |  | 2259 |   | 
        
           |  |  | 2260 | 		// Parse body into XML
 | 
        
           |  |  | 2261 | 		if ($this->response->error === false && isset($this->response->headers['type']) &&
 | 
        
           |  |  | 2262 | 		$this->response->headers['type'] == 'application/xml' && isset($this->response->body))
 | 
        
           |  |  | 2263 | 		{
 | 
        
           |  |  | 2264 | 			$this->response->body = simplexml_load_string($this->response->body);
 | 
        
           |  |  | 2265 |   | 
        
           |  |  | 2266 | 			// Grab S3 errors
 | 
        
           |  |  | 2267 | 			if (!in_array($this->response->code, array(200, 204, 206)) &&
 | 
        
           |  |  | 2268 | 			isset($this->response->body->Code, $this->response->body->Message))
 | 
        
           |  |  | 2269 | 			{
 | 
        
           |  |  | 2270 | 				$this->response->error = array(
 | 
        
           |  |  | 2271 | 					'code' => (string)$this->response->body->Code,
 | 
        
           |  |  | 2272 | 					'message' => (string)$this->response->body->Message
 | 
        
           |  |  | 2273 | 				);
 | 
        
           |  |  | 2274 | 				if (isset($this->response->body->Resource))
 | 
        
           |  |  | 2275 | 					$this->response->error['resource'] = (string)$this->response->body->Resource;
 | 
        
           |  |  | 2276 | 				unset($this->response->body);
 | 
        
           |  |  | 2277 | 			}
 | 
        
           |  |  | 2278 | 		}
 | 
        
           |  |  | 2279 |   | 
        
           |  |  | 2280 | 		// Clean up file resources
 | 
        
           |  |  | 2281 | 		if ($this->fp !== false && is_resource($this->fp)) fclose($this->fp);
 | 
        
           |  |  | 2282 |   | 
        
           |  |  | 2283 | 		return $this->response;
 | 
        
           |  |  | 2284 | 	}
 | 
        
           |  |  | 2285 |   | 
        
           |  |  | 2286 | 	/**
 | 
        
           |  |  | 2287 | 	* Sort compare for meta headers
 | 
        
           |  |  | 2288 | 	*
 | 
        
           |  |  | 2289 | 	* @internal Used to sort x-amz meta headers
 | 
        
           |  |  | 2290 | 	* @param string $a String A
 | 
        
           |  |  | 2291 | 	* @param string $b String B
 | 
        
           |  |  | 2292 | 	* @return integer
 | 
        
           |  |  | 2293 | 	*/
 | 
        
           |  |  | 2294 | 	private function __sortMetaHeadersCmp($a, $b)
 | 
        
           |  |  | 2295 | 	{
 | 
        
           |  |  | 2296 | 		$lenA = strpos($a, ':');
 | 
        
           |  |  | 2297 | 		$lenB = strpos($b, ':');
 | 
        
           |  |  | 2298 | 		$minLen = min($lenA, $lenB);
 | 
        
           |  |  | 2299 | 		$ncmp = strncmp($a, $b, $minLen);
 | 
        
           |  |  | 2300 | 		if ($lenA == $lenB) return $ncmp;
 | 
        
           |  |  | 2301 | 		if (0 == $ncmp) return $lenA < $lenB ? -1 : 1;
 | 
        
           |  |  | 2302 | 		return $ncmp;
 | 
        
           |  |  | 2303 | 	}
 | 
        
           |  |  | 2304 |   | 
        
           |  |  | 2305 | 	/**
 | 
        
           |  |  | 2306 | 	* CURL write callback
 | 
        
           |  |  | 2307 | 	*
 | 
        
           |  |  | 2308 | 	* @param resource &$curl CURL resource
 | 
        
           |  |  | 2309 | 	* @param string &$data Data
 | 
        
           |  |  | 2310 | 	* @return integer
 | 
        
           |  |  | 2311 | 	*/
 | 
        
           |  |  | 2312 | 	private function __responseWriteCallback(&$curl, &$data)
 | 
        
           |  |  | 2313 | 	{
 | 
        
           |  |  | 2314 | 		if (in_array($this->response->code, array(200, 206)) && $this->fp !== false)
 | 
        
           |  |  | 2315 | 			return fwrite($this->fp, $data);
 | 
        
           |  |  | 2316 | 		else
 | 
        
           |  |  | 2317 | 			$this->response->body .= $data;
 | 
        
           |  |  | 2318 | 		return strlen($data);
 | 
        
           |  |  | 2319 | 	}
 | 
        
           |  |  | 2320 |   | 
        
           |  |  | 2321 |   | 
        
           |  |  | 2322 | 	/**
 | 
        
           |  |  | 2323 | 	* Check DNS conformity
 | 
        
           |  |  | 2324 | 	*
 | 
        
           |  |  | 2325 | 	* @param string $bucket Bucket name
 | 
        
           |  |  | 2326 | 	* @return boolean
 | 
        
           |  |  | 2327 | 	*/
 | 
        
           |  |  | 2328 | 	private function __dnsBucketName($bucket)
 | 
        
           |  |  | 2329 | 	{
 | 
        
           |  |  | 2330 | 		if (strlen($bucket) > 63 || preg_match("/[^a-z0-9\.-]/", $bucket) > 0) return false;
 | 
        
           |  |  | 2331 | 		if (S3::$useSSL && strstr($bucket, '.') !== false) return false;
 | 
        
           |  |  | 2332 | 		if (strstr($bucket, '-.') !== false) return false;
 | 
        
           |  |  | 2333 | 		if (strstr($bucket, '..') !== false) return false;
 | 
        
           |  |  | 2334 | 		if (!preg_match("/^[0-9a-z]/", $bucket)) return false;
 | 
        
           |  |  | 2335 | 		if (!preg_match("/[0-9a-z]$/", $bucket)) return false;
 | 
        
           |  |  | 2336 | 		return true;
 | 
        
           |  |  | 2337 | 	}
 | 
        
           |  |  | 2338 |   | 
        
           |  |  | 2339 |   | 
        
           |  |  | 2340 | 	/**
 | 
        
           |  |  | 2341 | 	* CURL header callback
 | 
        
           |  |  | 2342 | 	*
 | 
        
           |  |  | 2343 | 	* @param resource $curl CURL resource
 | 
        
           |  |  | 2344 | 	* @param string $data Data
 | 
        
           |  |  | 2345 | 	* @return integer
 | 
        
           |  |  | 2346 | 	*/
 | 
        
           |  |  | 2347 | 	private function __responseHeaderCallback($curl, $data)
 | 
        
           |  |  | 2348 | 	{
 | 
        
           |  |  | 2349 | 		if (($strlen = strlen($data)) <= 2) return $strlen;
 | 
        
           |  |  | 2350 | 		if (substr($data, 0, 4) == 'HTTP')
 | 
        
           |  |  | 2351 | 			$this->response->code = (int)substr($data, 9, 3);
 | 
        
           |  |  | 2352 | 		else
 | 
        
           |  |  | 2353 | 		{
 | 
        
           |  |  | 2354 | 			$data = trim($data);
 | 
        
           |  |  | 2355 | 			if (strpos($data, ': ') === false) return $strlen;
 | 
        
           |  |  | 2356 | 			list($header, $value) = explode(': ', $data, 2);
 | 
        
           |  |  | 2357 | 			if ($header == 'Last-Modified')
 | 
        
           |  |  | 2358 | 				$this->response->headers['time'] = strtotime($value);
 | 
        
           |  |  | 2359 | 			elseif ($header == 'Date')
 | 
        
           |  |  | 2360 | 				$this->response->headers['date'] = strtotime($value);
 | 
        
           |  |  | 2361 | 			elseif ($header == 'Content-Length')
 | 
        
           |  |  | 2362 | 				$this->response->headers['size'] = (int)$value;
 | 
        
           |  |  | 2363 | 			elseif ($header == 'Content-Type')
 | 
        
           |  |  | 2364 | 				$this->response->headers['type'] = $value;
 | 
        
           |  |  | 2365 | 			elseif ($header == 'ETag')
 | 
        
           |  |  | 2366 | 				$this->response->headers['hash'] = $value[0] == '"' ? substr($value, 1, -1) : $value;
 | 
        
           |  |  | 2367 | 			elseif (preg_match('/^x-amz-meta-.*$/', $header))
 | 
        
           |  |  | 2368 | 				$this->response->headers[$header] = $value;
 | 
        
           |  |  | 2369 | 		}
 | 
        
           |  |  | 2370 | 		return $strlen;
 | 
        
           |  |  | 2371 | 	}
 | 
        
           |  |  | 2372 |   | 
        
           |  |  | 2373 | }
 | 
        
           |  |  | 2374 |   | 
        
           |  |  | 2375 | /**
 | 
        
           |  |  | 2376 |  * S3 exception class
 | 
        
           |  |  | 2377 |  *
 | 
        
           |  |  | 2378 |  * @link http://undesigned.org.za/2007/10/22/amazon-s3-php-class
 | 
        
           |  |  | 2379 |  * @version 0.5.0-dev
 | 
        
           |  |  | 2380 |  */
 | 
        
           |  |  | 2381 |   | 
        
           |  |  | 2382 | class S3Exception extends Exception {
 | 
        
           |  |  | 2383 | 	/**
 | 
        
           |  |  | 2384 | 	 * Class constructor
 | 
        
           |  |  | 2385 | 	 *
 | 
        
           |  |  | 2386 | 	 * @param string $message Exception message
 | 
        
           |  |  | 2387 | 	 * @param string $file File in which exception was created
 | 
        
           |  |  | 2388 | 	 * @param string $line Line number on which exception was created
 | 
        
           |  |  | 2389 | 	 * @param int $code Exception code
 | 
        
           |  |  | 2390 | 	 */
 | 
        
           |  |  | 2391 | 	function __construct($message, $file, $line, $code = 0)
 | 
        
           |  |  | 2392 | 	{
 | 
        
           |  |  | 2393 | 		parent::__construct($message, $code);
 | 
        
           |  |  | 2394 | 		$this->file = $file;
 | 
        
           |  |  | 2395 | 		$this->line = $line;
 | 
        
           |  |  | 2396 | 	}
 | 
        
           |  |  | 2397 | }
 |