Skip to content

Instantly share code, notes, and snippets.

@varunsridharan
Last active April 2, 2018 08:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varunsridharan/7f44aab3d0eae2cdc8367c1d7b4905af to your computer and use it in GitHub Desktop.
Save varunsridharan/7f44aab3d0eae2cdc8367c1d7b4905af to your computer and use it in GitHub Desktop.
ShareX Custom Uploader In PHP
<?php
date_default_timezone_set("Asia/Kolkata");
global $sharex_config ;
$sharex_config = array(
'upload_dir' => 'i',
'site_url' => 'http://varun.in/',
'salt_key' => 'some_key_here',
'folder_stucture' => 1, # 1 : year/month/date | 2: month/year/date | 3: date/month/year
'url_salt_key' => 'key',
'url_file_key' => 'd',
'url_file_name_key' => 'name',
'REQUEST_METHOD' => 'POST',
'no_salt_key' => "Invalid Auth Key. Provide A Valid Auth Key",
'upload_filed' => "Something Went Wrong !! Please Try Again ! later",
'file_deleted' => 'File Deleted Successfully',
'already_file_deleted' => 'File Already Deleted / File Does Not Exists',
'file_not_exists' => 'File Doesn\'t Exists. Or Deleted By The Uploader',
'url_shorturl_key_path' => 'url',
'folder_stuctures' => array(
1 => array(date('Y'),date('M'),date('d')),
2 => array(date('Y'),date('M'),date('d')),
3 => array(date("YMd")),
),
);
class VS_ShareX_Uploader {
public function __construct() {
global $sharex_config;
$this->save_path = '';
$this->save_abs_path = '';
$this->settings = $sharex_config;
$this->init();
}
public function create_html_file($path){
$contents = '<meta http-equiv="refresh" content="0; url=http://varunsridharan.in/"/> <h4>Redirecting.....</h4>';
$file_name = 'index.html';
if(!file_exists($path.$file_name)){
file_put_contents($path.$file_name, $contents);
}
}
public function get_method(){
$m = $this->settings['REQUEST_METHOD'];
if($m == 'REQUEST'){return $_REQUEST;}
if($m == 'GET'){return $_GET;}
return $_POST;
}
public function get_settings($key = ''){
if(isset($this->settings[$key])){
return $this->settings[$key];
} else {
return false;
}
}
public function error_msg($key){
echo $this->get_settings($key);
exit;
}
public function get_folder_stucture(){
$folder_type = $this->get_settings('folder_stucture');
$folders = $this->get_settings("folder_stuctures");
if(!isset($folders[$folder_type])){
return array(date('Y'),date('M'),date('d'));
}
return $folders[$folder_type];
}
public function verify_dir_path(){
$main_path = __DIR__.'/';
$folder_path = $this->get_settings("upload_dir").'/';
if(!file_exists($main_path.$folder_path)){
mkdir($main_path.$folder_path);
}
$this->create_html_file($main_path.$folder_path);
$save_type = $this->get_folder_stucture();
foreach($save_type as $folder){
$folder_path .= $folder.'/';
if(!file_exists($main_path.$folder_path)){
mkdir($main_path.$folder_path);
}
$this->create_html_file($main_path.$folder_path);
}
$this->save_path = $folder_path;
$this->save_abs_path = $main_path.$folder_path;
}
public function save_upload_file($file_name = ''){
if(isset($_FILES[$this->get_settings("url_file_key")])){
$file = $_FILES[$this->get_settings("url_file_key")];
$temp_file = $file['tmp_name'];
$parts = explode(".", $file["name"]);
$mfile = $file_name.rand(1,100);
$target = $this->save_abs_path.$mfile.'.'.end($parts);
if (move_uploaded_file($temp_file, $target)) {
$this->save_file_name = $mfile.'.'.end($parts);
$this->actual_file_name = $file_name.'.'.end($parts);
} else {
$this->error_msg("upload_failed");
}
}
}
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function base64url_decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
public function get_short_url($url){
$url = urlencode($url);
$surl = file_get_contents('http://s.svarun.in/api/?url='.$url);
$surl = json_decode($surl,true);
return isset($surl['short']) ? $surl['short'] : false;
}
public function init(){
$data = array();
$M = $this->get_method();
if(!isset($_GET['view'])){
if(isset($M[$this->get_settings("url_salt_key")])){
if(isset($M[$this->get_settings("url_shorturl_key_path")])){
$url = $M[$this->get_settings("url_shorturl_key_path")];
$short = $this->get_short_url($url);
$data = array('view' => $short);
} else {
$file_name = isset($M[$this->get_settings("url_file_name_key")]) ? $M[$this->get_settings("url_file_name_key")] : time();
$this->verify_dir_path();
$this->save_upload_file($file_name);
$urlName = $this->base64url_encode($this->save_path.$this->save_file_name);
$view_url = $this->get_settings("site_url").$urlName.'.html';
$view_url = $this->get_short_url($view_url);
$delete_url = $this->get_settings("site_url").'delete-'.$urlName.'.html';
$data = array(
'view' => $view_url,
'delete' => $delete_url,
);
}
echo json_encode($data);
//echo $this->get_settings("site_url").$urlName.'.html';
exit;
} else {
$this->error_msg('no_salt_key');
}
} else {
$name = explode("-",$_GET['view']);
if($name[0] == 'delete'){
$image = $this->base64url_decode($name[1]);
if(file_exists(__DIR__.'/'.$image)){
unlink(__DIR__.'/'.$image);
$this->error_msg('file_deleted');
} else {
$this->error_msg('already_file_deleted');
}
} else{
$image = $this->base64url_decode($name[0]);
if(!empty($image)){
if(file_exists(__DIR__.'/'.$image)){
$size = getimagesize($image);
$fp = fopen($image, 'rb');
header('Content-Length: '.filesize($image));
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($image)).' GMT');
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && filemtime($image) == strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
header('HTTP/1.1 304 Not Modified');
}
if(is_array($size)){
if ($size and $fp) {
header('Content-Type: '.$size['mime']);
}
} else {
header('Content-Type: text/plain');
}
fpassthru($fp);
exit;
} else {
$this->error_msg('file_not_exists');
}
} else {
$this->error_msg('file_not_exists');
}
}
}
}
}
new VS_ShareX_Uploader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment