* * Licensed under GPLv3 * (see COPYING for full license text) * */ class File_mod extends CI_Model { var $data = array(); function __construct() { parent::__construct(); $this->load->model("muser"); $this->data["title"] = "FileBin"; $this->data["username"] = $this->muser->get_username(); } // Returns an unused ID // TODO: make threadsafe function new_id() { $id = random_id(3,6); if ($this->id_exists($id) || $id == 'file' || $id == 'user') { 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, date 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; } // Add a hash to the DB // TODO: Should only update not insert; see new_id() function add_file($hash, $id, $filename) { $this->muser->require_access(); $userid = $this->muser->get_userid(); $mimetype = exec("perl ".FCPATH.'scripts/mimetype '.escapeshellarg($filename).' '.escapeshellarg($this->file($hash))); $query = $this->db->query(' INSERT INTO `files` (`hash`, `id`, `filename`, `user`, `date`, `mimetype`) VALUES (?, ?, ?, ?, ?, ?)', array($hash, $id, $filename, $userid, time(), $mimetype)); } function show_url($id, $mode) { $redirect = false; if ($mode) { $this->data['url'] = site_url($id).'/'.$mode; } else { $this->data['url'] = site_url($id).'/'; $filedata = $this->get_filedata($id); $file = $this->file($filedata['hash']); $type = $filedata['mimetype']; $mode = $this->mime2mode($type); // 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) { $redirect = false; } if ($redirect) { redirect($this->data['url'], "location", 303); } else { $this->load->view($this->var->view_dir.'/header', $this->data); $this->load->view($this->var->view_dir.'/show_url', $this->data); $this->load->view($this->var->view_dir.'/footer', $this->data); } } function non_existent() { $this->data["title"] .= " - Not Found"; $this->output->set_status_header(404); $this->load->view($this->var->view_dir.'/header', $this->data); $this->load->view($this->var->view_dir.'/non_existent', $this->data); $this->load->view($this->var->view_dir.'/footer', $this->data); } // remove old/invalid/broken IDs function valid_id($id) { $filedata = $this->get_filedata($id); if (!$filedata) { return false; } $file = $this->file($filedata['hash']); if (!file_exists($file)) { if (isset($filedata["hash"])) { $this->db->query('DELETE FROM files WHERE hash = ?', array($filedata['hash'])); } return false; } // small files don't expire if (filesize($file) <= $this->config->item("small_upload_size")) { return true; } // files older than this should be removed $remove_before = (time()-$this->config->item('upload_max_age')); if ($filedata["date"] < $remove_before) { // if the file has been uploaded multiple times the mtime is the time // of the last upload if (filemtime($file) < $remove_before) { unlink($file); $this->db->query('DELETE FROM files WHERE hash = ?', array($filedata['hash'])); } else { $this->db->query('DELETE FROM files WHERE id = ? LIMIT 1', array($id)); } return false; } return true; } // download a given ID // TODO: make smaller function download() { $id = $this->uri->segment(1); $mode = $this->uri->segment(2); $filedata = $this->get_filedata($id); $file = $this->file($filedata['hash']); if (!$this->valid_id($id)) { $this->non_existent(); return; } // MODIFIED SINCE SUPPORT -- START // helps to keep traffic low when reloading $etag = strtolower($filedata["hash"]."-".$filedata["date"]); $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; } } if (!$modified) { header("HTTP/1.1 304 Not Modified"); header('Etag: "'.$etag.'"'); exit(); } // MODIFIED SINCE SUPPORT -- END $type = $filedata['mimetype']; // autodetect the mode for highlighting if the URL contains a / after the ID (/ID/) // /ID/mode disables autodetection $autodetect_mode = !$mode && substr_count(ltrim($this->uri->uri_string(), "/"), '/') >= 1; if ($autodetect_mode) { $mode = $this->get_highlight_mode($type, $filedata["filename"]); } // resolve aliases of modes // this is mainly used for compatibility $mode = $this->resolve_mode_alias($mode); header("Last-Modified: ".date('D, d M Y H:i:s', $filedata["date"])." GMT"); header('Etag: "'.$etag.'"'); // create the qr code for /ID/ if ($mode == "qr") { header("Content-disposition: inline; filename=\"".$id."_qr.png\"\n"); header("Content-Type: image/png\n"); passthru('qrencode -s 10 -o - '.escapeshellarg(site_url($id).'/')); exit(); } // user wants to the the plain file if ($mode == 'plain') { rangeDownload($file, $filedata["filename"], "text/plain"); exit(); } // if there is no mimetype mapping we can't highlight it $can_highlight = $this->can_highlight($type); $filesize_too_big = filesize($file) > $this->config->item('upload_max_text_size'); if (!$can_highlight || $filesize_too_big || !$mode) { foreach (array("X-WebKit-CSP", "X-Content-Security-Policy") as $header_name) { header("$header_name: allow 'none'; img-src *; media-src *; font-src *; style-src *; script-src 'none'; object-src *; frame-src 'none'; "); } rangeDownload($file, $filedata["filename"], $type); exit(); } $this->data['title'] = htmlspecialchars($filedata['filename']); $this->data['raw_link'] = site_url($id); $this->data['new_link'] = site_url(); $this->data['plain_link'] = site_url($id.'/plain'); $this->data['auto_link'] = site_url($id).'/'; $this->data['rmd_link'] = site_url($id.'/rmd'); $this->data['delete_link'] = site_url("file/delete/".$id); header("Content-Type: text/html\n"); $this->data['current_highlight'] = htmlspecialchars($mode); if (filesize($file) > $this->config->item("small_upload_size")) { $this->data['timeout'] = date("r", $filedata["date"] + $this->config->item("upload_max_age")); } else { $this->data['timeout'] = "never"; } echo $this->load->view($this->var->view_dir.'/html_header', $this->data, true); // highlight the file and chache the result $this->load->library("MemcacheLibrary"); if (! $cached = $this->memcachelibrary->get($filedata['hash'].'_'.$mode)) { ob_start(); if ($mode == "rmd") { echo '
'."\n"; passthru('perl '.FCPATH.'scripts/ansi2html '.escapeshellarg($file)); echo "\n"; } else { echo '
'; // generate line numbers (links) passthru('perl -ne \'print "$.\n"\' '.escapeshellarg($file)); echo '