thomascube
2012-05-04 5b04ddd6bc9e0af5f73694371cd3988b1d5be7e8
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  $Id$
22
23 */
24
25 /**
26  * Helper class to turn relative urls into absolute ones
27  * using a predefined base
28  *
29  * @package Core
30  * @author Thomas Bruederli <roundcube@gmail.com>
31  */
32 class rcube_base_replacer
33 {
34     private $base_url;
35
36
37     public function __construct($base)
38     {
39         $this->base_url = $base;
40     }
41
42
43     public function callback($matches)
44     {
45         return $matches[1] . '="' . self::absolute_url($matches[3], $this->base_url) . '"';
46     }
47
48
49     public function replace($body)
50     {
51         return preg_replace_callback(array(
52             '/(src|background|href)=(["\']?)([^"\'\s]+)(\2|\s|>)/Ui',
53             '/(url\s*\()(["\']?)([^"\'\)\s]+)(\2)\)/Ui',
54         ),
55         array($this, 'callback'), $body);
56     }
57
58
59     /**
60      * Convert paths like ../xxx to an absolute path using a base url
61      *
62      * @param string $path     Relative path
63      * @param string $base_url Base URL
64      *
65      * @return string Absolute URL
66      */
67     public static function absolute_url($path, $base_url)
68     {
69         $host_url = $base_url;
70         $abs_path = $path;
71
72         // check if path is an absolute URL
73         if (preg_match('/^[fhtps]+:\/\//', $path)) {
74             return $path;
75         }
76
77         // check if path is a content-id scheme
78         if (strpos($path, 'cid:') === 0) {
79             return $path;
80         }
81
82         // cut base_url to the last directory
83         if (strrpos($base_url, '/') > 7) {
84             $host_url = substr($base_url, 0, strpos($base_url, '/', 7));
85             $base_url = substr($base_url, 0, strrpos($base_url, '/'));
86         }
87
88         // $path is absolute
89         if ($path[0] == '/') {
90             $abs_path = $host_url.$path;
91         }
92         else {
93             // strip './' because its the same as ''
94             $path = preg_replace('/^\.\//', '', $path);
95
96             if (preg_match_all('/\.\.\//', $path, $matches, PREG_SET_ORDER)) {
97                 foreach ($matches as $a_match) {
98                     if (strrpos($base_url, '/')) {
99                         $base_url = substr($base_url, 0, strrpos($base_url, '/'));
100                     }
101                     $path = substr($path, 3);
102                 }
103             }
104
105             $abs_path = $base_url.'/'.$path;
106         }
107
108         return $abs_path;
109     }
110 }