PluginProbe
Page Optimize / trunk
Page Optimize vtrunk
trunk 0.4.2 0.4.3 0.4.4 0.4.6 0.4.7 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 0.5.8 0.6.0 0.6.1 0.6.2 0.6.3
page-optimize / dependency-path-mapping.php

dependency-path-mapping.php in Page Optimize trunk, at dependency-path-mapping.php

138 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This is a class to map script and style URLs to local filesystem paths.
5 * This is necessary when we are deciding what we can concatenate and when
6 * actually building the concatenation.
7 */
8 class Page_Optimize_Dependency_Path_Mapping {
9 // Save entire site URL so we can check whether other URLs are based on it (internal URLs)
10 public $site_url;
11
12 // Save URI path and dir for mapping URIs to filesystem paths
13 public $site_uri_path = null;
14 public $site_dir = null;
15 public $content_uri_path = null;
16 public $content_dir = null;
17 public $plugin_uri_path = null;
18 public $plugin_dir = null;
19
20 function __construct(
21 // Expose URLs and DIRs for unit test
22 $site_url = null, // default site URL is determined dynamically
23 $site_dir = PAGE_OPTIMIZE_ABSPATH,
24 $content_url = WP_CONTENT_URL,
25 $content_dir = WP_CONTENT_DIR,
26 $plugin_url = WP_PLUGIN_URL,
27 $plugin_dir = WP_PLUGIN_DIR
28 ) {
29 if ( null === $site_url ) {
30 $site_url = is_multisite() ? get_site_url( get_current_blog_id() ) : get_site_url();
31 }
32 $site_url = trailingslashit( $site_url );
33 $this->site_url = $site_url;
34 $this->site_uri_path = parse_url( $site_url, PHP_URL_PATH );
35 $this->site_dir = trailingslashit( $site_dir );
36
37 // Only resolve content URLs if they are under the site URL
38 if ( $this->is_internal_uri( $content_url ) ) {
39 $this->content_uri_path = parse_url( trailingslashit( $content_url ), PHP_URL_PATH );
40 $this->content_dir = trailingslashit( $content_dir );
41 }
42
43 // Only resolve plugin URLs if they are under the site URL
44 if ( $this->is_internal_uri( $plugin_url ) ) {
45 $this->plugin_uri_path = parse_url( trailingslashit( $plugin_url ), PHP_URL_PATH );
46 $this->plugin_dir = trailingslashit( $plugin_dir );
47 }
48 }
49
50 /**
51 * Given the full URL of a script/style dependency, return its local filesystem path.
52 */
53 function dependency_src_to_fs_path( $src ) {
54 if ( ! $this->is_internal_uri( $src ) ) {
55 // If a URI is not internal, we can have no confidence
56 // we are resolving to the correct file.
57 return false;
58 }
59
60 $src_parts = parse_url( $src );
61 if ( false === $src_parts ) {
62 return false;
63 }
64
65 if ( empty( $src_parts['path'] ) ) {
66 // We can't find anything to resolve
67 return false;
68 }
69 $path = $src_parts['path'];
70
71 if ( empty( $src_parts['host'] ) ) {
72 // With no host, this is a path relative to the WordPress root
73 $fs_path = "{$this->site_dir}{$path}";
74
75 return file_exists( $fs_path ) ? $fs_path : false;
76 }
77
78 return $this->uri_path_to_fs_path( $path );
79 }
80
81 /**
82 * Given a URI path of a script/style resource, return its local filesystem path.
83 */
84 function uri_path_to_fs_path( $uri_path ) {
85 if ( 1 === preg_match( '#(?:^|/)\.\.?(?:/|$)#', $uri_path ) ) {
86 // Reject relative paths
87 return false;
88 }
89
90 // The plugin URI path may be contained within the content URI path, so we check it before the content URI.
91 // And both the plugin and content URI paths must be contained within the site URI path,
92 // so we check them before checking the site URI.
93 if ( isset( $this->plugin_uri_path ) && static::is_descendant_uri( $this->plugin_uri_path, $uri_path ) ) {
94 $file_path = $this->plugin_dir . substr( $uri_path, strlen( $this->plugin_uri_path ) );
95 } else if ( isset( $this->content_uri_path ) && static::is_descendant_uri( $this->content_uri_path, $uri_path ) ) {
96 $file_path = $this->content_dir . substr( $uri_path, strlen( $this->content_uri_path ) );
97 } else if ( static::is_descendant_uri( $this->site_uri_path, $uri_path ) ) {
98 $file_path = $this->site_dir . substr( $uri_path, strlen( $this->site_uri_path ) );
99 }
100
101 if ( isset( $file_path ) && file_exists( $file_path ) ) {
102 return $file_path;
103 } else {
104 return false;
105 }
106 }
107
108 /**
109 * Determine whether a URI is internal, contained by this site.
110 *
111 * This method helps ensure we only resolve to local FS paths.
112 */
113 function is_internal_uri( $uri ) {
114 if ( page_optimize_starts_with( '/', $uri ) && ! page_optimize_starts_with( '//', $uri ) ) {
115 // Absolute paths are internal because they are based on the site dir (typically ABSPATH),
116 // and this looks like an absolute path.
117 return true;
118 }
119
120 // To be internal, a URL must have the same scheme, host, and port as the site URL
121 // and start with the same path as the site URL.
122 return static::is_descendant_uri( $this->site_url, $uri );
123 }
124
125 /**
126 * Check whether a path is descended from the given directory path.
127 *
128 * Does not handle relative paths.
129 */
130 static function is_descendant_uri( $dir_path, $candidate ) {
131 // Ensure a trailing slash to avoid false matches like
132 // "/wp-content/resource" being judged a descendant of "/wp".
133 $dir_path = trailingslashit( $dir_path );
134
135 return page_optimize_starts_with( $dir_path, $candidate );
136 }
137 }
138