Allow parenthesis before hasPerm in smarty templates
[platal.git] / classes / plwikipage.php
index 20bcf97..19cb736 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -36,6 +36,7 @@ class PlWikiPage
     public $name;
     public $path;
 
+    private $content = null;
     private $perms = null;
 
     /** Build a new page from a PmWiki page name (ie NameSpace.Page).
@@ -63,27 +64,73 @@ class PlWikiPage
         return self::workDir() . '/cache_' . $this->name . '.tpl';
     }
 
-    /** Fetch the permissions.
+    /** Remove the cache for the current page.
      */
-    private function fetchPerms()
+    public function removePageCache()
     {
-        if (!is_null($this->perms)) {
+        @unlink($this->cacheFilename());
+        global $globals;
+        $page = glob($globals->spoolroot . '/spool/templates_c/*cache_' . $this->name . '.tpl.php');
+        if (count($page) > 0) {
+            @unlink($page[0]);
+        }
+    }
+
+    /** Fetch the content of the wiki page.
+     */
+    private function fetchContent()
+    {
+        if (!is_null($this->content)) {
             return;
         }
         $file = $this->filename();
-        if (!file_exists($file)) {
-            $this->perms = self::$defaulPerms;
+        if (!is_file($file)) {
             return;
         }
         $lines = explode("\n", file_get_contents($file));
+        $this->content = array();
         foreach ($lines as $line) {
             @list($k, $v) = explode('=', $line, 2);
-            if ($k == 'platal_perms') {
-                $this->perms = explode(':', $v);
-                return;
-            }
+            $this->content[$k] = $v;
+        }
+    }
+
+    /** Save the content of the wiki page based on the
+     * fetched content.
+     */
+    private function saveContent()
+    {
+        if (is_null($this->content)) {
+            return false;
+        }
+        $lines = array();
+        foreach ($this->content as $k => $v) {
+            $lines[] = "$k=$v";
+        }
+        return file_put_contents($this->filename(), implode("\n", $lines)) !== false;
+    }
+
+    /** Get a field from the wiki content.
+     */
+    public function getField($name)
+    {
+        $this->fetchContent();
+        return @$this->content[$name];
+    }
+
+    /** Fetch the permissions.
+     */
+    private function fetchPerms()
+    {
+        if (!is_null($this->perms)) {
+            return;
+        }
+        $this->fetchContent();
+        if (isset($this->content['platal_perms'])) {
+            $this->perms = explode(':', $this->content['platal_perms']);
+        } else {
+            $this->perms = self::$defaulPerms;
         }
-        $this->perms = self::$defaulPerms;
     }
 
     /** Return read perms.
@@ -124,24 +171,13 @@ class PlWikiPage
      */
     public function setPerms($read, $write)
     {
-        $file = $this->filename();
-        if (!file_exists($file)) {
+        $this->fetchContent();
+        if (is_null($this->content)) {
             return false;
         }
-
-        $p = $read . ':' . $write;
-        $lines = explode("\n", file_get_contents($file));
-        foreach ($lines as $i => $line) {
-            list($k, $v) = explode('=', $line, 2);
-            if ($k == 'platal_perms') {
-                unset($lines[$i]);
-                break;
-            }
-        }
-        array_splice($lines, 1, 0, array('platal_perms=' . $p));
-        file_put_contents($file, join("\n", $lines));
+        $this->content['platal_perms'] = $read . ':' . $write;
         $this->perms = array($read, $write);
-        return true;
+        return $this->saveContent();
     }
 
 
@@ -254,6 +290,10 @@ class PlWikiPage
     public function rename($newname, $changeLinks = true)
     {
         $newpage = new PlWikiPage(str_replace('/', '.', $newname));
+        $cache = $this->cacheFilename();
+        if (is_file($cache)) {
+            unlink($cache);
+        }
 
         list($groupname, ) = explode('.', $this->name);
         list($newgroupname, ) = explode('.', $newpage->name);
@@ -364,10 +404,11 @@ class PlWikiPage
           case 'public':
             return true;
           case 'logged':
-          case 'mdp':
             return S::logged();
+          case 'mdp':
+            return S::identified();
           case 'admin':
-            return S::has_perms();
+            return S::admin();
           default:
             return false;
         }
@@ -381,11 +422,19 @@ class PlWikiPage
           case 'public':
             return;
           case 'logged':
-            Platal::session()->start(AUTH_PUBLIC + 1);
-            return;
+            $ok = Platal::session()->start(AUTH_PUBLIC + 1);
+            break;
           default:
-            Platal::session()->start(Platal::session()->sureLevel());
-            return;
+            $ok = Platal::session()->start(Platal::session()->sureLevel());
+            break;
+        }
+        if (!$ok) {
+            global $platal;
+            $page =& Platal::page();
+            $platal->force_login($page);
+        } else if ($perm == 'admin' && !S::admin()) {
+            global $platal;
+            $platal->error403();
         }
     }
 
@@ -409,7 +458,29 @@ class PlWikiPage
 
         pl_redirect($a . '/' . $b);
     }
+
+    /** List wiki pages.
+     */
+    public static function listPages()
+    {
+        $wiki_pages = array();
+        $dir = PlWikiPage::workDir();
+        if (is_dir($dir)) {
+            if ($dh = opendir($dir)) {
+                while (($file = readdir($dh)) !== false) {
+                    if ($file{0} >= 'A' && $file{0} <= 'Z') {
+                        $wp = new PlWikiPage($file);
+                        $wiki_pages[$file] = array('read' => $wp->readPerms(),
+                                                   'edit' => $wp->writePerms(),
+                                                   'cached' => is_file($wp->cacheFilename()));
+                    }
+                }
+                closedir($dh);
+            }
+        }
+        return $wiki_pages;
+    }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>