* * Licensed under GPLv3 * (see COPYING for full license text) * */ class File_mod extends CI_Model { function __construct() { parent::__construct(); } // 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, 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; } 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 && $password !== "") { 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) { $redirect = false; } if ($redirect) { redirect($data['url']); } else { $this->load->view($this->var->view_dir.'/header', $data); $this->load->view($this->var->view_dir.'/show_url', $data); $this->load->view($this->var->view_dir.'/footer', $data); } } function non_existent() { $data["title"] = "Not Found"; $this->output->set_status_header(404); $this->load->view($this->var->view_dir.'/header', $data); $this->load->view($this->var->view_dir.'/non_existent', $data); $this->load->view($this->var->view_dir.'/footer', $data); } function check_client_version() { if ($this->var->cli_client == "fb-client") { $client_version = substr($_SERVER['HTTP_USER_AGENT'], 10); if ($this->var->latest_client != $client_version) { echo "Your are using an old client version. Latest is ".$this->var->latest_client."\n"; } } } // 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 (file_exists($file)) { $oldest_time = (time()-$this->config->item('upload_max_age')); if (filesize($file) > $this->config->item("small_upload_size") && $filedata["date"] < $oldest_time) { if (filemtime($file) < $oldest_time) { 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)); } $this->non_existent(); return; } // 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; } $mode = $this->extension_aliases($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'); $data['delete_link'] = site_url("file/delete/".$id); header("Content-Type: text/html\n"); if ($mode) { $data['current_highlight'] = $mode; } else { $data['current_highlight'] = $this->mime2extension($type); } if (filesize($file) > $this->config->item("small_upload_size")) { $data['timeout'] = date("r", $filedata["date"]+$this->config->item("upload_max_age")); } else { $data['timeout'] = "never"; } echo $this->load->view($this->var->view_dir.'/html_header', $data, true); $this->load->library("MemcacheLibrary"); if (! $cached = $this->memcachelibrary->get($filedata['hash'].'_'.$mode)) { ob_start(); if ($mode == "rmd") { echo '
'; // generate line numbers (links) passthru('/usr/bin/perl -ne \'print "$.\n"\' '.escapeshellarg($file)); echo '