<commit_before><?php
/**
 * BoxBilling
 *
 * @copyright BoxBilling, Inc (http://www.boxbilling.com)
 * @license   Apache-2.0
 *
 * Copyright BoxBilling, Inc
 * This source file is subject to the Apache-2.0 License that is bundled
 * with this source code in the file LICENSE
 */


$di = new Box_Di();
$di['config'] = function() {
    $array = include BB_PATH_ROOT . '/bb-config.php';
    return new Box_Config($array);
};
$di['logger'] = function () use ($di) {
    $logFile = $di['config']['path_logs'];
    $writer  = new Box_LogStream($logFile);
    $log     = new Box_Log();
    $log->addWriter($writer);

    $log_to_db = isset($di['config']['log_to_db']) && $di['config']['log_to_db'];
    if ($log_to_db) {
        $activity_service = $di['mod_service']('activity');
        $writer2          = new Box_LogDb($activity_service);
        if ($di['auth']->isAdminLoggedIn()){
                $admin = $di['loggedin_admin'];
                $log->setEventItem('admin_id', $admin->id);
        }
        elseif ($di['auth']->isClientLoggedIn()) {
            $client = $di['loggedin_client'];
            $log->setEventItem('client_id', $client->id);
        }
        $log->addWriter($writer2);
    }

    return $log;
};
$di['crypt'] = function() use ($di) {
    $crypt =  new Box_Crypt();
    $crypt->setDi($di);
    return $crypt;
};
$di['pdo'] = function() use ($di) {
    $c = $di['config']['db'];

    $pdo = new PDO($c['type'].':host='.$c['host'].';dbname='.$c['name'],
        $c['user'],
        $c['password'],
        array(
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY         => true,
            PDO::ATTR_ERRMODE                          => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE               => PDO::FETCH_ASSOC,
        )
    );

    if(isset($c['debug']) && $c['debug']) {
        $pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array ('Box_DbLoggedPDOStatement'));
    }

    if($c['type'] == 'mysql') {
        $pdo->exec( 'SET NAMES "utf8"' );
        $pdo->exec( 'SET CHARACTER SET utf8' );
        $pdo->exec( 'SET CHARACTER_SET_CONNECTION = utf8' );
        $pdo->exec( 'SET CHARACTER_SET_DATABASE = utf8' );
        $pdo->exec( 'SET character_set_results = utf8' );
        $pdo->exec( 'SET character_set_server = utf8' );
        $pdo->exec( 'SET SESSION interactive_timeout = 28800' );
        $pdo->exec( 'SET SESSION wait_timeout = 28800' );
    }

    return $pdo;
};
$di['db'] = function() use ($di) {

    require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rb.php';
    R::setup($di['pdo']);

    $helper = new Box_BeanHelper();
    $helper->setDi($di);

    $mapper = new \RedBeanPHP\Facade();
    $mapper->getRedBean()->setBeanHelper($helper);
    $freeze = isset($di['config']['db']['freeze']) ? (bool)$di['config']['db']['freeze'] : true;
    $mapper->freeze($freeze);

    $db = new Box_Database();
    $db->setDi($di);
    $db->setDataMapper($mapper);
    return $db;
};
$di['pager'] = function() use($di) {
    $service = new Box_Pagination();
    $service->setDi($di);
    return $service;
};
$di['url'] = function() use ($di) {
    $url  = new Box_Url();
    $url->setDi($di);
    $url->setBaseUri(BB_URL);
    return $url;
};
$di['mod'] = $di->protect(function ($name) use($di) {
    $mod = new Box_Mod($name);
    $mod->setDi($di);
    return $mod;
});
$di['mod_service'] = $di->protect(function ($mod, $sub = '') use($di) {
    return $di['mod']($mod)->getService($sub);
});
$di['mod_config'] = $di->protect(function ($name) use($di) {
    return $di['mod']($name)->getConfig();
});
$di['events_manager'] = function() use ($di) {
    $service = new Box_EventManager();
    $service->setDi($di);
    return $service;
};
$di['session'] = function () use ($di) {
    $handler = new PdoSessionHandler($di['pdo']);
    return new Box_Session($handler);
};
$di['cookie'] = function () use ($di) {
    $service = new Box_Cookie();
    $service->setDi($di);
    return $service;
};
$di['request'] = function () use ($di) {
    $service = new Box_Request();
    $service->setDi($di);
    return $service;
};
$di['cache'] = function () use ($di) { return new FileCache();};
$di['auth'] = function () use ($di) { return new Box_Authorization($di);};
$di['twig'] = function () use ($di) {
    $config = $di['config'];
    $options = $config['twig'];

    $loader = new Twig\Loader\ArrayLoader();
    $twig = new Twig\Environment($loader, $options);

    $box_extensions = new Box_TwigExtensions();
    $box_extensions->setDi($di);

      //$twig->addExtension(new Twig\Extension\OptimizerExtension());
      $twig->addExtension(new \Twig\Extension\StringLoaderExtension());
      $twig->addExtension(new Twig\Extension\DebugExtension());
      $twig->addExtension(new Twig\Extensions\I18nExtension());
    $twig->addExtension($box_extensions);
      $twig->getExtension(Twig\Extension\CoreExtension::class)->setDateFormat($config['locale_date_format']);
      $twig->getExtension(Twig\Extension\CoreExtension::class)->setTimezone($config['timezone']);
  

    // add globals
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
        $_GET['ajax'] = TRUE;
    }

    $twig->addGlobal('request', $_GET);
    $twig->addGlobal('guest', $di['api_guest']);

    return $twig;
};

