summaryrefslogtreecommitdiffstats
path: root/system/application/models/file_mod.php
blob: 20ebe58f4dbc7acd8471cf336f2412f24c08d858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/*
 * Copyright 2009-2010 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under GPLv3
 * (see COPYING for full license text)
 *
 */

class File_mod extends Model {

	function __construct()
	{
		parent::Model();
	}

	// Returns an unused ID
	// TODO: make threadsafe
	function new_id()
	{
		$id = $this->random_id(3,6);

		if ($this->id_exists($id) || $id == 'file') {
			return $this->new_id();
		} else {
			return $id;
		}
	}

	function id_exists($id)
	{
		if(!$id) {
			return false;
		}

		$sql = '
			SELECT id
			FROM `files`
			WHERE `id` = ?
			LIMIT 1';
		$query = $this->db->query($sql, array($id));

		if ($query->num_rows() == 1) {
			return true;
		} else {
			return false;
		}
	}

	function get_filedata($id)
	{
		$sql = '
			SELECT hash,filename,mimetype
			FROM `files`
			WHERE `id` = ?
			LIMIT 1';
		$query = $this->db->query($sql, array($id));

		if ($query->num_rows() == 1) {
			$return = $query->result_array();
			return $return[0];
		} else {
			return false;
		}
	}

	// return the folder in which the file with $hash is stored
	function folder($hash) {
		return $this->config->item('upload_path').'/'.substr($hash, 0, 3);
	}

	// Returns the full path to the file with $hash
	function file($hash) {
		return $this->folder($hash).'/'.$hash;
	}

	function hash_password($password)
	{
		return sha1($this->config->item('passwordsalt').$password);
	}

	// Returns the password submitted by the user
	function get_password()
	{
		$password = $this->input->post('password');
		if ($password !== false) {
			return $this->hash_password($password);
		} elseif (isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_PW'] != '') {
			return $this->hash_password($_SERVER['PHP_AUTH_PW']);
		}
		return 'NULL';
	}

	// Add a hash to the DB
	// TODO: Should only update not insert; see new_id()
	function add_file($hash, $id, $filename)
	{
		$mimetype = exec(FCPATH.'scripts/mimetype -b --orig-name '.escapeshellarg($filename).' '.escapeshellarg($this->file($hash)));
		$query = $this->db->query('
			INSERT INTO `files` (`hash`, `id`, `filename`, `password`, `date`, `mimetype`)
			VALUES (?, ?, ?, ?, ?, ?)',
			array($hash, $id, $filename, $this->get_password(), time(), $mimetype));
	}

	function show_url($id, $mode)
	{
		$data = array();
		$redirect = false;

		if ($mode) {
			$data['url'] = site_url($id).'/'.$mode;
		} else {
			$data['url'] = site_url($id).'/';

			$filedata = $this->get_filedata($id);
			$file = $this->file($filedata['hash']);
			$type = $filedata['mimetype'] ? $filedata['mimetype'] : exec(FCPATH.'scripts/mimetype -b --orig-name '.escapeshellarg($filedata['filename']).' '.escapeshellarg($file));
			$mode = $this->mime2extension($type);
			$mode = $this->filename2extension($filedata['filename']) ? $this->filename2extension($filedata['filename']) : $mode;

			// If we detected a highlightable file redirect,
			// otherwise show the URL because browsers would just show a DL dialog
			if ($mode) {
				$redirect = true;
			}
		}

		if ($this->var->cli_client) {
			echo $data['url']."\n";
		} else {
			if ($redirect) {
				redirect($data['url']);
			} else {
				$this->load->view('file/header', $data);
				$this->load->view('file/show_url', $data);
				$this->load->view('file/footer', $data);
			}
		}
	}

