3d9d94cb0b910302a51b910b212889bf1f043444
[platal.git] / plugins / pmwiki.platalAuth.php
1 <?php
2
3 $AuthFunction = 'AuthPlatal';
4
5 $Conditions['logged'] = logged();
6 $Conditions['identified'] = identified();
7 $Conditions['has_perms'] = has_perms();
8 $Conditions['public'] = 'true';
9 $Conditions['only_public'] = !identified();
10
11 function authPerms($pagename, $key, $could=false)
12 {
13 $words = explode(' ', $key);
14 $auth = false;
15 $and = false;
16 foreach ($words as $word) {
17 $iauth = false;
18 if ($word == 'and:') {
19 $and = true;
20 continue;
21 }
22 $parts = explode(':', $word);
23 $cond = $parts[0];
24 $param = $parts[1];
25 if ($cond == 'identified' && $could) {
26 $cond = 'logged';
27 }
28 $iauth = CondText($pagename, 'if '.$cond.' '.$param, true);
29 if ($and) {
30 $auth &= $iauth;
31 } else {
32 $auth |= $iauth;
33 }
34 $and = false;
35 }
36 return $auth;
37 }
38
39 function TryAllAuths($pagename, $level, $page_read, $group_read)
40 {
41 global $DefaultPasswords;
42 if (isset($page_read['passwd'.$level]) && $page_read['passwd'.$level] != '*') {
43 return array('page', $page_read['passwd'.$level]);
44 }
45 if (isset($group_read['passwd'.$level]) && $group_read['passwd'.$level] != '*') {
46 return array('group', $group_read['passwd'.$level]);
47 }
48 if (isset($DefaultPasswords[$level])) {
49 return array('site', $DefaultPasswords[$level]);
50 }
51 return array('none', '');
52 }
53
54 function AuthPlatal($pagename, $level, $authprompt, $since)
55 {
56 global $Conditions;
57 $authUser = false;
58 $authPage = false;
59
60 $page_read = ReadPage($pagename, $since);
61 $groupattr = FmtPageName('$Group/GroupAttributes', $pagename);
62 $group_read = ReadPage($groupattr, $since);
63
64 $levels = array('read', 'attr', 'edit', 'upload');
65
66 foreach ($levels as $l)
67 {
68 list($from, $pass) = TryAllAuths($pagename, $l, $page_read, $group_read);
69 $passwds[$l] = $pass;
70 $pwsources[$l] = $from;
71 }
72
73 if (!isset($Conditions['canedit'])) {
74 $Conditions['canedit'] = authPerms($pagename, $passwds['edit'], true);
75 }
76 if (!isset($Conditions['canattr'])) {
77 $Conditions['canattr'] = authPerms($pagename, $passwds['attr'], true);
78 }
79
80 if (authPerms($pagename, $passwds[$level]))
81 {
82 $page_read['=passwd'] = $passwds;
83 $page_read['=pwsource'] = $pwsources;
84 return $page_read;
85 }
86
87 if ($authprompt && !identified())
88 {
89 new_skinned_page('wiki.tpl', AUTH_MDP);
90 }
91
92 global $page;
93 new_skinned_page('', AUTH_MDP);
94 if (has_perms()) {
95 $page->trig('Erreur : page Wiki inutilisable sur plat/al');
96 } else {
97 $page->trig("Tu n'as pas le droit d'accéder à ce service");
98 }
99 // don't return false or pmwiki will send an exit breaking smarty page
100 return 1;
101 }
102
103 ?>