commit | author | age
|
0c2596
|
1 |
<?php |
A |
2 |
|
a95874
|
3 |
/** |
0c2596
|
4 |
+-----------------------------------------------------------------------+ |
A |
5 |
| This file is part of the Roundcube Webmail client | |
|
6 |
| Copyright (C) 2005-2012, The Roundcube Dev Team | |
|
7 |
| | |
|
8 |
| Licensed under the GNU General Public License version 3 or | |
|
9 |
| any later version with exceptions for skins & plugins. | |
|
10 |
| See the README file for a full license statement. | |
|
11 |
| | |
|
12 |
| PURPOSE: | |
|
13 |
| Provide basic functions for base URL replacement | |
|
14 |
+-----------------------------------------------------------------------+ |
|
15 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
16 |
+-----------------------------------------------------------------------+ |
|
17 |
*/ |
|
18 |
|
|
19 |
/** |
|
20 |
* Helper class to turn relative urls into absolute ones |
|
21 |
* using a predefined base |
|
22 |
* |
9ab346
|
23 |
* @package Framework |
a6fd15
|
24 |
* @subpackage Utils |
9ab346
|
25 |
* @author Thomas Bruederli <roundcube@gmail.com> |
0c2596
|
26 |
*/ |
A |
27 |
class rcube_base_replacer |
|
28 |
{ |
|
29 |
private $base_url; |
|
30 |
|
|
31 |
|
a95874
|
32 |
/** |
AM |
33 |
* Class constructor |
|
34 |
* |
|
35 |
* @param string $base Base URL |
|
36 |
*/ |
0c2596
|
37 |
public function __construct($base) |
A |
38 |
{ |
|
39 |
$this->base_url = $base; |
|
40 |
} |
|
41 |
|
a95874
|
42 |
/** |
AM |
43 |
* Replace callback |
|
44 |
* |
|
45 |
* @param array $matches Matching entries |
|
46 |
* |
|
47 |
* @return string Replaced text with absolute URL |
|
48 |
*/ |
0c2596
|
49 |
public function callback($matches) |
A |
50 |
{ |
|
51 |
return $matches[1] . '="' . self::absolute_url($matches[3], $this->base_url) . '"'; |
|
52 |
} |
|
53 |
|
a95874
|
54 |
/** |
AM |
55 |
* Convert base URLs to absolute ones |
|
56 |
* |
|
57 |
* @param string $body Text body |
|
58 |
* |
|
59 |
* @return string Replaced text |
|
60 |
*/ |
0c2596
|
61 |
public function replace($body) |
A |
62 |
{ |
a95874
|
63 |
$regexp = array( |
a79017
|
64 |
'/(src|background|href)=(["\']?)([^"\'\s>]+)(\2|\s|>)/i', |
AM |
65 |
'/(url\s*\()(["\']?)([^"\'\)\s]+)(\2)\)/i', |
a95874
|
66 |
); |
0c2596
|
67 |
|
a95874
|
68 |
return preg_replace_callback($regexp, array($this, 'callback'), $body); |
AM |
69 |
} |
0c2596
|
70 |
|
A |
71 |
/** |
|
72 |
* Convert paths like ../xxx to an absolute path using a base url |
|
73 |
* |
|
74 |
* @param string $path Relative path |
|
75 |
* @param string $base_url Base URL |
|
76 |
* |
|
77 |
* @return string Absolute URL |
|
78 |
*/ |
|
79 |
public static function absolute_url($path, $base_url) |
|
80 |
{ |
|
81 |
// check if path is an absolute URL |
|
82 |
if (preg_match('/^[fhtps]+:\/\//', $path)) { |
|
83 |
return $path; |
|
84 |
} |
|
85 |
|
|
86 |
// check if path is a content-id scheme |
|
87 |
if (strpos($path, 'cid:') === 0) { |
|
88 |
return $path; |
|
89 |
} |
9e4246
|
90 |
|
AM |
91 |
$host_url = $base_url; |
|
92 |
$abs_path = $path; |
0c2596
|
93 |
|
A |
94 |
// cut base_url to the last directory |
|
95 |
if (strrpos($base_url, '/') > 7) { |
|
96 |
$host_url = substr($base_url, 0, strpos($base_url, '/', 7)); |
|
97 |
$base_url = substr($base_url, 0, strrpos($base_url, '/')); |
|
98 |
} |
|
99 |
|
|
100 |
// $path is absolute |
|
101 |
if ($path[0] == '/') { |
|
102 |
$abs_path = $host_url.$path; |
|
103 |
} |
|
104 |
else { |
|
105 |
// strip './' because its the same as '' |
|
106 |
$path = preg_replace('/^\.\//', '', $path); |
|
107 |
|
|
108 |
if (preg_match_all('/\.\.\//', $path, $matches, PREG_SET_ORDER)) { |
9e4246
|
109 |
$cnt = count($matches); |
AM |
110 |
while ($cnt--) { |
ff6de9
|
111 |
if ($pos = strrpos($base_url, '/')) { |
AM |
112 |
$base_url = substr($base_url, 0, $pos); |
0c2596
|
113 |
} |
A |
114 |
$path = substr($path, 3); |
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
$abs_path = $base_url.'/'.$path; |
|
119 |
} |
|
120 |
|
|
121 |
return $abs_path; |
|
122 |
} |
|
123 |
} |