Fix issues with non-ASCII characters in wiki file names
[platal.git] / include / wiki.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
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 ***************************************************************************/
21
22 function wiki_pagename()
23 {
24 if (!Get::v('n')) {
25 return null;
26 }
27
28 $words = explode('/', trim(Get::v('n'), '/'));
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
37 pl_redirect($a.'/'.$b);
38 }
39
40 function wiki_filename($s)
41 {
42 if (@iconv('utf-8', 'utf-8', $s) == $s) {
43 return $s;
44 }
45 return utf8_encode($s);
46 }
47
48 function wiki_work_dir()
49 {
50 global $globals;
51 return $globals->spoolroot.'/spool/wiki.d';
52 }
53
54 function wiki_clear_all_cache()
55 {
56 system('rm -f '.wiki_work_dir().'/cache_*');
57 }
58
59 function wiki_perms_options() {
60 return array('public' => 'Public', 'logged' => 'Connecté',
61 'mdp' => 'Authentifié', 'admin' => 'Admin');
62 }
63
64 function wiki_get_perms($n)
65 {
66 $file = wiki_work_dir().'/'.wiki_filename(str_replace('/', '.', $n));
67 $lines = explode("\n", @file_get_contents($file));
68 foreach ($lines as $line) {
69 @list($k, $v) = explode('=', $line, 2);
70 if ($k == 'platal_perms') {
71 return explode(':', $v);
72 }
73 }
74 return array('logged', 'admin');
75 }
76
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 {
86 $file = wiki_work_dir().'/'.wiki_filename(str_replace('/', '.', $n));
87 if (!file_exists($file)) {
88 return false;
89 }
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
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
117 function wiki_apply_perms($perm) {
118 global $page, $platal, $globals;
119
120 switch ($perm) {
121 case 'public':
122 return;
123
124 case 'logged':
125 if (!call_user_func(array($globals->session, 'doAuthCookie'))) {
126 $platal = empty($GLOBALS['IS_XNET_SITE']) ? new Platal() : new Xnet();
127 $platal->force_login($page);
128 }
129 return;
130
131 default:
132 if (!call_user_func(array($globals->session, 'doAuth'))) {
133 $platal = empty($GLOBALS['IS_XNET_SITE']) ? new Platal() : new Xnet();
134 $platal->force_login($page);
135 }
136 if ($perm == 'admin') {
137 check_perms();
138 }
139 return;
140 }
141 }
142
143 function wiki_require_page($pagename)
144 {
145 global $globals;
146 $pagename_slashes = str_replace('.','/',$pagename);
147 $pagename_dots = str_replace('/','.',$pagename);
148 if (is_file(wiki_work_dir().'/cache_'.$pagename_dots.'.tpl')) return;
149 system('wget '.$globals->baseurl.'/'.$pagename_slashes.' -O /dev/null');
150 }
151
152 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
153 ?>