PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/aws-sdk/sdk.class.php

http://github.com/punkave/aS3StreamWrapper
PHP | 1403 lines | 692 code | 201 blank | 510 comment | 87 complexity | b2022d4b532190f134385da1fb0477f1 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. /*%******************************************************************************************%*/
  17. // EXCEPTIONS
  18. /**
  19. * Default CFRuntime Exception.
  20. */
  21. class CFRuntime_Exception extends Exception {}
  22. /**
  23. * Parsing Exception.
  24. */
  25. class Parser_Exception extends Exception {}
  26. /*%******************************************************************************************%*/
  27. // DETERMINE WHAT ENVIRONMENT DATA TO ADD TO THE USERAGENT FOR METRIC TRACKING
  28. /*
  29. Define a temporary callback function for this calculation. Get the PHP version and any
  30. required/optional extensions that are leveraged.
  31. Tracking this data gives Amazon better metrics about what configurations are being used
  32. so that forward-looking plans for the code can be made with more certainty (e.g. What
  33. version of PHP are most people running? Do they tend to have the latest PCRE?).
  34. */
  35. function __aws_sdk_ua_callback()
  36. {
  37. $ua_append = '';
  38. $extensions = get_loaded_extensions();
  39. $sorted_extensions = array();
  40. if ($extensions)
  41. {
  42. foreach ($extensions as $extension)
  43. {
  44. if ($extension === 'curl' && function_exists('curl_version'))
  45. {
  46. $curl_version = curl_version();
  47. $sorted_extensions[strtolower($extension)] = $curl_version['version'];
  48. }
  49. elseif ($extension === 'pcre' && defined('PCRE_VERSION'))
  50. {
  51. $pcre_version = explode(' ', PCRE_VERSION);
  52. $sorted_extensions[strtolower($extension)] = $pcre_version[0];
  53. }
  54. elseif ($extension === 'openssl' && defined('OPENSSL_VERSION_TEXT'))
  55. {
  56. $openssl_version = explode(' ', OPENSSL_VERSION_TEXT);
  57. $sorted_extensions[strtolower($extension)] = $openssl_version[1];
  58. }
  59. else
  60. {
  61. $sorted_extensions[strtolower($extension)] = phpversion($extension);
  62. }
  63. }
  64. }
  65. foreach (array('simplexml', 'json', 'pcre', 'spl', 'curl', 'openssl', 'apc', 'xcache', 'memcache', 'memcached', 'pdo', 'pdo_sqlite', 'sqlite', 'sqlite3', 'zlib', 'xdebug') as $ua_ext)
  66. {
  67. if (isset($sorted_extensions[$ua_ext]) && $sorted_extensions[$ua_ext])
  68. {
  69. $ua_append .= ' ' . $ua_ext . '/' . $sorted_extensions[$ua_ext];
  70. }
  71. elseif (isset($sorted_extensions[$ua_ext]))
  72. {
  73. $ua_append .= ' ' . $ua_ext . '/0';
  74. }
  75. }
  76. foreach (array('memory_limit', 'date.timezone', 'open_basedir', 'safe_mode', 'zend.enable_gc') as $cfg)
  77. {
  78. $cfg_value = ini_get($cfg);
  79. if (in_array($cfg, array('memory_limit', 'date.timezone'), true))
  80. {
  81. $ua_append .= ' ' . $cfg . '/' . str_replace('/', '.', $cfg_value);
  82. }
  83. elseif (in_array($cfg, array('open_basedir', 'safe_mode', 'zend.enable_gc'), true))
  84. {
  85. if ($cfg_value === false || $cfg_value === '' || $cfg_value === 0)
  86. {
  87. $cfg_value = 'off';
  88. }
  89. elseif ($cfg_value === true || $cfg_value === '1' || $cfg_value === 1)
  90. {
  91. $cfg_value = 'on';
  92. }
  93. $ua_append .= ' ' . $cfg . '/' . $cfg_value;
  94. }
  95. }
  96. return $ua_append;
  97. }
  98. /*%******************************************************************************************%*/
  99. // INTERMEDIARY CONSTANTS
  100. define('CFRUNTIME_NAME', 'aws-sdk-php');
  101. define('CFRUNTIME_VERSION', '1.5.2');
  102. // define('CFRUNTIME_BUILD', gmdate('YmdHis', filemtime(__FILE__))); // @todo: Hardcode for release.
  103. define('CFRUNTIME_BUILD', '20120202002749');
  104. define('CFRUNTIME_USERAGENT', CFRUNTIME_NAME . '/' . CFRUNTIME_VERSION . ' PHP/' . PHP_VERSION . ' ' . str_replace(' ', '_', php_uname('s')) . '/' . str_replace(' ', '_', php_uname('r')) . ' Arch/' . php_uname('m') . ' SAPI/' . php_sapi_name() . ' Integer/' . PHP_INT_MAX . ' Build/' . CFRUNTIME_BUILD . __aws_sdk_ua_callback());
  105. /*%******************************************************************************************%*/
  106. // CLASS
  107. /**
  108. * Core functionality and default settings shared across all SDK classes. All methods and properties in this
  109. * class are inherited by the service-specific classes.
  110. *
  111. * @version 2012.02.01
  112. * @license See the included NOTICE.md file for more information.
  113. * @copyright See the included NOTICE.md file for more information.
  114. * @link http://aws.amazon.com/php/ PHP Developer Center
  115. */
  116. class CFRuntime
  117. {
  118. /*%******************************************************************************************%*/
  119. // CONSTANTS
  120. /**
  121. * Name of the software.
  122. */
  123. const NAME = CFRUNTIME_NAME;
  124. /**
  125. * Version of the software.
  126. */
  127. const VERSION = CFRUNTIME_VERSION;
  128. /**
  129. * Build ID of the software.
  130. */
  131. const BUILD = CFRUNTIME_BUILD;
  132. /**
  133. * User agent string used to identify the software.
  134. */
  135. const USERAGENT = CFRUNTIME_USERAGENT;
  136. /*%******************************************************************************************%*/
  137. // PROPERTIES
  138. /**
  139. * The Amazon API Key.
  140. */
  141. public $key;
  142. /**
  143. * The Amazon API Secret Key.
  144. */
  145. public $secret_key;
  146. /**
  147. * The Amazon Authentication Token.
  148. */
  149. public $auth_token;
  150. /**
  151. * Handle for the utility functions.
  152. */
  153. public $util;
  154. /**
  155. * An identifier for the current AWS service.
  156. */
  157. public $service = null;
  158. /**
  159. * The supported API version.
  160. */
  161. public $api_version = null;
  162. /**
  163. * The state of whether auth should be handled as AWS Query.
  164. */
  165. public $use_aws_query = true;
  166. /**
  167. * The default class to use for utilities (defaults to <CFUtilities>).
  168. */
  169. public $utilities_class = 'CFUtilities';
  170. /**
  171. * The default class to use for HTTP requests (defaults to <CFRequest>).
  172. */
  173. public $request_class = 'CFRequest';
  174. /**
  175. * The default class to use for HTTP responses (defaults to <CFResponse>).
  176. */
  177. public $response_class = 'CFResponse';
  178. /**
  179. * The default class to use for parsing XML (defaults to <CFSimpleXML>).
  180. */
  181. public $parser_class = 'CFSimpleXML';
  182. /**
  183. * The default class to use for handling batch requests (defaults to <CFBatchRequest>).
  184. */
  185. public $batch_class = 'CFBatchRequest';
  186. /**
  187. * The state of SSL/HTTPS use.
  188. */
  189. public $use_ssl = true;
  190. /**
  191. * The state of SSL certificate verification.
  192. */
  193. public $ssl_verification = true;
  194. /**
  195. * The proxy to use for connecting.
  196. */
  197. public $proxy = null;
  198. /**
  199. * The alternate hostname to use, if any.
  200. */
  201. public $hostname = null;
  202. /**
  203. * The state of the capability to override the hostname with <set_hostname()>.
  204. */
  205. public $override_hostname = true;
  206. /**
  207. * The alternate port number to use, if any.
  208. */
  209. public $port_number = null;
  210. /**
  211. * The alternate resource prefix to use, if any.
  212. */
  213. public $resource_prefix = null;
  214. /**
  215. * The state of cache flow usage.
  216. */
  217. public $use_cache_flow = false;
  218. /**
  219. * The caching class to use.
  220. */
  221. public $cache_class = null;
  222. /**
  223. * The caching location to use.
  224. */
  225. public $cache_location = null;
  226. /**
  227. * When the cache should be considered stale.
  228. */
  229. public $cache_expires = null;
  230. /**
  231. * The state of cache compression.
  232. */
  233. public $cache_compress = null;
  234. /**
  235. * The current instantiated cache object.
  236. */
  237. public $cache_object = null;
  238. /**
  239. * The current instantiated batch request object.
  240. */
  241. public $batch_object = null;
  242. /**
  243. * The internally instantiated batch request object.
  244. */
  245. public $internal_batch_object = null;
  246. /**
  247. * The state of batch flow usage.
  248. */
  249. public $use_batch_flow = false;
  250. /**
  251. * The state of the cache deletion setting.
  252. */
  253. public $delete_cache = false;
  254. /**
  255. * The state of the debug mode setting.
  256. */
  257. public $debug_mode = false;
  258. /**
  259. * The number of times to retry failed requests.
  260. */
  261. public $max_retries = 3;
  262. /**
  263. * The user-defined callback function to call when a stream is read from.
  264. */
  265. public $registered_streaming_read_callback = null;
  266. /**
  267. * The user-defined callback function to call when a stream is written to.
  268. */
  269. public $registered_streaming_write_callback = null;
  270. /**
  271. * The credentials to use for authentication.
  272. */
  273. public $credentials = array();
  274. /**
  275. * The authentication class to use.
  276. */
  277. public $auth_class = null;
  278. /**
  279. * The operation to execute.
  280. */
  281. public $operation = null;
  282. /**
  283. * The payload to send.
  284. */
  285. public $payload = array();
  286. /**
  287. * The string prefix to prepend to the operation name.
  288. */
  289. public $operation_prefix = '';
  290. /**
  291. * The number of times a request has been retried.
  292. */
  293. public $redirects = 0;
  294. /**
  295. * The state of whether the response should be parsed or not.
  296. */
  297. public $parse_the_response = true;
  298. /*%******************************************************************************************%*/
  299. // CONSTRUCTOR
  300. /**
  301. * The constructor. This class should not be instantiated directly. Rather, a service-specific class
  302. * should be instantiated.
  303. *
  304. * @param array $options (Optional) An associative array of parameters that can have the following keys: <ul>
  305. * <li><code>certificate_authority</code> - <code>boolean</code> - Optional - Determines which Cerificate Authority file to use. A value of boolean <code>false</code> will use the Certificate Authority file available on the system. A value of boolean <code>true</code> will use the Certificate Authority provided by the SDK. Passing a file system path to a Certificate Authority file (chmodded to <code>0755</code>) will use that. Leave this set to <code>false</code> if you're not sure.</li>
  306. * <li><code>credentials</code> - <code>string</code> - Optional - The name of the credential set to use for authentication.</li>
  307. * <li><code>default_cache_config</code> - <code>string</code> - Optional - This option allows a preferred storage type to be configured for long-term caching. This can be changed later using the <set_cache_config()> method. Valid values are: <code>apc</code>, <code>xcache</code>, or a file system path such as <code>./cache</code> or <code>/tmp/cache/</code>.</li>
  308. * <li><code>key</code> - <code>string</code> - Optional - Your AWS key, or a session key. If blank, the default credential set will be used.</li>
  309. * <li><code>secret</code> - <code>string</code> - Optional - Your AWS secret key, or a session secret key. If blank, the default credential set will be used.</li>
  310. * <li><code>token</code> - <code>string</code> - Optional - An AWS session token.</li></ul>
  311. * @return void
  312. */
  313. public function __construct(array $options = array())
  314. {
  315. // Instantiate the utilities class.
  316. $this->util = new $this->utilities_class();
  317. // Determine the current service.
  318. $this->service = get_class($this);
  319. // Create credentials based on the options
  320. $instance_credentials = new CFCredential($options);
  321. // Retreive a credential set from config.inc.php if it exists
  322. if (isset($options['credentials']))
  323. {
  324. // Use a specific credential set and merge with the instance credentials
  325. $this->credentials = CFCredentials::get($options['credentials'])
  326. ->merge($instance_credentials);
  327. }
  328. else
  329. {
  330. try
  331. {
  332. // Use the default credential set and merge with the instance credentials
  333. $this->credentials = CFCredentials::get(CFCredentials::DEFAULT_KEY)
  334. ->merge($instance_credentials);
  335. }
  336. catch (CFCredentials_Exception $e)
  337. {
  338. if (isset($options['key']) && isset($options['secret']))
  339. {
  340. // Only the instance credentials were provided
  341. $this->credentials = $instance_credentials;
  342. }
  343. else
  344. {
  345. // No credentials provided in the config file or constructor
  346. throw new CFCredentials_Exception('No credentials were provided to ' . $this->service . '.');
  347. }
  348. }
  349. }
  350. // Set internal credentials after they are resolved
  351. $this->key = $this->credentials->key;
  352. $this->secret_key = $this->credentials->secret;
  353. $this->auth_token = $this->credentials->token;
  354. // Automatically enable whichever caching mechanism is set to default.
  355. $this->set_cache_config($this->credentials->default_cache_config);
  356. }
  357. /**
  358. * Alternate approach to constructing a new instance. Supports chaining.
  359. *
  360. * @param array $options (Optional) An associative array of parameters that can have the following keys: <ul>
  361. * <li><code>certificate_authority</code> - <code>boolean</code> - Optional - Determines which Cerificate Authority file to use. A value of boolean <code>false</code> will use the Certificate Authority file available on the system. A value of boolean <code>true</code> will use the Certificate Authority provided by the SDK. Passing a file system path to a Certificate Authority file (chmodded to <code>0755</code>) will use that. Leave this set to <code>false</code> if you're not sure.</li>
  362. * <li><code>credentials</code> - <code>string</code> - Optional - The name of the credential set to use for authentication.</li>
  363. * <li><code>default_cache_config</code> - <code>string</code> - Optional - This option allows a preferred storage type to be configured for long-term caching. This can be changed later using the <set_cache_config()> method. Valid values are: <code>apc</code>, <code>xcache</code>, or a file system path such as <code>./cache</code> or <code>/tmp/cache/</code>.</li>
  364. * <li><code>key</code> - <code>string</code> - Optional - Your AWS key, or a session key. If blank, the default credential set will be used.</li>
  365. * <li><code>secret</code> - <code>string</code> - Optional - Your AWS secret key, or a session secret key. If blank, the default credential set will be used.</li>
  366. * <li><code>token</code> - <code>string</code> - Optional - An AWS session token.</li></ul>
  367. * @return void
  368. */
  369. public static function factory(array $options = array())
  370. {
  371. if (version_compare(PHP_VERSION, '5.3.0', '<'))
  372. {
  373. throw new Exception('PHP 5.3 or newer is required to instantiate a new class with CLASS::factory().');
  374. }
  375. $self = get_called_class();
  376. return new $self($options);
  377. }
  378. /*%******************************************************************************************%*/
  379. // MAGIC METHODS
  380. /**
  381. * A magic method that allows `camelCase` method names to be translated into `snake_case` names.
  382. *
  383. * @param string $name (Required) The name of the method.
  384. * @param array $arguments (Required) The arguments passed to the method.
  385. * @return mixed The results of the intended method.
  386. */
  387. public function __call($name, $arguments)
  388. {
  389. // Convert camelCase method calls to snake_case.
  390. $method_name = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name));
  391. if (method_exists($this, $method_name))
  392. {
  393. return call_user_func_array(array($this, $method_name), $arguments);
  394. }
  395. throw new CFRuntime_Exception('The method ' . $name . '() is undefined. Attempted to map to ' . $method_name . '() which is also undefined. Error occurred');
  396. }
  397. /*%******************************************************************************************%*/
  398. // SET CUSTOM SETTINGS
  399. /**
  400. * Set the proxy settings to use.
  401. *
  402. * @param string $proxy (Required) Accepts proxy credentials in the following format: `proxy://user:pass@hostname:port`
  403. * @return $this A reference to the current instance.
  404. */
  405. public function set_proxy($proxy)
  406. {
  407. $this->proxy = $proxy;
  408. return $this;
  409. }
  410. /**
  411. * Set the hostname to connect to. This is useful for alternate services that are API-compatible with
  412. * AWS, but run from a different hostname.
  413. *
  414. * @param string $hostname (Required) The alternate hostname to use in place of the default one. Useful for mock or test applications living on different hostnames.
  415. * @param integer $port_number (Optional) The alternate port number to use in place of the default one. Useful for mock or test applications living on different port numbers.
  416. * @return $this A reference to the current instance.
  417. */
  418. public function set_hostname($hostname, $port_number = null)
  419. {
  420. if ($this->override_hostname)
  421. {
  422. $this->hostname = $hostname;
  423. if ($port_number)
  424. {
  425. $this->port_number = $port_number;
  426. $this->hostname .= ':' . (string) $this->port_number;
  427. }
  428. }
  429. return $this;
  430. }
  431. /**
  432. * Set the resource prefix to use. This method is useful for alternate services that are API-compatible
  433. * with AWS.
  434. *
  435. * @param string $prefix (Required) An alternate prefix to prepend to the resource path. Useful for mock or test applications.
  436. * @return $this A reference to the current instance.
  437. */
  438. public function set_resource_prefix($prefix)
  439. {
  440. $this->resource_prefix = $prefix;
  441. return $this;
  442. }
  443. /**
  444. * Disables any subsequent use of the <set_hostname()> method.
  445. *
  446. * @param boolean $override (Optional) Whether or not subsequent calls to <set_hostname()> should be obeyed. A `false` value disables the further effectiveness of <set_hostname()>. Defaults to `true`.
  447. * @return $this A reference to the current instance.
  448. */
  449. public function allow_hostname_override($override = true)
  450. {
  451. $this->override_hostname = $override;
  452. return $this;
  453. }
  454. /**
  455. * Disables SSL/HTTPS connections for hosts that don't support them. Some services, however, still
  456. * require SSL support.
  457. *
  458. * This method will throw a user warning when invoked, which can be hidden by changing your
  459. * <php:error_reporting()> settings.
  460. *
  461. * @return $this A reference to the current instance.
  462. */
  463. public function disable_ssl()
  464. {
  465. trigger_error('Disabling SSL connections is potentially unsafe and highly discouraged.', E_USER_WARNING);
  466. $this->use_ssl = false;
  467. return $this;
  468. }
  469. /**
  470. * Disables the verification of the SSL Certificate Authority. Doing so can enable an attacker to carry
  471. * out a man-in-the-middle attack.
  472. *
  473. * https://secure.wikimedia.org/wikipedia/en/wiki/Man-in-the-middle_attack
  474. *
  475. * This method will throw a user warning when invoked, which can be hidden by changing your
  476. * <php:error_reporting()> settings.
  477. *
  478. * @return $this A reference to the current instance.
  479. */
  480. public function disable_ssl_verification($ssl_verification = false)
  481. {
  482. trigger_error('Disabling the verification of SSL certificates can lead to man-in-the-middle attacks. It is potentially unsafe and highly discouraged.', E_USER_WARNING);
  483. $this->ssl_verification = $ssl_verification;
  484. return $this;
  485. }
  486. /**
  487. * Enables HTTP request/response header logging to `STDERR`.
  488. *
  489. * @param boolean $enabled (Optional) Whether or not to enable debug mode. Defaults to `true`.
  490. * @return $this A reference to the current instance.
  491. */
  492. public function enable_debug_mode($enabled = true)
  493. {
  494. $this->debug_mode = $enabled;
  495. return $this;
  496. }
  497. /**
  498. * Sets the maximum number of times to retry failed requests.
  499. *
  500. * @param integer $retries (Optional) The maximum number of times to retry failed requests. Defaults to `3`.
  501. * @return $this A reference to the current instance.
  502. */
  503. public function set_max_retries($retries = 3)
  504. {
  505. $this->max_retries = $retries;
  506. return $this;
  507. }
  508. /**
  509. * Set the caching configuration to use for response caching.
  510. *
  511. * @param string $location (Required) <p>The location to store the cache object in. This may vary by cache method.</p><ul><li>File - The local file system paths such as <code>./cache</code> (relative) or <code>/tmp/cache/</code> (absolute). The location must be server-writable.</li><li>APC - Pass in <code>apc</code> to use this lightweight cache. You must have the <a href="http://php.net/apc">APC extension</a> installed.</li><li>XCache - Pass in <code>xcache</code> to use this lightweight cache. You must have the <a href="http://xcache.lighttpd.net">XCache</a> extension installed.</li><li>Memcached - Pass in an indexed array of associative arrays. Each associative array should have a <code>host</code> and a <code>port</code> value representing a <a href="http://php.net/memcached">Memcached</a> server to connect to.</li><li>PDO - A URL-style string (e.g. <code>pdo.mysql://user:pass@localhost/cache</code>) or a standard DSN-style string (e.g. <code>pdo.sqlite:/sqlite/cache.db</code>). MUST be prefixed with <code>pdo.</code>. See <code>CachePDO</code> and <a href="http://php.net/pdo">PDO</a> for more details.</li></ul>
  512. * @param boolean $gzip (Optional) Whether or not data should be gzipped before being stored. A value of `true` will compress the contents before caching them. A value of `false` will leave the contents uncompressed. Defaults to `true`.
  513. * @return $this A reference to the current instance.
  514. */
  515. public function set_cache_config($location, $gzip = true)
  516. {
  517. // If we have an array, we're probably passing in Memcached servers and ports.
  518. if (is_array($location))
  519. {
  520. $this->cache_class = 'CacheMC';
  521. }
  522. else
  523. {
  524. // I would expect locations like `/tmp/cache`, `pdo.mysql://user:pass@hostname:port`, `pdo.sqlite:memory:`, and `apc`.
  525. $type = strtolower(substr($location, 0, 3));
  526. switch ($type)
  527. {
  528. case 'apc':
  529. $this->cache_class = 'CacheAPC';
  530. break;
  531. case 'xca': // First three letters of `xcache`
  532. $this->cache_class = 'CacheXCache';
  533. break;
  534. case 'pdo':
  535. $this->cache_class = 'CachePDO';
  536. $location = substr($location, 4);
  537. break;
  538. default:
  539. $this->cache_class = 'CacheFile';
  540. break;
  541. }
  542. }
  543. // Set the remaining cache information.
  544. $this->cache_location = $location;
  545. $this->cache_compress = $gzip;
  546. return $this;
  547. }
  548. /**
  549. * Register a callback function to execute whenever a data stream is read from using
  550. * <CFRequest::streaming_read_callback()>.
  551. *
  552. * The user-defined callback function should accept three arguments:
  553. *
  554. * <ul>
  555. * <li><code>$curl_handle</code> - <code>resource</code> - Required - The cURL handle resource that represents the in-progress transfer.</li>
  556. * <li><code>$file_handle</code> - <code>resource</code> - Required - The file handle resource that represents the file on the local file system.</li>
  557. * <li><code>$length</code> - <code>integer</code> - Required - The length in kilobytes of the data chunk that was transferred.</li>
  558. * </ul>
  559. *
  560. * @param string|array|function $callback (Required) The callback function is called by <php:call_user_func()>, so you can pass the following values: <ul>
  561. * <li>The name of a global function to execute, passed as a string.</li>
  562. * <li>A method to execute, passed as <code>array('ClassName', 'MethodName')</code>.</li>
  563. * <li>An anonymous function (PHP 5.3+).</li></ul>
  564. * @return $this A reference to the current instance.
  565. */
  566. public function register_streaming_read_callback($callback)
  567. {
  568. $this->registered_streaming_read_callback = $callback;
  569. return $this;
  570. }
  571. /**
  572. * Register a callback function to execute whenever a data stream is written to using
  573. * <CFRequest::streaming_write_callback()>.
  574. *
  575. * The user-defined callback function should accept two arguments:
  576. *
  577. * <ul>
  578. * <li><code>$curl_handle</code> - <code>resource</code> - Required - The cURL handle resource that represents the in-progress transfer.</li>
  579. * <li><code>$length</code> - <code>integer</code> - Required - The length in kilobytes of the data chunk that was transferred.</li>
  580. * </ul>
  581. *
  582. * @param string|array|function $callback (Required) The callback function is called by <php:call_user_func()>, so you can pass the following values: <ul>
  583. * <li>The name of a global function to execute, passed as a string.</li>
  584. * <li>A method to execute, passed as <code>array('ClassName', 'MethodName')</code>.</li>
  585. * <li>An anonymous function (PHP 5.3+).</li></ul>
  586. * @return $this A reference to the current instance.
  587. */
  588. public function register_streaming_write_callback($callback)
  589. {
  590. $this->registered_streaming_write_callback = $callback;
  591. return $this;
  592. }
  593. /*%******************************************************************************************%*/
  594. // SET CUSTOM CLASSES
  595. /**
  596. * Set a custom class for this functionality. Use this method when extending/overriding existing classes
  597. * with new functionality.
  598. *
  599. * The replacement class must extend from <CFUtilities>.
  600. *
  601. * @param string $class (Optional) The name of the new class to use for this functionality.
  602. * @return $this A reference to the current instance.
  603. */
  604. public function set_utilities_class($class = 'CFUtilities')
  605. {
  606. $this->utilities_class = $class;
  607. $this->util = new $this->utilities_class();
  608. return $this;
  609. }
  610. /**
  611. * Set a custom class for this functionality. Use this method when extending/overriding existing classes
  612. * with new functionality.
  613. *
  614. * The replacement class must extend from <CFRequest>.
  615. *
  616. * @param string $class (Optional) The name of the new class to use for this functionality.
  617. * @param $this A reference to the current instance.
  618. */
  619. public function set_request_class($class = 'CFRequest')
  620. {
  621. $this->request_class = $class;
  622. return $this;
  623. }
  624. /**
  625. * Set a custom class for this functionality. Use this method when extending/overriding existing classes
  626. * with new functionality.
  627. *
  628. * The replacement class must extend from <CFResponse>.
  629. *
  630. * @param string $class (Optional) The name of the new class to use for this functionality.
  631. * @return $this A reference to the current instance.
  632. */
  633. public function set_response_class($class = 'CFResponse')
  634. {
  635. $this->response_class = $class;
  636. return $this;
  637. }
  638. /**
  639. * Set a custom class for this functionality. Use this method when extending/overriding existing classes
  640. * with new functionality.
  641. *
  642. * The replacement class must extend from <CFSimpleXML>.
  643. *
  644. * @param string $class (Optional) The name of the new class to use for this functionality.
  645. * @return $this A reference to the current instance.
  646. */
  647. public function set_parser_class($class = 'CFSimpleXML')
  648. {
  649. $this->parser_class = $class;
  650. return $this;
  651. }
  652. /**
  653. * Set a custom class for this functionality. Use this method when extending/overriding existing classes
  654. * with new functionality.
  655. *
  656. * The replacement class must extend from <CFBatchRequest>.
  657. *
  658. * @param string $class (Optional) The name of the new class to use for this functionality.
  659. * @return $this A reference to the current instance.
  660. */
  661. public function set_batch_class($class = 'CFBatchRequest')
  662. {
  663. $this->batch_class = $class;
  664. return $this;
  665. }
  666. /*%******************************************************************************************%*/
  667. // AUTHENTICATION
  668. /**
  669. * Default, shared method for authenticating a connection to AWS.
  670. *
  671. * @param string $operation (Required) Indicates the operation to perform.
  672. * @param array $payload (Required) An associative array of parameters for authenticating. See the individual methods for allowed keys.
  673. * @return CFResponse Object containing a parsed HTTP response.
  674. */
  675. public function authenticate($operation, $payload)
  676. {
  677. $original_payload = $payload;
  678. $method_arguments = func_get_args();
  679. $curlopts = array();
  680. $return_curl_handle = false;
  681. if (substr($operation, 0, strlen($this->operation_prefix)) !== $this->operation_prefix)
  682. {
  683. $operation = $this->operation_prefix . $operation;
  684. }
  685. // Extract the custom CURLOPT settings from the payload
  686. if (is_array($payload) && isset($payload['curlopts']))
  687. {
  688. $curlopts = $payload['curlopts'];
  689. unset($payload['curlopts']);
  690. }
  691. // Determine whether the response or curl handle should be returned
  692. if (is_array($payload) && isset($payload['returnCurlHandle']))
  693. {
  694. $return_curl_handle = isset($payload['returnCurlHandle']) ? $payload['returnCurlHandle'] : false;
  695. unset($payload['returnCurlHandle']);
  696. }
  697. // Use the caching flow to determine if we need to do a round-trip to the server.
  698. if ($this->use_cache_flow)
  699. {
  700. // Generate an identifier specific to this particular set of arguments.
  701. $cache_id = $this->key . '_' . get_class($this) . '_' . $operation . '_' . sha1(serialize($method_arguments));
  702. // Instantiate the appropriate caching object.
  703. $this->cache_object = new $this->cache_class($cache_id, $this->cache_location, $this->cache_expires, $this->cache_compress);
  704. if ($this->delete_cache)
  705. {
  706. $this->use_cache_flow = false;
  707. $this->delete_cache = false;
  708. return $this->cache_object->delete();
  709. }
  710. // Invoke the cache callback function to determine whether to pull data from the cache or make a fresh request.
  711. $data = $this->cache_object->response_manager(array($this, 'cache_callback'), $method_arguments);
  712. // Parse the XML body
  713. $data = $this->parse_callback($data);
  714. // End!
  715. return $data;
  716. }
  717. /*%******************************************************************************************%*/
  718. // Signer
  719. $signer = new $this->auth_class($this->hostname, $operation, $payload, $this->credentials);
  720. $signer->key = $this->key;
  721. $signer->secret_key = $this->secret_key;
  722. $signer->auth_token = $this->auth_token;
  723. $signer->api_version = $this->api_version;
  724. $signer->utilities_class = $this->utilities_class;
  725. $signer->request_class = $this->request_class;
  726. $signer->response_class = $this->response_class;
  727. $signer->use_ssl = $this->use_ssl;
  728. $signer->proxy = $this->proxy;
  729. $signer->util = $this->util;
  730. $signer->registered_streaming_read_callback = $this->registered_streaming_read_callback;
  731. $signer->registered_streaming_write_callback = $this->registered_streaming_write_callback;
  732. $request = $signer->authenticate();
  733. // Update RequestCore settings
  734. $request->request_class = $this->request_class;
  735. $request->response_class = $this->response_class;
  736. $request->ssl_verification = $this->ssl_verification;
  737. /*%******************************************************************************************%*/
  738. // Debug mode
  739. if ($this->debug_mode)
  740. {
  741. $request->debug_mode = $this->debug_mode;
  742. }
  743. // Set custom CURLOPT settings
  744. if (count($curlopts))
  745. {
  746. $request->set_curlopts($curlopts);
  747. }
  748. // Manage the (newer) batch request API or the (older) returnCurlHandle setting.
  749. if ($this->use_batch_flow)
  750. {
  751. $handle = $request->prep_request();
  752. $this->batch_object->add($handle);
  753. $this->use_batch_flow = false;
  754. return $handle;
  755. }
  756. elseif ($return_curl_handle)
  757. {
  758. return $request->prep_request();
  759. }
  760. // Send!
  761. $request->send_request();
  762. // Prepare the response.
  763. $headers = $request->get_response_header();
  764. $headers['x-aws-stringtosign'] = $signer->string_to_sign;
  765. if (isset($signer->canonical_request))
  766. {
  767. $headers['x-aws-canonicalrequest'] = $signer->canonical_request;
  768. }
  769. $headers['x-aws-request-headers'] = $request->request_headers;
  770. $headers['x-aws-body'] = $signer->querystring;
  771. $data = new $this->response_class($headers, ($this->parse_the_response === true) ? $this->parse_callback($request->get_response_body()) : $request->get_response_body(), $request->get_response_code());
  772. // Was it Amazon's fault the request failed? Retry the request until we reach $max_retries.
  773. if (
  774. (integer) $request->get_response_code() === 500 || // Internal Error (presumably transient)
  775. (integer) $request->get_response_code() === 503) // Service Unavailable (presumably transient)
  776. {
  777. if ($this->redirects <= $this->max_retries)
  778. {
  779. // Exponential backoff
  780. $delay = (integer) (pow(4, $this->redirects) * 100000);
  781. usleep($delay);
  782. $this->redirects++;
  783. $data = $this->authenticate($operation, $original_payload);
  784. }
  785. }
  786. // DynamoDB has custom logic
  787. elseif (
  788. (integer) $request->get_response_code() === 400 &&
  789. stripos((string) $request->get_response_body(), 'com.amazonaws.dynamodb.') !== false && (
  790. stripos((string) $request->get_response_body(), 'ProvisionedThroughputExceededException') !== false
  791. )
  792. )
  793. {
  794. if ($this->redirects === 0)
  795. {
  796. $this->redirects++;
  797. $data = $this->authenticate($operation, $original_payload);
  798. }
  799. elseif ($this->redirects <= max($this->max_retries, 10))
  800. {
  801. // Exponential backoff
  802. $delay = (integer) (pow(2, ($this->redirects - 1)) * 50000);
  803. usleep($delay);
  804. $this->redirects++;
  805. $data = $this->authenticate($operation, $original_payload);
  806. }
  807. }
  808. $this->redirects = 0;
  809. return $data;
  810. }
  811. /*%******************************************************************************************%*/
  812. // BATCH REQUEST LAYER
  813. /**
  814. * Specifies that the intended request should be queued for a later batch request.
  815. *
  816. * @param CFBatchRequest $queue (Optional) The <CFBatchRequest> instance to use for managing batch requests. If not available, it generates a new instance of <CFBatchRequest>.
  817. * @return $this A reference to the current instance.
  818. */
  819. public function batch(CFBatchRequest &$queue = null)
  820. {
  821. if ($queue)
  822. {
  823. $this->batch_object = $queue;
  824. }
  825. elseif ($this->internal_batch_object)
  826. {
  827. $this->batch_object = &$this->internal_batch_object;
  828. }
  829. else
  830. {
  831. $this->internal_batch_object = new $this->batch_class();
  832. $this->batch_object = &$this->internal_batch_object;
  833. }
  834. $this->use_batch_flow = true;
  835. return $this;
  836. }
  837. /**
  838. * Executes the batch request queue by sending all queued requests.
  839. *
  840. * @param boolean $clear_after_send (Optional) Whether or not to clear the batch queue after sending a request. Defaults to `true`. Set this to `false` if you are caching batch responses and want to retrieve results later.
  841. * @return array An array of <CFResponse> objects.
  842. */
  843. public function send($clear_after_send = true)
  844. {
  845. if ($this->use_batch_flow)
  846. {
  847. // When we send the request, disable batch flow.
  848. $this->use_batch_flow = false;
  849. // If we're not caching, simply send the request.
  850. if (!$this->use_cache_flow)
  851. {
  852. $response = $this->batch_object->send();
  853. $parsed_data = array_map(array($this, 'parse_callback'), $response);
  854. $parsed_data = new CFArray($parsed_data);
  855. // Clear the queue
  856. if ($clear_after_send)
  857. {
  858. $this->batch_object->queue = array();
  859. }
  860. return $parsed_data;
  861. }
  862. // Generate an identifier specific to this particular set of arguments.
  863. $cache_id = $this->key . '_' . get_class($this) . '_' . sha1(serialize($this->batch_object));
  864. // Instantiate the appropriate caching object.
  865. $this->cache_object = new $this->cache_class($cache_id, $this->cache_location, $this->cache_expires, $this->cache_compress);
  866. if ($this->delete_cache)
  867. {
  868. $this->use_cache_flow = false;
  869. $this->delete_cache = false;
  870. return $this->cache_object->delete();
  871. }
  872. // Invoke the cache callback function to determine whether to pull data from the cache or make a fresh request.
  873. $data_set = $this->cache_object->response_manager(array($this, 'cache_callback_batch'), array($this->batch_object));
  874. $parsed_data = array_map(array($this, 'parse_callback'), $data_set);
  875. $parsed_data = new CFArray($parsed_data);
  876. // Clear the queue
  877. if ($clear_after_send)
  878. {
  879. $this->batch_object->queue = array();
  880. }
  881. // End!
  882. return $parsed_data;
  883. }
  884. // Load the class
  885. $null = new CFBatchRequest();
  886. unset($null);
  887. throw new CFBatchRequest_Exception('You must use $object->batch()->send()');
  888. }
  889. /**
  890. * Parses a response body into a PHP object if appropriate.
  891. *
  892. * @param CFResponse|string $response (Required) The <CFResponse> object to parse, or an XML string that would otherwise be a response body.
  893. * @param string $content_type (Optional) The content-type to use when determining how to parse the content.
  894. * @return CFResponse|string A parsed <CFResponse> object, or parsed XML.
  895. */
  896. public function parse_callback($response, $headers = null)
  897. {
  898. // Bail out
  899. if (!$this->parse_the_response) return $response;
  900. // Shorten this so we have a (mostly) single code path
  901. if (isset($response->body))
  902. {
  903. if (is_string($response->body))
  904. {
  905. $body = $response->body;
  906. }
  907. else
  908. {
  909. return $response;
  910. }
  911. }
  912. elseif (is_string($response))
  913. {
  914. $body = $response;
  915. }
  916. else
  917. {
  918. return $response;
  919. }
  920. // Decompress gzipped content
  921. if (isset($headers['content-encoding']))
  922. {
  923. switch (strtolower(trim($headers['content-encoding'], "\x09\x0A\x0D\x20")))
  924. {
  925. case 'gzip':
  926. case 'x-gzip':
  927. $decoder = new CFGzipDecode($body);
  928. if ($decoder->parse())
  929. {
  930. $body = $decoder->data;
  931. }
  932. break;
  933. case 'deflate':
  934. if (($uncompressed = gzuncompress($body)) !== false)
  935. {
  936. $body = $uncompressed;
  937. }
  938. elseif (($uncompressed = gzinflate($body)) !== false)
  939. {
  940. $body = $uncompressed;
  941. }
  942. break;
  943. }
  944. }
  945. // Look for XML cues
  946. if (
  947. (isset($headers['content-type']) && ($headers['content-type'] === 'text/xml' || $headers['content-type'] === 'application/xml')) || // We know it's XML
  948. (!isset($headers['content-type']) && (stripos($body, '<?xml') === 0 || strpos($body, '<Error>') === 0) || preg_match('/^<(\w*) xmlns="http(s?):\/\/(\w*).amazon(aws)?.com/im', $body)) // Sniff for XML
  949. )
  950. {
  951. // Strip the default XML namespace to simplify XPath expressions
  952. $body = str_replace("xmlns=", "ns=", $body);
  953. try {
  954. // Parse the XML body
  955. $body = new $this->parser_class($body);
  956. }
  957. catch (Exception $e)
  958. {
  959. throw new Parser_Exception($e->getMessage());
  960. }
  961. }
  962. // Look for JSON cues
  963. elseif (
  964. (isset($headers['content-type']) && ($headers['content-type'] === 'application/json') || $headers['content-type'] === 'application/x-amz-json-1.0') || // We know it's JSON
  965. (!isset($headers['content-type']) && $this->util->is_json($body)) // Sniff for JSON
  966. )
  967. {
  968. // Normalize JSON to a CFSimpleXML object
  969. $body = CFJSON::to_xml($body, $this->parser_class);
  970. }
  971. // Put the parsed data back where it goes
  972. if (isset($response->body))
  973. {
  974. $response->body = $body;
  975. }
  976. else
  977. {
  978. $response = $body;
  979. }
  980. return $response;
  981. }
  982. /*%******************************************************************************************%*/
  983. // CACHING LAYER
  984. /**
  985. * Specifies that the resulting <CFResponse> object should be cached according to the settings from
  986. * <set_cache_config()>.
  987. *
  988. * @param string|integer $expires (Required) The time the cache is to expire. Accepts a number of seconds as an integer, or an amount of time, as a string, that is understood by <php:strtotime()> (e.g. "1 hour").
  989. * @param $this A reference to the current instance.
  990. * @return $this
  991. */
  992. public function cache($expires)
  993. {
  994. // Die if they haven't used set_cache_config().
  995. if (!$this->cache_class)
  996. {
  997. throw new CFRuntime_Exception('Must call set_cache_config() before using cache()');
  998. }
  999. if (is_string($expires))
  1000. {
  1001. $expires = strtotime($expires);
  1002. $this->cache_expires = $expires - time();
  1003. }
  1004. elseif (is_int($expires))
  1005. {
  1006. $this->cache_expires = $expires;
  1007. }
  1008. $this->use_cache_flow = true;
  1009. return $this;
  1010. }
  1011. /**
  1012. * The callback function that is executed when the cache doesn't exist or has expired. The response of
  1013. * this method is cached. Accepts identical parameters as the <authenticate()> method. Never call this
  1014. * method directly -- it is used internally by the caching system.
  1015. *
  1016. * @param string $operation (Required) Indicates the operation to perform.
  1017. * @param array $payload (Required) An associative array of parameters for authenticating. See the individual methods for allowed keys.
  1018. * @return CFResponse A parsed HTTP response.
  1019. */
  1020. public function cache_callback($operation, $payload)
  1021. {
  1022. // Disable the cache flow since it's already been handled.
  1023. $this->use_cache_flow = false;
  1024. // Make the request
  1025. $response = $this->authenticate($operation, $payload);
  1026. // If this is an XML document, convert it back to a string.
  1027. if (isset($response->body) && ($response->body instanceof SimpleXMLElement))
  1028. {
  1029. $response->body = $response->body->asXML();
  1030. }
  1031. return $response;
  1032. }
  1033. /**
  1034. * Used for caching the results of a batch request. Never call this method directly; it is used
  1035. * internally by the caching system.
  1036. *
  1037. * @param CFBatchRequest $batch (Required) The batch request object to send.
  1038. * @return CFResponse A parsed HTTP response.
  1039. */
  1040. public function cache_callback_batch(CFBatchRequest $batch)
  1041. {
  1042. return $batch->send();
  1043. }
  1044. /**
  1045. * Deletes a cached <CFResponse> object using the specified cache storage type.
  1046. *
  1047. * @return boolean A value of `true` if cached object exists and is successfully deleted, otherwise `false`.
  1048. */
  1049. public function delete_cache()
  1050. {
  1051. $this->use_cache_flow = true;
  1052. $this->delete_cache = true;
  1053. return $this;
  1054. }
  1055. }
  1056. /**
  1057. * Contains the functionality for auto-loading service classes.
  1058. */
  1059. class CFLoader
  1060. {
  1061. /*%******************************************************************************************%*/
  1062. // AUTO-LOADER
  1063. /**
  1064. * Automatically load classes that aren't included.
  1065. *
  1066. * @param string $class (Required) The classname to load.
  1067. * @return boolean Whether or not the file was successfully loaded.
  1068. */
  1069. public static function autoloader($class)
  1070. {
  1071. $path = dirname(__FILE__) . DIRECTORY_SEPARATOR;
  1072. // Amazon SDK classes
  1073. if (strstr($class, 'Amazon'))
  1074. {
  1075. if (file_exists($require_this = $path . 'services' . DIRECTORY_SEPARATOR . str_ireplace('Amazon', '', strtolower($class)) . '.class.php'))
  1076. {
  1077. require_once $require_this;
  1078. return true;
  1079. }
  1080. return false;
  1081. }
  1082. // Utility classes
  1083. elseif (strstr($class, 'CF'))
  1084. {
  1085. if (file_exists($require_this = $path . 'utilities' . DIRECTORY_SEPARATOR . str_ireplace('CF', '', strtolower($class)) . '.class.php'))
  1086. {
  1087. require_once $require_this;
  1088. return true;
  1089. }
  1090. return false;
  1091. }
  1092. // Load CacheCore
  1093. elseif (strstr($class, 'Cache'))
  1094. {
  1095. if (file_exists($require_this = $path . 'lib' . DIRECTORY_SEPARATOR . 'cachecore' . DIRECTORY_SEPARATOR . strtolower($class) . '.class.php'))
  1096. {
  1097. require_once $require_this;
  1098. return true;
  1099. }
  1100. return false;
  1101. }
  1102. // Load RequestCore
  1103. elseif (strstr($class, 'RequestCore') || strstr($class, 'ResponseCore'))
  1104. {
  1105. if (file_exists($require_this = $path . 'lib' . DIRECTORY_SEPARATOR . 'requestcore' . DIRECTORY_SEPARATOR . 'requestcore.class.php'))
  1106. {
  1107. require_once $require_this;
  1108. return true;
  1109. }
  1110. return false;
  1111. }
  1112. // Load array-to-domdocument
  1113. elseif (strstr($class, 'Array2DOM'))
  1114. {
  1115. if (file_exists($require_this = $path . 'lib' . DIRECTORY_SEPARATOR . 'dom' . DIRECTORY_SEPARATOR . 'ArrayToDOMDocument.php'))
  1116. {
  1117. require_once $require_this;
  1118. return true;
  1119. }
  1120. return false;
  1121. }
  1122. // Load Authentication Signers
  1123. elseif (strstr($class, 'Auth'))
  1124. {
  1125. if (file_exists($require_this = $path . 'authentication' . DIRECTORY_SEPARATOR . str_replace('auth', 'signature_', strtolower($class)) . '.class.php'))
  1126. {
  1127. require_once $require_this;
  1128. return true;
  1129. }
  1130. return false;
  1131. }
  1132. // Load Signer interface
  1133. elseif ($class === 'Signer')
  1134. {
  1135. if (!interface_exists('Signable', false) &&
  1136. file_exists($require_this = $path . 'authentication' . DIRECTORY_SEPARATOR . 'signable.interface.php'))
  1137. {
  1138. require_once $require_this;
  1139. }
  1140. if (file_exists($require_this = $path . 'authentication' . DIRECTORY_SEPARATOR . 'signer.abstract.php'))
  1141. {
  1142. require_once $require_this;
  1143. return true;
  1144. }
  1145. return false;
  1146. }
  1147. // Load Symfony YAML classes
  1148. elseif (strstr($class, 'sfYaml'))
  1149. {
  1150. if (file_exists($require_this = $path . 'lib' . DIRECTORY_SEPARATOR . 'yaml' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'sfYaml.php'))
  1151. {
  1152. require_once $require_this;
  1153. return true;
  1154. }
  1155. return false;
  1156. }
  1157. return false;
  1158. }
  1159. }
  1160. // Register the autoloader.
  1161. spl_autoload_register(array('CFLoader', 'autoloader'));
  1162. /*%******************************************************************************************%*/
  1163. // CONFIGURATION
  1164. // Look for include file in the same directory (e.g. `./config.inc.php`).
  1165. if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.inc.php'))
  1166. {
  1167. include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.inc.php';
  1168. }
  1169. // Fallback to `~/.aws/sdk/config.inc.php`
  1170. else
  1171. {
  1172. if (!isset($_ENV['HOME']) && isset($_SERVER['HOME']))
  1173. {
  1174. $_ENV['HOME'] = $_SERVER['HOME'];
  1175. }
  1176. elseif (!isset($_ENV['HOME']) && !isset($_SERVER['HOME']))
  1177. {
  1178. $_ENV['HOME'] = `cd ~ && pwd`;
  1179. if (!$_ENV['HOME'])
  1180. {
  1181. switch (strtolower(PHP_OS))
  1182. {
  1183. case 'darwin':
  1184. $_ENV['HOME'] = '/Users/' . get_current_user();
  1185. break;
  1186. case 'windows':
  1187. case 'winnt':
  1188. case 'win32':
  1189. $_ENV['HOME'] = 'c:' . DIRECTORY_SEPARATOR . 'Documents and Settings' . DIRECTORY_SEPARATOR . get_current_user();
  1190. break;
  1191. default:
  1192. $_ENV['HOME'] = '/home/' . get_current_user();
  1193. break;
  1194. }
  1195. }
  1196. }
  1197. if (getenv('HOME') && file_exists(getenv('HOME') . DIRECTORY_SEPARATOR . '.aws' . DIRECTORY_SEPARATOR . 'sdk' . DIRECTORY_SEPARATOR . 'config.inc.php'))
  1198. {
  1199. include_once getenv('HOME') . DIRECTORY_SEPARATOR . '.aws' . DIRECTORY_SEPARATOR . 'sdk' . DIRECTORY_SEPARATOR . 'config.inc.php';
  1200. }
  1201. }