0df3edb9 |
1 | <?php |
2 | /*************************************************************************** |
5ddeb07c |
3 | * Copyright (C) 2003-2007 Polytechnique.org * |
0df3edb9 |
4 | * http://opensource.polytechnique.org/ * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
9 | * (at your option) any later version. * |
10 | * * |
11 | * This program is distributed in the hope that it will be useful, * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
14 | * GNU General Public License for more details. * |
15 | * * |
16 | * You should have received a copy of the GNU General Public License * |
17 | * along with this program; if not, write to the Free Software * |
18 | * Foundation, Inc., * |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
20 | ***************************************************************************/ |
0df3edb9 |
21 | |
3b962ded |
22 | function wiki_pagename() |
23 | { |
5e2307dc |
24 | if (!Get::v('n')) { |
ed912c6e |
25 | return null; |
26 | } |
06a5e65b |
27 | |
5e2307dc |
28 | $words = explode('/', trim(Get::v('n'), '/')); |
06a5e65b |
29 | if (count($words) == 2) { |
30 | return join('.', $words); |
31 | } |
32 | |
33 | array_unshift($words, $words[0]); |
34 | $b = array_pop($words); |
35 | $a = array_pop($words); |
36 | |
8b00e0e0 |
37 | pl_redirect($a.'/'.$b); |
0df3edb9 |
38 | } |
39 | |
3b962ded |
40 | function wiki_filename($s) |
41 | { |
42 | if (@iconv('utf-8', 'utf-8', $s) == $s) { |
caf4ad02 |
43 | return $s; |
3b962ded |
44 | } |
caf4ad02 |
45 | return utf8_encode($s); |
3b962ded |
46 | } |
47 | |
48 | function wiki_work_dir() |
49 | { |
0df3edb9 |
50 | global $globals; |
06a5e65b |
51 | return $globals->spoolroot.'/spool/wiki.d'; |
0df3edb9 |
52 | } |
53 | |
ed912c6e |
54 | function wiki_clear_all_cache() |
55 | { |
d7a8b04c |
56 | system('rm -f '.wiki_work_dir().'/cache_*'); |
0df3edb9 |
57 | } |
58 | |
3aec1c21 |
59 | function wiki_perms_options() { |
a7de4ef7 |
60 | return array('public' => 'Public', 'logged' => 'Connecté', |
61 | 'mdp' => 'Authentifié', 'admin' => 'Admin'); |
3aec1c21 |
62 | } |
63 | |
89232034 |
64 | function wiki_get_perms($n) |
f6ce9a88 |
65 | { |
3b962ded |
66 | $file = wiki_work_dir().'/'.wiki_filename(str_replace('/', '.', $n)); |
89232034 |
67 | $lines = explode("\n", @file_get_contents($file)); |
f6ce9a88 |
68 | foreach ($lines as $line) { |
1a013db7 |
69 | @list($k, $v) = explode('=', $line, 2); |
f6ce9a88 |
70 | if ($k == 'platal_perms') { |
71 | return explode(':', $v); |
72 | } |
73 | } |
74 | return array('logged', 'admin'); |
75 | } |
76 | |
89232034 |
77 | function wiki_putfile($f, $s) |
78 | { |
79 | $fp = fopen($f, 'w'); |
80 | fputs($fp, $s); |
81 | fclose($fp); |
82 | } |
83 | |
84 | function wiki_set_perms($n, $pr, $pw) |
85 | { |
3b962ded |
86 | $file = wiki_work_dir().'/'.wiki_filename(str_replace('/', '.', $n)); |
87 | if (!file_exists($file)) { |
89232034 |
88 | return false; |
3b962ded |
89 | } |
89232034 |
90 | |
91 | $p = $pr . ':' . $pw; |
92 | |
93 | $lines = explode("\n", file_get_contents($file)); |
94 | foreach ($lines as $i => $line) { |
95 | list($k, $v) = explode('=', $line, 2); |
96 | if ($k == 'platal_perms') { |
97 | $lines[$i] = 'platal_perms='.$p; |
98 | wiki_putfile($file, join("\n", $lines)); |
99 | return true; |
100 | } |
101 | } |
102 | |
103 | array_splice($lines, 1, 0, array('platal_perms='.$p)); |
104 | wiki_putfile($file, join("\n", $lines)); |
105 | return true; |
106 | } |
107 | |
99ce0a25 |
108 | function wiki_may_have_perms($perm) { |
109 | switch ($perm) { |
110 | case 'public': return true; |
111 | case 'logged': return S::logged(); |
112 | case 'mdp': return S::logged(); |
113 | default: return S::has_perms(); |
114 | } |
115 | } |
116 | |
0f384dd3 |
117 | function wiki_apply_feed_perms($perm) |
118 | { |
119 | if ($perm == 'public') { |
120 | return; |
121 | } |
122 | |
123 | require_once 'rss.inc.php'; |
124 | $uid = init_rss(null, Env::v('user'), Env::v('hash')); |
a2b461ce |
125 | $res = XDB::query('SELECT user_id AS uid, IF (nom_usage <> \'\', nom_usage, nom) AS nom, prenom, perms |
0f384dd3 |
126 | FROM auth_user_md5 |
127 | WHERE user_id = {?}', $uid); |
128 | if (!$res->numRows()) { |
129 | exit; |
130 | } |
131 | $table = $res->fetchOneAssoc(); |
132 | $_SESSION = array_merge($_SESSION, $table, array('forlife' => Env::v('user'))); |
a2b461ce |
133 | require_once 'xorg/session.inc.php'; |
134 | $_SESSION['perms'] =& XorgSession::make_perms($_SESSION['perms']); |
135 | if ($perm == 'logged' || $_SESSION['perms']->hasFlag('admin')) { |
0f384dd3 |
136 | return; |
137 | } |
138 | exit; |
139 | } |
140 | |
f6ce9a88 |
141 | function wiki_apply_perms($perm) { |
c0d6753f |
142 | global $page, $platal, $globals; |
f6ce9a88 |
143 | |
144 | switch ($perm) { |
145 | case 'public': |
146 | return; |
147 | |
148 | case 'logged': |
c0d6753f |
149 | if (!call_user_func(array($globals->session, 'doAuthCookie'))) { |
fce088c1 |
150 | $platal = empty($GLOBALS['IS_XNET_SITE']) ? new Platal() : new Xnet(); |
c0d6753f |
151 | $platal->force_login($page); |
6f97366a |
152 | } |
c0d6753f |
153 | return; |
f6ce9a88 |
154 | |
155 | default: |
c0d6753f |
156 | if (!call_user_func(array($globals->session, 'doAuth'))) { |
6f97366a |
157 | $platal = empty($GLOBALS['IS_XNET_SITE']) ? new Platal() : new Xnet(); |
f6ce9a88 |
158 | $platal->force_login($page); |
159 | } |
160 | if ($perm == 'admin') { |
161 | check_perms(); |
162 | } |
163 | return; |
164 | } |
165 | } |
166 | |
ab694f12 |
167 | function wiki_require_page($pagename) |
168 | { |
169 | global $globals; |
170 | $pagename_slashes = str_replace('.','/',$pagename); |
171 | $pagename_dots = str_replace('/','.',$pagename); |
172 | if (is_file(wiki_work_dir().'/cache_'.$pagename_dots.'.tpl')) return; |
173 | system('wget '.$globals->baseurl.'/'.$pagename_slashes.' -O /dev/null'); |
174 | } |
175 | |
9162f4ed |
176 | function wiki_delete_page($pagename) |
177 | { |
178 | $pagename_dots = str_replace('/','.',$pagename); |
179 | if (!strpos($pagename_dots, '.')) { |
180 | return false; |
181 | } |
182 | $file = wiki_work_dir().'/'.wiki_filename($pagename_dots); |
eaf30d86 |
183 | $cachefile = wiki_work_dir().'/cache_'.$pagename_dots.'.tpl'; |
9162f4ed |
184 | if (is_file($cachefile)) { |
185 | unlink($cachefile); |
186 | } |
187 | if (!is_file($file)) { |
188 | return false; |
189 | } |
190 | unlink($file); |
191 | return true; |
192 | } |
193 | |
194 | function wiki_links_in_line($line, $groupname) |
195 | { |
196 | $links = array(); |
197 | if (preg_match_all('@\[\[([^~][^\]\|\?#]*)((\?|#)[^\]\|]+)?(\\|[^\]]+)?\]\]@', $line, $matches, PREG_OFFSET_CAPTURE)) { |
198 | foreach ($matches[1] as $j => $link) if (!preg_match('@http://@', $link[0])) { |
199 | $mylink = str_replace('/','.',trim($link[0])); |
200 | $sup = trim(substr($matches[2][$j][0],1)); |
201 | $alt = trim(substr($matches[4][$j][0],1)); |
202 | $newlink = str_replace(' ','',ucwords($mylink)); |
203 | if (strpos($newlink,'.') === false) { |
204 | $newlink = $groupname.'.'.$newlink; |
205 | } |
206 | if (!$alt && $mylink != $newlink) { |
207 | $alt = trim($link[0]); |
208 | } |
209 | $links[] = array( |
210 | 'pos' => $matches[0][$j][1], |
211 | 'size' => strlen($matches[0][$j][0]), |
212 | 'href' => $newlink, |
213 | 'sup' => $sup, |
214 | 'alt' => $alt, |
215 | 'group' => substr($mylink, 0, strpos($mylink, '.'))); |
216 | } |
217 | } |
218 | return $links; |
219 | } |
220 | |
221 | function wiki_rename_page($pagename, $newname, $changeLinks = true) |
222 | { |
223 | $pagename_dots = str_replace('/','.',$pagename); |
224 | $newname_dots = str_replace('/','.',$newname); |
225 | if (!strpos($pagename_dots, '.') || !strpos($newname_dots, '.')) { |
226 | return false; |
227 | } |
228 | $groupname = substr($pagename_dots, 0, strpos($pagename_dots,'.')); |
229 | $newgroupname = substr($newname_dots, 0, strpos($pagename_dots,'.')); |
eaf30d86 |
230 | |
9162f4ed |
231 | $file = wiki_work_dir().'/'.wiki_filename($pagename_dots); |
232 | $newfile = wiki_work_dir().'/'.wiki_filename($newname_dots); |
233 | if (!is_file($file)) { |
234 | // old page doesn't exist |
235 | return false; |
236 | } |
237 | if (!rename($file, $newfile)) { |
238 | // impossible to renama page |
239 | return false; |
240 | } |
241 | |
242 | if (!$changeLinks) { |
243 | return true; |
244 | } |
245 | |
246 | $changedLinks = 0; |
247 | // change name inside this folder and ingroup links if changing group |
248 | $lines = explode("\n", file_get_contents($newfile)); |
249 | $changed = false; |
250 | foreach ($lines as $i => $line) { |
251 | list($k, $v) = explode('=', $line, 2); |
252 | if ($k == 'name' && $v == $pagename_dots) { |
253 | $lines[$i] = 'name='.$newname_dots; |
254 | $changed = true; |
255 | } else if ($groupname != $newgroupname) { |
256 | $links = wiki_links_in_line($line, $groupname); |
257 | $newline = ''; $last = 0; |
258 | foreach ($links as $link) if ($link['group'] == $groupname) { |
259 | $newline .= substr($line, $last, $link['pos']); |
260 | $newline .= '[['.$link['href'].$link['sup'].($link['alt']?(' |'.$link['alt']):'').']]'; |
261 | $last = $link['pos']+$link['size']; |
262 | $changedLinks++; |
263 | } |
264 | if ($last != 0) { |
265 | $newline .= substr($line, $last); |
266 | $lines[$i] = $newline; |
267 | $changed = true; |
268 | } |
269 | } |
270 | } |
271 | wiki_putfile($newfile, join("\n", $lines)); |
272 | |
273 | // change wiki links in all wiki pages |
274 | $endname = substr($pagename_dots, strpos($pagename_dots,'.')+1); |
275 | $pages = array(); |
276 | exec("grep ".$endname." ".wiki_work_dir()."/* -sc", $pages); |
277 | foreach($pages as $line) { |
278 | if (preg_match('%/([^/:]+):([0-9]+)$%', $line, $vals) && $vals[2] > 0) { |
279 | $inpage = $vals[1]; |
280 | $lines = explode("\n", file_get_contents(wiki_work_dir().'/'.$inpage)); |
281 | $changed = false; |
282 | // find all wiki links in page and change if linked to this page |
283 | foreach ($lines as $i => $line) { |
284 | $links = wiki_links_in_line($line, substr($inpage, 0, strpos($inpage, '.'))); |
285 | $newline = ''; $last = 0; |
286 | foreach ($links as $link) { |
287 | if ($link['href'] == $pagename_dots) { |
288 | $newline .= substr($line, $last, $link['pos']); |
289 | $newline .= '[['.$newname_dots.$link['sup'].($link['alt']?(' |'.$link['alt']):'').']]'; |
290 | $last = $link['pos']+$link['size']; |
291 | $changedLinks++; |
292 | } |
293 | } |
294 | if ($last != 0) { |
295 | $newline .= substr($line, $last); |
296 | $lines[$i] = $newline; |
297 | $changed = true; |
298 | } |
299 | } |
300 | if ($changed) |
301 | { |
302 | wiki_putfile(wiki_work_dir().'/'.$inpage, join("\n", $lines)); |
303 | } |
304 | } |
305 | } |
306 | if ($changedLinks > 0) { |
307 | return $changedLinks; |
308 | } |
309 | return true; |
310 | } |
311 | |
312 | function wiki_rename_folder($pagename, $newname, $changeLinks = true) |
313 | { |
314 | } |
315 | |
a7de4ef7 |
316 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0df3edb9 |
317 | ?> |