pending commit, finished during MQ/S download ...
[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 if (!$n) {
26 return null;
27 }
28 $keywords = explode('.', $n);
29 $count = count($keywords);
30 if ($count == 1)
31 $n = $keywords[0].".".$keywords[0];
32 else
33 $n = $keywords[$count - 2].".".$keywords[$count - 1];
34 global $globals;
35 if (($urln = str_replace('.', '/', $n)) != Env::get('n') && $n != Env::get('n'))
36 redirect($globals->relurl.'/'.$urln);
37 $_REQUEST['n'] = $n;
38 return $n;
39 }
40
41 function wiki_work_dir() {
42 global $globals;
43 return realpath($globals->spoolroot.'htdocs/'.$globals->wiki->workdir);
44 }
45
46 function wiki_template($n) {
47 global $wikisite;
48 return $tpl = wiki_work_dir().'/cache_'.$wikisite.'_'.$n.'.tpl';
49 }
50
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) {
59 $wikisite = $s;
60 $tpl = wiki_template($n);
61 @unlink($tpl);
62 $page->clear_compiled_tpl($tpl);
63 }
64 $wikisite = $oldwikisite;
65 }
66
67 function wiki_clear_all_cache()
68 {
69 system("rm -f ".wiki_work_dir()."/cache_*");
70 }
71
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');
77 fputs($f, $content);
78 fclose($f);
79 return $tmpfile;
80 }
81
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());
86 $time = time();
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);
92 }
93 }
94 }
95
96 function wiki_assign_auth() {
97 global $page, $wiki_auths;
98 $page->assign('logged', S::logged());
99 $page->assign('identified', S::identified());
100 $page->assign('has_perms', S::has_perms());
101 $page->assign('public', true);
102 $page->assign('wiki_admin', S::has_perms() && S::identified());
103 }
104
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() )
109 {
110
111 $wiki_template = wiki_template($n);
112 $tmpfile_exists = file_exists($wiki_template);
113
114 // don't recreate the tpl if it already exists
115 if (Env::get('action') || !$tmpfile_exists)
116 {
117 if ($tmpfile_exists) {
118 wiki_clear_cache($n);
119 }
120
121 // we leave pmwiki do whatever it wants and store everything
122 ob_start();
123 require_once($globals->spoolroot.'/'.$globals->wiki->wikidir.'/pmwiki.php');
124
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);
129
130 }
131 if (Env::get('action'))
132 {
133 // clean old tmp files
134 wiki_clean_tmp();
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));
138 } else {
139 if (!$tmpfile_exists)
140 {
141 $f = fopen($wiki_template, 'w');
142 fputs($f, substr($wikiAll, $j));
143 fclose($f);
144 }
145 }
146 }
147 //return $wiki_template;
148 //}
149 ?>