3 # this is the upload/download plugin, which allows to put arbitrary binary
4 # files into the ewiki database using the provided specialized form, or the
5 # standard image upload form below every edit page (if EWIKI_ALLOW_BINARY)
9 define("EWIKI_UPLOAD_MAXSIZE", 2*1024*1024);
10 define("EWIKI_PAGE_UPLOAD", "FileUpload");
11 define("EWIKI_PAGE_DOWNLOAD", "FileDownload");
12 define("EWIKI_ACTION_ATTACHMENTS", "attachments"); #-- define to 0 to disable
14 #-- register plugin (main part)
15 $ewiki_plugins["page"][EWIKI_PAGE_UPLOAD] = "ewiki_page_fileupload";
16 $ewiki_plugins["page"][EWIKI_PAGE_DOWNLOAD] = "ewiki_page_filedownload";
17 $ewiki_plugins["action"]["binary"] = "ewiki_binary";
18 // $ewiki_config["action_links"]["binary_get"]["links"] = 'Linked from';
19 $ewiki_config["action_links"]["binary_get"]["info"] = 'More Info';
21 #-- allow per-page downloads
22 if (defined("EWIKI_ACTION_ATTACHMENTS") && EWIKI_ACTION_ATTACHMENTS) {
23 $ewiki_plugins["action"][EWIKI_ACTION_ATTACHMENTS] = "ewiki_action_attachments";
24 $ewiki_config["action_links"]["view"][EWIKI_ACTION_ATTACHMENTS] = "Attachments";
28 #-- icons (best given absolute to www root)
29 $ewiki_binary_icons = array(
30 ".bin" => "/icons/exec.gif",
31 "application/" => "/icons/exec.gif",
32 "application/octet-stream" => "/icons/exec.gif",
33 ".ogg" => "/icons/son.gif",
34 ".jpeg" => "/icons/pic.gif",
35 "text/" => "/icons/txt.gif",
36 ".pdf" => "/icons/txt.gif",
40 #-- the upload function __can__ use different sections
41 $ewiki_upload_sections = array(
43 "section2" => "section2",
47 #-- text, translations
48 $ewiki_t["en"]["UPLOAD0"] = "Use this form to upload an arbitrary binary file into the wiki:<br />";
49 $ewiki_t["en"]["UPL_NEWNAM"] = "Save with different filename";
50 $ewiki_t["en"]["UPL_INSECT"] = "Upload into section";
51 $ewiki_t["en"]["UPL_TOOLARGE"] = "Your upload has been rejected, because that file was too large!";
52 $ewiki_t["en"]["UPL_REJSECT"] = 'The given download section "$sect" has been rejected. Please only use the default ones, or tell the WikiAdmin to reenable per-page uploads; else others can\'t find your uploaded files easily.<br /><br />';
53 $ewiki_t["en"]["UPL_OK"] = "Your file was uploaded correctly, please see <a href=\"\$script\">".EWIKI_PAGE_DOWNLOAD."</a>.<br /><br />";
54 $ewiki_t["en"]["UPL_ERROR"] = "We're sorry, but something went wrong during the file upload.<br /><br />";
55 $ewiki_t["en"]["DWNL_SEEUPL"] = 'See also <a href="$script'.EWIKI_PAGE_UPLOAD.'">FileUpload</a>, this page is only about downloading.<br /><br />';
56 $ewiki_t["en"]["DWNL_NOFILES"] = "No files uploaded yet.<br />\n";
57 $ewiki_t["en"]["file"] = "File";
58 $ewiki_t["en"]["of"] = "of";
59 $ewiki_t["en"]["comment"] = "Comment";
60 $ewiki_t["en"]["dwnl_section"] = "download section";
61 $ewiki_t["en"]["DWNL_ENTRY_FORMAT"] =
62 '<div class="download"><a href="$url">$icon$title</a><small>$size<br />'.
63 'uploaded on <b>$time</b>, downloaded <tt>$hits</tt> times<br />'.
64 '(<a href="$url">$id</a>)<br />'.
65 '$section'.'file is of type <tt>$type</tt>'.
68 "</small></div><br />\n";
70 $ewiki_t["de"]["UPLOAD0"] = "Mit diesem Formular kannst du beliebige Dateien in das Wiki abspeichern:<br />";
71 $ewiki_t["de"]["UPL_NEWNAM"] = "Mit unterschiedlichem Dateinamen speichern";
72 $ewiki_t["de"]["UPL_INSECT"] = "Hochladen in Bereich:";
73 $ewiki_t["de"]["UPL_TOOLARGE"] = "Deine Datei wurde nicht aufgenommen, weil sie zu groß war!";
74 $ewiki_t["de"]["UPL_REJSECT"] = 'Der angegebene Download-Bereich "$sect" wird nicht verwendet. Bitte verwende einen von den voreingestellten Bereichen, damit Andere die Datei später auch finden können, oder frag den Administrator das Hochladen für beliebige Seiten zu aktivieren.<br /><br />';
75 $ewiki_t["de"]["UPL_OK"] = "Deine Datei wurde korrekt hochgeladen, sehe einfach auf der <a href=\"\$script\">".EWIKI_PAGE_DOWNLOAD."</a> nach.<br /><br />";
76 $ewiki_t["de"]["UPL_ERROR"] = "'Tschuldige, aber irgend etwas ist während des Hochladens gründlich schief gelaufen.<br /><br />";
77 $ewiki_t["de"]["DWNL_SEEUPL"] = 'Siehe auch <a href="$script'.EWIKI_PAGE_UPLOAD.'">DateiHochladen</a>, auf dieser Seite stehen nur die Downloads.<br /><br />';
78 $ewiki_t["de"]["DWNL_NOFILES"] = "Noch keine Dateien hochgeladen.<br />\n";
79 $ewiki_t["de"]["file"] = "Datei";
80 $ewiki_t["de"]["of"] = "von";
81 $ewiki_t["de"]["comment"] = "Kommentar";
82 $ewiki_t["de"]["dwnl_section"] = "Download Bereich";
83 $ewiki_t["de"]["DWNL_ENTRY_FORMAT"] =
84 '<div class="download"><a href="$url">$icon$title</a><small>$size<br />'.
85 'am <b>$time</b> hochgeladen, <tt>$hits</tt> mal abgerufen<br />'.
86 '(<a href="$url">$id</a>)<br />'.
87 '$section'.'Datei ist vom Typ <tt>$type</tt>'.
88 '$comment'."</small></div><br />\n";
93 function ewiki_page_fileupload($id, $data, $action, $def_sec="") {
95 global $ewiki_upload_sections, $ewiki_plugins;
97 $o = ewiki_make_title($id, $id, 2);
99 $upload_file = $_FILES[EWIKI_UP_UPLOAD];
100 if (empty($upload_file)) {
102 $o .= ewiki_t("UPLOAD0");
104 $o .= '<div class="upload">'.
106 ewiki_script( ($action!="view" ? $action : ""), $id).
107 '" method="POST" enctype="multipart/form-data">' .
108 '<b>'.ewiki_t("file").'</b><br /><input type="file" name="'.EWIKI_UP_UPLOAD.'"><br /><br />' .
109 '<input type="submit" value="' . EWIKI_PAGE_UPLOAD . '"><br /><br />';
111 $o .= '<b>' . ewiki_t("comment") . '</b><br /><textarea name="comment" cols="35" rows="3"></textarea><br /><br />';
113 if (empty($ewiki_upload_sections[$def_sec])) {
114 $ewiki_upload_sections[$def_sec] = $def_sec;
116 if (count($ewiki_upload_sections) > 1) {
117 if (empty($def_sec)) {
118 $def_sec = $_REQUEST["section"];
120 $o .= '<b>'.ewiki_t("UPL_INSECT").'</b><br /><select name="section">';
121 foreach ($ewiki_upload_sections as $id => $title) {
122 $o .= '<option value="'.$id.'"' .($id==$def_sec?' selected':''). '>'.$title.'</option>';
124 $o .= '</select><br /><br />';
127 $o .= '<b>'.ewiki_t("UPL_NEWNAM").'</b><br /><input type="text" name="new_filename" size="20"><br /><br />';
129 $o .= '</form></div>';
132 elseif ($upload_file["size"] > EWIKI_UPLOAD_MAXSIZE) {
134 $o .= ewiki_t("UPL_TOOLARGE");
140 "X-Content-Type" => $upload_file["type"],
141 "Cache-control" => "private",
142 #"X-Content-Length" => $upload_file["size"],
144 if (($s = $upload_file["name"]) && (strlen($s) >= 3)
145 || ($s = substr(md5(time()+microtime()),0,8) . ".dat"))
147 if (strlen($uu = trim($_REQUEST["new_filename"])) >= 3) {
149 $meta["Original-Filename"] = $s;
153 $meta["Content-Location"] = $s;
155 ($p = strrpos($s, "/")) and ($p++) or
156 ($p = strrpos($s, '\\')) and ($p++);
157 $meta["Content-Disposition"] = 'attachment; filename="'.urlencode(substr($s, $p)).'"';
159 if (strlen($sect = $_REQUEST["section"])) {
160 if ($ewiki_upload_sections[$sect]
161 || ($action==EWIKI_ACTION_ATTACHMENTS) && ($data["content"])
162 && strlen($ewiki_plugins["action"][EWIKI_ACTION_ATTACHMENTS])) {
163 $meta["section"] = $sect;
166 $o .= ewiki_t("UPL_REJSECT", array('sect' => $sect));
171 if (strlen($s = trim($_REQUEST["comment"]))) {
172 $meta["comment"] = $s;
175 $result = ewiki_binary_save_image($upload_file["tmp_name"], "", "RETURN", $meta, "ACCEPT_ALL", $care_for_images=0);
178 $o .= ewiki_t("UPL_OK", array('$script'=>ewiki_script(EWIKI_PAGE_DOWNLOAD)));
179 ewiki_log("file uploaded to section '$sect'");
182 $o .= ewiki_t("UPL_ERROR");
193 function ewiki_page_filedownload($id, $data, $action, $def_sec="") {
195 global $ewiki_binary_icons, $ewiki_upload_sections;
197 $o = ewiki_make_title($id, $id, 2);
198 #<off># $o .= ewiki_t("DWNL_SEEUPL", '$scr'=>ewiki_script("", ""));
201 #-- params (section, orderby)
202 ($orderby = $_REQUEST["orderby"]) or ($orderby = "created");
208 ($section = $_REQUEST["section"]) or ($section = "");
209 if (count($ewiki_upload_sections) > 1) {
211 $ewiki_upload_sections["*"] = "*";
212 if (empty($ewiki_plugins["action"][EWIKI_ACTION_ATTACHMENTS])) {
213 $ewiki_upload_sections["**"] = "**";
215 foreach ($ewiki_upload_sections as $sec=>$title) {
216 $oa[] = '<a href="' . ewiki_script("", $id, array(
217 "orderby"=>$orderby, "section" => $sec)) .
218 '">' . $title . "</a>";
220 $o .= '<div align="center" class="darker">'.implode(" · ", $oa).'</div><br />';
228 $result = ewiki_db::GETALL(array("flags", "meta", "created", "hits"));
230 while ($row = $result->get()) {
231 if (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_BINARY) {
234 if ($m["section"] != $section) {
235 if ($section == "**") {
237 elseif (($section == "*") && !empty($ewiki_upload_sections[$m["section"]])) {
243 if (EWIKI_PROTECTED_MODE && EWIKI_PROTECTED_MODE_HIDING && !ewiki_auth($row["id"], $uuu, "binary-get")) {
247 $files[$row["id"]] = $row;
248 $sorted[$row["id"]] = $row[$orderby];
258 ($pnum = $_REQUEST[EWIKI_UP_PAGENUM]) or ($pnum = 0);
259 if (count($sorted) > EWIKI_LIST_LIMIT) {
260 $o_nl .= '<div class="lighter">>> ';
261 for ($n=0; $n < (int)(count($sorted) / EWIKI_LIST_LIMIT); $n++) {
262 $o_nl .= '<a href="' . ewiki_script("", $id, array(
263 "orderby"=>$orderby, "section"=>$section, EWIKI_UP_PAGENUM=>$n)) .
264 '">[' . $n . "]</a> ";
266 $o_nl .= '</div><br />';
269 $sorted = array_slice($sorted, $pnum * EWIKI_LIST_LIMIT, EWIKI_LIST_LIMIT);
273 if (empty($sorted)) {
275 $o .= ewiki_t("DWNL_NOFILES");
279 foreach ($sorted as $id=>$uu) {
281 $o .= ewiki_entry_downloads($row, $section[0]=="*");
294 function ewiki_entry_downloads($row, $show_section=0) {
296 global $ewiki_binary_icons, $ewiki_upload_sections,$ewiki_config;
298 $meta = &$row["meta"];
301 $p_title = basename($meta["Content-Location"]);
302 $p_time = strftime("%c", $row["created"]);
303 $p_hits = ($uu = $row["hits"] ? $uu : "0");
304 $p_size = $meta["size"];
305 $p_size = isset($p_size) ? (", " . ($p_size>=4096 ? round($p_size/1024)."K" : $p_size." bytes")) : "";
306 $p_ct1 = $meta["Content-Type"];
307 $p_ct2 = $meta["X-Content-Type"];
308 if ($p_ct1==$p_ct2) { unset($p_ct2); }
309 if ($p_ct1 && !$p_ct2) { $p_ct = "<tt>$p_ct1</tt>"; }
310 elseif (!$p_ct1 && $p_ct2) { $p_ct = "<tt>$p_ct2</tt>"; }
311 elseif ($p_ct1 && $p_ct2) { $p_ct = "<tt>$p_ct1</tt>, <tt>$p_ct2</tt>"; }
312 else { $p_ct = "<tt>application/octet-stream</tt>"; }
313 $p_section = $ewiki_upload_sections[$meta["section"]];
314 $p_section = $p_section ? $p_section : $meta["section"];
315 $p_comment = strlen($meta["comment"]) ? '<table border="1" cellpadding="2" cellspacing="0"><tr><td class="lighter">'.
316 str_replace('</p>', '', str_replace('<p>', '',
317 ewiki_format($meta["comment"]))) . '</td></tr></table>' : "<br />";
320 foreach ($ewiki_binary_icons as $str => $i) {
321 if (empty($str) || strstr($row["Content-Location"], $str) || strstr($p_ct, $str) || strstr($p_ct2, $str)) {
332 "icon" => ($p_icon ? '<img src="'.$p_icon.'" alt="['.$p_icon_t.']" align="left" width="14" height="14" border="0" /> ' : ''),
335 "section" => ($show_section ? ewiki_t('dwnl_section') . ": $p_section<br />" : ''),
337 "url" => ewiki_script_binary("", $row["id"]),
339 'comment' => $p_comment,
340 'control_links'=> ewiki_control_links_list($id, &$data, $ewiki_config["action_links"]["binary_get"],$data["version"])
349 #------------------------------------------------------- per-page uploads ---
352 function ewiki_action_attachments($id, $data, $action=EWIKI_ACTION_ATTACHMENTS) {
354 if (!empty($_FILES[EWIKI_UP_UPLOAD])) {
355 $o .= ewiki_page_fileupload($id, $data, EWIKI_ACTION_ATTACHMENTS, $id);
358 $o .= ewiki_page_filedownload(ucwords(EWIKI_ACTION_ATTACHMENTS) . " " . ewiki_t("of") . " $id", $data, "view", $id);
360 unset($_FILES[EWIKI_UP_UPLOAD]);
361 $o .= ewiki_page_fileupload($id, $data, EWIKI_ACTION_ATTACHMENTS, $id);