PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/login.php

https://bitbucket.org/navigatecms/navigatecms
PHP | 367 lines | 300 code | 58 blank | 9 comment | 30 complexity | dc4a30fe040e40b89760247321955bc4 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-2.1, BSD-3-Clause, AGPL-3.0, Apache-2.0
  1. <?php
  2. require_once('cfg/globals.php');
  3. require_once('cfg/common.php');
  4. $navigate_url = !empty($_ENV['SCRIPT_URI'])? dirname($_ENV['SCRIPT_URI']) : dirname(nvweb_self_url());
  5. if(substr($navigate_url, -1)=='/') $navigate_url = substr($navigate_url, 0, -1);
  6. define('NAVIGATE_URL', $navigate_url);
  7. // create database connection
  8. $DB = new database();
  9. if(!$DB->connect())
  10. {
  11. die(APP_NAME.' # ERROR<br /> '.$DB->get_last_error());
  12. }
  13. if(!empty($_SESSION['APP_USER#'.APP_UNIQUE]))
  14. {
  15. session_write_close();
  16. header('location: '.NAVIGATE_MAIN);
  17. exit;
  18. }
  19. $user = new user();
  20. $website = new website(); // only needed for the users log
  21. if(!empty($_COOKIE['navigate-user']))
  22. {
  23. $nuid = $DB->query_single('id', 'nv_users', 'cookie_hash = '.protect($_COOKIE['navigate-user']));
  24. if(!empty($nuid))
  25. {
  26. $user->load($nuid);
  27. $_SESSION['APP_USER#'.APP_UNIQUE] = $nuid;
  28. session_write_close();
  29. header('location: '.NAVIGATE_MAIN);
  30. exit;
  31. }
  32. }
  33. if(!empty($_POST['login-username']) && !empty($_POST['login-password']))
  34. {
  35. $error = !$user->authenticate($_POST['login-username'], $_POST['login-password']);
  36. if(empty($error) && $user->blocked == '1')
  37. $error = true;
  38. if(!$error)
  39. {
  40. $_SESSION['APP_USER#'.APP_UNIQUE] = $user->id;
  41. if($_REQUEST['login-remember']=='1')
  42. $user->set_cookie();
  43. else
  44. $user->remove_cookie();
  45. $login_request_uri = $_SESSION["login_request_uri"];
  46. $website->load(); // load first website available (needed in the users log)
  47. users_log::action(0, $user->id, 'login', $user->username);
  48. $_SESSION["login_request_uri"] = '';
  49. setcookie('navigate-session-id', session_id(), time() + 60, '/'); // 60 seconds
  50. session_write_close();
  51. header('location: '.NAVIGATE_MAIN.'?'.$login_request_uri);
  52. exit;
  53. }
  54. }
  55. /* CHECK USER BROWSER LANGUAGE PREFERENCES */
  56. $language_default = 'en';
  57. $DB->query('SELECT code
  58. FROM nv_languages
  59. WHERE nv_dictionary != ""', 'array');
  60. $languages_available = $DB->result('code');
  61. $langs = array();
  62. if(!empty($_COOKIE['navigate-language']))
  63. {
  64. $language_default = $_COOKIE['navigate-language'];
  65. }
  66. else if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  67. {
  68. preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
  69. if (count($lang_parse[1]))
  70. {
  71. $langs = array_combine($lang_parse[1], $lang_parse[4]);
  72. foreach ($langs as $lang => $val)
  73. {
  74. if ($val === '') $langs[$lang] = 1;
  75. }
  76. arsort($langs, SORT_NUMERIC);
  77. }
  78. $found = false;
  79. foreach($langs as $language_browser => $val)
  80. {
  81. foreach($languages_available as $foo => $language_available)
  82. {
  83. if($language_available == $language_browser)
  84. {
  85. $language_default = $language_browser;
  86. $found = true;
  87. break;
  88. }
  89. }
  90. if($found) break;
  91. }
  92. }
  93. /* LOAD LANGUAGE */
  94. $lang = new language();
  95. $lang->load($language_default);
  96. // is a recover password request?
  97. if($_REQUEST['action']=='forgot-password')
  98. {
  99. $value = mb_strtolower(trim($_REQUEST['value']));
  100. // look for an existing username or e-mail in Navigate CMS users table
  101. $found_id = $DB->query_single(
  102. 'id',
  103. 'nv_users',
  104. ' username = '.protect($value).' OR
  105. email = '.protect($value)
  106. );
  107. if(!$found_id)
  108. echo 'not_found';
  109. else
  110. {
  111. $user->load($found_id);
  112. $sent = $user->forgot_password();
  113. if(!$sent)
  114. echo 'not_sent';
  115. else
  116. echo 'sent';
  117. }
  118. core_terminate();
  119. }
  120. $layout = new layout('navigate');
  121. echo $layout->doctype();
  122. echo $layout->head();
  123. $current_version = update::latest_installed();
  124. ?>
  125. <body>
  126. <div class="navigate-top"></div>
  127. <div id="navigate-status" class="ui-corner-all">
  128. <div>
  129. <div style="float: left;">
  130. </div>
  131. <div style="float: right;">
  132. <?php echo APP_NAME;?> v<?php echo $current_version->version;?>, &copy; <?php echo date('Y');?>
  133. </div>
  134. <div style=" clear: both; "></div>
  135. </div>
  136. </div>
  137. <div id="navigate-login" class="ui-corner-all" style=" border: solid 1px #ddd; top: 50%; margin-top: -150px; position: absolute; margin-left: -325px; left: 50%; padding: 4px; width: 700px; height: 350px; ">
  138. <form
  139. name="navigate-content-form"
  140. action="<?php echo $_SERVER['PHP_SELF'];?>"
  141. method="post"
  142. enctype="multipart/form-data"
  143. style=" margin-left: auto; margin-right: auto; margin-top: 50px; height: 350px; ">
  144. <div style=" float: left; margin-right: 55px; margin-left: 20px; ">
  145. <img src="img/navigate-logo-430x200.png" width="300" height="140" />
  146. <a href="http://www.navigatecms.com" style=" display: block; text-decoration: none; color: #2E476E; text-align: center; " target="_blank">www.navigatecms.com</a>
  147. </div>
  148. <div style=" float: left; width: 288px; ">
  149. <div class="navigate-form-row">
  150. <label style=" padding-top: 6px; margin-bottom: 6px; font-size: 15px; "><?php echo t(1, 'User');?></label>
  151. <br />
  152. <input type="text" value="" size="32" name="login-username" id="login-username" style=" width: 278px; font-size: 20px; " />
  153. </div>
  154. <div class="navigate-form-row">
  155. <label style=" padding-top: 6px; margin-bottom: 6px; font-size: 15px; "><?php echo t(2, 'Password');?></label>
  156. <br />
  157. <input type="password" value="" size="32" name="login-password" id="login-password" style=" width: 278px; font-size: 20px; " />
  158. </div>
  159. <div class="navigate-form-row">
  160. <input type="checkbox" name="login-remember" id="login-remember" value="1" />
  161. <label onclick="$('#login-remember').trigger('click');" style=" margin-left: 3px; margin-top: 2px; position: absolute; "><?php echo t(406, 'Remember me');?></label>
  162. </div>
  163. <div class="navigate-form-row" id="login-button" style=" margin-top: 48px; font-size: 15px; ">
  164. <button style=" background: none; border: none; color: transparent; display: block; float: left; "><?php echo t(3, 'Enter');?></button>
  165. <a href="#" style=" color: #2E476E; font-size: 10px; line-height: 30px; float: right; text-decoration: none;"><?php echo t(407, 'Forgot password?');?></a>
  166. </div>
  167. </div>
  168. <?php
  169. if(isset($error))
  170. {
  171. ?>
  172. <div class="navigate-form-row" style=" padding-top: 20px; text-align: center; display: none; ">
  173. <span class="error"><img src="img/icons/silk/decline.png" width="16" height="16" align="absmiddle" /> <?php echo t(4, 'Login incorrect.');?></span>
  174. </div>
  175. <?php
  176. }
  177. ?>
  178. </form>
  179. </div>
  180. <div id="navigate-lost-password-dialog" style=" display: none; ">
  181. <form action="?" method="post">
  182. <div class="navigate-form-row">
  183. <label style=" padding-top: 6px; margin-bottom: 6px; font-size: 11px; width: auto; ">
  184. <?php echo t(449, "Enter your Navigate CMS username or e-mail address");?>
  185. </label>
  186. <br />
  187. <input type="text" value="" size="32" name="forgot-password" id="forgot-password" style=" width: 96%; font-size: 17px; " />
  188. <br />
  189. <div id="forgot-password-problem" class="subcomment" style=" margin-left: 0px; color: #f33; ">&nbsp;</div>
  190. </div>
  191. <div class="navigate-form-row" style=" margin-top: 20px; ">
  192. <button style=" background: none; border: none; color: transparent; display: block; float: left; font-size: 12px; ">
  193. <?php echo t(190, 'Ok');?>
  194. </button>
  195. </div>
  196. </div>
  197. </div>
  198. </body>
  199. <script language="javascript" type="text/javascript">
  200. var NAVIGATE_APP = "<?php echo NAVIGATE_URL.'/'.NAVIGATE_MAIN;?>";
  201. $(window).on('load,resize', function()
  202. {
  203. $('#navigate-status').css({ 'width': $(document).width() - 18 });
  204. });
  205. $(document).ready(function()
  206. {
  207. $('button').removeAttr('style').css({'font-size': '14px', 'padding-bottom': '4px'}).hide().fadeIn('slow', function()
  208. {
  209. $('.error').parent().fadeIn('slow');
  210. });
  211. $('input[name="login-username"]').focus();
  212. $('#navigate-lost-password-dialog form').on('submit', function(e)
  213. {
  214. $('#forgot-password-problem').html('&nbsp;');
  215. e.stopPropagation();
  216. e.preventDefault();
  217. $.post(
  218. 'login.php',
  219. {
  220. 'action': 'forgot-password',
  221. 'value': $('#forgot-password').val()
  222. },
  223. function(data)
  224. {
  225. if(data=='sent')
  226. {
  227. //$('#navigate-lost-password-dialog').dialog('close');
  228. $('#navigate-lost-password-dialog').html('');
  229. $('#navigate-lost-password-dialog').append('<div style="text-align: center; margin: 16px; "><i class="fa fa-5x fa-envelope" style="color: #BBD6F5"></i><i style="position: absolute; margin-top: 28px; margin-left: -12px; color: #2E476E;" class="fa fa-2x fa-check"></i></div>');
  230. $('#navigate-lost-password-dialog').append('<div style="text-align: center; font-weight: bold; padding: 10px; "><?php echo t(454, 'An e-mail with a confirmation link has been sent to your e-mail account.', false, true); ?></div>');
  231. }
  232. else if(data=='not_found')
  233. {
  234. $('#forgot-password-problem').html("<?php echo t(453, "Couldn't find this username or e-mail address", false, true);?>");
  235. }
  236. else// if(data=='not_sent')
  237. {
  238. $('#forgot-password-problem').html("<?php echo t(452, "E-mail could not be sent; please contact the administrator", false, true);?>");
  239. }
  240. }
  241. );
  242. });
  243. // forgot password button
  244. $('#login-button a').on('click', function()
  245. {
  246. $('#navigate-lost-password-dialog').dialog({
  247. title: "<?php echo t(407, 'Forgot password?', false, true);?>",
  248. modal: true,
  249. width: 350,
  250. height: 220
  251. });
  252. });
  253. $.setCookie("navigate-tinymce-scroll", '{}');
  254. });
  255. </script>
  256. <?php
  257. // are we on a password change process?
  258. if($_REQUEST['action']=='password-reset')
  259. {
  260. $value = trim($_REQUEST['value']);
  261. // look for an existing username or e-mail in Navigate CMS users table
  262. $found_id = $DB->query_single(
  263. 'id',
  264. 'nv_users',
  265. 'activation_key = '.protect($value)
  266. );
  267. if(!empty($found_id))
  268. {
  269. $user->load($found_id);
  270. if(!empty($_REQUEST['login-password']))
  271. {
  272. $user->activation_key = '';
  273. $user->set_password(trim($_REQUEST['login-password']));
  274. $user->save();
  275. ?>
  276. <script language="javascript">
  277. $(document).ready(function()
  278. {
  279. $('form:first').append('<div class="navigate-form-row" style=" padding-top: 20px; text-align: center; display: none; "></div>');
  280. $('form:first').find('div:last').html('<span class="ok" style="color: #579A4D; font-weight: bold; "><img src="img/icons/silk/accept.png" width="16" height="16" align="absmiddle" /> <?php echo t(455, 'Your new password has been activated.');?></span>');
  281. $('form:first').find('div:last').fadeIn('slow');
  282. });
  283. </script>
  284. <?php
  285. }
  286. else
  287. {
  288. ?>
  289. <script language="javascript">
  290. $(document).ready(function()
  291. {
  292. $('#login-username').parent().remove();
  293. $('#login-button a').remove();
  294. $('#login-remember').parent().remove();
  295. $('#login-button').remove();
  296. $('#navigate-lost-password-dialog').remove();
  297. $('form').attr('action', $('form').attr('action') + '?action=password-reset&value=<?php echo $value;?>');
  298. $('form').append('<button id="login-button" style="margin-top: 20px; font-size: 14px; "><?php echo t(34, "Save");?></button>');
  299. });
  300. </script>
  301. <?php
  302. }
  303. }
  304. }
  305. ?>
  306. </html>
  307. <?php
  308. $DB->disconnect();
  309. ?>