PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/rackspace-cloud-files-cdn/lib/class.rs_cdn.php

https://bitbucket.org/theshipswakecreative/psw
PHP | 271 lines | 152 code | 47 blank | 72 comment | 22 complexity | 107d2f0b40754442ba4e6295ac19a7fb MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0
  1. <?php
  2. /**
  3. * Functions used to connect to the CDN
  4. */
  5. /**
  6. * Include global vars
  7. */
  8. global $wpdb;
  9. /**
  10. * CDN class
  11. */
  12. class RS_CDN {
  13. public $oc_connection;
  14. public $oc_container;
  15. public $cdn_url;
  16. public $api_settings;
  17. public $uploads;
  18. /**
  19. * Create new Openstack Object
  20. */
  21. function __construct($custom_settings = null, $oc_version = null) {
  22. // Get settings, if they exist
  23. (object) $custom_settings = (!is_null($custom_settings)) ? $custom_settings : get_option( RS_CDN_OPTIONS );
  24. // Set settings
  25. $settings = new stdClass();
  26. $settings->username = (isset($custom_settings->username)) ? $custom_settings->username : 'Username';
  27. $settings->apiKey = (isset($custom_settings->apiKey)) ? $custom_settings->apiKey : 'API Key';
  28. $settings->use_ssl = (isset($custom_settings->use_ssl)) ? $custom_settings->use_ssl : false;
  29. $settings->container = (isset($custom_settings->container)) ? $custom_settings->container : 'default';
  30. $settings->cdn_url = (isset($custom_settings->cdn_url)) ? $custom_settings->cdn_url : null;
  31. $settings->files_to_ignore = (isset($custom_settings->files_to_ignore)) ? $custom_settings->files_to_ignore : null;
  32. $settings->remove_local_files = (isset($custom_settings->remove_local_files)) ? $custom_settings->remove_local_files : false;
  33. $settings->custom_cname = (isset($custom_settings->custom_cname)) ? $custom_settings->custom_cname : null;
  34. $settings->region = (isset($custom_settings->region)) ? $custom_settings->region : 'ORD';
  35. $settings->url = (isset($custom_settings->url)) ? $custom_settings->url : 'https://identity.api.rackspacecloud.com/v2.0/';
  36. // Set API settings
  37. $this->api_settings = (object) $settings;
  38. // Set container object
  39. try {
  40. $the_container_obj = $this->container_object();
  41. // Assign container object
  42. $this->oc_container = $the_container_obj;
  43. } catch (Exception $exc) {
  44. return false;
  45. }
  46. }
  47. /**
  48. * Openstack Connection Object
  49. */
  50. function connection_object(){
  51. // If connection object is already set, return it
  52. if (isset($this->oc_connection)) {
  53. // Return existing connection object
  54. return $this->oc_connection;
  55. }
  56. // Get settings
  57. $api_settings = $this->api_settings;
  58. // Create connection object
  59. $connection = new \OpenCloud\Rackspace(
  60. $api_settings->url,
  61. array(
  62. 'username' => $api_settings->username,
  63. 'apiKey' => $api_settings->apiKey
  64. )
  65. );
  66. // Try to create connection object
  67. try {
  68. $cdn = $connection->ObjectStore( 'cloudFiles', $api_settings->region, 'publicURL' );
  69. $this->oc_connection = $cdn;
  70. return $this->oc_connection;
  71. } catch (Exception $exc) {
  72. $this->oc_connection = null;
  73. return null;
  74. }
  75. }
  76. /**
  77. * Retrieve Openstack CDN Container Object
  78. */
  79. public function container_object() {
  80. // If container object is already set, return it
  81. if (isset($this->oc_container)) {
  82. // Return existing container
  83. return $this->oc_container;
  84. }
  85. // Get settings
  86. $api_settings = $this->api_settings;
  87. // Check if connection object is valid
  88. if (is_null($this->connection_object())) {
  89. return null;
  90. }
  91. // Setup container
  92. try {
  93. // Try to set container
  94. $this->oc_container = $this->connection_object()->Container($api_settings->container);
  95. // Return container
  96. return $this->oc_container;
  97. } catch (Exception $exc) {
  98. $this->oc_container = null;
  99. return null;
  100. }
  101. }
  102. /**
  103. * Create Openstack CDN File Object
  104. */
  105. public function file_object($container, $file_path, $file_name = null){
  106. // Get file content
  107. $file_contents = @file_get_contents( $file_path );
  108. $file_name = (isset($file_name) && !is_null($file_name)) ? $file_name : basename( $file_path );
  109. // Create file object
  110. $file = $container->DataObject();
  111. $file->SetData( $file_contents );
  112. $file->name = $file_name;
  113. return $file;
  114. }
  115. /**
  116. * Uploads given file attachment to CDN
  117. */
  118. public function upload_file( $file_path , $file_name = null, $existing_container = null, $post_id = null){
  119. global $wpdb;
  120. // Check if file exists
  121. $check_file_name = (isset($file_name)) ? $file_name : basename($file_path);
  122. // Get ready to upload file to CDN
  123. $container = $this->container_object();
  124. $file = $this->file_object($container, $file_path, $file_name);
  125. // Upload object
  126. $content_type = get_content_type( $file_path );
  127. if ($content_type !== false) {
  128. if ($file->Create(array('content_type' => $content_type))) {
  129. return true;
  130. }
  131. } else {
  132. if ($file->Create()) {
  133. return true;
  134. }
  135. }
  136. // Upload failed, remove local images
  137. if (stripos($file_path, 'http') == 0) {
  138. $upload_dir = wp_upload_dir();
  139. $file_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file_path);
  140. unlink($file_path);
  141. } else {
  142. unlink($file_path);
  143. }
  144. // Upload failed, remove attachment from db
  145. if (isset($post_id)) {
  146. $wpdb->query("DELETE FROM $wpdb->posts WHERE ID='$post_id' AND post_type='attachment'");
  147. $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id='$post_id'");
  148. }
  149. return false;
  150. }
  151. /**
  152. * Get list of CDN objects
  153. */
  154. public function get_cdn_objects( $force_cache = false ) {
  155. // Ensure CDN instance exists
  156. if (check_cdn() === false) {
  157. return array();
  158. }
  159. // Path to cache file
  160. $cache_file_path = RS_CDN_PATH.'object_cache';
  161. // Set array to store CDN objects
  162. $cdn_objects = array();
  163. // Check if caching is enabled
  164. if ($force_cache === true || !is_writable(RS_CDN_PATH) || !is_writable($cache_file_path)) {
  165. // Update object cache
  166. try {
  167. $objects = $this->container_object()->objectList();
  168. } catch (Exception $exc) {
  169. return array();
  170. }
  171. // Setup objects
  172. $cdn_objects = array();
  173. foreach ($objects as $object) {
  174. $cdn_objects[] = array('fn' => $object['name'], 'fs' => $object['bytes']);
  175. }
  176. // Write files to cache file
  177. if (is_writable(RS_CDN_PATH) && is_writable($cache_file_path)) {
  178. // Write to cache file
  179. file_put_contents($cache_file_path, json_encode($cdn_objects));
  180. }
  181. // Return CDN objects
  182. return $cdn_objects;
  183. } else {
  184. // Return caching
  185. return json_decode(file_get_contents($cache_file_path), true);
  186. }
  187. }
  188. /**
  189. * Force CDN object cache
  190. */
  191. public function force_object_cache() {
  192. $this->get_cdn_objects( true );
  193. }
  194. /**
  195. * Removes given file attachment(s) from CDN
  196. */
  197. public function delete_files( $files ) {
  198. // Get container object
  199. $container = $this->container_object();
  200. // Delete object(s)
  201. if (count($files) > 0) {
  202. foreach ($files as $cur_file) {
  203. if (trim($cur_file) == '') {
  204. continue;
  205. }
  206. try {
  207. $file = $container->DataObject();
  208. $file->name = $cur_file;
  209. try {
  210. @$file->Delete();
  211. } catch (Exception $exc) {
  212. // Do nothing
  213. }
  214. } catch (Exception $exc) {
  215. // Do nothing
  216. }
  217. }
  218. // Force CDN cache because we removed files
  219. $this->force_object_cache();
  220. }
  221. return true;
  222. }
  223. }
  224. ?>