PageRenderTime 32ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Upload/admin/modules/home/preferences.php

https://gitlab.com/Conors99/ppm-1.8
PHP | 191 lines | 130 code | 40 blank | 21 comment | 17 complexity | 710038e7db9629a1939f25443b6a89df MD5 | raw file
  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. // Disallow direct access to this file for security reasons
  11. if(!defined("IN_MYBB"))
  12. {
  13. die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  14. }
  15. $page->add_breadcrumb_item($lang->preferences_and_personal_notes, "index.php?module=home-preferences");
  16. $plugins->run_hooks("admin_home_preferences_begin");
  17. if($mybb->input['action'] == "recovery_codes")
  18. {
  19. $page->add_breadcrumb_item($lang->recovery_codes, "index.php?module=home-preferences&action=recovery_codes");
  20. // First: regenerate the codes
  21. $codes = generate_recovery_codes();
  22. $db->update_query("adminoptions", array("recovery_codes" => $db->escape_string(my_serialize($codes))), "uid='{$mybb->user['uid']}'");
  23. // And now display them
  24. $page->output_header($lang->recovery_codes);
  25. $table = new Table;
  26. $table->construct_header($lang->recovery_codes);
  27. $table->construct_cell($lang->recovery_codes_warning);
  28. $table->construct_row();
  29. $table->construct_cell(implode("<br />", $codes));
  30. $table->construct_row();
  31. $table->output($lang->recovery_codes);
  32. $page->output_footer();
  33. }
  34. if(!$mybb->input['action'])
  35. {
  36. require_once MYBB_ROOT."inc/3rdparty/2fa/GoogleAuthenticator.php";
  37. $auth = new PHPGangsta_GoogleAuthenticator;
  38. $plugins->run_hooks("admin_home_preferences_start");
  39. if($mybb->request_method == "post")
  40. {
  41. $query = $db->simple_select("adminoptions", "permissions, defaultviews, authsecret, recovery_codes", "uid='{$mybb->user['uid']}'");
  42. $adminopts = $db->fetch_array($query);
  43. $secret = $adminopts['authsecret'];
  44. // Was the option changed? empty = disabled so ==
  45. if($mybb->input['2fa'] == empty($secret))
  46. {
  47. // 2FA was enabled -> create secret and log
  48. if($mybb->input['2fa'])
  49. {
  50. $secret = $auth->createSecret();
  51. // We don't want to close this session now
  52. $db->update_query("adminsessions", array("authenticated" => 1), "sid='".$db->escape_string($mybb->cookies['adminsid'])."'");
  53. log_admin_action("enabled");
  54. }
  55. // 2FA was disabled -> clear secret
  56. else
  57. {
  58. $secret = "";
  59. $adminopts['recovery_codes'] = "";
  60. log_admin_action("disabled");
  61. }
  62. }
  63. $sqlarray = array(
  64. "notes" => $db->escape_string($mybb->input['notes']),
  65. "cpstyle" => $db->escape_string($mybb->input['cpstyle']),
  66. "cplanguage" => $db->escape_string($mybb->input['cplanguage']),
  67. "permissions" => $db->escape_string($adminopts['permissions']),
  68. "defaultviews" => $db->escape_string($adminopts['defaultviews']),
  69. "uid" => $mybb->user['uid'],
  70. "codepress" => $mybb->get_input('codepress', MyBB::INPUT_INT), // It's actually CodeMirror but for compatibility purposes lets leave it codepress
  71. "authsecret" => $db->escape_string($secret),
  72. "recovery_codes" => $db->escape_string($adminopts['recovery_codes']),
  73. );
  74. $db->replace_query("adminoptions", $sqlarray, "uid");
  75. $plugins->run_hooks("admin_home_preferences_start_commit");
  76. flash_message($lang->success_preferences_updated, 'success');
  77. admin_redirect("index.php?module=home-preferences");
  78. }
  79. $page->output_header($lang->preferences_and_personal_notes);
  80. $sub_tabs['preferences'] = array(
  81. 'title' => $lang->preferences_and_personal_notes,
  82. 'link' => "index.php?module=home-preferences",
  83. 'description' => $lang->prefs_and_personal_notes_description
  84. );
  85. $page->output_nav_tabs($sub_tabs, 'preferences');
  86. $query = $db->simple_select("adminoptions", "notes, cpstyle, cplanguage, codepress, authsecret", "uid='".$mybb->user['uid']."'", array('limit' => 1));
  87. $admin_options = $db->fetch_array($query);
  88. $form = new Form("index.php?module=home-preferences", "post");
  89. $dir = @opendir(MYBB_ADMIN_DIR."/styles");
  90. $folders = array();
  91. while($folder = readdir($dir))
  92. {
  93. if($folder != "." && $folder != ".." && @file_exists(MYBB_ADMIN_DIR."/styles/$folder/main.css"))
  94. {
  95. $folders[$folder] = ucfirst($folder);
  96. }
  97. }
  98. closedir($dir);
  99. ksort($folders);
  100. $setting_code = $form->generate_select_box("cpstyle", $folders, $admin_options['cpstyle']);
  101. $languages = array_merge(array('' => $lang->use_default), $lang->get_languages(1));
  102. $language_code = $form->generate_select_box("cplanguage", $languages, $admin_options['cplanguage']);
  103. $table = new Table;
  104. $table->construct_header($lang->global_preferences);
  105. $table->construct_cell("<strong>{$lang->acp_theme}</strong><br /><small>{$lang->select_acp_theme}</small><br /><br />{$setting_code}");
  106. $table->construct_row();
  107. $table->construct_cell("<strong>{$lang->acp_language}</strong><br /><small>{$lang->select_acp_language}</small><br /><br />{$language_code}");
  108. $table->construct_row();
  109. $table->construct_cell("<strong>{$lang->codemirror}</strong><br /><small>{$lang->use_codemirror_desc}</small><br /><br />".$form->generate_on_off_radio('codepress', $admin_options['codepress']));
  110. $table->construct_row();
  111. // If 2FA is enabled we need to display a link to the recovery codes page
  112. if(!empty($admin_options['authsecret']))
  113. {
  114. $lang->use_2fa_desc .= "<br />".$lang->recovery_codes_desc." ".$lang->recovery_codes_warning;
  115. }
  116. $table->construct_cell("<strong>{$lang->my2fa}</strong><br /><small>{$lang->use_2fa_desc}</small><br /><br />".$form->generate_on_off_radio('2fa', (int)!empty($admin_options['authsecret'])));
  117. $table->construct_row();
  118. if(!empty($admin_options['authsecret']))
  119. {
  120. $qr = $auth->getQRCodeGoogleUrl($mybb->user['username']."@".str_replace(" ", "", $mybb->settings['bbname']), $admin_options['authsecret']);
  121. $table->construct_cell("<strong>{$lang->my2fa_qr}</strong><br /><img src=\"{$qr}\"");
  122. $table->construct_row();
  123. }
  124. $table->output($lang->preferences);
  125. $table->construct_header($lang->notes_not_shared);
  126. $table->construct_cell($form->generate_text_area("notes", $admin_options['notes'], array('style' => 'width: 99%; height: 300px;')));
  127. $table->construct_row();
  128. $table->output($lang->personal_notes);
  129. $buttons[] = $form->generate_submit_button($lang->save_notes_and_prefs);
  130. $form->output_submit_wrapper($buttons);
  131. $form->end();
  132. $page->output_footer();
  133. }
  134. /**
  135. * Generate 10 random recovery codes, each with a length of 6 and without duplicates
  136. *
  137. * @return array
  138. */
  139. function generate_recovery_codes()
  140. {
  141. $t = array();
  142. while(count($t) < 10)
  143. {
  144. $g = random_str(6);
  145. if(!in_array($g, $t))
  146. {
  147. $t[] = $g;
  148. }
  149. }
  150. return $t;
  151. }