2006 => 2007 Happy New Year\!
[platal.git] / include / wiki.inc.php
index 94d8b0b..838693e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  *  Foundation, Inc.,                                                      *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
-$wikisites = array('xorg','xnet');
 
 function wiki_pagename() {
-    $n = str_replace('/', '.', Env::get('n', false));
-    if (!$n) {
+    if (!Get::v('n')) {
         return null;
     }
-    $keywords = explode('.', $n);
-    $count = count($keywords);
-    if ($count == 1)
-        $n = $keywords[0].".".$keywords[0];
-    else
-        $n = $keywords[$count - 2].".".$keywords[$count - 1];
-    global $globals;
-    if (($urln = str_replace('.', '/', $n)) != Env::get('n') && $n != Env::get('n'))
-        redirect($globals->baseurl.'/'.$urln);
-    $_REQUEST['n'] = $n;
-    return $n;
+
+    $words = explode('/', trim(Get::v('n'), '/'));
+    if (count($words) == 2) {
+        return join('.', $words);
+    }
+
+    array_unshift($words, $words[0]);
+    $b = array_pop($words);
+    $a = array_pop($words);
+
+    pl_redirect($a.'/'.$b);
 }
 
 function wiki_work_dir() {
     global $globals;
-    return realpath($globals->spoolroot.'htdocs/'.$globals->wiki->workdir);
+    return $globals->spoolroot.'/spool/wiki.d';
+}
+
+function wiki_clear_all_cache()
+{
+    system('rm -f '.wiki_work_dir().'/cache_*');
 }
 
-function wiki_template($n) {
-    global $wikisite;
-    return $tpl = wiki_work_dir().'/cache_'.$wikisite.'_'.$n.'.tpl';
+function wiki_perms_options() {
+    return array('public' => 'Public', 'logged' => 'Connecté',
+                  'mdp' => 'Authentifié', 'admin' => 'Admin');
 }
 
-// several files are used for wiki :
-// - spool/wiki.d/PageName                           : the wiki page
-// - spool/wiki.d/cache_PageName.tpl                 : the template cache
-// - spool/templates_c/%%...%%cache_PageName.tpl.php : the PHP from Smarty
-function wiki_clear_cache($n) {
-    global $page, $wikisite, $wikisites;
-    $oldwikisite = $wikisite;
-    foreach ($wikisites as $s) {
-        $wikisite = $s;
-        $tpl = wiki_template($n);
-        @unlink($tpl);
-        $page->clear_compiled_tpl($tpl);
+function wiki_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);
+        }
     }
-    $wikisite = $oldwikisite;
+    return array('logged', 'admin');
 }
 
-function wiki_clear_all_cache()
+function wiki_putfile($f, $s)
 {
-    system("rm -f ".wiki_work_dir()."/cache_*");
+    $fp = fopen($f, 'w');
+    fputs($fp, $s);
+    fclose($fp);
 }
 
-// editing pages are not static but used templates too, so we used
-// temp template files containing result from wiki
-function wiki_create_tmp($content) {
-    $tmpfile = tempnam(wiki_work_dir(), "temp_");
-    $f = fopen($tmpfile, 'w');
-    fputs($f, $content);
-    fclose($f);
-    return $tmpfile;
-}
+function wiki_set_perms($n, $pr, $pw)
+{
+    $file  = wiki_work_dir().'/'.str_replace('/', '.', $n);
+    if (!file_exists($file))
+        return false;
 
-function wiki_clean_tmp() {
-    // clean old tmp files (more than one hour)
-    $wiki_work_dir = wiki_work_dir();
-    $dh = opendir(wiki_work_dir());
-    $time = time();
-    while (($file = readdir($dh)) !== false) {
-        if (strpos($file, 'temp_') === 0) {
-            $created = filectime($wiki_work_dir.'/'.$file);
-            if ($time-$created > 60 * 60)
-                @unlink($wiki_work_dir.'/'.$file);
+    $p = $pr . ':' . $pw;
+
+    $lines = explode("\n", file_get_contents($file));
+    foreach ($lines as $i => $line) {
+        list($k, $v) = explode('=', $line, 2);
+        if ($k == 'platal_perms') {
+            $lines[$i] = 'platal_perms='.$p;
+            wiki_putfile($file, join("\n", $lines));
+            return true;
         }
     }
-}
 
-function wiki_assign_auth() {
-    global $page, $wiki_auths;
-    $page->assign('logged', S::logged());
-    $page->assign('identified', S::identified());
-    $page->assign('has_perms', S::has_perms());
-    $page->assign('public', true);
-    $page->assign('wiki_admin', S::has_perms() && S::identified());
+    array_splice($lines, 1, 0, array('platal_perms='.$p));
+    wiki_putfile($file, join("\n", $lines));
+    return true;
 }
 
-// cannot be in a function because pmwiki use all vars as if it was globals
-//function new_wiki_page() {
-    // the wiki keword is given in the n var
-    if ( $n = wiki_pagename() )
-    {
-
-        $wiki_template = wiki_template($n);
-        $tmpfile_exists = file_exists($wiki_template);
+function wiki_may_have_perms($perm) {
+    switch ($perm) {
+      case 'public': return true;
+      case 'logged': return S::logged();
+      case 'mdp':    return S::logged();
+      default:       return S::has_perms();
+    }
+}
 
-        // don't recreate the tpl if it already exists
-        if (Env::get('action') || !$tmpfile_exists)
-        {
-            if ($tmpfile_exists) {
-                wiki_clear_cache($n);
-            }
+function wiki_apply_perms($perm) {
+    global $page, $platal, $globals;
 
-            // we leave pmwiki do whatever it wants and store everything
-            ob_start();
-            require_once($globals->spoolroot.'/'.$globals->wiki->wikidir.'/pmwiki.php');
+    switch ($perm) {
+      case 'public':
+        return;
 
-            $wikiAll = ob_get_clean();
-            // the pmwiki skin we are using (almost empty) has these keywords:
-            $i = strpos($wikiAll, "<!--/HeaderText-->");
-            $j = strpos($wikiAll, "<!--/PageLeftFmt-->", $i);
+      case 'logged':
+        if (!call_user_func(array($globals->session, 'doAuthCookie'))) {
+            $platal = new Platal();
+            $platal->force_login($page);
+        }
+        return;
 
+      default:
+        if (!call_user_func(array($globals->session, 'doAuth'))) {
+            $platal = empty($GLOBALS['IS_XNET_SITE']) ? new Platal() : new Xnet();
+            $platal->force_login($page);
+        }
+        if ($perm == 'admin') {
+            check_perms();
         }
-        if (Env::get('action'))
-        {
-            // clean old tmp files
-            wiki_clean_tmp();
-            $page->assign('xorg_extra_header', substr($wikiAll, 0, $i));
-            // create new tmp files with editing page from wiki engine
-            $wiki_template = wiki_create_tmp(substr($wikiAll, $j));
-        } else {
-            if (!$tmpfile_exists)
-            {
-                $f = fopen($wiki_template, 'w');
-                fputs($f, substr($wikiAll, $j));
-                fclose($f);
-            }
-        } 
+        return;
     }
-   //return $wiki_template;    
-//}
+}
+
+function wiki_require_page($pagename)
+{
+    global $globals;
+    $pagename_slashes = str_replace('.','/',$pagename);
+    $pagename_dots = str_replace('/','.',$pagename);
+    if (is_file(wiki_work_dir().'/cache_'.$pagename_dots.'.tpl')) return;
+    system('wget '.$globals->baseurl.'/'.$pagename_slashes.' -O /dev/null');
+}
+
 ?>