Moving to GitHub.
[platal.git] / include / security.inc.php
index 683dd10..827e39d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -34,8 +34,13 @@ function check_ip($level)
             $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
         }
         $ips[] = $_SERVER['REMOTE_ADDR'];
-        foreach ($ips as &$ip) {
-            $ip = '(ip & mask) = (' . ip_to_uint($ip) . '& mask)';
+        foreach ($ips as $key=>$ip) {
+            $v = ip_to_uint($ip);
+            if (is_null($v)) {
+                unset($ips[$key]);
+            } else {
+                $ips[$key] = '(ip & mask) = (' . $v . '& mask)';
+            }
         }
         $res = XDB::query('SELECT  state, description
                              FROM  ip_watch
@@ -61,10 +66,11 @@ function check_ip($level)
 
 function check_email($email, $message)
 {
-    $res = XDB::query("SELECT state, description
-        FROM emails_watch
-        WHERE state != 'safe' AND email = {?}", $email);
-    if ($res->numRows()) {
+    $res = XDB::fetchOneCell('SELECT  COUNT(*)
+                                FROM  email_watch
+                               WHERE  state != \'safe\' AND email = {?}',
+                             $email);
+    if ($res) {
         send_warning_mail($message);
         return true;
     }
@@ -73,14 +79,18 @@ function check_email($email, $message)
 
 function check_account()
 {
-    return S::v('watch_account');
+    if (S::user()) {
+        return S::user()->watch;
+    }
+    return false;
 }
 
 function check_redirect($red = null)
 {
     require_once 'emails.inc.php';
     if (is_null($red)) {
-        $red = new Redirect(S::user());
+        $user = S::user();
+        $red = new Redirect($user);
     }
     if ($red->get_uid() == S::v('uid')) {
         $_SESSION['no_redirect'] = !$red->other_active('');
@@ -88,23 +98,28 @@ function check_redirect($red = null)
     }
 }
 
-function send_warning_mail($title)
+function send_warning_mail($title, $body = '')
 {
     global $globals;
     $mailer = new PlMailer();
     $mailer->setFrom("webmaster@" . $globals->mail->domain);
     $mailer->addTo($globals->core->admin_email);
     $mailer->setSubject("[Plat/al Security Alert] $title");
-    $mailer->setTxtBody("Identifiants de session :\n" . var_export($_SESSION, true) . "\n\n"
+    // Note: we can't do $session = var_export($_SESSION, true) as var_export
+    // doesn't handle circular dependency correctly.
+    ob_start();
+    var_dump($_SESSION);
+    $session = ob_get_clean();
+    $mailer->setTxtBody($body . "Identifiants de session :\n" . $session . "\n\n"
         ."Identifiants de connexion :\n" . var_export($_SERVER, true));
     $mailer->send();
 }
 
 function kill_sessions()
 {
-    assert(S::has_perms());
+    assert(S::admin());
     shell_exec('sudo -u root ' . dirname(dirname(__FILE__)) . '/bin/kill_sessions.sh');
 }
 
-// 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:
 ?>