コード例 #1
0
ファイル: auth.php プロジェクト: vetermanve/vk_workmart
function web_controller_auth_register()
{
    if (web_router_get_method() !== 'POST') {
        web_router_render_page('auth', 'register', []);
        return;
    }
    $email = web_router_get_param('email');
    if (!$email) {
        web_router_render_page('auth', 'register', ['msg' => 'Введите email', 'wrong' => 'email']);
        return;
    }
    preg_match('/[\\w\\d]+@[\\w\\d]+[\\w\\d\\.]+/', $email, $matches);
    if (!isset($matches[0])) {
        web_router_render_page('auth', 'register', ['msg' => 'Введите корректный email', 'wrong' => 'email']);
        return;
    }
    lets_use('user_register');
    $authUserId = user_register_get_user_id_by_email($email);
    if ($authUserId) {
        web_router_render_page('auth', 'register', ['msg' => 'Пользователь с таким email уже существует', 'wrong' => 'email']);
        return;
    }
    $userName = web_router_get_param('name');
    if (!$userName) {
        web_router_render_page('auth', 'register', ['msg' => 'Введите ваше имя', 'wrong' => 'name']);
        return;
    }
    $pass = web_router_get_param('pass');
    if (!$pass || mb_strlen($pass) < 6) {
        web_router_render_page('auth', 'register', ['msg' => 'Пароль должен быть задан и не менее 6ти символов', 'wrong' => 'pass']);
        return;
    }
    $userId = user_register_new_user($userName, $email, $pass);
    if (!$userId) {
        web_router_render_page('auth', 'register', ['msg' => 'Ошибка при сохранении пользвателя, повторите позднее', 'wrong' => 'error']);
        return;
    }
    lets_use('user_session');
    $secret = user_session_get_secret($userId);
    $token = user_session_build_token($userId, $secret);
    user_session_write_session_cookie($userId, $token, 86400 * 30);
    web_router_redirect('/');
}
コード例 #2
0
ファイル: order.php プロジェクト: vetermanve/vk_workmart
function web_controller_order_create()
{
    lets_use('user_self');
    $authorId = user_self_id();
    if (!$authorId) {
        web_router_redirect('/auth/auth');
        return;
    }
    if (web_router_get_method() === 'POST') {
        $cost = web_router_get_param('cost');
        if (!$cost) {
            web_router_render_page('order', 'create', ['msg' => 'Цена должна быть задана', 'error' => 'cost']);
            return;
        }
        $title = web_router_get_param('title');
        if (!$title) {
            web_router_render_page('order', 'create', ['msg' => 'Название должно быть задано', 'error' => 'title']);
            return;
        }
        $desc = web_router_get_param('desc');
        if (!$desc) {
            web_router_render_page('order', 'create', ['msg' => 'Описание должно быть задано', 'error' => 'desc']);
            return;
        }
        lets_use('web_router', 'order_storage', 'billing_balance', 'billing_account', 'billing_transaction', 'billing_locks', 'user_self');
        $sum = (double) web_router_get_param('cost');
        if (!$sum || $sum < 0) {
            web_router_render_page('order', 'create', ['msg' => 'Сумма заказа должна быть задана и положительна', 'error' => 'sum']);
            return;
        }
        $sum = round($sum, 2);
        $accountFrom = billing_account_get_user_main_account($authorId);
        $accountTo = billing_account_get_user_locked_account($authorId);
        $trId = billing_transaction_register($accountFrom, $accountTo, $sum);
        if (!$trId) {
            // cant register transaction
            billing_transaction_fail($trId);
            web_router_render_page('order', 'create', ['result' => false, 'msg' => 'Ошибка сервера, повторите позже.']);
            return;
        }
        $lockRes = billing_locks_lock_transaction($trId, [$accountFrom, $accountTo]);
        if (!$lockRes) {
            // cant lock transaction
            billing_transaction_fail($trId);
            web_router_render_page('order', 'create', ['result' => false, 'msg' => 'В данный момент операция невозможна, повторите позже']);
            return;
        }
        $movementPossible = billing_balance_check_sum_available($accountFrom, $sum);
        if (!$movementPossible) {
            // not enough money
            billing_transaction_fail($trId);
            billing_locks_unlock_transaction($trId);
            web_router_render_page('order', 'create', ['result' => false, 'msg' => 'На исходящем счете недостаточно денег']);
            return;
        }
        $dbTransactionLock = billing_balance_storage_transaction_start();
        if (!$dbTransactionLock) {
            // cant begin db transaction
            billing_transaction_fail($trId);
            billing_locks_unlock_transaction($trId);
            web_router_render_page('order', 'create', ['result' => false, 'msg' => 'Не удалось начать транзакцию']);
            return;
        }
        $moveRes = billing_balance_process_move($accountFrom, $accountTo, $sum, $trId);
        if (!$moveRes) {
            // cant move money
            billing_balance_storage_transaction_rollback();
            billing_transaction_fail($trId);
            billing_locks_unlock_transaction($trId);
            web_router_render_page('order', 'create', ['result' => false, 'msg' => 'Не удалось перевести деньги']);
            return;
        }
        $orderId = order_storage_create_order($title, $desc, $authorId, $cost);
        if (!$orderId) {
            billing_transaction_fail($trId);
            billing_locks_unlock_transaction($trId);
            web_router_render_page('order', 'create', ['result' => false, 'msg' => 'Не удалось сохранить заказ']);
            return;
        }
        $transactionCommit = billing_balance_storage_transaction_commit();
        if ($transactionCommit) {
            // cant commit db transaction
            billing_transaction_fail($trId);
            billing_locks_unlock_transaction($trId);
            web_router_render_page('order', 'create', ['result' => false, 'msg' => 'Не удалось завершить транзакцию']);
            return;
        }
        order_storage_change_order_status($orderId, ORDER_STORAGE_ORDER_STATUS_OK);
        billing_transaction_success($trId);
        billing_locks_unlock_transaction($trId);
        web_router_redirect('/order/success?id=' . $orderId);
    }
    web_router_render_page('order', 'create');
}