Mercurial > hg > rc2
comparison program/lib/Roundcube/rcube_base_replacer.php @ 0:4681f974d28b
vanilla 1.3.3 distro, I hope
author | Charlie Root |
---|---|
date | Thu, 04 Jan 2018 15:52:31 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4681f974d28b |
---|---|
1 <?php | |
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 * | |
23 * @package Framework | |
24 * @subpackage Utils | |
25 * @author Thomas Bruederli <roundcube@gmail.com> | |
26 */ | |
27 class rcube_base_replacer | |
28 { | |
29 private $base_url; | |
30 | |
31 | |
32 /** | |
33 * Class constructor | |
34 * | |
35 * @param string $base Base URL | |
36 */ | |
37 public function __construct($base) | |
38 { | |
39 $this->base_url = $base; | |
40 } | |
41 | |
42 /** | |
43 * Replace callback | |
44 * | |
45 * @param array $matches Matching entries | |
46 * | |
47 * @return string Replaced text with absolute URL | |
48 */ | |
49 public function callback($matches) | |
50 { | |
51 return $matches[1] . '="' . self::absolute_url($matches[3], $this->base_url) . '"'; | |
52 } | |
53 | |
54 /** | |
55 * Convert base URLs to absolute ones | |
56 * | |
57 * @param string $body Text body | |
58 * | |
59 * @return string Replaced text | |
60 */ | |
61 public function replace($body) | |
62 { | |
63 $regexp = array( | |
64 '/(src|background|href)=(["\']?)([^"\'\s>]+)(\2|\s|>)/i', | |
65 '/(url\s*\()(["\']?)([^"\'\)\s]+)(\2)\)/i', | |
66 ); | |
67 | |
68 return preg_replace_callback($regexp, array($this, 'callback'), $body); | |
69 } | |
70 | |
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 } | |
90 | |
91 $host_url = $base_url; | |
92 $abs_path = $path; | |
93 | |
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)) { | |
109 $cnt = count($matches); | |
110 while ($cnt--) { | |
111 if ($pos = strrpos($base_url, '/')) { | |
112 $base_url = substr($base_url, 0, $pos); | |
113 } | |
114 $path = substr($path, 3); | |
115 } | |
116 } | |
117 | |
118 $abs_path = $base_url.'/'.$path; | |
119 } | |
120 | |
121 return $abs_path; | |
122 } | |
123 } |