PageRenderTime 24ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-super-cache/trunk/ossdl-cdn.php

https://github.com/brandonburke/WordPress-Plugin-Baseline
PHP | 200 lines | 154 code | 16 blank | 30 comment | 46 complexity | 05b9e6c48531a6814570e7caaae36b1f MD5 | raw file
  1. <?php
  2. /* Taken from OSSDL CDN off-linker, a plugin by W-Mark Kubacki (http://mark.ossdl.de/) and used with permission */
  3. /* Set up some defaults */
  4. if ( get_option( 'ossdl_off_cdn_url' ) == false )
  5. add_option('ossdl_off_cdn_url', get_option('siteurl'));
  6. $ossdl_off_blog_url = get_option('siteurl');
  7. $ossdl_off_cdn_url = trim( get_option('ossdl_off_cdn_url') );
  8. if ( get_option( 'ossdl_off_include_dirs' ) == false )
  9. add_option('ossdl_off_include_dirs', 'wp-content,wp-includes');
  10. $ossdl_off_include_dirs = trim(get_option('ossdl_off_include_dirs'));
  11. if ( get_option( 'ossdl_off_exclude' ) == false )
  12. add_option('ossdl_off_exclude', '.php');
  13. $ossdl_off_exclude = trim(get_option('ossdl_off_exclude'));
  14. $arr_of_excludes = array_map('trim', explode(',', $ossdl_off_exclude));
  15. if ( get_option( 'ossdl_cname' ) == false )
  16. add_option('ossdl_cname', '');
  17. $ossdl_cname = trim(get_option('ossdl_cname'));
  18. $ossdl_https = trim(get_option('ossdl_https'));
  19. $arr_of_cnames = array_map('trim', explode(',', $ossdl_cname));
  20. if ($arr_of_cnames[0] == '') $arr_of_cnames = array();
  21. /**
  22. * Determines whether to exclude a match.
  23. *
  24. * @param String $match URI to examine
  25. * @param Array $excludes array of "badwords"
  26. * @return Boolean true if to exclude given match from rewriting
  27. */
  28. function scossdl_off_exclude_match($match, $excludes) {
  29. foreach ($excludes as $badword) {
  30. if (stristr($match, $badword) != false) {
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. /**
  37. * Compute string modulo, based on SHA1 hash
  38. */
  39. function scossdl_string_mod($s, $mod) {
  40. /* The full SHA1 is too large for PHP integer types. This should be
  41. * enough for our purpose */
  42. $n = hexdec(substr(sha1($s), 0, 5));
  43. return $n % $mod;
  44. }
  45. /**
  46. * Rewriter of URLs, used as replace-callback.
  47. *
  48. * Called by #scossdl_off_filter.
  49. */
  50. function scossdl_off_rewriter($match) {
  51. global $ossdl_off_blog_url, $ossdl_off_cdn_url, $arr_of_excludes, $arr_of_cnames, $ossdl_https;
  52. if ( $ossdl_off_cdn_url == '' )
  53. return $match[0];
  54. if ( $ossdl_https && substr( $match[0], 0, 5 ) == 'https' )
  55. return $match[0];
  56. if ( false == in_array( $ossdl_off_cdn_url, $arr_of_cnames ) )
  57. $arr_of_cnames[] = $ossdl_off_cdn_url;
  58. if (scossdl_off_exclude_match($match[0], $arr_of_excludes)) {
  59. return $match[0];
  60. } else {
  61. $include_dirs = scossdl_off_additional_directories();
  62. if ( preg_match( '/' . $include_dirs . '/', $match[0] ) ) {
  63. $offset = scossdl_string_mod($match[1], count($arr_of_cnames));
  64. return str_replace($ossdl_off_blog_url, $arr_of_cnames[$offset], $match[0]);
  65. } else {
  66. return $match[0];
  67. }
  68. }
  69. }
  70. /**
  71. * Creates a regexp compatible pattern from the directories to be included in matching.
  72. *
  73. * @return String with the pattern with {@literal |} as prefix, or empty
  74. */
  75. function scossdl_off_additional_directories() {
  76. global $ossdl_off_include_dirs;
  77. $input = explode(',', $ossdl_off_include_dirs);
  78. if ($ossdl_off_include_dirs == '' || count($input) < 1) {
  79. return 'wp\-content|wp\-includes';
  80. } else {
  81. return implode('|', array_map('quotemeta', array_map('trim', $input)));
  82. }
  83. }
  84. /**
  85. * Output filter which runs the actual plugin logic.
  86. */
  87. function scossdl_off_filter($content) {
  88. global $ossdl_off_blog_url, $ossdl_off_cdn_url;
  89. if ($ossdl_off_blog_url == $ossdl_off_cdn_url) { // no rewrite needed
  90. return $content;
  91. } else {
  92. $dirs = scossdl_off_additional_directories();
  93. $regex = '#(?<=[(\"\'])'.quotemeta($ossdl_off_blog_url).'/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#';
  94. return preg_replace_callback($regex, 'scossdl_off_rewriter', $content);
  95. }
  96. }
  97. /**
  98. * Registers scossdl_off_filter as output buffer, if needed.
  99. */
  100. function do_scossdl_off_ob_start() {
  101. global $ossdl_off_blog_url, $ossdl_off_cdn_url;
  102. if ($ossdl_off_blog_url != $ossdl_off_cdn_url) {
  103. add_filter( 'wp_cache_ob_callback_filter', 'scossdl_off_filter' );
  104. }
  105. }
  106. if ( false == isset( $ossdlcdn ) )
  107. $ossdlcdn = 1; // have to default to on for existing users.
  108. if ( $ossdlcdn == 1 )
  109. add_action('init', 'do_scossdl_off_ob_start');
  110. function scossdl_off_options() {
  111. global $ossdlcdn, $wp_cache_config_file;
  112. $valid_nonce = isset($_REQUEST['_wpnonce']) ? wp_verify_nonce($_REQUEST['_wpnonce'], 'wp-cache') : false;
  113. if ( $valid_nonce && isset($_POST['action']) && ( $_POST['action'] == 'update_ossdl_off' )){
  114. update_option('ossdl_off_cdn_url', $_POST['ossdl_off_cdn_url']);
  115. update_option('ossdl_off_include_dirs', $_POST['ossdl_off_include_dirs'] == '' ? 'wp-content,wp-includes' : $_POST['ossdl_off_include_dirs']);
  116. update_option('ossdl_off_exclude', $_POST['ossdl_off_exclude']);
  117. update_option('ossdl_cname', $_POST['ossdl_cname']);
  118. update_option('ossdl_https', (int)$_POST['ossdl_https']);
  119. if ( isset( $_POST[ 'ossdlcdn' ] ) ) {
  120. $ossdlcdn = 1;
  121. } else {
  122. $ossdlcdn = 0;
  123. }
  124. wp_cache_replace_line('^ *\$ossdlcdn', "\$ossdlcdn = $ossdlcdn;", $wp_cache_config_file);
  125. }
  126. $example_cdn_uri = str_replace( 'http://', 'http://cdn.', str_replace( 'www.', '', get_option( 'siteurl' ) ) );
  127. $example_cnames = str_replace( 'http://cdn.', 'http://cdn1.', $example_cdn_uri );
  128. $example_cnames .= ',' . str_replace( 'http://cdn.', 'http://cdn2.', $example_cdn_uri );
  129. $example_cnames .= ',' . str_replace( 'http://cdn.', 'http://cdn3.', $example_cdn_uri );
  130. $example_cdn_uri = get_option('ossdl_off_cdn_url') == get_option('siteurl') ? $example_cdn_uri : get_option('ossdl_off_cdn_url');
  131. $example_cdn_uri .= '/wp-includes/js/prototype.js';
  132. ?>
  133. <p><?php _e( 'Your website probably uses lots of static files. Image, Javascript and CSS files are usually static files that could just as easily be served from another site or CDN. Therefore this plugin replaces any links in the <code>wp-content</code> and <code>wp-includes</code> directories (except for PHP files) on your site with the URL you provide below. That way you can either copy all the static content to a dedicated host or mirror the files to a CDN by <a href="http://knowledgelayer.softlayer.com/questions/365/How+does+Origin+Pull+work%3F" target="_blank">origin pull</a>.', 'wp-super-cache' ); ?></p>
  134. <p><?php printf( __( 'The <a href="%1$s">CDN Sync Tool</a> plugin will help upload files to Amazon S3/Cloudfront if you would rather not depend on origin pull. See the <a href="%2$s">plugin support forum</a> if you have any queries about this plugin.', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/cdn-sync-tool/', 'http://wordpress.org/tags/cdn-sync-tool?forum_id=10' ); ?></p>
  135. <p><?php printf( __( '<strong style="color: red">WARNING:</strong> Test some static urls e.g., %s to ensure your CDN service is fully working before saving changes.', 'wp-super-cache' ), '<code>' . $example_cdn_uri . '</code>' ); ?></p>
  136. <p><?php _e( 'You can define different CDN URLs for each site on a multsite network.', 'wp-super-cache' ); ?></p>
  137. <p><form method="post" action="">
  138. <?php wp_nonce_field('wp-cache'); ?>
  139. <table class="form-table"><tbod>
  140. <tr valign="top">
  141. <td style='text-align: right'>
  142. <input id='ossdlcdn' type="checkbox" name="ossdlcdn" value="1" <?php if ( $ossdlcdn ) echo "checked=1"; ?> />
  143. </td>
  144. <th scope="row"><label for="ossdlcdn"><?php _e( 'Enable CDN Support', 'wp-super-cache' ); ?></label></th>
  145. </tr>
  146. <tr valign="top">
  147. <th scope="row"><label for="ossdl_off_cdn_url"><?php _e( 'Off-site URL', 'wp-super-cache' ); ?></label></th>
  148. <td>
  149. <input type="text" name="ossdl_off_cdn_url" value="<?php echo(get_option('ossdl_off_cdn_url')); ?>" size="64" class="regular-text code" /><br />
  150. <span class="description"><?php printf( __( 'The new URL to be used in place of %1$s for rewriting. No trailing <code>/</code> please.<br />Example: <code>%2$s</code>.', 'wp-super-cache' ), get_option( 'siteurl' ), $example_cdn_uri ); ?></span>
  151. </td>
  152. </tr>
  153. <tr valign="top">
  154. <th scope="row"><label for="ossdl_off_include_dirs"><?php _e( 'Include directories', 'wp-super-cache' ); ?></label></th>
  155. <td>
  156. <input type="text" name="ossdl_off_include_dirs" value="<?php echo esc_attr( get_option( 'ossdl_off_include_dirs' ) ); ?>" size="64" class="regular-text code" /><br />
  157. <span class="description"><?php _e( 'Directories to include in static file matching. Use a comma as the delimiter. Default is <code>wp-content, wp-includes</code>, which will be enforced if this field is left empty.', 'wp-super-cache' ); ?></span>
  158. </td>
  159. </tr>
  160. <tr valign="top">
  161. <th scope="row"><label for="ossdl_off_exclude"><?php _e( 'Exclude if substring', 'wp-super-cache' ); ?></label></th>
  162. <td>
  163. <input type="text" name="ossdl_off_exclude" value="<?php echo esc_attr( get_option( 'ossdl_off_exclude' ) ); ?>" size="64" class="regular-text code" /><br />
  164. <span class="description"><?php _e( 'Excludes something from being rewritten if one of the above strings is found in the match. Use a comma as the delimiter like this, <code>.php, .flv, .do</code>, and always include <code>.php</code> (default).', 'wp-super-cache' ); ?></span>
  165. </td>
  166. </tr>
  167. <tr valign="top">
  168. <th scope="row"><label for="ossdl_cname"><?php _e( 'Additional CNAMES', 'wp-super-cache' ); ?></label></th>
  169. <td>
  170. <input type="text" name="ossdl_cname" value="<?php echo esc_attr( get_option( 'ossdl_cname' ) ); ?>" size="64" class="regular-text code" /><br />
  171. <span class="description"><?php printf( __( 'These <a href="http://en.wikipedia.org/wiki/CNAME_record">CNAMES</a> will be used in place of %1$s for rewriting (in addition to the off-site URL above). Use a comma as the delimiter. For pages with a large number of static files, this can improve browser performance. CNAMEs may also need to be configured on your CDN.<br />Example: %2$s', 'wp-super-cache' ), get_option( 'siteurl' ), $example_cnames ); ?></span>
  172. </td>
  173. </tr>
  174. <tr valign="top">
  175. <th scope="row" colspan='2'><label><input type='checkbox' name='ossdl_https' value='1' <?php if ( get_option( 'ossdl_https' ) ) { echo 'checked'; } ?> /> <?php _e( 'Skip https URLs to avoid "mixed content" errors', 'wp-super-cache' ); ?></label></th>
  176. </tr>
  177. </tbody></table>
  178. <input type="hidden" name="action" value="update_ossdl_off" />
  179. <p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /></p>
  180. </form></p>
  181. <p><?php _e( 'CDN functionality provided by <a href="http://wordpress.org/extend/plugins/ossdl-cdn-off-linker/">OSSDL CDN Off Linker</a> by <a href="http://mark.ossdl.de/">Mark Kubacki</a>', 'wp-super-cache' ); ?></p>
  182. <?php
  183. }
  184. ?>