From 9c0e85d5dda265ac0afd71f17aeec7610cece1a3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Thu, 26 Jul 2018 13:54:16 +0200
Subject: [PATCH] :ambulance: fix(core) Cleaned up error handling in session

Removed unused session entry, made sure all error reset goes through the
 appropriate function.

issue #5854
---
 html/autocomplete.php             |  2 +-
 html/getbin.php                   |  2 +-
 html/geticon.php                  |  2 +-
 html/index.php                    |  4 +---
 html/main.php                     | 17 ++---------------
 html/setup.php                    | 11 -----------
 ihtml/themes/breezy/framework.tpl |  1 -
 ihtml/themes/breezy/login.tpl     |  2 --
 ihtml/themes/legacy/login.tpl     |  2 --
 include/class_standAlonePage.inc  |  3 ---
 include/functions.inc             |  4 +---
 include/login/class_LoginPost.inc | 12 ------------
 setup/setup_frame.tpl             |  1 -
 13 files changed, 7 insertions(+), 56 deletions(-)

diff --git a/html/autocomplete.php b/html/autocomplete.php
index 00c2ebac9..0a400bb6b 100644
--- a/html/autocomplete.php
+++ b/html/autocomplete.php
@@ -26,7 +26,7 @@
 
 session_cache_limiter("private");
 session::start();
