4406402b0c8cf343d06b63dd0831e69fdf0eae52
[atutor.git] / mods / wiki / plugins / interwiki / editable.php
1 <?php
2
3 /*
4    A publically EditableInterMap implementation. Entries therein cannot
5    override the default entries of the bultin intermap array (or the one
6    from the list extension plugin).
7    All entries must be provided in the definition list form:
8      :WikiMoniker:httpz://www.example.net/cgi-bin/wiki.cgi/...
9
10    Set the _APPENDONLY page flag to restrict editing.
11 */
12
13
14 #-- load data from intermap page
15 $ewiki_plugins["init"][] = "ewiki_load_editable_intermap";
16
17
18 function ewiki_load_editable_intermap($uu=0, $uu=0, $uu=0) {
19
20    global $ewiki_plugins, $ewiki_config;
21    $inter = &$ewiki_config["interwiki"];
22
23    #-- fetch from db
24    $id = "EditableInterMap";
25    if ($data = ewiki_db::GET($id)) {
26
27       #-- extract entries
28       if (preg_match_all('/^:(\w+):([^\s]+)/m', $data["content"], $uu)) {
29          foreach ($uu[1] as $i=>$moni) {
30             if (!isset($inter[$moni])) {
31                $inter[$moni] .= $uu[2][$i];
32             }
33       }  }
34       /*
35          WONT_WORK
36          $refs = explode("\n", trim($data["refs"]));
37          for ($n=1; $n<count($refs); $n++) {
38             if (strpos($refs[$n], "://")) {
39                $moni = $refs[$n-1];
40                if (!isset($inter[$moni])) {
41                   $inter[$moni] .= $refs[$n];
42                }
43             }
44          }
45       */
46
47       #-- enable _APPENDONLY part
48       if ($data["flags"] & EWIKI_DB_F_APPENDONLY) {
49          $ewiki_plugins["page"]["EditableInterMap"] = "ewiki_editable_intermap";
50       }
51    }
52 }
53
54
55 #-- provides an append-form to prevent total-editing (which
56 #   otherwise allowed random removal of existing entries)
57 function ewiki_editable_intermap($id, $data, $action) {
58
59    global $ewiki_config;
60
61    $o = "";
62
63    if (($url = $_REQUEST["add_url"]) && ($moni = $_REQUEST["add_moniker"])) {
64       if (!preg_match('#^http[sz]?://(\w{2,}\.)+\w{2,}(:\d+)?[^\[\]\"\s]+$#', $url) || strpos($url, "example")) {
65          $o .= "URL was rejected.";
66       }
67       elseif (!preg_match('#^(['.EWIKI_CHARS_U.']+['.EWIKI_CHARS_L.']+){2,}['.EWIKI_CHARS.']+$#', $moni) || ($moni == "WikiName")) {
68          $o .= "Choosen InterWiki moniker not acceptable.";
69       }
70       else {
71          if ($ewiki_config["interwiki"][$moni]) {
72             $o .= "(Note: eventually overriding earlier entry.)<br />";
73          }
74          $data["content"] =
75              "\n" .
76              trim($data["content"]) .
77              "\n" .
78              ":$moni:$url" .
79              "\n";
80          ewiki_data_update($data);
81          $data["version"]++;
82          if (ewiki_db::WRITE($data)) {
83             $o .= "Map was updated.";
84          } else {
85             $o .= "Error occoured when saving your changes.";
86          }
87       }
88       $o .= "<br />";
89    }
90
91    $o .= ewiki_make_title($id, $id, 2);
92    $o .= ewiki_page_view($id, $data, $action, 0);
93
94    $o .= ewiki_t(<<<EOT
95     <form class="intermap-append" action="$_SERVER[REQUEST_URI]" method="POST" enctype="multipart/form-data">
96       <hr>
97       <input type="hidden" name="id" value="$id">
98       :<input name="add_moniker" value="WikiName" size="16">:<input name="add_url" value="http://www.example.com/..." size="42">
99       <br />
100       <input type="submit" value="_{add}">
101     </form>
102 EOT
103    );
104
105    return($o);
106 }
107
108 ?>