From 7d3663c1206b823200f239721828f19a5b4d3f41 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Thu, 9 Aug 2012 23:23:28 +0200 Subject: rename file_mod to mfile Muser already uses this scheme as it is easier to type. Signed-off-by: Florian Pritz --- application/controllers/file.php | 64 ++++---- application/models/file_mod.php | 312 --------------------------------------- application/models/mfile.php | 312 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 344 insertions(+), 344 deletions(-) delete mode 100644 application/models/file_mod.php create mode 100644 application/models/mfile.php (limited to 'application') diff --git a/application/controllers/file.php b/application/controllers/file.php index 2bd8b6c8e..ee983a639 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -28,7 +28,7 @@ class File extends CI_Controller { mb_internal_encoding('UTF-8'); $this->load->helper(array('form', 'filebin')); - $this->load->model('file_mod'); + $this->load->model('mfile'); $this->load->model('muser'); $this->var->latest_client = false; @@ -61,7 +61,7 @@ class File extends CI_Controller { $id = $this->uri->segment(1); if(isset($_FILES['file'])) { $this->do_upload(); - } elseif ($id != "file" && $this->file_mod->id_exists($id)) { + } elseif ($id != "file" && $this->mfile->id_exists($id)) { $this->_download(); } elseif ($id && $id != "file") { $this->_non_existent(); @@ -75,10 +75,10 @@ class File extends CI_Controller { $id = $this->uri->segment(1); $mode = $this->uri->segment(2); - $filedata = $this->file_mod->get_filedata($id); - $file = $this->file_mod->file($filedata['hash']); + $filedata = $this->mfile->get_filedata($id); + $file = $this->mfile->file($filedata['hash']); - if (!$this->file_mod->valid_id($id)) { + if (!$this->mfile->valid_id($id)) { $this->_non_existent(); return; } @@ -97,12 +97,12 @@ class File extends CI_Controller { $autodetect_mode = !$mode && substr_count(ltrim($this->uri->uri_string(), "/"), '/') >= 1; if ($autodetect_mode) { - $mode = $this->file_mod->get_highlight_mode($filedata["mimetype"], $filedata["filename"]); + $mode = $this->mfile->get_highlight_mode($filedata["mimetype"], $filedata["filename"]); } // resolve aliases of modes // this is mainly used for compatibility - $mode = $this->file_mod->resolve_mode_alias($mode); + $mode = $this->mfile->resolve_mode_alias($mode); // create the qr code for /ID/ if ($mode == "qr") { @@ -126,7 +126,7 @@ class File extends CI_Controller { } // if there is no mimetype mapping we can't highlight it - $can_highlight = $this->file_mod->can_highlight($filedata["mimetype"]); + $can_highlight = $this->mfile->can_highlight($filedata["mimetype"]); $filesize_too_big = filesize($file) > $this->config->item('upload_max_text_size'); @@ -147,7 +147,7 @@ class File extends CI_Controller { header("Content-Type: text/html\n"); $this->data['current_highlight'] = htmlspecialchars($mode); - $this->data['timeout'] = $this->file_mod->get_timeout_string($id); + $this->data['timeout'] = $this->mfile->get_timeout_string($id); echo $this->load->view($this->var->view_dir.'/html_header', $this->data, true); @@ -185,9 +185,9 @@ class File extends CI_Controller { function _display_info($id) { $this->data["title"] .= " - Info $id"; - $this->data["filedata"] = $this->file_mod->get_filedata($id); + $this->data["filedata"] = $this->mfile->get_filedata($id); $this->data["id"] = $id; - $this->data['timeout'] = $this->file_mod->get_timeout_string($id); + $this->data['timeout'] = $this->mfile->get_timeout_string($id); $this->load->view($this->var->view_dir.'/header', $this->data); $this->load->view($this->var->view_dir.'/file_info', $this->data); @@ -222,10 +222,10 @@ class File extends CI_Controller { } else { $this->data['url'] = site_url($id).'/'; - $filedata = $this->file_mod->get_filedata($id); - $file = $this->file_mod->file($filedata['hash']); + $filedata = $this->mfile->get_filedata($id); + $file = $this->mfile->file($filedata['hash']); $type = $filedata['mimetype']; - $mode = $this->file_mod->should_highlight($type); + $mode = $this->mfile->should_highlight($type); // If we detected a highlightable file redirect, // otherwise show the URL because browsers would just show a DL dialog @@ -383,12 +383,12 @@ class File extends CI_Controller { foreach ($ids as $id) { $total_count++; - if (!$this->file_mod->id_exists($id)) { + if (!$this->mfile->id_exists($id)) { $errors[] = "'$id' didn't exist anymore."; continue; } - if ($this->file_mod->delete_id($id)) { + if ($this->mfile->delete_id($id)) { $msgs[] = "'$id' has been removed."; $deleted_count++; } else { @@ -418,13 +418,13 @@ class File extends CI_Controller { $id = $this->uri->segment(3); $this->data["id"] = $id; - if ($id && !$this->file_mod->id_exists($id)) { + if ($id && !$this->mfile->id_exists($id)) { $this->output->set_status_header(404); echo "Unknown ID '$id'.\n"; return; } - if ($this->file_mod->delete_id($id)) { + if ($this->mfile->delete_id($id)) { echo "$id has been deleted.\n"; } else { echo "Deletion failed. Do you really own that file?\n"; @@ -455,16 +455,16 @@ class File extends CI_Controller { return; } - $id = $this->file_mod->new_id(); + $id = $this->mfile->new_id(); $hash = md5($content); - $folder = $this->file_mod->folder($hash); + $folder = $this->mfile->folder($hash); file_exists($folder) || mkdir ($folder); - $file = $this->file_mod->file($hash); + $file = $this->mfile->file($hash); file_put_contents($file, $content); chmod($file, 0600); - $this->file_mod->add_file($hash, $id, $filename); + $this->mfile->add_file($hash, $id, $filename); $this->_show_url($id, false); } @@ -503,7 +503,7 @@ class File extends CI_Controller { return; } - $id = $this->file_mod->new_id(); + $id = $this->mfile->new_id(); $hash = md5_file($_FILES['file']['tmp_name']); // work around a curl bug and allow the client to send the real filename base64 encoded @@ -517,13 +517,13 @@ class File extends CI_Controller { $filename = $_FILES['file']['name']; } - $folder = $this->file_mod->folder($hash); + $folder = $this->mfile->folder($hash); file_exists($folder) || mkdir ($folder); - $file = $this->file_mod->file($hash); + $file = $this->mfile->file($hash); move_uploaded_file($_FILES['file']['tmp_name'], $file); chmod($file, 0600); - $this->file_mod->add_file($hash, $id, $filename); + $this->mfile->add_file($hash, $id, $filename); $this->_show_url($id, $extension); } @@ -534,13 +534,13 @@ class File extends CI_Controller { $last_upload = $this->session->userdata("last_upload"); $id = $last_upload["id"]; - $filedata = $this->file_mod->get_filedata($id); + $filedata = $this->mfile->get_filedata($id); if ($filedata["user"] != 0) { show_error("Someone already owns '$id', can't reassign."); } - $this->file_mod->adopt($id); + $this->mfile->adopt($id); $this->session->unset_userdata("last_upload"); @@ -570,7 +570,7 @@ class File extends CI_Controller { array($oldest_time, $oldest_session_time)); foreach($query->result_array() as $row) { - $file = $this->file_mod->file($row['hash']); + $file = $this->mfile->file($row['hash']); if (!file_exists($file)) { $this->db->query('DELETE FROM files WHERE id = ? LIMIT 1', array($row['id'])); continue; @@ -634,7 +634,7 @@ class File extends CI_Controller { $id = $this->uri->segment(3); - $file_data = $this->file_mod->get_filedata($id); + $file_data = $this->mfile->get_filedata($id); if (empty($file_data)) { echo "unknown id \"$id\"\n"; @@ -648,7 +648,7 @@ class File extends CI_Controller { WHERE hash = ? ", array($hash)); - unlink($this->file_mod->file($hash)); + unlink($this->mfile->file($hash)); echo "removed hash \"$hash\"\n"; } @@ -671,7 +671,7 @@ class File extends CI_Controller { foreach ($query as $key => $item) { $hash = $item["hash"]; - $filesize = intval(filesize($this->file_mod->file($hash))); + $filesize = intval(filesize($this->mfile->file($hash))); $this->db->query(" UPDATE files SET filesize = ? diff --git a/application/models/file_mod.php b/application/models/file_mod.php deleted file mode 100644 index a60292138..000000000 --- a/application/models/file_mod.php +++ /dev/null @@ -1,312 +0,0 @@ - - * - * Licensed under GPLv3 - * (see COPYING for full license text) - * - */ - -class File_mod extends CI_Model { - - function __construct() - { - parent::__construct(); - $this->load->model("muser"); - } - - // Returns an unused ID - function new_id() - { - $id = random_alphanum(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, user, filesize - 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 - function add_file($hash, $id, $filename) - { - $userid = $this->muser->get_userid(); - - $mimetype = exec("perl ".FCPATH.'scripts/mimetype '.escapeshellarg($filename).' '.escapeshellarg($this->file($hash))); - $filesize = filesize($this->file($hash)); - $query = $this->db->query(' - INSERT INTO `files` (`hash`, `id`, `filename`, `user`, `date`, `mimetype`, `filesize`) - VALUES (?, ?, ?, ?, ?, ?, ?)', - array($hash, $id, $filename, $userid, time(), $mimetype, $filesize)); - } - - function adopt($id) - { - $userid = $this->muser->get_userid(); - - $this->db->query(" - UPDATE files - SET user = ? - WHERE id = ? - ", array($userid, $id)); - } - - // 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; - } - - function get_timeout_string($id) - { - $filedata = $this->get_filedata($id); - $file = $this->file($filedata["hash"]); - - if (filesize($file) > $this->config->item("small_upload_size")) { - return date("r", $filedata["date"] + $this->config->item("upload_max_age")); - } else { - return "unknown"; - } - } - - 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) - { - $this->muser->require_access(); - $filedata = $this->get_filedata($id); - $userid = $this->muser->get_userid(); - - if(!$this->id_exists($id)) { - return false; - } - - $sql = ' - DELETE - FROM `files` - WHERE `id` = ? - AND user = ? - LIMIT 1'; - $this->db->query($sql, array($id, $userid)); - - 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; - } - - function should_highlight($type) - { - if ($this->mime2mode($type)) return true; - - return false; - } - - // Allow certain types to be highlight without doing it automatically - function can_highlight($type) - { - $typearray = array( - 'image/svg+xml', - ); - if (in_array($type, $typearray)) return true; - - if ($this->mime2mode($type)) return true; - - return false; - } - - // Return the mode that should be used for highlighting - function get_highlight_mode($type, $filename) - { - $mode = $this->mime2mode($type); - - // filename modes overwrite mime type mappings - $filename_mode = $this->filename2mode($filename); - if ($filename_mode) { - return $filename_mode; - } - - return $mode; - } - - // Map MIME types to modes needed for highlighting - private function mime2mode($type) - { - $typearray = array( - 'text/plain' => 'text', - 'text/plain-ascii' => 'ascii', - '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-scheme' => 'scheme', - 'text/x-makefile' => 'make', - #'text/x-log' => 'log', - 'text/html' => 'xml', - 'text/css' => 'css', - 'text/x-ocaml' => 'ocaml', - 'message/rfc822' => 'email', - #'image/svg+xml' => 'xml', - 'application/x-perl' => 'perl', - 'application/xml' => 'xml', - 'application/xml-dtd' => "xml", - 'application/xslt+xml' => "xml", - 'application/javascript' => 'javascript', - 'application/smil' => 'ocaml', - 'application/x-desktop' => 'text', - 'application/x-m4' => 'text', - 'application/x-awk' => 'text', - 'application/x-fluid' => '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 modes - private function filename2mode($name) - { - $namearray = array( - 'PKGBUILD' => 'bash', - '.vimrc' => 'vim' - ); - if (array_key_exists($name, $namearray)) return $namearray[$name]; - - return false; - } - - // Handle mode aliases - function resolve_mode_alias($alias) - { - if ($alias === false) return false; - $aliasarray = array( - 'py' => 'python', - 'sh' => 'bash', - 's' => 'asm', - 'pl' => 'perl' - ); - if (array_key_exists($alias, $aliasarray)) return $aliasarray[$alias]; - - return $alias; - } - -} - -# vim: set noet: diff --git a/application/models/mfile.php b/application/models/mfile.php new file mode 100644 index 000000000..d854f528b --- /dev/null +++ b/application/models/mfile.php @@ -0,0 +1,312 @@ + + * + * Licensed under GPLv3 + * (see COPYING for full license text) + * + */ + +class Mfile extends CI_Model { + + function __construct() + { + parent::__construct(); + $this->load->model("muser"); + } + + // Returns an unused ID + function new_id() + { + $id = random_alphanum(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, user, filesize + 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 + function add_file($hash, $id, $filename) + { + $userid = $this->muser->get_userid(); + + $mimetype = exec("perl ".FCPATH.'scripts/mimetype '.escapeshellarg($filename).' '.escapeshellarg($this->file($hash))); + $filesize = filesize($this->file($hash)); + $query = $this->db->query(' + INSERT INTO `files` (`hash`, `id`, `filename`, `user`, `date`, `mimetype`, `filesize`) + VALUES (?, ?, ?, ?, ?, ?, ?)', + array($hash, $id, $filename, $userid, time(), $mimetype, $filesize)); + } + + function adopt($id) + { + $userid = $this->muser->get_userid(); + + $this->db->query(" + UPDATE files + SET user = ? + WHERE id = ? + ", array($userid, $id)); + } + + // 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; + } + + function get_timeout_string($id) + { + $filedata = $this->get_filedata($id); + $file = $this->file($filedata["hash"]); + + if (filesize($file) > $this->config->item("small_upload_size")) { + return date("r", $filedata["date"] + $this->config->item("upload_max_age")); + } else { + return "unknown"; + } + } + + 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) + { + $this->muser->require_access(); + $filedata = $this->get_filedata($id); + $userid = $this->muser->get_userid(); + + if(!$this->id_exists($id)) { + return false; + } + + $sql = ' + DELETE + FROM `files` + WHERE `id` = ? + AND user = ? + LIMIT 1'; + $this->db->query($sql, array($id, $userid)); + + 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; + } + + function should_highlight($type) + { + if ($this->mime2mode($type)) return true; + + return false; + } + + // Allow certain types to be highlight without doing it automatically + function can_highlight($type) + { + $typearray = array( + 'image/svg+xml', + ); + if (in_array($type, $typearray)) return true; + + if ($this->mime2mode($type)) return true; + + return false; + } + + // Return the mode that should be used for highlighting + function get_highlight_mode($type, $filename) + { + $mode = $this->mime2mode($type); + + // filename modes overwrite mime type mappings + $filename_mode = $this->filename2mode($filename); + if ($filename_mode) { + return $filename_mode; + } + + return $mode; + } + + // Map MIME types to modes needed for highlighting + private function mime2mode($type) + { + $typearray = array( + 'text/plain' => 'text', + 'text/plain-ascii' => 'ascii', + '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-scheme' => 'scheme', + 'text/x-makefile' => 'make', + #'text/x-log' => 'log', + 'text/html' => 'xml', + 'text/css' => 'css', + 'text/x-ocaml' => 'ocaml', + 'message/rfc822' => 'email', + #'image/svg+xml' => 'xml', + 'application/x-perl' => 'perl', + 'application/xml' => 'xml', + 'application/xml-dtd' => "xml", + 'application/xslt+xml' => "xml", + 'application/javascript' => 'javascript', + 'application/smil' => 'ocaml', + 'application/x-desktop' => 'text', + 'application/x-m4' => 'text', + 'application/x-awk' => 'text', + 'application/x-fluid' => '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 modes + private function filename2mode($name) + { + $namearray = array( + 'PKGBUILD' => 'bash', + '.vimrc' => 'vim' + ); + if (array_key_exists($name, $namearray)) return $namearray[$name]; + + return false; + } + + // Handle mode aliases + function resolve_mode_alias($alias) + { + if ($alias === false) return false; + $aliasarray = array( + 'py' => 'python', + 'sh' => 'bash', + 's' => 'asm', + 'pl' => 'perl' + ); + if (array_key_exists($alias, $aliasarray)) return $aliasarray[$alias]; + + return $alias; + } + +} + +# vim: set noet: -- cgit v1.2.3-24-g4f1b