function register()
    {
        if(ALLOW_REGISTRATION == false)
            die('Registration has bean disabled on this node');

        if(!isset($_POST['Submit']))
        {
            $form_vals = make_reg_vals_array('', '', '', '');

        // display register form
            $view = instance_view('register');
            $view = $view->parse_to_variable(array(
                'form_vals' => $form_vals));

            $this->set_template_paramiters(
                array('main_content' => $view,
                      'sidebar'      => ''));
        }
        else
        {
        // reed the form
            $form_vals = array(
                'errs'   => array(),
                'name'   => $_POST['name'],
                'email'  => $_POST['email'], 
                'pass'   => $_POST['pass'],
                'pass_v' => $_POST['pass_v']);

        // Instance users model
            $usr = instance_model('users');
            $test_exists = array();

        // Validate user name
            try
            {
                validate_username($form_vals['name']);
                $test_exists = $usr->get_user_by_name($form_vals['name']);

                if($test_exists != array())
                {
                    new_flash('User name is already tacken on this node', 1);
                    $form_vals['name'] = '';
                }
            }
            catch(exception $e)
            {
                if(strlen($form_vals['name']) < 3)
                {
                    new_flash('User name too short, min 3 charicters', 1);
                    $form_vals['name'] = '';
                }

                else if(strlen($form_vals['name']) > 30)
                {
                    new_flash('User name too long, max 30 charicters', 1);
                    $form_vals['name'] = '';
                }

                else if(!preg_match('/^[a-zA-Z0-9_]+$/', $form_vals['name']))
                {
                    new_flash('User names must contain only alphanumeric charicters and the underscore', 1);
                    $form_vals['name'] = '';
                }
            }

        // Validate email
            if(!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+'
                .'(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $form_vals['email']))
            {
                new_flash('Email address is invalid', 1);
                $form_vals['email'] = "";
            }

        // Validate passwords
            if(mb_strlen($form_vals['pass'], 'utf8') < 6)
                new_flash('Password too short, min 6 charicters', 1);

            else if(sha1($form_vals['pass']) != sha1($form_vals['pass_v']))
                new_flash('Passwords do not match', 1);

            if(count(get_errors()) == 0)
            {
            // Everything was valid, save, login and redirect
                $usr->new_user($form_vals['name'], $form_vals['email'], $form_vals['pass']);

                $new_id = $usr->get_user_by_name($form_vals['name']);

                log_in_user($new_id[0]['User_name'], $new_id[0]['ID']);
            }

        // else re-display the register form and show errors
            else
            {
                $view = instance_view("register");
                $view = $view->parse_to_variable(array(
                    'form_vals' => $form_vals));

                $this->set_template_paramiters(
                    array('main_content' => $view,
                          'sidebar'      => ''));
            }
        }
    }
    function test_log_in_user()
    {
        try
        {
            log_in_user('fred', 1);
            $this->fail();
        }
        catch(exception $e)
        {
            $this->assertEquals(preg_match('/messages/', $e->getMessage()), 1);
        }

        $this->assertEquals($_SESSION['active_user']['name'], 'fred');
        $this->assertEquals($_SESSION['active_user']['id'],   1);
    }
Ejemplo n.º 3
0
<?php

// Returns an error code.
require_once "api.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $connection = get_connection();
    check_connection($connection);
    $username = $connection->real_escape_string($_POST["username"]);
    $password = $connection->real_escape_string($_POST["password"]);
    echo log_in_user($connection, $username, $password);
    $connection->close();
} else {
    goto_page("");
}