Refactoring template constructor.

skala
Julio Montoya 12 years ago
parent bdaf478306
commit 99fc79538b
  1. 76
      main/inc/global.inc.php
  2. 22
      main/inc/lib/template.lib.php
  3. 70
      main/inc/services.php

@ -45,7 +45,6 @@ $configurationYMLFile = $includePath.'/../../config/configuration.yml';
$configurationFileAppPath = $includePath.'/../../config/configuration.php';
$alreadyInstalled = false;
$_configuration = array();
if (file_exists($configurationFilePath) || file_exists($configurationYMLFile) || file_exists($configurationFileAppPath)) {
@ -135,7 +134,6 @@ if (isset($urlInfo['path'])) {
}
$libPath = $includePath.'/lib/';
$langPath = api_get_path(SYS_LANG_PATH);
// Database constants
require_once $libPath.'database.constants.inc.php';
@ -144,10 +142,10 @@ require_once $libPath.'database.constants.inc.php';
require_once $libPath.'events.lib.inc.php';
// Load allowed tag definitions for kses and/or HTMLPurifier.
require_once $libPath.'formvalidator/Rule/allowed_tags.inc.php';
//require_once $libPath.'formvalidator/Rule/allowed_tags.inc.php';
// Add the path to the pear packages to the include path
ini_set('include_path', api_create_include_path_setting());
ini_set('include_path', api_create_include_path_setting($includePath));
$app['configuration_file'] = $configurationFilePath;
$app['configuration_yml_file'] = $configurationYMLFile;
@ -212,25 +210,6 @@ if ($alreadyInstalled) {
/** Including service providers */
require_once 'services.php';
// Setting the static database class
$database = isset($app['database']) ? $app['database'] : null;
// Retrieving all the chamilo config settings for multiple URLs feature
$_configuration['access_url'] = 1;
if (api_get_multiple_access_url()) {
$access_urls = api_get_access_urls();
$protocol = ((!empty($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) != 'OFF') ? 'https' : 'http').'://';
$request_url1 = $protocol.$_SERVER['SERVER_NAME'].'/';
$request_url2 = $protocol.$_SERVER['HTTP_HOST'].'/';
foreach ($access_urls as & $details) {
if ($request_url1 == $details['url'] or $request_url2 == $details['url']) {
$_configuration['access_url'] = $details['id'];
}
}
}
}
$charset = 'UTF-8';
@ -257,6 +236,9 @@ $app->error(
} else {
$code = null;
}
Session::setSession($app['session']);
//$code = ($e instanceof HttpException) ? $e->getStatusCode() : 500;
// It seems that error() is executed first than the before() middleware
// @ŧodo check this one
@ -267,13 +249,15 @@ $app->error(
if (!is_dir($app['sys_root'].'main/template/'.$templateStyle)) {
$templateStyle = 'default';
}
$app['template_style'] = $templateStyle;
// Default layout.
$app['default_layout'] = $app['template_style'].'/layout/layout_1_col.tpl';
$app['template']->assign('error', array('code' => $code, 'message' => $message));
$response = $app['template']->render_layout('error.tpl');
/** @var Template $template */
$template = $app['template'];
$template->assign('error', array('code' => $code, 'message' => $message));
$response = $template->render_layout('error.tpl');
return new Response($response);
}
);
@ -512,7 +496,6 @@ $app->before(
$request->getSession()->start();
// Setting session obj
Session::setSession($app['session']);
UserManager::setEntityManager($app['orm.em']);
@ -529,14 +512,27 @@ $app->before(
$filesystem->copyFolders($app['temp.paths']->copyFolders);
}
// Check and modify the date of user in the track.e.online table
Online::loginCheck(api_get_user_id());
// Setting access_url id (multiple url feature)
if (api_get_multiple_access_url()) {
//for some reason $app['configuration'] doesn't work. Use $_config
global $_configuration;
$_configuration = $app['configuration'];
$_configuration['access_url'] = 1;
$access_urls = api_get_access_urls();
$protocol = ((!empty($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) != 'OFF') ? 'https' : 'http').'://';
$request_url1 = $protocol.$_SERVER['SERVER_NAME'].'/';
$request_url2 = $protocol.$_SERVER['HTTP_HOST'].'/';
foreach ($access_urls as & $details) {
if ($request_url1 == $details['url'] or $request_url2 == $details['url']) {
$_configuration['access_url'] = $details['id'];
}
}
Session::write('url_id', $_configuration['access_url']);
Session::write('url_info', api_get_current_access_url_info($_configuration['access_url']));
} else {
@ -602,10 +598,12 @@ $app->before(
$user = null;
/** Security component. */
if ($app['security']->isGranted('IS_AUTHENTICATED_FULLY')) {
/** @var Symfony\Component\Security\Core\SecurityContext $security */
$security = $app['security'];
if ($security->isGranted('IS_AUTHENTICATED_FULLY')) {
// Checking token in order to get the current user.
$token = $app['security']->getToken();
$token = $security->getToken();
if (null !== $token) {
/** @var Entity\User $user */
$user = $token->getUser();
@ -619,12 +617,12 @@ $app->before(
$app['current_user'] = $userInfo;
// Setting admin permissions.
if ($app['security']->isGranted('ROLE_ADMIN')) {
if ($security->isGranted('ROLE_ADMIN')) {
Session::write('is_platformAdmin', true);
}
// Setting teachers permissions.
if ($app['security']->isGranted('ROLE_TEACHER')) {
if ($security->isGranted('ROLE_TEACHER')) {
Session::write('is_allowedCreateCourse', true);
}
@ -639,14 +637,17 @@ $app->before(
$language = api_get_setting('platformLanguage');
$iso = api_get_language_isocode($language);
$app['translator']->setLocale($iso);
/** @var Symfony\Component\Translation\Translator $translator */
$translator = $app['translator'];
$translator->setLocale($iso);
// From the login page
$language = $request->get('language');
if (!empty($language)) {
$iso = api_get_language_isocode($language);
$app['translator']->setLocale($iso);
$translator->setLocale($iso);
}
// From the user
@ -655,14 +656,14 @@ $app->before(
//$language = $user->getLanguage();
$language = $userInfo['language'];
$iso = api_get_language_isocode($language);
$app['translator']->setLocale($iso);
$translator->setLocale($iso);
}
// From the course
$courseInfo = api_get_course_info();
if ($courseInfo && !empty($courseInfo)) {
$iso = api_get_language_isocode($courseInfo['language']);
$app['translator']->setLocale($iso);
$translator->setLocale($iso);
}
$file = $request->get('file');
@ -734,6 +735,7 @@ $app->before(
$app['template']->assign('course_session', $session);
}
}
);

@ -28,19 +28,17 @@ class Template
public $force_plugin_load = true;
public $navigation_array;
public $loadBreadcrumb = true;
/** @var Symfony\Component\Security\Core\SecurityContext */
private $security;
/**
* @param Application $app
*/
public function __construct(Application $app = null)
public function __construct(Application $app, $database, $security)
{
if (empty($app)) {
global $app;
$this->app = &$app;
} else {
//ugly fix just for now
$this->app = &$app;
}
$this->app = &$app;
$this->security = $security;
$this->app['classic_layout'] = true;
$this->navigation_array = $this->returnNavigationArray();
@ -433,6 +431,9 @@ class Template
}
}
/**
* @param array $htmlHeadXtra
*/
public function addJsFiles($htmlHeadXtra = array())
{
$extra_headers = null;
@ -988,12 +989,9 @@ class Template
// Adding block settings for each role
if (isset($this->app['allow_admin_toolbar'])) {
/** @var \Symfony\Component\Security\Core\SecurityContext $security */
$security = $this->app['security'];
$roleTemplate = array();
foreach ($this->app['allow_admin_toolbar'] as $role) {
if ($security->isGranted($role)) {
if ($this->security->getToken() && $this->security->isGranted($role)) {
// Fixes in order to match the templates
if ($role == 'ROLE_ADMIN') {
$role = 'administrator';

@ -61,33 +61,24 @@ $app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
'http_cache.cache_dir' => $app['http_cache.cache_dir'].'/',
));*/
// http://symfony.com/doc/master/reference/configuration/security.html
class SecurityServiceProvider extends \Silex\Provider\SecurityServiceProvider
{
public function addFakeRoute($method, $pattern, $name)
{
// Don't do anything otherwise the closures will be dumped and that leads to fatal errors.
}
}
$app->register(new SecurityServiceProvider, array(
$app->register(new \Silex\Provider\SecurityServiceProvider, array(
'security.firewalls' => array(
'login' => array(
'pattern' => '^/login$',
'anonymous' => true
'anonymous' => true,
'security' => false
),
'secured' => array(
'pattern' => '^/.*$',
'form' => array(
'login_path' => '/login',
'check_path' => '/admin/login_check',
'check_path' => '/secured/login_check',
'default_target_path' => '/userportal',
'username_parameter' => 'username',
'password_parameter' => 'password',
),
'logout' => array(
'logout_path' => '/admin/logout',
'logout_path' => '/secured/logout',
'target' => '/'
),
'users' => $app->share(function() use ($app) {
@ -144,21 +135,16 @@ $app['security.role_hierarchy'] = array(
// Role rules
$app['security.access_rules'] = array(
//array('^/admin', 'ROLE_ADMIN', 'https'),
array('^/admin/administrator', 'ROLE_ADMIN'),
//array('^/main/admin/extra_fields.php', 'ROLE_QUESTION_MANAGER'),
//array('^/main/admin/extra_field_options.php', 'ROLE_QUESTION_MANAGER'),
//array('^/main/admin/extra_field_workflow.php', 'ROLE_QUESTION_MANAGER'),
array('^/main/admin/.*', 'ROLE_ADMIN'),
array('^/admin/questionmanager', 'ROLE_QUESTION_MANAGER'),
array('^/main/auth/inscription.php', 'IS_AUTHENTICATED_ANONYMOUSLY'),
array('^/main/auth/lostPassword.php', 'IS_AUTHENTICATED_ANONYMOUSLY'),
array('^/main/.*', array('ROLE_STUDENT')),
array('^/courses/.*/curriculum/category', 'ROLE_TEACHER'),
array('^/courses/.*/curriculum/item', 'ROLE_TEACHER'),
array('^/courses/.*/curriculum/user', 'ROLE_STUDENT'),
array('^/courses/.*/curriculum', 'ROLE_STUDENT'),
//array('^.*$', 'ROLE_USER'),
//array('^/main/.*', array('ROLE_STUDENT')),
);
// Roles that have an admin toolbar
@ -168,6 +154,28 @@ $app['allow_admin_toolbar'] = array(
'ROLE_SESSION_MANAGER'
);
use SilexOpauth\OpauthExtension;
$app['opauth'] = array(
'login' => '/auth/login',
'callback' => '/auth/callback',
'config' => array(
'security_salt' => 'LDFmiilYf8Fyw5W10rxx4W1KsVrieQCnpBzzpTBWA5vJidQKDx8pMJbmw28R1C4m',
'Strategy' => array(
'Facebook' => array(
'app_id' => 'APP_ID',
'app_secret' => 'APP_SECRET'
),
'OpenID' => array(),
'Google' => array(
'client_id' => '789282956155.apps.googleusercontent.com',
'client_secret' => 'MKoR9EWis_JtwJj4GAL1mZkv'
)
)
)
);
$app->register(new OpauthExtension());
/*
$app['security.access_manager'] = $app->share(function($app) {
return new AccessDecisionManager($app['security.voters'], 'unanimous');
@ -335,8 +343,8 @@ $app->register(
new Silex\Provider\TwigServiceProvider(),
array(
'twig.path' => array(
api_get_path(SYS_CODE_PATH).'template', //template folder
api_get_path(SYS_PLUGIN_PATH) //plugin folder
$app['sys_root'].'main/template', //template folder
$app['sys_root'].'plugin' //plugin folder
),
// twitter bootstrap form twig templates
'twig.form.templates' => array('form_div_layout.html.twig', 'default/form/form_custom_template.tpl'),
@ -492,7 +500,6 @@ if ($app['assetic.enabled']) {
));
$am->get('styles')->setTargetPath($app['assetic.output.path_to_css']);
$am->set('scripts', new Assetic\Asset\AssetCache(
new Assetic\Asset\GlobAsset($app['assetic.input.path_to_js']),
new Assetic\Cache\FilesystemCache($app['assetic.path_to_cache'])
@ -523,12 +530,21 @@ class ChamiloServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
// Database.
$app['database'] = $app->share(function () use ($app) {
$db = new Database($app['db'], $app['dbs']);
return $db;
});
$database = $app['database'];
// Template class
$app['template'] = $app->share(function () use ($app) {
$template = new Template($app);
$template = new Template($app, $app['database'], $app['security']);
return $template;
});
// Paths
$app['paths'] = $app->share(function () use ($app) {
return array(
//'root_web' => $app['root_web'],
@ -558,12 +574,6 @@ class ChamiloServiceProvider implements ServiceProviderInterface
$mailGenerator = new ChamiloLMS\Component\Mail\MailGenerator($app['twig'], $app['mailer']);
return $mailGenerator;
});
// Database.
$app['database'] = $app->share(function () use ($app) {
$db = new Database($app['db'], $app['dbs']);
return $db;
});
}
public function boot(Application $app)

Loading…
Cancel
Save