W3TCCdn_Core

Class Cdn_Core.

Defined (1)

The class is defined in the following location(s).

/Cdn_Core.php  
  1. /* class Cdn_Core 
  2. */ 
  3. class Cdn_Core { 
  4. /** 
  5. * Config 
  6. */ 
  7. private $_config = null; 
  8.  
  9. /** 
  10. * Runs plugin 
  11. */ 
  12. function __construct() { 
  13. $this->_config = Dispatcher::config(); 
  14.  
  15. /** 
  16. * Adds file to queue 
  17. * @param string $local_path 
  18. * @param string $remote_path 
  19. * @param integer $command 
  20. * @param string $last_error 
  21. * @return integer 
  22. */ 
  23. function queue_add( $local_path, $remote_path, $command, $last_error ) { 
  24. global $wpdb; 
  25.  
  26. $table = $wpdb->base_prefix . W3TC_CDN_TABLE_QUEUE; 
  27. $rows = $wpdb->get_results( $wpdb->prepare( 
  28. 'SELECT id, command '. 
  29. "FROM $table " . 
  30. 'WHERE local_path = %s AND remote_path = %s',  
  31. $local_path, $remote_path ) ); 
  32.  
  33. $already_exists = false; 
  34. foreach ( $rows as $row ) { 
  35. if ( $row->command != $command ) 
  36. $wpdb->query( $wpdb->prepare( 
  37. "DELETE FROM $table " . 
  38. 'WHERE id = %d', $row->id ) ); 
  39. else 
  40. $already_exists = true; 
  41.  
  42. if ( $already_exists ) 
  43. return true; 
  44.  
  45. // insert if not yet there 
  46. return $wpdb->query( $wpdb->prepare( 
  47. "INSERT INTO $table " . 
  48. '(local_path, remote_path, command, last_error, date) ' . 
  49. 'VALUES (%s, %s, %d, %s, NOW())',  
  50. $local_path, $remote_path, $command, $last_error ) ); 
  51.  
  52. /** 
  53. * Returns array of array('local_path' => '', 'remote_path' => '') for specified file 
  54. * @param string $file 
  55. * @return array 
  56. */ 
  57. function get_files_for_upload( $file ) { 
  58. $files = array(); 
  59. $upload_info = Util_Http::upload_info(); 
  60.  
  61. if ( $upload_info ) { 
  62. $file = $this->normalize_attachment_file( $file ); 
  63.  
  64. $local_file = $upload_info['basedir'] . '/' . $file; 
  65. $remote_file = ltrim( $upload_info['baseurlpath'] . $file, '/' ); 
  66.  
  67. $files[] = $this->build_file_descriptor( $local_file, $remote_file ); 
  68.  
  69. return $files; 
  70.  
  71. /** 
  72. * Returns array of files from sizes array 
  73. * @param string $attached_file 
  74. * @param array $sizes 
  75. * @return array 
  76. */ 
  77. function _get_sizes_files( $attached_file, $sizes ) { 
  78. $files = array(); 
  79. $base_dir = Util_File::dirname( $attached_file ); 
  80.  
  81. foreach ( (array) $sizes as $size ) { 
  82. if ( isset( $size['file'] ) ) { 
  83. if ( $base_dir ) { 
  84. $file = $base_dir . '/' . $size['file']; 
  85. } else { 
  86. $file = $size['file']; 
  87.  
  88. $files = array_merge( $files, $this->get_files_for_upload( $file ) ); 
  89.  
  90. return $files; 
  91.  
  92. /** 
  93. * Returns attachment files by metadata 
  94. * @param array $metadata 
  95. * @return array 
  96. */ 
  97. function get_metadata_files( $metadata ) { 
  98. $files = array(); 
  99.  
  100. if ( isset( $metadata['file'] ) && isset( $metadata['sizes'] ) ) { 
  101. $files = array_merge( $files, $this->_get_sizes_files( $metadata['file'], $metadata['sizes'] ) ); 
  102.  
  103. return $files; 
  104.  
  105. /** 
  106. * Returns attachment files by attachment ID 
  107. * @param integer $attachment_id 
  108. * @return array 
  109. */ 
  110. function get_attachment_files( $attachment_id ) { 
  111. $files = array(); 
  112.  
  113. /** 
  114. * Get attached file 
  115. */ 
  116. $attached_file = get_post_meta( $attachment_id, '_wp_attached_file', true ); 
  117.  
  118. if ( $attached_file != '' ) { 
  119. $files = array_merge( $files, $this->get_files_for_upload( $attached_file ) ); 
  120.  
  121. /** 
  122. * Get backup sizes files 
  123. */ 
  124. $attachment_backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); 
  125.  
  126. if ( is_array( $attachment_backup_sizes ) ) { 
  127. $files = array_merge( $files, $this->_get_sizes_files( $attached_file, $attachment_backup_sizes ) ); 
  128.  
  129. /** 
  130. * Get files from metadata 
  131. */ 
  132. $attachment_metadata = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); 
  133.  
  134. if ( is_array( $attachment_metadata ) ) { 
  135. $files = array_merge( $files, $this->get_metadata_files( $attachment_metadata ) ); 
  136.  
  137. return $files; 
  138.  
  139. /** 
  140. * Uploads files to CDN 
  141. * @param array $files 
  142. * @param boolean $queue_failed 
  143. * @param array $results 
  144. * @return boolean 
  145. */ 
  146. function upload( $files, $queue_failed, &$results, $timeout_time = NULL ) { 
  147. $cdn = $this->get_cdn(); 
  148. $force_rewrite = $this->_config->get_boolean( 'cdn.force.rewrite' ); 
  149.  
  150. @set_time_limit( $this->_config->get_integer( 'timelimit.cdn_upload' ) ); 
  151.  
  152. $engine = $this->_config->get_string( 'cdn.engine' ); 
  153. $return = $cdn->upload( $files, $results, $force_rewrite, $timeout_time ); 
  154.  
  155. if ( !$return && $queue_failed ) { 
  156. foreach ( $results as $result ) { 
  157. if ( $result['result'] != W3TC_CDN_RESULT_OK ) { 
  158. $this->queue_add( $result['local_path'], $result['remote_path'], W3TC_CDN_COMMAND_UPLOAD, $result['error'] ); 
  159.  
  160. return $return; 
  161.  
  162. /** 
  163. * Deletes files frrom CDN 
  164. * @param array $files 
  165. * @param boolean $queue_failed 
  166. * @param array $results 
  167. * @return boolean 
  168. */ 
  169. function delete( $files, $queue_failed, &$results ) { 
  170. $cdn = $this->get_cdn(); 
  171.  
  172. @set_time_limit( $this->_config->get_integer( 'timelimit.cdn_delete' ) ); 
  173.  
  174. $return = $cdn->delete( $files, $results ); 
  175.  
  176. if ( !$return && $queue_failed ) { 
  177. foreach ( $results as $result ) { 
  178. if ( $result['result'] != W3TC_CDN_RESULT_OK ) { 
  179. $this->queue_add( $result['local_path'], $result['remote_path'], W3TC_CDN_COMMAND_DELETE, $result['error'] ); 
  180.  
  181. return $return; 
  182.  
  183. /** 
  184. * Purges files from CDN 
  185. * @param array $files consisting of array('local_path'=>'', 'remote_path'=>'') 
  186. * @param boolean $queue_failed 
  187. * @param array $results 
  188. * @return boolean 
  189. */ 
  190. function purge( $files, $queue_failed, &$results ) { 
  191. /** 
  192. * Purge varnish servers before mirror purging 
  193. */ 
  194. if ( Cdn_Util::is_engine_mirror( $this->_config->get_string( 'cdn.engine' ) ) && $this->_config->get_boolean( 'varnish.enabled' ) ) { 
  195. $varnish = Dispatcher::component( 'Varnish_Flush' ); 
  196.  
  197. foreach ( $files as $file ) { 
  198. $remote_path = $file['remote_path']; 
  199. $varnish->flush_url( network_site_url( $remote_path ) ); 
  200.  
  201. /** 
  202. * Purge CDN 
  203. */ 
  204. $cdn = $this->get_cdn(); 
  205.  
  206. @set_time_limit( $this->_config->get_integer( 'timelimit.cdn_purge' ) ); 
  207.  
  208. $return = $cdn->purge( $files, $results ); 
  209.  
  210. if ( !$return && $queue_failed ) { 
  211. foreach ( $results as $result ) { 
  212. if ( $result['result'] != W3TC_CDN_RESULT_OK ) { 
  213. $this->queue_add( $result['local_path'], $result['remote_path'], W3TC_CDN_COMMAND_PURGE, $result['error'] ); 
  214.  
  215. return $return; 
  216.  
  217. /** 
  218. * Purge CDN completely 
  219. * @param unknown $results 
  220. * @return mixed 
  221. */ 
  222. function purge_all( &$results ) { 
  223. /** 
  224. * Purge CDN 
  225. */ 
  226. $cdn = $this->get_cdn(); 
  227.  
  228. @set_time_limit( $this->_config->get_integer( 'timelimit.cdn_purge' ) ); 
  229.  
  230. $return = $cdn->purge_all( $results ); 
  231. return $return; 
  232.  
  233. /** 
  234. * Queues file upload. 
  235. * Links wp_cron call to do that by the end of request processing 
  236. * @param string $url 
  237. * @return void 
  238. */ 
  239. function queue_upload_url( $url ) { 
  240. $docroot_filename = Util_Environment::url_to_docroot_filename( $url ); 
  241. $filename = Util_Environment::document_root() . '/' . $docroot_filename; 
  242.  
  243. $a = parse_url( $url ); 
  244. $uri = $a['path']; 
  245.  
  246. $remote_file_name = $this->uri_to_cdn_uri( $uri ); 
  247. $this->queue_add( $filename, $remote_file_name,  
  248. W3TC_CDN_COMMAND_UPLOAD, 'Pending' ); 
  249.  
  250. /** 
  251. * Normalizes attachment file 
  252. * @param string $file 
  253. * @return string 
  254. */ 
  255. function normalize_attachment_file( $file ) { 
  256. $upload_info = Util_Http::upload_info(); 
  257. if ( $upload_info ) { 
  258. $file = ltrim( str_replace( $upload_info['basedir'], '', $file ), '/\\' ); 
  259. $matches = null; 
  260.  
  261. if ( preg_match( '~(\d{4}/\d{2}/)?[^/]+$~', $file, $matches ) ) { 
  262. $file = $matches[0]; 
  263.  
  264. return $file; 
  265.  
  266. /** 
  267. * Returns CDN object 
  268. */ 
  269. function get_cdn() { 
  270. static $cdn = array(); 
  271.  
  272. if ( !isset( $cdn[0] ) ) { 
  273. $engine = $this->_config->get_string( 'cdn.engine' ); 
  274. $compression = ( $this->_config->get_boolean( 'browsercache.enabled' ) && $this->_config->get_boolean( 'browsercache.html.compression' ) ); 
  275.  
  276. switch ( $engine ) { 
  277. case 'akamai': 
  278. $engine_config = array( 
  279. 'username' => $this->_config->get_string( 'cdn.akamai.username' ),  
  280. 'password' => $this->_config->get_string( 'cdn.akamai.password' ),  
  281. 'zone' => $this->_config->get_string( 'cdn.akamai.zone' ),  
  282. 'domain' => $this->_config->get_array( 'cdn.akamai.domain' ),  
  283. 'ssl' => $this->_config->get_string( 'cdn.akamai.ssl' ),  
  284. 'email_notification' => $this->_config->get_array( 'cdn.akamai.email_notification' ),  
  285. 'compression' => false 
  286. ); 
  287. break; 
  288.  
  289. case 'att': 
  290. $engine_config = array( 
  291. 'account' => $this->_config->get_string( 'cdn.att.account' ),  
  292. 'token' => $this->_config->get_string( 'cdn.att.token' ),  
  293. 'domain' => $this->_config->get_array( 'cdn.att.domain' ),  
  294. 'ssl' => $this->_config->get_string( 'cdn.att.ssl' ),  
  295. 'compression' => false 
  296. ); 
  297. break; 
  298.  
  299. case 'azure': 
  300. $engine_config = array( 
  301. 'user' => $this->_config->get_string( 'cdn.azure.user' ),  
  302. 'key' => $this->_config->get_string( 'cdn.azure.key' ),  
  303. 'container' => $this->_config->get_string( 'cdn.azure.container' ),  
  304. 'cname' => $this->_config->get_array( 'cdn.azure.cname' ),  
  305. 'ssl' => $this->_config->get_string( 'cdn.azure.ssl' ),  
  306. 'compression' => false 
  307. ); 
  308. break; 
  309.  
  310. case 'cf': 
  311. $engine_config = array( 
  312. 'key' => $this->_config->get_string( 'cdn.cf.key' ),  
  313. 'secret' => $this->_config->get_string( 'cdn.cf.secret' ),  
  314. 'bucket' => $this->_config->get_string( 'cdn.cf.bucket' ),  
  315. 'id' => $this->_config->get_string( 'cdn.cf.id' ),  
  316. 'cname' => $this->_config->get_array( 'cdn.cf.cname' ),  
  317. 'ssl' => $this->_config->get_string( 'cdn.cf.ssl' ),  
  318. 'compression' => $compression 
  319. ); 
  320. break; 
  321.  
  322. case 'cf2': 
  323. $engine_config = array( 
  324. 'key' => $this->_config->get_string( 'cdn.cf2.key' ),  
  325. 'secret' => $this->_config->get_string( 'cdn.cf2.secret' ),  
  326. 'id' => $this->_config->get_string( 'cdn.cf2.id' ),  
  327. 'cname' => $this->_config->get_array( 'cdn.cf2.cname' ),  
  328. 'ssl' => $this->_config->get_string( 'cdn.cf2.ssl' ),  
  329. 'compression' => false 
  330. ); 
  331. break; 
  332.  
  333. case 'cotendo': 
  334. $engine_config = array( 
  335. 'username' => $this->_config->get_string( 'cdn.cotendo.username' ),  
  336. 'password' => $this->_config->get_string( 'cdn.cotendo.password' ),  
  337. 'zones' => $this->_config->get_array( 'cdn.cotendo.zones' ),  
  338. 'domain' => $this->_config->get_array( 'cdn.cotendo.domain' ),  
  339. 'ssl' => $this->_config->get_string( 'cdn.cotendo.ssl' ),  
  340. 'compression' => false 
  341. ); 
  342. break; 
  343.  
  344. case 'edgecast': 
  345. $engine_config = array( 
  346. 'account' => $this->_config->get_string( 'cdn.edgecast.account' ),  
  347. 'token' => $this->_config->get_string( 'cdn.edgecast.token' ),  
  348. 'domain' => $this->_config->get_array( 'cdn.edgecast.domain' ),  
  349. 'ssl' => $this->_config->get_string( 'cdn.edgecast.ssl' ),  
  350. 'compression' => false 
  351. ); 
  352. break; 
  353.  
  354. case 'ftp': 
  355. $engine_config = array( 
  356. 'host' => $this->_config->get_string( 'cdn.ftp.host' ),  
  357. 'type' => $this->_config->get_string( 'cdn.ftp.type' ),  
  358. 'user' => $this->_config->get_string( 'cdn.ftp.user' ),  
  359. 'pass' => $this->_config->get_string( 'cdn.ftp.pass' ),  
  360. 'path' => $this->_config->get_string( 'cdn.ftp.path' ),  
  361. 'pasv' => $this->_config->get_boolean( 'cdn.ftp.pasv' ),  
  362. 'domain' => $this->_config->get_array( 'cdn.ftp.domain' ),  
  363. 'ssl' => $this->_config->get_string( 'cdn.ftp.ssl' ),  
  364. 'compression' => false,  
  365. 'docroot' => Util_Environment::document_root() 
  366. ); 
  367. break; 
  368.  
  369. case 'google_drive': 
  370. $state = Dispatcher::config_state(); 
  371.  
  372. $engine_config = array( 
  373. 'client_id' => 
  374. $this->_config->get_string( 'cdn.google_drive.client_id' ),  
  375. 'access_token' => 
  376. $state->get_string( 'cdn.google_drive.access_token' ),  
  377. 'refresh_token' => 
  378. $this->_config->get_string( 'cdn.google_drive.refresh_token' ),  
  379. 'root_url' => 
  380. $this->_config->get_string( 'cdn.google_drive.folder.url' ),  
  381. 'root_folder_id' => 
  382. $this->_config->get_string( 'cdn.google_drive.folder.id' ),  
  383. 'new_access_token_callback' => array( 
  384. $this,  
  385. 'on_google_drive_new_access_token' 
  386. ); 
  387. break; 
  388.  
  389. case 'highwinds': 
  390. $state = Dispatcher::config_state(); 
  391.  
  392. $engine_config = array( 
  393. 'domains' => 
  394. $this->_config->get_array( 'cdn.highwinds.host.domains' ),  
  395. 'ssl' => 
  396. $this->_config->get_string( 'cdn.highwinds.ssl' ),  
  397. 'api_token' => 
  398. $this->_config->get_string( 'cdn.highwinds.api_token' ),  
  399. 'account_hash' => 
  400. $this->_config->get_string( 'cdn.highwinds.account_hash' ),  
  401. 'host_hash_code' => 
  402. $this->_config->get_string( 'cdn.highwinds.host.hash_code' ) 
  403. ); 
  404. break; 
  405.  
  406. case 'maxcdn': 
  407. $engine_config = array( 
  408. 'authorization_key' => $this->_config->get_string( 'cdn.maxcdn.authorization_key' ),  
  409. 'zone_id' => $this->_config->get_integer( 'cdn.maxcdn.zone_id' ),  
  410. 'domain' => $this->_config->get_array( 'cdn.maxcdn.domain' ),  
  411. 'ssl' => $this->_config->get_string( 'cdn.maxcdn.ssl' ),  
  412. 'compression' => false 
  413. ); 
  414. break; 
  415.  
  416. case 'mirror': 
  417. $engine_config = array( 
  418. 'domain' => $this->_config->get_array( 'cdn.mirror.domain' ),  
  419. 'ssl' => $this->_config->get_string( 'cdn.mirror.ssl' ),  
  420. 'compression' => false 
  421. ); 
  422. break; 
  423.  
  424. case 'netdna': 
  425. $engine_config = array( 
  426. 'authorization_key' => $this->_config->get_string( 'cdn.netdna.authorization_key' ),  
  427. 'zone_id' => $this->_config->get_integer( 'cdn.netdna.zone_id' ),  
  428. 'domain' => $this->_config->get_array( 'cdn.netdna.domain' ),  
  429. 'ssl' => $this->_config->get_string( 'cdn.netdna.ssl' ),  
  430. 'compression' => false 
  431. ); 
  432. break; 
  433.  
  434. case 'rackspace_cdn': 
  435. $state = Dispatcher::config_state(); 
  436.  
  437. $engine_config = array( 
  438. 'user_name' => $this->_config->get_string( 'cdn.rackspace_cdn.user_name' ),  
  439. 'api_key' => $this->_config->get_string( 'cdn.rackspace_cdn.api_key' ),  
  440. 'region' => $this->_config->get_string( 'cdn.rackspace_cdn.region' ),  
  441. 'service_access_url' => $this->_config->get_string( 'cdn.rackspace_cdn.service.access_url' ),  
  442. 'service_id' => $this->_config->get_string( 'cdn.rackspace_cdn.service.id' ),  
  443. 'service_protocol' => $this->_config->get_string( 'cdn.rackspace_cdn.service.protocol' ),  
  444. 'domains' => $this->_config->get_array( 'cdn.rackspace_cdn.domains' ),  
  445. 'access_state' => 
  446. $state->get_string( 'cdn.rackspace_cdn.access_state' ),  
  447. 'new_access_state_callback' => array( 
  448. $this,  
  449. 'on_rackspace_cdn_new_access_state' 
  450.  
  451. ); 
  452. break; 
  453. case 'rscf': 
  454. $state = Dispatcher::config_state(); 
  455.  
  456. $engine_config = array( 
  457. 'user_name' => $this->_config->get_string( 'cdn.rscf.user' ),  
  458. 'api_key' => $this->_config->get_string( 'cdn.rscf.key' ),  
  459. 'region' => $this->_config->get_string( 'cdn.rscf.location' ),  
  460. 'container' => $this->_config->get_string( 'cdn.rscf.container' ),  
  461. 'cname' => $this->_config->get_array( 'cdn.rscf.cname' ),  
  462. 'ssl' => $this->_config->get_string( 'cdn.rscf.ssl' ),  
  463. 'compression' => false,  
  464. 'access_state' => 
  465. $state->get_string( 'cdn.rackspace_cf.access_state' ),  
  466. 'new_access_state_callback' => array( 
  467. $this,  
  468. 'on_rackspace_cf_new_access_state' 
  469.  
  470. ); 
  471. break; 
  472.  
  473. case 's3': 
  474. $engine_config = array( 
  475. 'key' => $this->_config->get_string( 'cdn.s3.key' ),  
  476. 'secret' => $this->_config->get_string( 'cdn.s3.secret' ),  
  477. 'bucket' => $this->_config->get_string( 'cdn.s3.bucket' ),  
  478. 'cname' => $this->_config->get_array( 'cdn.s3.cname' ),  
  479. 'ssl' => $this->_config->get_string( 'cdn.s3.ssl' ),  
  480. 'compression' => $compression 
  481. ); 
  482. break; 
  483.  
  484. case 's3_compatible': 
  485. $engine_config = array( 
  486. 'key' => $this->_config->get_string( 'cdn.s3.key' ),  
  487. 'secret' => $this->_config->get_string( 'cdn.s3.secret' ),  
  488. 'bucket' => $this->_config->get_string( 'cdn.s3.bucket' ),  
  489. 'cname' => $this->_config->get_array( 'cdn.s3.cname' ),  
  490. 'ssl' => $this->_config->get_string( 'cdn.s3.ssl' ),  
  491. 'compression' => $compression,  
  492. 'api_host' => $this->_config->get_string( 'cdn.s3_compatible.api_host' ) 
  493. ); 
  494. break; 
  495.  
  496. $engine_config = array_merge( $engine_config, array( 
  497. 'debug' => $this->_config->get_boolean( 'cdn.debug' ) 
  498. ) ); 
  499.  
  500. $cdn[0] = CdnEngine::instance( $engine, $engine_config ); 
  501.  
  502. /** 
  503. * Set cache config for CDN 
  504. */ 
  505. if ( $this->_config->get_boolean( 'browsercache.enabled' ) ) { 
  506. $w3_plugin_browsercache = Dispatcher::component( 'BrowserCache_Plugin' ); 
  507. $cdn[0]->cache_config = $w3_plugin_browsercache->get_cache_config(); 
  508.  
  509. return $cdn[0]; 
  510.  
  511. /** 
  512. * Called when new access token is issued by cdnengine 
  513. */ 
  514. public function on_google_drive_new_access_token( $access_token ) { 
  515. $state = Dispatcher::config_state(); 
  516. $state->set( 'cdn.google_drive.access_token', $access_token ); 
  517. $state->save(); 
  518.  
  519. /** 
  520. * Called when new access state is issued by cdnengine 
  521. */ 
  522. public function on_rackspace_cdn_new_access_state( $access_state ) { 
  523. $state = Dispatcher::config_state(); 
  524. $state->set( 'cdn.rackspace_cdn.access_state', $access_state ); 
  525. $state->save(); 
  526.  
  527. /** 
  528. * Called when new access state is issued by cdnengine 
  529. */ 
  530. public function on_rackspace_cf_new_access_state( $access_state ) { 
  531. $state = Dispatcher::config_state(); 
  532. $state->set( 'cdn.rackspace_cf.access_state', $access_state ); 
  533. $state->save(); 
  534.  
  535. /** 
  536. * Convert relative file which is relative to ABSPATH (wp folder on disc) to path uri 
  537. * @param unknown $file 
  538. * @return string 
  539. */ 
  540. function docroot_filename_to_uri( $file ) { 
  541. $file = ltrim( $file, '/' ); 
  542. // Translate multisite subsite uploads paths 
  543. $file = str_replace( basename( WP_CONTENT_DIR ) . '/blogs.dir/' . 
  544. Util_Environment::blog_id() . '/', '', $file ); 
  545. return $file; 
  546.  
  547.  
  548. /** 
  549. * Convert a relative path (relative to ABSPATH (wp folder on disc) into a absolute path 
  550. * @param unknown $file 
  551. * @return string 
  552. */ 
  553. function docroot_filename_to_absolute_path( $file ) { 
  554. if ( is_file( $file ) ) 
  555. return $file; 
  556.  
  557. if ( DIRECTORY_SEPARATOR != '/' ) 
  558. $file = str_replace( '/', DIRECTORY_SEPARATOR, $file ); 
  559.  
  560. return rtrim( Util_Environment::document_root(), '/\\' ) . 
  561. DIRECTORY_SEPARATOR . ltrim( $file, '/\\' ); 
  562.  
  563. /** 
  564. * Convert local uri path to CDN type specific path 
  565. * @param unknown $local_uri_path 
  566. * @return string 
  567. */ 
  568. function uri_to_cdn_uri( $local_uri ) { 
  569. $local_uri = ltrim( $local_uri, '/' ); 
  570. $remote_uri = $local_uri; 
  571.  
  572. if ( Util_Environment::is_wpmu() && defined( 'DOMAIN_MAPPING' ) && DOMAIN_MAPPING ) 
  573. $remote_uri = str_replace( site_url(), '', $local_uri ); 
  574.  
  575. $engine = $this->_config->get_string( 'cdn.engine' ); 
  576.  
  577. if ( Cdn_Util::is_engine_mirror( $engine ) ) { 
  578. if ( Util_Environment::is_wpmu() && strpos( $local_uri, 'files' ) === 0 ) { 
  579. $upload_dir = Util_Environment::wp_upload_dir(); 
  580. $remote_uri = $this->abspath_to_relative_path( 
  581. dirname( $upload_dir['basedir'] ) ) . '/' . $local_uri; 
  582. elseif ( Util_Environment::is_wpmu() && 
  583. !Util_Environment::is_wpmu_subdomain() && 
  584. Util_Environment::is_using_master_config() && 
  585. Cdn_Util::is_engine_push( $engine ) ) { 
  586. // in common config files are uploaded for network home url 
  587. // so mirror will not contain /subblog/ path in uri 
  588. $home = trim( home_url( '', 'relative' ), '/' ) . '/'; 
  589. $network_home = trim( network_home_url( '', 'relative' ), '/' ) . '/'; 
  590.  
  591. if ( $home != $network_home && 
  592. substr( $local_uri, 0, strlen( $home ) ) == $home ) { 
  593. $remote_uri = $network_home . substr( $local_uri, strlen( $home ) ); 
  594.  
  595. return ltrim( $remote_uri, '/' ); 
  596.  
  597. /** 
  598. * Returns the sitepath for multisite subfolder or subdomain path for multisite subdomain 
  599. * @return string 
  600. */ 
  601. private function _get_multisite_url_identifier() { 
  602. if ( defined( 'DOMAIN_MAPPING' ) && DOMAIN_MAPPING ) { 
  603. $parsedUrl = parse_url( site_url() ); 
  604. return $parsedUrl['host']; 
  605. } elseif ( Util_Environment::is_wpmu_subdomain() ) { 
  606. $parsedUrl = parse_url( Util_Environment::home_domain_root_url() ); 
  607. $urlparts = explode( '.', $parsedUrl['host'] ); 
  608.  
  609. if ( sizeof( $urlparts ) > 2 ) { 
  610. $subdomain = array_shift( $urlparts ); 
  611. return trim( $subdomain, '/' ); 
  612. return trim( Util_Environment::site_url_uri(), '/' ); 
  613.  
  614. /** 
  615. * Taks an absolute path and converts to a relative path to root 
  616. * @param unknown $path 
  617. * @return mixed 
  618. */ 
  619. function abspath_to_relative_path( $path ) { 
  620. return str_replace( Util_Environment::document_root(), '', $path ); 
  621.  
  622. /** 
  623. * Takes a root relative path and converts to a full uri 
  624. * @param unknown $path 
  625. * @return string 
  626. */ 
  627. function relative_path_to_url( $path ) { 
  628. $cdnuri = $this->docroot_filename_to_uri( ltrim( $path, "/" ) ); 
  629. return rtrim( Util_Environment::home_domain_root_url(), "/" ) . '/' . $cdnuri; 
  630.  
  631. /** 
  632. * Constructs a CDN file descriptor 
  633. * @param unknown $local_path 
  634. * @param unknown $remote_path 
  635. * @return array 
  636. */ 
  637. function build_file_descriptor( $local_path, $remote_path ) { 
  638. $file = array( 'local_path' => $local_path,  
  639. 'remote_path' => $remote_path,  
  640. 'original_url' => $this->relative_path_to_url( $local_path ) ); 
  641.  
  642. $file = apply_filters( 'w3tc_build_cdn_file_array', $file ); 
  643. return $file;