Added Validation library, updated Auth component
Showing
2 changed files
with
71 additions
and
26 deletions
| ... | @@ -33,7 +33,7 @@ class Auth { | ... | @@ -33,7 +33,7 @@ class Auth { |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | $auth = _signon(Array( | 35 | $auth = _signon(Array( |
| 36 | 'user_login' => $username | 36 | 'user_login' => esc_sql($username) |
| 37 | , 'user_password' => esc_sql($password) | 37 | , 'user_password' => esc_sql($password) |
| 38 | , 'remember' => $remember | 38 | , 'remember' => $remember |
| 39 | )); | 39 | )); |
| ... | @@ -63,9 +63,23 @@ class Auth { | ... | @@ -63,9 +63,23 @@ class Auth { |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | public static function register($user_data = Array(), $registration_method) { | 65 | public static function register($user_data = Array(), $registration_method) { |
| 66 | require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); | ||
| 67 | |||
| 68 | $valid = new Auth_Validation($user_data); | ||
| 69 | if (count($valid->errors) > 0) { | ||
| 70 | throw new BadMethodCallException(implode("\n", $valid->errors)); | ||
| 71 | } | ||
| 72 | |||
| 73 | array_filter($user_data, 'esc_sql'); | ||
| 74 | $id = (int)_insert_user($user_data); | ||
| 75 | |||
| 76 | global $wpdb; | ||
| 77 | $wpdb->query("UPDATE `{$wpdb->users}` SET `user_status` = 1 WHERE `ID` = {$id}"); | ||
| 78 | |||
| 79 | return $id; | ||
| 66 | } | 80 | } |
| 67 | 81 | ||
| 68 | public static function activate() { | 82 | public static function activate($username, $activation_key) { |
| 69 | do_action(self::ACTION_ACTIVATE, $user_id); | 83 | do_action(self::ACTION_ACTIVATE, $user_id); |
| 70 | } | 84 | } |
| 71 | 85 | ||
| ... | @@ -74,63 +88,49 @@ class Auth { | ... | @@ -74,63 +88,49 @@ class Auth { |
| 74 | } | 88 | } |
| 75 | } | 89 | } |
| 76 | 90 | ||
| 77 | class Auth_Validation { | 91 | class Auth_Validation extends Validation { |
| 78 | public static $errors = Array(); | ||
| 79 | |||
| 80 | /** | 92 | /** |
| 81 | * @rule Not blank | 93 | * @rule Not blank |
| 82 | * @rule Valid WordPress username | 94 | * @rule Valid WordPress username |
| 83 | * @returns Boolean | 95 | * @returns Boolean |
| 84 | */ | 96 | */ |
| 85 | public static function username($val) { | 97 | protected function username($val) { |
| 86 | if (empty($val)) { | 98 | if (empty($val)) { |
| 87 | self::$errors[] = 'Username is blank'; | 99 | throw new Exception('Username is blank'); |
| 88 | return false; | ||
| 89 | } | 100 | } |
| 90 | 101 | ||
| 91 | require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); | 102 | require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); |
| 92 | if (!validate_username($val)) { | 103 | if (!validate_username($val)) { |
| 93 | self::$errors[] = 'Username must be at least 4 characters, letters and numbers only'; | 104 | throw new Exception('Username must be at least 4 characters, letters and numbers only'); |
| 94 | return false; | ||
| 95 | } | 105 | } |
| 96 | 106 | ||
| 97 | if (username_exists($_POST['reg_username'])) { | 107 | if (username_exists($_POST['reg_username'])) { |
| 98 | self::$errors[] = 'Username already exists'; | 108 | throw new Exception('Username already exists'); |
| 99 | return false; | ||
| 100 | } | 109 | } |
| 101 | |||
| 102 | return true; | ||
| 103 | } | 110 | } |
| 104 | 111 | ||
| 105 | /** | 112 | /** |
| 106 | * @rule Not blank | 113 | * @rule Not blank |
| 107 | * @returns Boolean | 114 | * @returns Boolean |
| 108 | */ | 115 | */ |
| 109 | public static function password($val) { | 116 | protected function password($val) { |
| 110 | if (empty($val)) { | 117 | if (empty($val)) { |
| 111 | self::$errors[] = 'Password can not be blank'; | 118 | throw new Exception('Password can not be blank'); |
| 112 | return false; | ||
| 113 | } | 119 | } |
| 114 | |||
| 115 | return true; | ||
| 116 | } | 120 | } |
| 117 | 121 | ||
| 118 | /** | 122 | /** |
| 119 | * @rule Valid email address (*@*.*) | 123 | * @rule Valid email address (*@*.*) |
| 120 | * @returns Boolean | 124 | * @returns Boolean |
| 121 | */ | 125 | */ |
| 122 | public static function email($val) { | 126 | protected function email($val) { |
| 123 | if (!(boolean)filter_var($val, FILTER_VALIDATE_EMAIL)) { | 127 | if (!(boolean)filter_var($val, FILTER_VALIDATE_EMAIL)) { |
| 124 | self::$errors[] = 'Invalid email address'; | 128 | throw new Exception('Invalid email address'); |
| 125 | return false; | ||
| 126 | } | 129 | } |
| 127 | 130 | ||
| 128 | if (false !== email_exists($val)) { | 131 | if (false !== email_exists($val)) { |
| 129 | self::$errors[] = 'Email address already registered'; | 132 | throw new Exception('Email address already registered'); |
| 130 | return false; | ||
| 131 | } | 133 | } |
| 132 | |||
| 133 | return true; | ||
| 134 | } | 134 | } |
| 135 | } | 135 | } |
| 136 | ?> | 136 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
lib/Validation.php
0 → 100644
| 1 | <?php | ||
| 2 | abstract class Validation { | ||
| 3 | /** | ||
| 4 | * Associative array of valid fields | ||
| 5 | * @type Array | ||
| 6 | * @public | ||
| 7 | * @read-only | ||
| 8 | */ | ||
| 9 | private $valid = Array(); | ||
| 10 | |||
| 11 | /** | ||
| 12 | * Associative array if invalid fields | ||
| 13 | * @type Array | ||
| 14 | * @public | ||
| 15 | * @read-only | ||
| 16 | */ | ||
| 17 | private $errors = Array(); | ||
| 18 | |||
| 19 | /** | ||
| 20 | * @param {Array} $data Associative array of data to validate | ||
| 21 | */ | ||
| 22 | final public function __construct(Array $data) { | ||
| 23 | foreach ($data as $key => $val) { | ||
| 24 | if (method_exists($this, $key)) { | ||
| 25 | try { | ||
| 26 | call_user_func(Array($this, $key), $val); | ||
| 27 | $this->valid[$key] = $val; | ||
| 28 | } catch (Exception $e) { | ||
| 29 | $this->errors[$key] = $e->getMessage(); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @private | ||
| 37 | */ | ||
| 38 | final public function __get($key) { | ||
| 39 | $private = $key; | ||
| 40 | if (isset($this->$private)) { | ||
| 41 | return $this->$private; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | ?> |
-
Please register or sign in to post a comment