Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

osz2 branch - cURL, module checks, status codes #3

Closed
wants to merge 3 commits into from

1 participant

@sp1ky

cURL support added.
Checks for modules and files added (in place of constants)
If a problem occurs, HTTP status code is returned instead of a blank exit()

@sp1ky sp1ky closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 100 additions and 79 deletions.
  1. +33 −19 mirror/a.php
  2. +5 −7 mirror/config.php
  3. +62 −53 mirror/d.php
View
52 mirror/a.php
@@ -1,31 +1,45 @@
-<?
+<?php
+/*
+ * Handle remote administration requests from main server.
+ */
+
require_once('config.php');
require_once('global.php');
-//Handle remote administration requests from main server.
-
-set_time_limit(300);
+set_time_limit(3600);
-//Read in request variables...
$mirrorId = (int)$_REQUEST['m'];
$key = $_REQUEST['k'];
$beatmapSetId = (int)$_REQUEST['s'];
$noVideo = $_REQUEST['n'] == '1';
-//Check for correct key...
-if ($key != SECRET_KEY)
+// Check for correct key...
+if ($_GET['k'] != SECRET_KEY)
+{
+ header('HTTP/1.0 403 Forbidden');
exit();
-
-set_time_limit(3600);
-
-if (!file_exists("mapsgohere/"))
- exec('mkdir mapsgohere');
-
-//wget is most reliable and memory-efficient.
-exec('wget ' . "\"http://osu.ppy.sh/d/$beatmapSetId?m=$mirrorId&k=" . SECRET_KEY . "\" -O " . FILES_DIRECTORY . $beatmapSetId);
-//if ($noVideo)
-// exec('wget ' . "\"http://osu.ppy.sh/d/$beatmapSetId"."n?m=$mirrorId&k=" . SECRET_KEY . "\" -O " . FILES_DIRECTORY . $beatmapSetId . 'n');
+}
+
+if(!file_exists(FILES_DIRECTORY))
+{
+ mkdir(FILES_DIRECTORY);
+}
+
+if(function_exists('curl_init'))
+{
+ $fp = fopen(FILES_DIRECTORY . $beatmapSetId, 'w+');
+ $ch = curl_init("http://osu.ppy.sh/d/" .$beatmapSetId . "?m=" . $mirrorId . "&k=" . SECRET_KEY);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 50);
+ curl_setopt($ch, CURLOPT_FILE, $fp);
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+ curl_exec($ch);
+ curl_close($ch);
+ fclose($fp);
+}
+ else
+{
+ exec('wget ' . "\"http://osu.ppy.sh/d/$beatmapSetId?m=$mirrorId&k=" . SECRET_KEY . "\" -O " . FILES_DIRECTORY . $beatmapSetId);
+}
echo("success");
-
-exit();
+exit();
View
12 mirror/config.php
@@ -1,7 +1,5 @@
-<?
- //configuration constants
- define(SECRET_KEY,'mirror_key_goes_here');
- define(FILES_DIRECTORY,'mapsgohere/');
- define(MAX_LINK_AGE,120);
- define(URL_FOPEN_SUPPORT,true);
-?>
+<?php
+ // configuration constants
+ define(SECRET_KEY, 'mirror_key_goes_here');
+ define(FILES_DIRECTORY, 'mapsgohere/');
+ define(MAX_LINK_AGE, 120);
View
115 mirror/d.php
@@ -1,8 +1,12 @@
-<?
+<?php
+/*
+ * Main download script
+ */
+
require_once('config.php');
require_once('global.php');
-//Read in request variables.
+// Read in request variables.
$noVideo = strpos($_REQUEST['s'],'n') !== false;
$filename = $_REQUEST['f'];
$receivedChecksum = $_REQUEST['c'];
@@ -11,73 +15,78 @@
$userId = (int)$_REQUEST['u'];
$filesize = (int) $_REQUEST['l'];
-$calculatedChecksum = md5($filename . $userId . $timestamp . SECRET_KEY);
-
-//Require a valid user...
-if ($userId == 0)
+// Require a valid user
+if ($userId == 0 || !is_numeric($userId))
+{
+ header('HTTP/1.0 403 Forbidden');
exit();
+}
-//Check incoming checksum...
-if ($receivedChecksum != $calculatedChecksum)
+// Compare checksums
+if ($receivedChecksum != md5($filename . $userId . $timestamp . SECRET_KEY))
+{
+ header('HTTP/1.0 403 Forbidden');
exit();
+}
-//Check link expiry.
-if (abs($timestamp - time()) > MAX_LINK_AGE)
+// Check link expiry.
+if (abs(time() - $timestamp) > MAX_LINK_AGE)
{
- echo "<head><META HTTP-EQUIV='REFRESH' CONTENT='1;url=http://osu.ppy.sh/s/$beatmapSetId'></head>";
- echo "This link has expired. Please go back a step and try again.";
+ header("Location: http://osu.ppy.sh/s/$beatmapSetId");
exit();
}
-if (URL_FOPEN_SUPPORT)
+// If we can support it, we notify the main server that the fulfillment was completed successfully.
+if(function_exists('fopen') && ini_get('allow_url_fopen'))
{
- //If we can support it, we notify the main server that the fulfillment was completed successfully.
- $handle = fopen("http://osu.ppy.sh/web/dl-check.php?k=".SECRET_KEY."&u=$userId&s=$beatmapSetId&t=$timestamp", "r");
+ $handle = fopen("http://osu.ppy.sh/web/dl-check.php?k=" . SECRET_KEY . "&u=" . $userId . "&s=" . $beatmapSetId . "&t=" . $timestamp, "r");
$success = fgets($handle);
fclose($handle);
-
- if (!$success)
- {
- echo "No download managers please!";
- exit();
- }
+}
+elseif(function_exists('curl_init'))
+{
+ $handle = curl_init("http://osu.ppy.sh/web/dl-check.php?k=" . SECRET_KEY . "&u=" . $userId . "&s=" . $beatmapSetId . "&t=" . $timestamp);
+ $success = curl_exec($handle);
+ curl_close($handle);
}
-$localFilename = FILES_DIRECTORY . $beatmapSetId ;
-
-//$filesize = filesize($localFilename);
+$localFilename = FILES_DIRECTORY . $beatmapSetId . ($noVideo ? 'n' : '');
-header("Content-Type: application/force-download");
-header("Content-Type: application/octet-stream");
-header("Content-Type: application/download");
-header("Content-Disposition: attachment;filename=\"$filename\";");
-header("Content-Transfer-Encoding: binary");
-header("Content-Length: $filesize");
+if (file_exists($localFilename))
+{
+ header("Content-Type: application/force-download");
+ header("Content-Type: application/octet-stream");
+ header("Content-Type: application/download");
+ header("Content-Disposition: attachment;filename=\"$filename\";");
+ header("Content-Transfer-Encoding: binary");
+ header("Content-Length: $filesize");
-//Set request timeout to 1 hour.
-set_time_limit(3600);
+ set_time_limit(3600);
-//KB per file read operation. Larger requires less IO seeking but uses more memory.
-$READ_CHUNK_SIZE = 512;
+ $READ_CHUNK_SIZE = 512;
-ob_implicit_flush(true);
-ob_end_flush();
+ ob_implicit_flush(true);
+ ob_end_flush();
-if($file = fopen($localFilename, 'rb'))
-{
- while(!feof($file) && !connection_aborted())
- {
- $pos = ftell($file);
- //in novideo mode filesize oould be smaller than the real filesize
- if ($noVideo && $pos >= ($filesize - 1024 * $READ_CHUNK_SIZE))
- {
- $bytesleft = $filesize - $pos;
- echo fread($file, $bytesleft);
- break;
- }
- echo fread($file, 1024 * $READ_CHUNK_SIZE);
- }
- fclose($file);
+ if($file = fopen($localFilename, 'rb'))
+ {
+ while(!feof($file) && !connection_aborted())
+ {
+ $pos = ftell($file);
+ //in novideo mode filesize oould be smaller than the real filesize
+ if ($noVideo && $pos >= ($filesize - 1024 * $READ_CHUNK_SIZE))
+ {
+ $bytesleft = $filesize - $pos;
+ echo fread($file, $bytesleft);
+ break;
+ }
+ echo fread($file, 1024 * $READ_CHUNK_SIZE);
+ }
+ fclose($file);
+ }
}
-
-exit();
+ else
+{
+ header('HTTP/1.0 404 Not Found');
+ exit();
+}
Something went wrong with that request. Please try again.