8027ba5c8ba6d49d4d35cbcf5283a3f2c82299e8
[atutor.git] / mods / wiki / plugins / module / downloads.php
1 <?php
2
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)
6
7
8 #-- settings
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
13
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';
20
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";
25 }
26
27
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",
37 );
38
39
40 #-- the upload function __can__ use different sections
41 $ewiki_upload_sections = array(
42    "" => "main",
43    "section2" => "section2",
44 );
45
46
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>'.
66         '$comment'.
67         '$control_links'.
68         "</small></div><br />\n";
69
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";
89
90
91
92
93 function ewiki_page_fileupload($id, $data, $action, $def_sec="") {
94
95    global $ewiki_upload_sections, $ewiki_plugins;
96
97    $o = ewiki_make_title($id, $id, 2);
98
99    $upload_file = $_FILES[EWIKI_UP_UPLOAD];
100    if (empty($upload_file)) {
101
102       $o .= ewiki_t("UPLOAD0");
103
104       $o .= '<div class="upload">'.
105             '<form action="' .
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 />';
110
111       $o .= '<b>' . ewiki_t("comment") . '</b><br /><textarea name="comment" cols="35" rows="3"></textarea><br /><br />';
112
113       if (empty($ewiki_upload_sections[$def_sec])) {
114          $ewiki_upload_sections[$def_sec] = $def_sec;
115       }
116       if (count($ewiki_upload_sections) > 1) {
117          if (empty($def_sec)) {
118             $def_sec = $_REQUEST["section"];
119          }
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>';
123          }
124          $o .= '</select><br /><br />';
125       }
126
127       $o .= '<b>'.ewiki_t("UPL_NEWNAM").'</b><br /><input type="text" name="new_filename" size="20"><br /><br />';
128
129       $o .= '</form></div>';
130
131    }
132    elseif ($upload_file["size"] > EWIKI_UPLOAD_MAXSIZE) {
133
134       $o .= ewiki_t("UPL_TOOLARGE");
135
136    }
137    else {
138
139       $meta = array(
140          "X-Content-Type" => $upload_file["type"],
141          "Cache-control" => "private", 
142         #"X-Content-Length" => $upload_file["size"],
143       );
144       if (($s = $upload_file["name"]) && (strlen($s) >= 3)
145          || ($s = substr(md5(time()+microtime()),0,8) . ".dat"))
146       {
147          if (strlen($uu = trim($_REQUEST["new_filename"])) >= 3) {
148             if ($uu != $s) {
149                $meta["Original-Filename"] = $s;
150             }
151             $s = $uu;
152          }
153          $meta["Content-Location"] = $s;
154          ($p = 0) or
155          ($p = strrpos($s, "/")) and ($p++) or
156          ($p = strrpos($s, '\\')) and ($p++);
157          $meta["Content-Disposition"] = 'attachment; filename="'.urlencode(substr($s, $p)).'"';
158       }
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;
164          }
165          else {
166             $o .= ewiki_t("UPL_REJSECT", array('sect' => $sect));
167
168             return($o);
169          }
170       }
171       if (strlen($s = trim($_REQUEST["comment"]))) {
172          $meta["comment"] = $s;
173       }
174
175       $result = ewiki_binary_save_image($upload_file["tmp_name"], "", "RETURN", $meta, "ACCEPT_ALL", $care_for_images=0);
176
177       if ($result) {
178          $o .= ewiki_t("UPL_OK", array('$script'=>ewiki_script(EWIKI_PAGE_DOWNLOAD)));
179          ewiki_log("file uploaded to section '$sect'");
180       }
181       else {
182          $o .= ewiki_t("UPL_ERROR");
183       }
184
185    }
186
187    return($o);
188 }
189
190
191
192
193 function ewiki_page_filedownload($id, $data, $action, $def_sec="") {
194
195    global $ewiki_binary_icons, $ewiki_upload_sections;
196
197    $o = ewiki_make_title($id, $id, 2);
198 #<off>#   $o .= ewiki_t("DWNL_SEEUPL", '$scr'=>ewiki_script("", ""));
199
200
201    #-- params (section, orderby)
202    ($orderby = $_REQUEST["orderby"]) or ($orderby = "created");
203
204    if ($def_sec) {
205       $section = $def_sec;
206    }
207    else {
208       ($section = $_REQUEST["section"]) or ($section = "");
209       if (count($ewiki_upload_sections) > 1) {
210          $oa = array();
211          $ewiki_upload_sections["*"] = "*";
212          if (empty($ewiki_plugins["action"][EWIKI_ACTION_ATTACHMENTS])) {
213             $ewiki_upload_sections["**"] = "**";
214          }
215          foreach ($ewiki_upload_sections as $sec=>$title) {
216             $oa[] = '<a href="' . ewiki_script("", $id, array(
217                "orderby"=>$orderby, "section" => $sec)) .
218                '">' . $title . "</a>";
219          }
220          $o .= '<div align="center" class="darker">'.implode(" &middot; ", $oa).'</div><br />';
221       }
222    }
223
224
225    #-- collect entries
226    $files = array();
227    $sorted = array();
228    $result = ewiki_db::GETALL(array("flags", "meta", "created", "hits"));
229
230    while ($row = $result->get()) {
231       if (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_BINARY) {
232
233          $m = &$row["meta"];
234          if ($m["section"] != $section) {
235             if ($section == "**") {
236             }
237             elseif (($section == "*") && !empty($ewiki_upload_sections[$m["section"]])) {
238             }
239             else {
240                continue;
241             }
242          }
243          if (EWIKI_PROTECTED_MODE && EWIKI_PROTECTED_MODE_HIDING && !ewiki_auth($row["id"], $uuu, "binary-get")) {
244              continue;
245          }   
246
247          $files[$row["id"]] = $row;
248          $sorted[$row["id"]] = $row[$orderby];
249       }
250    }
251
252
253    #-- sort
254    arsort($sorted);
255
256
257    #-- slice
258    ($pnum = $_REQUEST[EWIKI_UP_PAGENUM]) or ($pnum = 0);
259    if (count($sorted) > EWIKI_LIST_LIMIT) {
260       $o_nl .= '<div class="lighter">&gt;&gt; ';
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>  ";
265       }
266       $o_nl .= '</div><br />';
267       $o .= $o_nl;
268    }
269    $sorted = array_slice($sorted, $pnum * EWIKI_LIST_LIMIT, EWIKI_LIST_LIMIT);
270
271
272    #-- output
273    if (empty($sorted)) {
274
275       $o .= ewiki_t("DWNL_NOFILES");
276    }
277    else {
278
279       foreach ($sorted as $id=>$uu) {
280          $row = $files[$id];
281          $o .= ewiki_entry_downloads($row, $section[0]=="*");
282       }
283    }
284
285    $o .= $o_nl;
286
287    return($o);
288
289 }
290
291
292
293
294 function ewiki_entry_downloads($row, $show_section=0) {
295
296    global $ewiki_binary_icons, $ewiki_upload_sections,$ewiki_config;
297
298    $meta = &$row["meta"];
299
300    $id = $row["id"];
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 />";
318
319    $p_icon = "";
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)) { 
322          $p_icon = $i;
323          $p_icon_t = $str;
324       }
325    }
326
327    $o .= ewiki_t(
328      "DWNL_ENTRY_FORMAT",
329      array(
330         "id" => $id,
331         "size" => $p_size,
332         "icon" => ($p_icon ? '<img src="'.$p_icon.'" alt="['.$p_icon_t.']" align="left" width="14" height="14" border="0" /> ' : ''),
333         "time" => $p_time,
334         "hits" => $p_hits,
335         "section" => ($show_section ? ewiki_t('dwnl_section') . ": $p_section<br />" : ''),
336         "type" => $p_ct,
337         "url" =>  ewiki_script_binary("", $row["id"]),
338         "title" => $p_title,
339         'comment' => $p_comment,
340     'control_links'=> ewiki_control_links_list($id, &$data, $ewiki_config["action_links"]["binary_get"],$data["version"])
341      )
342    );
343
344    return($o);
345 }
346
347
348
349 #------------------------------------------------------- per-page uploads ---
350
351
352 function ewiki_action_attachments($id, $data, $action=EWIKI_ACTION_ATTACHMENTS) {
353
354    if (!empty($_FILES[EWIKI_UP_UPLOAD])) {
355       $o .= ewiki_page_fileupload($id, $data, EWIKI_ACTION_ATTACHMENTS, $id);
356    }
357
358    $o .= ewiki_page_filedownload(ucwords(EWIKI_ACTION_ATTACHMENTS) . " " . ewiki_t("of") . " $id", $data, "view", $id);
359
360    unset($_FILES[EWIKI_UP_UPLOAD]);
361    $o .= ewiki_page_fileupload($id, $data, EWIKI_ACTION_ATTACHMENTS, $id);
362
363    return($o);
364
365 }
366
367
368 ?>