refactor wiki.php *again*.
authorx2000habouzit <x2000habouzit@839d8a87-29fc-0310-9880-83ba4fa771e5>
Sat, 22 Jul 2006 16:09:57 +0000 (16:09 +0000)
committerx2000habouzit <x2000habouzit@839d8a87-29fc-0310-9880-83ba4fa771e5>
Sat, 22 Jul 2006 16:09:57 +0000 (16:09 +0000)
implement our simple permissions level checks.

now will have to implement things to modify them

git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@563 839d8a87-29fc-0310-9880-83ba4fa771e5

htdocs/wiki.php
include/wiki.inc.php
include/xorg/session.inc.php

index c702d7a..1d0963e 100644 (file)
  ***************************************************************************/
 
 require_once 'xorg.inc.php';
+require_once 'wiki.inc.php';
 
-new_skinned_page('wiki.tpl');
-if (!S::identified()) {
-    XorgSession::doAuth();
+require_once dirname(__FILE__).'/../classes/Platal.php';
+require_once dirname(__FILE__).'/../classes/PLModule.php';
+
+$n = wiki_pagename();
+if (!$n) {
+    pl_redirect('');
 }
 
-require_once 'wiki.inc.php';
+new_skinned_page('wiki.tpl');
+
+switch (Env::get('action')) {
+  case '':
+    list($r) = get_perms($n);
+    wiki_apply_perms($r);
+    break;
 
-if ($n = wiki_pagename()) {
-    $wiki_cache   = wiki_work_dir().'/cache_'.$n.'.tpl';
-    $cache_exists = file_exists($wiki_cache);
+  case 'edit':
+    list(, $e) = get_perms($n);
+    wiki_apply_perms($e);
+    break;
 
-    if (Env::get('action') || !$cache_exists) {
-        @unlink($wiki_cache);
+  default:
+    wiki_apply_perms('admin');
+    break;
+}
 
-        // we leave pmwiki do whatever it wants and store everything
-        ob_start();
-        require_once($globals->spoolroot.'/wiki/pmwiki.php');
+$wiki_cache   = wiki_work_dir().'/cache_'.$n.'.tpl';
+$cache_exists = file_exists($wiki_cache);
 
-        $wikiAll = ob_get_clean();
-        // the pmwiki skin we are using (almost empty) has these keywords:
-        $i = strpos($wikiAll, "<!--/HeaderText-->");
-        $j = strpos($wikiAll, "<!--/PageLeftFmt-->", $i);
-    }
+if (Env::get('action') || !$cache_exists) {
+    @unlink($wiki_cache);
+
+    // we leave pmwiki do whatever it wants and store everything
+    ob_start();
+    require_once($globals->spoolroot.'/wiki/pmwiki.php');
 
-    if (Env::get('action')) {
-        $page->assign('xorg_extra_header', substr($wikiAll, 0, $i));
+    $wikiAll = ob_get_clean();
+    // the pmwiki skin we are using (almost empty) has these keywords:
+    $i = strpos($wikiAll, "<!--/HeaderText-->");
+    $j = strpos($wikiAll, "<!--/PageLeftFmt-->", $i);
+}
+
+if (Env::get('action')) {
+    $page->assign('xorg_extra_header', substr($wikiAll, 0, $i));
+    $wikiAll = substr($wikiAll, $j);
+} else {
+    if (!$cache_exists) {
+        $f = fopen($wiki_cache, 'w');
         $wikiAll = substr($wikiAll, $j);
+        fputs($f, $wikiAll);
+        fclose($f);
     } else {
-        if (!$cache_exists) {
-            $f = fopen($wiki_cache, 'w');
-            $wikiAll = substr($wikiAll, $j);
-            fputs($f, $wikiAll);
-            fclose($f);
-        } else {
-            $wikiAll = file_get_contents($wiki_cache);
-        }
+        $wikiAll = file_get_contents($wiki_cache);
     }
-
-    $page->assign('wikipage', str_replace('.', '/', $n));
 }
 
+$page->assign('wikipage', str_replace('.', '/', $n));
 $page->assign('pmwiki', $wikiAll);
 $page->assign('has_perms',  S::has_perms());
 $page->addCssLink('css/wiki.css');
index 6f9c83c..000690e 100644 (file)
@@ -46,4 +46,43 @@ function wiki_clear_all_cache()
     system('rm -f '.wiki_work_dir().'/cache_*');
 }
 
+function get_perms($n)
+{
+    $file  = wiki_work_dir().'/'.str_replace('/', '.', $n);
+    $lines = explode("\n", file_get_contents($file));
+    foreach ($lines as $line) {
+        list($k, $v) = explode('=', $line, 2);
+        if ($k == 'platal_perms') {
+            return explode(':', $v);
+        }
+    }
+    return array('logged', 'admin');
+}
+
+function wiki_apply_perms($perm) {
+    global $page, $platal;
+
+    switch ($perm) {
+      case 'public':
+        return;
+
+      case 'logged':
+        if (!XorgSession::doAuthCookie()) {
+            $platal = new Platal();
+            $platal->force_login($page);
+        }
+        return;
+
+      default:
+        if (!XorgSession::doAuth()) {
+            $platal = new Platal();
+            $platal->force_login($page);
+        }
+        if ($perm == 'admin') {
+            check_perms();
+        }
+        return;
+    }
+}
+
 ?>
index df30806..841bbd1 100644 (file)
@@ -146,10 +146,10 @@ class XorgSession
      *
      * @param page the calling page (by reference)
      */
-    function doAuthCookie(&$page)
+    function doAuthCookie()
     {
        if (S::logged()) {
-           return;
+           return true;
         }
 
        if (Env::has('username') and Env::has('response')) {
@@ -159,6 +159,8 @@ class XorgSession
        if ($r = try_cookie()) {
            return XorgSession::doAuth(($r > 0));
         }
+
+        return false;
     }
 
     // }}}