function ym_register_flow($flow_id, $pack_id = false, $widget = false)
{
    global $current_page, $next_page, $the_flow_id, $wpdb, $ym_res, $ym_sys;
    global $post_data, $pack_data;
    $html = $form_top = '';
    $payment_gateway_detected = false;
    if (!is_singular() && !$widget) {
        return __('A Register Flow Error Occurred (Type 0) Not on a Flow Page', 'ym');
    }
    if (!$flow_id) {
        return __('A Register Flow Error Occurred (Type 1) No Flow Selected', 'ym');
    }
    if (ym_post('flowcomplete')) {
        // complete
        $query = 'SELECT complete_text FROM ' . $wpdb->prefix . 'ym_register_flows WHERE flow_id = ' . $flow_id;
        $complete_text = $wpdb->get_var($query);
        if ($complete_text) {
            return '<p>' . $complete_text . '</p>';
        } else {
            return '<p>' . __('Registration/Upgrade is complete', 'ym') . '</p>';
        }
    }
    $flow_pages = 'SELECT flow_pages, complete_button FROM ' . $wpdb->prefix . 'ym_register_flows WHERE flow_id = ' . $flow_id;
    $flow_pages = $wpdb->get_row($flow_pages);
    if (!$flow_pages) {
        return __('A Register Flow Error Occurred (Type 2) Flow Not Found', 'ym');
    }
    $complete_button = $flow_pages->complete_button;
    $the_flow_id = $flow_id;
    // have a flow
    $flow_pages = unserialize($flow_pages->flow_pages);
    $last_page = ym_post('ym_register_flow_page', 0);
    $current_page = ym_post('ym_register_flow_next_page', 0);
    $flowcomplete = ym_post('flowcomplete', 0);
    if (!$current_page) {
        $copy = $flow_pages;
        $current_page = array_shift($copy);
    }
    $next_page = 0;
    while ($next_page == 0 && count($flow_pages)) {
        $page = array_shift($flow_pages);
        if ($page == $current_page) {
            $next_page = array_shift($flow_pages);
        }
    }
    //$permalink = get_permalink();
    $permalink = '';
    if (ym_superuser()) {
        echo '<div class="ym_message"><p class="ym_message_liner">' . __('Warning, entering this flow may change your WordPress role', 'ym') . '</p></div>';
    }
    echo '
<style type="text/css">
	label {
		display: block;
	}
</style>
';
    $form = '
<form action="' . $permalink . '" method="post" enctype="multipart/form-data" id="ym_register_flow_form">
';
    $html .= $form;
    $form_top .= $form;
    $custom_data = get_option('ym_custom_fields');
    $custom_data = $custom_data->entries;
    // required?
    $required_data = isset($_POST['required']) ? $_POST['required'] : array();
    $ok = true;
    $email = true;
    $useremail = true;
    $username = true;
    $coupon = true;
    $dupepassword = true;
    $dont_hidden = array('email_address', 'username', 'signed_request');
    // maintaint
    $post_data = array();
    foreach ($_POST as $field => $entry) {
        if ($field != 'ym_register_flow_page' && $field != 'ym_register_flow_next_page' && $field != 'required' && $field != 'flowcomplete') {
            if (isset($required_data[$field]) && $required_data[$field] == 1 && !$entry) {
                $ok = false;
            }
            if ($field == 'email_address' && !is_email($entry)) {
                $email = false;
                $entry = '';
            } else {
                if ($field == 'email_address') {
                    // verify unique
                    if (email_exists($entry)) {
                        $useremail = false;
                    }
                }
            }
            if ($field == 'username') {
                if (username_exists($entry)) {
                    $username = false;
                }
            }
            if ($field == 'coupon' && $entry) {
                $type = ym_post('coupon_type');
                if ($type == 'coupon_register') {
                    $type = array(0);
                } else {
                    if ($type == 'coupon_upgrade') {
                        $type = array(1);
                    } else {
                        if (!is_int($type)) {
                            // both
                            $type = array(0, 1);
                        }
                    }
                }
                $value = false;
                $coupon_type = '';
                foreach ($type as $t) {
                    $value = ym_validate_coupon($entry, $t);
                    if ($value) {
                        $coupon_type = $t;
                        // TODO: register coupon use
                        break;
                    }
                }
                if ($value) {
                    //valid
                    $post_data['coupon_value'] = $value;
                    $form = '<input type="hidden" name="coupon_value" value="' . $value . '" />';
                    $post_data['coupon_type'] = $coupon_type;
                    $form = '<input type="hidden" name="coupon_type" value="' . $coupon_type . '" />';
                    $coupon = true;
                } else {
                    // not valid
                    $coupon = false;
                }
            }
            //YM duplicate password check
            if ($field == 'ym_password') {
                if (ym_post('ym_password_check') || ym_post('ym_password_dupe')) {
                    $dupepassword = false;
                    if (ym_post('ym_password') == ym_post('ym_password_check')) {
                        $dupepassword = true;
                    }
                }
            }
            if (!isset($post_data[$field])) {
                $post_data[$field] = ym_post($field);
                //$entry;
                if (!in_array($field, $dont_hidden)) {
                    $form = '<input type="hidden" name="' . $field . '" value="' . $entry . '" />
';
                    $html .= $form;
                    $form_top .= $form;
                }
            }
        }
    }
    $call_login = 0;
    if ($_POST) {
        if (!$ok || !$email || !$useremail || !$username || !$coupon || !$dupepassword) {
            $next_page = $current_page;
            $current_page = $last_page;
            if (!$email) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_email_invalid . '</p></div>';
            }
            if (!$useremail) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_email_inuse . '</p></div>';
            }
            if (!$username) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_username_inuse . '</p></div>';
            }
            if (!$ok) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_required_fields . '</p></div>';
            }
            if (!$coupon) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_invalid_coupon . '</p></div>';
            }
            if (!$dupepassword) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_invalid_password . '</p></div>';
            }
            $ok = false;
        }
        global $current_user;
        get_currentuserinfo();
        $username = $password = $fb_widget_ok = false;
        // check registation
        if ($ok) {
            if (!$current_user->ID) {
                $email = isset($post_data['email_address']) ? $post_data['email_address'] : '';
                $username = isset($post_data['username']) ? $post_data['username'] : '';
                $password = isset($post_data['password']) ? $post_data['password'] : '';
                if ($email) {
                    // minimum for registeration
                    if (!$username) {
                        $username = $email;
                    }
                    if (username_exists($username)) {
                        // register failed
                        $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_username_inuse . '</p></div>';
                    } else {
                        // able to registers
                        $ym_user = new YourMember_User();
                        $user_id = $ym_user->create($email, false, true, $username, $password);
                        wp_set_current_user($user_id);
                        $call_login = 1;
                    }
                } else {
                    if (ym_post('signed_request')) {
                        $data = ym_facebook_uncode(ym_post('signed_request'));
                        if ($data) {
                            if ($data->registration) {
                                // register!
                                if (email_exists($data->registration->email) || username_exists($data->registration->email)) {
                                    $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_email_inuse . '</p></div>';
                                } else {
                                    $ym_user = new YourMember_User();
                                    $user_id = $ym_user->create($data->registration->email, false, true, $data->registration->email, $data->registration->password, array('first_name' => $data->registration->first_name, 'last_name' => $data->registration->last_name));
                                    wp_set_current_user($user_id);
                                    $call_login = 1;
                                    $fb_widget_ok = true;
                                }
                            } else {
                                $html .= '<div class="ym_message"><p class="ym_message_liner">' . __('Faecbook Registration Error (2)', 'ym') . '</p></div>';
                            }
                        } else {
                            $html .= '<div class="ym_message"><p class="ym_message_liner">' . __('Faecbook Registration Error (1)', 'ym') . '</p></div>';
                        }
                    }
                }
            } else {
                // update key user entries
                if (isset($post_data['username'])) {
                    if ($username = $post_data['username']) {
                        $query = 'UPDATE ' . $wpdb->users . ' SET user_login = \'' . $username . '\' WHERE ID = ' . $current_user->ID;
                        $wpdb->query($query);
                    }
                }
                if (isset($post_data['password'])) {
                    if ($password = $post_data['password']) {
                        $pw_hash = wp_hash_password($password);
                        $query = 'UPDATE ' . $wpdb->users . ' SET user_pass = \'' . $pw_hash . '\' WHERE ID = ' . $current_user->ID;
                        $wpdb->query($query);
                        $call_login = 1;
                    }
                }
            }
            // customs
            ym_update_custom_fields();
        }
    }
    $gateway_return = ym_request('gateway_return', false);
    if ($gateway_return) {
        // return from gateway into flow
        // all details dropped :-(
        $to_remove = array('gateway_return', 'item', 'ym_register_flow_page', 'ym_register_flow_next_page', 'user_id');
        $query = $_SERVER['QUERY_STRING'];
        foreach ($to_remove as $remove) {
            $query = preg_replace('/' . $remove . '\\=' . "([a-zA-Z0-9_]+)/", '', $query);
        }
        while (substr($query, -1, 1) == '&') {
            $query = substr($query, 0, -1);
        }
        $html = str_replace('<form action=""', '<form action="?' . $query . '"', $html);
        $user_id = ym_request('user_id', false);
        if ($user_id) {
            $call_login = 1;
        }
    }
    $ym_register_user_id = ym_request('ym_register_user_id', false);
    if ($ym_register_user_id) {
        $call_login = 1;
        $user_id = $ym_register_user_id;
    }
    if ($call_login) {
        // temp login
        wp_set_current_user($user_id);
        $html .= '<input type="hidden" name="ym_register_user_id" value="' . $user_id . '" />';
    }
    unset($username);
    unset($password);
    $form = '
	<input type="hidden" name="ym_register_flow_page" value="' . $current_page . '" />
	<input type="hidden" name="ym_register_flow_next_page" value="' . $next_page . '" />
	';
    $html .= $form;
    $form_top .= $form;
    // data maintain whats left
    foreach ($post_data as $key => $item) {
        if (!in_array($key, $dont_hidden)) {
            $form = '
	<input type="hidden" name="' . $key . '" value="' . $item . '" />
	';
            $html .= $form;
            $form_top .= $form;
        }
    }
    // load
    $page = 'SELECT page_fields, button_text FROM ' . $wpdb->prefix . 'ym_register_pages WHERE page_id = ' . $current_page;
    $page = $wpdb->get_row($page);
    if (!$page) {
        return __('A Register Flow Error Occurred (Type 3) Page Not Found', 'ym');
    }
    $page_data = $page->page_fields;
    $next_button = $page->button_text;
    $page_data = unserialize($page_data);
    foreach ($page_data as $item => $field) {
        foreach ($field as $i => $f) {
            $page_data[$item][$i] = stripslashes(urldecode($f));
        }
    }
    $block_logic = array();
    // parse pack data
    $pack_data = false;
    if (isset($post_data['pack_id'])) {
        // load from form
        $pack_id = $post_data['pack_id'];
        // pass thru....
    }
    if ($pack_id) {
        $pack_data = ym_get_pack_by_id($pack_id);
    } else {
        // no pack id :-(
        // default
        $pack_order = ym_get_packs();
        $pack_data = array_shift($pack_order);
    }
    $first_button = true;
    foreach ($page_data as $index => $field_data) {
        $display = true;
        if ($field_data['iflogic']) {
            // block has logic
            $display = false;
            // evaulate the block logic result
            // is it a then or a else?
            switch ($field_data['iflogic']) {
                case 'loggedin':
                    $match = $field_data['iflogic_quantity_loggedin'];
                    $logged_in = is_user_logged_in();
                    if ($logged_in && $match) {
                        // user is logged on and the match is for logged in
                        $this_logic = 'then';
                    } else {
                        if (!$logged_in && !$match) {
                            $this_logic = 'then';
                        } else {
                            $this_logic = 'else';
                        }
                    }
                    break;
                case 'buying':
                    $match = $field_data['iflogic_quantity_pack'];
                    if (isset($post_data['pack_id']) && $post_data['pack_id'] == $match) {
                        $this_logic = 'then';
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                case 'currentlyon':
                    $match = $field_data['iflogic_quantity_pack'];
                    if (is_user_logged_in()) {
                        global $ym_user;
                        $pack_id = $ym_user->pack_id ? $ym_user->pack_id : 0;
                        if ($pack_id == $match) {
                            $this_logic = 'then';
                        } else {
                            $this_logic = 'else';
                        }
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                case 'accounttype':
                    $match = $field_data['iflogic_quantity_pack'];
                    $match = strtolower($match);
                    if (is_user_logged_in()) {
                        global $ym_user;
                        $account_type = $ym_user->account_type ? $ym_user->account_type : '';
                        $account_type = strtolower($account_type);
                        if ($account_type == $match) {
                            $this_logic = 'then';
                        } else {
                            $this_logic = 'else';
                        }
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                case 'filledin':
                    // custom field
                    $field = $field_data['iflogic_quantity_custom'];
                    $value = $field_data['iflogic_quantity_custom_compare'];
                    if (is_user_logged_in()) {
                        $customs = get_user_meta($current_user->ID, 'ym_custom_fields', true);
                        $test = $customs->{$field};
                        if ($test == $value) {
                            $this_logic = 'then';
                        } else {
                            $this_logic = 'else';
                        }
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                case 'servervar':
                case 'getvar':
                case 'postvar':
                case 'cookievar':
                    $source = '_' . substr($field_data['iflogic'], 0, -3);
                    $match_name = $field_data['iflogic_quantity_field'];
                    $match_value = $field_data['iflogic_quantity_entry'];
                    $current_value = $source[$match_name];
                    if ($current_value == $match_value) {
                        $this_logic = 'then';
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                case 'registeredfor':
                    //				case 'memberfor':
                //				case 'memberfor':
                case 'expiresin':
                    $match_value = $field_data['iflogic_quantity_memberfor_value'];
                    $match_unit = $field_data['iflogic_quantity_memberfor_unit'];
                    if (is_user_logged_in()) {
                        global $ym_user;
                        if ($field_data['iflogic'] == 'registeredfor') {
                            $math_date = strtotime($current_user->user_registered);
                        } else {
                            if ($field_data['iflogic'] == 'expiresin') {
                                $math_data = $ym_user->expire_date;
                            } else {
                                $math_date = '';
                            }
                        }
                        $seconds = ym_register_flow_date_math($match_value, $match_unit);
                        $diff = time() - $math_date;
                        if ($diff > $seconds) {
                            $this_logic = 'then';
                        } else {
                            $this_logic = 'else';
                        }
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                default:
                    $this_logic = 'else';
            }
            if ($this_logic == 'then' && $field_data['iflogic_showhide'] == 'show' || $this_logic == 'else' && $field_data['iflogic_showhide'] == 'hide') {
                $display = true;
            } else {
                $display = false;
            }
        }
        if ($field_data['label'] == 'page_logic' && ($this_logic == 'then' && $field_data['iflogic_showhide'] == 'hide' || $this_logic == 'else' && $field_data['iflogic_showhide'] == 'hide')) {
            $html .= '
<script type="text/javascript">
	jQuery(document).ready(function() {
		jQuery(\'#ym_register_flow_form\').submit();
	});
</script>
';
            $html .= '</form>';
            return $html;
        } else {
            if ($field_data['label'] == 'page_logic') {
                // skip the IF and just skip the whole loop if should?
                continue;
            }
        }
        /**
         output render
        */
        $html .= '<span class="' . $field_data['classes'] . '">';
        //open class span
        if ($field_data['types'] == 'freetext' && $display) {
            $html .= '<p>' . nl2br($field_data['names']) . '</p>';
            //names o.0 lol
            /**
            Customs Processor
            */
        } else {
            if ($field_data['types'] == 'custom' && $display) {
                // LIFTED FROM ym.php line 642
                // modded tooooo!
                if ($field_data['names'] == 'terms_and_conditions' && !empty($ym_res->tos)) {
                    $html .= '<p>
						<textarea name="tos" cols="29" rows="5" readonly="readonly">' . $ym_res->tos . '</textarea>';
                    $html .= '</p>';
                    $html .= '<p>
						<label class="ym_label" for="terms_and_conditions">
							<div><input type="checkbox" class="checkbox" name="terms_and_conditions" id="terms_and_conditions" value="1" />
							' . __('I agree to the Terms and Conditions.', 'ym') . '</div>
						</label>
					</p>' . "\n";
                } else {
                    if ($field_data['names'] == 'ym_password') {
                        $html .= '<label for="ym_password">' . $field_data['label'];
                        $html .= '<input type="password" name="' . $field_data['names'] . '" value="" />';
                        $html .= '<input type="hidden" name="required[' . $field_data['names'] . ']" value="' . $field_data['required'] . '" />';
                        if ($field_data['required']) {
                            $html .= $ym_sys->required_custom_field_symbol;
                        }
                        $html .= '</label>';
                        if (!empty($field_data['options'])) {
                            $html .= '<label for="ym_password_check">' . __('Confirm Password', 'ym');
                            $html .= '<input type="password" name="ym_password_check" value="" />';
                            $html .= '<input type="hidden" name="ym_password_dupe" value="1" />';
                            if ($field_data['required']) {
                                $html .= $ym_sys->required_custom_field_symbol;
                            }
                            $html .= '</label>';
                        }
                    } else {
                        if ($field_data['names'] == 'subscription_introduction' && !empty($ym_res->subs_intro)) {
                            $html .= '<div class="ym_subs_intro">' . $ym_res->subs_intro . '</div>';
                        } else {
                            if ($field_data['names'] == 'subscription_options') {
                                global $ym_packs;
                                $upsell_from = ym_request('gateway_return', false) ? $pack_id : false;
                                // TO DO
                                $pack_data = false;
                                if ($pack_id && !ym_request('gateway_return', false)) {
                                    $pack_data = ym_get_pack_by_id($pack_id);
                                    if ($pack_data) {
                                        $label = ym_get_pack_label($pack_id);
                                        $html .= '<p>' . sprintf(__('You are subscribing to <b>%s</b>', 'ym'), $label) . '</p>';
                                        $html .= '<input type="hidden" name="pack_id" value="' . $pack_id . '" />';
                                    } else {
                                        return __('A Register Flow Error Occurred (Type 4) Specified Pack Not Found', 'ym');
                                    }
                                } else {
                                    $ym_packs->packs = apply_filters('ym_packs', $ym_packs->packs);
                                    foreach ($ym_packs->packs as $pack) {
                                        if ($upsell_from == $pack['id']) {
                                            continue;
                                        }
                                        $label = ym_get_pack_label($pack['id']);
                                        $html .= '<label for="pack_id_' . $pack['id'] . '">';
                                        $html .= '<input type="radio" name="pack_id" id="pack_id_' . $pack['id'] . '" value="' . $pack['id'] . '" />';
                                        $html .= ' ' . $label . ' ';
                                        $html .= '</label>';
                                    }
                                }
                            } else {
                                if ($field_data['names'] == 'birthdate') {
                                    $html .= '<label for="ym_birthdate_month">' . $field_data['label'];
                                    $birthdate_fields = ym_birthdate_fields('ym_birthdate', ym_post('ym_birthdate_month', ''), ym_post('ym_birthdate_day', ''), ym_post('ym_birthdate_year', ''));
                                    $html .= $birthdate_fields;
                                    $html .= '<input type="hidden" name="required[ym_birthdate_month]" value="' . $field_data['required'] . '" />';
                                    $html .= '<input type="hidden" name="required[ym_birthdate_day]" value="' . $field_data['required'] . '" />';
                                    $html .= '<input type="hidden" name="required[ym_birthdate_year]" value="' . $field_data['required'] . '" />';
                                    if ($field_data['required']) {
                                        $html .= $ym_sys->required_custom_field_symbol;
                                    }
                                    $html .= '</label>';
                                } else {
                                    if ($field_data['names'] == 'country') {
                                        $html .= '<label for="ym_country">' . $field_data['label'];
                                        $countries_sel = ym_countries_list('ym_country', ym_post('ym_country', false));
                                        $html .= $countries_sel;
                                        $html .= '<input type="hidden" name="required[ym_country]" value="' . $field_data['required'] . '" />';
                                        if ($field_data['required']) {
                                            $html .= $ym_sys->required_custom_field_symbol;
                                        }
                                        $html .= '</label>';
                                    } else {
                                        // HERE
                                        $this_custom = '';
                                        foreach ($custom_data as $custom) {
                                            $label = $custom['label'];
                                            if (!$label) {
                                                $label = strtolower(str_replace(' ', '_', $custom['name']));
                                            }
                                            if ($label == $field_data['label']) {
                                                // found
                                                $this_custom = $custom;
                                                break;
                                            }
                                        }
                                        if ($this_custom) {
                                            $ro = $this_custom['readonly'] ? 'readonly="readonly"' : '';
                                            // check for special
                                            $value = $this_custom['value'];
                                            if (strpos($value, ':') !== false) {
                                                $array = explode(':', $value);
                                                if (count($array)) {
                                                    switch ($array[0]) {
                                                        case 'cookie':
                                                            $value = ym_cookie($array[1]);
                                                            break;
                                                        case 'session':
                                                            $value = ym_session($array[1]);
                                                            break;
                                                        case 'get':
                                                            $value = ym_get($array[1]);
                                                            break;
                                                        case 'post':
                                                            $value = ym_post($array[1]);
                                                            break;
                                                        case 'request':
                                                        case 'qs':
                                                            $value = ym_request($array[1]);
                                                            break;
                                                        default:
                                                            $value = '';
                                                            break;
                                                    }
                                                    $this_custom['value'] = ym_post($this_custom['name'], $value);
                                                }
                                            } else {
                                                if (is_user_logged_in()) {
                                                    $this_custom['value'] = ym_custom_value($this_custom['id']);
                                                } else {
                                                    $this_custom['value'] = ym_post($this_custom['name'], $this_custom['value']);
                                                }
                                            }
                                            // ro adjust for fields that should not be changed
                                            switch ($this_custom['type']) {
                                                case 'password':
                                                case 'text':
                                                    $html .= '<label for="' . $this_custom['name'] . '">' . $this_custom['label'];
                                                    $html .= '<input type="' . $this_custom['type'] . '" name="' . $this_custom['name'] . '" value="' . $this_custom['value'] . '" ' . $ro . ' />';
                                                    $html .= '<input type="hidden" name="required[' . $this_custom['name'] . ']" value="' . $field_data['required'] . '" />';
                                                    if ($field_data['required'] && !$ro) {
                                                        $html .= $ym_sys->required_custom_field_symbol;
                                                    }
                                                    $html .= '</label>';
                                                    break;
                                                case 'hidden':
                                                    $html .= '<input type="hidden" name="' . $this_custom['name'] . '" value="' . $this_custom['value'] . '" ' . $ro . ' />';
                                                    break;
                                                case 'yesnocheckbox':
                                                    $html .= '<label for="' . $this_custom['name'] . '">' . $this_custom['label'];
                                                    $html .= '<input type="checkbox" name="' . $this_custom['name'] . '" value="1" ' . ($this_custom['value'] ? 'checked="checked"' : '') . ' ' . $ro . ' />';
                                                    $html .= '<input type="hidden" name="required[' . $this_custom['name'] . ']" value="' . $field_data['required'] . '" />';
                                                    if ($field_data['required'] && !$ro) {
                                                        $html .= $ym_sys->required_custom_field_symbol;
                                                    }
                                                    $html .= '</label>';
                                                    break;
                                                case 'yesno':
                                                case 'select':
                                                case 'multiselect':
                                                    $html .= '<label for="' . $this_custom['name'] . '">' . $this_custom['label'];
                                                    if ($this_custom['type'] == 'multiselect') {
                                                        $html .= '<select name="' . $this_custom['name'] . '[]" multiple="multiple"';
                                                    } else {
                                                        $html .= '<select name="' . $this_custom['name'] . '" ';
                                                    }
                                                    $html .= '>';
                                                    if ($this_custom['type'] == 'select' || $this_custom['type'] == 'multiselect') {
                                                        $options = explode(';', $this_custom['available_values']);
                                                    } else {
                                                        $options = array(__('Yes', 'ym'), __('No', 'ym'));
                                                    }
                                                    foreach ($options as $option) {
                                                        if (strpos($option, ':')) {
                                                            list($option, $val) = explode(':', $option);
                                                            $html .= '<option value="' . $option . '" ' . ($option == $this_custom['value'] ? 'selected="selected"' : '') . '>' . $val . '</option>';
                                                        } else {
                                                            $html .= '<option value="' . $option . '" ' . ($option == $this_custom['value'] ? 'selected="selected"' : '') . '>' . $option . '</option>';
                                                        }
                                                    }
                                                    $html .= '
</select>
';
                                                    $html .= '<input type="hidden" name="required[' . $this_custom['name'] . ']" value="' . $field_data['required'] . '" />';
                                                    if ($field_data['required'] && !$ro) {
                                                        $html .= $ym_sys->required_custom_field_symbol;
                                                    }
                                                    $html .= '</label>';
                                                    break;
                                                case 'textarea':
                                                    $html .= '<label for="' . $this_custom['name'] . '">' . $this_custom['label'];
                                                    $html .= '<textarea name="' . $this_custom['name'] . '" cols="29" rows="5" ' . $ro . '>' . $this_custom['value'] . '</textarea>';
                                                    $html .= '<input type="hidden" name="required[' . $this_custom['name'] . ']" value="' . $field_data['required'] . '" />';
                                                    if ($field_data['required'] && !$ro) {
                                                        $html .= $ym_sys->required_custom_field_symbol;
                                                    }
                                                    $html .= '</label>';
                                                    break;
                                                case 'file':
                                                    $html .= '<label for="' . $this_custom['name'] . '">' . $this_custom['label'];
                                                    $html .= '<input type="file" name="' . $this_custom['name'] . '" />';
                                                    $html .= '</label>';
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // END LIFT
                /**
                Buttons
                */
            } else {
                if (($field_data['types'] == 'payment_button' || $field_data['types'] == 'payment_action') && $display) {
                    $payment_gateway_detected = true;
                    add_filter('ym_additional_code', 'ym_register_flow_override_return', 10, 3);
                    $enabled = get_option('ym_modules');
                    // use the ym user id function
                    if (ym_get_user_id()) {
                        if (in_array($field_data['names'], $enabled)) {
                            // register flow
                            $class = $field_data['names'];
                            $pay = new $class();
                            if ($first_button) {
                                $html .= '</form>';
                                $first_button = false;
                            }
                            $this_pack = $pack_data;
                            // coupon check
                            if (isset($post_data['coupon_value']) && $post_data['coupon_value']) {
                                // stop
                                // stash
                                $value = ym_apply_coupon($post_data['coupon'], $post_data['coupon_type'], $this_pack['cost']);
                                $type = ym_get_coupon_type($value);
                                if ($type == 'percent') {
                                    // percent cost change
                                    $this_pack['cost'] = $this_pack['cost'] / 100 * $value;
                                } else {
                                    if ($type == 'sub_pack') {
                                        // diff pack
                                        $this_pack = ym_get_pack_by_id($value);
                                    } else {
                                        // other
                                        // new cost
                                        $this_pack['cost'] = $value;
                                    }
                                }
                                ym_register_coupon_use($post_data['coupon'], ym_get_user_id(), 'buy_subscription_' . $pack_data['id']);
                                if (!$this_pack['cost']) {
                                    // change to free
                                    //							$field_data['names'] = 'ym_free';
                                    // lifted from 135 of ym-register.include.php
                                    $code_to_use = 'freebie_code';
                                    // attempt to redirect to the processor.
                                    $loc = $ym_home . '/index.php?ym_process=ym_free&' . $code_to_use . '=buy_subscription_' . $this_pack['id'] . '_' . ym_get_user_id();
                                    if (!headers_sent()) {
                                        header('Location: ' . $loc);
                                        exit;
                                    } else {
                                        echo '<script type="text/javascript">window.location = "' . $loc . '";</script>';
                                    }
                                    die;
                                }
                            }
                            // there will always be pack data becuase I picked the default one earlier
                            // but it will default to the default pack anyway
                            if ($this_pack['cost']) {
                                // && $field_data['names'] != 'ym_free') {
                                //						$gw_button_form = $pay->getButton($this_pack['id'], (isset($post_data['coupon_value']) ? $this_pack['cost'] : false));
                                //						$html .= $gw_button_form;
                                $gw_button_form = $pay->getButton($this_pack['id'], isset($post_data['coupon_value']) ? $this_pack['cost'] : false);
                                if ($field_data['types'] == 'payment_action') {
                                    if (method_exists($pay, 'register_auto_payment_action')) {
                                        $html .= $pay->register_auto_payment_action($this_pack['id'], isset($post_data['coupon_value']) ? $this_pack['cost'] : false, true);
                                    } else {
                                        if ($gw_button_form) {
                                            $html .= $gw_button_form . '
<script type="text/javascript">
	jQuery(document).ready(function() {
		jQuery(\'.' . $field_data['names'] . '_form\').submit();
	});
</script>
';
                                        }
                                    }
                                } else {
                                    if (method_exists($pay, 'register_payment_action')) {
                                        $html .= $pay->register_payment_action(true);
                                    } else {
                                        $html .= $gw_button_form;
                                    }
                                }
                            } else {
                                if (!$this_pack['cost'] && $field_data['names'] == 'ym_free') {
                                    // free
                                    $gw_button_form = $pay->getButton($this_pack['id'], false);
                                    $html .= $gw_button_form;
                                    if ($field_data['types'] == 'payment_action') {
                                        if (method_exists($pay, 'register_auto_payment_action')) {
                                            $html .= $pay->register_auto_payment_action($this_pack['id'], false, true);
                                        } else {
                                            if ($gw_button_form) {
                                                $html .= '
<script type="text/javascript">
	jQuery(document).ready(function() {
		jQuery(\'.ym_free_form\').submit();
	});
</script>
';
                                            }
                                        }
                                    } else {
                                        if (method_exists($pay, 'register_payment_action')) {
                                            $html .= $pay->register_payment_action(true);
                                        }
                                    }
                                } else {
                                    if (isset($post_data['coupon_value']) && $post_data['coupon_value']) {
                                        return __('A Register Flow Error Occurred (Type 5) Pack/Coupon Error', 'ym');
                                    }
                                }
                            }
                        }
                    } else {
                        // user not logged in // No User ID Determined
                        return __('In order to continue you need to Register or Login', 'ym');
                    }
                    /**
                    Widgets
                    */
                } else {
                    if ($field_data['types'] == 'widget' && $display) {
                        if ($field_data['names'] == 'login') {
                            // login form
                            $html .= '
	<input type="hidden" name="ym_register_flow_page" value="' . $current_page . '" />
	<input type="hidden" name="ym_register_flow_next_page" value="' . $current_page . '" />
	';
                            //	<input type="hidden" name="ym_register_flow_do_login" value="1" />';
                            $html .= ym_login_form();
                        } else {
                            if ($field_data['names'] == 'register_facebook') {
                                // check for and handle a signed request
                                if ($fb_widget_ok) {
                                    // skippy
                                    $html .= '
					<script type="text/javascript">
						jQuery(document).ready(function() {
							jQuery(\'#ym_register_flow_form\').submit();
						});
					</script>
					';
                                } else {
                                    $html .= '
<iframe src="https://www.facebook.com/plugins/registration?
             client_id=' . get_option('ym_register_flow_fb_app_id') . '&
             redirect_uri=' . get_permalink() . '&
             fields=name,email,first_name,last_name,password"
        scrolling="auto"
        frameborder="no"
        style="border:none"
        allowTransparency="true"
        width="100%"
        height="550">
</iframe>
';
                                    // no next/complete please
                                }
                                $payment_gateway_detected = true;
                            } else {
                                $html .= 'Undefined Widget: (' . $field_data['names'] . ')';
                            }
                        }
                        /**
                        Coupon
                        */
                    } else {
                        if ($field_data['names'] == 'coupon' && $display) {
                            $value = isset($post_data[$field_data['names']]) ? $post_data[$field_data['names']] : '';
                            $html .= '<label for="' . $field_data['names'] . '">' . $field_data['label'];
                            $html .= '<input type="text" name="' . $field_data['names'] . '" id="' . $field_data['names'] . '" value="' . $value . '" />';
                            $html .= '<input type="hidden" name="coupon_type" value="' . $field_data['types'] . '" />';
                            $html .= '<input type="hidden" name="required[' . $field_data['names'] . ']" value="' . $field_data['required'] . '" />';
                            if ($field_data['required']) {
                                $html .= $ym_sys->required_custom_field_symbol;
                            }
                            $html .= '</label>';
                            /**
                            Display everything else
                            */
                        } else {
                            if ($display) {
                                $value = isset($post_data[$field_data['names']]) ? $post_data[$field_data['names']] : '';
                                $html .= '<label for="' . $field_data['names'] . '">' . $field_data['label'];
                                $html .= '<input type="' . $field_data['types'] . '" name="' . $field_data['names'] . '" id="' . $field_data['names'] . '" value="' . $value . '" />';
                                $html .= '<input type="hidden" name="required[' . $field_data['names'] . ']" value="' . $field_data['required'] . '" />';
                                if ($field_data['required']) {
                                    $html .= $ym_sys->required_custom_field_symbol;
                                }
                                $html .= '</label>';
                            }
                        }
                    }
                }
            }
        }
        $html .= '</span>';
        //closes class span
    }
    if (!$first_button) {
        // kill id
        $html = str_replace('id="ym_register_flow_form"', '', $html);
        // complete?
        if (!$next_page) {
            $query = 'SELECT complete_url FROM ' . $wpdb->prefix . 'ym_register_flows WHERE flow_id = ' . $flow_id;
            if ($url = $wpdb->get_var($query)) {
                $url = site_url($url);
                $form_top = str_replace('<form action=""', '<form action="' . $url . '"', $form_top);
            }
        }
        // append the form top
        $html .= $form_top;
        // end it
    }
    // payment gateway?
    if (!$payment_gateway_detected) {
        $html .= '<p>';
        if ($next_page) {
            $html .= '<input type="submit" value="' . $next_button . '" />';
        } else {
            $html .= '
<input type="hidden" name="flowcomplete" value="1" />
<input type="submit" value="' . $complete_button . '" />';
        }
        $html .= '</p>';
    }
    $html .= '</form>';
    return $html;
}
function ym_fbook_init()
{
    global $facebook_settings, $location;
    //	session_start();// wp does this
    if (ym_request('destroy')) {
        session_destroy();
        header('Location: ' . site_url());
        exit;
    }
    $location = get_permalink() ? get_permalink() : 'http' . (is_ssl() ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    /********************************************/
    /* AWAKE?
    	/********************************************/
    if (!ym_facebook_settings()) {
        // no settings
        // iframe break out if iframe present
        add_action('wp_head', 'ym_fbook_init_iframe_breakout');
        // abort
        return;
    }
    if (!$facebook_settings->enabled) {
        // not enabled
        // iframe break out if iframe present
        add_action('wp_head', 'ym_fbook_init_iframe_breakout');
        // abort
        return;
    }
    /********************************************/
    /* Aborts
    	/********************************************/
    if (is_admin()) {
        // in the admin system
        return;
    }
    // abort login
    if (FALSE !== strpos($_SERVER['REQUEST_URI'], 'wp-login')) {
        return;
    }
    // IPN
    if (ym_request('ym_process')) {
        return;
    }
    // Cron
    if (ym_request('doing_wp_cron')) {
        return;
    }
    /********************************************/
    /* basic defines
    	/********************************************/
    define('YM_FBOOK_APPID', $facebook_settings->app_id);
    define('YM_FBOOK_SECRET', $facebook_settings->app_secret);
    $Location = str_replace('?logged_out=1', '', $location);
    define('YM_FBOOK_IN_HERE', $location);
    // app target
    define('YM_FBOOK_APP_TARGET', 'https://apps.facebook.com/' . $facebook_settings->canvas_url . str_replace(site_url(), '', $location));
    define('YM_FBOOK_PAGE_TARGET', 'https://facebook.com/' . $facebook_settings->page_url . '?sk=app_' . YM_FBOOK_APPID);
    // can't take args
    if (isset($_SESSION['facebook_last_page']) && $_SESSION['facebook_last_page']) {
        define('YM_FBOOK_LAST_PAGE', $_SESSION['facebook_last_page']);
    } else {
        // TODO: use location for now, but should be defined FB Root URL based on page/app
        define('YM_FBOOK_LAST_PAGE', $location);
    }
    $_SESSION['facebook_last_page'] = $location;
    /********************************************/
    /* Linter
    	/********************************************/
    // come back to this if needed
    $test = 'facebookexternalhit';
    if (substr($_SERVER['HTTP_USER_AGENT'], 0, strlen($test)) == $test) {
        return;
    }
    /********************************************/
    /* Scope
    	/********************************************/
    $scope = array();
    global $scopes;
    foreach ($scopes as $fbsetting => $entry) {
        if (isset($facebook_settings->{$fbsetting}) && $facebook_settings->{$fbsetting}) {
            $scope[] = $entry;
        }
    }
    if (count($scope)) {
        $scope = '&scope=' . implode(',', $scope);
    } else {
        $scope = '';
    }
    /********************************************/
    /* Defines
    	/********************************************/
    // oauth target
    $base = 'http://www.facebook.com/dialog/oauth/?client_id=' . YM_FBOOK_APPID . $scope . '&redirect_uri=';
    define('YM_FBOOK_AUTH_APP', $base . urlencode(YM_FBOOK_APP_TARGET));
    define('YM_FBOOK_AUTH_PAGE', $base . urlencode(YM_FBOOK_PAGE_TARGET));
    define('YM_FBOOK_AUTH_NO', $base . urlencode($location));
    /********************************************/
    /* Exceptions
    	/********************************************/
    if (isset($_SESSION['in_facebook']) && $_SESSION['in_facebook'] == 1 && ym_request('leavefacebook') == 1 && $facebook_settings->enable_leave_facebook) {
        // leaving :-(
        unset($_SESSION['in_facebook']);
        // route to last page, not current page (aka location)
        // as that is leavefacebook = 1
        echo '<script type="text/javascript">top.location.href="' . $_SESSION['facebook_last_page'] . '";</script>';
        exit;
    }
    if (ym_request('loggedout') == 1) {
        wp_logout();
        $in_facebook = isset($_SESSION['in_facebook']) ? $_SESSION['in_facebook'] : FALSE;
        $in_facebook_page = isset($_SESSION['in_facebook_page']) ? $_SESSION['in_facebook_page'] : FALSE;
        session_destroy();
        session_start();
        $_SESSION['in_facebook'] = $in_facebook;
        $_SESSION['in_facebook_page'] = $in_facebook_page;
        $r = ym_fbook_oauth_go();
        //		echo $r . '<br />';
        $r = str_replace(array('loggedout=1&', 'loggedout=1'), '', $r);
        $r = str_replace(array(urlencode('loggedout=1&'), urlencode('loggedout=1')), '', $r);
        //		$_SESSION['ym_fb_auth_target'] = $r;
        $_SESSION['ym_fb_auth_target'] = str_replace(array(urlencode('loggedout=1&'), urlencode('loggedout=1'), 'loggedout=1'), '', $_SESSION['ym_fb_auth_target']);
        //		echo $r;
        //echo $_SESSION['ym_fb_auth_target'];
        //echo '<script type="text/javascript">top.location.href="' . $_SESSION['ym_fb_auth_target'] . '";</script>';
        echo '<script type="text/javascript">top.location.href="' . $r . '";</script>';
        exit;
    }
    // Ping check to see if facebook exists and is alive
    // Most commonly analytics
    if (ym_get('ymfbook')) {
        $_SESSION['in_facebook'] = 1;
    }
    if ($_SESSION['in_facebook']) {
        wp_enqueue_script('ym-fb', site_url('wp-content/plugins/ym_facebook/js/fb.js'), array('jquery'), YM_FB_PLUGIN_VERSION);
        wp_enqueue_style('ym-fb-login', site_url('wp-content/plugins/ym_facebook/css/ym_fbook_login.css'), array(), YM_FB_PLUGIN_VERSION);
    }
    // height controls
    if ($facebook_settings->iframe_size == 'scrollbars') {
        if ($facebook_settings->iframe_size_height) {
            define('YM_FBOOK_HEIGHT', 'FB.Canvas.setSize({height: ' . $facebook_settings->iframe_size_height . '});');
        } else {
            define('YM_FBOOK_HEIGHT', '');
            // height of window-ish
        }
    } else {
        define('YM_FBOOK_HEIGHT', 'FB.Canvas.setAutoResize();');
    }
    // width controls
    if (isset($_SESSION['in_facebook_page']) && $_SESSION['in_facebook_page']) {
        $width = 450;
    } else {
        $width = 600;
    }
    define('YM_FBOOK_WIDTH', $width);
    /********************************************/
    /* post or session
    	/********************************************/
    if (ym_post('signed_request', false)) {
        // landed in facebook from the outside world
        // store the request
        $_SESSION['facebook_signed_request'] = $_POST['signed_request'];
        // set in facebook here as we are defo. in facebook
        // cant do it on data uncode as we could be on the main site
        // using a wordpress side facebook like wall
        // for example
        $_SESSION['in_facebook'] = TRUE;
        // if in_facebook then redirect there
        // if in_facebook and in_facebook_page then go to page
        // if in_facebook_page only do nothing (as not in facebook)
        $_SESSION['facebook_signed_request'] = $_POST['signed_request'];
    }
    /********************************************/
    /* force
    	/********************************************/
    if ($facebook_settings->force_facebook && !$_SESSION['in_facebook']) {
        // force
        $_SESSION['in_facebook'] = 1;
        if ($facebook_settings->page_url) {
            $_SESSION['in_facebook_page'] = 1;
        }
        header('Location: ' . ym_fbook_oauth_go());
        //header('Location: ' . ($facebook_settings->page_url ? YM_FBOOK_PAGE_TARGET : YM_FBOOK_APP_TARGET));
        exit;
    }
    /********************************************/
    /* interupt for auth
    	/********************************************/
    // check for a get code
    if (ym_get('code')) {
        // landed with a code
        // oAuth return, validate
        // get token
        if ($_SESSION['in_facebook_page'] && $facebook_settings->page_url) {
            $url = 'https://facebook.com/' . $facebook_settings->page_url . '/';
        } else {
            $url = 'https://apps.facebook.com/' . $facebook_settings->canvas_url . '/';
        }
        if (!$_SESSION['ym_fb_auth_target']) {
            // no target
            ym_fbook_oauth_go();
        }
        // generate auth code
        //		$_SESSION['ym_fb_auth_target'] = str_replace(array(urlencode('loggedout=1&'), urlencode('loggedout=1'), 'loggedout=1&', 'loggedout=1'), '', $_SESSION['ym_fb_auth_target']);
        //			'&redirect_uri=' . urlencode($_SESSION['ym_fb_auth_target']) .
        $auth_code = '?client_id=' . YM_FBOOK_APPID . '&redirect_uri=' . urlencode($url) . '&client_secret=' . YM_FBOOK_SECRET . '&code=' . $_GET['code'];
        //		echo $auth_code;//exit;
        // exchange
        $facebook_auth = new Facebook('', $auth_code);
        // decode
        $auth = $facebook_auth->auth;
        $test = json_decode($auth);
        if ($test->error->message) {
            echo $test->error->message;
            ym_fbook_add_message(str_replace('_', ' ', $test->error->message));
            return;
        }
        parse_str($auth, $query);
        $_SESSION['facebook_oauth_token'] = $query['access_token'];
        $_SESSION['facebook_oauth_start'] = time();
        $_SESSION['facebook_oauth_expires'] = $query['expires'];
        // clean
        $_SESSION['ym_fb_auth_target'] = str_replace('code=' . ym_get('code'), '', $_SESSION['ym_fb_auth_target']);
        echo '<script type="text/javascript">top.location.href="' . $_SESSION['ym_fb_auth_target'] . '"</script>';
        unset($_SESSION['ym_fb_auth_target']);
        // and GO BABY GO!!!!!!!!!!!
        exit;
    }
    /********************************************/
    /* munch
    	/********************************************/
    if (isset($_SESSION['facebook_signed_request']) && $_SESSION['facebook_signed_request']) {
        // exisiting session
        // validate
        $data = facebook_uncode($_SESSION['facebook_signed_request']);
        if ($data) {
            // last control
            if (isset($_SESSION['facebook_use_last_page']) && $_SESSION['facebook_use_last_page']) {
                unset($_SESSION['facebook_use_last_page']);
                header('Location: ' . YM_FBOOK_LAST_PAGE);
                exit;
            }
            if (isset($data->page) && $data->page) {
                $_SESSION['facebook_page'] = $data->page;
                // defo in a page
                // should only occur on landing on the page
                $_SESSION['in_facebook_page'] = TRUE;
                // ALERT LANDING TRIGGER LANDING CONTROLLER
                if ($facebook_settings->page_landing && $_SERVER['REQUEST_URI'] != $facebook_settings->page_landing && $_POST['signed_request']) {
                    // somewhere to land
                    header('Location: /' . $facebook_settings->page_landing);
                    exit;
                }
                define('ym_fbphp_dev_in_page', TRUE);
            } else {
                define('ym_fbphp_dev_in_page', TRUE);
            }
            // landing control needed?
            if ($facebook_settings->canvas_landing && $_SERVER['REQUEST_URI'] != $facebook_settings->canvas_landing && $_POST['signed_request'] && !$_SESSION['facebook_has_landed'] && !$_SESSION['in_facebook_page']) {
                // landing control if the Root Page is requested
                header('Location: /' . $facebook_settings->canvas_landing);
                exit;
            }
            $_SESSION['facebook_has_landed'] = TRUE;
            // oauth
            $_SESSION['facebook_oauth_token'] = $data->oauth_token;
            $_SESSION['facebook_oauth_start'] = $data->issued_at;
            $_SESSION['facebook_oauth_expires'] = $data->expires;
        }
    } else {
        if (isset($_SESSION['facebook_oauth_token']) && $_SESSION['facebook_oauth_token']) {
            // we have a facebook session
        } else {
            // no session
            ym_fbook_do_template();
            return;
        }
    }
    // GIVE ME A CLIENT
    global $facebook_client;
    $facebook_client = new Facebook($_SESSION['facebook_oauth_token']);
    if (!$facebook_client->initok) {
        $facebook_client = FALSE;
    } else {
        // permissions check
        // check oauth granted match what we need
        // in case the game has changed.
        $permissions = $facebook_client->permissions();
        global $scopes;
        $scope_copy = $scopes;
        foreach ($scope_copy as $fbsetting => $entry) {
            if (isset($facebook_settings->{$fbsetting}) && $facebook_settings->{$fbsetting}) {
                // check presence
                if (isset($permissions->data[0]->{$entry}) && $permissions->data[0]->{$entry} == 1) {
                    unset($scope_copy[$fbsetting]);
                }
            } else {
                // not requested
                unset($scope_copy[$fbsetting]);
            }
        }
        if (count($scope_copy)) {
            add_action('ym_fbook_messages', 'ym_fbook_messages');
            add_action('template_redirect', 'ym_fbook_template');
            define('FBOOK_TEMPLATE_OVERRIDE', 'login');
            return;
        }
    }
    if ($facebook_client && !$data) {
        // build data
        $data = new stdClass();
        $data->user_id = $facebook_client->user_data->id;
        $data->locale = $facebook_client->user_data->locale;
    }
    // facebook user ID
    $_SESSION['facebook_user_id'] = $data->user_id;
    $_SESSION['locale'] = isset($data->locale) ? $data->locale : 'en_GB';
    // check facebook login
    if ($facebook_settings->force_facebook_auth && !$data->user_id) {
        // require login
        // redirect to facebook login
        // use template login template
        add_action('ym_fbook_messages', 'ym_fbook_messages');
        add_action('template_redirect', 'ym_fbook_template');
        define('FBOOK_TEMPLATE_OVERRIDE', 'login');
        return;
    }
    if (isset($data->id) && $data->id) {
        ym_fbook_has_oauth_expired();
    }
    // check wordpress login
    ym_fbook_maintain_wordpress();
    /*
    if ($_REQUEST['register'] == 1 && !$_SESSION['facebook_registering']) {
    	// need login.....
    	$_SESSION['facebook_use_last_page'] = 1;
    	$_SESSION['facebook_registering'] = 1;
    	unset($_SESSION['ym_facebook_me_cache']);
    	echo '<script type="text/javascript">top.location.href="' . ym_fbook_oauth_go() . '"</script>';
    	exit;
    }
    if ($_REQUEST['register'] == 1 && $_SESSION['facebook_registering']) {
    	// go register dammit
    	add_action('ym_fbook_messages', 'ym_fbook_messages');
    	add_action('template_redirect', 'ym_fbook_template');
    	define('FBOOK_TEMPLATE_OVERRIDE', 'register');
    	return;
    }
    */
    // Hidden Reg
    if ($facebook_settings->register_with_facebook_hidden && $_SESSION['facebook_user_id'] && !$_SESSION['wordpress_user_id']) {
        // not logged into WP
        // go hidden reg
        ym_fbook_hidden_register();
    }
    // action cases
    if (ym_request('dolink', false)) {
        // go for link
        $_SESSION['dolink'] = 1;
        echo '<script type="text/javascript">top.location.href="' . ym_fbook_oauth_go() . '"</script>';
        exit;
    }
    if (ym_session('dolink')) {
        if (!$_SESSION['facebook_user_id']) {
            header('Location: ' . ym_fbook_oauth_go());
            exit;
        }
        ym_fbook_dolink();
    }
    if (ym_request('dounlink')) {
        // unlink
        ym_fbook_dounlink();
    }
    ym_fbook_do_template();
}
Example #3
0
function ym_register_form($return = false, $page = 1, $pack_id = false, $hide_custom_fields = false, $hide_further_pages = false, $autologin = false)
{
    global $duration_str, $ym_sys, $ym_res;
    $html = '';
    $fld_obj = get_option('ym_custom_fields');
    $hide = $ym_sys->hide_custom_fields;
    $user_id = ym_get_user_id();
    $hide_custom_fields = explode(',', $hide_custom_fields);
    if (!is_array($hide_custom_fields)) {
        $hide_custom_fields = array($hide_custom_fields);
    }
    $entries = $fld_obj->entries;
    $order = $fld_obj->order;
    if (empty($order)) {
        return;
    }
    if (strpos($order, ';') !== false) {
        $orders = explode(';', $order);
    } else {
        $orders = array($order);
    }
    $html .= '<div style="clear:both; height: 1px;">&nbsp;</div>';
    if ($redirect_to = ym_get('ym_redirector')) {
        $html .= '<input type="hidden" name="ym_redirector" value="' . urlencode($redirect_to) . '" />';
    }
    if ($autologin) {
        $html .= '<input type="hidden" name="ym_autologin" value="1" />';
    }
    $another_page = false;
    $lowest_page = ym_get_last_custom_field_page() + 1;
    //must be higher than the highest page
    $values = array();
    if ($username = ym_get('username')) {
        $values = ym_get_custom_fields_by_username($username);
    }
    foreach ($orders as $order) {
        foreach ($entries as $entry) {
            if ($order == $entry['id']) {
                if (in_array($entry['id'], $hide_custom_fields)) {
                    continue;
                }
                $entry['page'] = !isset($entry['page']) ? 1 : $entry['page'];
                if ($page == $entry['page']) {
                    if (isset($_POST['hide_ym_field-' . $entry['id']])) {
                        $entry['type'] = 'hidden';
                        //will hide the field if the appropriate post data is present.
                        //This is intended to go with hard coded signups where the register page will act as stage 2
                    }
                    $value = false;
                    $row = '';
                    $hide_label = false;
                    if (isset($values[$entry['id']])) {
                        if (trim($values[$entry['id']])) {
                            $value = trim($values[$entry['id']]);
                        }
                    } else {
                        $value = ym_post('ym_field-' . $entry['id']);
                    }
                    if ($value) {
                        $entry['value'] = $value;
                    }
                    if ($value = $entry['value']) {
                        if (strpos($value, ':') !== false) {
                            $array = explode(':', $value);
                            if (count($array)) {
                                switch ($array[0]) {
                                    case 'cookie':
                                        $entry['value'] = ym_cookie($array[1], '');
                                        break;
                                    case 'session':
                                        $entry['value'] = ym_session($array[1], '');
                                        break;
                                    case 'get':
                                        $entry['value'] = ym_get($array[1], '');
                                        break;
                                    case 'post':
                                        $entry['value'] = ym_post($array[1], '');
                                        break;
                                    case 'request':
                                    case 'qs':
                                        $entry['value'] = ym_request($array[1], '');
                                        break;
                                    default:
                                        $entry['value'] = '';
                                        break;
                                }
                            }
                        }
                    }
                    if ($entry['name'] == 'terms_and_conditions' && !empty($ym_res->tos)) {
                        $row .= '<p>
								<textarea name="tos" cols="29" rows="5" readonly="readonly">' . $ym_res->tos . '</textarea>';
                        $row .= '</p>';
                        $row .= '<p>
								<label class="ym_label" for="ym_tos">
									<div><input type="checkbox" class="checkbox" name="ym_tos" id="ym_tos" value="1" />
									' . __('I agree to the Terms and Conditions.', 'ym') . '</div>
								</label>
							</p>' . "\n";
                    } else {
                        if ($entry['name'] == 'subscription_introduction' && !empty($ym_res->subs_intro)) {
                            $row .= '<div class="ym_subs_intro">' . $ym_res->subs_intro . '</div>';
                        } else {
                            if ($entry['name'] == 'subscription_options') {
                                //						$pack_restriction = false;
                                //						if (strpos(',', $pack_id)) {
                                //							$pack_restriction = explode(',', $pack_id);
                                //						}
                                if (ym_request('ym_subscription', $pack_id)) {
                                    // pre selected!
                                    // could be from a ym_register and the reg is hidden so showing the selector here is bad
                                    $row .= '<input type="hidden" name="ym_subscription" value="' . ym_request('ym_subscription', $pack_id) . '" />';
                                    $hide_label = TRUE;
                                } else {
                                    global $ym_packs;
                                    $packs = $ym_packs->packs;
                                    $active_modules = get_option('ym_modules');
                                    if (empty($active_modules)) {
                                        $row .= '<p>' . __('There are no payment gateways active. Please contact the administrator.', 'ym') . '</p>';
                                    } else {
                                        // RENDER
                                        $packs_shown = 0;
                                        if ($existing_data = ym_request('ym_subscription')) {
                                            $default = $existing_data;
                                        } else {
                                            $default = ym_get_default_pack();
                                        }
                                        $did_checked = FALSE;
                                        foreach ($packs as $pack) {
                                            /*
                                            if (count($pack_restriction)) {
                                            	// has restiction
                                            	if (in_array($pack['id'], $pack_restriction)) {
                                            		// do not show aka hide
                                            		$pack['hide_subscription'] = 1;
                                            	}
                                            }
                                            */
                                            if (!$pack['hide_subscription']) {
                                                $row .= '<div class="ym_register_form_subs_row">
													<div class="ym_reg_form_pack_radio">
														<input type="radio" ';
                                                if ($pack['id'] == $default && !$did_checked) {
                                                    $row .= 'checked="checked"';
                                                    $did_checked = TRUE;
                                                }
                                                $packs_shown++;
                                                $row .= ' class="checkbox" id="ym_subscription_' . $pack['id'] . '" name="ym_subscription" value="' . $pack['id'] . '" />
													</div>
													<label for="ym_subscription_' . $pack['id'] . '" class="ym_subs_opt_label ym_reg_form_pack_name">' . ym_get_pack_label($pack['id']) . '</label>
												</div>';
                                            }
                                        }
                                        if (!$packs_shown) {
                                            $hide_label = true;
                                        } else {
                                            if ($entry['caption']) {
                                                $row = '<div class="ym_clear">&nbsp;</div><div class="ym_register_form_caption">' . $entry['caption'] . '</div>' . $row;
                                            }
                                        }
                                        // END RENDER
                                    }
                                }
                            } else {
                                if ($entry['name'] == 'birthdate' && !$hide) {
                                    $birthdate_fields = ym_birthdate_fields('ym_birthdate');
                                    $row .= '<p>' . $birthdate_fields . '</p>';
                                } else {
                                    if ($entry['name'] == 'country' && !$hide) {
                                        $countries_sel = ym_countries_list('ym_country');
                                        $row .= '<p>' . $countries_sel . '</p>';
                                    } else {
                                        if ((!$entry['profile_only'] || $entry['profile_only'] == false) && !$hide) {
                                            $ro = $entry['readonly'] ? 'readonly="readonly"' : '';
                                            if ($entry['type'] == 'text') {
                                                $fld = '<input type="text" name="ym_field-' . $entry['id'] . '" value="' . $entry['value'] . '" ' . $ro . ' class="ym_reg_input" size="25" />';
                                            } else {
                                                if ($entry['type'] == 'hidden') {
                                                    $fld = '<input type="hidden" name="ym_field-' . $entry['id'] . '" value="' . $entry['value'] . '" />';
                                                    $hide_label = true;
                                                } else {
                                                    if ($entry['type'] == 'yesno') {
                                                        $fld = '<select class="ym_reg_select" name="ym_field-' . $entry['id'] . '" ' . $ro . '>';
                                                        $options = array('Yes', 'No');
                                                        foreach ($options as $option) {
                                                            $fld .= '<option value="' . $option . '" ' . (trim($option) == $value ? 'selected="selected"' : '') . '>' . $option . '</option>';
                                                        }
                                                        $fld .= '</select>';
                                                    } else {
                                                        if ($entry['type'] == 'password') {
                                                            // primary use is ym_password
                                                            if ($entry['name'] == 'ym_password') {
                                                                $fld = '<input type="password" name="ym_password" value="' . $entry['value'] . '" ' . $ro . ' class="ym_reg_input" size="25" />';
                                                                ym_login_remove_password_string();
                                                            } else {
                                                                // allow other password fields
                                                                $fld = '<input type="password" name="' . $entry['name'] . '" value="' . $entry['value'] . '" ' . $ro . ' class="ym_reg_input" size="25" />';
                                                            }
                                                        } else {
                                                            if ($entry['type'] == 'html') {
                                                                $fld = '<div class="ym_reg_html">' . $entry['value'] . '</div>';
                                                            } else {
                                                                if ($entry['type'] == 'textarea') {
                                                                    $fld = '<textarea class="ym_reg_textarea" name="ym_field-' . $entry['id'] . '" cols="29" rows="5" ' . $ro . '>' . $entry['value'] . '</textarea>';
                                                                } else {
                                                                    if ($entry['type'] == 'select') {
                                                                        $fld = '<select class="ym_reg_select" name="ym_field-' . $entry['id'] . '" ' . $ro . '>';
                                                                        $options = explode(';', $entry['available_values']);
                                                                        foreach ($options as $option) {
                                                                            if (strpos($option, ':')) {
                                                                                list($option, $val) = explode(':', $option);
                                                                                $fld .= '<option value="' . $option . '" ' . ($option == $value ? 'selected="selected"' : '') . '>' . $val . '</option>';
                                                                            } else {
                                                                                $fld .= '<option value="' . $option . '" ' . ($option == $value ? 'selected="selected"' : '') . '>' . $option . '</option>';
                                                                            }
                                                                        }
                                                                        $fld .= '</select>';
                                                                    } else {
                                                                        if ($entry['type'] == 'multiselect') {
                                                                            $fld = '<select class="ym_reg_multiselect" name="ym_field-' . $entry['id'] . '[]" ' . $ro . ' multiple="multiple">';
                                                                            $options = explode(';', $entry['available_values']);
                                                                            foreach ($options as $option) {
                                                                                if (strpos($option, ':')) {
                                                                                    list($option, $val) = explode(':', $option);
                                                                                    $fld .= '<option value="' . $option . '" ' . ($option == $value ? 'selected="selected"' : '') . '>' . $val . '</option>';
                                                                                } else {
                                                                                    $fld .= '<option value="' . $option . '" ' . ($option == $value ? 'selected="selected"' : '') . '>' . $option . '</option>';
                                                                                }
                                                                            }
                                                                            $fld .= '</select>';
                                                                        } else {
                                                                            if ($entry['type'] == 'file') {
                                                                                $fld = '<input type="file" name="ym_field-' . $entry['id'] . '" />';
                                                                                if ($entry['available_values'] == 'image') {
                                                                                    $fld .= $entry['value'];
                                                                                }
                                                                            } else {
                                                                                if ($entry['type'] == 'callback') {
                                                                                    $callback = 'ym_callback_custom_fields_' . $entry['name'] . '_editor';
                                                                                    if (function_exists($callback)) {
                                                                                        $fld = $callback($entry['id']);
                                                                                    }
                                                                                } else {
                                                                                    if (!($fld = apply_filters('ym_generate_custom_field_type_' . $entry['type'], '', 'ym_field-' . $entry['id'], $entry, $value))) {
                                                                                        $fld = '<input type="text" name="ym_field-' . $entry['id'] . '" value="' . $entry['value'] . '" ' . $ro . ' class="ym_reg_input" size="25" />';
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            if ($entry['required']) {
                                                $fld .= '<div class="ym_clear">&nbsp;</div><div class="ym_register_form_required">' . $ym_sys->required_custom_field_symbol . '</div>';
                                            }
                                            if ($entry['caption']) {
                                                $fld .= '<div class="ym_clear">&nbsp;</div><div class="ym_register_form_caption">' . $entry['caption'] . '</div>';
                                            }
                                            $row .= '<p>' . $fld . '</p>';
                                        }
                                    }
                                }
                            }
                        }
                    }
                    ////Adding of the row
                    if ((!$entry['profile_only'] || $entry['profile_only'] == false) && !$hide && !$hide_label) {
                        $html .= '<div class="ym_register_form_row" id="' . str_replace(' ', '_', $entry['name']) . '_row">';
                        $label = $entry['label'];
                        $html .= '<label class="ym_label">' . $label . '</label>';
                    }
                    $html .= $row;
                    if ((!$entry['profile_only'] || $entry['profile_only'] == false) && !$hide && !$hide_label) {
                        $html .= '<div class="ym_clear">&nbsp;</div>';
                        $html .= '</div>';
                    }
                    ////End adding of the row
                }
                if (!$hide_further_pages) {
                    if ($entry['page'] > $page) {
                        if ($entry['page'] < $lowest_page) {
                            $lowest_page = $entry['page'];
                        }
                        $another_page = true;
                    }
                }
            }
        }
    }
    $html .= '<input type="hidden" name="ym_page" value="' . $page . '" />';
    //so that the update function knows which pages to validate
    if ($another_page) {
        $html .= '<input type="hidden" name="another_page_needed" value="' . $lowest_page . '" />';
        //so that the rendering function knows to add another page before sending off to the gateway
    }
    if ($return) {
        return $html;
    } else {
        echo $html;
    }
}