Browse Source

Creation of the world

Chouchen 6 years ago
commit
08d7614cb5
12 changed files with 499 additions and 0 deletions
  1. 22
    0
      .gitattributes
  2. 163
    0
      .gitignore
  3. 15
    0
      .htaccess
  4. 54
    0
      FileToZip.php
  5. 42
    0
      FileToZipDispatcher.php
  6. 37
    0
      FileToZipEmail.php
  7. 50
    0
      ToDownloadIndex.php
  8. 22
    0
      download.php
  9. 26
    0
      index.html
  10. 37
    0
      index.php
  11. 31
    0
      shortcut.php
  12. 0
    0
      todl.txt

+ 22
- 0
.gitattributes View File

@@ -0,0 +1,22 @@
1
+# Auto detect text files and perform LF normalization
2
+* text=auto
3
+
4
+# Custom for Visual Studio
5
+*.cs     diff=csharp
6
+*.sln    merge=union
7
+*.csproj merge=union
8
+*.vbproj merge=union
9
+*.fsproj merge=union
10
+*.dbproj merge=union
11
+
12
+# Standard to msysgit
13
+*.doc	 diff=astextplain
14
+*.DOC	 diff=astextplain
15
+*.docx diff=astextplain
16
+*.DOCX diff=astextplain
17
+*.dot  diff=astextplain
18
+*.DOT  diff=astextplain
19
+*.pdf  diff=astextplain
20
+*.PDF	 diff=astextplain
21
+*.rtf	 diff=astextplain
22
+*.RTF	 diff=astextplain

+ 163
- 0
.gitignore View File

@@ -0,0 +1,163 @@
1
+#################
2
+## Eclipse
3
+#################
4
+
5
+*.pydevproject
6
+.project
7
+.metadata
8
+bin/
9
+tmp/
10
+*.tmp
11
+*.bak
12
+*.swp
13
+*~.nib
14
+local.properties
15
+.classpath
16
+.settings/
17
+.loadpath
18
+
19
+# External tool builders
20
+.externalToolBuilders/
21
+
22
+# Locally stored "Eclipse launch configurations"
23
+*.launch
24
+
25
+# CDT-specific
26
+.cproject
27
+
28
+# PDT-specific
29
+.buildpath
30
+
31
+
32
+#################
33
+## Visual Studio
34
+#################
35
+
36
+## Ignore Visual Studio temporary files, build results, and
37
+## files generated by popular Visual Studio add-ons.
38
+
39
+# User-specific files
40
+*.suo
41
+*.user
42
+*.sln.docstates
43
+
44
+# Build results
45
+[Dd]ebug/
46
+[Rr]elease/
47
+*_i.c
48
+*_p.c
49
+*.ilk
50
+*.meta
51
+*.obj
52
+*.pch
53
+*.pdb
54
+*.pgc
55
+*.pgd
56
+*.rsp
57
+*.sbr
58
+*.tlb
59
+*.tli
60
+*.tlh
61
+*.tmp
62
+*.vspscc
63
+.builds
64
+*.dotCover
65
+
66
+## TODO: If you have NuGet Package Restore enabled, uncomment this
67
+#packages/
68
+
69
+# Visual C++ cache files
70
+ipch/
71
+*.aps
72
+*.ncb
73
+*.opensdf
74
+*.sdf
75
+
76
+# Visual Studio profiler
77
+*.psess
78
+*.vsp
79
+
80
+# ReSharper is a .NET coding add-in
81
+_ReSharper*
82
+
83
+# Installshield output folder
84
+[Ee]xpress
85
+
86
+# DocProject is a documentation generator add-in
87
+DocProject/buildhelp/
88
+DocProject/Help/*.HxT
89
+DocProject/Help/*.HxC
90
+DocProject/Help/*.hhc
91
+DocProject/Help/*.hhk
92
+DocProject/Help/*.hhp
93
+DocProject/Help/Html2
94
+DocProject/Help/html
95
+
96
+# Click-Once directory
97
+publish
98
+
99
+# Others
100
+[Bb]in
101
+[Oo]bj
102
+sql
103
+TestResults
104
+*.Cache
105
+ClientBin
106
+stylecop.*
107
+~$*
108
+*.dbmdl
109
+Generated_Code #added for RIA/Silverlight projects
110
+
111
+# Backup & report files from converting an old project file to a newer
112
+# Visual Studio version. Backup files are not needed, because we have git ;-)
113
+_UpgradeReport_Files/
114
+Backup*/
115
+UpgradeLog*.XML
116
+
117
+
118
+
119
+############
120
+## Windows
121
+############
122
+
123
+# Windows image file caches
124
+Thumbs.db
125
+
126
+# Folder config file
127
+Desktop.ini
128
+
129
+
130
+#############
131
+## Python
132
+#############
133
+
134
+*.py[co]
135
+
136
+# Packages
137
+*.egg
138
+*.egg-info
139
+dist
140
+build
141
+eggs
142
+parts
143
+bin
144
+var
145
+sdist
146
+develop-eggs
147
+.installed.cfg
148
+
149
+# Installer logs
150
+pip-log.txt
151
+
152
+# Unit test / coverage reports
153
+.coverage
154
+.tox
155
+
156
+#Translations
157
+*.mo
158
+
159
+#Mr Developer
160
+.mr.developer.cfg
161
+
162
+# Mac crap
163
+.DS_Store

