Example #1
0
function web_render_page_content($module, $template, $data = [], $layout = 'main')
{
    global $_web_render_global_params;
    global $_web_render_scope_params;
    $templateFile = __DIR__ . '/templates/' . $module . '/' . $template . '.phtml';
    $layoutFile = __DIR__ . '/templates/layouts/' . $layout . '.phtml';
    /* check templates exists */
    if (!file_exists($templateFile)) {
        core_error('tpl file: ' . $templateFile . ' not found');
        return '';
    }
    if (!file_exists($layoutFile)) {
        core_error('layout file: ' . $layoutFile . ' not found');
        return '';
    }
    /* write scope data to global var to access via _v */
    $_web_render_scope_params = $data;
    /* extract vars to straight access */
    extract((array) $_web_render_global_params);
    extract((array) $data);
    /* render page content */
    ob_start();
    require $templateFile;
    $content = ob_get_clean();
    /* write page work time */
    core_log_work_time();
    /* render layout */
    ob_start();
    require $layoutFile;
    $result = ob_get_clean();
    return $result;
}
Example #2
0
function web_response_flush()
{
    global $_web_response_lock;
    global $_web_response_content;
    global $_web_response_cookie;
    // check response already sent
    if ($_web_response_lock) {
        core_error('trying to flush on locked response');
        return;
    }
    // lock response, to prevent second sending
    $_web_response_lock = true;
    // set http code =)
    http_response_code($_web_response_content[_WEB_RESPONSE_CODE]);
    // first of all send content type if set
    if ($_web_response_content[_WEB_RESPONSE_CONTENT_TYPE]) {
        header('Content-Type: ' . $_web_response_content[_WEB_RESPONSE_CONTENT_TYPE] . '; charset=utf-8');
    }
    if ($_web_response_cookie) {
        foreach ($_web_response_cookie as $name => $params) {
            call_user_func_array('setcookie', $params);
        }
    }
    // send additional headers if set
    if ($_web_response_content[_WEB_RESPONSE_HEADERS]) {
        foreach ($_web_response_content[_WEB_RESPONSE_HEADERS] as $header) {
            header($header, true);
        }
    }
    // send body if set
    if ($_web_response_content[_WEB_RESPONSE_BODY]) {
        echo $_web_response_content[_WEB_RESPONSE_BODY];
    }
}
Example #3
0
function _billing_balance_pack_money($amount)
{
    $amount = round($amount, 2, PHP_ROUND_HALF_DOWN) * 100;
    if ($amount < 0) {
        core_error('Trying to pack negative money value: ' . $amount);
        // @todo pass additional info
        $amount = 0;
    }
    return $amount;
}
Example #4
0
function user_session_set_secret($userId, $secret)
{
    lets_use('core_config', 'storage_db', 'storage_nosql');
    $dbResult = storage_db_insert_row(USER_SESSION_DB_TABLE, ['user_id' => $userId, 'secret' => $secret]);
    if (!$dbResult) {
        core_error('cannot write new session to db: ' . storage_db_get_last_error(USER_SESSION_DB_TABLE));
        return false;
    }
    storage_nosql_set_prefix(CORE_CONFIG_REDIS_MAIN, USER_SESSION_REDIS_KEY_PREFIX, $userId, null);
    return true;
}
Example #5
0
function core_shutdown_process_checks()
{
    global $_core_shutdown_callbacks;
    if (empty($_core_shutdown_callbacks)) {
        return;
    }
    foreach ($_core_shutdown_callbacks as $name => $callback) {
        try {
            $callback();
        } catch (Exception $e) {
            core_error($e->getMessage() . '; on shutdown callback :' . $name);
        }
    }
}
Example #6
0
function billing_locks_lock_transaction($transactionId, $accountsIds)
{
    global $_billing_locks_by_transaction;
    if (isset($_billing_locks_by_transaction[$transactionId])) {
        core_error('trying to re-lock non-empty transaction');
        return false;
    }
    $_billing_locks_by_transaction[$transactionId] = [];
    foreach ($accountsIds as $accountId) {
        $lock = _billing_locks_lock($transactionId, $accountId);
        if (!$lock) {
            core_error('cant get lock on account:' . $accountId, __FUNCTION__);
            billing_locks_unlock_transaction($transactionId);
            return false;
        }
        $_billing_locks_by_transaction[$transactionId][] = $accountId;
    }
    return true;
}
Example #7
0
function billing_account_get_account($ownerId, $type, $autoCreate)
{
    lets_use('storage_db');
    $accountId = storage_db_get_value(BILLING_ACCOUNT_DB_TABLE, BILLING_ACCOUNT_FIELD_ID, [[BILLING_ACCOUNT_FIELD_OWNER_ID, $ownerId], [BILLING_ACCOUNT_FIELD_TYPE, $type]], ['LIMIT' => 1]);
    if (storage_db_get_last_error(BILLING_ACCOUNT_DB_TABLE)) {
        core_error('cant fetch account ' . json_encode(func_get_args()));
        return false;
    }
    // create account
    if (!$accountId && $autoCreate) {
        $bind = [BILLING_ACCOUNT_FIELD_OWNER_ID => $ownerId, BILLING_ACCOUNT_FIELD_TYPE => $type, BILLING_ACCOUNT_FIELD_CREATED => time()];
        $accountId = storage_db_insert_row(BILLING_ACCOUNT_DB_TABLE, $bind);
        if (!$accountId) {
            core_error('cant create an account ' . json_encode(func_get_args()));
            return false;
        }
    }
    return $accountId;
}
Example #8
0
function user_register_new_user($name, $email, $pass)
{
    lets_use('storage_db');
    storage_db_transaction_begin('users');
    $userId = storage_db_insert_row('users', ['name' => $name, 'email' => $email]);
    if (!$userId) {
        storage_db_transaction_rollback('users');
        core_error('cannot save user data to db table');
        return false;
    }
    lets_use('user_session');
    $token = user_session_create_token($userId, $pass);
    if (!$token) {
        storage_db_transaction_rollback('users');
        core_error('cannot save user token');
        return false;
    }
    storage_db_transaction_commit('users');
    return $userId;
}
Example #9
0
function core_error_lng($var)
{
    core_error(lang($var));
}
Example #10
0
function _storage_db_build_where($connection, $where)
{
    if (!isset($where[0])) {
        core_error('incorrect where bind: ' . json_encode($where), __FUNCTION__);
        return '0';
    }
    if (!is_array($where[0])) {
        $where = [$where];
    }
    $whereString = '';
    foreach ($where as $whereParam) {
        if (count($whereParam) == 2) {
            list($field, $value) = $whereParam;
            $operation = '=';
        } else {
            list($field, $operation, $value) = $whereParam;
        }
        $field = mysqli_real_escape_string($connection, $field);
        $glue = $whereString ? ' AND ' : '';
        if (is_array($value)) {
            if ($value) {
                foreach ($value as &$val) {
                    $val = mysqli_real_escape_string($connection, $val);
                    if (!is_numeric($val)) {
                        $val = '"' . $val . '"';
                    }
                }
                $whereString .= $glue . '(' . $field . ' in (' . implode(',', $value) . ')' . ')';
            } else {
                $whereString .= '0';
            }
        } else {
            $val = mysqli_real_escape_string($connection, $value);
            if (!is_numeric($val)) {
                $val = '"' . $val . '"';
            }
            $whereString .= $glue . '(' . $field . ' ' . $operation . ' ' . $val . ')';
        }
    }
    return trim($whereString);
}
Example #11
0
function storage_nosql_set_prefix($redisId, $prefix, $key, $value)
{
    $connect = _storage_nosql_connect($redisId);
    if (!$connect) {
        core_error('Missing connection to redis on ' . __FUNCTION__);
        return null;
    }
    if ($value === null) {
        core_log('REDIS DEL PREFIX: ' . $prefix . '::' . $key, __FUNCTION__);
        return $connect->hDel($prefix, $key);
    }
    core_log('REDIS SET PREFIX: ' . $prefix . '::' . $key . ' = ' . $value, __FUNCTION__);
    return $connect->hSet($prefix, $key, $value);
}