www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] I built a registration class and i need help wiring up the front end (api layer)

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Location
    California
    Posts
    383

    resolved [RESOLVED] I built a registration class and i need help wiring up the front end (api layer)

    Using oracle server 11g.

    My front end is only 4 input boxes. I have some javascript validation as well but it's working fine so i won't post it, mainly having problems with how my classes interact with the form elements.

    HTML Code:
                <form id='register' action='register.php' onsubmit="return validateForm()" method='post' accept-charset='UTF-8'>
    <fieldset>
    <legend><br/>Create An Account</legend><br/>
    <input type='hidden' name='submitted' id='submitted' value='1'/>
    <label for='username' >Username*: </label>
    <input type='text' name='username' id='username' maxlength="50" /><br/><br/>
    <label for='email' >Email Address*:</label>
    <input type='text' name='email' id='email' maxlength="50" /><br/><br/>
    <label for="password">Password*:</label>  
    <input type="password" name="password" placeholder="password" required><br/><br/>
    <label for="password">Confirm Password*:</label>  
    <input type="password" name="password" placeholder="password" required><br/><br/>
    <label for='cpassword' >&zwnj;</label>
    <input type="hidden" name="fsubmitted" value="TRUE"><input type='submit' name='Submit' value='Register' />
    </fieldset>
    </form>
    PHP Code:

    public function create($password) {
            global 
    $db;

            if (!
    $this->get_email() || !$this->get_username()) {
                return 
    false;
            }

            
    $vars = array();
            
    $vars[] = array(':email'$this->get_email());
            
    $vars[] = array(':username'$this->get_username());
            
    $vars[] = array(':password'$password);

            
    $id $db->get_function_as_proc('custom.japi_shopper_identity.create_user(:email, :username,  :password)'$vars);
            
    $this->set_id($id);

            
    // If it failed, it'll puke on the procedure. If we've come this far, we
            // know it worked.
            
    return true;
        }

    public function 
    request_activation() {
            global 
    $db;

            
    $vars = array();
            
    $vars[] = array(':i_shopper_id'$this->get_id());

            
    // Returns a temp token
            
    $temp_token $db->get_function_as_proc('custom.japi_shopper_identity.activate_user_request(:i_shopper_id)'$vars);

            if (
    $temp_token == null) {
                return 
    false;
            } else {
                
    $this->send_activation_email();
                return 
    $temp_token;
            }
        }

    public function 
    set_email($email) {
            return 
    $this->email $email;
        }

     public function 
    set_username($username) {
            return 
    $this->user_name $username;
        } 
    When I hit the register button what code should i have in action="register.php"?

    Should i be able to keep all my code on one page?

    And just instantiate the class Shopper?

    $shopper = new Shopper();
    $shopper->set_email($new_username.'@example.com');
    $shopper->set_username($new_username);
    $shopper->create('password');
    $token = $shopper->request_activation();

    Also, I should aware of empty strings??

    and than hopefully the request_activation function will send them the e-mail for them to click the activate link?? Any help would be greatly appreciated. Thanks in advance.
    Last edited by Supplement; 11-12-2013 at 01:09 PM.

  2. #2
    Join Date
    Jun 2011
    Location
    California
    Posts
    383

    Red face

    Here's the full class if it will help anybody help me.

    PHP Code:
    // Most objects in this framework are populated by calling the constructor, but
    // this one has a variety of entry points. They don't do any sanity checking
    // with eachother, so you can have $user->create and $user->register refer to
    // completely different rows.
    class Shopper extends Base {

        protected 
    $shopper_id;
        protected 
    $email;
        protected 
    $user_name;
        protected 
    $temp_token;
        protected 
    $sign_in_token;

        protected 
    $UserShoppingList;
     
        function 
    __construct($email null) {
            
    // For testing use only. Declined to wrap in this_is_dev because I
            // foresee using it somewhere in the code, pushing live, and being 
    //        parent::__construct('jfw_shoppers', array('SHOPPER_ID' => $shopper_id));

            // Allow them to pass an e-mail address or the token
            
    if (strpos($email'@') === false) {
                
    $this->sign_in_token $email;
            } else {
                
    $this->email $email;
            }
        }

        
    // todo: need a new function to do the actual activation.
        
    public function activate($temp_token) {
            global 
    $db;

            
    $this->set_temp_token($temp_token);

            
    $vars = array();
            
    $vars[] = array(':i_temp_token'$this->get_temp_token());

            
    // Returns a Y or N
            
    return $db->get_function_as_proc('custom.japi_shopper_identity.Activate_User(:i_temp_token)'$vars) == 'Y';
        }

        public function 
    create($password) {
            global 
    $db;

            if (!
    $this->get_email() || !$this->get_username()) {
                return 
    false;
            }

            
    $vars = array();
            
    $vars[] = array(':email'$this->get_email());
            
    $vars[] = array(':username'$this->get_username());
            
    $vars[] = array(':password'$password);

            
    $id $db->get_function_as_proc('custom.japi_shopper_identity.create_user(:email, :username,  :password)'$vars);
            
    $this->set_id($id);

            
    // If it failed, it'll puke on the procedure. If we've come this far, we
            // know it worked.
            
    return true;
        }

        public function 
    get_email() {
            return 
    $this->email;
        }

        private function 
    get_id() {
            if (isset(
    $this->shopper_id)) {
                return 
    $this->shopper_id;

            
    // If this object has an e-mail address or the user sent one
            
    } else if ($this->get_email())  {
                global 
    $db;

                
    $vars = array();
                
    $vars[] = array(':i_email_id'$this->get_email());

                
    // FUNCTION get_id_by_email(i_email_id IN jfw_shoppers.email%TYPE)
                
    $id array_pop(array_pop($db->get_function('custom.japi_shopper_identity.get_id_by_email(:i_email_id)'$vars)));

                
    $this->set_id($id);
                
    $this->shopper_id $id;
                return 
    $this->shopper_id;

            
    // Can also get from token
            
    } else if ($this->get_sign_in_token())  {
                
    // todo: call get_id_by_token
                
    return false;
            }
        }


        
    // todo: test
        
    public function get_lists($clobber false) {
            global 
    $pd;
    //        $pd->print_object($this, 'User - has token?');
    //        $pd->print_object($this->get_sign_in_token(), 'Token');

            
    if ($this->UserShoppingList != null && !$clobber) {
                return 
    $this->UserShoppingList;

            } else if (
    $this->get_sign_in_token()) {
                global 
    $db;
                
    $pd->print_object($this'User - has token?');
                
    $pd->print_object(strtolower($this->get_sign_in_token()), 'token?');

                
    $vars = array();
                
    $vars[] = array(':i_sign_in_token'strtolower($this->get_sign_in_token()));

                
    $pd->print_object($this->get_sign_in_token(), 'About to seek lists using token');
                
    $rows $db->get_function('custom.japi_shopper_identity.get_lists_for_shopper(:i_sign_in_token)'$vars);
                
    $pd->print_object($rows'Rows returned by get_lists using token '.$this->get_sign_in_token());

                
    // Turn the rows into objects
                
    $this->UserShoppingList array_to_objects($rows'UserShoppingList');

                return 
    $this->UserShoppingList;
            } else {
                return 
    false;
            }
        }

        public function 
    get_sign_in_token() {
            if (
    $this->sign_in_token != null) {
                return 
    $this->sign_in_token;
            } else {
                return 
    false;
            }
        }

        public function 
    get_temp_token() {
            if (
    $this->temp_token != null) {
                return 
    $this->temp_token;
            } else {
                return 
    false;
            }
        }

        public function 
    get_username() {
            return 
    $this->user_name;
        }

        public function 
    json($obj null$return_json false) {
            if (
    $obj == null) {
                
    $obj $this;
            }
            return 
    parent::json($obj$return_json);
        }

        
    // Most objects in this framework are populated by calling the constructor,
        // but the only way to populate this one is to call this function with good 
        // credentials.
        
    public function login($password) {
            global 
    $db;

            if (!
    $this->get_email()) {
                return 
    false;
            }

            
    // Log them in now that we know who they are. 
            
    $vars = array();
            
    $vars[] = array(':i_email_id'$this->get_email());
            
    $vars[] = array(':i_password'$password);

            
    // This also exists, but is not yet in use:
            // $token = $db->get_function_as_proc('custom.japi_shopper_identity.login_by_username(:i_username, :i_password)', $vars);
            
    $token $db->get_function_as_proc('custom.japi_shopper_identity.Login_by_Email(:i_email_id, :i_password)'$vars);
            
    // todo: what if it's bad credentials?

            
    if ($token == null) {
                return 
    false;

            } else {
                
    $this->set_sign_in_token($token);
                return 
    $this->get_sign_in_token();
            }
        }

        public function 
    password_reset($tmp_token$password) {
            global 
    $db;

            if (
    strlen($password) < 8) {
                return 
    false;
            }

            
    $vars = array();
            
    $vars[] = array(':temp_token'$tmp_token);
            
    $vars[] = array(':new_password'$password);

            return 
    $db->get_function_as_proc('custom.japi_shopper_identity.password_reset(:temp_token, :new_password)'$vars) == 'Y';
        }

        public function 
    request_activation() {
            global 
    $db;

            
    $vars = array();
            
    $vars[] = array(':i_shopper_id'$this->get_id());

            
    // Returns a temp token
            
    $temp_token $db->get_function_as_proc('custom.japi_shopper_identity.activate_user_request(:i_shopper_id)'$vars);

            if (
    $temp_token == null) {
                return 
    false;
            } else {
                
    $this->send_activation_email();
                return 
    $temp_token;
            }
        }

        public function 
    request_password_reset() {
            global 
    $db$pd;

            if (!
    $this->get_id()) {
                return 
    false;
            }

            
    $vars = array();
            
    $vars[] = array(':shopper_id'$this->get_id());

            
    $temp_token $db->get_function_as_proc('custom.japi_shopper_identity.password_reset_request(:shopper_id)'$vars);
            if (
    $temp_token == null) {
                return 
    false;
            } else {
                
    $this->set_temp_token($temp_token);
                
    $pd->print_object('About to send the e-mail');
                
    $this->send_password_email();
                
    $pd->print_object('Sent the email');
                return 
    $this->get_temp_token();
            }
        }

        
        private function 
    send_activation_email() {
            if (!
    $this->get_email() || !$this->get_temp_token())  {
                return 
    false;
            }

           
            
    $fancy '
    <div style="text-align: center;"><img src="logo.jpg" /></div>
    <h2>Welcome to com!</h2>
    <p>To complete your registration, <a href="todo: ">click here</a> or copy and paste the URL into your browser:</p>

    URL?token='
    .$this->get_temp_token().'

    Thanks!
    '
    ;
            
            
    $plain 'Welcome to com!

    To complete your registration, please activate your account by going to the URL below:

    URL?token='
    .$this->get_temp_token().'

    Thanks!
    '
    ;

            
    // todo: subject could probably be better
            
    return email_customer($this->get_email(), 'Welcome to com!'$fancy$plain);
        }

        
        private function 
    send_password_email() {
            global 
    $pd;
            
    $pd->print_object('In send_password_email');
            
    $pd->print_object($this->get_email(), 'E-mail');
            
    $pd->print_object($this->get_temp_token(), 'Token');

            if (!
    $this->get_email() || !$this->get_temp_token())  {
                return 
    false;
            }

            
    $pd->print_object($this->get_email(), 'Have all the data I need');

           
            
    $fancy '
    <div style="text-align: center;"><img src="logo.jpg" /></div>
    <h2>Welcome to com!</h2>
    <p>To reset your password, <a href="todo: ">click here</a> or copy and paste the URL into your browser:</p>

    <p>URL?token='
    .$this->get_temp_token().'</p>

    <p>Thanks!</p>
    '
    ;
            
    $plain 'Welcome to com!

    To reset your password by going to the URL below:

    URL?token='
    .$this->get_temp_token().'

    Thanks!
    '
    ;
            
    $pd->print_object('About to actually e-mail');

            
            return 
    email_customer($this->get_email(), "Reset your com password"$fancy$plain);
        }

        public function 
    set_email($email) {
            return 
    $this->email $email;
        }

        public function 
    set_id($email) {
            return 
    $this->shopper_id;
        }

        public function 
    set_sign_in_token($token) {
            return 
    $this->sign_in_token $token;
        }

        public function 
    set_temp_token($token) {
            return 
    $this->temp_token $token;
        }

        public function 
    set_username($username) {
            return 
    $this->user_name $username;
        }


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10462 seconds
  • Memory Usage 3,106KB
  • Queries Executed 13 (?)
More Information
Template Usage (31):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)bbcode_html
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (2)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./includes/functions_threadedmode.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • bbcode_parse_start
  • postbit_imicons
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates