Skip to content

Instantly share code, notes, and snippets.

@SebbeJohansson
Created November 21, 2018 20:59
Show Gist options
  • Save SebbeJohansson/78dfaf706c15fbb4265c4b56ce3efd42 to your computer and use it in GitHub Desktop.
Save SebbeJohansson/78dfaf706c15fbb4265c4b56ce3efd42 to your computer and use it in GitHub Desktop.
<?php
$config = require('config.php');
$key = $config['secure_key'];
$uploadhost = $config['output_host'];
$uploadfolder = $config['output_folder'];
$redirect = $config['redirect_url'];
$giphy_key = $config['giphy_api_key'];
if ($_SERVER["REQUEST_URI"] == "/robot.txt") { die("User-agent: *\nDisallow: /"); }
function mime_content_type_custom($filename) {
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mp4' => 'video/mp4',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
else {
return 'application/octet-stream';
}
}
function executeCURL($url, $header = array(), $mode = "GET", $variables = array()){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $mode);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $variables);
$content = curl_exec($curl);
$array = json_decode($content, true);
return $array;
}
if (isset($_POST['key'])) {
if ($_POST['key'] == $key) {
$parts = explode(".", $_FILES["d"]["name"]);
$target = $_SERVER['DOCUMENT_ROOT'] . $uploadfolder . $_POST['name'] . "." . end($parts);
if (in_array(mime_content_type_custom($target), array("image/gif", "video/mp4"))) {
if (move_uploaded_file($_FILES['d']['tmp_name'], $target)) {
$target_parts = explode($uploadfolder, $target);
$localurl = $uploadhost . $uploadfolder . end($target_parts);
if($giphy_key != null){
$header = array(
"api_key: $giphy_key",
);
$body = array(
"source_image_url" => $localurl,
"source_post_url" => $localurl,
);
$response = executeCURL("upload.giphy.com/v1/gifs", $header, "POST", $body);
$giphyid = $response['data']['id'];
$gif = executeCURL("http://api.giphy.com/v1/gifs/$giphyid?api_key=$giphy_key");
echo $gif['data']['url'];
}else{
echo $localurl;
}
}else{
echo "File not moved successfully.";
}
}elseif (move_uploaded_file($_FILES['d']['tmp_name'], $target)) {
$target_parts = explode($uploadfolder, $target);
echo $uploadhost . $uploadfolder . end($target_parts);
} else {
echo "Sorry, there was a problem uploading your file. (Ensure your directory has 777 permissions)";
}
} else {
echo "WRONG KEY! GET THE FUCK OUT.";
}
} else {
echo "No key set.";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment