<?php
/*
if ((array_key_exists('ver', $_GET)) && ($_GET['ver'] == '1')) {
    setcookie('VER', '1', time()+8000, '/');
} elseif ($_COOKIE['VER'] != '1') {
    setcookie('VER', '0', time()+8000, '/');
    echo '<script>location="index.html";</script>';
}
*/

define('ROOT_PATH', dirname(dirname(dirname(__FILE__))));
define('ADMIN_PATH', ROOT_PATH . DIRECTORY_SEPARATOR . 'application_admin');
define('CURRENT_WWW_PATH', dirname(__FILE__));
define('WWW_PATH', dirname(dirname(__FILE__)));
define('PUBLIC_UPLOAD_PATH', WWW_PATH . DIRECTORY_SEPARATOR . 'uploads');
define('PRIVATE_UPLOAD_PATH', WWW_PATH . DIRECTORY_SEPARATOR . 'uploads');
define('LIBRARY_PATH', ROOT_PATH . DIRECTORY_SEPARATOR . 'library');
define('APPLICATION_PATH', ROOT_PATH . DIRECTORY_SEPARATOR . 'application');
define('VIEWS_PATH', ROOT_PATH . DIRECTORY_SEPARATOR . 'views');
define('GENERIC_VIEWS_PATH', ADMIN_PATH . DIRECTORY_SEPARATOR . 'Index' . DIRECTORY_SEPARATOR . 'views');
define('CACHE_PATH', ROOT_PATH . DIRECTORY_SEPARATOR . 'cache');
define('LANG_PATH', ROOT_PATH . DIRECTORY_SEPARATOR . 'lang');
define('TRANSLATOR_QUOTE', ROOT_PATH . DIRECTORY_SEPARATOR . 'lang');
define('CHARSET', 'UTF-8');
//define('CHARSET', 'ISO-8859-1');

set_include_path(get_include_path() .
PATH_SEPARATOR . ROOT_PATH . '/library/Custom/' .
PATH_SEPARATOR . ROOT_PATH . '/library/' .
PATH_SEPARATOR . APPLICATION_PATH .
PATH_SEPARATOR . '.'
);

include LIBRARY_PATH . DIRECTORY_SEPARATOR . 'Goweb.php';

// application
Goweb::loadClass('Goweb_Application');
$application = new Goweb_Application(ADMIN_PATH);


$frontController = Zend_Controller_Front::getInstance();
//$frontController->setControllerDirectory(APPLICATION_PATH);
$frontController->setControllerDirectory(ADMIN_PATH);
Goweb::loadClass('Goweb_Request');
$request = new Goweb_Request();
$frontController->setRequest($request); 

$application->initSession('_ADMIN', true);
$application->initLanguage(isset($_GET['lang']) ? $_GET['lang'] : null, true, true);
$application->initLanguageAdmin(isset($_GET['lang']) ? $_GET['lang'] : null, true, true);
$application->initPainel();
$application->runApp();


/**
 * Efectua a traducao de uma string.
 */
function translate() {
    static $translator = null;
    if (is_null($translator)) {
        /**
         * por vezes é chamado o translate antes de ele estar disponível, apesar de ainda não ser necessário nessa fase.
         */
        if (!Goweb::isRegistered('translator')){
            return '';
        }
        $translator = Goweb::registry('translator');
    }
    $args = func_get_args();
    return call_user_func_array(array($translator, 'translate'), $args);
}

/**
 * Efectua a traducao de uma string na administracao.
 */
function translateAdmin() {
    static $translatorAdmin = null;
    if (is_null($translatorAdmin)) {
        /**
         * por vezes é chamado o translateAdmin antes de ele estar disponível, apesar de ainda não ser necessário nessa fase.
         */
        if (!Goweb::isRegistered('translatorAdmin')){
            return '';
        }
    	$translatorAdmin = Goweb::registry('translatorAdmin');
    }
    $args = func_get_args();    
    return call_user_func_array(array($translatorAdmin, 'translate'), $args);
}

function isUTF8($str) {
    if ($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", "UTF-8"), "UTF-8", "UTF-32")) {
        return true;
    } else {
        return false;
    }
}