1 | <?php |
---|
2 | /** |
---|
3 | * Simpler Passwortschutz von Textbereichen in einer t29-Seite. |
---|
4 | * Prinzipiell ist dieser Passwortschut wirkungsvoll, sofern HTTPS |
---|
5 | * verwendet wird. Die primitive Übermittlung per GET (oder POST) |
---|
6 | * sorgt aber dafür, dass die Benutzererfahrung schwach ist (das |
---|
7 | * Passwort wird etwa plain-text in der URL angezeigt oder ein einfaches |
---|
8 | * Neuladen einer ansonsten statischen Seite führt stets zu den POST- |
---|
9 | * Warnungen). |
---|
10 | * |
---|
11 | * In der Praxis auf t29 ist dieser Passwortschutz allerdings schon alleine |
---|
12 | * deswegen völlig wirkungslos, weil die Files alle öffentlich per |
---|
13 | * SVN einsehbar sind. Aber Heribert wollte es ja so. Ist nur ein |
---|
14 | * Demonstrator. |
---|
15 | * |
---|
16 | * Beispiel einer verwenden Seite gibt es weiter unten. |
---|
17 | * |
---|
18 | * Es gibt zwei Varianten des Schutzes, wobei der t29FencedContent |
---|
19 | * der allgemeinere ist. Die t29PasswordBarrier hingegen geht nur, |
---|
20 | * wenn die Seite danach aufhört. |
---|
21 | * |
---|
22 | * Aktuelle Demo zu t29FencedContent gibt es unter |
---|
23 | * /physical-computing/geschuetzte-demo.php |
---|
24 | * |
---|
25 | **/ |
---|
26 | |
---|
27 | class t29FencedContent { |
---|
28 | public $password; |
---|
29 | public $id; // used in form #id and in paramkey |
---|
30 | public $paramkey; // computed from $id |
---|
31 | |
---|
32 | function __construct($id, $password=Null) { |
---|
33 | $this->id = $id; |
---|
34 | $this->password = $password; |
---|
35 | $this->paramkey = "pwd-fence-$id"; |
---|
36 | } |
---|
37 | |
---|
38 | function isAuthenticated() { |
---|
39 | return (isset($_GET[$this->paramkey]) && |
---|
40 | $_GET[$this->paramkey] == $this->password); |
---|
41 | } |
---|
42 | |
---|
43 | function failedToAuthenticate() { |
---|
44 | return (isset($_GET[$this->paramkey]) && |
---|
45 | $_GET[$this->paramkey] != $this->password); |
---|
46 | } |
---|
47 | |
---|
48 | function printAnchor() { |
---|
49 | print '<!-- t29FencedContent anchor -->'; |
---|
50 | print '<a id="'.$this->id.'"></a>'; |
---|
51 | } |
---|
52 | |
---|
53 | function printMiniForm() { |
---|
54 | // Print small inline "login/logut" form, Heribert style |
---|
55 | if( $this->failedToAuthenticate()) { |
---|
56 | ?> |
---|
57 | <strong>Passwort falsch</strong>, bitte nochmal probieren: |
---|
58 | <?php |
---|
59 | $this->printLoginForm(); |
---|
60 | } else if($this->isAuthenticated()) { |
---|
61 | $this->printLogoutForm(); |
---|
62 | } else { |
---|
63 | $this->printLoginForm(); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | function printLoginForm() { |
---|
68 | ?> |
---|
69 | <form method="get" style="display: inline-block; font-size: 80%" |
---|
70 | action="#<?php echo $this->id; ?>"> |
---|
71 | <input type="password" placeholder="Passwort" name="<?= $this->paramkey; ?>"> |
---|
72 | <input type="submit" value="Anmelden"> |
---|
73 | </form> |
---|
74 | <?php |
---|
75 | } |
---|
76 | |
---|
77 | function printLogoutForm() { |
---|
78 | ?> |
---|
79 | <form method="get" class="inline"> |
---|
80 | <input type="submit" value="Abmelden"> |
---|
81 | </form> |
---|
82 | <?php |
---|
83 | } |
---|
84 | |
---|
85 | function start() { |
---|
86 | if(!$this->isAuthenticated()) { |
---|
87 | // buffer (thus hide) content |
---|
88 | ob_start(); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | function end() { |
---|
93 | if(!$this->isAuthenticated()) { |
---|
94 | // delete buffer content, thus hide content |
---|
95 | ob_end_clean(); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | /** |
---|
104 | * Simpler Passwortschutz im Sinne einer "Barriere", die inmitten einer |
---|
105 | * t29-Seite stehen kann. |
---|
106 | * Prinzipiell ist dieser Passwortschut wirkungsvoll, sofern HTTPS |
---|
107 | * verwendet wird. Die primitive Übermittlung per GET (oder POST) |
---|
108 | * sorgt aber dafür, dass die Benutzererfahrung schwach ist (das |
---|
109 | * Passwort wird etwa plain-text in der URL angezeigt oder ein einfaches |
---|
110 | * Neuladen einer ansonsten statischen Seite führt stets zu den POST- |
---|
111 | * Warnungen). |
---|
112 | * |
---|
113 | * In der Praxis auf t29 ist dieser Passwortschutz allerdings schon alleine |
---|
114 | * deswegen völlig wirkungslos, weil die Files alle öffentlich per |
---|
115 | * SVN einsehbar sind. Aber Heribert wollte es ja so. Ist nur ein |
---|
116 | * Demonstrator. |
---|
117 | * |
---|
118 | * Beispiel einer verwenden Seite gibt es weiter unten. |
---|
119 | * |
---|
120 | **/ |
---|
121 | |
---|
122 | class t29PasswordBarrier { |
---|
123 | public $password; |
---|
124 | public $paramkey = 't29-pwd-barrier'; |
---|
125 | |
---|
126 | function __construct($password=Null) { |
---|
127 | $this->password = $password; |
---|
128 | } |
---|
129 | |
---|
130 | function isAuthenticated() { |
---|
131 | return (isset($_GET[$this->paramkey]) && |
---|
132 | $_GET[$this->paramkey] == $this->password); |
---|
133 | } |
---|
134 | |
---|
135 | function failedToAuthenticate() { |
---|
136 | return (isset($_GET[$this->paramkey]) && |
---|
137 | $_GET[$this->paramkey] != $this->password); |
---|
138 | } |
---|
139 | |
---|
140 | function printAuthForm() { |
---|
141 | $failed = $this->failedToAuthenticate(); |
---|
142 | |
---|
143 | ?> |
---|
144 | <div class="alert"> |
---|
145 | Passwort benötigt, um diesen Teil der Seite zu sehen: |
---|
146 | <form method="get" class="inline"> |
---|
147 | <input type="password" name="<?= $this->paramkey; ?>"> |
---|
148 | <input type="submit" value="Anmelden"> |
---|
149 | </form> |
---|
150 | </div> |
---|
151 | <?php |
---|
152 | } |
---|
153 | |
---|
154 | function printSuccessForm() { |
---|
155 | ?> |
---|
156 | <div class="alert alert-success"> |
---|
157 | <h4>Erfolgreich eingeloggt</h4> |
---|
158 | Sie sind berechtigt, den folgenden Text zu lesen. |
---|
159 | <form method="get" class="inline"> |
---|
160 | <input type="submit" value="Abmelden"> |
---|
161 | </form> |
---|
162 | </div> |
---|
163 | <?php |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | /* Demo-Seite: |
---|
168 | |
---|
169 | <?php |
---|
170 | $seiten_id = 'passwort-demo'; |
---|
171 | $titel = 'Passwort-Demo'; |
---|
172 | |
---|
173 | $dynamischer_inhalt = true; |
---|
174 | require "../lib/simplepassword.php"; |
---|
175 | $barriere = new t29PasswordBarrier(); |
---|
176 | $barriere->password = "huhn"; |
---|
177 | // $barriere->startCheck(); |
---|
178 | |
---|
179 | require "../lib/technikum29.php"; |
---|
180 | ?> |
---|
181 | |
---|
182 | <h2>Schüleraufgaben</h2> |
---|
183 | |
---|
184 | <p>Den Text hier kann jeder lesen</p> |
---|
185 | |
---|
186 | <?php |
---|
187 | if(! $barriere->isAuthenticated()) { |
---|
188 | $barriere->printAuthForm(); |
---|
189 | return; |
---|
190 | } else { |
---|
191 | $barriere->printSuccessForm(); |
---|
192 | } |
---|
193 | ?> |
---|
194 | |
---|
195 | Den hier kann man nur lesen, wenn man an der Barriere vorbei ist. |
---|
196 | |
---|
197 | */ |
---|