Rename xorg_ constants to pl_
[platal.git] / include / wiki / engine.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 require_once 'wiki.inc.php';
23
24 $n = wiki_pagename();
25 if (!$n) {
26 pl_redirect('');
27 }
28
29 new_skinned_page('core/wiki.tpl');
30 $perms = wiki_get_perms($n);
31 $feed = false;
32
33 // Check user perms
34 switch (Env::v('action')) {
35 case 'rss': case 'atom': case 'sdf': case 'dc':
36 wiki_apply_feed_perms($perms[0]);
37 $feed = true;
38 case '': case 'search':
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
50 if ($p = Post::v('setrperms')) {
51 wiki_apply_perms('admin');
52 if (wiki_set_perms($n, $p, $perms[1])) {
53 $perms = wiki_get_perms($n);
54 $page->trigSuccess('Permissions mises à jour');
55 }
56 }
57
58 if ($p = Post::v('setwperms')) {
59 wiki_apply_perms('admin');
60 if (wiki_set_perms($n, $perms[0], $p)) {
61 $perms = wiki_get_perms($n);
62 $page->trigSuccess('Permissions mises à jour');
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);
69 if (Env::v('action') || !$cache_exists) {
70 if ($cache_exists && !$feed) {
71 unlink($wiki_cache);
72 $files = glob($globals->spoolroot . '/spool/templates_c/*cache_' . wiki_filename($n) . '.tpl*');
73 foreach ($files as $file) {
74 unlink($file);
75 }
76 }
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 pl_clear_errors();
84
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
90 $wiki_exists = file_exists(wiki_work_dir() . '/' . wiki_filename($n));
91
92 if ($feed) {
93 $wikiAll = str_replace('dc:contributor', 'author', $wikiAll);
94 $wikiAll = preg_replace('!<author>.*?\..*?\.(\d{4})\|(.*?)</author>!u', '<author>$2 (X$1)</author>', $wikiAll);
95 $wikiAll = str_replace('<link>./', '<link>' . $globals->baseurl . '/' . $platal->ns, $wikiAll);
96 echo $wikiAll;
97 pl_clear_errors();
98 exit;
99 } elseif (Env::v('action')) {
100 $page->assign('pl_extra_header', substr($wikiAll, 0, $i));
101 $wikiAll = substr($wikiAll, $j);
102 } else {
103 if (!$cache_exists && $wiki_exists) {
104 $wikiAll = substr($wikiAll, $j);
105 wiki_putfile($wiki_cache, $wikiAll);
106 } elseif ($cache_exists) {
107 $wikiAll = file_get_contents($wiki_cache);
108 } elseif (S::has_perms()) {
109 $wikiAll = "<p>La page de wiki $n n'existe pas. "
110 . "Il te suffit de <a href='" . str_replace('.', '/', $n) . "?action=edit'>l'éditer</a></p>";
111 } else {
112 $page->changeTpl('core/404.tpl');
113 }
114 }
115
116 // Check user perms
117 wiki_apply_perms($perms[0]);
118
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));
126 if (!$feed && $perms[1] == 'admin' && !Env::v('action') && $wiki_exists) {
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');
134 if (!Env::v('action')) {
135 $url = '/' . str_replace('.', '/', $n) . '?action=rss';
136 if (S::logged()) {
137 $url .= '&user=' . S::v('forlife') . '&hash=' . S::v('core_rss_hash');
138 }
139 $page->setRssLink($n, $url);
140 }
141
142 $page->run();
143
144 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
145 ?>