Test Failed
Pull Request — master (#9)
by Andrew
01:38
created

Response::hadleProxyURL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace Ditto;
4
5
class Response
6
{
7
	public $origHtml;
8
	public $html;
9
	public $proxyPath;
10
11
	public function __construct($html)
12
	{
13
		$this->html = $this->origHtml = $html;
14
	}
15
16
	public function setProxyPath($path)
17
	{
18
		$this->proxyPath = $path;
19
	}
20
21
	public function replaceDomainLinks($domain)
22
	{
23
		$this->html = str_replace($domain, $this->proxyPath, $this->html);
24
	}
25
26
	// handle url encoding
27
	public function hadleProxyURL($url)
28
	{
29
		return \strstr($this->proxyPath, '?') ? urlencode($url) : $url;
30
	}
31
32
	public function replaceInternalHtmlLinks($usingUrlParam=false)
33
	{
34
		// replace href & src links where they DO NOT start with https?:... or //...
35
		$this->html = preg_replace_callback('/(src|href)=(["\'])(?!((["\'])?https?:|(["\'])?\/\/))(.*?)\2/i', function ($matches) use ($usingUrlParam) {
36
            return $matches[1] . '=' . $matches[2]
37
                . rtrim($this->proxyPath, '/')
38
                . $this->hadleProxyURL('/' . ltrim($usingUrlParam ? htmlspecialchars_decode($matches[6]) : $matches[6], '/'))
39
                . $matches[2];
40
        }, $this->html);
41
	}
42
43
	public function replaceInternalCssLinks($usingUrlParam=false)
44
	{
45
		// replace url() links where they DO NOT start with https?:... or //...
46
		$this->html = preg_replace_callback('/url\((["\'])?(?!((["\'])?https?:|(["\'])?\/\/))(.*?)(["\'])?\)/i', function ($matches) use ($usingUrlParam) {
47
			return 'url(' . $matches[1] . rtrim($this->proxyPath, '/') .'/'. ltrim($usingUrlParam ? $this->hadleProxyURL(htmlspecialchars_decode($matches[5])) : $matches[5], '/') . $matches[1] . ')';
48
		}, $this->html);
49
	}
50
51
	public function getHtml()
52
	{
53
		return $this->html;
54
	}
55
56
	public function getOrigHtml()
57
	{
58
		return $this->origHtml;
59
	}
60
}
61