PageRenderTime 22ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/content/plugins/w3-total-cache/CdnEngine_S3_Compatible.php

https://gitlab.com/karlen/ayo_wp
PHP | 345 lines | 273 code | 28 blank | 44 comment | 12 complexity | 21ba605383c746879e6679a7c290e16b MD5 | raw file
  1. <?php
  2. namespace W3TC;
  3. /**
  4. * Amazon S3 CDN engine
  5. */
  6. if ( !class_exists( 'S3' ) ) {
  7. require_once W3TC_LIB_DIR . '/S3.php';
  8. }
  9. /**
  10. * class CdnEngine_S3
  11. */
  12. class CdnEngine_S3_Compatible extends CdnEngine_Base {
  13. /**
  14. * S3 object
  15. */
  16. private $_s3 = null;
  17. /**
  18. * PHP5 Constructor
  19. *
  20. * @param array $config
  21. */
  22. function __construct( $config = array() ) {
  23. $config = array_merge( array(
  24. 'key' => '',
  25. 'secret' => '',
  26. 'bucket' => '',
  27. 'cname' => array(),
  28. ), $config );
  29. $this->_s3 = new \S3( $config['key'], $config['secret'], false,
  30. $config['api_host'] );
  31. parent::__construct( $config );
  32. }
  33. /**
  34. * Formats URL
  35. *
  36. * @param string $path
  37. * @return string
  38. */
  39. function _format_url( $path ) {
  40. $domain = $this->get_domain( $path );
  41. if ( $domain ) {
  42. $scheme = $this->_get_scheme();
  43. // it does not support '+', requires '%2B'
  44. $path = str_replace( '+', '%2B', $path );
  45. $url = sprintf( '%s://%s/%s', $scheme, $domain, $path );
  46. return $url;
  47. }
  48. return false;
  49. }
  50. /**
  51. * Uploads files to S3
  52. *
  53. * @param array $files
  54. * @param array $results
  55. * @param boolean $force_rewrite
  56. * @return boolean
  57. */
  58. function upload( $files, &$results, $force_rewrite = false,
  59. $timeout_time = NULL ) {
  60. $error = null;
  61. foreach ( $files as $file ) {
  62. if ( !is_null( $timeout_time ) && time() > $timeout_time )
  63. break;
  64. $local_path = $file['local_path'];
  65. $remote_path = $file['remote_path'];
  66. $results[] = $this->_upload( $file, $force_rewrite );
  67. if ( $this->_config['compression'] && $this->_may_gzip( $remote_path ) ) {
  68. $file['remote_path_gzip'] = $remote_path . $this->_gzip_extension;
  69. $results[] = $this->_upload_gzip( $file, $force_rewrite );
  70. }
  71. }
  72. return !$this->_is_error( $results );
  73. }
  74. /**
  75. * Uploads single file to S3
  76. *
  77. * @param array CDN file array
  78. * @param boolean $force_rewrite
  79. * @return array
  80. */
  81. function _upload( $file, $force_rewrite = false ) {
  82. $local_path = $file['local_path'];
  83. $remote_path = $file['remote_path'];
  84. if ( !file_exists( $local_path ) ) {
  85. return $this->_get_result( $local_path, $remote_path,
  86. W3TC_CDN_RESULT_ERROR, 'Source file not found.', $file );
  87. }
  88. if ( !$force_rewrite ) {
  89. $this->_set_error_handler();
  90. $info = @$this->_s3->getObjectInfo( $this->_config['bucket'],
  91. $remote_path );
  92. $this->_restore_error_handler();
  93. if ( $info ) {
  94. $hash = @md5_file( $local_path );
  95. $s3_hash = ( isset( $info['hash'] ) ? $info['hash'] : '' );
  96. if ( $hash === $s3_hash ) {
  97. return $this->_get_result( $local_path, $remote_path,
  98. W3TC_CDN_RESULT_OK, 'Object up-to-date.', $file );
  99. }
  100. }
  101. }
  102. $headers = $this->_get_headers( $file );
  103. $this->_set_error_handler();
  104. $result = @$this->_s3->putObjectFile( $local_path,
  105. $this->_config['bucket'], $remote_path,
  106. \S3::ACL_PUBLIC_READ, array(), $headers );
  107. $this->_restore_error_handler();
  108. if ( $result ) {
  109. return $this->_get_result( $local_path, $remote_path,
  110. W3TC_CDN_RESULT_OK, 'OK', $file );
  111. }
  112. return $this->_get_result( $local_path, $remote_path,
  113. W3TC_CDN_RESULT_ERROR,
  114. sprintf( 'Unable to put object (%s).', $this->_get_last_error() ),
  115. $file );
  116. }
  117. /**
  118. * Uploads gzip version of file
  119. *
  120. * @param string $local_path
  121. * @param string $remote_path
  122. * @param boolean $force_rewrite
  123. * @return array
  124. */
  125. function _upload_gzip( $file, $force_rewrite = false ) {
  126. $local_path = $file['local_path'];
  127. $remote_path = $file['remote_path_gzip'];
  128. if ( !function_exists( 'gzencode' ) )
  129. return $this->_get_result( $local_path, $remote_path,
  130. W3TC_CDN_RESULT_ERROR, "GZIP library doesn't exist.", $file );
  131. if ( !file_exists( $local_path ) )
  132. return $this->_get_result( $local_path, $remote_path,
  133. W3TC_CDN_RESULT_ERROR, 'Source file not found.', $file );
  134. $contents = @file_get_contents( $local_path );
  135. if ( $contents === false )
  136. return $this->_get_result( $local_path, $remote_path,
  137. W3TC_CDN_RESULT_ERROR, 'Unable to read file.', $file );
  138. $data = gzencode( $contents );
  139. if ( !$force_rewrite ) {
  140. $this->_set_error_handler();
  141. $info = @$this->_s3->getObjectInfo( $this->_config['bucket'],
  142. $remote_path );
  143. $this->_restore_error_handler();
  144. if ( $info ) {
  145. $hash = md5( $data );
  146. $s3_hash = ( isset( $info['hash'] ) ? $info['hash'] : '' );
  147. if ( $hash === $s3_hash ) {
  148. return $this->_get_result( $local_path, $remote_path,
  149. W3TC_CDN_RESULT_OK, 'Object up-to-date.', $file );
  150. }
  151. }
  152. }
  153. $headers = $this->_get_headers( $file );
  154. $headers = array_merge( $headers, array(
  155. 'Vary' => 'Accept-Encoding',
  156. 'Content-Encoding' => 'gzip'
  157. ) );
  158. $this->_set_error_handler();
  159. $result = @$this->_s3->putObjectString( $data, $this->_config['bucket'],
  160. $remote_path, \S3::ACL_PUBLIC_READ, array(), $headers );
  161. $this->_restore_error_handler();
  162. if ( $result )
  163. return $this->_get_result( $local_path, $remote_path,
  164. W3TC_CDN_RESULT_OK, 'OK', $file );
  165. return $this->_get_result( $local_path, $remote_path,
  166. W3TC_CDN_RESULT_ERROR, sprintf( 'Unable to put object (%s).',
  167. $this->_get_last_error() ), $file );
  168. }
  169. /**
  170. * Deletes files from S3
  171. *
  172. * @param array $files
  173. * @param array $results
  174. * @return boolean
  175. */
  176. function delete( $files, &$results ) {
  177. $error = null;
  178. foreach ( $files as $file ) {
  179. $local_path = $file['local_path'];
  180. $remote_path = $file['remote_path'];
  181. $this->_set_error_handler();
  182. $result = @$this->_s3->deleteObject( $this->_config['bucket'],
  183. $remote_path );
  184. $this->_restore_error_handler();
  185. if ( $result ) {
  186. $results[] = $this->_get_result( $local_path, $remote_path,
  187. W3TC_CDN_RESULT_OK, 'OK', $file );
  188. } else {
  189. $results[] = $this->_get_result( $local_path, $remote_path,
  190. W3TC_CDN_RESULT_ERROR,
  191. sprintf( 'Unable to delete object (%s).',
  192. $this->_get_last_error() ), $file );
  193. }
  194. if ( $this->_config['compression'] ) {
  195. $remote_path_gzip = $remote_path . $this->_gzip_extension;
  196. $this->_set_error_handler();
  197. $result = @$this->_s3->deleteObject( $this->_config['bucket'],
  198. $remote_path_gzip );
  199. $this->_restore_error_handler();
  200. if ( $result ) {
  201. $results[] = $this->_get_result( $local_path,
  202. $remote_path_gzip, W3TC_CDN_RESULT_OK, 'OK', $file );
  203. } else {
  204. $results[] = $this->_get_result( $local_path,
  205. $remote_path_gzip, W3TC_CDN_RESULT_ERROR,
  206. sprintf( 'Unable to delete object (%s).',
  207. $this->_get_last_error() ),
  208. $file );
  209. }
  210. }
  211. }
  212. return !$this->_is_error( $results );
  213. }
  214. /**
  215. * Tests S3
  216. *
  217. * @param string $error
  218. * @return boolean
  219. */
  220. function test( &$error ) {
  221. if ( !parent::test( $error ) ) {
  222. return false;
  223. }
  224. $string = 'test_s3_' . md5( time() );
  225. $this->_set_error_handler();
  226. if ( !@$this->_s3->putObjectString( $string, $this->_config['bucket'],
  227. $string, \S3::ACL_PUBLIC_READ ) ) {
  228. $error = sprintf( 'Unable to put object (%s).',
  229. $this->_get_last_error() );
  230. $this->_restore_error_handler();
  231. return false;
  232. }
  233. $object = @$this->_s3->getObject( $this->_config['bucket'], $string );
  234. if ( !$object ) {
  235. $error = sprintf( 'Unable to get object (%s).',
  236. $this->_get_last_error() );
  237. $this->_restore_error_handler();
  238. return false;
  239. }
  240. if ( $object->body != $string ) {
  241. $error = 'Objects are not equal.';
  242. @$this->_s3->deleteObject( $this->_config['bucket'], $string );
  243. $this->_restore_error_handler();
  244. return false;
  245. }
  246. if ( !@$this->_s3->deleteObject( $this->_config['bucket'], $string ) ) {
  247. $error = sprintf( 'Unable to delete object (%s).',
  248. $this->_get_last_error() );
  249. $this->_restore_error_handler();
  250. return false;
  251. }
  252. $this->_restore_error_handler();
  253. return true;
  254. }
  255. /**
  256. * Returns CDN domain
  257. *
  258. * @return array
  259. */
  260. function get_domains() {
  261. return (array) $this->_config['cname'];
  262. }
  263. /**
  264. * Returns via string
  265. *
  266. * @return string
  267. */
  268. function get_via() {
  269. return sprintf( 'S3-compatible: %s', parent::get_via() );
  270. }
  271. /**
  272. * How and if headers should be set
  273. *
  274. * @return string W3TC_CDN_HEADER_NONE, W3TC_CDN_HEADER_UPLOADABLE,
  275. * W3TC_CDN_HEADER_MIRRORING
  276. */
  277. function headers_support() {
  278. return W3TC_CDN_HEADER_UPLOADABLE;
  279. }
  280. }