2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 ***************************************************************************/
22 /** This namespace groups all the methods relative to the integration
23 * of PmWiki into plat/al.
25 * pagename: A page name is NameSpace.Page and must be acceed trough
30 static private $permLevels = array('public' => 'Public',
31 'logged' => 'Connecté',
32 'mdp' => 'Authentifié',
34 static private $defaulPerms = array('logged', 'admin');
39 private $perms = null
;
41 /** Build a new page from a PmWiki page name (ie NameSpace.Page).
43 public function __construct($n)
49 $this->path
= str_replace('.', '/', $n);
52 /** Return the filename.
54 public function filename()
56 return self
::workDir() . '/' . $this->name
;
59 /** Return the filename for the cache file.
61 public function cacheFilename()
63 return self
::workDir() . '/cache_' . $this->name
. '.tpl';
66 /** Fetch the permissions.
68 private function fetchPerms()
70 if (!is_null($this->perms
)) {
73 $file = $this->filename();
74 if (!file_exists($file)) {
75 $this->perms
= self
::$defaulPerms;
78 $lines = explode("\n", file_get_contents($file));
79 foreach ($lines as $line) {
80 @list
($k, $v) = explode('=', $line, 2);
81 if ($k == 'platal_perms') {
82 $this->perms
= explode(':', $v);
86 $this->perms
= self
::$defaulPerms;
89 /** Return read perms.
91 public function readPerms()
93 if (is_null($this->perms
)) {
96 return $this->perms
[0];
99 /** Check if the user can read the page.
101 public function canRead()
103 return self
::havePerms($this->readPerms());
106 /** Return write perms.
108 public function writePerms()
110 if (is_null($this->perms
)) {
113 return $this->perms
[1];
116 /** Check if the user can write the page.
118 public function canWrite()
120 return self
::havePerms($this->writePerms());
123 /** Set the permission level for the page.
125 public function setPerms($read, $write)
127 $file = $this->filename();
128 if (!file_exists($file)) {
132 $p = $read . ':' . $write;
133 $lines = explode("\n", file_get_contents($file));
134 foreach ($lines as $i => $line) {
135 list($k, $v) = explode('=', $line, 2);
136 if ($k == 'platal_perms') {
141 array_splice($lines, 1, 0, array('platal_perms=' . $p));
142 file_put_contents($file, join("\n", $lines));
143 $this->perms
= array($read, $write);
148 /** Check permission for RSS feed.
150 public function prepareFeed()
152 if ($this->canRead()) {
155 $uid = Platal
::session()->tokenAuth(Get
::v('user'), Get
::v('hash'));
156 if ($this->canRead()) {
162 /** Apply the read permissions for the current page.
164 public function applyReadPerms()
166 if ($this->canRead()) {
169 $this->applyPerms($this->readPerms());
172 /** Apply the write permissions for the current page.
174 public function applyWritePerms()
176 if ($this->canWrite()) {
179 $this->applyPerms($this->writePerms());
182 /** Build the cache for the page.
184 public function buildCache()
187 if (is_file($this->cacheFilename())) {
190 system('wget --no-check-certificate ' . escapeshellarg($globals->baseurl
. '/' . $this->path
) . ' -O /dev/null');
195 public function delete()
197 $file = $this->filename();
198 $cache = $this->cacheFilename();
199 if (is_file($cache)) {
202 if (!is_file($file)) {
209 /** Return the wiki storage dir.
211 public static function workDir()
214 return $globals->spoolroot
. '/spool/wiki.d';
217 /** Clear wiki cache.
219 public static function clearCache()
221 system('rm -f ' . self
::workDir() . '/cache_*');
225 /** Search links in the a page
227 private static function findLinks($line, $groupname)
230 if (preg_match_all('@\[\[([^~][^\]\|\?#]*)((\?|#)[^\]\|]+)?(\\|[^\]]+)?\]\]@', $line, $matches, PREG_OFFSET_CAPTURE
)) {
231 foreach ($matches[1] as $j => $link) if (!preg_match('@http://@', $link[0])) {
232 $mylink = str_replace('/','.',trim($link[0]));
233 $sup = trim(substr($matches[2][$j][0],1));
234 $alt = trim(substr($matches[4][$j][0],1));
235 $newlink = str_replace(' ','',ucwords($mylink));
236 if (strpos($newlink,'.') === false
) {
237 $newlink = $groupname.'.'.$newlink;
239 if (!$alt && $mylink != $newlink) {
240 $alt = trim($link[0]);
243 'pos' => $matches[0][$j][1],
244 'size' => strlen($matches[0][$j][0]),
248 'group' => substr($mylink, 0, strpos($mylink, '.')));
254 public function rename($newname, $changeLinks = true
)
256 $newpage = new PlWikiPage(str_replace('/', '.', $newname));
258 list($groupname, ) = explode('.', $this->name
);
259 list($newgroupname, ) = explode('.', $newpage->name
);
261 $file = $this->filename();
262 $newfile = $newpage->filename();
263 if (!is_file($file)) {
264 // old page doesn't exist
267 if (!rename($file, $newfile)) {
268 // impossible to renama page
273 $this->name
= $newpage->name
;
274 $this->path
= $newpage->path
;
279 // change name inside this folder and ingroup links if changing group
280 $lines = explode("\n", file_get_contents($newfile));
282 foreach ($lines as $i => $line) {
283 list($k, $v) = explode('=', $line, 2);
284 if ($k == 'name' && $v == $pagename_dots) {
285 $lines[$i] = 'name=' . $newpage->name
;
287 } else if ($groupname != $newgroupname) {
288 $links = self
::findLinks($line, $groupname);
291 foreach ($links as $link) {
292 if ($link['group'] == $groupname) {
293 $newline .= substr($line, $last, $link['pos']);
294 $newline .= '[['.$link['href'].$link['sup'].($link['alt']?
(' |'.$link['alt']):'').']]';
295 $last = $link['pos']+
$link['size'];
300 $newline .= substr($line, $last);
301 $lines[$i] = $newline;
306 file_put_contents($newfile, join("\n", $lines));
308 // change wiki links in all wiki pages
309 $endname = substr($pagename_dots, strpos($this->name
, '.') +
1);
311 exec('grep ' . $endname . ' ' . self
::workDir() . '/* -sc', $pages);
312 foreach($pages as $line) {
313 if (preg_match('%/([^/:]+):([0-9]+)$%', $line, $vals) && $vals[2] > 0) {
315 $lines = explode("\n", file_get_contents(self
::workDir().'/'.$inpage));
317 // find all wiki links in page and change if linked to this page
318 foreach ($lines as $i => $line) {
319 $links = self
::findLinks($line, substr($inpage, 0, strpos($inpage, '.')));
322 foreach ($links as $link) {
323 if ($link['href'] == $pagename_dots) {
324 $newline .= substr($line, $last, $link['pos']);
325 $newline .= '[[' . $newname_dots . $link['sup'] . ($link['alt'] ?
(' |' . $link['alt']) : '') . ']]';
326 $last = $link['pos'] +
$link['size'];
331 $newline .= substr($line, $last);
332 $lines[$i] = $newline;
337 file_put_contents(self
::workDir() . '/' . $inpage, join("\n", $lines));
341 if ($changedLinks > 0) {
342 return $changedLinks;
344 $this->name
= $newpage->name
;
345 $this->path
= $newpage->path
;
349 /** Return the authentication level translation table.
351 public static function permOptions()
353 return array('public' => 'Public',
354 'logged' => 'Connecté',
355 'mdp' => 'Authentifié',
359 /** Check permissions.
361 public static function havePerms($perm)
370 return S
::has_perms();
376 /** Apply permissions.
378 public static function applyPerms($perm)
384 Platal
::session()->start(AUTH_PUBLIC +
1);
387 Platal
::session()->start(Platal
::session()->sureLevel());
392 /** Return the current page.
394 public static function currentPage()
400 $words = explode('/', trim(Get
::v('n'), '/'));
401 if (count($words) == 2) {
402 return new PlWikiPage(join('.', $words));
405 // We are on NameSpace.Page, redirect to NameSpace/Page
406 array_unshift($words, $words[0]);
407 $b = array_pop($words);
408 $a = array_pop($words);
410 pl_redirect($a . '/' . $b);
414 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: