changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / feature / imgfile_naming.php
1 <?php
2
3 /*
4    Enable this plugin to get rid of the "md5md5md5md5md5" in uploaded
5    images` storage/filenames. It can automatically discard frivolous
6    names like "test0.gif" and "DSC00001.jpg" and provides the md5-name
7    as fallback anyhow.
8
9    This plugin must be included() BEFORE the binary_store plugin (if
10    that one was enabled)!
11 */
12
13
14
15 $ewiki_plugins["binary_store"][] = "ewiki_imgupload_better_fn";
16
17
18 function ewiki_imgupload_better_fn(&$fn, &$id, &$meta, &$ext) {
19
20    $parent = $_REQUEST[EWIKI_UP_PARENTID];
21    $name = $meta["Content-Location"];
22    $bad_names = '/^(DSC.?0\d+|test|bild.?\d+|pic.?\d+|image.?\d+|img.?\d+)/i';
23
24    #-- normalize desired name (discard path/ and old .extension)
25    $name = substr($name, strrpos($name, "/\\"));
26    $name = substr($name, 0, strrpos($name, "."));
27    $name = preg_replace('/[^-_+.\w\d]+/', "_", $name);
28
29    #-- filter names
30    if (preg_match($bad_names, $name)) {
31       $name = "";
32    }
33    elseif (strlen($name) < 5) {
34       $name = "";
35    }
36
37    #-- check if wish name is free
38    if ($name) {
39       $name = $name . ".$ext";
40       $found = ewiki_db::FIND(array($name));
41       if ($found[$name]) {
42          $name = "";  // no, is used
43       }
44    }
45
46    #-- else use page name as base
47    if (!$name && $parent) {
48       for ($n=1; $n++; $n<=99) {
49          $name = "$parent.$n.$ext";
50          $found = ewiki_db::FIND(array($name));
51          if ($found[$name]) {
52             $name = "";   // name is already occoupied
53          }
54          else {
55             break;    // done
56          }
57       }
58    }
59
60    #-- assign new internal:// name
61    if ($name) {
62       $id = EWIKI_IDF_INTERNAL . $name;
63    }
64    else {
65       // (it else gets a md5md5md5md5-sum as name)
66    }
67 }
68
69
70 ?>