View file sys/inc/functions/ds-system.php

File size: 6.7Kb
<?php 

/**
* Алиасы класса Registry
* @return object | string | array
*/

function ds_get($key, $default = NULL) {
    if (isset($key)) {
        $get = Registry::get($key); 

        if ($get === NULL && $default !== NULL) {
            return $default;
        }
        return $get; 
    }
}

/**
* Регистрирует глобальную переменную
* @return bolean true
*/
function ds_set($key, $var) {
    return Registry::set($key, $var); 
}

/**
* Возвращает все глобальные данные
* @return array
*/
function ds_getAll() {
    return Registry::getAll($key); 
}

function ds_die($msg) 
{
    die('
    <html>
        <head>
            <title>Ошибка DCMS-Social</title>
            <style>
            body {
                background-color: white;
                margin: 0; 
                padding: 0;
            }
            #ds-error {
                padding: 20px;
                width: 700px;
                max-width: 90%;
                margin: 30px auto;
                border: 1px solid #eaeaea;
                border-radius: 10px;
                box-sizing: border-box;
                background-color: #f9f9f9;
            }
            </style>
        </head>
        <body><div id="ds-error">
            ' . $msg . '
        </div></body>
    </html>');

}

/**
* Получает настройки сайта
* Может возвращать как значение так и все настройки
* @return array | string
*/ 

function get_settings($key = false, $default = '') 
{
    $set = array();
    $set_default = array();
    $set_dinamic = array();
    $set_replace = array();

    $ini = parse_ini_file( ROOTPATH.'/sys/upgrade/settings.ini', true );

    if (is_array($ini['DEFAULT'])) {
        $set_default = $ini['DEFAULT'];
    }
    
    if (is_array($ini['REPLACE'])) {
        $set_replace = $ini['REPLACE'];
    }
    
    $set_dinamic = get_options();
    $set = use_filters('ds_settings_filter', array_merge($set_default, $set_dinamic, $set_replace));

    if ($key === false) {
        return $set; 
    } elseif (isset($set[$key])) {
		return $set[$key]; 
	}

	return $default; 
}

function sort_position($a, $b) { 
    return strnatcmp($a["position"], $b["position"]); 
} 

function file_add_cache($cacheFile, $array) {
    $cacheFile = PATH_CACHE . '/' . $cacheFile . '.cache';

    if (is_file($cacheFile)) {
        unlink($cacheFile);
    } 

    file_put_contents($cacheFile, json_encode($array));
}

function file_get_cache($cacheFile) {
    $cacheFile = PATH_CACHE . '/' . $cacheFile . '.cache';

    if (is_file($cacheFile)) {
        $cache = json_decode(file_get_contents($cacheFile), 1); 
        return $cache;
    }

    return false; 
}

function file_delete_cache($cacheFile) {
    $cacheFile = PATH_CACHE . '/' . $cacheFile . '.cache';
    if (is_file($cacheFile)) {
        unlink($cacheFile);
    } 
}


function get_options($type = 'autoload') 
{
    $options = ds_get('ds_options_' . $type);

    if (!empty($options[$type])) { 
        return $options[$type]; 
    }

    $res = db::select("SELECT * FROM `options` WHERE `type` = '" . $type . "'"); 

    foreach($res AS $key => $option) {
        $options[$type][$option['name']] = $option['value']; 
    }

    ds_set('ds_options_' . $type, $options);

    return $options[$type]; 
}

function get_option($key, $default = NULL) 
{
    $options = ds_get('ds_options');

    if (!empty($options)) {
        if (isset($options[$key])) {
            return $options[$key];
        }
    }

    $cache = file_get_cache('options.cache'); 
    if (!empty($cache)) {
        if (isset($cache[$key])) {
            return $cache[$key]; 
        }
    }
    
    $res = db::fetch("SELECT * FROM `options` WHERE `name` = '" . my_esc($key) . "' LIMIT 1"); 

    if ($res) {
        return $res['value']; 
    }
    
    return $default; 
}

function update_option($key, $value, $type = '') 
{ 
    // Удаляем кеш опций
    file_delete_cache('options'); 

    $option = db::fetch("SELECT * FROM `options` WHERE `name` = '" . $key . "' LIMIT 1"); 

    if (isset($option['id'])) {
        if (is_array($value)) {
            $value = json_encode($value); 
        }
        $update = array(
            'value' => $value, 
        ); 
        db::update('options', $update, array('name' => $key)); 
    }
    
    else {
       db::query("INSERT INTO `options` (`name`, `value`, `type`) VALUES ('$key', '$value', '$type')");
    }
}

function delete_option($key) 
{ 
    // Удаляем кеш опций
    file_delete_cache('options'); 
    $option = db::fetch("SELECT * FROM `options` WHERE `name` = '" . $key . "' LIMIT 1"); 
    if (isset($option['id'])) {
        db::delete('options', array('name' => $key));
        return true;  
    }
    return false; 
}

function ds_options_load() 
{
    $cache = file_get_cache('options'); 

    if (empty($cache)) {
        $options = db::select("SELECT * FROM `options` WHERE type = 'autoload'", ARRAY_A);

        $cache = array(); 
        foreach($options AS $option) {
            $cache[$option['name']] = $option['value']; 
        }
        file_add_cache('options', $cache);
        ds_get('ds_options', $cache); 
    }

    ds_set('ds_options', $cache);
}



function ds_readdir_files_list($dir, $ds_files_recursive = array()) 
{
    $opdirbase = opendir($dir);
    while ($filebase = readdir($opdirbase)) {
        if ($filebase == '..' || $filebase == '.') continue; 

        if (is_file($dir . '/' . $filebase)) {
            $ds_files_recursive[] = $dir . '/' . $filebase;
        }

        elseif (is_dir($dir . '/' . $filebase)) {
            $ds_files_recursive = array_merge_recursive($ds_files_recursive, ds_readdir_files_list($dir . '/' . $filebase)); 
        }
    }

    return $ds_files_recursive; 
}

function ds_readdir_dir_list($dir, $ds_directory_recursive = array()) 
{
    $opdirbase = opendir($dir);
    while ($filebase = readdir($opdirbase)) {
        if ($filebase == '..' || $filebase == '.') continue; 

        if (is_dir($dir . '/' . $filebase)) {
            $ds_directory_recursive[] = $dir . '/' . $filebase;
            $ds_directory_recursive = array_merge_recursive($ds_directory_recursive, ds_readdir_dir_list($dir . '/' . $filebase)); 
        }
    }

    return $ds_directory_recursive; 
}

function ds_check_installed() 
{
    $install = true; 
    if (!is_file(ROOTPATH . '/config.php')) {
        header('Location: ' . ds_site_url() . '/adm_panel/install.php'); 
        exit;
    }
}