<?php
// Some config stuff :)
$config['default_lang'] = 'nl';
if (!isset($config['path']['script'])) {
    $config['path']['script'] = './';
}
$config['path']['lang'] = $config['path']['script'] . 'languages/';
$config['path']['flags'] = $config['path']['script'] . 'images/flags/';
$config['lang_save_time'] = 7 * 24 * 60 * 60; // A week
$config['inc_alt_lang'] = true;
$config['make_lang_bar'] = true;

// Initiate language var
$GLOBALS['lang'] = array();

/* Search for language files */
$lang_files = array(); // This array will hold the installed languages

// Open the language file directory
$handle = opendir($config['path']['lang']);

// Loop over the files
while (($file = readdir($handle)) !== false)
{
    // Filter out non-files
    if ($file != '.' && $file != '..')
    {
        // Check if the 'file' is an valid language directory
        if (is_dir($config['path']['lang'] . $file) && ereg('^[a-z]{2}$', $file))
        {
            $lang_files[] = $file;
        }
    }
}

/* Search for user selected languages */
$user_langs = array();     // This array will hold the user selected languages
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
    // The user has selected preferred languages, prepare them to read
    $accept_lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    $accept_lang = preg_replace('/(;q=[0-9]+\.[0-9]+)/i', '', $accept_lang);
    $accept_langs = explode(',', $accept_lang);

    // Loop over the languages and add them to the array
    foreach ($accept_langs as $accept_lang)
    {
        // Remove potential countries
        $accept_lang = explode('-', $accept_lang);
        $user_langs[] = $accept_lang[0];
    }
    // Remove duplicate values from the array
    $user_langs = array_unique($user_langs);
}


/* Determine the language in which the page will be shown */
// First check if the user selected a language to show the page
if (isset($_GET['lang']))
{
    $suggested_lang = $_GET['lang'];
}
// Check for a previously selected language
elseif (isset($_COOKIE['lang']))
{
    $suggested_lang = $_COOKIE['lang'];
}
// If a preferred language is selected check if that language file is available
if (isset($suggested_lang) && !empty($suggested_lang) && in_array($suggested_lang, $lang_files))
{
    $page['lang'] = $suggested_lang;
    // Save the preferred language in a cookie
    setcookie('lang', $suggested_lang, time() + $config['lang_save_time']);
}
else
{
    /* None or invalid language preference is expressed, obtain a language automatically */
    // Select the first language which is available
    foreach ($user_langs as $user_lang)
    {
        if (in_array($user_lang, $lang_files))
        {
            $page['lang'] = $user_lang;
            // Stop searching
            break;
        }
    }
    if (empty($page['lang']))
    {
        // Still no language has been selected use the default one
        $page['lang'] = $config['default_lang'];
    }
}

// Include some language files
include_lang_file('all');
include_lang_file('standard');


// Make the language bar if enabled in the config
if ($config['make_lang_bar'])
{
    // These flags dont't have the same name as their language
    $flag_fix = array(
        'en' => 'gb'
    );

    // Start making the language bar
    $lang_bar = '';
    foreach ($lang_files as $lang_file)
    {
        if ($lang_file != $page['lang'])
        {
            // Fix some flags
            $flag = $lang_file;
            foreach ($flag_fix as $old => $new)
            {
                $flag = ereg_replace($old, $new, $lang_file);
            }

            // The path to each flag
            $path = $config['path']['flags'] . $flag . '.gif';

            // Add current language to the language bar
            $lang_bar .= '<a href="?lang=' . $lang_file . '" title="'. $lang[$lang_file] . '"><img src="' . $path . '" alt="' . $lang[$lang_file] . '" border="0" /></a>' . "\n";
        }
    }
}

/* Include the given or an alternative language file */
function include_lang_file($name)
{
    if (!empty($name))
    {
        // Also make the config and page arrays available
        global $config, $page;

        // Make the path to the language file
        $path = $config['path']['lang'];
        $path .= $name == 'all' ? 'lang.all.php' : $page['lang'] . '/lang.' . $name . '.php';

        if (file_exists($path))
        {
            include $path;
        }

        // Try to include another file if enabled in the config
        elseif ($name != 'all' && $config['inc_alt_lang'])
        {
            global $user_langs;

            // Make the path to an alternative language file
            $alt_path = '';

            // Select the first user language which is available
            foreach ($user_langs as $user_lang)
            {
                if (file_exists($config['path']['lang'] . $user_lang . '/lang.' . $name . '.php'))
                {
                    $alt_path = $config['path']['lang'] . $user_lang . '/lang.' . $name . '.php';
                    // Stop searching
                    break;
                }
            }
            // If still no language has been found, use the default one
            if (empty($alt_path))
            {
                $alt_path = $config['path']['lang'] . $config['default_lang'] . '/lang.' . $name . '.php';
            }
            // Try to include the language file;
            if (file_exists($alt_path))
            {
                include $alt_path;
            }
        }
    }
    
    // Make globally available
    return $GLOBALS['lang'] = array_merge($GLOBALS['lang'], $lang);
}