forked from surrealroad/wp-onebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.php
338 lines (294 loc) · 10.5 KB
/
render.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/*
Generate Onebox HTML
*/
if($_REQUEST['debug']==1) {
// debug
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors',1);
}
require_once("lib/Encoding.php");
require_once("lib/phpQuery.php");
//Allow translations
load_plugin_textdomain('onebox', false, basename(dirname(__FILE__)).'/languages');
// set up list of parsers to include
$parsers = array(
"ebay",
"xivdb",
"kickstarter",
"wikipedia",
"googleplay",
"lynda",
"origin",
"greenmangaming",
"macgamestore",
"steam",
"github",
"gog",
"itunes",
"opengraph",
"twittercard",
"meta",
"favicon",
);
class Onebox {
// variables
public $properties = array(
"url",
"displayurl",
"countrycode",
"sitename",
"title",
"image",
"favicon",
"description",
"additional",
"footer",
"footerbutton",
"titlebutton",
);
public $data = array();
private $cacheid = "";
private $classes = array();
private $HTML = NULL;
private $doc = NULL;
public $affiliateLinks;
public $cached = false;
public $shouldCacheLocation = false;
public function __construct($url, $title, $description) {
foreach($this->properties as $property) {
$this->data[$property] = "";
}
$this->data['url'] = esc_url_raw($url);
if(isset($title)) $this->data['title'] = strip_tags($title);
if(isset($description)) $this->data['description'] = strip_tags($description);
$this->data['countrycode'] = self::user_cc();
if($this->data['countrycode']) $this->cacheid =md5($this->data['url']."|".$this->data['title']."|".$this->data['description']."|".$this->data['countrycode']);
else $this->cacheid =md5($this->data['url']."|".$this->data['title']."|".$this->data['description']);
$this->affiliateLinks = get_option('onebox_affiliate_links');
}
public function outputjson() {
$cache = $this->readCache();
if($cache) {
$output = $cache;
} else {
$this->data['favicon'] = self::sanitize_favicon($this->data['favicon'], $this->data['url']);
if($this->data['description']) {
$this->data['description'] = \ForceUTF8\Encoding::toUTF8($this->data['description']);
} else {
$this->data['description'] = __('No description available for this site', "onebox");
}
$this->data['additional'] = \ForceUTF8\Encoding::toUTF8($this->data['additional']);
if(!$this->data['sitename']) $this->data['sitename']= str_ireplace('www.', '', parse_url($this->data['url'], PHP_URL_HOST));
if(!$this->affiliateLinks) $this->data['displayurl']="";
$output = array('data'=>$this->data, 'classes'=>self::writeClasses());
// cache result
$this->writeCache($output);
}
return json_encode($output);
}
public function addClass($class) {
$this->classes[] = $class;
}
private function writeClasses() {
return implode(" ", $this->classes);
}
public function getHTML($forceencoding="") {
if(!$this->HTML && isset($this->data['url'])) {
if(self::isFileGetContentsAllowed()) {
if($forceencoding == "utf-8") {
$html = file_get_contents($this->data['url']);
$html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
$this->HTML = $html;
} else {
$html = file_get_contents($this->data['url']);
$html = mb_convert_encoding($html, 'HTML-ENTITIES');
$this->HTML = $html;
}
} else { // fall back to cURL
// from opengraph helper
$curl = curl_init($this->data['url']);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$response = curl_exec($curl);
curl_close($curl);
if (!empty($response)) {
$this->HTML = $response;
} else {
$this->HTML = false;
}
}
}
return $this->HTML;
}
// get source text for a URL, using appropriate method
public function getSource($url, $header=NULL) {
if(self::isFileGetContentsAllowed()) {
if($header) {
$options = array('http' => $header);
$context = stream_context_create($options);
$source = file_get_contents($url, false, $context);
} else {
$source = file_get_contents($url);
}
} else { // fall back to cURL
// from opengraph helper
$curl = curl_init($url);
if($header) {
$curlHeader = array();
foreach($curlHeader as $key => $value) {
$curlHeader[] = $key.": ".$value;
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeader);
}
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$source = curl_exec($curl);
curl_close($curl);
}
return $source;
}
public function getDoc($forceencoding="") {
if(!isset($this->doc) && isset($this->data['url'])) {
$this->doc = new DomDocument();
$old_libxml_error = libxml_use_internal_errors(true);
$this->doc->loadHTML(self::getHTML($forceencoding));
libxml_use_internal_errors($old_libxml_error);
}
return $this->doc;
}
public function availableProperties() {
$found = array();
foreach($this->properties as $property) {
if($this->data[$property]) $found[] = $property;
}
return $found;
}
// get user country code
private function user_cc() {
if(function_exists("geoip_country_code_by_name")) {
// http://stackoverflow.com/questions/10004294/php-if-statment-based-on-geo-location
if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip_address = getenv('HTTP_X_FORWARDED_FOR');
} else {
$ip_address = getenv('REMOTE_ADDR');
}
return @geoip_country_code_by_name($ip_address);
}
return "";
}
// http://snipplr.com/view.php?codeview&id=36437
public function country_currency( $bc, $amount = 0 ) {
$currency_before = '';
$currency_after = '';
$bc = strtoupper($bc);
if( $bc == 'GB' || $bc == 'IE' || $bc == 'CY' ) $currency_before = '£';
if( $bc == 'AT' || $bc == 'BE' || $bc == 'FI' || $bc == 'FR' ||
$bc == 'DE' || $bc == 'GR' || $bc == 'GP' || $bc == 'IT' ||
$bc == 'LU' || $bc == 'NL' || $bc == 'PT' || $bc == 'SI' ||
$bc == 'ES') $currency_before = '€';
if( $bc == 'BR' ) $currency_before = 'R$';
if( $bc == 'CN' || $bc == 'JP' ) $currency_before = '¥';
if( $bc == 'CR' ) $currency_before = '¢';
if( $bc == 'HR' ) $currency_after = ' kn';
if( $bc == 'CZ' ) $currency_after = ' kc';
if( $bc == 'DK' ) $currency_before = 'DKK ';
if( $bc == 'EE' ) $currency_after = ' EEK';
if( $bc == 'HK' ) $currency_before = 'HK$';
if( $bc == 'HU' ) $currency_after = ' Ft';
if( $bc == 'IS' || $bc == 'SE' ) $currency_after = ' kr';
if( $bc == 'IN' ) $currency_before = 'Rs. ';
if( $bc == 'ID' ) $currency_before = 'Rp. ';
if( $bc == 'IL' ) $currency_after = ' NIS';
if( $bc == 'LV' ) $currency_before = 'Ls ';
if( $bc == 'LT' ) $currency_after = ' Lt';
if( $bc == 'MY' ) $currency_before = 'RM';
if( $bc == 'MT' ) $currency_before = 'Lm';
if( $bc == 'NO' ) $currency_before = 'kr ';
if( $bc == 'PH' ) $currency_before = 'PHP';
if( $bc == 'PL' ) $currency_after = ' z';
if( $bc == 'RO' ) $currency_after = ' lei';
if( $bc == 'RU' ) $currency_before = 'RUB';
if( $bc == 'SK' ) $currency_after = ' Sk';
if( $bc == 'ZA' ) $currency_before = 'R ';
if( $bc == 'KR' ) $currency_before = 'W';
if( $bc == 'CH' ) $currency_before = 'SFr. ';
if( $bc == 'SY' ) $currency_after = ' SYP';
if( $bc == 'TH' ) $currency_after = ' Bt';
if( $bc == 'TT' ) $currency_before = 'TT$';
if( $bc == 'TR' ) $currency_after = ' TL';
if( $bc == 'AE' ) $currency_before = 'Dhs. ';
if( $bc == 'VE' ) $currency_before = 'Bs. ';
if( $currency_before == '' && $currency_after == '' ) $currency_before = '$';
return $currency_before . number_format( $amount, 2 ) . $currency_after;
}
private function sanitize_favicon($favicon, $url) {
// Prepend "http://" to any link missing the HTTP protocol text, unless it's in the form "//".
if ($favicon && preg_match('|^https*://|', $favicon) === 0 && substr($favicon, 0, 2) !== '//') {
if (substr($favicon, 0, 1) !== '/') $favicon = "/".$favicon;
$favicon = parse_url($url, PHP_URL_SCHEME)."://".parse_url($url, PHP_URL_HOST) . $favicon;
}
return $favicon;
}
// update missing data values with new data values
public function update($newdata) {
if(isset($newdata)) {
foreach ($newdata as $key => $value) {
if((!isset($this->data[$key]) || !$this->data[$key]) && $value) $this->data[$key]=$value;
}
}
}
private function isAPCCacheInstalled() {
return extension_loaded('apc');
}
public function readCache() {
if(!get_option('onebox_enable_apc_cache') || !$this->isAPCCacheInstalled()) return false;
return apc_fetch($this->cacheid);
}
private function writeCache($output) {
if(!get_option('onebox_enable_apc_cache') || !$this->isAPCCacheInstalled()) return false;
$id = $this->cacheid;
$ttl = 43200; //12 * 60 * 60;
$this->cached = apc_store($id, $output, $ttl);
}
public function isFileGetContentsAllowed() {
return ini_get('allow_url_fopen');
}
// ### Checks for presence of the cURL extension. http://cleverwp.com/function-curl-php-extension-loaded/
public function isCurlInstalled() {
if (in_array ('curl', get_loaded_extensions())) {
return true;
}
else{
return false;
}
}
public function oneboxDate ($date) {
if(get_option('date_format')) return date(get_option('date_format'), $date);
else return date('F jS Y', $date);
}
}
if(get_query_var("onebox_url")) {
$onebox = new Onebox(urldecode(get_query_var("onebox_url")), urldecode(get_query_var("onebox_title")), urldecode(get_query_var("onebox_description")));
if($_REQUEST['debug']==1) var_dump($onebox);
if($onebox->readCache()) {
if($_REQUEST['debug']==1) echo "<strong>CACHED</strong><br/>";
echo json_encode($onebox->readCache());
} else {
// run parsers
foreach($parsers as $parser) include("parsers/".$parser.".php");
echo $onebox->outputjson();
}
if($_REQUEST['debug']==1) var_dump($onebox);
}