You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_base_replacer.php 3.6KB

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