Skip to content

Instantly share code, notes, and snippets.

@ddarkr
Forked from Subtixx/steamworkshop.php
Created February 24, 2019 11:26
Show Gist options
  • Save ddarkr/99e6582d9256fa854cb557cc1fdcece2 to your computer and use it in GitHub Desktop.
Save ddarkr/99e6582d9256fa854cb557cc1fdcece2 to your computer and use it in GitHub Desktop.
Steam Workshop downloader
<?php
/*
* Usage Multiples:
* http://localhost/steamworkshop.php?ids[]=779297608&ids[]=468207596&ids[]=568872931&ids[]=898613611
* Usage Single:
* http://localhost/steamworkshop.php?id=779297608
*/
set_time_limit(0);
define("API_URL", "http://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v0001/");
function getWorkshopFileUrl($id)
{
$ch = curl_init(API_URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "itemcount=1&publishedfileids[0]=" . $id . "&format=json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$sharedfile = json_decode($output);
curl_close($ch);
if(!isset($sharedfile->response))
return null;
if(!isset($sharedfile->response->publishedfiledetails) || sizeof($sharedfile->response->publishedfiledetails) == 0)
return null;
return array(
"url" => $sharedfile->response->publishedfiledetails[0]->file_url,
"name" => $sharedfile->response->publishedfiledetails[0]->filename
);
}
function getWorkshopFileDetails($num, $ids)
{
$ch = curl_init(API_URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "itemcount=" . $num . "&" . $ids . "&format=json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$sharedfile = json_decode($output);
curl_close($ch);
if(!isset($sharedfile->response))
return null;
if(!isset($sharedfile->response->publishedfiledetails) || sizeof($sharedfile->response->publishedfiledetails) == 0)
return null;
return $sharedfile->response->publishedfiledetails;
}
function convertIdsToParameter($ids)
{
$res = array();
for ($i = 0; $i < sizeof($ids); $i++)
array_push($res, "publishedfileids[" . $i . "]=" . $ids[$i]);
return join("&", $res);
}
function downloadFileTemp($fileUrl, $fileName, $method = 0)
{
//$tempFileName = sys_get_temp_dir() . "/" . $fileName;
$tempFileName = "temp/" . $fileName;
if ($method == 0) {
if (!file_put_contents($tempFileName, fopen($fileUrl, 'r')))
throw new Exception("Downloading file failed.");
} elseif ($method == 1) {
$fp = fopen($tempFileName, "w");
$ch = curl_init();
$options = array(
CURLOPT_FILE => $fp,
CURLOPT_TIMEOUT => 28800,
CURLOPT_URL => $fileUrl,
);
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
return $tempFileName;
}
if (isset($_GET['id'])) {
$id = $_GET['id'];
if (!is_numeric($id))
exit("Unsupported ID.");
$file = getWorkshopFileUrl($id);
if($file == null)
die("File not found.");
$fileUrl = $file['url'];
$fileName = $file['name'];
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . $fileName . "\"");
if( ini_get('allow_url_fopen') ) {
readfile($fileUrl);
exit();
}else{
exit("Not supported.");
}
} elseif (isset($_GET['ids'])) {
$ids = $_GET['ids'];
if (!is_array($ids))
exit("Unsupported.");
$files = getWorkshopFileDetails(sizeof($ids), convertIdsToParameter($ids));
if($files == null)
die("Not supported.");
$fileNames = array();
foreach ($files as $file) {
$fileNames[] = downloadFileTemp($file->file_url, basename($file->filename));
}
$zipname = time() . "_" . $_SERVER['REMOTE_ADDR'] . ".zip";
$zip = new ZipArchive;
if(!$zip->open($zipname, ZipArchive::CREATE))
die("Error creating zip..");
foreach ($fileNames as $file) {
$zip->addFile($file, basename($file));
}
$zip->close();
foreach ($fileNames as $file) {
@unlink($file);
}
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
@unlink($zipname);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment