Fix session bootstrap
[platal.git] / include / wiki / engine.php
CommitLineData
fce088c1 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
22require_once 'wiki.inc.php';
23
24$n = wiki_pagename();
25if (!$n) {
26 pl_redirect('');
27}
28
29new_skinned_page('core/wiki.tpl');
30$perms = wiki_get_perms($n);
0a5cc8b1 31$feed = false;
fce088c1 32
33// Check user perms
34switch (Env::v('action')) {
0a5cc8b1 35 case 'rss': case 'atom': case 'sdf': case 'dc':
0f384dd3 36 wiki_apply_feed_perms($perms['0']);
0a5cc8b1 37 $feed = true;
0f384dd3 38 case '': case 'search':
fce088c1 39 break;
40
41 case 'edit':
42 wiki_apply_perms($perms[1]);
43 break;
44
45 default:
46 wiki_apply_perms('admin');
47 break;
48}
49
50if ($p = Post::v('setrperms')) {
51 wiki_apply_perms('admin');
52 if (wiki_set_perms($n, $p, $perms[1])) {
53 $perms = wiki_get_perms($n);
a7de4ef7 54 $page->trig('Permissions mises à jour');
fce088c1 55 }
56}
57
58if ($p = Post::v('setwperms')) {
59 wiki_apply_perms('admin');
60 if (wiki_set_perms($n, $perms[0], $p)) {
61 $perms = wiki_get_perms($n);
a7de4ef7 62 $page->trig('Permissions mises à jour');
fce088c1 63 }
64}
65
66// Generate cache even if we don't have access rights
67$wiki_cache = wiki_work_dir().'/cache_'.wiki_filename($n).'.tpl';
68$cache_exists = file_exists($wiki_cache);
fce088c1 69if (Env::v('action') || !$cache_exists) {
90489c07 70 if ($cache_exists && !$feed) {
3da341b6 71 unlink($wiki_cache);
c77fed74 72 $files = glob($globals->spoolroot . '/spool/templates_c/*cache_' . wiki_filename($n) . '.tpl*');
73 foreach ($files as $file) {
74 unlink($file);
75 }
3da341b6 76 }
fce088c1 77
78 // we leave pmwiki do whatever it wants and store everything
79 ob_start();
80 require_once($globals->spoolroot.'/wiki/pmwiki.php');
81
82 $wikiAll = ob_get_clean();
83 // the pmwiki skin we are using (almost empty) has these keywords:
84 $i = strpos($wikiAll, "<!--/HeaderText-->");
85 $j = strpos($wikiAll, "<!--/PageLeftFmt-->", $i);
86}
87
3da341b6 88$wiki_exists = file_exists(wiki_work_dir() . '/' . wiki_filename($n));
89
0a5cc8b1 90if ($feed) {
91 $wikiAll = str_replace('dc:contributor', 'author', $wikiAll);
92 $wikiAll = preg_replace('!<author>.*?\..*?\.(\d{4})\|(.*?)</author>!u', '<author>$2 (X$1)</author>', $wikiAll);
c339246f 93 echo $wikiAll;
94 pl_clear_errors();
95 exit;
0a5cc8b1 96} elseif (Env::v('action')) {
fce088c1 97 $page->assign('xorg_extra_header', substr($wikiAll, 0, $i));
98 $wikiAll = substr($wikiAll, $j);
0a5cc8b1 99} else {
3da341b6 100 if (!$cache_exists && $wiki_exists) {
fce088c1 101 $wikiAll = substr($wikiAll, $j);
102 wiki_putfile($wiki_cache, $wikiAll);
3da341b6 103 } elseif ($cache_exists) {
fce088c1 104 $wikiAll = file_get_contents($wiki_cache);
3da341b6 105 } elseif (S::has_perms()) {
106 $wikiAll = "<p>La page de wiki $n n'existe pas. "
a7de4ef7 107 . "Il te suffit de <a href='" . str_replace('.', '/', $n) . "?action=edit'>l'éditer</a></p>";
3da341b6 108 } else {
109 $page->changeTpl('core/404.tpl');
fce088c1 110 }
111}
112
0f384dd3 113// Check user perms
114wiki_apply_perms($perms[0]);
115
fce088c1 116$page->assign('perms', $perms);
117$page->assign('perms_opts', wiki_perms_options());
118
119$page->assign('canedit', wiki_may_have_perms($perms[1]));
120$page->assign('has_perms', wiki_may_have_perms('admin'));
121
122$page->assign('wikipage', str_replace('.', '/', $n));
90489c07 123if (!$feed && $perms[1] == 'admin' && !Env::v('action') && $wiki_exists) {
fce088c1 124 $page->assign('pmwiki_cache', $wiki_cache);
125} else {
126 $page->assign('pmwiki', $wikiAll);
127 $page->assign('text', true);
128}
129$page->addCssLink('wiki.css');
130$page->addJsLink('wiki.js');
90489c07 131if (!Env::v('action')) {
0f384dd3 132 $url = '/' . str_replace('.', '/', $n) . '?action=rss';
133 if (S::logged()) {
134 $url .= '&user=' . S::v('forlife') . '&hash=' . S::v('core_rss_hash');
135 }
136 $page->setRssLink($n, $url);
90489c07 137}
fce088c1 138
139$page->run();
140
a7de4ef7 141// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
fce088c1 142?>