$di['is_client_logged'] = function() use($di) {
    if(!$di['auth']->isClientLoggedIn()) {
        $url = $di['url']->link('login');
        header("Location: $url");  
        throw new Exception('Client is not logged in');<commit_msg>Create twig instances with pimple factory<commit_after><?php
/**
 * BoxBilling
 *
 * @copyright BoxBilling, Inc (http://www.boxbilling.com)
 * @license   Apache-2.0
 *
 * Copyright BoxBilling, Inc
 * This source file is subject to the Apache-2.0 License that is bundled
 * with this source code in the file LICENSE
 */


$di = new Box_Di();
$di['config'] = function() {
    $array = include BB_PATH_ROOT . '/bb-config.php';
    return new Box_Config($array);
};
$di['logger'] = function () use ($di) {
    $logFile = $di['config']['path_logs'];
    $writer  = new Box_LogStream($logFile);
    $log     = new Box_Log();
    $log->addWriter($writer);

    $log_to_db = isset($di['config']['log_to_db']) && $di['config']['log_to_db'];
    if ($log_to_db) {
        $activity_service = $di['mod_service']('activity');
        $writer2          = new Box_LogDb($activity_service);
        if ($di['auth']->isAdminLoggedIn()){
                $admin = $di['loggedin_admin'];
                $log->setEventItem('admin_id', $admin->id);
        }
        elseif ($di['auth']->isClientLoggedIn()) {
            $client = $di['loggedin_client'];
            $log->setEventItem('client_id', $client->id);
        }
        $log->addWriter($writer2);
    }

    return $log;
};
$di['crypt'] = function() use ($di) {
    $crypt =  new Box_Crypt();
    $crypt->setDi($di);
    return $crypt;
};
$di['pdo'] = function() use ($di) {
    $c = $di['config']['db'];

    $pdo = new PDO($c['type'].':host='.$c['host'].';dbname='.$c['name'],
        $c['user'],
        $c['password'],
        array(
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY         => true,
            PDO::ATTR_ERRMODE                          => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE               => PDO::FETCH_ASSOC,
        )
    );

    if(isset($c['debug']) && $c['debug']) {
        $pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array ('Box_DbLoggedPDOStatement'));
    }

    if($c['type'] == 'mysql') {
        $pdo->exec( 'SET NAMES "utf8"' );
        $pdo->exec( 'SET CHARACTER SET utf8' );
        $pdo->exec( 'SET CHARACTER_SET_CONNECTION = utf8' );
        $pdo->exec( 'SET CHARACTER_SET_DATABASE = utf8' );
        $pdo->exec( 'SET character_set_results = utf8' );
        $pdo->exec( 'SET character_set_server = utf8' );
        $pdo->exec( 'SET SESSION interactive_timeout = 28800' );
        $pdo->exec( 'SET SESSION wait_timeout = 28800' );
    }

    return $pdo;
};
$di['db'] = function() use ($di) {

    require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rb.php';
    R::setup($di['pdo']);

    $helper = new Box_BeanHelper();
    $helper->setDi($di);

    $mapper = new \RedBeanPHP\Facade();
    $mapper->getRedBean()->setBeanHelper($helper);
    $freeze = isset($di['config']['db']['freeze']) ? (bool)$di['config']['db']['freeze'] : true;
    $mapper->freeze($freeze);

    $db = new Box_Database();
    $db->setDi($di);
    $db->setDataMapper($mapper);
    return $db;
};
$di['pager'] = function() use($di) {
    $service = new Box_Pagination();
    $service->setDi($di);
    return $service;
};
$di['url'] = function() use ($di) {
    $url  = new Box_Url();
    $url->setDi($di);
    $url->setBaseUri(BB_URL);
    return $url;
};
$di['mod'] = $di->protect(function ($name) use($di) {
    $mod = new Box_Mod($name);
    $mod->setDi($di);
    return $mod;
});
$di['mod_service'] = $di->protect(function ($mod, $sub = '') use($di) {
    return $di['mod']($mod)->getService($sub);
});
$di['mod_config'] = $di->protect(function ($name) use($di) {
    return $di['mod']($name)->getConfig();
});
$di['events_manager'] = function() use ($di) {
    $service = new Box_EventManager();
    $service->setDi($di);
    return $service;
};
$di['session'] = function () use ($di) {
    $handler = new PdoSessionHandler($di['pdo']);
    return new Box_Session($handler);
};
$di['cookie'] = function () use ($di) {
    $service = new Box_Cookie();
    $service->setDi($di);
    return $service;
};
$di['request'] = function () use ($di) {
    $service = new Box_Request();
    $service->setDi($di);
    return $service;
};
$di['cache'] = function () use ($di) { return new FileCache();};
$di['auth'] = function () use ($di) { return new Box_Authorization($di);};
$di['twig'] = $di->factory(function () use ($di) {
    $config = $di['config'];
    $options = $config['twig'];

    $loader = new Twig\Loader\ArrayLoader();
    $twig = new Twig\Environment($loader, $options);

    $box_extensions = new Box_TwigExtensions();
    $box_extensions->setDi($di);

      //$twig->addExtension(new Twig\Extension\OptimizerExtension());
      $twig->addExtension(new \Twig\Extension\StringLoaderExtension());
      $twig->addExtension(new Twig\Extension\DebugExtension());
      $twig->addExtension(new Twig\Extensions\I18nExtension());
    $twig->addExtension($box_extensions);
      $twig->getExtension(Twig\Extension\CoreExtension::class)->setDateFormat($config['locale_date_format']);
      $twig->getExtension(Twig\Extension\CoreExtension::class)->setTimezone($config['timezone']);
  

    // add globals
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
        $_GET['ajax'] = TRUE;
    }

    $twig->addGlobal('request', $_GET);
    $twig->addGlobal('guest', $di['api_guest']);

    return $twig;
});

$di['is_client_logged'] = function() use($di) {
    if(!$di['auth']->isClientLoggedIn()) {
        $url = $di['url']->link('login');
        header("Location: $url");  
        throw new Exception('Client is not logged in');
{'fraction_non_alphanumeric': 0.20955551556595287, 'fraction_numerical': 0.004318877091956092, 'mean_word_length': 3.363957597173145, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7097857', 'n_tokens_mistral': 3880, 'n_tokens_neox': 3808, 'n_words': 678}