tons of fixes and/or simplifications.
[platal.git] / include / wiki.inc.php
CommitLineData
0df3edb9 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 ***************************************************************************/
0df3edb9 21
22function wiki_pagename() {
06a5e65b 23 if (!Env::get('n')) {
ed912c6e 24 return null;
25 }
06a5e65b 26
27 $words = explode('/', Env::get('n'));
28 if (count($words) == 2) {
29 return join('.', $words);
30 }
31
32 array_unshift($words, $words[0]);
33 $b = array_pop($words);
34 $a = array_pop($words);
35
0df3edb9 36 global $globals;
06a5e65b 37 redirect($globals->baseurl.'/'.$a.'/'.$b);
0df3edb9 38}
39
40function wiki_work_dir() {
41 global $globals;
06a5e65b 42 return $globals->spoolroot.'/spool/wiki.d';
0df3edb9 43}
44
ed912c6e 45function wiki_clear_all_cache()
46{
47 system("rm -f ".wiki_work_dir()."/cache_*");
48}
49
0df3edb9 50// editing pages are not static but used templates too, so we used
51// temp template files containing result from wiki
52function wiki_create_tmp($content) {
53 $tmpfile = tempnam(wiki_work_dir(), "temp_");
54 $f = fopen($tmpfile, 'w');
55 fputs($f, $content);
56 fclose($f);
57 return $tmpfile;
58}
59
60function wiki_clean_tmp() {
61 // clean old tmp files (more than one hour)
62 $wiki_work_dir = wiki_work_dir();
63 $dh = opendir(wiki_work_dir());
64 $time = time();
65 while (($file = readdir($dh)) !== false) {
66 if (strpos($file, 'temp_') === 0) {
67 $created = filectime($wiki_work_dir.'/'.$file);
68 if ($time-$created > 60 * 60)
69 @unlink($wiki_work_dir.'/'.$file);
70 }
71 }
72}
73
74function wiki_assign_auth() {
75 global $page, $wiki_auths;
06a5e65b 76 $page->assign('true', true);
77 $page->assign('public', true);
78 $page->assign('logged', S::logged());
cab08090 79 $page->assign('identified', S::identified());
06a5e65b 80 $page->assign('has_perms', S::has_perms());
0df3edb9 81}
82
83// cannot be in a function because pmwiki use all vars as if it was globals
06a5e65b 84if ($n = wiki_pagename()) {
85 $wiki_template = wiki_work_dir().'/cache_'.$n.'.tpl';
86 $tmpfile_exists = file_exists($wiki_template);
0df3edb9 87
06a5e65b 88 if (Env::get('action') || !$tmpfile_exists) {
89 if ($tmpfile_exists) {
90 @unlink($wiki_template);
91 $page->clear_compiled_tpl($wiki_template);
92 }
0df3edb9 93
06a5e65b 94 // we leave pmwiki do whatever it wants and store everything
95 ob_start();
96 require_once($globals->spoolroot.'/wiki/pmwiki.php');
0df3edb9 97
06a5e65b 98 $wikiAll = ob_get_clean();
99 // the pmwiki skin we are using (almost empty) has these keywords:
100 $i = strpos($wikiAll, "<!--/HeaderText-->");
101 $j = strpos($wikiAll, "<!--/PageLeftFmt-->", $i);
102 }
0df3edb9 103
06a5e65b 104 if (Env::get('action')) {
105 // clean old tmp files
106 wiki_clean_tmp();
107 $page->assign('xorg_extra_header', substr($wikiAll, 0, $i));
ed912c6e 108
06a5e65b 109 // create new tmp files with editing page from wiki engine
110 $wiki_template = wiki_create_tmp(substr($wikiAll, $j));
111 } else {
112 if (!$tmpfile_exists) {
113 $f = fopen($wiki_template, 'w');
114 fputs($f, substr($wikiAll, $j));
115 fclose($f);
ed912c6e 116 }
ed912c6e 117 }
06a5e65b 118}
0df3edb9 119?>