MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / wiki / ewiki / plugins / moodle / downloads.php
blob6e3cc41e0a5dca7fe79d006490a615f0eb7fec18
1 <?php // $Id$
2 require_once("../../config.php");
3 require_once($CFG->libdir.'/filelib.php');
5 # this is the upload/download plugin, which allows to put arbitrary binary
6 # files into the ewiki database using the provided specialized form, or the
7 # standard image upload form below every edit page (if EWIKI_ALLOW_BINARY)
10 #-- settings
12 # Defined in view.php
13 #define("EWIKI_UPLOAD_MAXSIZE", 2*1024*1024);
14 define("EWIKI_PAGE_UPLOAD", "FileUpload");
15 define("EWIKI_PAGE_DOWNLOAD", "FileDownload");
16 define("EWIKI_ACTION_ATTACHMENTS", "attachments"); #-- define to 0 to disable
18 #-- register plugin (main part)
19 $ewiki_plugins["page"][EWIKI_PAGE_UPLOAD] = "ewiki_page_fileupload";
20 $ewiki_plugins["page"][EWIKI_PAGE_DOWNLOAD] = "ewiki_page_filedownload";
21 $ewiki_plugins["action"]["binary"] = "ewiki_binary";
23 #-- allow per-page downloads
24 if (defined("EWIKI_ACTION_ATTACHMENTS") && EWIKI_ACTION_ATTACHMENTS) {
25 $ewiki_plugins["action"][EWIKI_ACTION_ATTACHMENTS] = "ewiki_action_attachments";
26 $ewiki_config["action_links"]["view"][EWIKI_ACTION_ATTACHMENTS] = "Attachments";
30 #-- icons (best given absolute to www root)
31 /*$ewiki_binary_icons = array(
32 ".bin" => "/icons/exec.gif",
33 "application/" => "/icons/exec.gif",
34 "application/octet-stream" => "/icons/exec.gif",
35 ".ogg" => "/icons/son.gif",
36 ".jpeg" => "/icons/pic.gif",
37 "text/" => "/icons/txt.gif",
38 ".pdf" => "/icons/txt.gif",
39 );*/
42 #-- the upload function __can__ use different sections
43 $ewiki_upload_sections = array(
44 "" => "main",
45 # "section2" => "section2",
49 #-- text, translations
50 $ewiki_t["en"]["UPLOAD0"] = "Use this form to upload an arbitrary binary file into the wiki:<br />";
51 $ewiki_t["en"]["UPL_NEWNAM"] = "Save with different filename";
52 $ewiki_t["en"]["UPL_INSECT"] = "Upload into section";
53 $ewiki_t["en"]["UPL_TOOLARGE"] = "Your upload has been rejected, because that file was too large!";
54 $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 />';
55 $ewiki_t["en"]["UPL_OK"] = "Your file was uploaded correctly, please see <a href=\"\$script".EWIKI_PAGE_DOWNLOAD."\">".EWIKI_PAGE_DOWNLOAD."</a>.<br /><br />";
56 $ewiki_t["en"]["UPL_ERROR"] = "We're sorry, but something went wrong during the file upload.<br /><br />";
57 $ewiki_t["en"]["DWNL_SEEUPL"] = 'See also <a href="$script'.EWIKI_PAGE_UPLOAD.'">FileUpload</a>, this page is only about downloading.<br /><br />';
58 $ewiki_t["en"]["DWNL_NOFILES"] = "No files uploaded yet.<br />\n";
59 $ewiki_t["en"]["file"] = "File";
60 $ewiki_t["en"]["of"] = "of";
61 $ewiki_t["en"]["comment"] = "Comment";
62 $ewiki_t["en"]["dwnl_section"] = "download section";
63 $ewiki_t["en"]["DWNL_ENTRY_FORMAT"] =
64 '<div class="download"><a href="$url">$icon$title</a><small>$size<br />'.
65 'uploaded on <b>$time</b>, downloaded <tt>$hits</tt> times<br />'.
66 '(<a href="$url">$id</a>)<br />'.
67 '$section'.'file is of type <tt>$type</tt>'.
68 '$comment'."</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."\">".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 $CFG, $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">'.
105 '<form action="' .
106 ewiki_script( ($action!="view" ? $action : ""), $id).
107 '" method="post" enctype="multipart/form-data">' ;
108 $o .= '<fieldset class="invisiblefieldset">';
109 require_once($CFG->dirroot.'/lib/uploadlib.php');
110 $o .= upload_print_form_fragment(1,array(EWIKI_UP_UPLOAD),array(ewiki_t("file")),false,null,0,0,true);
111 $o .= '<input type="submit" value="' . EWIKI_PAGE_UPLOAD . '" /><br /><br />'
112 .'<b>' . ewiki_t("comment") . '</b><br /><textarea name="comment" cols="35" rows="3"></textarea><br /><br />';
114 if (empty($ewiki_upload_sections[$def_sec])) {
115 $ewiki_upload_sections[$def_sec] = $def_sec;
117 if (count($ewiki_upload_sections) > 1) {
118 if (empty($def_sec)) {
119 $def_sec = $_REQUEST["section"];
121 $o .= '<b>'.ewiki_t("UPL_INSECT").'</b><br /><select name="section">';
122 foreach ($ewiki_upload_sections as $id => $title) {
123 $o .= '<option value="'.$id.'"' .($id==$def_sec?' selected':''). '>'.$title.'</option>';
125 $o .= '</select><br /><br />';
128 $o .= '<b>'.ewiki_t("UPL_NEWNAM").'</b><br /><input type="text" name="new_filename" size="20" /><br /><br />';
130 $o .= '</fieldset></form></div>';
133 elseif ($upload_file["size"] > EWIKI_UPLOAD_MAXSIZE) {
135 $o .= ewiki_t("UPL_TOOLARGE");
138 else {
140 $meta = array(
141 "X-Content-Type" => $upload_file["type"],
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) {
148 if ($uu != $s) {
149 $meta["Original-Filename"] = $s;
151 $s = $uu;
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)).'"';
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;
165 else {
166 $o .= ewiki_t("UPL_REJSECT", array('sect' => $sect));
168 return($o);
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);
177 if ($result) {
178 $o .= ewiki_t("UPL_OK", array('$script'=>ewiki_script()));
180 else {
181 $o .= ewiki_t("UPL_ERROR");
186 return($o);
192 function ewiki_page_filedownload($id, $data, $action, $def_sec="") {
194 global $ewiki_binary_icons, $ewiki_upload_sections;
196 $o = ewiki_make_title($id, $id, 2);
197 #<off># $o .= ewiki_t("DWNL_SEEUPL", '$scr'=>ewiki_script("", ""));
200 #-- params (section, orderby)
201 ($orderby = $_REQUEST["orderby"]) or ($orderby = "created");
203 if ($def_sec) {
204 $section = $def_sec;
206 else {
207 ($section = $_REQUEST["section"]) or ($section = "");
208 if (count($ewiki_upload_sections) > 1) {
209 $oa = array();
210 $ewiki_upload_sections["*"] = "*";
211 if (empty($ewiki_plugins["action"][EWIKI_ACTION_ATTACHMENTS])) {
212 $ewiki_upload_sections["**"] = "**";
214 foreach ($ewiki_upload_sections as $sec=>$title) {
215 $oa[] = '<a href="' . ewiki_script("", $id, array(
216 "orderby"=>$orderby, "section" => $sec)) .
217 '">' . $title . "</a>";
219 $o .= '<div align="center" class="darker">'.implode(" &middot; ", $oa).'</div><br />';
224 #-- collect entries
225 $files = array();
226 $sorted = array();
227 $result = ewiki_database("GETALL", array("flags", "meta", "created", "hits", "userid"));
229 while ($row = $result->get()) {
230 if (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_BINARY) {
232 $m = &$row["meta"];
233 if(!$section) {
234 $section="**";
236 if ($m["section"] != $section) {
237 if ($section == "**") {
239 elseif (($section == "*") && !empty($ewiki_upload_sections[$m["section"]])) {
241 else {
242 continue;
245 else {
248 $files[$row["id"]] = $row;
249 $sorted[$row["id"]] = $row[$orderby];
254 #-- sort
255 arsort($sorted);
258 #-- slice
259 ($pnum = $_REQUEST[EWIKI_UP_PAGENUM]) or ($pnum = 0);
260 if (count($sorted) > EWIKI_LIST_LIMIT) {
261 $o_nl .= '<div class="lighter">&gt;&gt; ';
262 for ($n=0; $n < (int)(count($sorted) / EWIKI_LIST_LIMIT); $n++) {
263 $o_nl .= '<a href="' . ewiki_script("", $id, array(
264 "orderby"=>$orderby, "section"=>$section, EWIKI_UP_PAGENUM=>$n)) .
265 '">[' . $n . "]</a> ";
267 $o_nl .= '</div><br />';
268 $o .= $o_nl;
270 $sorted = array_slice($sorted, $pnum * EWIKI_LIST_LIMIT, EWIKI_LIST_LIMIT);
273 #-- output
274 if (empty($sorted)) {
276 $o .= ewiki_t("DWNL_NOFILES");
278 else {
280 foreach ($sorted as $id=>$uu) {
281 $row = $files[$id];
282 $o .= ewiki_entry_downloads($row, $section[0]=="*", true);
286 $o .= $o_nl;
288 return($o);
295 function ewiki_entry_downloads($row, $show_section=0, $fullinfo=false) {
297 global $ewiki_binary_icons, $ewiki_upload_sections;
299 $meta = &$row["meta"];
301 $id = $row["id"];
302 $p_title = basename($meta["Content-Location"]);
303 $p_time = userdate($row["created"]);
306 $p_hits = ($row["hits"] ? $row["hits"] : "0");
307 $p_size = $meta["size"];
308 $p_size = isset($p_size) ? (", " . ($p_size>=4096 ? round($p_size/1024)."K" : $p_size." bytes")) : "";
309 $p_ct1 = $meta["Content-Type"];
310 $p_ct2 = $meta["X-Content-Type"];
311 if ($p_ct1==$p_ct2) { unset($p_ct2); }
312 if ($p_ct1 && !$p_ct2) { $p_ct = "<tt>$p_ct1</tt>"; }
313 elseif (!$p_ct1 && $p_ct2) { $p_ct = "<tt>$p_ct2</tt>"; }
314 elseif ($p_ct1 && $p_ct2) { $p_ct = "<tt>$p_ct1</tt>, <tt>$p_ct2</tt>"; }
315 else { $p_ct = "<tt>application/octet-stream</tt>"; }
316 $p_section = $ewiki_upload_sections[$meta["section"]];
317 $p_section = $p_section ? $p_section : $meta["section"];
318 $p_comment = strlen($meta["comment"]) ? '<table border="1" cellpadding="2" cellspacing="0"><tr><td class="lighter">'.
319 str_replace('</p>', '', str_replace('<p>', '',
320 ewiki_format($meta["comment"]))) . '</td></tr></table>' : "<br />";
322 $p_icon = "";
323 /*foreach ($ewiki_binary_icons as $str => $i) {
324 if (empty($str) || strstr($row["Content-Location"], $str) || strstr($p_ct, $str) || strstr($p_ct2, $str)) {
325 $p_icon = $i;
326 $p_icon_t = $str;
330 /// Moodle Icon Handling
331 global $CFG;
332 $icon = mimeinfo("icon", $id);
333 $p_icon="$CFG->pixpath/f/$icon";
334 $p_icon_t="";
336 $info->id = $id;
337 $info->size = $p_size;
338 $info->icon = ($p_icon ? '<img src="'.$p_icon.'" alt="['.$p_icon_t.']" class="icon" /> ' : '');
339 $info->time = $p_time;
340 $info->hits = $p_hits;
341 $info->section = ($show_section ? ewiki_t('dwnl_section') . ": $p_section<br />" : '');
342 $info->type = $p_ct;
343 $info->url = ewiki_script_binary("", $row["id"]);
344 $info->title = $p_title;
345 $info->comment = format_text($p_comment);
347 if ($fullinfo) {
348 if ($user = get_record('user', 'id', $row['userid'])) {
349 if (!isset($course->id)) {
350 $course->id = 1;
352 $picture = print_user_picture($user->id, $course->id, $user->picture, false, true, true);
353 $value = $picture." <a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">".
354 fullname($user)."</a>";
357 $o .= '<a href="'.$info->url.'">'.$info->icon.$info->title.'</a>'.$info->size.'<br />'.
358 $info->comment.
359 $info->section." ".get_string("fileisoftype","wiki").": ".$info->type.'<br />'.
360 get_string("uploadedon","wiki").": ".$info->time.", ".
361 ' by '.$value.'<br />'.
362 get_string("downloadtimes","wiki",$info->hits)."<br />".
363 // '(<a href="'.$info->url.'">'.$info->id."</a>)<br />".
364 '<br /><br />';
366 else {
367 // global $moodle_format; // from wiki/view.php
368 $o .= '<a href="'.$info->url.'">'.$info->icon.$info->title.'</a>'.$info->size.'<br />'.
369 $info->comment.'<br /><br />';
370 // $o = format_text($o, $moodle_format);
375 ewiki_t("DWNL_ENTRY_FORMAT", $info);
377 return($o);
382 #------------------------------------------------------- per-page uploads ---
385 function ewiki_action_attachments($id, $data, $action=EWIKI_ACTION_ATTACHMENTS) {
387 if (!empty($_FILES[EWIKI_UP_UPLOAD])) {
388 $o .= ewiki_page_fileupload($id, $data, EWIKI_ACTION_ATTACHMENTS, $id);
391 $o .= ewiki_page_filedownload(ucwords(EWIKI_ACTION_ATTACHMENTS) . " " . ewiki_t("of") . " $id", $data, "view", $id);
393 unset($_FILES[EWIKI_UP_UPLOAD]);
394 $o .= ewiki_page_fileupload($id, $data, EWIKI_ACTION_ATTACHMENTS, $id);
396 return($o);