PluginProbe ʕ •ᴥ•ʔ
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF / 2.0.7
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF v2.0.7
2.0.7 2.0.6 2.0.5 trunk 1.3.7 1.4.0 1.4.1 1.4.2 1.4.6 1.5.0 1.5.3 1.5.6 1.5.8 1.6.5 1.6.6 1.6.9 1.7.0 1.7.4 1.8.1 1.8.2 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4
robin-image-optimizer / libs / class-rio-relative-to-abs-uri.php
robin-image-optimizer / libs Last commit date
addons 1 month ago factory 1 day ago class-rio-relative-to-abs-uri.php 7 months ago
class-rio-relative-to-abs-uri.php
180 lines
1 <?php
2
3 namespace WRIO\Paths;
4
5 // Exit if accessed directly
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 /**
11 * A php library for converting relative urls to absolute.
12 * Website: https://github.com/monkeysuffrage/phpuri
13 *
14 * <pre>
15 * echo phpUri::parse('https://www.google.com/')->join('foo');
16 * //==> https://www.google.com/foo
17 * </pre>
18 *
19 * Licensed under The MIT License
20 * Redistributions of files must retain the above copyright notice.
21 *
22 * @version 1.0
23 */
24 class phpUri {
25
26 /**
27 * http(s)://
28 *
29 * @var string
30 */
31 public $scheme;
32 /**
33 * www.example.com
34 *
35 * @var string
36 */
37 public $authority;
38 /**
39 * /search
40 *
41 * @var string
42 */
43 public $path;
44 /**
45 * ?q=foo
46 *
47 * @var string
48 */
49 public $query;
50 /**
51 * #bar
52 *
53 * @var string
54 */
55 public $fragment;
56
57 private function __construct( $string ) {
58 preg_match_all( '/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/', $string, $m );
59 $this->scheme = $m[2][0];
60 $this->authority = $m[4][0];
61 /**
62 * CHANGE:
63 *
64 * @since 24 Mai 2015 10:02 Uhr
65 *
66 * Former code: $this->path = ( empty( $m[ 5 ][ 0 ] ) ) ? '/' : $m[ 5 ][ 0 ];
67 * No tests failed, when the path is empty.
68 * With the former code, the relative urls //g and #s failed
69 */
70 $this->path = $m[5][0];
71 $this->query = $m[7][0];
72 $this->fragment = $m[9][0];
73 }
74
75 private function to_str() {
76 $ret = '';
77 if ( ! empty( $this->scheme ) ) {
78 $ret .= "{$this->scheme}:";
79 }
80 if ( ! empty( $this->authority ) ) {
81 $ret .= "//{$this->authority}";
82 }
83 $ret .= $this->normalize_path( $this->path );
84 if ( ! empty( $this->query ) ) {
85 $ret .= "?{$this->query}";
86 }
87 if ( ! empty( $this->fragment ) ) {
88 $ret .= "#{$this->fragment}";
89 }
90
91 return $ret;
92 }
93
94 private function normalize_path( $path ) {
95 if ( empty( $path ) ) {
96 return '';
97 }
98 $normalized_path = $path;
99 $normalized_path = preg_replace( '`//+`', '/', $normalized_path, - 1, $c0 );
100 $normalized_path = preg_replace( '`^/\\.\\.?/`', '/', $normalized_path, - 1, $c1 );
101 $normalized_path = preg_replace( '`/\\.(/|$)`', '/', $normalized_path, - 1, $c2 );
102 /**
103 * CHANGE:
104 *
105 * @since 24 Mai 2015 10:05 Uhr
106 * changed limit form -1 to 1, because climbing up the directory-tree failed
107 */
108 $normalized_path = preg_replace( '`/[^/]*?/\\.\\.(/|$)`', '/', $normalized_path, 1, $c3 );
109 $num_matches = $c0 + $c1 + $c2 + $c3;
110
111 return ( $num_matches > 0 ) ? $this->normalize_path( $normalized_path ) : $normalized_path;
112 }
113
114 /**
115 * Parse an url string
116 *
117 * @param string $url the url to parse
118 *
119 * @return phpUri
120 */
121 public static function parse( $url ) {
122 $uri = new phpUri( $url );
123 /**
124 * CHANGE:
125 *
126 * @since 24 Mai 2015 10:25 Uhr
127 * The base-url should always have a path
128 */
129 if ( empty( $uri->path ) ) {
130 $uri->path = '/';
131 }
132
133 return $uri;
134 }
135
136 /**
137 * Join with a relative url
138 *
139 * @param string $relative the relative url to join
140 *
141 * @return string
142 */
143 public function join( $relative ) {
144 $uri = new phpUri( $relative );
145 switch ( true ) {
146 case ! empty( $uri->scheme ):
147 break;
148 case ! empty( $uri->authority ):
149 break;
150 case empty( $uri->path ):
151 $uri->path = $this->path;
152 if ( empty( $uri->query ) ) {
153 $uri->query = $this->query;
154 }
155 break;
156 case strpos( $uri->path, '/' ) === 0:
157 break;
158 default:
159 $base_path = $this->path;
160 if ( strpos( $base_path, '/' ) === false ) {
161 $base_path = '';
162 } else {
163 $base_path = preg_replace( '/\/[^\/]+$/', '/', $base_path );
164 }
165 if ( empty( $base_path ) && empty( $this->authority ) ) {
166 $base_path = '/';
167 }
168 $uri->path = $base_path . $uri->path;
169 }
170 if ( empty( $uri->scheme ) ) {
171 $uri->scheme = $this->scheme;
172 if ( empty( $uri->authority ) ) {
173 $uri->authority = $this->authority;
174 }
175 }
176
177 return $uri->to_str();
178 }
179 }
180