예제 #1
0
파일: sessions.php 프로젝트: ekudel/vkt
function getCurrentUserId()
{
    if (!startSessionIfNecessary()) {
        return null;
    }
    return isset($_SESSION) ? getIfExists($_SESSION, 'user_id') : null;
}
예제 #2
0
파일: common.php 프로젝트: ekudel/vkt
function getCommonConstant($key)
{
    $value = getIfExists(COMMON_CONSTANTS, $key);
    if (is_null($value)) {
        logError('cannot find constant "' . $key . '"');
        exit;
    }
    return $value;
}
예제 #3
0
<?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]);
예제 #4
0
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;
    }
}
예제 #5
0
파일: cache.php 프로젝트: ekudel/vkt
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);
}
예제 #6
0
}
$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']);
예제 #7
0
파일: ajax_common.php 프로젝트: ekudel/vkt
<?php

$secondsToSleep = getIfExists(CONFIG, 'show_down_ajax');
if ($secondsToSleep > 0) {
    sleep($secondsToSleep);
}
예제 #8
0
파일: shards.php 프로젝트: ekudel/vkt
function getDbForUserIdGeneration()
{
    $shards = getShardsByKey('user_id_generation_sequence');
    return $shards ? getIfExists($shards, 0) : null;
}
예제 #9
0
파일: storage.php 프로젝트: ekudel/vkt
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;
}
예제 #10
0
파일: login.php 프로젝트: ekudel/vkt
    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();
예제 #11
0
파일: create_order.php 프로젝트: ekudel/vkt
$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']];
예제 #12
0
파일: register.php 프로젝트: ekudel/vkt
<?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;
예제 #13
0
파일: util.php 프로젝트: ekudel/vkt
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;
}
예제 #14
0
파일: database.php 프로젝트: ekudel/vkt
function fetchOnlyValue($result)
{
    $row = mysqli_fetch_row($result);
    return is_array($row) ? getIfExists($row, 0) : null;
}