Move the wiki engine in the core.
[platal.git] / include / wiki.engine.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 $wp = PlWikiPage::currentPage();
23 $n = $wp->path;
24 $feed = false;
25
26 // Check user perms
27 switch (Env::v('action')) {
28 case 'rss': case 'atom': case 'sdf': case 'dc':
29 $wp->prepareFeed();
30 $feed = true;
31 case '': case 'search':
32 break;
33
34 case 'edit':
35 $wp->applyWritePerms();
36 break;
37
38 default:
39 $wp->applyReadPerms();
40 break;
41 }
42
43 if ($p = Post::v('setrperms')) {
44 $wp->applyPerms('admin');
45 if ($wp->setPerms($p, $wp->writePerms())) {
46 $page->trigSuccess('Permissions mises à jour');
47 }
48 }
49
50 if ($p = Post::v('setwperms')) {
51 $wp->applyPerms('admin');
52 if ($wp->setPerms($wp->readPerms(), $p)) {
53 $page->trigSuccess('Permissions mises à jour');
54 }
55 }
56
57 // Generate cache even if we don't have access rights
58 $wiki_cache = $wp->cacheFilename();
59 $cache_exists = file_exists($wiki_cache);
60 if (Env::v('action') || !$cache_exists) {
61 if ($cache_exists && !$feed) {
62 unlink($wiki_cache);
63 }
64
65 // we leave pmwiki do whatever it wants and store everything
66 ob_start();
67 require_once($globals->spoolroot . '/wiki/pmwiki.php');
68
69 $wikiAll = ob_get_clean();
70 pl_clear_errors();
71
72 // the pmwiki skin we are using (almost empty) has these keywords:
73 $i = strpos($wikiAll, "<!--/HeaderText-->");
74 $j = strpos($wikiAll, "<!--/PageLeftFmt-->", $i);
75 }
76
77 $wiki_exists = file_exists($wp->filename());
78
79 $page =& Platal::page();
80 $page->changeTpl('core/wiki.tpl');
81
82 if ($feed) {
83 $wikiAll = str_replace('dc:contributor', 'author', $wikiAll);
84 $wikiAll = preg_replace('!<author>.*?\..*?\.(\d{4})\|(.*?)</author>!u', '<author>$2 (X$1)</author>', $wikiAll);
85 $wikiAll = str_replace('<link>./', '<link>' . $globals->baseurl . '/' . $platal->ns, $wikiAll);
86 echo $wikiAll;
87 pl_clear_errors();
88 exit;
89 } elseif (Env::v('action')) {
90 $page->assign('pl_extra_header', substr($wikiAll, 0, $i));
91 $wikiAll = substr($wikiAll, $j);
92 } else {
93 if (!$cache_exists && $wiki_exists) {
94 $wikiAll = substr($wikiAll, $j);
95 file_put_contents($wiki_cache, $wikiAll);
96 } elseif ($cache_exists) {
97 $wikiAll = file_get_contents($wiki_cache);
98 } elseif (S::has_perms()) {
99 $wikiAll = "<p>La page de wiki $n n'existe pas. "
100 . "Il te suffit de <a href='" . str_replace('.', '/', $n) . "?action=edit'>l'éditer</a></p>";
101 } else {
102 $page->changeTpl('core/404.tpl');
103 }
104 }
105
106 // Check user perms
107 $wp->applyReadPerms();
108
109 $page->assign('perms', array($wp->readPerms(), $wp->writePerms()));
110 $page->assign('perms_opts', PlWikiPage::permOptions());
111
112 $page->assign('canedit', $wp->canWrite());
113 $page->assign('has_perms', S::has_perms());
114
115 $page->assign('wikipage', str_replace('.', '/', $n));
116 if (!$feed && $wp->writePerms() == 'admin' && !Env::v('action') && $wiki_exists) {
117 $page->assign('pmwiki_cache', $wiki_cache);
118 } else {
119 $page->assign('pmwiki', $wikiAll);
120 $page->assign('text', true);
121 }
122 $page->addCssLink('wiki.css');
123 $page->addJsLink('wiki.js');
124 if (!Env::v('action')) {
125 $url = '/' . str_replace('.', '/', $n) . '?action=rss';
126 if (S::logged()) {
127 $url .= '&user=' . S::v('forlife') . '&hash=' . S::v('core_rss_hash');
128 }
129 $page->setRssLink($n, $url);
130 }
131
132 $page->run();
133
134 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
135 ?>