function magicBuildPage($request) { $orderConnection = mappedConnection('orders'); $usersConnection = mappedConnection('users'); $query = $request['query']; // пагинация по айдишникам, что бы не было сдвигов/повторов при добавлении/удаленнии элементов в ленте $minId = array_key_exists('min_id', $query) ? (int) $query['min_id'] : null; $maxId = array_key_exists('max_id', $query) ? (int) $query['max_id'] : null; $pageSize = 10; // селектим на 1 элемент больше, // что бы понять, достигнут ли конец и не селектить count $pageSeizeInc = $pageSize + 1; $orders = loadOrders($orderConnection, $pageSeizeInc, $minId, $maxId); $isLastPage = count($orders) < $pageSeizeInc; // удаляем лишний элемент if (!$isLastPage) { array_pop($orders); } $userIds = []; foreach ($orders as $order) { if (($id = $order['created_by']) && !in_array($id, $userIds, true)) { $userIds[] = $id; } } $users = loadUsersByIds($usersConnection, $userIds); $vars = ['users' => $users, 'orders' => $orders, 'is_last_page' => $isLastPage]; if (array_key_exists('HTTP_ACCEPT', $request['server']) && strpos(strtolower($request['server']['HTTP_ACCEPT']), 'application/json') !== false) { return createResponse(json_encode($vars), 200, ['content-type' => 'application/json']); } else { $vars['form'] = createCreateForm(); return createResponse(render('orders/list.html.php', $vars)); } }
$sql = 'SELECT id, name, email, hashed_password FROM users WHERE email = ? LIMIT 1'; $statement = mysqli_prepare($connection, $sql); mysqli_stmt_bind_param($statement, 's', $email); mysqli_stmt_execute($statement); mysqli_stmt_bind_result($statement, $id, $name, $email, $hp); mysqli_stmt_fetch($statement); mysqli_stmt_close($statement); if (isset($id, $name, $email, $hp)) { return ['id' => $id, 'name' => $name, 'email' => $email, 'hashed_password' => $hp]; } return null; } return function (array $request) { if (currentUser()) { return createRedirectResponse('/index.php/'); } $form = createCreateForm(); formHandleRequest($form, $request); if (formIsValid($form)) { $auth = extractValues($form); $user = findUserByEmail(mappedConnection('users'), $auth['email']); if ($user && password_verify($auth['password'], $user['hashed_password'])) { authorize($user); return createRedirectResponse('/'); } else { $form['valid'] = false; $form['fields']['password']['errors'][] = 'Неверный Пароль'; } } return createResponse(render('session/new.html.php', ['form' => $form])); };
<?php include_once __DIR__ . '/create_form.php'; include_once SRC_DIR . '/template.php'; include_once SRC_DIR . '/forms.php'; return function ($request) { if (currentUser()) { return createRedirectResponse('/index.php/'); } return createResponse(render('session/new.html.php', ['form' => createCreateForm()])); };