|
1 | 1 | <?php |
2 | | -namespace wcf\system\event\listener; |
3 | | -use wcf\util\Signer; |
| 2 | +namespace wcf\system\event\listener; |
| 3 | +use wcf\util\Signer; |
4 | 4 |
|
5 | 5 | /** |
6 | | - * Replace images with proxy images. |
7 | | - * |
| 6 | + * Replace images with proxy images. |
| 7 | + * |
8 | 8 | * @author Joshua Rüsweg |
9 | 9 | * @copyright 2015 Joshua Rüsweg |
10 | 10 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
11 | 11 | * @package be.bastelstu.josh.imageproxy |
12 | 12 | */ |
13 | 13 | class ProxyListener implements \wcf\system\event\listener\IParameterizedEventListener { |
14 | | - |
| 14 | + |
15 | 15 | /** |
16 | 16 | * @see \wcf\system\event\IEventListener::execute() |
17 | 17 | */ |
18 | 18 | public function execute($eventObj, $className, $eventName, array &$parameters) { |
19 | | - if (!MODULE_PROXY) return; |
20 | | - |
| 19 | + if (!MODULE_PROXY) return; |
| 20 | + |
21 | 21 | if (!$eventObj->message || \wcf\data\bbcode\BBCodeCache::getInstance()->getBBCodeByTag('img') === null) { |
22 | 22 | return; |
23 | 23 | } |
24 | | - |
| 24 | + |
25 | 25 | // match [img]link[/img] |
26 | | - preg_match_all('~\[img\]([^\]]*)\[\/img\]~i', $eventObj->message, $matches, PREG_SET_ORDER); |
27 | | - |
| 26 | + preg_match_all('~\[img\](?P<url>[^\]]*)\[\/img\]~i', $eventObj->message, $matches, PREG_SET_ORDER); |
| 27 | + |
28 | 28 | // match all [img=link,(none|left|right|center),[0-9]+] |
29 | | - preg_match_all("~\[img=\'([^\]]*)\',(none|left|right|center),([0-9]+)\](\[\/img\])?~i", $eventObj->message, $matches2, PREG_SET_ORDER); |
30 | | - |
| 29 | + preg_match_all("~\[img=\'(?P<url>[^\]]*)\',(?P<orientation>none|left|right|center),(?P<size>[0-9]+)\](\[\/img\])?~i", $eventObj->message, $matches2, PREG_SET_ORDER); |
| 30 | + |
31 | 31 | // match all [img=link,(none|left|right|center)] |
32 | | - preg_match_all("~\[img=\'([^\]]*)\',(none|left|right|center)\](\[\/img\])?~i", $eventObj->message, $matches3, PREG_SET_ORDER); |
| 32 | + preg_match_all("~\[img=\'(?P<url>[^\]]*)\',(?P<orientation>none|left|right|center)\](\[\/img\])?~i", $eventObj->message, $matches3, PREG_SET_ORDER); |
33 | 33 |
|
34 | 34 | // match [img=link] |
35 | | - preg_match_all("~\[img=\'?([^\,\]]*)\'?\](\[\/img\])?~i", $eventObj->message, $matches4, PREG_SET_ORDER); |
| 35 | + preg_match_all("~\[img=\'?(?P<url>[^\,\]]*)\'?\](\[\/img\])?~i", $eventObj->message, $matches4, PREG_SET_ORDER); |
36 | 36 |
|
37 | 37 | $matches = array_merge($matches, $matches2, $matches3, $matches4); |
38 | 38 |
|
39 | 39 | foreach ($matches as $match) { |
40 | 40 | if (function_exists('gethostbyname')) { |
41 | | - // is localhost? |
42 | | - $url = parse_url($match[1]); |
| 41 | + // is localhost? |
| 42 | + $url = parse_url($match['url']); |
43 | 43 |
|
44 | 44 | if ($url === false) { |
45 | 45 | // url isn't a url |
46 | 46 | continue; |
47 | 47 | } |
48 | 48 |
|
49 | | - $host = @gethostbyname($url['host']); |
50 | | - $localhost = false; |
| 49 | + $host = @gethostbyname($url['host']); |
51 | 50 |
|
52 | 51 | if (\wcf\system\Regex::compile('127.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}')->match($host)) { |
53 | | - $localhost = true; |
| 52 | + continue; |
54 | 53 | } |
55 | 54 |
|
56 | 55 | if (\wcf\system\Regex::compile('10.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}')->match($host)) { |
57 | | - $localhost = true; |
| 56 | + continue; |
58 | 57 | } |
59 | 58 |
|
60 | 59 | if (\wcf\system\Regex::compile('192.168.[0-9]{1,3}.[0-9]{1,3}')->match($host)) { |
61 | | - $localhost = true; |
| 60 | + continue; |
62 | 61 | } |
63 | 62 |
|
64 | 63 | if (\wcf\system\Regex::compile('172.16.[0-9]{1,3}.[0-9]{1,3}')->match($host)) { |
65 | | - $localhost = true; |
| 64 | + continue; |
66 | 65 | } |
67 | 66 |
|
68 | | - if (!$localhost && !\wcf\system\application\ApplicationHandler::getInstance()->isInternalURL($match[1])) { |
69 | | - $eventObj->message = \wcf\util\StringUtil::replaceIgnoreCase($match[0], '[img=\''. $this->buildImageURL($match[1]) .'\''. ((isset($match[2]) && $match[2] != '[/img]') ? ','.$match[2].((isset($match[3]) && $match[3] != '[/img]') ? ','.$match[3] : '') : '') .'][/img]', $eventObj->message); |
| 67 | + if (!\wcf\system\application\ApplicationHandler::getInstance()->isInternalURL($match['url'])) { |
| 68 | + $eventObj->message = \wcf\util\StringUtil::replaceIgnoreCase($match[0], '[img=\''. $this->buildImageURL($match['url']) .'\''. ((isset($match['orientation'])) ? ','.$match['orientation'].((isset($match['size'])) ? ','.$match['size'] : '') : '') .'][/img]', $eventObj->message); |
70 | 69 | } |
71 | 70 | } |
72 | 71 | } |
73 | 72 | } |
74 | | - |
| 73 | + |
75 | 74 | public static function buildImageURL($url) { |
76 | 75 | return \wcf\system\request\LinkHandler::getInstance()->getLink('ImageProxy', array('image' => Signer::createSignedString($url))); |
77 | 76 | } |
|
0 commit comments