PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/lib/W3/Cdn/Rscf.php

https://gitlab.com/endomorphosis/jeffersonsmithmayor
PHP | 359 lines | 207 code | 71 blank | 81 comment | 24 complexity | fe68745723b524893a8fa25adc6b8128 MD5 | raw file
  1. <?php
  2. /**
  3. * Rackspace Cloud Files CDN engine
  4. */
  5. if (!defined('ABSPATH')) {
  6. die();
  7. }
  8. require_once W3TC_LIB_W3_DIR . '/Cdn/Base.php';
  9. require_once W3TC_LIB_CF_DIR . '/cloudfiles.php';
  10. /**
  11. * Class W3_Cdn_Rscf
  12. */
  13. class W3_Cdn_Rscf extends W3_Cdn_Base {
  14. /**
  15. * Auth object
  16. *
  17. * @var CF_Authentication
  18. */
  19. var $_auth = null;
  20. /**
  21. * Connection object
  22. *
  23. * @var CF_Connection
  24. */
  25. var $_connection = null;
  26. /**
  27. * Container object
  28. *
  29. * @var CF_Container
  30. */
  31. var $_container = null;
  32. /**
  33. * PHP5 Constructor
  34. *
  35. * @param array $config
  36. */
  37. function __construct($config = array()) {
  38. $config = array_merge(array(
  39. 'user' => '',
  40. 'key' => '',
  41. 'location' => 'us',
  42. 'container' => '',
  43. 'cname' => array(),
  44. ), $config);
  45. parent::__construct($config);
  46. }
  47. /**
  48. * PHP4 Constructor
  49. *
  50. * @param array $config
  51. */
  52. function W3_Cdn_Rscf($config = array()) {
  53. $this->__construct($config);
  54. }
  55. /**
  56. * Init connection object
  57. *
  58. * @param string $error
  59. * @return boolean
  60. */
  61. function _init(&$error) {
  62. if (empty($this->_config['user'])) {
  63. $error = 'Empty username.';
  64. return false;
  65. }
  66. if (empty($this->_config['key'])) {
  67. $error = 'Empty API key.';
  68. return false;
  69. }
  70. if (empty($this->_config['location'])) {
  71. $error = 'Empty API key.';
  72. return false;
  73. }
  74. switch ($this->_config['location']) {
  75. default:
  76. case 'us':
  77. $host = US_AUTHURL;
  78. break;
  79. case 'uk':
  80. $host = UK_AUTHURL;
  81. break;
  82. }
  83. try {
  84. $this->_auth = new CF_Authentication($this->_config['user'], $this->_config['key'], null, $host);
  85. $this->_auth->ssl_use_cabundle();
  86. $this->_auth->authenticate();
  87. $this->_connection = new CF_Connection($this->_auth);
  88. $this->_connection->ssl_use_cabundle();
  89. } catch (Exception $exception) {
  90. $error = $exception->getMessage();
  91. return false;
  92. }
  93. return true;
  94. }
  95. /**
  96. * Init container object
  97. *
  98. * @param string $error
  99. * @return boolean
  100. */
  101. function _init_container(&$error) {
  102. if (empty($this->_config['container'])) {
  103. $error = 'Empty container.';
  104. return false;
  105. }
  106. try {
  107. $this->_container = $this->_connection->get_container($this->_config['container']);
  108. } catch (Exception $exception) {
  109. $error = $exception->getMessage();
  110. return false;
  111. }
  112. return true;
  113. }
  114. /**
  115. * Uploads files to CDN
  116. *
  117. * @param array $files
  118. * @param array $results
  119. * @param boolean $force_rewrite
  120. * @return boolean
  121. */
  122. function upload($files, &$results, $force_rewrite = false) {
  123. $error = null;
  124. if (!$this->_init($error) || !$this->_init_container($error)) {
  125. $results = $this->_get_results($files, W3TC_CDN_RESULT_HALT, $error);
  126. return false;
  127. }
  128. foreach ($files as $local_path => $remote_path) {
  129. if (!file_exists($local_path)) {
  130. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, 'Source file not found.');
  131. continue;
  132. }
  133. try {
  134. @$object = & new CF_Object($this->_container, $remote_path, false, false);
  135. } catch (Exception $exception) {
  136. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf('Unable to create object (%s).', $exception->getMessage()));
  137. continue;
  138. }
  139. if (!$force_rewrite) {
  140. try {
  141. list($status, $reason, $etag, $last_modified, $content_type, $content_length, $metadata) = $this->_container->cfs_http->head_object($object);
  142. } catch (Exception $exception) {
  143. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf('Unable to get object info (%s).', $exception->getMessage()));
  144. continue;
  145. }
  146. if ($status >= 200 && $status < 300) {
  147. $hash = @md5_file($local_path);
  148. if ($hash === $etag) {
  149. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, 'Object up-to-date.');
  150. continue;
  151. }
  152. }
  153. }
  154. try {
  155. $object->load_from_filename($local_path);
  156. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, 'OK');
  157. } catch (Exception $exception) {
  158. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf('Unable to write object (%s).', $exception->getMessage()));
  159. }
  160. }
  161. return !$this->_is_error($results);
  162. }
  163. /**
  164. * Deletes files from CDN
  165. *
  166. * @param array $files
  167. * @param array $results
  168. * @return boolean
  169. */
  170. function delete($files, &$results) {
  171. $error = null;
  172. if (!$this->_init($error) || !$this->_init_container($error)) {
  173. $results = $this->_get_results($files, W3TC_CDN_RESULT_HALT, $error);
  174. return false;
  175. }
  176. foreach ($files as $local_path => $remote_path) {
  177. try {
  178. $this->_container->delete_object($remote_path);
  179. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, 'OK');
  180. } catch (Exception $exception) {
  181. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf('Unable to delete object (%s).', $exception->getMessage()));
  182. }
  183. }
  184. return !$this->_is_error($results);
  185. }
  186. /**
  187. * Test CDN connection
  188. *
  189. * @param string $error
  190. * @return boolean
  191. */
  192. function test(&$error) {
  193. if (!parent::test($error)) {
  194. return false;
  195. }
  196. if (!$this->_init($error) || !$this->_init_container($error)) {
  197. return false;
  198. }
  199. $string = 'test_rscf_' . md5(time());
  200. try {
  201. $object = $this->_container->create_object($string);
  202. $object->content_type = 'text/plain';
  203. $object->write($string, strlen($string));
  204. } catch (Exception $exception) {
  205. $error = sprintf('Unable to write object (%s).', $exception->getMessage());
  206. return false;
  207. }
  208. try {
  209. $object = $this->_container->get_object($string);
  210. $data = $object->read();
  211. } catch (Exception $exception) {
  212. $error = sprintf('Unable to read object (%s).', $exception->getMessage());
  213. try {
  214. $this->_container->delete_object($string);
  215. } catch (Exception $exception) {
  216. }
  217. return false;
  218. }
  219. if ($data != $string) {
  220. $error = 'Objects are not equal.';
  221. try {
  222. $this->_container->delete_object($string);
  223. } catch (Exception $exception) {
  224. }
  225. return false;
  226. }
  227. try {
  228. $this->_container->delete_object($string);
  229. } catch (Exception $exception) {
  230. $error = sprintf('Unable to delete object (%s).', $exception->getMessage());
  231. return false;
  232. }
  233. return true;
  234. }
  235. /**
  236. * Returns CDN domain
  237. *
  238. * @return array
  239. */
  240. function get_domains() {
  241. if (!empty($this->_config['cname'])) {
  242. return (array) $this->_config['cname'];
  243. }
  244. return array();
  245. }
  246. /**
  247. * Returns VIA string
  248. *
  249. * @return string
  250. */
  251. function get_via() {
  252. return sprintf('Rackspace Cloud Files: %s', parent::get_via());
  253. }
  254. /**
  255. * Creates container
  256. *
  257. * @param string $container_id
  258. * @param string $error
  259. * @return boolean
  260. */
  261. function create_container(&$container_id, &$error) {
  262. if (!$this->_init($error)) {
  263. return false;
  264. }
  265. try {
  266. $containers = $this->_connection->list_containers();
  267. } catch (Exception $exception) {
  268. $error = sprintf('Unable to list containers (%s).', $exception->getMessage());
  269. return false;
  270. }
  271. if (in_array($this->_config['container'], (array) $containers)) {
  272. $error = sprintf('Container already exists: %s.', $this->_config['container']);
  273. return false;
  274. }
  275. try {
  276. $container = $this->_connection->create_container($this->_config['container']);
  277. $container->make_public();
  278. } catch (Exception $exception) {
  279. $error = sprintf('Unable to create container: %s (%s).', $this->_config['container'], $exception->getMessage());
  280. return false;
  281. }
  282. $matches = null;
  283. if (preg_match('~^https?://(.+)$~', $container->cdn_uri, $matches)) {
  284. $container_id = $matches[1];
  285. }
  286. return true;
  287. }
  288. }