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

/admin/lib/lib_auth.php

http://pixie-cms.googlecode.com/
PHP | 162 lines | 123 code | 0 blank | 39 comment | 41 complexity | d1bc91172be504bc1fc3961f483c1d21 MD5 | raw file
  1. <?php
  2. if (!defined('DIRECT_ACCESS')) {
  3. header('Location: ../../');
  4. exit();
  5. }
  6. /**
  7. * Pixie: The Small, Simple, Site Maker.
  8. *
  9. * Licence: GNU General Public License v3
  10. * Copyright (C) 2010, Scott Evans
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see http://www.gnu.org/licenses/
  24. *
  25. * Title: lib_auth
  26. *
  27. * @package Pixie
  28. * @copyright 2008-2010 Scott Evans
  29. * @author Scott Evans
  30. * @author Sam Collett
  31. * @author Tony White
  32. * @author Isa Worcs
  33. * @link http://www.getpixie.co.uk
  34. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3
  35. *
  36. */
  37. if ((isset($login_submit)) && ($login_submit)) {
  38. if (!isset($username)) {
  39. $username = NULL;
  40. }
  41. if (!isset($password)) {
  42. $password = NULL;
  43. }
  44. if (!isset($remember)) {
  45. $remember = NULL;
  46. }
  47. $log_in = auth_login($username, $password, $remember);
  48. if (!$log_in) {
  49. $s = 'myaccount';
  50. logme($lang['ok_login'], 'no', 'user');
  51. } else {
  52. $s = 'login';
  53. $message = $log_in;
  54. logme($lang['failed_login'], 'yes', 'error');
  55. }
  56. } else if ((isset($s)) && ($s == 'logout')) {
  57. setcookie('pixie_login', ' ', time() - 3600, '/');
  58. $s = 'login';
  59. if ((isset($tool)) && ($tool == 'home')) {
  60. header('Location: ../');
  61. exit();
  62. }
  63. } else {
  64. $log_in = auth_check();
  65. if (isset($GLOBALS['pixie_user'])) {
  66. if ($GLOBALS['pixie_user']) {
  67. if ((isset($s)) && ($s)) {
  68. /* Then use $s */
  69. } else {
  70. $s = 'myaccount';
  71. }
  72. } else {
  73. /*if ($s == 'help') {
  74. $s = 'help';
  75. } else {*/
  76. $s = 'login';
  77. /*}*/
  78. $message = $log_in;
  79. }
  80. }
  81. }
  82. // -------------------------------------------------------------
  83. function auth_login($username, $password, $remember) {
  84. global $lang;
  85. global $timezone;
  86. $username = sterilise_txt($username, TRUE);
  87. $password = sterilise_txt($password, TRUE);
  88. $remember = sterilise_txt($remember, TRUE);
  89. $howmany = count(safe_rows('*', 'pixie_log', "log_message = '" . $lang['failed_login'] . "' and user_ip = '" . $_SERVER["REMOTE_ADDR"] . "' and log_time < utc_timestamp() and log_time > DATE_ADD(utc_timestamp(), INTERVAL -1 DAY)"));
  90. sleep(1); // should halt dictionary attacks
  91. // no more logins than 3 in 24 hours
  92. if ($howmany > 3) {
  93. $message = $lang['login_exceeded'];
  94. logme($lang['logins_exceeded'], 'yes', 'error');
  95. return $message;
  96. } else {
  97. if (isset($username) && isset($password)) {
  98. $r = safe_field('user_name', 'pixie_users', "user_name = '$username'and
  99. pass = password(lower('" . doSlash($password) . "')) and privs >= 0");
  100. if ($r) {
  101. $user_hits = safe_field('user_hits', 'pixie_users', "user_name='$username'");
  102. safe_update('pixie_users', "last_access = utc_timestamp()", "user_name = '$username'");
  103. safe_update('pixie_users', "user_hits = $user_hits + 1", "user_name = '$username'");
  104. $nonce = safe_field('nonce', 'pixie_users', "user_name='$username'");
  105. if ((isset($remember)) && ($remember)) { // persistent cookie required
  106. setcookie('pixie_login', $username . ',' . md5($username . $nonce), time() + 3600 * 24 * 365, '/');
  107. } else { // session-only cookie required
  108. setcookie('pixie_login', $username . ',' . md5($username . $nonce), 0, '/');
  109. }
  110. $privs = safe_field('privs', 'pixie_users', "user_name='$username'"); // login is good, create user
  111. $realname = safe_field('realname', 'pixie_users', "user_name='$username'");
  112. $nonce = safe_field('nonce', 'pixie_users', "user_name='$username'");
  113. if (isset($realname)) {
  114. $GLOBALS['pixie_real_name'] = $realname;
  115. }
  116. if (isset($privs)) {
  117. $GLOBALS['pixie_user_privs'] = $privs;
  118. }
  119. $GLOBALS['pixie_user'] = $username;
  120. $GLOBALS['nonce'] = $nonce;
  121. return '';
  122. } else { // login failed
  123. $GLOBALS['pixie_user'] = '';
  124. $message = $lang['login_incorrect'];
  125. return $message;
  126. }
  127. } else {
  128. $GLOBALS['pixie_user'] = '';
  129. $message = $lang['login_missing'];
  130. return $message;
  131. }
  132. }
  133. }
  134. // -------------------------------------------------------------
  135. function auth_check() {
  136. global $lang;
  137. if (isset($_COOKIE['pixie_login'])) {
  138. list($username, $cookie_hash) = explode(',', $_COOKIE['pixie_login']);
  139. $nonce = safe_field('nonce', 'pixie_users', "user_name='$username'");
  140. if (md5($username . $nonce) == $cookie_hash) { // check nonce
  141. $privs = safe_field('privs', 'pixie_users', "user_name='$username'"); // login is good, create user
  142. $realname = safe_field('realname', 'pixie_users', "user_name='$username'");
  143. if (isset($realname)) {
  144. $GLOBALS['pixie_real_name'] = $realname;
  145. }
  146. if (isset($privs)) {
  147. $GLOBALS['pixie_user_privs'] = $privs;
  148. }
  149. $GLOBALS['pixie_user'] = $username;
  150. return '';
  151. } else { // something's wrong
  152. $GLOBALS['pixie_user'] = '';
  153. setcookie('pixie_login', '', time() - 3600);
  154. $message = $lang['bad_cookie'];
  155. return $message;
  156. }
  157. } else {
  158. $GLOBALS['pixie_user'] = '';
  159. setcookie('pixie_login', '', time() - 3600);
  160. }
  161. }
  162. ?>