-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.php
75 lines (65 loc) · 2.21 KB
/
index.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
<?php
require 'vendor/autoload.php';
use PhpZip\ZipFile;
if ( !isset($_GET['url']) ) {
include 'homepage.html';
die();
}
$gh_url = 'https://github.com/' . $_GET['url'];
// Only download zip files.
$ch = curl_init($gh_url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Content-Type: application/zip');
// header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="github.zip"');
header('Pragma: no-cache');
$error_array = [];
if ($curl_info['content_type'] != 'application/octet-stream') {
array_push($error_array, '• Unexpected content type. Expected <i>application/octet-stream</i>, got <i>' . $curl_info['content_type'] . '</i>');
}
if ($curl_info['http_code'] != 200) {
array_push($error_array, '• Unexpected HTTP code. Expected <i>200</i>, got <i>' . $curl_info['http_code'] . '</i>');
}
// Not sure if this is a consistent/reliable header yet.
// if ($curl_info['download_content_length'] <= 0) {
// array_push($error_array, '• Empty file');
// }
if (empty($error_array)) {
header('Content-Length: ' . $curl_info['download_content_length'] );
readfile($gh_url);
} else {
// Return a plugin that displays the error message in the admin :)
$message = '';
foreach($error_array as $error) {
$message .= $error . '<br>';
}
$plugin = <<<PLUGIN
<?php
/*
Plugin Name: Error Message Plugin
Description: Temporary error message from Github Proxy for WP Playground
Author: Github Proxy
*/
function displayProxyErrorMessage() {
echo '<div class="notice notice-error"><p>The proxied file generated an error from Github.<br>';
echo '$message';
echo '</p></div>';
}
add_action('admin_notices', 'displayProxyErrorMessage');
?>
PLUGIN;
$zip = new ZipFile();
$zip->addFromString('error_plugin.php', $plugin);
$zip_data = $zip->outputAsString();
header('Content-Length: ' . strlen($zip_data));
echo $zip_data;
$zip->close();
}