/
shortcode.php
104 lines (91 loc) · 3.47 KB
/
shortcode.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
* Copyright 2016 Samuel Behan <samuel(.)behan(at)dob(.)sk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* ( at your option ) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
defined('ABSPATH') or exit();
function include_url_shortcode($attrs, $msg = null) {
// href=url
$href = $attrs['href'];
// params=param1,param2,param3
$params = isset($attrs['params']) ? $attrs['params'] : $attrs['param'];
// timeout=seconds
$timeout = isset($attrs['timeout']) ? $attrs['timeout'] : 10;
// cache=seconds
$cache = isset($attrs['cache']) ? $attrs['cache'] : 0;
// allow-file=int
$allow_file = isset($attrs['allow-file']) ? $attrs['allow-file'] : 0;
$is_file = (($allow_file != 0) && preg_match('/^file:\//', $href)) ? 1 : 0;
// allow-other=int
$allow_other = isset($attrs['allow-other']) ? $attrs['allow-other'] : 0;
if (!isset($href))
return '<b>include-url: required href parameter</b>';
if (($allow_other == 0) && (!preg_match('/^https?:\//', $href) && (($allow_file == 0) || !$is_file)))
return '<b>include-url: only http://, https:// and file:// (if allow-file="1") are urls allowed in href parameter (' . $href . ')</b>';
// allow-file=1 - prepend document root
if ($is_file && $allow_file == 1) {
$href = preg_replace('/^file:\/\//', 'file://' . $_SERVER['DOCUMENT_ROOT'] . '/', $href);
}
$cache_key = 'include-url';
$args = array();
$params = explode(',', $params);
foreach ($params as $key) {
if (isset($_GET[$key]))
$args[$key] = urlencode($_GET[$key]);
$cache_key .= ':' . $_GET[$key];
}
// build url
$url = add_query_arg($args, $href);
// fetch content
$content = '';
if ($cache > 0)
$content = get_transient($cache_key);
if (!$content) {
if (function_exists('curl_version')) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0");
//curl_setopt($c, CURLOPT_COOKIE, 'CookieName1=Value;');
//curl_setopt($c, CURLOPT_MAXREDIRS, 10);
//$follow_allowed = ( ini_get('open_basedir') || ini_get('safe_mode')) ? false : true;
//if ($follow_allowed)
// curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($c, CURLOPT_REFERER, $url);
curl_setopt($c, CURLOPT_TIMEOUT, $timeout);
curl_setopt($c, CURLOPT_AUTOREFERER, true);
//curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');
$content = curl_exec($c);
$status = curl_getinfo($c);
curl_close($c);
if ($status['http_code'] != 200 && !$is_file)
$content = $msg;
} else {
$content = file_get_contents($url);
if (!$content)
$content = $msg;
}
if ($cache > 0 && $content)
set_transient($cache_key, $content, $cache);
}
return $content;
}
add_shortcode('include-url', 'include_url_shortcode');
?>