	// download a given ID
	// TODO: make smaller
	function download()
	{
		$data = array();
		$id = $this->uri->segment(1);
		$mode = $this->uri->segment(2);

		$filedata = $this->get_filedata($id);
		$file = $this->file($filedata['hash']);

		if ($this->id_exists($id) && file_exists($file)) {
			// MODIFIED SINCE SUPPORT -- START
			// helps to keep traffic low when reloading an image
			$filedate = filectime($file);
			$etag = strtolower(md5_file($file));
			$modified = true;

			// No need to check because different files have different IDs/hashes
			if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
					$modified = false;
			}

			if(isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
				$oldtag = trim(strtolower($_SERVER['HTTP_IF_NONE_MATCH']), '"');
				if($oldtag == $etag) {
					$modified = false;
				} else {
					$modified = true;
				}
			}
			// MODIFIED SINCE SUPPORT -- END

			if (!$modified) {
				header("HTTP/1.1 304 Not Modified");
				header('Etag: "'.$etag.'"');
			} else {
				$type = $filedata['mimetype'] ? $filedata['mimetype'] : exec(FCPATH.'scripts/mimetype -b --orig-name '.escapeshellarg($filedata['filename']).' '.escapeshellarg($file));

				// /$mode at the end of the URL overwrites autodetection
				if (!$mode && substr_count(ltrim($this->uri->uri_string(), "/"), '/') >= 1) {
					$mode = $this->mime2extension($type);
					$mode = $this->filename2extension($filedata['filename']) ? $this->filename2extension($filedata['filename']) : $mode;
				}

				// TODO: cleanup conditions
				if ($mode && $mode != 'plain' && $mode != 'qr'
				&& $this->mime2extension($type)
				&& filesize($file) <= $this->config->item('upload_max_text_size')
				) {
					$data['title'] = $filedata['filename'];
					$data['raw_link'] = site_url($id);
					$data['new_link'] = site_url();
					$data['plain_link'] = site_url($id.'/plain');
					$data['auto_link'] = site_url($id).'/';
					$data['rmd_link'] = site_url($id.'/rmd');

					header("Content-Type: text/html\n");
					if ($mode) {
						$data['current_highlight'] = $mode;
					} else {
						$data['current_highlight'] = $this->mime2extension($type);
					}
					echo $this->load->view('file/html_header', $data, true);
					$this->load->library("MemcacheLibrary");
					if (! $cached = $this->memcachelibrary->get($filedata['hash'].'_'.$mode)) {
						ob_start();
						if ($mode == "rmd") {
								echo '<td class="markdownrender">'."\n";
								passthru('/usr/bin/perl /usr/bin/perlbin/vendor/Markdown.pl '.escapeshellarg($file));
						} else {
							echo '<td class="numbers"><pre>';
							// generate line numbers (links)
							passthru('/usr/bin/perl -ne \'print "<a href=\"#n$.\" class=\"no\" id=\"n$.\">$.</a>\n"\' '.escapeshellarg($file));
							echo '</pre></td><td class="code">'."\n";
							$this->load->library('geshi');
							$this->geshi->initialize(array('set_language' => $mode, 'set_source' => file_get_contents($file), 'enable_classes' => 'true'));
							echo $this->geshi->output();
						}
						$cached = ob_get_contents();
						ob_end_clean();
						$this->memcachelibrary->set($filedata['hash'].'_'.$mode, $cached, 100);
					}
					echo $cached;
					echo $this->load->view('file/html_footer', $data, true);
				} else {
					if ($mode == 'plain') {
						header("Content-Type: text/plain\n");
					} elseif ($mode == "qr") {
						header("Content-disposition: inline; filename=\"".$id."_qr.png\"\n");
						header("Content-Type: image/png\n");
						passthru('/usr/bin/qrencode -s 10 -o - '.escapeshellarg(site_url($id).'/'));
						exit();
					} else {
						header("Content-Type: ".$type."\n");
					}
					header("Content-disposition: inline; filename=\"".$filedata['filename']."\"\n");
					header("Content-Length: ".filesize($file)."\n");
					header("Last-Modified: ".date('D, d M Y H:i:s', $filedate)." GMT");
					header('Etag: "'.$etag.'"');
					$fp = fopen($file,"r");
					while (!feof($fp)) {
						echo fread($fp,4096);
					}
					fclose($fp);
				}
			}
			exit();
		} else {
			// TODO: remove -controller function has been removed
			$this->load->view('file/header');
			$this->load->view('file/non_existant');
			$this->load->view('file/footer');
		}
	}

	private function unused_file($hash)
	{
		$sql = '
			SELECT id
			FROM `files`
			WHERE `hash` = ?
			LIMIT 1';
		$query = $this->db->query($sql, array($hash));

		if ($query->num_rows() == 0) {
			return true;
		} else {
			return false;
		}
	}

	function delete_id($id, $password)
	{
		$filedata = $this->get_filedata($id);
		$password = $this->get_password();

		if ($password == "NULL") {
			return false;
		}

		if(!$this->id_exists($id)) {
			return false;
		}

		$sql = '
			DELETE
			FROM `files`
			WHERE `id` = ?
			AND password = ?
			LIMIT 1';
		$this->db->query($sql, array($id, $password));

		if($this->id_exists($id))  {
			return false;
		}

		if($this->unused_file($filedata['hash'])) {
			unlink($this->file($filedata['hash']));
			@rmdir($this->folder($filedata['hash']));
		}
		return true;
	}

	// Generate a random ID
	private function random_id($min_length, $max_length)
	{
		$random = '';
		$char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$char_list .= "abcdefghijklmnopqrstuvwxyz";
		$char_list .= "1234567890";
		$length = rand()%($max_length-$min_length) + $min_length;

		for($i = 0; $i < $max_length; $i++) {
			if (strlen($random) == $length) break;
			$random .= substr($char_list,(rand()%(strlen($char_list))), 1);
		}
		return $random;
	}

	// Map MIME types to extensions needed for highlighting
	function mime2extension($type)
	{
		$typearray = array(
		'text/plain' => 'text',
		'text/x-python' => 'python',
		'text/x-csrc' => 'c',
		'text/x-chdr' => 'c',
		'text/x-c++hdr' => 'c',
		'text/x-c++src' => 'cpp',
		'text/x-patch' => 'diff',
		'text/x-lua' => 'lua',
		'text/x-java' => 'java',
		'text/x-haskell' => 'haskell',
		'text/x-literate-haskell' => 'haskell',
		'text/x-subviewer' => 'bash',
		'text/x-makefile' => 'make',
		#'text/x-log' => 'log',
		'text/html' => 'html',
		'text/css' => 'css',
		'message/rfc822' => 'email',
		#'image/svg+xml' => 'xml',
		'application/x-perl' => 'perl',
		'application/xml' => 'xml',
		'application/javascript' => 'javascript',
		'application/x-desktop' => 'text',
		'application/x-m4' => 'text',
		'application/x-awk' => 'text',
		'application/x-java' => 'java',
		'application/x-php' => 'php',
		'application/x-ruby' => 'ruby',
		'application/x-shellscript' => 'bash',
		'application/x-x509-ca-cert' => 'text',
		'application/mbox' => 'email',
		'application/x-genesis-rom' => 'text',
		'application/x-applix-spreadsheet' => 'actionscript'
		);
		if (array_key_exists($type, $typearray)) return $typearray[$type];

		if (strpos($type, 'text/') === 0) return 'text';

		# default
		return false;
	}

	// Map special filenames to extensions
	function filename2extension($name)
	{
		$namearray = array(
			'PKGBUILD' => 'bash',
			'.vimrc' => 'vim'
		);
		if (array_key_exists($name, $namearray)) return $namearray[$name];

		return false;
	}

}

# vim: set noet: