3e0024be5445f64e9a0310ae1737990a268e1c80
[atutor.git] / mods / wiki / plugins / db / binary_store.php
1 <?php
2
3 /*
4    This plugin intercepts some of the binary handling functions to
5    store uploaded files (as is) into a dedicated directory.
6    Because the ewiki database abstraction layer was not designed to
7    hold large files (because it reads records in one chunk), you may need
8    to use this, else large files may break.
9
10    WARNING: this is actually a hack and not a database layer extension,
11    so it will only work with the ewiki.php script itself. The database
12    administration tools are not aware of this agreement and therefore
13    cannot (for example) backup the externally stored data files!
14    If you later choose to disable this extension, the uploaded (and thus
15    externally stored) files then cannot be accessed any longer, of course.
16
17    - You must load this plugin __before__ the main script, because the
18      binary stuff in ewiki.php always engages automatically.
19    - The store directory can be the same as for dbff (filenames differ).
20    - All the administration tools/ are not aware of this hack, so __you__
21      must take care, when it comes to creating backups.
22 */
23
24
25 #-- config
26 define("EWIKI_DB_STORE_DIRECTORY", "/tmp");     // where to save binary files
27 define("EWIKI_DB_STORE_MINSIZE", 0);            // send smaller files into db
28 define("EWIKI_DB_STORE_MAXSIZE", 32 <<20);      // 32MB max per file (but
29           // there is actually no way to upload such large files via HTTP)
30
31 #  define("EWIKI_DB_STORE_URL", "http://example.com/wiki/files/store/");
32           // allows clients to directly access stored plain data files,
33           // without redirection through ewiki.php, RTFM
34
35
36 #-- glue
37 $ewiki_plugins["binary_store"][] = "ewiki_binary_store_file";
38 $ewiki_plugins["binary_get"][] = "ewiki_binary_store_get_file";
39
40
41 #-- upload
42 function ewiki_binary_store_file(&$filename, &$id, &$meta, $ext=".bin") {
43
44    if (($meta["size"] >= EWIKI_DB_STORE_MINSIZE) && ($meta["size"] <= EWIKI_DB_STORE_MAXSIZE)) {
45
46       #-- generate internal://md5sum
47       if (empty($id)) {
48          $md5sum = md5_file($filename);
49          $id = EWIKI_IDF_INTERNAL . $md5sum . ".$ext";
50          ewiki_log("generated md5sum '$md5sum' from file content");
51       }
52
53       #-- move file to dest. location
54       $dbfname = EWIKI_DB_STORE_DIRECTORY."/".rawurlencode($id);
55       if (@rename($filename, $dbfname) || copy($filename, $dbfname) && unlink($filename)) {
56          $filename = "";
57          $meta["binary_store"] = 1;
58          return(true);
59       }
60       else {
61          ewiki_log("file store error with '$dbfname'", 0);
62       }
63    }
64
65    return(false);
66 }
67
68
69 #-- download
70 function ewiki_binary_store_get_file($id, &$meta) {
71
72    if (@$meta["binary_store"]) {
73
74       #-- check for file
75       $dbfname = EWIKI_DB_STORE_DIRECTORY."/".rawurlencode($id);
76       if (file_exists($dbfname)) {
77          readfile($dbfname);
78          return(true);
79       }
80       else {
81          return(false);
82       }
83    }
84
85 }
86
87
88 ?>