PageRenderTime 686ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/lib/amazonSdk/sdk.class.php

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