sdk.class.php 45 KB

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