* * Licensed under AGPLv3 * (see COPYING for full license text) * */ class File extends MY_Controller { function __construct() { parent::__construct(); $this->load->model('mfile'); $this->load->model('mmultipaste'); if (is_cli_client()) { $this->var->view_dir = "file_plaintext"; } else { $this->var->view_dir = "file"; } } function index() { if ($this->input->is_cli_request()) { $this->load->library("../controllers/tools"); return $this->tools->index(); } // Try to guess what the user would like to do. $id = $this->uri->segment(1); if (!empty($_FILES)) { $this->do_upload(); } elseif (strpos($id, "m-") === 0 && $this->mmultipaste->id_exists($id)) { $this->_download(); } elseif ($id != "file" && $this->mfile->id_exists($id)) { $this->_download(); } elseif ($id && $id != "file") { $this->_non_existent(); } else { $this->upload_form(); } } /** * Generate a page title of the format "Multipaste - $filename, $filename, … (N more)". * This mainly helps in IRC channels to quickly determine what is in a multipaste. * * @param files array of filedata * @return title to be used */ private function _multipaste_page_title(array $files) { $filecount = count($files); $title = "Multipaste ($filecount files) - "; $titlenames = array(); $len = strlen($title); $delimiter = ', '; $maxlen = 100; foreach ($files as $file) { if ($len > $maxlen) break; $filename = $file['filename']; $titlenames[] = htmlspecialchars($filename); $len += strlen($filename) + strlen($delimiter); } $title .= implode($delimiter, $titlenames); $leftover_count = $filecount - count($titlenames); if ($leftover_count > 0) { $title .= $delimiter.'… ('.$leftover_count.' more)'; } return $title; } function _download() { $id = $this->uri->segment(1); $lexer = urldecode($this->uri->segment(2)); $is_multipaste = false; if ($this->mmultipaste->id_exists($id)) { $is_multipaste = true; if(!$this->mmultipaste->valid_id($id)) { $this->mmultipaste->delete_id($id); return $this->_non_existent(); } $files = $this->mmultipaste->get_files($id); $this->data["title"] = $this->_multipaste_page_title($files); } elseif ($this->mfile->id_exists($id)) { if (!$this->mfile->valid_id($id)) { return $this->_non_existent(); } $files = array($this->mfile->get_filedata($id)); $this->data["title"] = htmlspecialchars($files[0]["filename"]); } else { assert(0); } assert($files !== false); assert(is_array($files)); assert(count($files) >= 1); // don't allow unowned files to be downloaded foreach ($files as $filedata) { if ($filedata["user"] == 0) { return $this->_non_existent(); } } $etag = ""; foreach ($files as $filedata) { $etag = sha1($etag.$filedata["data_id"]); } // handle some common "lexers" here switch ($lexer) { case "": break; case "qr": handle_etag($etag); header("Content-disposition: inline; filename=\"".$id."_qr.png\"\n"); header("Content-Type: image/png\n"); $qr = new \Endroid\QrCode\QrCode(); $qr->setText(site_url($id).'/') ->setSize(350) ->setErrorCorrection('low') ->render(); exit(); case "info": return $this->_display_info($id); case "tar": if ($is_multipaste) { return $this->_tarball($id); } case "pls": if ($is_multipaste) { return $this->_generate_playlist($id); } default: if ($is_multipaste) { throw new \exceptions\UserInputException("file/download/invalid-action", "Invalid action \"".htmlspecialchars($lexer)."\""); } break; } $this->load->driver("ddownload"); // user wants the plain file if ($lexer == 'plain') { assert(count($files) == 1); handle_etag($etag); $filedata = $files[0]; $filepath = $this->mfile->file($filedata["data_id"]); $this->ddownload->serveFile($filepath, $filedata["filename"], "text/plain"); exit(); } $this->load->library("output_cache"); foreach ($files as $key => $filedata) { $file = $this->mfile->file($filedata['data_id']); $pygments = new \libraries\Pygments($file, $filedata["mimetype"], $filedata["filename"]); // autodetect the lexer for highlighting if the URL contains a / after the ID (/ID/) // /ID/lexer disables autodetection $autodetect_lexer = !$lexer && substr_count(ltrim($this->uri->uri_string(), "/"), '/') >= 1; $autodetect_lexer = $is_multipaste ? true : $autodetect_lexer; if ($autodetect_lexer) { $lexer = $pygments->autodetect_lexer(); } // resolve aliases // this is mainly used for compatibility $lexer = $pygments->resolve_lexer_alias($lexer); // if there is no mimetype mapping we can't highlight it $can_highlight = $pygments->can_highlight(); $filesize_too_big = filesize($file) > $this->config->item('upload_max_text_size'); if (!$can_highlight || $filesize_too_big || !$lexer) { if (!$is_multipaste) { // prevent javascript from being executed and forbid frames // this should allow us to serve user submitted HTML content without huge security risks foreach (array("X-WebKit-CSP", "X-Content-Security-Policy", "Content-Security-Policy") as $header_name) { header("$header_name: default-src 'none'; img-src *; media-src *; font-src *; style-src 'unsafe-inline' *; script-src 'none'; object-src *; frame-src 'none'; "); } handle_etag($etag); $this->ddownload->serveFile($file, $filedata["filename"], $filedata["mimetype"]); exit(); } else { $mimetype = $filedata["mimetype"]; $base = explode("/", $filedata["mimetype"])[0]; if (\libraries\Image::type_supported($mimetype)) { $filedata["tooltip"] = $this->_tooltip_for_image($filedata); $filedata["orientation"] = libraries\Image::get_exif_orientation($file); $this->output_cache->add_merge( array("items" => array($filedata)), 'file/fragments/thumbnail' ); } else if ($base == "audio") { $this->output_cache->add(array("filedata" => $filedata), "file/fragments/audio-player"); } else if ($base == "video") { $this->output_cache->add(array("filedata" => $filedata), "file/fragments/video-player"); } else { $this->output_cache->add_merge( array("items" => array($filedata)), 'file/fragments/uploads_table' ); } continue; } } $this->output_cache->add_function(function() use ($filedata, $lexer, $is_multipaste) { $this->_highlight_file($filedata, $lexer, $is_multipaste); }); } // TODO: move lexers json to dedicated URL $this->data['lexers'] = \libraries\Pygments::get_lexers(); // Output everything // Don't use the output class/append_output because it does too // much magic ({elapsed_time} and {memory_usage}). // Direct echo puts us on the safe side. echo $this->load->view($this->var->view_dir.'/html_header', $this->data, true); $this->output_cache->render(); echo $this->load->view($this->var->view_dir.'/html_footer', $this->data, true); } private function _colorify($file, $lexer, $anchor_id = false) { $output = ""; $lines_to_remove = 0; $output .= '
'."\n";
if ($lexer == "ascii") {
// TODO: use exec safe and catch exception
$ret = (new \libraries\ProcRunner(array('ansi2html', '-p')))
->input(file_get_contents($file))
->forbid_stderr()
->exec();
// Last line is empty
$lines_to_remove = 1;
} else {
// TODO: use exec safe and catch exception
$ret = (new \libraries\ProcRunner(array('pygmentize', '-F', 'codetagify', '-O', 'encoding=guess,outencoding=utf8,stripnl=False', '-l', $lexer, '-f', 'html', $file)))
->exec();
// Last 2 items are "
", "", $line);
}
$anchor = "n$line_number";
if ($anchor_id !== false) {
$anchor = "n-$anchor_id-$line_number";
}
// Be careful not to add superflous whitespace here (we are in a )
$output .= ""
.""
." "
.""
."".$line."\n";
$output .= "";
}
$output .= "
$message
"), "file/fragments/alert-wide" ); } } $data = array_merge($this->data, array( 'title' => htmlspecialchars($filedata['filename']), 'id' => $filedata["id"], 'current_highlight' => htmlspecialchars($lexer), 'timeout' => $this->mfile->get_timeout_string($filedata["id"]), 'filedata' => $filedata, )); $this->output_cache->render_now($data, $this->var->view_dir.'/html_paste_header'); $this->output_cache->render_now($highlit["output"]); $this->output_cache->render_now($data, $this->var->view_dir.'/html_paste_footer'); } private function _tooltip_for_image($filedata) { $filesize = format_bytes($filedata["filesize"]); $file = $this->mfile->file($filedata["data_id"]); $upload_date = date("r", $filedata["date"]); $height = 0; $width = 0; try { list($width, $height) = getimagesize($file); } catch (\ErrorException $e) { // likely unsupported filetype // TODO: support more (using identify from imagemagick is likely slow :( ) } $tooltip = "${filedata["id"]} - $filesizeContact info not available.
'; } $this->load->view('header', $this->data); $this->load->view('contact', $this->data); $this->load->view('footer', $this->data); } /* Functions below this comment can only be run via the CLI * `php index.php file