Merge branch 'master' of /home/git/platal into profile_edit
[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':
a2b461ce 36 wiki_apply_feed_perms($perms[0]);
0a5cc8b1 37 $feed = true;
787bb3d7 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();
c9653a57 83 pl_clear_errors();
84
fce088c1 85 // the pmwiki skin we are using (almost empty) has these keywords:
86 $i = strpos($wikiAll, "<!--/HeaderText-->");
87 $j = strpos($wikiAll, "<!--/PageLeftFmt-->", $i);
88}
89
3da341b6 90$wiki_exists = file_exists(wiki_work_dir() . '/' . wiki_filename($n));
91
0a5cc8b1 92if ($feed) {
93 $wikiAll = str_replace('dc:contributor', 'author', $wikiAll);
94 $wikiAll = preg_replace('!<author>.*?\..*?\.(\d{4})\|(.*?)</author>!u', '<author>$2 (X$1)</author>', $wikiAll);
75d6f6c5 95 $wikiAll = str_replace('<link>./', '<link>' . $globals->baseurl . '/' . $platal->ns, $wikiAll);
c339246f 96 echo $wikiAll;
97 pl_clear_errors();
98 exit;
0a5cc8b1 99} elseif (Env::v('action')) {
fce088c1 100 $page->assign('xorg_extra_header', substr($wikiAll, 0, $i));
101 $wikiAll = substr($wikiAll, $j);
0a5cc8b1 102} else {
3da341b6 103 if (!$cache_exists && $wiki_exists) {
fce088c1 104 $wikiAll = substr($wikiAll, $j);
105 wiki_putfile($wiki_cache, $wikiAll);
3da341b6 106 } elseif ($cache_exists) {
fce088c1 107 $wikiAll = file_get_contents($wiki_cache);
3da341b6 108 } elseif (S::has_perms()) {
109 $wikiAll = "<p>La page de wiki $n n'existe pas. "
a7de4ef7 110 . "Il te suffit de <a href='" . str_replace('.', '/', $n) . "?action=edit'>l'éditer</a></p>";
3da341b6 111 } else {
112 $page->changeTpl('core/404.tpl');
fce088c1 113 }
114}
115
0f384dd3 116// Check user perms
117wiki_apply_perms($perms[0]);
118
fce088c1 119$page->assign('perms', $perms);
120$page->assign('perms_opts', wiki_perms_options());
121
122$page->assign('canedit', wiki_may_have_perms($perms[1]));
123$page->assign('has_perms', wiki_may_have_perms('admin'));
124
125$page->assign('wikipage', str_replace('.', '/', $n));
90489c07 126if (!$feed && $perms[1] == 'admin' && !Env::v('action') && $wiki_exists) {
fce088c1 127 $page->assign('pmwiki_cache', $wiki_cache);
128} else {
129 $page->assign('pmwiki', $wikiAll);
130 $page->assign('text', true);
131}
132$page->addCssLink('wiki.css');
133$page->addJsLink('wiki.js');
90489c07 134if (!Env::v('action')) {
0f384dd3 135 $url = '/' . str_replace('.', '/', $n) . '?action=rss';
136 if (S::logged()) {
137 $url .= '&user=' . S::v('forlife') . '&hash=' . S::v('core_rss_hash');
787bb3d7 138 }
0f384dd3 139 $page->setRssLink($n, $url);
90489c07 140}
fce088c1 141
142$page->run();
143
a7de4ef7 144// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
fce088c1 145?>