+ 15
- 0
.htaccess View File

@@ -0,0 +1,15 @@
1
+<Files .htaccess>
2
+order allow,deny
3
+deny from all
4
+</Files>
5
+
6
+Options -Indexes
7
+Options +FollowSymLinks
8
+ 
9
+RewriteEngine On
10
+RewriteBase /
11
+RewriteRule download index.php?action=download [QSA,L]
12
+RewriteRule ^$ index.php?action=form [QSA,L]
13
+RewriteRule ^(.*+)$ index.php?action=shortcut&sc=$1 [QSA,L]
14
+
15
+DirectoryIndex filename.html

+ 54
- 0
FileToZip.php View File

@@ -0,0 +1,54 @@
1
+<?php
2
+/**
3
+ * Class for the downloaded and zipped file 
4
+ * @author Shikiryu
5
+ */
6
+class FileToZip{
7
+	private $_fileName 		= ''; // original file name
8
+	private $_finalFileName = ''; // zipped file name
9
+	private $_filePath 		= ''; // original URL
10
+	private $_fileContent	= null; // content of the original file
11
+	private $_zipFile		= null; // ZipArchive file
12
+	
13
+	public function __construct($path){
14
+		$this->_filePath 		= $path;
15
+		$this->_fileName 		= end(explode('/',$this->_filePath));
16
+		$this->_finalFileName 	= $this->_fileName.'.zip';
17
+		
18
+		// create a new CURL resource 
19
+		$ch = curl_init(); 
20
+		
21
+		// set URL and other appropriate options 
22
+		curl_setopt($ch, CURLOPT_URL, $this->_filePath); //set an url
23
+		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //do not output directly, use variable
24
+		curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); //do a binary transfer
25
+		curl_setopt($ch, CURLOPT_FAILONERROR, 1); //stop if an error occurred
26
+		
27
+		$this->_fileContent = curl_exec($ch); //store the content in variable
28
+		// close CURL resource, and free up system resources 
29
+		curl_close($ch); 
30
+	}
31
+
32
+	/**
33
+	 * Create a zip with the original file in it
34
+	 * @return null or $this
35
+	 */
36
+	public function build(){
37
+		$this->_zipFile = new ZipArchive;
38
+		$res = $this->_zipFile->open($this->_finalFileName, ZipArchive::CREATE);
39
+		if ($res === TRUE) {
40
+			$this->_zipFile->addFromString($this->_fileName, $this->_fileContent);
41
+			$this->_zipFile->close();
42
+			return $this;
43
+		} else {
44
+			return null;
45
+		}
46
+	}
47
+	
48
+	/**
49
+	 * just a getter
50
+	 */
51
+	public function getFinalFileName(){
52
+		return $this->_finalFileName;
53
+	}
54
+}

+ 42
- 0
FileToZipDispatcher.php View File

@@ -0,0 +1,42 @@
1
+<?php
2
+/**
3
+ * Class to dispatch the zipped file with the given method
4
+ * @author Shikiryu
5
+ */
6
+class FileToZipDispatcher{
7
+	private $_method = array();
8
+	private $_zip;
9
+	
10
+	public function __construct(array $method, FileToZip $zip){
11
+		$this->_method = $method;
12
+		$this->_zip = $zip;
13
+	}
14
+	
15
+	public function dispatch($options){
16
+		foreach($this->_method as $method){
17
+			if(method_exists($this, '_'.$method)) { 
18
+				return call_user_func(array($this, '_'.$method), $options); 
19
+			}else{ 
20
+				throw new Exception(sprintf('The required method "%s" does not exist for %s', $method, get_class($this))); 
21
+			} 
22
+		}
23
+		if(!in_array('toDownloadLater', $this->_method)){
24
+			unlink($this->_zip->getFinalFileName());
25
+		}
26
+	}
27
+	
28
+	private function _toDownloadNow($options){
29
+		header("Content-type: application/zip");
30
+		header("Content-Disposition: attachment; filename=".$this->_zip->getFinalFileName());
31
+		header("Pragma: no-cache");
32
+		header("Expires: 0");
33
+		readfile($this->_zip->getFinalFileName());
34
+	}
35
+	
36
+	private function _toDownloadLater($options){
37
+		$index = new ToDownloadIndex();
38
+		$shortcut = $index->saveNewLine($this->_zip->getFinalFileName());
39
+		$email = new FileToZipEmail($options['email'], $shortcut);
40
+		$email->send();
41
+	}	
42
+}