-session::global_set('errorsAlreadyPosted', array());
+reset_errors();
 
 /* Logged in? Simple security check */
 if (!session::global_is_set('ui')) {
diff --git a/html/getbin.php b/html/getbin.php
index 40c472ff4..631e0560b 100644
--- a/html/getbin.php
+++ b/html/getbin.php
@@ -27,7 +27,7 @@
 error_reporting (0);
 session_cache_limiter('private');
 session::start();
-session::global_set('errorsAlreadyPosted', array());
+reset_errors();
 
 /* Logged in? Simple security check */
 if (!session::global_is_set('ui')) {
diff --git a/html/geticon.php b/html/geticon.php
index 334d254d9..dbd9cf101 100644
--- a/html/geticon.php
+++ b/html/geticon.php
@@ -25,7 +25,7 @@
 
 session_cache_limiter("private");
 session::start();
-session::global_set('errorsAlreadyPosted', array());
+reset_errors();
 
 $theme = '';
 if (session::global_is_set('config')) {
diff --git a/html/index.php b/html/index.php
index 4f1e1cc42..3af61a05f 100644
--- a/html/index.php
+++ b/html/index.php
@@ -63,9 +63,7 @@ if (isset($_REQUEST['signout']) && $_REQUEST['signout']) {
 }
 
 /* Reset errors */
-session::set('errors', '');
-session::set('errorsAlreadyPosted', '');
-session::set('LastError', '');
+reset_errors();
 
 /* Check if we need to run setup */
 if (!file_exists(CONFIG_DIR.'/'.CONFIG_FILE)) {
diff --git a/html/main.php b/html/main.php
index 2b199e821..dc6211519 100644
--- a/html/main.php
+++ b/html/main.php
@@ -37,7 +37,7 @@ textdomain($domain);
 
 /* Remember everything we did after the last click */
 session::start();
-session::set('errorsAlreadyPosted', array());
+reset_errors();
 session::global_set('runtime_cache', array());
 session::set('limit_exceeded', FALSE);
 
@@ -280,23 +280,12 @@ if (isset($_POST) && count($_POST) && !isset($_POST['php_c_check'])) {
 }
 
 /* Assign errors to smarty */
-if (session::is_set('errors')) {
-  $smarty->assign("errors", session::get('errors'));
-}
 if ($error_collector != "") {
   $smarty->assign("php_errors", preg_replace("/%BUGBODY%/", $error_collector_mailto, $error_collector)."</div>");
 } else {
   $smarty->assign("php_errors", "");
 }
 
-/* Set focus to the error button if we've an error message */
-$focus = "";
-if (session::is_set('errors') && session::get('errors') != "") {
-  $focus = '<script type="text/javascript">';
-  $focus .= 'document.forms[0].error_accept.focus();';
-  $focus .= '</script>';
-}
-
 $focus = '<script type="text/javascript">';
 $focus .= 'next_msg_dialog();';
 $focus .= '</script>';
@@ -336,6 +325,4 @@ echo $display;
 /* Save plist and config */
 session::global_set('plist', $plist);
 session::global_set('config', $config);
-session::set('errorsAlreadyPosted', array());
-
-?>
+reset_errors();
diff --git a/html/setup.php b/html/setup.php
index 908503bde..62dd14c35 100644
--- a/html/setup.php
+++ b/html/setup.php
@@ -50,7 +50,6 @@ ini_set("session.gc_maxlifetime", 24 * 60 * 60);
 /* Start session */
 session::start();
 session::global_set('DEBUGLEVEL', 0);
-session::set('errorsAlreadyPosted', array());
 
 CSRFProtection::check();
 
@@ -107,16 +106,6 @@ require_once("../setup/main.inc");
 $smarty->assign("date", date("l, dS F Y H:i:s O"));
 $header = $smarty->fetch(get_template_path('headers.tpl'));
 
-
-
-/* Set focus to the error button if we've an error message */
-$focus = "";
-if (session::is_set('errors') && session::get('errors') != "") {
-  $focus = '<script type="text/javascript">';
-  $focus .= 'document.forms[0].error_accept.focus();';
-  $focus .= '</script>';
-}
-
 $focus = '<script type="text/javascript">';
 $focus .= 'next_msg_dialog();';
 $focus .= '</script>';
diff --git a/ihtml/themes/breezy/framework.tpl b/ihtml/themes/breezy/framework.tpl
index ba6b98d15..4230424f9 100644
--- a/ihtml/themes/breezy/framework.tpl
+++ b/ihtml/themes/breezy/framework.tpl
@@ -52,7 +52,6 @@
       </tbody>
     </table>
 
-    {$errors}
     {$focus}
     <input type="hidden" name="php_c_check" value="1"/>
     <input type="hidden" name="CSRFtoken" value="{$CSRFtoken}"/>
diff --git a/ihtml/themes/breezy/login.tpl b/ihtml/themes/breezy/login.tpl
index 1f7f6674c..6c7b5a13e 100644
--- a/ihtml/themes/breezy/login.tpl
+++ b/ihtml/themes/breezy/login.tpl
@@ -72,8 +72,6 @@
 </form>
 </div>
 
-{$errors}
-
 </div>
 
 {include file={filePath file="copynotice.tpl"}}
diff --git a/ihtml/themes/legacy/login.tpl b/ihtml/themes/legacy/login.tpl
index fd469c895..ffea89670 100644
--- a/ihtml/themes/legacy/login.tpl
+++ b/ihtml/themes/legacy/login.tpl
@@ -72,8 +72,6 @@
 </form>
 </div>
 
-{$errors}
-
 </div>
 
 {include file={filePath file="copynotice.tpl"}}
diff --git a/include/class_standAlonePage.inc b/include/class_standAlonePage.inc
index ad7ddc7ef..be278aca5 100644
--- a/include/class_standAlonePage.inc
+++ b/include/class_standAlonePage.inc
@@ -163,9 +163,6 @@ class standAlonePage {
     $smarty = get_smarty();
 
     $smarty->assign('PHPSESSID', session_id());
-    if (session::is_set('errors')) {
-      $smarty->assign('errors', session::get('errors'));
-    }
     if ($error_collector != '') {
       $smarty->assign('php_errors', preg_replace('/%BUGBODY%/', $error_collector_mailto, $error_collector).'</div>');
     } else {
diff --git a/include/functions.inc b/include/functions.inc
index 888194677..11d86370c 100644
--- a/include/functions.inc
+++ b/include/functions.inc
@@ -411,7 +411,7 @@ function ldap_get_user ($username)
   $ldap = $config->get_ldap_link();
   if (!$ldap->success()) {
     msg_dialog::display(_('LDAP error'),
-        msgPool::ldaperror($ldap->get_error(), '', LDAP_AUTH).'<br/><br/>'.session::get('errors'),
+        msgPool::ldaperror($ldap->get_error(), '', LDAP_AUTH),
         FATAL_ERROR_DIALOG);
     exit();
   }
@@ -2215,9 +2215,7 @@ function mark($needle, $haystack)
 
 function reset_errors()
 {
-  session::set('errors', '');
   session::set('errorsAlreadyPosted', array());
-  session::set('LastError', '');
 }
 
 function load_all_classes()
diff --git a/include/login/class_LoginPost.inc b/include/login/class_LoginPost.inc
index 61d3ec659..e1d9ff8f4 100644
--- a/include/login/class_LoginPost.inc
+++ b/include/login/class_LoginPost.inc
@@ -63,15 +63,6 @@ class LoginPost extends LoginMethod
     /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
     $smarty->assign('cookies', '<b>'._('Warning').':</b> '._('Your browser has cookies disabled. Please enable cookies and reload this page before logging in!'));
 
-    /* Set focus to the error button if we've an error message */
-    $focus = '';
-    if (session::is_set('errors') && session::get('errors') != '') {
-      $focus = '<script type="text/javascript">';
-      $focus .= 'document.forms[0].error_accept.focus();';
-      $focus .= '</script>';
-    }
-    $smarty->assign('focus', $focus);
-
     static::displayLogin();
   }
 
@@ -127,9 +118,6 @@ class LoginPost extends LoginMethod
 
     /* show login screen */
     $smarty->assign('PHPSESSID', session_id());
-    if (session::is_set('errors')) {
-      $smarty->assign('errors', session::get('errors'));
-    }
     if ($error_collector != '') {
       $smarty->assign('php_errors', preg_replace('/%BUGBODY%/', $error_collector_mailto, $error_collector).'</div>');
     } else {
diff --git a/setup/setup_frame.tpl b/setup/setup_frame.tpl
index 03dd16c50..a5b10331d 100644
--- a/setup/setup_frame.tpl
+++ b/setup/setup_frame.tpl
@@ -36,7 +36,6 @@
       </tbody>
     </table>
 
-    {$errors}
     {$focus}
     <input type="hidden" name="setup_goto_step" value=""/>
     <input type="hidden" name="CSRFtoken" value="{$CSRFtoken}"/>
-- 
GitLab