Merge remote branch 'origin/core/1.1.1/maint' into core/master
[platal.git] / modules / core.php
index 618c0e5..6557ca4 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -24,70 +24,186 @@ class CoreModule extends PLModule
     function handlers()
     {
         return array(
-            '403'  => $this->make_hook('403', AUTH_PUBLIC),
-            '404'  => $this->make_hook('404', AUTH_PUBLIC),
-            'exit' => $this->make_hook('exit', AUTH_PUBLIC),
+            '403'           => $this->make_hook('403',           AUTH_PUBLIC),
+            '404'           => $this->make_hook('404',           AUTH_PUBLIC),
+            'login'         => $this->make_hook('login',         AUTH_COOKIE),
+            'send_bug'      => $this->make_hook('bug',           AUTH_COOKIE),
+            'purge_cache'   => $this->make_hook('purge_cache',   AUTH_COOKIE, 'admin'),
+            'kill_sessions' => $this->make_hook('kill_sessions', AUTH_COOKIE, 'admin'),
+            'sql_errors'    => $this->make_hook('siteerror',     AUTH_COOKIE, 'admin'),
+            'assert_errors' => $this->make_hook('siteerror',     AUTH_COOKIE, 'admin'),
+            'site_errors'   => $this->make_hook('siteerror',     AUTH_COOKIE, 'admin'),
+
+            'embedded'      => $this->make_hook('embedded',      AUTH_PUBLIC),
+
+            'wiki_help'     => $this->make_hook('wiki_help',     AUTH_PUBLIC),
+            'wiki_preview'  => $this->make_hook('wiki_preview',  AUTH_COOKIE, 'user', NO_AUTH),
+
+            'valid.html'    => $this->make_hook('valid',         AUTH_PUBLIC),
+            'favicon.ico'   => $this->make_hook('favicon',       AUTH_PUBLIC),
+            'robots.txt'    => $this->make_hook('robotstxt',     AUTH_PUBLIC, 'user', NO_HTTPS),
         );
     }
 
-    function handler_exit(&$page, $level = null)
+    function handler_valid($page)
     {
-        if (Session::has('suid')) {
-            if (Session::has('suid')) {
-                $a4l  = Session::get('forlife');
-                $suid = Session::getMixed('suid');
-                $log  = Session::getMixed('log');
-                $log->log("suid_stop", Session::get('forlife') . " by " . $suid['forlife']);
-                $_SESSION = $suid;
-                Session::kill('suid');
-                redirect($globals->baseurl.'/admin/utilisateurs.php?login='.$a4l);
-            } else {
-                redirect("login.php");
-            }
-        }
+        readfile($page->compile_dir.'/valid.html');
+        exit;
+    }
 
-        if ($level == 'forget' || $level == 'forgetall') {
-            setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
-            Cookie::kill('ORGaccess');
-            if (isset($_SESSION['log']))
-                $_SESSION['log']->log("cookie_off");
-        }
+    function handler_403($page)
+    {
+        global $globals;
+        header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
+        $page->trigError('Tu n\'as pas les permissions nécessaires pour accéder à cette page.');
+        $page->coreTpl('403.tpl');
+    }
 
