e37451581e08688ed2ed5b8076ae7c2d9ae8cd92
2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 function wiki_pagename()
28 $words = explode('/', trim(Get
::v('n'), '/'));
29 if (count($words) == 2) {
30 return join('.', $words);
33 array_unshift($words, $words[0]);
34 $b = array_pop($words);
35 $a = array_pop($words);
37 pl_redirect($a.'/'.$b);
40 function wiki_filename($s)
42 if (@iconv
('utf-8', 'utf-8', $s) == $s) {
45 return utf8_encode($s);
48 function wiki_work_dir()
51 return $globals->spoolroot
.'/spool/wiki.d';
54 function wiki_clear_all_cache()
56 system('rm -f '.wiki_work_dir().'/cache_*');
59 function wiki_perms_options() {
60 return array('public' => 'Public', 'logged' => 'Connecté',
61 'mdp' => 'Authentifié', 'admin' => 'Admin');
64 function wiki_get_perms($n)
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);
74 return array('logged', 'admin');
77 function wiki_putfile($f, $s)
84 function wiki_set_perms($n, $pr, $pw)
86 $file = wiki_work_dir().'/'.wiki_filename(str_replace('/', '.', $n));
87 if (!file_exists($file)) {
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));
103 array_splice($lines, 1, 0, array('platal_perms='.$p));
104 wiki_putfile($file, join("\n", $lines));
108 function wiki_may_have_perms($perm) {
110 case 'public': return true
;
111 case 'logged': return S
::logged();
112 case 'mdp': return S
::logged();
113 default: return S
::has_perms();
117 function wiki_apply_feed_perms($perm)
119 if ($perm == 'public') {
123 require_once 'rss.inc.php';
124 $uid = init_rss(null
, Env
::v('user'), Env
::v('hash'));
125 $res = XDB
::query('SELECT user_id AS uid, IF (nom_usage <> \'\', nom_usage, nom) AS nom, prenom, perms
127 WHERE user_id = {?}', $uid);
128 if (!$res->numRows()) {
131 $table = $res->fetchOneAssoc();
132 $_SESSION = array_merge($_SESSION, $table, array('forlife' => Env
::v('user')));
133 require_once 'xorg/session.inc.php';
134 $_SESSION['perms'] =& XorgSession
::make_perms($_SESSION['perms']);
135 if ($perm == 'logged' ||
$_SESSION['perms']->hasFlag('admin')) {
141 function wiki_apply_perms($perm) {
142 global $page, $platal, $globals;
149 if (!call_user_func(array($globals->session
, 'doAuthCookie'))) {
150 $platal = empty($GLOBALS['IS_XNET_SITE']) ?
new Platal() : new Xnet();
151 $platal->force_login($page);
156 if (!call_user_func(array($globals->session
, 'doAuth'))) {
157 $platal = empty($GLOBALS['IS_XNET_SITE']) ?
new Platal() : new Xnet();
158 $platal->force_login($page);
160 if ($perm == 'admin') {
167 function wiki_require_page($pagename)
170 $pagename_slashes = str_replace('.','/',$pagename);
171 $pagename_dots = str_replace('/','.',$pagename);
172 if (is_file(wiki_work_dir().'/cache_'.$pagename_dots.'.tpl')) {
175 system('wget --no-check-certificate '. escapeshellarg($globals->baseurl
.'/'.$pagename_slashes) . ' -O /dev/null');
178 function wiki_delete_page($pagename)
180 $pagename_dots = str_replace('/','.',$pagename);
181 if (!strpos($pagename_dots, '.')) {
184 $file = wiki_work_dir().'/'.wiki_filename($pagename_dots);
185 $cachefile = wiki_work_dir().'/cache_'.$pagename_dots.'.tpl';
186 if (is_file($cachefile)) {
189 if (!is_file($file)) {
196 function wiki_links_in_line($line, $groupname)
199 if (preg_match_all('@\[\[([^~][^\]\|\?#]*)((\?|#)[^\]\|]+)?(\\|[^\]]+)?\]\]@', $line, $matches, PREG_OFFSET_CAPTURE
)) {
200 foreach ($matches[1] as $j => $link) if (!preg_match('@http://@', $link[0])) {
201 $mylink = str_replace('/','.',trim($link[0]));
202 $sup = trim(substr($matches[2][$j][0],1));
203 $alt = trim(substr($matches[4][$j][0],1));
204 $newlink = str_replace(' ','',ucwords($mylink));
205 if (strpos($newlink,'.') === false
) {
206 $newlink = $groupname.'.'.$newlink;
208 if (!$alt && $mylink != $newlink) {
209 $alt = trim($link[0]);
212 'pos' => $matches[0][$j][1],
213 'size' => strlen($matches[0][$j][0]),
217 'group' => substr($mylink, 0, strpos($mylink, '.')));
223 function wiki_rename_page($pagename, $newname, $changeLinks = true
)
225 $pagename_dots = str_replace('/','.',$pagename);
226 $newname_dots = str_replace('/','.',$newname);
227 if (!strpos($pagename_dots, '.') ||
!strpos($newname_dots, '.')) {
230 $groupname = substr($pagename_dots, 0, strpos($pagename_dots,'.'));
231 $newgroupname = substr($newname_dots, 0, strpos($pagename_dots,'.'));
233 $file = wiki_work_dir().'/'.wiki_filename($pagename_dots);
234 $newfile = wiki_work_dir().'/'.wiki_filename($newname_dots);
235 if (!is_file($file)) {
236 // old page doesn't exist
239 if (!rename($file, $newfile)) {
240 // impossible to renama page
249 // change name inside this folder and ingroup links if changing group
250 $lines = explode("\n", file_get_contents($newfile));
252 foreach ($lines as $i => $line) {
253 list($k, $v) = explode('=', $line, 2);
254 if ($k == 'name' && $v == $pagename_dots) {
255 $lines[$i] = 'name='.$newname_dots;
257 } else if ($groupname != $newgroupname) {
258 $links = wiki_links_in_line($line, $groupname);
259 $newline = ''; $last = 0;
260 foreach ($links as $link) if ($link['group'] == $groupname) {
261 $newline .= substr($line, $last, $link['pos']);
262 $newline .= '[['.$link['href'].$link['sup'].($link['alt']?
(' |'.$link['alt']):'').']]';
263 $last = $link['pos']+
$link['size'];
267 $newline .= substr($line, $last);
268 $lines[$i] = $newline;
273 wiki_putfile($newfile, join("\n", $lines));
275 // change wiki links in all wiki pages
276 $endname = substr($pagename_dots, strpos($pagename_dots,'.')+
1);
278 exec("grep ".$endname." ".wiki_work_dir()."/* -sc", $pages);
279 foreach($pages as $line) {
280 if (preg_match('%/([^/:]+):([0-9]+)$%', $line, $vals) && $vals[2] > 0) {
282 $lines = explode("\n", file_get_contents(wiki_work_dir().'/'.$inpage));
284 // find all wiki links in page and change if linked to this page
285 foreach ($lines as $i => $line) {
286 $links = wiki_links_in_line($line, substr($inpage, 0, strpos($inpage, '.')));
287 $newline = ''; $last = 0;
288 foreach ($links as $link) {
289 if ($link['href'] == $pagename_dots) {
290 $newline .= substr($line, $last, $link['pos']);
291 $newline .= '[['.$newname_dots.$link['sup'].($link['alt']?
(' |'.$link['alt']):'').']]';
292 $last = $link['pos']+
$link['size'];
297 $newline .= substr($line, $last);
298 $lines[$i] = $newline;
304 wiki_putfile(wiki_work_dir().'/'.$inpage, join("\n", $lines));
308 if ($changedLinks > 0) {
309 return $changedLinks;
314 function wiki_rename_folder($pagename, $newname, $changeLinks = true
)
318 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: