This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
class-wpcdnrewrite.php
554 lines (470 loc) · 15 KB
/
class-wpcdnrewrite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<?php
/**
* External/shared functions
*/
require_once( 'functions.php' );
class WP_CDN_Rewrite {
/**
* The name of the plugin
*/
const NAME = 'CDN Rewrite';
/**
* The slug to use for plugin URLs
*/
const SLUG = 'wpcdnrewrite';
/**
* Required user capability
*/
const REQUIRED_CAPABILITY = 'manage_options';
/**
* Version of the plugin
*/
const VERSION = '1.1';
/**
* wp_options key for the plugin version
*/
const VERSION_KEY = 'wpcdnrewrite-version';
/**
* wp_options key for rules
*/
const RULES_KEY = 'wpcdnrewrite-rules';
/**
* wp_options key for domains to rewrite URLs for
*/
const WHITELIST_KEY = 'wpcdnrewrite-whitelist';
/**
* Constant to signify that a rule is only for the host portion of the url
*/
const REWRITE_TYPE_HOST_ONLY = 1;
/**
* Constant to signify that a rule is for the full URL up to the file
*/
const REWRITE_TYPE_FULL_URL = 2;
/**
* Used to determine if the content-type is xml
*/
const CONTENT_TYPE_XML = 'xml';
/**
* Used to determine if the content-type is html
*/
const CONTENT_TYPE_HTML = 'html';
/**
* Stores the type of content we're rewriting
*
* @var String
*/
protected $content_type;
/**
* Creates a new WP_CDN_Rewrite object
*
* @package WP CDN Rewrite
* @since 1.0
*
* @return object A new WP_CDN_Rewrite object
*/
public function __construct() {
// Only register the admin call backs if we're in the admin app
if ( is_admin() ) {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'include_admin_javascript' ) );
}
register_uninstall_hook( __FILE__, array( 'WP_CDN_Rewrite', 'uninstall' ) );
// Add filters to run our rewrite code on
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
add_filter( 'muplugins_loaded', array( $this, 'startup' ), 5 );
} else {
add_filter( 'plugins_loaded', array( $this, 'startup' ), 5 );
}
add_filter( 'shutdown', array( $this, 'shutdown' ), 20 );
}
/**
* Filter to start buffering at the start of WordPress' work
*
* @package WP CDN Rewrite
* @since 1.0
*
* @return void
*/
public function startup() {
if ( ! is_admin() ) {
$ret = ob_start( 'wpcdn_rewrite_content' );
}
}
/**
* Filter to end buffering/flush any remaining buffer at the end of WordPress' work
*
* @package WP CDN Rewrite
* @since 1.0
*
* @return void
*/
public function shutdown() {
if ( ! is_admin() ) {
@ob_end_flush();
}
}
/**
* The admin_init hook runs as soon as the admin initializes and we use it
* to add our settings to the whitelist of allowed options
*
* @package WP CDN Rewrite
* @since 1.0
*
* @return void
*/
public function admin_init() {
register_setting( 'wpcdnrewrite', self::RULES_KEY, array( $this, 'sanitize_rules' ) );
register_setting( 'wpcdnrewrite', self::WHITELIST_KEY, array( $this, 'sanitize_whitelist' ) );
}
/**
* Adds a link to our settings page under the Settings menu
*
* @package WP CDN Rewrite
* @since 1.0
*
* @return void
*/
public function admin_menu() {
add_options_page( self::NAME, self::NAME, self::REQUIRED_CAPABILITY, self::SLUG, array( $this, 'show_config' ) );
}
/**
* adds the necessary wordpress options for the plugin to use later. Only runs on activation
*
* @return void
*/
public function activate() {
$host = parse_url( network_site_url(), PHP_URL_HOST );
// add_option only runs if the option doesn't exist
add_option( self::VERSION_KEY, self::VERSION );
add_option( self::RULES_KEY, array() );
add_option( self::WHITELIST_KEY, array( $host ) );
}
/**
* @param String $content
*/
public function get_content_type($content) {
if(preg_match("/^<\?xml/", $content)) {
return static::CONTENT_TYPE_XML;
}
return static::CONTENT_TYPE_HTML;
}
/**
* Adds admin.js to the <head>
*
* @package WP CDN Rewrite
* @since 1.0
*
* @return void
*/
public function include_admin_javascript() {
wp_enqueue_script( 'admin.js', plugins_url( 'html/admin.js', __FILE__ ), array( 'jquery' ) );
}
/**
* Shows the configuration page within the settings
*
* @package WP CDN Rewrite
* @since 1.0
*
* @return void
*/
public function show_config() {
if ( ! current_user_can( self::REQUIRED_CAPABILITY ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
require_once( 'html/config.php' );
}
/**
* Loads the content into a DomDocument object
*
* @param String $content
* @return DOMDocument
*/
public function load_content($content) {
$dom = new DOMDocument();
if(static::CONTENT_TYPE_XML == $this->content_type) {
$dom->loadXML($content);
} else {
$dom->loadHTML($content);
}
return $dom;
}
/**
* Make sure we save our content in the right format from DOMDocument
*
* @param DOMDocument $domDocument
* @return string
*/
public function save_content(DOMDocument $domDocument) {
if(static::CONTENT_TYPE_XML == $this->content_type) {
return $domDocument->saveXML();
}
return $domDocument->saveHTML();
}
/**
* Rewrites the specified content per specified rules
*
* @package WP CDN Rewrite
* @since 1.0
*
* @param string $content The text to rewrite
* @return string The new content with appropriate URLs rewritten
*/
public function rewrite_content( $content ) {
// Grab the version number we're working with
$version = get_option( self::VERSION_KEY );
$this->content_type = $this->get_content_type($content);
if ($version <= '1.2') {
// Pull the rules and whitelist arrays from the database
$rules = get_option( self::RULES_KEY );
$whitelist = get_option( self::WHITELIST_KEY );
// Get a DOM object for this content that we can manipulate
$dom = $this->load_content($content);
$dom->formatOutput = true;
// Rewrite URLs
$this->do_rewrite( $dom, $rules, $whitelist, 'a', 'href' );
$this->do_rewrite( $dom, $rules, $whitelist, 'img', 'src' );
$this->do_rewrite( $dom, $rules, $whitelist, 'script', 'src' );
$this->do_rewrite( $dom, $rules, $whitelist, 'link', 'href' );
// Grab the modified HTML
$newContent = $this->save_content($dom);
return $newContent;
}
return $content;
}
/**
* Deletes all of the stuff we put into the database so that we don't leave anything behind to corrupt future installs
*
* @package WP CDN Rewrite
* @since 1.0
*
* @return void
*/
public static function uninstall() {
delete_option( self::VERSION_KEY );
delete_option( self::RULES_KEY );
delete_option( self::WHITELIST_KEY );
}
/**
* Does the actual URL rewriting for a given DOMDocument object
*
* @package WP CDN Rewrite
* @since 1.0
*
* @param DOMDocument $dom The DOM to rewrite URLs in
* @param array $rules Rewrite rules
* @param array $whiteList Array of server names to rewrite links for
* @param string $tag The tag type to rewrite for
* @param string $attribute The attribute to rewrite for on the specified tag
* @return void
*/
protected function do_rewrite( $dom, $rules = array(), $whitelist = array(), $tag = 'a', $attribute = 'href' ) {
// Make sure we got a valid DOM
if ( NULL == $dom ) {
wp_die( "Invalid DOM passed to WP CDN Rewrite's do_rewrite()" );
}
// Go through all of the tags of the type specified…
$tags = $dom->getElementsByTagName( $tag );
if ( ! is_null( $tags ) ) {
foreach ( $tags as $tag ) {
// …and look for ones that have the requested attribute
if ( $tag->hasAttribute( $attribute ) ) {
$url = $tag->getAttribute( $attribute );
if ( $this->starts_with( $url, '/' ) ) {
$base = network_site_url();
$url = $base . $url;
}
$parsed = parse_url( $url );
if ( FALSE !== $parsed ) {
$host = $parsed['host'];
if ( in_array( $host, $whitelist ) ) {
// The target is on a whitelisted domain, so
// we want to rewrite the url
$matchedRule = NULL;
foreach ( $rules as $rule ) {
$path = $parsed['path'];
if ( $this->ends_with( $path, $rule['match'] ) ) {
// Found a rule to rewrite for
$matchedRule = $rule;
break;
}
}
if ( NULL != $matchedRule )
$tag->setAttribute( $attribute, $this->rewrite_url( $url, $matchedRule ) );
}
}
}
}
}
}
/**
* Rewrites one URL per the specified rule
*
* @package WP CDN Rewrite
* @since 1.0
*
* @param string $url The URL
* @param array $rule Rewrite rule
* @return string The rewritten URL
*/
protected function rewrite_url( $url, $rule ) {
$ret = $url;
if ( self::REWRITE_TYPE_HOST_ONLY == $rule['type'] ) {
$host = parse_url( $ret, PHP_URL_HOST );
// Set the scheme and host if we have an absolute path
if ( FALSE === $host ) {
$host = network_site_url();
}
// Find the stuff to the left and right of the host
$oldHostLen = strlen( $host );
$leftLen = strpos( $ret, $host );
$rightLen = strlen( $ret ) - ( $leftLen + $oldHostLen );
$left = substr( $ret, 0, $leftLen );
$right = substr( $ret, $leftLen + $oldHostLen );
// Build a new URL with our replacement host
$ret = $left . $rule['rule'] . $right;
}
else if ( self::REWRITE_TYPE_FULL_URL == $rule['type'] ) {
$filename = pathinfo( parse_url( $ret, PHP_URL_PATH ), PATHINFO_BASENAME );
$ret = $rule['rule'];
// Make sure we have a / on the end
if ( ! $this->ends_with( $ret, '/' ) ) {
$ret = $ret . '/';
}
$ret = $ret . $filename;
// Add in the scheme and host for an absolute path
if ( $this->starts_with( $ret, '/' ) ) {
$base = network_site_url();
if ( ! $this->ends_with( $base, '/' ) ) {
$base = $base . '/';
}
$ret = $base . $ret;
}
}
return $ret;
}
/**
* Sanitize the array of rules
*
* @package WP CDN Rewrite
* @since 1.0
*
* @param array $ruleArray Array of rules
* @return array Array of sanitized rules
*/
public function sanitize_rules( array $ruleArray ) {
$allowedTypes = array(
self::REWRITE_TYPE_FULL_URL,
self::REWRITE_TYPE_HOST_ONLY,
);
foreach ( $ruleArray as $key => $rule ) {
if ( ! in_array( $rule['type'], $allowedTypes ) ) {
unset( $ruleArray[$key] );
add_settings_error( self::RULES_KEY, self::RULES_KEY, 'Invalid rule type entered' );
continue;
}
$rule['match'] = preg_replace( '/\W/', '', $rule['match'] );
if ( trim( $rule['match'] ) == '' ) {
unset( $ruleArray[$key] );
continue;
}
$validRule = true;
if ( $rule['type'] == self::REWRITE_TYPE_FULL_URL ) {
$rule['rule'] = filter_var( $rule['rule'], FILTER_SANITIZE_URL );
$validRule = filter_var( $rule['rule'], FILTER_VALIDATE_URL );
} elseif ( $rule['type'] == self::REWRITE_TYPE_HOST_ONLY ) {
$rule['rule'] = preg_replace( '/[http|https]:\/\//', '', $rule['rule'] );
$validRule = self::validate_domain_name( $rule['rule'] );
}
if ( ! $validRule ) {
unset( $ruleArray[$key] );
add_settings_error( self::RULES_KEY, self::RULES_KEY, 'Invalid rewrite URL entered' );
} else {
$ruleArray[$key] = $rule;
}
}
// Make sure all the indexes are contiguous
$ruleArray = array_values( $ruleArray );
return $ruleArray;
}
/**
* Sanitize the array of domains
*
* @package WP CDN Rewrite
* @since 1.0
*
* @param array $valueArray Array of whitelist rules
* @return array Array of sanitized whitelist rules
*/
public function sanitize_whitelist( array $valueArray ) {
foreach ( $valueArray as $key => $value ) {
$value = trim( $value );
if ( $value == '' ) {
unset( $valueArray[$key] );
} else {
// Strip http, https, and ://
$value = preg_replace( '/[http|https]:\/\//', '', $value );
$validDomain = self::validate_domain_name( $value );
if ( false == $validDomain ) {
add_settings_error( self::WHITELIST_KEY, self::WHITELIST_KEY, 'Invalid domain name "{$value}" entered' );
} else {
$valueArray[$key] = $value;
}
}
}
// Make sure all the indexes are contiguous
$valueArray = array_values( $valueArray );
return $valueArray;
}
/**
* Tests whether a text starts with the given string or not
*
* @package WP CDN Rewrite
* @since 1.0
*
* @param string $haystack The text to search
* @param string $needle The string to search for
* @return bool True if the text starts with a given string, else false
*/
protected function starts_with( $haystack, $needle ) {
$needleLen = strlen( $needle );
return substr( $haystack, 0, $needleLen ) === $needle;
}
/**
* Tests whether a text ends with the given string or not
*
* @package WP CDN Rewrite
* @since 1.0
* @source http://www.jonasjohn.de/snippets/php/ends-with.htm
*
* @param int $count The rule number
* @param string $haystack The text to search
* @param string $needle The string to look for
* @return bool True if the text ends with a given string, else false
*/
protected function ends_with( $haystack, $needle ){
return strrpos( $haystack, $needle ) === strlen( $haystack ) - strlen( $needle );
}
/**
* Used to check and see if the domains that are posted are valid
*
* @package WP CDN Rewrite
* @since 1.0
*
* @param string $domainName The domain name to validate
* @return bool True if the domain name is valid, else false
*/
protected function validate_domain_name( $domainName ) {
$pieces = explode( '.', $domainName );
foreach ( $pieces as $piece ) {
if ( ! preg_match( '/^[a-z\d][a-z\d-]{0,62}$/i', $piece ) || preg_match( '/-$/', $piece ) ) {
return false;
}
}
return true;
}
}
//this is technically the activation hook but WP 3.3.1 doesn't run those anymore apparently...
// so this is kind of a hack
add_action('wp_loaded', array('WP_CDN_Rewrite', 'activate'));
new WP_CDN_Rewrite();