function invite_codes_create($email, $more = array())
{
    if ($invite = invite_codes_get_by_email($email)) {
        return array('ok' => 1, 'invite' => $invite);
    }
    $code = null;
    $tries = 0;
    while (!$code) {
        $code = random_string(12);
        $tries += 1;
        if (invite_codes_get_by_code($code)) {
            $code = null;
        }
        if ($tries == 50) {
            break;
        }
    }
    if (!$code) {
        return array('ok' => 0, 'error' => 'Failed to generate code');
    }
    $invite = array('email' => $email, 'code' => $code, 'created' => time());
    if (isset($more['invited_by'])) {
        $invite['invited_by'] = intval($more['invited_by']);
    }
    $hash = array();
    foreach ($invite as $k => $v) {
        $hash[$k] = AddSlashes($v);
    }
    $rsp = db_insert('InviteCodes', $hash);
    if ($rsp['ok']) {
        $rsp['invite'] = $invite;
    }
    return $rsp;
}
            $template = 'email_invite_user.txt';
            invite_codes_send_invite($invite, $template);
            $invite = invite_codes_get_by_code($code, $ensure_sent);
            $GLOBALS['smarty']->assign_by_ref("invite", $invite);
            $GLOBALS['smarty']->assign("invite_sent", 1);
        } else {
            $GLOBALS['error'] = "Invalid invite code";
        }
    } else {
        if (!$email) {
            $GLOBALS['error'] = "Missing email";
        } else {
            if (!rfc822_is_valid_email_address($email)) {
                $GLOBALS['error'] = "Invalid email ({$email})";
            } else {
                if ($invite = invite_codes_get_by_email($email)) {
                    $GLOBALS['smarty']->assign_by_ref("invite", $invite);
                } else {
                    $more = array('invited_by' => $GLOBALS['cfg']['user']['id']);
                    $rsp = invite_codes_invite_user($email, $more);
                    if ($rsp['ok']) {
                        $GLOBALS['smarty']->assign_by_ref("invite", $rsp['invite']);
                    } else {
                        $GLOBALS['error'] = $rsp['error'];
                    }
                }
            }
        }
    }
}
$GLOBALS['smarty']->display("page_god_generate_invite.txt");