commit | author | age
|
0c2596
|
1 |
<?php |
A |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
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 |
|
|
32 |
public function __construct($base) |
|
33 |
{ |
|
34 |
$this->base_url = $base; |
|
35 |
} |
|
36 |
|
|
37 |
|
|
38 |
public function callback($matches) |
|
39 |
{ |
|
40 |
return $matches[1] . '="' . self::absolute_url($matches[3], $this->base_url) . '"'; |
|
41 |
} |
|
42 |
|
|
43 |
|
|
44 |
public function replace($body) |
|
45 |
{ |
|
46 |
return preg_replace_callback(array( |
|
47 |
'/(src|background|href)=(["\']?)([^"\'\s]+)(\2|\s|>)/Ui', |
|
48 |
'/(url\s*\()(["\']?)([^"\'\)\s]+)(\2)\)/Ui', |
|
49 |
), |
|
50 |
array($this, 'callback'), $body); |
|
51 |
} |
|
52 |
|
|
53 |
|
|
54 |
/** |
|
55 |
* Convert paths like ../xxx to an absolute path using a base url |
|
56 |
* |
|
57 |
* @param string $path Relative path |
|
58 |
* @param string $base_url Base URL |
|
59 |
* |
|
60 |
* @return string Absolute URL |
|
61 |
*/ |
|
62 |
public static function absolute_url($path, $base_url) |
|
63 |
{ |
|
64 |
$host_url = $base_url; |
|
65 |
$abs_path = $path; |
|
66 |
|
|
67 |
// check if path is an absolute URL |
|
68 |
if (preg_match('/^[fhtps]+:\/\//', $path)) { |
|
69 |
return $path; |
|
70 |
} |
|
71 |
|
|
72 |
// check if path is a content-id scheme |
|
73 |
if (strpos($path, 'cid:') === 0) { |
|
74 |
return $path; |
|
75 |
} |
|
76 |
|
|
77 |
// cut base_url to the last directory |
|
78 |
if (strrpos($base_url, '/') > 7) { |
|
79 |
$host_url = substr($base_url, 0, strpos($base_url, '/', 7)); |
|
80 |
$base_url = substr($base_url, 0, strrpos($base_url, '/')); |
|
81 |
} |
|
82 |
|
|
83 |
// $path is absolute |
|
84 |
if ($path[0] == '/') { |
|
85 |
$abs_path = $host_url.$path; |
|
86 |
} |
|
87 |
else { |
|
88 |
// strip './' because its the same as '' |
|
89 |
$path = preg_replace('/^\.\//', '', $path); |
|
90 |
|
|
91 |
if (preg_match_all('/\.\.\//', $path, $matches, PREG_SET_ORDER)) { |
|
92 |
foreach ($matches as $a_match) { |
|
93 |
if (strrpos($base_url, '/')) { |
|
94 |
$base_url = substr($base_url, 0, strrpos($base_url, '/')); |
|
95 |
} |
|
96 |
$path = substr($path, 3); |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
$abs_path = $base_url.'/'.$path; |
|
101 |
} |
|
102 |
|
|
103 |
return $abs_path; |
|
104 |
} |
|
105 |
} |