+ 37
- 0
FileToZipEmail.php View File

@@ -0,0 +1,37 @@
1
+<?php
2
+/**
3
+ * Class for sending the hashed link into the given email
4
+ * @author Shikiryu
5
+ */
6
+class FileToZipEmail{
7
+	private $_email; // email obviously
8
+	private $_shortcut; // shortcut (uniqid)
9
+	
10
+	public function __construct($email, $zip){
11
+		$this->_email = $email;
12
+		$this->_shortcut = $zip;
13
+		$this->_protection();
14
+	}
15
+	
16
+	/**
17
+	 * Prevent hackingn see php doc
18
+	 */
19
+	private function _protection(){
20
+		$email = trim($this->_email);
21
+		if ((strpos ($email,"\\r")!==false) || 
22
+			(strpos ($email,"\\n")!==false) || 
23
+			(stripos ($email,"Content-Transfer-Encoding")!==false) || 
24
+			(stripos ($email,"MIME-Version")!==false) || 
25
+			(stripos ($email,"Content-Type")!==false) || 
26
+			(empty($_SERVER['HTTP_USER_AGENT']))) 
27
+				die('Incorrect request') ; //stop spammers 
28
+		mail('shikiryu@gmail.com', '[SPAMMER] someone is trying to hack you.', "Hello, \n\n".$this->_email." tried to hack you.\n\nBye o/");
29
+	}
30
+	
31
+	/**
32
+	 * obvious
33
+	 */
34
+	public function send(){
35
+		mail($this->_email, 'A new file to download', "Hello, \n\nYou can download a new file here : ".BASE_URL."/".$this->_shortcut."\n\nBye o/");
36
+	}
37
+}

+ 50
- 0
ToDownloadIndex.php View File

@@ -0,0 +1,50 @@
1
+<?php
2
+/**
3
+ * Class managing todl.txt (file which match zipped file to a uniqid
4
+ * @author Shikiryu
5
+ */
6
+class ToDownloadIndex{
7
+	const FILE = 'todl.txt';
8
+	private $_content = '';
9
+	private $_lines = array();
10
+	
11
+	public function __construct(){
12
+		$this->_content = file_get_contents(dirname(__FILE__).'/'.self::FILE);
13
+		$lines = explode("\n", $this->_content);
14
+		foreach($lines as $line){
15
+			$parts = explode(' => ', $line);
16
+			if(count($parts) == 2) $this->_lines[$parts[0]] = $parts[1];
17
+		}
18
+	}
19
+	
20
+	public function saveNewLine($url, $shortcut = ''){
21
+		if(empty($shortcut)) $shortcut = $this->_generateUID();
22
+		$this->_lines[$shortcut] = $url;
23
+		$this->_rebuildFile();
24
+		return $shortcut;
25
+	}
26
+	
27
+	public function readLine($shortcut){
28
+		return array_key_exists($shortcut, $this->_lines) ? $this->_lines[$shortcut] : '';
29
+	}
30
+	
31
+	public function deleteLine($shortcut){
32
+		if(array_key_exists($shortcut, $this->_lines)){
33
+			unset($this->_lines[$shortcut]);
34
+		}
35
+		$this->_rebuildFile();
36
+	}	
37
+	
38
+	private function _rebuildFile(){
39
+		$stringBuffer = '';
40
+		foreach($this->_lines as $shortcut => $url){
41
+			$stringBuffer .= $shortcut.' => '.$url."\n";
42
+		}
43
+		$this->_content = $stringBuffer;
44
+		file_put_contents(dirname(__FILE__).'/'.self::FILE, $stringBuffer);
45
+	}
46
+	
47
+	private function _generateUID(){
48
+		return uniqid();
49
+	}
50
+}

+ 22
- 0
download.php View File

