-
Notifications
You must be signed in to change notification settings - Fork 75
/
embedlyProxy.php
69 lines (55 loc) · 1.73 KB
/
embedlyProxy.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
<?php
// Directory to cache the results (should be writable !)
$cacheDir = 'cache';
// Embed.ly API key
// If null, the one set in JavaScript will be used
// If not null, this key will replace the one set in JavaScript
$key = 'EMBED.LY API KEY';
// Referer to restrict the call from
$referers = array(
'*.nyrodev.com/*'
);
// Embed.ly API URL (change only if you know what you're doing)
$urlApi = 'http://api.embed.ly/1/oembed';
// Replace callback used for caching. (change only if you know what you're doing)
$replaceClb = 'DUMMYCALLBACK';
//--------------------------------------------------------------//
// Do not edit under this line ---------------------------------//
//--------------------------------------------------------------//
// Referers checks Start
$continue = false;
$httpReferer = $_SERVER['HTTP_REFERER'];
if ($httpReferer) {
foreach($referers as $referer) {
$referer = '`^'.str_replace(
array('.', '*'),
array('\\.', '.*'),
$referer).'$`i';
$continue = $continue || !!preg_match($referer, $httpReferer);
}
}
if (!$continue) {
header('HTTP/1.0 401 Unauthorized');
echo 'Unauthorized';
exit;
}
// Referers checks End
$params = $_GET;
$clb = $params['callback'];
unset($params['callback']);
unset($params['_']);
$fileCache = $cacheDir.DIRECTORY_SEPARATOR.md5(serialize($params)).'.json';
if (!file_exists($fileCache)) {
// File do not exists in cache, get it
$params['callback'] = $replaceClb;
if ($key)
$params['key'] = $key;
$url = $urlApi.'?'.http_build_query($params);
$content = file_get_contents($url);
file_put_contents($fileCache, $content);
} else {
$content = file_get_contents($fileCache);
}
// Send the json
header('Content-type: application/javascript');
echo str_replace($replaceClb, $clb, $content);