f664acb59a238a85e948997251726a58a17ed9c1
[atutor.git] / mods / wiki / plugins / auth / password_locks.php
1 <?php
2
3 /*
4    This plugin allows to lock individual pages with a password, so they
5    will be editable only when this is given again. You can't use this in
6    conjunction to another auth plugin, this one is standalone.
7    It will probably be of little use for most Wiki setups, and certainly
8    requires an adminstrator to manage it with 'ewikictl' from time to time.
9 */
10
11 #-- switch ewiki into virtual paging mode ;)
12 define("EWIKI_PROTECTED_MODE", 1);
13
14 #-- _auth glue
15 $ewiki_plugins["auth_perm"][] = "ewiki_auth_perm_password_locks";
16 $ewiki_plugins["edit_form_append"][] = "ewiki_edit_field_pwlock";
17 $ewiki_plugins["edit_save"][] = "ewiki_edit_save_pwlock";
18
19 #-- text/translations
20 $ewiki_t["de"]["lock page with"] = "Seite schützen mit";
21 $ewiki_t["de"]["password"] = "Paßwort";
22
23
24 #-- this prints the password query <form>
25 function ewiki_auth_login_password(&$data, &$author, &$ring, $force) {
26
27    global $ewiki_errmsg;
28    static $cookie=0;
29
30    #-- set cookie
31    if ($pw = $_POST["unlock_password"]) {
32       if (!$cookie) {
33          setcookie("unlock_password", $pw, time()+7*24*3600, "/", $_SERVER["SERVER_NAME"]);
34          $cookie++;
35       }
36    }
37    #-- get cookie
38    else {
39       $pw = $_REQUEST["unlock_password"];
40    }
41
42    #-- compare
43    $ok = ($pw == $data["meta"]["password"]);
44
45    #-- print login form
46    if ($force && !$ok) {
47       $ewiki_errmsg = ewiki_t( <<<END
48 <h2>_{This page is locked}</h2>
49 <form action="{$_SERVER['REQUEST_URI']}" method="POST" enctype="multipart/form-data">
50 _{password} <input type="password" size="16" name="unlock_password">
51 <br />
52 <input type="submit" value="_{access}">
53 </form><br /><br />
54 END
55       );
56    }
57
58    return($ok);
59 }
60
61
62 #-- this separates password-protected pages from all others
63 function ewiki_auth_perm_password_locks($id, &$data, $action, $ring, $force) {
64
65    $PROTECT = array(
66       "edit", "delete", "export", "any_other_action",
67    );
68
69    #-- only protect pages with passwords for editing
70    if (in_array($action, $PROTECT)) {
71       if (empty($data["meta"]["password"])) {
72          return(true);
73       }
74       else {
75          $ok = ewiki_auth_login_password($data, $ewiki_author, $ewiki_ring,
76 $force=1);
77          return($ok);
78       }
79    }
80    #-- strip password here, so it won't display accidently
81    elseif ($action == "info") {
82       unset($data["meta"]["password"]);
83       return(true);
84    }
85    #-- pages we don't care
86    else {
87       return(true);
88       // return(NULL);  // for tri-state boolean ["auth_perm"]
89    }
90 }
91
92
93 #-- add password field on edit/ page
94 function ewiki_edit_field_pwlock($id, &$data, $action) {
95    $o = ewiki_t('<br /><br />_{lock page with} <b>_{password}</b> <input type="password" name="lock_password" size="16" value="').htmlentities($data["meta"]["password"]).'"><br />';
96    return($o);
97 }
98
99
100 #-- store password into page db entry {meta} field
101 function ewiki_edit_save_pwlock(&$save, &$old_data) {
102
103    $pw = $_REQUEST["lock_password"];
104    if (true) {
105
106       #-- keep in {meta} field
107       $save["meta"]["password"] = $pw;
108
109       #-- copy it immediately
110       if ($_REQUEST["unlock_password"] != $pw) {
111          setcookie("unlock_password", $pw, time()+7*24*3600, "/");
112          $_COOKIE["unlock_password"] = $pw;
113          $_REQUEST["unlock_password"] = $pw;
114       }
115    }
116
117
118
119 ?>