@@ -0,0 +1,22 @@
1
+<?php
2
+if(isset($_POST['url']) && isset($_POST['method'])){
3
+	
4
+	$file2zip = new FileToZip(filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL));
5
+	
6
+	$method = filter_has_var(INPUT_POST, 'method') ? is_array($_POST['method']) ? $_POST['method'] : array($_POST['method']) : null;
7
+	
8
+	$fileDispatcher = new FileToZipDispatcher($method, $file2zip->build());
9
+	
10
+	$options = $_REQUEST;
11
+	unset($options['method']);
12
+	unset($options['url']);
13
+	
14
+	$fileDispatcher->dispatch($options);
15
+	
16
+	if(!in_array('toDownloadNow', $method)){
17
+		header('Location: '.BASE_URL);
18
+		exit;
19
+	}
20
+}else{
21
+	header('Location: '.BASE_URL);
22
+}

+ 26
- 0
index.html View File

@@ -0,0 +1,26 @@
1
+<!DOCTYPE html>
2
+<html>
3
+	<head>
4
+		<title>Shikiryu's exe to zip download proxy</title>
5
+		<style>
6
+			#emailWrapper{display:none;}
7
+		</style>
8
+		<script type="text/javascript">
9
+			function toggleEmailWrapper(elt){
10
+				document.getElementById('emailWrapper').style.display = elt.checked ? "inline-block" : "none";
11
+			}
12
+		</script>
13
+	</head>
14
+	<body>
15
+		<form action="download" method="post">
16
+			<fieldset>
17
+				<legend>File to proxy</legend>
18
+				<label for="url">URL : </label><input type="text" name="url" id="url" />
19
+				<label for="toDownloadNow">Download now : </label><input type="checkbox" name="method[]" id="toDownloadNow" value="toDownloadNow" checked="checked" />
20
+				<label for="toDownloadLater">Download later : </label><input onclick="toggleEmailWrapper(this)" type="checkbox" name="method[]" id="toDownloadLater" value="toDownloadLater" />
21
+				<div id="emailWrapper"><label for="email">email for the link : </label><input type="email" name="email" id="email" /></div>
22
+				<input type="submit" value="download"/>
23
+			</fieldset>
24
+		</form>
25
+	</body>
26
+</html>

+ 37
- 0
index.php View File

@@ -0,0 +1,37 @@
1
+<?php
2
+// No display, log everything
3
+error_reporting(E_ALL);
4
+ini_set('display_errors', 0);
5
+ini_set('log_errors', 1);
6
+ini_set('error_log',dirname(__FILE__).'/error.log');
7
+
8
+// include everything
9
+include_once 'FileToZip.php';
10
+include_once 'FileToZipDispatcher.php';
11
+include_once 'ToDownloadIndex.php';
12
+include_once 'FileToZipEmail.php';
13
+
14
+$action = filter_input(INPUT_GET, 'action');
15
+$possibleActions = array('download', 'form', 'shortcut');
16
+
17
+define('BASE_URL', 'http://mywebsite.com');
18
+
19
+if($action == null || !in_array($action, $possibleActions)){
20
+	header('HTTP/1.0 404 Not Found');
21
+	exit;
22
+}
23
+
24
+if($action == 'form'){
25
+	include_once 'index.html';
26
+	exit;
27
+}
28
+
29
+if($action == 'download'){
30
+	include_once 'download.php';
31
+	exit;
32
+}
33
+
34
+if($action == 'shortcut'){
35
+	include_once 'shortcut.php';
36
+	exit;
37
+}

+ 31
- 0
shortcut.php View File

@@ -0,0 +1,31 @@
1
+<?php
2
+/**
3
+We need to find the matching file thanks to the todl.txt file
4
+Read it, send it then delete it from the txt file
5
+*/
6
+$idx = new ToDownloadIndex();
7
+
8
+$shortcut = !empty($_GET['sc']) ? $_GET['sc'] : '';
9
+
10
+if($shortcut == ''){
11
+	header('HTTP/1.0 404 Not Found');
12
+	exit;
13
+}
14
+
15
+$file = $idx->readLine($shortcut);
16
+
17
+if($file == null || !is_readable(dirname(__FILE__).'/'.$file)){
18
+	header('HTTP/1.0 404 Not Found');
19
+	exit;
20
+}
21
+
22
+header("Content-type: application/zip");
23
+header("Content-Disposition: attachment; filename=".$file);
24
+header("Pragma: no-cache");
25
+header("Expires: 0");
26
+readfile(dirname(__FILE__).'/'.$file);
27
+unlink(dirname(__FILE__).'/'.$file);
28
+
29
+$idx->deleteLine($shortcut);
30
+
31
+exit;

+ 0
- 0
todl.txt View File


Loading…
Cancel
Save