forked from oomphinc/Web-Foundation
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsubmit.php
More file actions
93 lines (70 loc) · 2.51 KB
/
submit.php
File metadata and controls
93 lines (70 loc) · 2.51 KB
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
<?php
/**
* W3F Web Index Survey - Google Spreadsheets POST proxy
*
* Copyright (C) 2014 Ben Doherty @ Oomph, Inc.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
function fail( $message ) {
header( '401 Bad Request' );
header( 'Content-Type: text/plain' );
echo $message;
}
// Basic sanity check required GET parameters, pull them into namespace
foreach( array( 'accessToken', 'url', 'method' ) as $param ) {
if( !isset( $_GET[$param] ) || !is_string( $_GET[$param] ) || empty( $_GET[$param] ) ) {
fail( "Missing $param" );
}
$$param = $_GET[$param];
}
// Further sanity checks
if( strpos( $url, 'https://spreadsheets.google.com/feeds/') !== 0 ) {
fail( "Invalid URL" );
}
if( $method != 'POST' && $method != 'PUT' && $method != 'DELETE' ) {
fail( "Invalid method" );
}
$ch = curl_init( $url );
$http_headers = array(
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/atom+xml'
);
if( $method != 'DELETE' && count( $_POST ) > 0 ) {
$payload = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">';
foreach( $_POST as $var => $val ) {
$payload .= '<gsx:' . $var . '>' . htmlspecialchars( $val ) . '</gsx:' . $var . '>';
}
$payload .= '</entry>';
$http_headers[] = 'Content-Length: ' . strlen( $payload );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
}
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $method );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $http_headers );
$result = curl_exec( $ch );
if( !$result ) {
http_response_code( 503 );
exit( 0 );
}
list( $headers, $body ) = split( "\r\n\r\n", $result, 2 );
$headers = split( "\r\n", $headers );
foreach( $headers as $header ) {
list( $header_name, $header_val ) = split( ':', $header, 2 );
$headers[$header_name] = $header;
}
header( $headers[0] );
header( $headers['Content-Type'] );
echo $body;