b4aefcae1003e7ab4c0bfa0f2bb4ce7c81591336
[platal.git] / include / wiki.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 $wikisites = array('xorg','xnet');
22
23 function wiki_pagename() {
24 $n = str_replace('/', '.', Env::get('n', false));
25 $keywords = explode('.', $n);
26 $count = count($keywords);
27 if ($count == 1)
28 $n = $keywords[0].".".$keywords[0];
29 else
30 $n = $keywords[$count - 2].".".$keywords[$count - 1];
31 global $globals;
32 if ($wikiurl && ($urln = str_replace('.', '/', $n)) != Env::get('n') && $n != Env::get('n'))
33 redirect($globals->relurl.'/'.$urln);
34 $_REQUEST['n'] = $n;
35 return $n;
36 }
37
38 function wiki_work_dir() {
39 global $globals;
40 return realpath($globals->spoolroot.'htdocs/'.$globals->wiki->workdir);
41 }
42
43 function wiki_template($n) {
44 global $wikisite;
45 return $tpl = wiki_work_dir().'/cache_'.$wikisite.'_'.$n.'.tpl';
46 }
47
48 // several files are used for wiki :
49 // - spool/wiki.d/PageName : the wiki page
50 // - spool/wiki.d/cache_PageName.tpl : the template cache
51 // - spool/templates_c/%%...%%cache_PageName.tpl.php : the PHP from Smarty
52 function wiki_clear_cache($n) {
53 global $page, $wikisite, $wikisites;
54 $oldwikisite = $wikisite;
55 foreach ($wikisites as $s) {
56 $wikisite = $s;
57 $tpl = wiki_template($n);
58 @unlink($tpl);
59 $page->clear_compiled_tpl($tpl);
60 }
61 $wikisite = $oldwikisite;
62 }
63
64 // editing pages are not static but used templates too, so we used
65 // temp template files containing result from wiki
66 function wiki_create_tmp($content) {
67 $tmpfile = tempnam(wiki_work_dir(), "temp_");
68 $f = fopen($tmpfile, 'w');
69 fputs($f, $content);
70 fclose($f);
71 return $tmpfile;
72 }
73
74 function wiki_clean_tmp() {
75 // clean old tmp files (more than one hour)
76 $wiki_work_dir = wiki_work_dir();
77 $dh = opendir(wiki_work_dir());
78 $time = time();
79 while (($file = readdir($dh)) !== false) {
80 if (strpos($file, 'temp_') === 0) {
81 $created = filectime($wiki_work_dir.'/'.$file);
82 if ($time-$created > 60 * 60)
83 @unlink($wiki_work_dir.'/'.$file);
84 }
85 }
86 }
87
88 function wiki_assign_auth() {
89 global $page, $wiki_auths;
90 $page->assign('logged', logged());
91 $page->assign('identified', identified());
92 $page->assign('has_perms', has_perms());
93 $page->assign('public', true);
94 $page->assign('wiki_admin', has_perms() && identified());
95 }
96
97 // cannot be in a function because pmwiki use all vars as if it was globals
98 //function new_wiki_page() {
99 global $page, $globals;
100 // the wiki keword is given in the n var
101 if ( $n = wiki_pagename() )
102 {
103
104 $wiki_template = wiki_template($n);
105 $tmpfile_exists = file_exists($wiki_template);
106
107 // don't recreate the tpl if it already exists
108 if (Env::get('action') || !$tmpfile_exists)
109 {
110 if ($tmpfile_exists) {
111 wiki_clear_cache($n);
112 }
113
114 // we leave pmwiki do whatever it wants and store everything
115 ob_start();
116 require_once($globals->spoolroot.'/'.$globals->wiki->wikidir.'/pmwiki.php');
117
118 $wikiAll = ob_get_clean();
119 // the pmwiki skin we are using (almost empty) has these keywords:
120 $i = strpos($wikiAll, "<!--/HeaderText-->");
121 $j = strpos($wikiAll, "<!--/PageLeftFmt-->", $i);
122
123 }
124 if (Env::get('action'))
125 {
126 // clean old tmp files
127 wiki_clean_tmp();
128 $page->assign('xorg_extra_header', substr($wikiAll, 0, $i));
129 // create new tmp files with editing page from wiki engine
130 $wiki_template = wiki_create_tmp(substr($wikiAll, $j));
131 } else {
132 if (!$tmpfile_exists)
133 {
134 $f = fopen($wiki_template, 'w');
135 fputs($f, substr($wikiAll, $j));
136 fclose($f);
137 }
138 }
139 }
140 //return $wiki_template;
141 //}
142 ?>