function getCurrentUserId() { if (!startSessionIfNecessary()) { return null; } return isset($_SESSION) ? getIfExists($_SESSION, 'user_id') : null; }
function getCommonConstant($key) { $value = getIfExists(COMMON_CONSTANTS, $key); if (is_null($value)) { logError('cannot find constant "' . $key . '"'); exit; } return $value; }
<?php require_once '../libraries/common.php'; include 'ajax_common.php'; const MAX_CHECK_COUNT = 20; $userId = \sessions\getCurrentUserId(); if (is_null($userId)) { notAuthErrorResponse(); return; } $lwTime = intval(getIfExists($_GET, 'lw_time')); $lwParsedOrderId = getParsedOrderId($_GET, 'lw_order_id'); $lwCustomerId = $lwParsedOrderId ? $lwParsedOrderId['customer_id'] : 0; $lwOrderId = $lwParsedOrderId ? $lwParsedOrderId['order_id'] : 0; $orders = \cache\getWaitingOrders($userId, $lwTime, $lwCustomerId, $lwOrderId, 0, 0, 0, MAX_CHECK_COUNT + 1); if (is_null($orders)) { internalErrorResponse(); return; } $newOrdersCount = min(MAX_CHECK_COUNT, sizeof($orders)); $newOrdersHasMore = sizeof($orders) > MAX_CHECK_COUNT; $log = \cache\getDoneOrCanceledLog($userId, $lwTime); if (is_null($log)) { internalErrorResponse(); return; } $presentableLog = array_map(function ($order) { return getCompositeOrderId($order); }, $log); echo jsonEncode(['new_orders_count' => $newOrdersCount, 'new_orders_has_more' => $newOrdersHasMore, 'done_or_canceled' => $presentableLog]);
require_once '../libraries/common.php'; include 'ajax_common.php'; $userId = \sessions\getCurrentUserId(); if (is_null($userId)) { notAuthErrorResponse(); return; } $lwTime = intval(getIfExists($_GET, 'lw_time')); $lwParsedOrderId = getParsedOrderId($_GET, 'lw_order_id'); $lwCustomerId = $lwParsedOrderId ? $lwParsedOrderId['customer_id'] : 0; $lwOrderId = $lwParsedOrderId ? $lwParsedOrderId['order_id'] : 0; $upTime = intval(getIfExists($_GET, 'up_time')); $upParsedOrderId = getParsedOrderId($_GET, 'up_order_id'); $upCustomerId = $upParsedOrderId ? $upParsedOrderId['customer_id'] : 0; $upOrderId = $upParsedOrderId ? $upParsedOrderId['order_id'] : 0; $count = intval(getIfExists($_GET, 'count')); if (!$count) { $count = 1; } $orders = \cache\getWaitingOrders($userId, $lwTime, $lwCustomerId, $lwOrderId, $upTime, $upCustomerId, $upOrderId, $count + 1); if (is_null($orders)) { internalErrorResponse(); return; } $list = []; foreach ($orders as $order) { array_push($list, ['order_id' => getCompositeOrderId($order), 'customer_id' => $order['customer_id'], 'description' => $order['description'], 'price' => number_format($order['price'], 2), 'time' => $order['time']]); if (sizeof($list) == $count) { break; } }
function getWaitingOrders($userId, $lwTime, $lwCustomerId, $lwOrderId, $upTime, $upCustomerId, $upOrderId, $count) { $cacheDbInfo = getDbForCache($userId); if (getIfExists(CONFIG, 'use_cache') !== true || !$cacheDbInfo) { return \storage\getWaitingOrders($lwTime, $lwCustomerId, $lwOrderId, $upTime, $upCustomerId, $upOrderId, $count); } $lowerBound = $lwTime ? [$lwTime, $lwCustomerId, $lwOrderId] : null; $upperBound = $upTime ? [$upTime, $upCustomerId, $upOrderId] : null; $link = \storage\connect($cacheDbInfo); if (!$link) { return null; } $timestamp = \database\getTimestamp($link); if (!$timestamp) { return null; } $ordersFromCache = getCachedWaitingOrders($link, $lowerBound, $upperBound, $count, $timestamp); if (!is_null($ordersFromCache) && $ordersFromCache !== false) { return $ordersFromCache; } if ($ordersFromCache !== false) { $orders = \storage\getWaitingOrders(0, 0, 0, 0, 0, 0, FEED_CACHE_SIZE); if (is_null($orders)) { return null; } if (!cacheWaitingOrders($link, $orders, $timestamp)) { logError('cannot cache waiting orders'); } $filteredOrders = filterWaitingOrders($orders, $lowerBound, $upperBound, $count); if ($filteredOrders !== false) { return $filteredOrders; } } return \storage\getWaitingOrders($lwTime, $lwCustomerId, $lwOrderId, $upTime, $upCustomerId, $upOrderId, $count); }
} $list = []; foreach ($orders as $order) { $element = ['order_id' => getCompositeOrderId($order), 'customer_id' => $order['customer_id'], 'description' => $order['description'], 'time' => $order['time']]; $price = getIfExists($order, 'price'); if ($price) { $element['price'] = number_format($price, 2); } $profit = getIfExists($order, 'profit'); if ($profit) { $element['profit'] = $profit; } $doneTime = getIfExists($order, 'done_time'); if ($doneTime) { $element['done_time'] = $doneTime; } if ($role === ROLE_CUSTOMER) { $executorId = getIfExists($order, 'executor_id'); $executorInfo = $executorId ? \storage\getUserInfoById($executorId) : null; $executorName = $executorInfo ? getIfExists($executorInfo, 'name') : null; if (!is_null($executorName)) { $element['executor'] = $executorName; } } array_push($list, $element); if (sizeof($list) == $count) { break; } } $hasMore = sizeof($orders) > $count; echo jsonEncode(['list' => $list, 'has_more' => $hasMore ? 'true' : 'value']);
<?php $secondsToSleep = getIfExists(CONFIG, 'show_down_ajax'); if ($secondsToSleep > 0) { sleep($secondsToSleep); }
function getDbForUserIdGeneration() { $shards = getShardsByKey('user_id_generation_sequence'); return $shards ? getIfExists($shards, 0) : null; }
function connect($dbInfo) { $host = getIfExists($dbInfo, 'host'); $database = getIfExists($dbInfo, 'database'); if (is_null($host) || is_null($database)) { logError('incorrect db info'); return null; } $port = getIfExists($dbInfo, 'port'); if (!$port) { $port = ''; } $login = getIfExists($dbInfo, 'login'); if (is_null($login)) { $login = ''; } $password = getIfExists($dbInfo, 'password'); if (is_null($password)) { $password = ''; } $key = $host . '||' . $database; global $hostAndDb2link; $link = getIfExists($hostAndDb2link, $key); if (!$link) { $link = \database\connect($host, $port, $database, $login, $password); if (!$link) { logError('cannot connect to database ' . $host . ': ' . $database); return null; } $hostAndDb2link[$key] = $link; } return $link; }
logError('incorrect request method ' . $requestMethod); internalErrorResponse(); return; } $userName = getIfExists($_POST, 'user-name'); $password = getIfExists($_POST, 'password'); if (!is_string($userName) || mb_strlen($userName) == 0) { validationErrorResponse(msg('no.username.error'), 'user-name'); return; } if (!is_string($password) || mb_strlen($password) == 0) { validationErrorResponse(msg('no.password.error'), 'password'); return; } if (mb_strlen($userName) > 20 || mb_strlen($password) > 20) { validationErrorResponse(msg('auth.failed.error')); return; } $userInfo = \storage\getUserInfoByName($userName); if (is_null($userInfo) || !array_key_exists('password', $userInfo) || !password_verify($password, $userInfo['password'])) { validationErrorResponse(msg('auth.failed.error')); return; } $userId = getIfExists($userInfo, 'id'); if (intval($userId) <= 0) { logError("user id should be a positive int but it is " . $userId); internalErrorResponse(); return; } \sessions\login($userId); successResponse();
$userId = \sessions\getCurrentUserId(); if (is_null($userId)) { notAuthErrorResponse(); return; } $description = trim(getIfExists($_POST, 'description')); if ($description == '') { validationErrorResponse(msg('no.description')); return; } $descriptionMaxLength = getCommonConstant('description.max.length'); if (mb_strlen($description) > $descriptionMaxLength) { validationErrorResponse(msg('description.length.error', $descriptionMaxLength)); return; } $price = floatval(getIfExists($_POST, 'price')); if ($price < 1) { validationErrorResponse(msg('min.price.error') . ' 1 ' . msg('currency')); return; } $maxPrice = getCommonConstant('order.max.price'); if ($price > $maxPrice) { validationErrorResponse(msg('max.price.error') . ' ' . $maxPrice . ' ' . msg('currency')); return; } $orderFromDb = \storage\addOrder($userId, $description, $price); if (is_null($orderFromDb)) { internalErrorResponse(); return; } $order = ['order_id' => getCompositeOrderId($orderFromDb), 'customer_id' => $orderFromDb['customer_id'], 'description' => $orderFromDb['description'], 'price' => number_format($orderFromDb['price'], 2), 'time' => $orderFromDb['time']];
<?php require_once '../libraries/common.php'; include 'ajax_common.php'; $userName = getIfExists($_POST, 'user-name'); $password = getIfExists($_POST, 'password'); $repeatPassword = getIfExists($_POST, 'repeat-password'); $role = getIfExists($_POST, 'role'); if (!is_string($userName) || mb_strlen($userName) == 0) { validationErrorResponse(msg('no.username.error'), 'user-name'); return; } $userNameMaxLength = getCommonConstant('user.name.max.length'); if (mb_strlen($userName) > $userNameMaxLength) { validationErrorResponse(msg('user.name.length.error', $userNameMaxLength), 'user-name'); return; } $userName = mb_strtolower($userName); if (!preg_match('/^\\w+$/', $userName)) { validationErrorResponse(msg('invalid.char.in.username.error'), 'user-name'); return; } if (!is_string($password) || mb_strlen($password) == 0) { validationErrorResponse(msg('no.password.error'), 'password'); return; } $passwordMinLength = getCommonConstant('password.min.length'); $passwordMaxLength = getCommonConstant('password.max.length'); if (mb_strlen($password) < $passwordMinLength || mb_strlen($password) > $passwordMaxLength) { validationErrorResponse(msg('password.length.error', $passwordMinLength, $passwordMaxLength), 'password'); return;
function mergeSortedArrays($arrays, $removeDuplicates, $asc, $func) { $result = []; $indexes = array_fill(0, sizeof($arrays), 0); while (true) { $bestValue = null; $bestElement = null; $bestArrayIndex = null; $i = 0; foreach ($arrays as $arr) { $index = $indexes[$i]; $element = getIfExists($arr, $index); if (!is_null($element)) { $value = $func($element); if (is_null($bestElement) || $asc && $value < $bestValue || !$asc && $value > $bestValue) { $bestElement = $element; $bestValue = $value; $bestArrayIndex = $i; } } $i++; } if (is_null($bestElement)) { break; } else { $indexes[$bestArrayIndex]++; $lastElement = getIfExists($result, sizeof($result) - 1); if (!$removeDuplicates || is_null($lastElement) || $func($lastElement) != $bestValue) { array_push($result, $bestElement); } } } return $result; }
function fetchOnlyValue($result) { $row = mysqli_fetch_row($result); return is_array($row) ? getIfExists($row, 0) : null; }