[ Index ]

PHP Cross Reference of MyBB 1.8.27

title

Body

[close]

/admin/ -> index.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  define("IN_MYBB", 1);
  12  define("IN_ADMINCP", 1);
  13  
  14  // Here you can change how much of an Admin CP IP address must match in a previous session for the user is validated (e.g. 3 means a.b.c need to match)
  15  define("ADMIN_IP_SEGMENTS", 0);
  16  define("ADMIN_IPV6_SEGMENTS", 0);
  17  
  18  require_once dirname(dirname(__FILE__))."/inc/init.php";
  19  
  20  $shutdown_queries = $shutdown_functions = array();
  21  
  22  send_page_headers();
  23  
  24  header('X-Frame-Options: SAMEORIGIN');
  25  header('Referrer-Policy: no-referrer');
  26  
  27  if(!isset($config['admin_dir']) || !file_exists(MYBB_ROOT.$config['admin_dir']."/inc/class_page.php"))
  28  {
  29      $config['admin_dir'] = basename(dirname(__FILE__));
  30  }
  31  
  32  define('MYBB_ADMIN_DIR', MYBB_ROOT.$config['admin_dir'].'/');
  33  
  34  define('COPY_YEAR', my_date('Y', TIME_NOW));
  35  
  36  require_once  MYBB_ADMIN_DIR."inc/class_page.php";
  37  require_once  MYBB_ADMIN_DIR."inc/class_form.php";
  38  require_once  MYBB_ADMIN_DIR."inc/class_table.php";
  39  require_once  MYBB_ADMIN_DIR."inc/functions.php";
  40  require_once  MYBB_ROOT."inc/functions_user.php";
  41  
  42  // Set cookie path to our admin dir temporarily, i.e. so that it affects the ACP only
  43  $loc = get_current_location('', '', true);
  44  $mybb->settings['cookiepath'] = substr($loc, 0, strrpos($loc, "/{$config['admin_dir']}/"))."/{$config['admin_dir']}/";
  45  
  46  if(!isset($cp_language))
  47  {
  48      $lang->set_language($mybb->settings['cplanguage'], "admin");
  49  }
  50  
  51  // Load global language phrases
  52  $lang->load("global");
  53  $lang->load("messages", true);
  54  
  55  if(function_exists('mb_internal_encoding') && !empty($lang->settings['charset']))
  56  {
  57      @mb_internal_encoding($lang->settings['charset']);
  58  }
  59  
  60  header("Content-type: text/html; charset={$lang->settings['charset']}");
  61  
  62  $time = TIME_NOW;
  63  $errors = null;
  64  
  65  if(is_dir(MYBB_ROOT."install") && !file_exists(MYBB_ROOT."install/lock"))
  66  {
  67      $mybb->trigger_generic_error("install_directory");
  68  }
  69  
  70  $ip_address = get_ip();
  71  unset($user);
  72  
  73  // Load Admin CP style
  74  if(!isset($cp_style))
  75  {
  76      if(!empty($mybb->settings['cpstyle']) && file_exists(MYBB_ADMIN_DIR."/styles/".$mybb->settings['cpstyle']."/main.css"))
  77      {
  78          $cp_style = $mybb->settings['cpstyle'];
  79      }
  80      else
  81      {
  82          $cp_style = "default";
  83      }
  84  }
  85  
  86  $default_page = new DefaultPage;
  87  
  88  $logged_out = false;
  89  $fail_check = 0;
  90  $post_verify = true;
  91  
  92  foreach(array('action', 'do', 'module') as $input)
  93  {
  94      if(!isset($mybb->input[$input]))
  95      {
  96          $mybb->input[$input] = '';
  97      }
  98  }
  99  
 100  if($mybb->input['action'] == "unlock")
 101  {
 102      $user = array();
 103      $error = '';
 104  
 105      $plugins->run_hooks("admin_unlock_start");
 106  
 107      if($mybb->input['username'])
 108      {
 109          $user = get_user_by_username($mybb->input['username'], array('fields' => '*'));
 110  
 111          if(!$user['uid'])
 112          {
 113              $error = $lang->error_invalid_username;
 114          }
 115      }
 116      else if($mybb->input['uid'])
 117      {
 118          $user = get_user($mybb->input['uid']);
 119          if(!$user['uid'])
 120          {
 121              $error = $lang->error_invalid_uid;
 122          }
 123      }
 124  
 125      // Do we have the token? If so let's process it
 126      if($mybb->input['token'] && $user['uid'])
 127      {
 128          $query = $db->simple_select("awaitingactivation", "COUNT(aid) AS num", "uid='".(int)$user['uid']."' AND code='".$db->escape_string($mybb->input['token'])."' AND type='l'");
 129  
 130          $plugins->run_hooks("admin_unlock_end");
 131  
 132          // If we're good to go
 133          if($db->fetch_field($query, "num") > 0)
 134          {
 135              $db->delete_query("awaitingactivation", "uid='".(int)$user['uid']."' AND code='".$db->escape_string($mybb->input['token'])."' AND type='l'");
 136              $db->update_query("adminoptions", array('loginlockoutexpiry' => 0, 'loginattempts' => 0), "uid='".(int)$user['uid']."'");
 137  
 138              admin_redirect("index.php");
 139          }
 140          else
 141          {
 142              $error = $lang->error_invalid_token;
 143          }
 144      }
 145  
 146      $default_page->show_lockout_unlock($error, 'error');
 147  }
 148  elseif($mybb->input['do'] == "login")
 149  {
 150      $plugins->run_hooks("admin_login");
 151  
 152      // We have an adminsid cookie?
 153      if(isset($mybb->cookies['adminsid']))
 154      {
 155          // Check admin session
 156          $query = $db->simple_select("adminsessions", "sid", "sid='".$db->escape_string($mybb->cookies['adminsid'])."'");
 157          $admin_session = $db->fetch_field($query, 'sid');
 158  
 159          // Session found: redirect to index
 160          if($admin_session)
 161          {
 162              admin_redirect("index.php");
 163          }
 164      }
 165  
 166      require_once  MYBB_ROOT."inc/datahandlers/login.php";
 167      $loginhandler = new LoginDataHandler("get");
 168  
 169      // Determine login method
 170      $login_lang_string = $lang->error_invalid_username_password;
 171      switch($mybb->settings['username_method'])
 172      {
 173          case 0: // Username only
 174              $login_lang_string = $lang->sprintf($login_lang_string, $lang->login_username);
 175              break;
 176          case 1: // Email only
 177              $login_lang_string = $lang->sprintf($login_lang_string, $lang->login_email);
 178              break;
 179          case 2: // Username and email
 180          default:
 181              $login_lang_string = $lang->sprintf($login_lang_string, $lang->login_username_and_password);
 182              break;
 183      }
 184  
 185      // Validate PIN first
 186      if(!empty($config['secret_pin']) && (empty($mybb->input['pin']) || $mybb->input['pin'] != $config['secret_pin']))
 187      {
 188          $login_user = get_user_by_username($mybb->input['username'], array('fields' => array('email', 'username')));
 189  
 190          $plugins->run_hooks("admin_login_incorrect_pin");
 191  
 192          if($login_user['uid'] > 0)
 193          {
 194              $db->update_query("adminoptions", array("loginattempts" => "loginattempts+1"), "uid='".(int)$login_user['uid']."'", '', true);
 195          }
 196  
 197          $loginattempts = login_attempt_check_acp($login_user['uid'], true);
 198  
 199          // Have we attempted too many times?
 200          if($loginattempts !== false && $loginattempts['loginattempts'] > 0)
 201          {
 202              // Have we set an expiry yet?
 203              if($loginattempts['loginlockoutexpiry'] == 0)
 204              {
 205                  $db->update_query("adminoptions", array("loginlockoutexpiry" => TIME_NOW+((int)$mybb->settings['loginattemptstimeout']*60)), "uid='".(int)$login_user['uid']."'");
 206              }
 207  
 208              // Did we hit lockout for the first time? Send the unlock email to the administrator
 209              if($loginattempts['loginattempts'] == $mybb->settings['maxloginattempts'])
 210              {
 211                  $db->delete_query("awaitingactivation", "uid='".(int)$login_user['uid']."' AND type='l'");
 212                  $lockout_array = array(
 213                      "uid" => $login_user['uid'],
 214                      "dateline" => TIME_NOW,
 215                      "code" => random_str(),
 216                      "type" => "l"
 217                  );
 218                  $db->insert_query("awaitingactivation", $lockout_array);
 219  
 220                  $subject = $lang->sprintf($lang->locked_out_subject, $mybb->settings['bbname']);
 221                  $message = $lang->sprintf($lang->locked_out_message, htmlspecialchars_uni($mybb->input['username']), $mybb->settings['bbname'], $mybb->settings['maxloginattempts'], $mybb->settings['bburl'], $mybb->config['admin_dir'], $lockout_array['code'], $lockout_array['uid']);
 222                  my_mail($login_user['email'], $subject, $message);
 223              }
 224  
 225              log_admin_action(array(
 226                      'type' => 'admin_locked_out',
 227                      'uid' => (int)$login_user['uid'],
 228                      'username' => $login_user['username'],
 229                  )
 230              );
 231  
 232              $default_page->show_lockedout();
 233          }
 234          else
 235          {
 236              $default_page->show_login($login_lang_string, "error");
 237          }
 238      }
 239  
 240      $loginhandler->set_data(array(
 241          'username' => $mybb->input['username'],
 242          'password' => $mybb->input['password']
 243      ));
 244  
 245      if($loginhandler->validate_login() == true)
 246      {
 247          $mybb->user = get_user($loginhandler->login_data['uid']);
 248      }
 249  
 250      if(!empty($mybb->user['uid']))
 251      {
 252          if(login_attempt_check_acp($mybb->user['uid']) == true)
 253          {
 254              log_admin_action(array(
 255                      'type' => 'admin_locked_out',
 256                      'uid' => (int)$mybb->user['uid'],
 257                      'username' => $mybb->user['username'],
 258                  )
 259              );
 260  
 261              $default_page->show_lockedout();
 262          }
 263  
 264          $plugins->run_hooks("admin_login_success");
 265  
 266          $db->delete_query("adminsessions", "uid='{$mybb->user['uid']}'");
 267  
 268          $sid = md5(random_str(50));
 269  
 270          $useragent = $_SERVER['HTTP_USER_AGENT'];
 271          if(my_strlen($useragent) > 200)
 272          {
 273              $useragent = my_substr($useragent, 0, 200);
 274          }
 275  
 276          // Create a new admin session for this user
 277          $admin_session = array(
 278              "sid" => $sid,
 279              "uid" => $mybb->user['uid'],
 280              "loginkey" => $mybb->user['loginkey'],
 281              "ip" => $db->escape_binary(my_inet_pton(get_ip())),
 282              "dateline" => TIME_NOW,
 283              "lastactive" => TIME_NOW,
 284              "data" => my_serialize(array()),
 285              "useragent" => $db->escape_string($useragent),
 286              "authenticated" => 0,
 287          );
 288          $db->insert_query("adminsessions", $admin_session);
 289          $admin_session['data'] = array();
 290  
 291          // Only reset the loginattempts when we're really logged in and the user doesn't need to enter a 2fa code
 292          $query = $db->simple_select("adminoptions", "authsecret", "uid='{$mybb->user['uid']}'");
 293          $admin_options = $db->fetch_array($query);
 294          if(empty($admin_options['authsecret']))
 295          {
 296              $db->update_query("adminoptions", array("loginattempts" => 0, "loginlockoutexpiry" => 0), "uid='{$mybb->user['uid']}'");
 297          }
 298  
 299          my_setcookie("adminsid", $sid, '', true, "lax");
 300          my_setcookie('acploginattempts', 0);
 301          $post_verify = false;
 302  
 303          $mybb->request_method = "get";
 304  
 305          if(!empty($mybb->input['module']))
 306          {
 307              // $query_string should contain the module
 308              $query_string = '?module='.htmlspecialchars_uni($mybb->input['module']);
 309  
 310              // Now we look for any paramters passed in $_SERVER['QUERY_STRING']
 311              if($_SERVER['QUERY_STRING'])
 312              {
 313                  $qstring = '?'.preg_replace('#adminsid=(.{32})#i', '', $_SERVER['QUERY_STRING']);
 314                  $qstring = str_replace('action=logout', '', $qstring);
 315                  $qstring = preg_replace('#&+#', '&', $qstring);
 316                  $qstring = str_replace('?&', '?', $qstring);
 317  
 318                  // So what do we do? We know that parameters are devided by ampersands
 319                  // That means we must get to work!
 320                  $parameters = explode('&', $qstring);
 321  
 322                  // Remove our first member if it's for the module
 323                  if(substr($parameters[0], 0, 8) == '?module=')
 324                  {
 325                      unset($parameters[0]);
 326                  }
 327  
 328                  foreach($parameters as $key => $param)
 329                  {
 330                      $params = explode("=", $param);
 331  
 332                      $query_string .= '&'.htmlspecialchars_uni($params[0])."=".htmlspecialchars_uni($params[1]);
 333                  }
 334              }
 335  
 336              admin_redirect("index.php".$query_string);
 337          }
 338      }
 339      else
 340      {
 341          $login_user = get_user_by_username($mybb->input['username'], array('fields' => array('email', 'username')));
 342  
 343          $plugins->run_hooks("admin_login_fail");
 344  
 345          $loginattempts = false;
 346          if(!empty($login_user['uid']) && $login_user['uid'] > 0)
 347          {
 348              $db->update_query("adminoptions", array("loginattempts" => "loginattempts+1"), "uid='".(int)$login_user['uid']."'", '', true);
 349              $loginattempts = login_attempt_check_acp($login_user['uid'], true);
 350          }
 351  
 352          // Have we attempted too many times?
 353          if($loginattempts !== false && $loginattempts['loginattempts'] > 0)
 354          {
 355              // Have we set an expiry yet?
 356              if($loginattempts['loginlockoutexpiry'] == 0)
 357              {
 358                  $db->update_query("adminoptions", array("loginlockoutexpiry" => TIME_NOW+((int)$mybb->settings['loginattemptstimeout']*60)), "uid='".(int)$login_user['uid']."'");
 359              }
 360  
 361              $plugins->run_hooks("admin_login_lockout");
 362  
 363              // Did we hit lockout for the first time? Send the unlock email to the administrator
 364              if($loginattempts['loginattempts'] == $mybb->settings['maxloginattempts'])
 365              {
 366                  $db->delete_query("awaitingactivation", "uid='".(int)$login_user['uid']."' AND type='l'");
 367                  $lockout_array = array(
 368                      "uid" => $login_user['uid'],
 369                      "dateline" => TIME_NOW,
 370                      "code" => random_str(),
 371                      "type" => "l"
 372                  );
 373                  $db->insert_query("awaitingactivation", $lockout_array);
 374  
 375                  $subject = $lang->sprintf($lang->locked_out_subject, $mybb->settings['bbname']);
 376                  $message = $lang->sprintf($lang->locked_out_message, htmlspecialchars_uni($mybb->input['username']), $mybb->settings['bbname'], $mybb->settings['maxloginattempts'], $mybb->settings['bburl'], $mybb->config['admin_dir'], $lockout_array['code'], $lockout_array['uid']);
 377                  my_mail($login_user['email'], $subject, $message);
 378              }
 379  
 380              log_admin_action(array(
 381                      'type' => 'admin_locked_out',
 382                      'uid' => (int)$login_user['uid'],
 383                      'username' => $login_user['username'],
 384                  )
 385              );
 386  
 387              $default_page->show_lockedout();
 388          }
 389  
 390          $fail_check = 1;
 391      }
 392  }
 393  else
 394  {
 395      // No admin session - show message on the login screen
 396      if(!isset($mybb->cookies['adminsid']))
 397      {
 398          $login_message = "";
 399      }
 400      // Otherwise, check admin session
 401      else
 402      {
 403          $query = $db->simple_select("adminsessions", "*", "sid='".$db->escape_string($mybb->cookies['adminsid'])."'");
 404          $admin_session = $db->fetch_array($query);
 405  
 406          // No matching admin session found - show message on login screen
 407          if(empty($admin_session) || !$admin_session['sid'])
 408          {
 409              $login_message = $lang->error_invalid_admin_session;
 410          }
 411          else
 412          {
 413              $admin_session['data'] = my_unserialize($admin_session['data']);
 414  
 415              // Fetch the user from the admin session
 416              $mybb->user = get_user($admin_session['uid']);
 417  
 418              // Login key has changed - force logout
 419              if(!$mybb->user['uid'] || $mybb->user['loginkey'] !== $admin_session['loginkey'])
 420              {
 421                  unset($mybb->user);
 422              }
 423              else
 424              {
 425                  // Admin CP sessions 2 hours old are expired
 426                  if($admin_session['lastactive'] < TIME_NOW-7200)
 427                  {
 428                      $login_message = $lang->error_admin_session_expired;
 429                      $db->delete_query("adminsessions", "sid='".$db->escape_string($mybb->cookies['adminsid'])."'");
 430                      unset($mybb->user);
 431                  }
 432                  // If IP matching is set - check IP address against the session IP
 433                  else if(ADMIN_IP_SEGMENTS > 0 && strpos($ip_address, ':') === false)
 434                  {
 435                      $exploded_ip = explode(".", $ip_address);
 436                      $exploded_admin_ip = explode(".", my_inet_ntop($admin_session['ip']));
 437                      $matches = 0;
 438                      $valid_ip = false;
 439                      for($i = 0; $i < ADMIN_IP_SEGMENTS; ++$i)
 440                      {
 441                          if($exploded_ip[$i] == $exploded_admin_ip[$i])
 442                          {
 443                              ++$matches;
 444                          }
 445                          if($matches == ADMIN_IP_SEGMENTS)
 446                          {
 447                              $valid_ip = true;
 448                              break;
 449                          }
 450                      }
 451  
 452                      // IP doesn't match properly - show message on logon screen
 453                      if(!$valid_ip)
 454                      {
 455                          $login_message = $lang->error_invalid_ip;
 456                          unset($mybb->user);
 457                      }
 458                  }
 459                  else if(ADMIN_IPV6_SEGMENTS > 0 && strpos($ip_address, ':') !== false)
 460                  {
 461                      // Expand IPv6 addresses
 462                      $hex = unpack("H*hex", my_inet_pton($ip_address));
 463                      $expanded_ip = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);
 464                      $hex_admin = unpack("H*hex", $admin_session['ip']);
 465                      $expanded_admin_ip = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex_admin['hex']), 0, -1);
 466  
 467                      $exploded_ip = explode(":", $expanded_ip);
 468                      $exploded_admin_ip = explode(":", $expanded_admin_ip);
 469                      $matches = 0;
 470                      $valid_ip = false;
 471                      for($i = 0; $i < ADMIN_IPV6_SEGMENTS; ++$i)
 472                      {
 473                          if($exploded_ip[$i] == $exploded_admin_ip[$i])
 474                          {
 475                              ++$matches;
 476                          }
 477                          if($matches == ADMIN_IPV6_SEGMENTS)
 478                          {
 479                              $valid_ip = true;
 480                              break;
 481                          }
 482                      }
 483  
 484                      // IP doesn't match properly - show message on logon screen
 485                      if(!$valid_ip)
 486                      {
 487                          $login_message = $lang->error_invalid_ip;
 488                          unset($mybb->user);
 489                      }
 490                  }
 491              }
 492          }
 493      }
 494  }
 495  
 496  if($mybb->input['action'] == "logout" && $mybb->user)
 497  {
 498      $plugins->run_hooks("admin_logout");
 499  
 500      if(verify_post_check($mybb->input['my_post_key']))
 501      {
 502          $db->delete_query("adminsessions", "sid='".$db->escape_string($mybb->cookies['adminsid'])."'");
 503          my_unsetcookie('adminsid');
 504          $logged_out = true;
 505      }
 506  }
 507  
 508  if(!isset($mybb->user['usergroup']))
 509  {
 510      $mybbgroups = 1;
 511  }
 512  else
 513  {
 514      $mybbgroups = $mybb->user['usergroup'].",".$mybb->user['additionalgroups'];
 515  }
 516  $mybb->usergroup = usergroup_permissions($mybbgroups);
 517  
 518  $is_super_admin = false;
 519  if(isset($mybb->user['uid']))
 520  {
 521      $is_super_admin = is_super_admin($mybb->user['uid']);
 522  }
 523  
 524  if($mybb->usergroup['cancp'] != 1 && !$is_super_admin || !$mybb->user['uid'])
 525  {
 526      $uid = 0;
 527      if(isset($mybb->user['uid']))
 528      {
 529          $uid = (int)$mybb->user['uid'];
 530      }
 531      $db->delete_query("adminsessions", "uid = '{$uid}'");
 532      unset($mybb->user);
 533      my_unsetcookie('adminsid');
 534  }
 535  
 536  if(!empty($mybb->user['uid']))
 537  {
 538      $query = $db->simple_select("adminoptions", "*", "uid='".$mybb->user['uid']."'");
 539      $admin_options = $db->fetch_array($query);
 540  
 541      // Only update language / theme once fully authenticated
 542      if(empty($admin_options['authsecret']) || $admin_session['authenticated'] == 1)
 543      {
 544          if(!empty($admin_options['cplanguage']))
 545          {
 546              $cp_language = $admin_options['cplanguage'];
 547              $lang->set_language($cp_language, "admin");
 548              $lang->load("global"); // Reload global language vars
 549              $lang->load("messages", true);
 550          }
 551  
 552          if(!empty($admin_options['cpstyle']) && file_exists(MYBB_ADMIN_DIR."/styles/{$admin_options['cpstyle']}/main.css"))
 553          {
 554              $cp_style = $admin_options['cpstyle'];
 555          }
 556      }
 557  
 558      // Update the session information in the DB
 559      if($admin_session['sid'])
 560      {
 561          $db->update_query("adminsessions", array('lastactive' => TIME_NOW, 'ip' => $db->escape_binary(my_inet_pton(get_ip()))), "sid='".$db->escape_string($admin_session['sid'])."'");
 562      }
 563  
 564      // Fetch administrator permissions
 565      $mybb->admin['permissions'] = get_admin_permissions($mybb->user['uid']);
 566  }
 567  
 568  // Include the layout generation class overrides for this style
 569  if(file_exists(MYBB_ADMIN_DIR."/styles/{$cp_style}/style.php"))
 570  {
 571      require_once MYBB_ADMIN_DIR."/styles/{$cp_style}/style.php";
 572  }
 573  
 574  // Check if any of the layout generation classes we can override exist in the style file
 575  $classes = array(
 576      "Page" => "DefaultPage",
 577      "SidebarItem" => "DefaultSidebarItem",
 578      "PopupMenu" => "DefaultPopupMenu",
 579      "Table" => "DefaultTable",
 580      "Form" => "DefaultForm",
 581      "FormContainer" => "DefaultFormContainer"
 582  );
 583  foreach($classes as $style_name => $default_name)
 584  {
 585      // Style does not have this layout generation class, create it
 586      if(!class_exists($style_name))
 587      {
 588          eval("class {$style_name} extends {$default_name} { }");
 589      }
 590  }
 591  
 592  $page = new Page;
 593  $page->style = $cp_style;
 594  
 595  // Do not have a valid Admin user, throw back to login page.
 596  if(!isset($mybb->user['uid']) || $logged_out == true)
 597  {
 598      if($logged_out == true)
 599      {
 600          $page->show_login($lang->success_logged_out);
 601      }
 602      elseif($fail_check == 1)
 603      {
 604          $page->show_login($login_lang_string, "error");
 605      }
 606      else
 607      {
 608          // If we have this error while retreiving it from an AJAX request, then send back a nice error
 609          if(isset($mybb->input['ajax']) && $mybb->input['ajax'] == 1)
 610          {
 611              echo json_encode(array("errors" => array("login")));
 612              exit;
 613          }
 614          $page->show_login($login_message, "error");
 615      }
 616  }
 617  
 618  // Time to check for Two-Factor Authentication
 619  // First: are we trying to verify a code?
 620  if($mybb->input['do'] == "do_2fa" && $mybb->request_method == "post")
 621  {
 622      // Test whether it's a recovery code
 623      $recovery = false;
 624      $codes = my_unserialize($admin_options['recovery_codes']);
 625      if(!empty($codes) && in_array($mybb->get_input('code'), $codes))
 626      {
 627          $recovery = true;
 628          $ncodes = array_diff($codes, array($mybb->input['code'])); // Removes our current code from the codes array
 629          $db->update_query("adminoptions", array("recovery_codes" => $db->escape_string(my_serialize($ncodes))), "uid='{$mybb->user['uid']}'");
 630  
 631          if(count($ncodes) == 0)
 632          {
 633              flash_message($lang->my2fa_no_codes, "error");
 634          }
 635      }
 636  
 637      // Validate the code
 638      require_once  MYBB_ROOT."inc/3rdparty/2fa/GoogleAuthenticator.php";
 639      $auth = new PHPGangsta_GoogleAuthenticator;
 640  
 641      $test = $auth->verifyCode($admin_options['authsecret'], $mybb->get_input('code'));
 642  
 643      // Either the code was okay or it was a recovery code
 644      if($test === true || $recovery === true)
 645      {
 646          // Correct code -> session authenticated
 647          $db->update_query("adminsessions", array("authenticated" => 1), "sid='".$db->escape_string($mybb->cookies['adminsid'])."'");
 648          $admin_session['authenticated'] = 1;
 649          $db->update_query("adminoptions", array("loginattempts" => 0, "loginlockoutexpiry" => 0), "uid='{$mybb->user['uid']}'");
 650          my_setcookie('acploginattempts', 0);
 651          admin_redirect("index.php");
 652      }
 653      else
 654      {
 655          // Wrong code -> close session (aka logout)
 656          $db->delete_query("adminsessions", "sid='".$db->escape_string($mybb->cookies['adminsid'])."'");
 657          my_unsetcookie('adminsid');
 658  
 659          // Now test whether we need to lock this guy completly
 660          $db->update_query("adminoptions", array("loginattempts" => "loginattempts+1"), "uid='{$mybb->user['uid']}'", '', true);
 661  
 662          $loginattempts = login_attempt_check_acp($mybb->user['uid'], true);
 663  
 664          // Have we attempted too many times?
 665          if($loginattempts !== false && $loginattempts['loginattempts'] > 0)
 666          {
 667              // Have we set an expiry yet?
 668              if($loginattempts['loginlockoutexpiry'] == 0)
 669              {
 670                  $db->update_query("adminoptions", array("loginlockoutexpiry" => TIME_NOW+((int)$mybb->settings['loginattemptstimeout']*60)), "uid='{$mybb->user['uid']}'");
 671               }
 672  
 673              // Did we hit lockout for the first time? Send the unlock email to the administrator
 674              if($loginattempts['loginattempts'] == $mybb->settings['maxloginattempts'])
 675              {
 676                  $db->delete_query("awaitingactivation", "uid='{$mybb->user['uid']}' AND type='l'");
 677                  $lockout_array = array(
 678                      "uid" => $mybb->user['uid'],
 679                      "dateline" => TIME_NOW,
 680                      "code" => random_str(),
 681                      "type" => "l"
 682                  );
 683                  $db->insert_query("awaitingactivation", $lockout_array);
 684  
 685                  $subject = $lang->sprintf($lang->locked_out_subject, $mybb->settings['bbname']);
 686                  $message = $lang->sprintf($lang->locked_out_message, htmlspecialchars_uni($mybb->user['username']), $mybb->settings['bbname'], $mybb->settings['maxloginattempts'], $mybb->settings['bburl'], $mybb->config['admin_dir'], $lockout_array['code'], $lockout_array['uid']);
 687                  my_mail($mybb->user['email'], $subject, $message);
 688              }
 689  
 690              log_admin_action(array(
 691                      'type' => 'admin_locked_out',
 692                      'uid' => $mybb->user['uid'],
 693                      'username' => $mybb->user['username'],
 694                  )
 695              );
 696  
 697              $page->show_lockedout();
 698          }
 699  
 700          // Still here? Show a custom login page
 701          $page->show_login($lang->my2fa_failed, "error");
 702      }
 703  }
 704  
 705  // Show our 2FA page
 706  if(!empty($admin_options['authsecret']) && $admin_session['authenticated'] != 1)
 707  {
 708      $page->show_2fa();
 709  }
 710  
 711  $page->add_breadcrumb_item($lang->home, "index.php");
 712  
 713  // Begin dealing with the modules
 714  $modules_dir = MYBB_ADMIN_DIR."modules";
 715  $dir = opendir($modules_dir);
 716  while(($module = readdir($dir)) !== false)
 717  {
 718      if(is_dir($modules_dir."/".$module) && !in_array($module, array(".", "..")) && file_exists($modules_dir."/".$module."/module_meta.php"))
 719      {
 720          require_once $modules_dir."/".$module."/module_meta.php";
 721  
 722          // Need to always load it for admin permissions / quick access
 723          $lang->load($module."_module_meta", false, true);
 724  
 725          $has_permission = false;
 726          if(function_exists($module."_admin_permissions"))
 727          {
 728              if(isset($mybb->admin['permissions'][$module]) || $is_super_admin == true)
 729              {
 730                  $has_permission = true;
 731              }
 732          }
 733          // This module doesn't support permissions
 734          else
 735          {
 736              $has_permission = true;
 737          }
 738  
 739          // Do we have permissions to run this module (Note: home is accessible by all)
 740          if($module == "home" || $has_permission == true)
 741          {
 742              $meta_function = $module."_meta";
 743              $initialized = $meta_function();
 744              if($initialized == true)
 745              {
 746                  $modules[$module] = 1;
 747              }
 748          }
 749          else
 750          {
 751              $modules[$module] = 0;
 752          }
 753      }
 754  }
 755  
 756  $modules = $plugins->run_hooks("admin_tabs", $modules);
 757  
 758  closedir($dir);
 759  
 760  if(strpos($mybb->input['module'], "/") !== false)
 761  {
 762      $current_module = explode("/", $mybb->input['module'], 2);
 763  }
 764  else
 765  {
 766      $current_module = explode("-", $mybb->input['module'], 2);
 767  }
 768  
 769  if(!isset($current_module[1]))
 770  {
 771      $current_module[1] = 'home';
 772  }
 773  
 774  if($mybb->input['module'] && isset($modules[$current_module[0]]))
 775  {
 776      $run_module = $current_module[0];
 777  }
 778  else
 779  {
 780      $run_module = "home";
 781  }
 782  
 783  $action_handler = $run_module."_action_handler";
 784  $action_file = $action_handler($current_module[1]);
 785  
 786  // Set our POST validation code here
 787  $mybb->post_code = generate_post_check();
 788  
 789  if($run_module != "home")
 790  {
 791      check_admin_permissions(array('module' => $page->active_module, 'action' => $page->active_action));
 792  }
 793  
 794  // Only POST actions with a valid post code can modify information. Here we check if the incoming request is a POST and if that key is valid.
 795  $post_check_ignores = array(
 796      "example/page" => array("action")
 797  ); // An array of modules/actions to ignore POST checks for.
 798  
 799  if($mybb->request_method == "post")
 800  {
 801      if(in_array($mybb->input['module'], $post_check_ignores))
 802      {
 803          $k = array_search($mybb->input['module'], $post_check_ignores);
 804          if(in_array($mybb->input['action'], $post_check_ignores[$k]))
 805          {
 806              $post_verify = false;
 807          }
 808      }
 809  
 810      if($post_verify == true)
 811      {
 812          // If the post key does not match we switch the action to GET and set a message to show the user
 813          if(!verify_post_check($mybb->input['my_post_key'], true))
 814          {
 815              $mybb->request_method = "get";
 816              $page->show_post_verify_error = true;
 817          }
 818      }
 819  }
 820  
 821  $lang->load("{$run_module}_{$page->active_action}", false, true);
 822  
 823  $plugins->run_hooks("admin_load");
 824  
 825  require $modules_dir."/".$run_module."/".$action_file;
 826  


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref