2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21 $wikisites = array('xorg','xnet');
23 function wiki_pagename() {
24 $n = str_replace('/', '.', Env
::get('n', false
));
28 $keywords = explode('.', $n);
29 $count = count($keywords);
31 $n = $keywords[0].".".$keywords[0];
33 $n = $keywords[$count - 2].".".$keywords[$count - 1];
35 if (($urln = str_replace('.', '/', $n)) != Env
::get('n') && $n != Env
::get('n'))
36 redirect($globals->relurl
.'/'.$urln);
41 function wiki_work_dir() {
43 return realpath($globals->spoolroot
.'htdocs/'.$globals->wiki
->workdir
);
46 function wiki_template($n) {
48 return $tpl = wiki_work_dir().'/cache_'.$wikisite.'_'.$n.'.tpl';
51 // several files are used for wiki :
52 // - spool/wiki.d/PageName : the wiki page
53 // - spool/wiki.d/cache_PageName.tpl : the template cache
54 // - spool/templates_c/%%...%%cache_PageName.tpl.php : the PHP from Smarty
55 function wiki_clear_cache($n) {
56 global $page, $wikisite, $wikisites;
57 $oldwikisite = $wikisite;
58 foreach ($wikisites as $s) {
60 $tpl = wiki_template($n);
62 $page->clear_compiled_tpl($tpl);
64 $wikisite = $oldwikisite;
67 function wiki_clear_all_cache()
69 system("rm -f ".wiki_work_dir()."/cache_*");
72 // editing pages are not static but used templates too, so we used
73 // temp template files containing result from wiki
74 function wiki_create_tmp($content) {
75 $tmpfile = tempnam(wiki_work_dir(), "temp_");
76 $f = fopen($tmpfile, 'w');
82 function wiki_clean_tmp() {
83 // clean old tmp files (more than one hour)
84 $wiki_work_dir = wiki_work_dir();
85 $dh = opendir(wiki_work_dir());
87 while (($file = readdir($dh)) !== false
) {
88 if (strpos($file, 'temp_') === 0) {
89 $created = filectime($wiki_work_dir.'/'.$file);
90 if ($time-$created > 60 * 60)
91 @unlink
($wiki_work_dir.'/'.$file);
96 function wiki_assign_auth() {
97 global $page, $wiki_auths;
98 $page->assign('logged', logged());
99 $page->assign('identified', identified());
100 $page->assign('has_perms', has_perms());
101 $page->assign('public', true
);
102 $page->assign('wiki_admin', has_perms() && identified());
105 // cannot be in a function because pmwiki use all vars as if it was globals
106 //function new_wiki_page() {
107 // the wiki keword is given in the n var
108 if ( $n = wiki_pagename() )
111 $wiki_template = wiki_template($n);
112 $tmpfile_exists = file_exists($wiki_template);
114 // don't recreate the tpl if it already exists
115 if (Env
::get('action') ||
!$tmpfile_exists)
117 if ($tmpfile_exists) {
118 wiki_clear_cache($n);
121 // we leave pmwiki do whatever it wants and store everything
123 require_once($globals->spoolroot
.'/'.$globals->wiki
->wikidir
.'/pmwiki.php');
125 $wikiAll = ob_get_clean();
126 // the pmwiki skin we are using (almost empty) has these keywords:
127 $i = strpos($wikiAll, "<!--/HeaderText-->");
128 $j = strpos($wikiAll, "<!--/PageLeftFmt-->", $i);
131 if (Env
::get('action'))
133 // clean old tmp files
135 $page->assign('xorg_extra_header', substr($wikiAll, 0, $i));
136 // create new tmp files with editing page from wiki engine
137 $wiki_template = wiki_create_tmp(substr($wikiAll, $j));
139 if (!$tmpfile_exists)
141 $f = fopen($wiki_template, 'w');
142 fputs($f, substr($wikiAll, $j));
147 //return $wiki_template;