Auth.php 3.93 KB
<?php

namespace Tz\WordPress\Tools\Auth;

use Tz\WordPress\Tools;
use Tz\Common;
use Exception, LogicException, InvalidArgumentException, BadMethodCallException;

const REG_METH_AUTO_REG    = 1;
const REG_METH_VALID_EMAIL = 2;

const FORGOT_METH_VALID_EMAIL = 1;
const FORGOT_METH_RAND_PASS   = 2;

const ACTION_ACTIVATE = 'activate_account';

const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`)

    call_user_func(function() {
        Vars::$options = new Tools\WP_Option(OPTION_NAME);

        if (is_admin()) {
            require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php');
        }

        if (is_array(Vars::$options['third_party'])) {
        foreach (Vars::$options['third_party'] as $tp => $on) {
            if ($on) {
                require_once(__DIR__ . DIRECTORY_SEPARATOR . $tp . DIRECTORY_SEPARATOR . $tp . '.php');
            }
        }
        }
    });

/**
 * Attempts to login the user
 * @param {String} $username
 * @param {String} $password
 * @param {Boolean} $remember
 * @returns WP_User instance
 * @throws LogicException If headers have already been passed
 * @throws InvalidArgumentException If the authentication is invalid
 */
function login($username, $password, $remember = true) {
    if (headers_sent()) {
        throw new LogicException('Unable to login because headers have been sent');
    }

    $auth = _signon(Array(
        'user_login'    => esc_sql($username)
      , 'user_password' => esc_sql($password)
      , 'remember'      => $remember
    ));

    if (get_class($auth) == 'WP_User') {
        return $auth;
    }

    throw new InvalidArgumentException('Invalid username/password');
        //$auth->get_error_message()); this would be nice except it links to a wp-page
}

/**
 * Attempts to log the user out
 * @returns Boolean
 * @throws LogicException If HTTP headers have already been sent
 */
function logout() {
    if (headers_sent()) {
        throw new LogicException('Unable to logout because headers have been sent');
    }

    _logout();

    return true;
}

function register($user_data = Array(), $registration_method) {
    require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');

    $valid = new Validation($user_data);
    if (count($valid->errors) > 0) {
        throw new BadMethodCallException(implode("\n", $valid->errors));
    }

    array_filter($user_data, 'esc_sql');
    $id = (int)_insert_user($user_data);

    global $wpdb;
    $wpdb->query("UPDATE `{$wpdb->users}` SET `user_status` = 1 WHERE `ID` = {$id}");

    return $id;
}

function activate($username, $activation_key) {
    do_action(ACTION_ACTIVATE, $user_id);
}

function forgot_password($username, $forgot_method) {
    
}

class Vars {
    public static $options;
}

class Validation extends Common\Validation {
    /**
     * @rule Not blank
     * @rule Valid WordPress username
     * @returns Boolean
     */
    protected function username($val) {
        if (empty($val)) {
            throw new Exception('Username is blank');
        }

        require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');
        if (!validate_username($val)) {
            throw new Exception('Username must be at least 4 characters, letters and numbers only');
        }

        if (username_exists($_POST['reg_username'])) {
            throw new Exception('Username already exists');
        }
    }

    /**
     * @rule Not blank
     * @returns Boolean 
     */
    protected function password($val) {
        if (empty($val)) {
            throw new Exception('Password can not be blank');
        }
    }

    /**
     * @rule Valid email address (*@*.*)
     * @returns Boolean
     */
    protected function email($val) {
        if (!(boolean)filter_var($val, FILTER_VALIDATE_EMAIL)) {
            throw new Exception('Invalid email address');
        }

        if (false !== email_exists($val)) {
            throw new Exception('Email address already registered');
        }
    }
}
?>