-        if ($level == 'forgetuid' || $level == 'forgetall') {
-            setcookie('ORGuid', '', time() - 3600, '/', '', 0);
-            Cookie::kill('ORGuid');
-            setcookie('ORGdomain', '', time() - 3600, '/', '', 0);
-            Cookie::kill('ORGdomain');
+    function handler_404($page)
+    {
+        global $globals, $platal;
+        header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
+        $page->coreTpl('404.tpl');
+        $page->assign('near', $platal->near_hook());
+        $page->trigError('Cette page n\'existe pas !!!');
+    }
+
+    function handler_login($page)
+    {
+        $allkeys = func_get_args();
+        unset($allkeys[0]);
+        $url = join('/',$allkeys);
+        pl_redirect($url);
+    }
+
+    function handler_favicon($page)
+    {
+        global $globals;
+        pl_cached_content_headers("image/x-icon");
+        readfile($globals->spoolroot . '/htdocs/images/favicon.ico');
+        exit;
+    }
+
+    function handler_robotstxt($page)
+    {
+        global $globals;
+
+        $disallowed_uris = array();
+        if ($globals->core->restricted_platal) {
+            $disallowed_uris[] = '/';
+        } else if (!empty($globals->core->robotstxt_disallowed_uris)) {
+            $disallowed_uris = preg_split('/[\s,]+/',
+                                          $globals->core->robotstxt_disallowed_uris,
+                                          -1, PREG_SPLIT_NO_EMPTY);
         }
 
-        if (isset($_SESSION['log'])) {
-            $ref = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
-            $_SESSION['log']->log('deconnexion',$ref);
+        if (count($disallowed_uris) > 0) {
+            pl_cached_content_headers("text/plain");
+            echo "User-agent: *\n";
+            foreach ($disallowed_uris as $uri) {
+                echo "Disallow: $uri\n";
+            }
+            exit;
         }
+        return PL_NOT_FOUND;
+    }
+
+    function handler_purge_cache($page)
+    {
+        S::assert_xsrf_token();
+
+        $page->clear_compiled_tpl();
+        PlWikiPage::clearCache();
+        PlCache::clearAll();
+
+        http_redirect(empty($_SERVER['HTTP_REFERER']) ? './' : $_SERVER['HTTP_REFERER']);
+    }
+
+    function handler_kill_sessions($page)
+    {
+        kill_sessions();
+    }
+
+    function handler_bug($page)
+    {
+        global $globals;
 
-        XorgSession::destroy();
+        if (empty($_SERVER['HTTP_REFERER'])) {
+            // We don't have a valid referer, we need to use the url
+            list($currentPage, $location) = explode('//', $_SERVER['REQUEST_URI'], 2);
 
-        if (Get::has('redirect')) {
-            redirect(rawurldecode(Get::get('redirect')));
+            $location = 'http'.(empty($_SERVER['HTTPS']) ? '' : 's').'://'.$_SERVER['SERVER_NAME'].'/'.$location;
         } else {
-            $page->changeTpl('exit.tpl');
+            $location = $_SERVER['HTTP_REFERER'];
         }
-        return PL_OK;
+
+        $page->coreTpl('bug.tpl', SIMPLE);
+        $page->assign('location', $location);
+        $page->addJsLink('close_on_esc.js');
+
+        if (Env::has('send') && trim(Env::v('detailed_desc'))) {
+            S::assert_xsrf_token();
+
+            $body = wordwrap(Env::v('detailed_desc'), 78) . "\n\n"
+                  . "----------------------------\n"
+                  . "Page        : " . Env::v('page') . "\n\n"
+                  . "Utilisateur : " . S::user()->login() . "\n"
+                  . "Navigateur  : " . $_SERVER['HTTP_USER_AGENT'] . "\n"
+                  . "Skin        : " . S::v('skin') . "\n";
+            $page->assign('bug_sent', 1);
+            $page->trigSuccess('Ton message a bien été envoyé au support de ' . $globals->core->sitename
+                             . ', tu devrais en recevoir une copie d\'ici quelques minutes. Nous allons '
+                             . 'le traiter et y répondre dans les plus brefs délais.');
+            $mymail = new PlMailer();
+            $mymail->setFrom(sprintf('"%s" <%s>', S::user()->fullName(), S::user()->bestEmail()));
+            $mymail->addCc(sprintf('"%s" <%s>', S::user()->fullName(), S::user()->bestEmail()));
+            $mymail->addTo('support+platal@' . $globals->mail->domain);
+            $mymail->setSubject('Plat/al '.Env::v('task_type').' : '.Env::v('item_summary'));
+            $mymail->setTxtBody($body);
+            $mymail->send();
+        } elseif (Env::has('send')) {
+            $page->trigError("Merci de remplir une explication du problème rencontré.");
+        }
+    }
+
+    function handler_wiki_help($page, $action = 'title')
+    {
+        $page->coreTpl('wiki.help.tpl', SIMPLE);
+        $page->assign('wiki_help', MiniWiki::help($action == 'title'));
     }
 
-    function handler_403(&$page)
+    /// Shared handler for wiki syntax result preview
+    function handler_wiki_preview($page, $action = 'title')
     {
-        header('HTTP/1.0 403 Forbidden');
-        $page->changeTpl('403.tpl');
-        return PL_OK;
+        pl_content_headers("text/html");
+        $text = Env::v('text');
+        echo MiniWiki::wikiToHtml($text, $action == 'title');
+        exit;
+    }
+
+    function handler_siteerror($page) {
+        global $globals;
+        $page->coreTpl('site_errors.tpl');
+        if (Post::has('clear')) {
+            PlErrorReport::clear();
+            $page->trigSuccess("Erreurs effacées.");
+        }
+        $page->assign('errors', PlErrorReport::iterate());
     }
 
-    function handler_404(&$page)
+    function handler_embedded($page)
     {
-        header('HTTP/1.0 404 Not Found');
-        $page->changeTpl('404.tpl');
-        return PL_OK;
+        global $platal, $globals;
+        $allkeys = func_get_args();
+        $mode = $allkeys[1];
+        unset($allkeys[0]);
+        unset($allkeys[1]);
+        $_REQUEST['display'] = $mode;
+        $globals->baseurl .= '/embedded/' . $mode;
+        $platal->path = join('/', $allkeys);
+        $platal->run();
     }
 }
 
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>