forked from claudex/olcc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.php
105 lines (90 loc) · 3.15 KB
/
backend.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
<?php /* vim: set ts=4 sw=4 noet ai : */
/* If $bDebug is true, append a line in the log file
to debug If-Modified-Since/Last-Modified handling. Do not
use in production environment. */
$bDebug = false ;
error_reporting(0);
$sVersion = '1.0.0' ;
$aHeaders = array( 'Accept: text/xml', 'Cache-Control: no-cache, must-revalidate' );
/* if (strpos($_REQUEST['url'], "bombefourchette")) die('[:kiki]'); */
/* Previous received "Last-Modified" HTTP response header
* returned by the server is sent back in a "If-Modified-Since"
* HTTP request header. */
if( isset( $_REQUEST['lastModified'] ) )
{
$aHeaders[] = 'If-Modified-Since: ' . $_REQUEST['lastModified'] ;
}
$rCurl = curl_init(
str_replace(
array( '{question}', '{amp}' ),
array( '?', '&' ),
$_REQUEST['url']
)
);
if( $rCurl === false )
{
die( 'Error: Unable to initialize cURL' );
}
curl_setopt( $rCurl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $rCurl, CURLOPT_HEADER, true );
curl_setopt( $rCurl, CURLOPT_HTTPHEADER, $aHeaders );
curl_setopt( $rCurl, CURLOPT_USERAGENT, "olcc-me/" . $sVersion );
curl_setopt( $rCurl, CURLOPT_CONNECTTIMEOUT, 5 );
curl_setopt( $rCurl, CURLOPT_TIMEOUT, 8 );
if( isset( $_REQUEST['cookie'] ) )
{
curl_setopt($rCurl, CURLOPT_COOKIE, $_REQUEST['cookie']);
}
$sMessage = curl_exec( $rCurl );
if ($sMessage === false)
{
die( 'Error: ' . curl_error( $rCurl ) );
}
header( 'HTTP/1.0 ' . curl_getinfo( $rCurl, CURLINFO_HTTP_CODE ) );
$sLastModified = null ;
$iHeaderLen = curl_getinfo( $rCurl, CURLINFO_HEADER_SIZE);
$aHeaders = explode( "\r\n", substr( $sMessage, 0, $iHeaderLen ) );
foreach( $aHeaders as $sHeader )
{
list( $sName, $sValue ) = explode( ":", $sHeader, 2 );
if( isset($sName) && isset($sValue) )
{
if( strtolower(trim($sName)) == 'last-modified' )
{
$sLastModified = trim( $sValue );
}
if( strtolower(trim($sName)) == 'content-type' )
{
$sContentType = trim( $sValue );
}
header( 'X-Olcc-' . trim($sName) . ':' . trim($sValue) );
}
}
$sBody = substr( $sMessage, $iHeaderLen );
if (0 === strpos($sContentType, "text/tab-separated-values")) {
header( 'Content-type: text/tab-separated-values');
echo $sBody;
} else {
header( 'Content-type: text/xml');
$iPos = strpos( $sBody, '<' );
if(( ! empty( $sBody) )&&( $iPos === false ))
{
echo 'Error: ' . $sBody ;
}
else
{
echo preg_replace( '/<!DOCTYPE[^>]*>/', '', substr( $sBody, $iPos ), 1 );
}
}
if( $bDebug )
{
error_log(
'OLCC - '
. $_REQUEST['url']
. ' : '
. 'If-Modified-Since: ' . ( isset( $_REQUEST['lastModified'] ) ? $_REQUEST['lastModified'] : "NULL" ) . ' ; '
. 'Last-Modified: ' . ( isset( $sLastModified ) ? $sLastModified : "NULL" ) . ' ; '
. 'Status: ' . curl_getinfo( $rCurl, CURLINFO_HTTP_CODE )
);
}
?>