305bda06 by Chris Boden

Added Auth library

1 parent b6849213
1 <?php
2 class Auth {
3 const REG_METH_AUTO_REG = 1;
4 const REG_METH_VALID_EMAIL = 2;
5
6 const FORGOT_METH_VALID_EMAIL = 1;
7 const FORGOT_METH_RAND_PASS = 2;
8
9 const ACTION_ACTIVATE = 'activate_account';
10
11 public static function make() {
12 static $made = false;
13 if (true === $made) {
14 throw new Exception('Auth has already been instantiated');
15 }
16 $made = true;
17
18 // if _GET activate self::activate();
19 }
20
21 /**
22 * Attempts to login the user
23 * @param {String} $username
24 * @param {String} $password
25 * @param {Boolean} $remember
26 * @returns WP_User instance
27 * @throws LogicException If headers have already been passed
28 * @throws InvalidArgumentException If the authentication is invalid
29 */
30 public static function login($username, $password, $remember = true) {
31 if (headers_sent()) {
32 throw new LogicException('Unable to login because headers have been sent');
33 }
34
35 $auth = _signon(Array(
36 'user_login' => $username
37 , 'user_password' => esc_sql($password)
38 , 'remember' => $remember
39 ));
40
41 $ref = new ReflectionObject($auth);
42 if ($ref->name == 'WP_User') {
43 return $auth;
44 }
45
46 throw new InvalidArgumentException('Invalid username/password');
47 //$auth->get_error_message()); this would be nice except it links to a wp-page
48 }
49
50 /**
51 * Attempts to log the user out
52 * @returns Boolean
53 * @throws LogicException If HTTP headers have already been sent
54 */
55 public static function logout() {
56 if (headers_sent()) {
57 throw new LogicException('Unable to logout because headers have been sent');
58 }
59
60 _logout();
61
62 return true;
63 }
64
65 public static function register($user_data = Array(), $registration_method) {
66 }
67
68 public static function activate() {
69 do_action(self::ACTION_ACTIVATE, $user_id);
70 }
71
72 public static function forgot_password($username, $forgot_method) {
73
74 }
75 }
76
77 class Auth_Validation {
78 public static $errors = Array();
79
80 /**
81 * @rule Not blank
82 * @rule Valid WordPress username
83 * @returns Boolean
84 */
85 public static function username($val) {
86 if (empty($val)) {
87 self::$errors[] = 'Username is blank';
88 return false;
89 }
90
91 require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');
92 if (!validate_username($val)) {
93 self::$errors[] = 'Username must be at least 4 characters, letters and numbers only';
94 return false;
95 }
96
97 if (username_exists($_POST['reg_username'])) {
98 self::$errors[] = 'Username already exists';
99 return false;
100 }
101
102 return true;
103 }
104
105 /**
106 * @rule Not blank
107 * @returns Boolean
108 */
109 public static function password($val) {
110 if (empty($val)) {
111 self::$errors[] = 'Password can not be blank';
112 return false;
113 }
114 self::$pass_check = $val;
115
116 return true;
117 }
118
119 /**
120 * @rule Valid email address (*@*.*)
121 * @returns Boolean
122 */
123 public static function email($val) {
124 if (!(boolean)filter_var($val, FILTER_VALIDATE_EMAIL)) {
125 self::$errors[] = 'Invalid email address';
126 return false;
127 }
128
129 if (false !== email_exists($val)) {
130 self::$errors[] = 'Email address already registered';
131 return false;
132 }
133
134 return true;
135 }
136 }
137 ?>
...\ No newline at end of file ...\ No newline at end of file