This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.php
61 lines (46 loc) · 1.68 KB
/
remote.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* @author Branko Wilhelm <branko.wilhelm@gmail.com>
* @link http://www.z-index.net
* @copyright (c) 2013 - 2014 Branko Wilhelm
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
defined('_JEXEC') or die;
class plgContentRemote extends JPlugin
{
public function onContentPrepare($context, &$row, &$params, $page = 0)
{
if (JString::strpos($row->text, '{remote') === false) {
return true;
}
preg_match_all('#{remote\s+(.*?)}#i', $row->text, $matches, PREG_SET_ORDER);
if (!empty($matches)) {
foreach ($matches as $match) {
$row->text = JString::str_ireplace($match[0], $this->remote($match[1]), $row->text);
}
}
}
private function remote($url)
{
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return 'not a valid url: ' . $url;
}
$cache = JFactory::getCache('remote', 'output');
$cache->setCaching(1);
$cache->setLifeTime($this->params->get('cache_time', 60) * 60);
$key = md5($url);
if (!$result = $cache->get($key)) {
try {
$http = JHttpFactory::getHttp();
$result = $http->get($url, null, $this->params->get('timeout', 10));
} catch (Exception $e) {
return $e->getMessage();
}
$cache->store($result, $key);
}
if ($result->code != 200) {
return __CLASS__ . ' HTTP-Status ' . JHtml::_('link', 'http://wikipedia.org/wiki/List_of_HTTP_status_codes#' . $result->code, $result->code, array('target' => '_blank'));
}
return $result->body;
}
}