summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2012-04-13 19:25:45 +0200
committerFlorian Pritz <bluewind@xinu.at>2012-04-13 19:25:45 +0200
commit4397fbcba26037acc2c1323e73e5a54200da7c17 (patch)
tree9ff3b21a08607bb580bbaa7c7e778a548f92ee66
parenteb820e55b3e4a7af62038f8f236c37fb6e6c66d9 (diff)
Add do_paste() and remove ajax from upload_form
If the session has timed out, javascript will get the login page and try to redirect to "base_url/$htmlcode" which will obviously fail. Instead of fixing the js code, reintroduce do_paste and use it. Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r--application/controllers/file.php44
-rw-r--r--application/views/file/upload_form.php17
-rw-r--r--data/js/upload_form.js47
3 files changed, 46 insertions, 62 deletions
diff --git a/application/controllers/file.php b/application/controllers/file.php
index b8b22f12b..49434699c 100644
--- a/application/controllers/file.php
+++ b/application/controllers/file.php
@@ -205,16 +205,50 @@ class File extends CI_Controller {
$this->load->view($this->var->view_dir.'/footer', $this->data);
}
- // Handles uploaded files
- function do_upload()
+ // Handle pastes
+ function do_paste()
{
$this->muser->require_access();
- if ($this->uri->segment(3)) {
- $this->var->cli_client = true;
- $this->var->view_dir = "file_plaintext";
+ $content = $this->input->post("content");
+ $filesize = strlen($content);
+ $filename = "stdin";
+
+ if(!$content) {
+ $this->output->set_status_header(400);
+ $this->data["msg"] = "Nothing was pasted, content is empty.";
+ $this->load->view($this->var->view_dir.'/header', $this->data);
+ $this->load->view($this->var->view_dir.'/upload_error', $this->data);
+ $this->load->view($this->var->view_dir.'/footer');
+ return;
+ }
+
+ if ($filesize > $this->config->item('upload_max_size')) {
+ $this->output->set_status_header(413);
+ $this->load->view($this->var->view_dir.'/header', $this->data);
+ $this->load->view($this->var->view_dir.'/too_big');
+ $this->load->view($this->var->view_dir.'/footer');
+ return;
}
+ $id = $this->file_mod->new_id();
+ $hash = md5($content);
+
+ $folder = $this->file_mod->folder($hash);
+ file_exists($folder) || mkdir ($folder);
+ $file = $this->file_mod->file($hash);
+
+ file_put_contents($file, $content);
+ chmod($file, 0600);
+ $this->file_mod->add_file($hash, $id, $filename);
+ $this->file_mod->show_url($id, $extension);
+ }
+
+ // Handles uploaded files
+ function do_upload()
+ {
+ $this->muser->require_access();
+
$extension = $this->input->post('extension');
if(!isset($_FILES['file']) || $_FILES['file']['error'] !== 0) {
$this->output->set_status_header(400);
diff --git a/application/views/file/upload_form.php b/application/views/file/upload_form.php
index ce1d00498..3ab70eb62 100644
--- a/application/views/file/upload_form.php
+++ b/application/views/file/upload_form.php
@@ -6,18 +6,15 @@
<input type="submit" value="Upload" id="upload_button" name="process" />
</p>
</form>
+ <p><b>OR</b></p>
+ <?php echo form_open_multipart('file/do_paste'); ?>
+ <p>
+ <textarea id="textarea" name="content" cols="80" rows="20"></textarea><br />
+ <input type="submit" value="Paste" name="process" />
+ </p>
+ </form>
<script type="text/javascript">
/* <![CDATA[ */
-document.write('\
- <p><b>OR</b></p>\
- <form action="javascript: do_paste()">\
- <p>\
- <textarea id="textarea" name="content" cols="80" rows="20"></textarea><br />\
- <input type="submit" value="Paste" name="process" />\
- </p>\
- </form>\
-');
- var upload_url = "<?php echo site_url("file/do_upload/dumb"); ?>";
var max_upload_size = "<?php echo $max_upload_size; ?>";
/* ]]> */
</script>
diff --git a/data/js/upload_form.js b/data/js/upload_form.js
index cb8f072d9..7a71a5174 100644
--- a/data/js/upload_form.js
+++ b/data/js/upload_form.js
@@ -14,50 +14,3 @@ if (window.File && window.FileList) {
document.getElementById('file').addEventListener('change', checkFileUpload, false);
}
-function encode64(inp){
- var key="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
- var chr1,chr2,chr3,enc3,enc4,i=0,out="";
- while(i<inp.length){
- chr1=inp.charCodeAt(i++);if(chr1>127) chr1=88;
- chr2=inp.charCodeAt(i++);if(chr2>127) chr2=88;
- chr3=inp.charCodeAt(i++);if(chr3>127) chr3=88;
- if(isNaN(chr3)) {enc4=64;chr3=0;} else enc4=chr3&63
- if(isNaN(chr2)) {enc3=64;chr2=0;} else enc3=((chr2<<2)|(chr3>>6))&63
- out+=key.charAt((chr1>>2)&63)+key.charAt(((chr1<<4)|(chr2>>4))&63)+key.charAt(enc3)+key.charAt(enc4);
- }
- return encodeURIComponent(out);
-}
-
-function gen_boundary() {
- var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
- var string_length = 40;
- var randomstring = '';
- for (var i=0; i<string_length; i++) {
- var rnum = Math.floor(Math.random() * chars.length);
- randomstring += chars.substring(rnum,rnum+1);
- }
- return randomstring;
-}
-function do_paste() {
- var http = new XMLHttpRequest();
- var CRLF = "\r\n";
- var boundary = "--" + gen_boundary();
- var body = "--" + boundary + CRLF
- + 'Content-Disposition: form-data; name="file"; filename="stdin"' + CRLF
- + "Content-Type: text/plain" + CRLF
- + CRLF
- + document.getElementById("textarea").value + CRLF
- + "--" + boundary + "--" + CRLF + CRLF;
- http.open("POST", upload_url, true);
-
- //Send the proper header information along with the request
- http.setRequestHeader("Content-type", "multipart/form-data; boundary=" + boundary);
-
- http.onreadystatechange = function() {
- if(http.readyState == 4 && http.status == 200) {
- window.location = http.responseText;
- }
- }
- http.send(body);
-}
-