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