services.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file includes all the services that are loaded via the ServiceProviderInterface
  5. *
  6. * @package chamilo.services
  7. */
  8. use Silex\Application;
  9. use Silex\ServiceProviderInterface;
  10. // Monolog.
  11. if (is_writable($app['sys_temp_path'])) {
  12. /**
  13. * Adding Monolog service provider.
  14. * Examples:
  15. * $app['monolog']->addDebug('Testing the Monolog logging.');
  16. * $app['monolog']->addInfo('Testing the Monolog logging.');
  17. * $app['monolog']->addError('Testing the Monolog logging.');
  18. */
  19. if ($app['debug']) {
  20. $app->register(
  21. new Silex\Provider\MonologServiceProvider(),
  22. array(
  23. 'monolog.logfile' => $app['chamilo.log'],
  24. 'monolog.name' => 'chamilo',
  25. )
  26. );
  27. }
  28. }
  29. //Setting HttpCacheService provider in order to use do: $app['http_cache']->run();
  30. /*
  31. $app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
  32. 'http_cache.cache_dir' => $app['http_cache.cache_dir'].'/',
  33. ));*/
  34. // http://symfony.com/doc/master/reference/configuration/security.html
  35. $app->register(new Silex\Provider\SecurityServiceProvider(), array(
  36. 'security.firewalls' => array(
  37. 'login' => array(
  38. 'pattern' => '^/login$',
  39. 'anonymous' => true
  40. ),
  41. 'admin' => array(
  42. //'http' => true,
  43. 'pattern' => '^/.*$',
  44. 'form' => array(
  45. 'login_path' => '/login',
  46. 'check_path' => '/admin/login_check',
  47. 'default_target_path' => '/userportal',
  48. 'username_parameter' => 'username',
  49. 'password_parameter' => 'password',
  50. ),
  51. 'logout' => array(
  52. 'logout_path' => '/admin/logout',
  53. 'target' => '/'
  54. ),
  55. 'users' => $app->share(function() use ($app) {
  56. return $app['orm.em']->getRepository('Entity\User');
  57. }),
  58. 'anonymous' => true
  59. ),/*
  60. 'classic' => array(
  61. 'pattern' => '^/.*$'
  62. )*/
  63. )
  64. ));
  65. // Registering Password encoder.
  66. use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
  67. $app['security.encoder.digest'] = $app->share(function($app) {
  68. // use the sha1 algorithm
  69. // don't base64 encode the password
  70. // use only 1 iteration
  71. return new MessageDigestPasswordEncoder($app['configuration']['password_encryption'], false, 1);
  72. });
  73. // What to do when login success?
  74. $app['security.authentication.success_handler.admin'] = $app->share(function($app) {
  75. return new ChamiloLMS\Component\Auth\LoginSuccessHandler($app['url_generator'], $app['security']);
  76. });
  77. // What to do when logout?
  78. $app['security.authentication.logout_handler.admin'] = $app->share(function($app) {
  79. return new ChamiloLMS\Component\Auth\LogoutSuccessHandler($app['url_generator'], $app['security']);
  80. });
  81. // Role hierarchy
  82. $app['security.role_hierarchy'] = array(
  83. 'ROLE_ADMIN' => array('ROLE_QUESTION_MANAGER', 'ROLE_SESSION_MANAGER', 'ROLE_TEACHER', 'ROLE_ALLOWED_TO_SWITCH', 'ROLE_DIRECTOR'),
  84. 'ROLE_RRHH' => array('ROLE_TEACHER'),
  85. 'ROLE_TEACHER' => array('ROLE_STUDENT'),
  86. 'ROLE_QUESTION_MANAGER' => array('ROLE_STUDENT', 'ROLE_QUESTION_MANAGER'),
  87. 'ROLE_SESSION_MANAGER' => array('ROLE_STUDENT', 'ROLE_SESSION_MANAGER'),
  88. 'ROLE_STUDENT' => array('ROLE_STUDENT'),
  89. 'ROLE_ANONYMOUS' => array('ROLE_ANONYMOUS')
  90. );
  91. // Role rules
  92. $app['security.access_rules'] = array(
  93. //array('^/admin', 'ROLE_ADMIN', 'https'),
  94. array('^/admin/administrator', 'ROLE_ADMIN'),
  95. //array('^/main/admin/extra_fields.php', 'ROLE_QUESTION_MANAGER'),
  96. //array('^/main/admin/extra_field_options.php', 'ROLE_QUESTION_MANAGER'),
  97. //array('^/main/admin/extra_field_workflow.php', 'ROLE_QUESTION_MANAGER'),
  98. array('^/main/admin/.*', 'ROLE_ADMIN'),
  99. array('^/admin/questionmanager', 'ROLE_QUESTION_MANAGER'),
  100. array('^/main/auth/inscription.php', 'IS_AUTHENTICATED_ANONYMOUSLY'),
  101. array('^/main/auth/lostPassword.php', 'IS_AUTHENTICATED_ANONYMOUSLY'),
  102. array('^/main/.*', array('ROLE_STUDENT')),
  103. //array('^.*$', 'ROLE_USER'),
  104. );
  105. /**
  106. $app['security.access_manager'] = $app->share(function($app) {
  107. return new AccessDecisionManager($app['security.voters'], 'unanimous');
  108. });*/
  109. // Setting Controllers as services provider.
  110. $app->register(new Silex\Provider\ServiceControllerServiceProvider());
  111. // Validator provider.
  112. $app->register(new Silex\Provider\ValidatorServiceProvider());
  113. // Implements Symfony2 translator.
  114. $app->register(new Silex\Provider\TranslationServiceProvider(), array(
  115. 'locale' => 'en',
  116. 'locale_fallback' => 'en'
  117. ));
  118. // Form provider
  119. $app->register(new Silex\Provider\FormServiceProvider());
  120. // URL generator provider
  121. $app->register(new Silex\Provider\UrlGeneratorServiceProvider());
  122. // Needed to use the "entity" option in symfony forms
  123. use Doctrine\Common\Persistence\AbstractManagerRegistry;
  124. class ManagerRegistry extends AbstractManagerRegistry
  125. {
  126. protected $container;
  127. protected function getService($name)
  128. {
  129. return $this->container[$name];
  130. }
  131. protected function resetService($name)
  132. {
  133. unset($this->container[$name]);
  134. }
  135. public function getAliasNamespace($alias)
  136. {
  137. throw new \BadMethodCallException('Namespace aliases not supported.');
  138. }
  139. public function setContainer(Application $container)
  140. {
  141. $this->container = $container;
  142. }
  143. }
  144. $app['form.extensions'] = $app->share($app->extend('form.extensions', function ($extensions, $app) {
  145. $managerRegistry = new ManagerRegistry(null, array('db'), array('orm.em'), null, null, $app['orm.proxies_namespace']);
  146. $managerRegistry->setContainer($app);
  147. $extensions[] = new \Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($managerRegistry);
  148. return $extensions;
  149. }));
  150. // Setting Doctrine service provider (DBAL)
  151. if (isset($app['configuration']['main_database'])) {
  152. /* The database connection can be overwritten if you set $_configuration['db.options']
  153. in configuration.php like this : */
  154. $defaultDatabaseOptions = array(
  155. 'db_read' => array(
  156. 'driver' => 'pdo_mysql',
  157. 'host' => $app['configuration']['db_host'],
  158. 'dbname' => $app['configuration']['main_database'],
  159. 'user' => $app['configuration']['db_user'],
  160. 'password' => $app['configuration']['db_password'],
  161. 'charset' => 'utf8',
  162. //'priority' => '1'
  163. ),
  164. 'db_write' => array(
  165. 'driver' => 'pdo_mysql',
  166. 'host' => $app['configuration']['db_host'],
  167. 'dbname' => $app['configuration']['main_database'],
  168. 'user' => $app['configuration']['db_user'],
  169. 'password' => $app['configuration']['db_password'],
  170. 'charset' => 'utf8',
  171. //'priority' => '2'
  172. ),
  173. );
  174. // Could be set in the $_configuration array
  175. if (isset($app['configuration']['db.options'])) {
  176. $defaultDatabaseOptions = $app['configuration']['db.options'];
  177. }
  178. $app->register(
  179. new Silex\Provider\DoctrineServiceProvider(),
  180. array(
  181. 'dbs.options' => $defaultDatabaseOptions
  182. )
  183. );
  184. $mappings = array(
  185. array(
  186. /* If true, only simple notations like @Entity will work.
  187. If false, more advanced notations and aliasing via use will work.
  188. (Example: use Doctrine\ORM\Mapping AS ORM, @ORM\Entity)*/
  189. 'use_simple_annotation_reader' => false,
  190. 'type' => 'annotation',
  191. 'namespace' => 'Entity',
  192. 'path' => api_get_path(INCLUDE_PATH).'Entity',
  193. // 'orm.default_cache' =>
  194. ),
  195. array(
  196. 'use_simple_annotation_reader' => false,
  197. 'type' => 'annotation',
  198. 'namespace' => 'Gedmo',
  199. 'path' => api_get_path(SYS_PATH).'vendors/gedmo/doctrine-extensions/lib/Gedmo',
  200. )
  201. );
  202. // Setting Doctrine ORM.
  203. $app->register(
  204. new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider,
  205. array(
  206. // Doctrine2 ORM cache
  207. /*'orm.default_cache' => 'apc', // array, apc, xcache, memcache, memcached
  208. 'metadata_cache' => 'apc',
  209. 'result_cache' => 'apc',*/
  210. // Proxies
  211. 'orm.auto_generate_proxies' => true,
  212. 'orm.proxies_dir' => $app['db.orm.proxies_dir'],
  213. 'orm.proxies_namespace' => 'Doctrine\ORM\Proxy\Proxy',
  214. 'orm.ems.default' => 'db_read',
  215. 'orm.ems.options' => array(
  216. 'db_read' => array(
  217. 'connection' => 'db_read',
  218. 'mappings' => $mappings,
  219. ),
  220. 'db_write' => array(
  221. 'connection' => 'db_write',
  222. 'mappings' => $mappings,
  223. ),
  224. ),
  225. )
  226. );
  227. }
  228. // Setting Twig as a service provider.
  229. $app->register(
  230. new Silex\Provider\TwigServiceProvider(),
  231. array(
  232. 'twig.path' => array(
  233. api_get_path(SYS_CODE_PATH).'template', //template folder
  234. api_get_path(SYS_PLUGIN_PATH) //plugin folder
  235. ),
  236. // twitter bootstrap form twig templates
  237. 'twig.form.templates' => array('form_div_layout.html.twig', 'default/form/form_custom_template.tpl'),
  238. 'twig.options' => array(
  239. 'debug' => $app['debug'],
  240. 'charset' => 'utf-8',
  241. 'strict_variables' => false,
  242. 'autoescape' => false,
  243. 'cache' => $app['debug'] ? false : $app['twig.cache.path'],
  244. 'optimizations' => -1, // turn on optimizations with -1
  245. )
  246. )
  247. );
  248. // Setting Twig options
  249. $app['twig'] = $app->share(
  250. $app->extend('twig', function ($twig) {
  251. $twig->addFilter('get_lang', new Twig_Filter_Function('get_lang'));
  252. $twig->addFilter('get_path', new Twig_Filter_Function('api_get_path'));
  253. $twig->addFilter('get_setting', new Twig_Filter_Function('api_get_setting'));
  254. $twig->addFilter('var_dump', new Twig_Filter_Function('var_dump'));
  255. $twig->addFilter('return_message', new Twig_Filter_Function('Display::return_message_and_translate'));
  256. $twig->addFilter('display_page_header', new Twig_Filter_Function('Display::page_header_and_translate'));
  257. $twig->addFilter(
  258. 'display_page_subheader',
  259. new Twig_Filter_Function('Display::page_subheader_and_translate')
  260. );
  261. $twig->addFilter('icon', new Twig_Filter_Function('Template::get_icon_path'));
  262. $twig->addFilter('format_date', new Twig_Filter_Function('Template::format_date'));
  263. return $twig;
  264. })
  265. );
  266. // Developer tools.
  267. if (is_writable($app['sys_temp_path'])) {
  268. if ($app['show_profiler']) {
  269. // Adding Symfony2 web profiler (memory, time, logs, etc)
  270. $app->register(
  271. $p = new Silex\Provider\WebProfilerServiceProvider(),
  272. array(
  273. 'profiler.cache_dir' => $app['profiler.cache_dir'],
  274. )
  275. );
  276. $app->mount('/_profiler', $p);
  277. // PHP errors for cool kids
  278. // $app->register(new Whoops\Provider\Silex\WhoopsServiceProvider);
  279. }
  280. }
  281. // Pagerfanta settings (Pagination using Doctrine2, arrays, etc)
  282. use FranMoreno\Silex\Provider\PagerfantaServiceProvider;
  283. $app->register(new PagerfantaServiceProvider());
  284. // Custom route params see https://github.com/franmomu/silex-pagerfanta-provider/pull/2
  285. //$app['pagerfanta.view.router.name']
  286. //$app['pagerfanta.view.router.params']
  287. $app['pagerfanta.view.options'] = array(
  288. 'routeName' => null,
  289. 'routeParams' => array(),
  290. 'pageParameter' => '[page]',
  291. 'proximity' => 3,
  292. 'next_message' => '&raquo;',
  293. 'prev_message' => '&laquo;',
  294. 'default_view' => 'twitter_bootstrap' // the pagination style
  295. );
  296. // Registering Menu service provider (too gently creating menus with the URLgenerator provider)
  297. $app->register(new \Knp\Menu\Silex\KnpMenuServiceProvider());
  298. // @todo use a app['image_processor'] setting
  299. define('IMAGE_PROCESSOR', 'gd'); // imagick or gd strings
  300. // Setting the Imagine service provider to deal with image transformations used in social group.
  301. $app->register(new Grom\Silex\ImagineServiceProvider(), array(
  302. 'imagine.factory' => 'Gd'
  303. ));
  304. // Prompts Doctrine SQL queries using Monolog.
  305. $app['dbal_logger'] = $app->share(function() {
  306. //return new Doctrine\DBAL\Logging\DebugStack();
  307. });
  308. if ($app['debug']) {
  309. /*$logger = $app['dbal_logger'];
  310. $app['db.config']->setSQLLogger($logger);
  311. $app->after(function() use ($app, $logger) {
  312. // Log all queries as DEBUG.
  313. foreach ($logger->queries as $query) {
  314. $app['monolog']->debug(
  315. $query['sql'],
  316. array(
  317. 'params' => $query['params'],
  318. 'types' => $query['types'],
  319. 'executionMS' => $query['executionMS']
  320. )
  321. );
  322. }
  323. });*/
  324. }
  325. // Email service provider.
  326. $app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
  327. 'swiftmailer.options' => array(
  328. 'host' => isset($platform_email['SMTP_HOST']) ? $platform_email['SMTP_HOST'] : null,
  329. 'port' => isset($platform_email['SMTP_PORT']) ? $platform_email['SMTP_PORT'] : null,
  330. 'username' => isset($platform_email['SMTP_USER']) ? $platform_email['SMTP_USER'] : null,
  331. 'password' => isset($platform_email['SMTP_PASS']) ? $platform_email['SMTP_PASS'] : null,
  332. 'encryption' => null,
  333. 'auth_mode' => null
  334. )
  335. ));
  336. // Mailer
  337. $app['mailer'] = $app->share(function ($app) {
  338. return new \Swift_Mailer($app['swiftmailer.transport']);
  339. });
  340. // Assetic service provider.
  341. if ($app['assetic.enabled']) {
  342. $app->register(new SilexAssetic\AsseticServiceProvider(), array(
  343. 'assetic.options' => array(
  344. 'debug' => $app['debug'],
  345. 'auto_dump_assets' => $app['assetic.auto_dump_assets'],
  346. )
  347. ));
  348. // Less filter
  349. $app['assetic.filter_manager'] = $app->share(
  350. $app->extend('assetic.filter_manager', function($fm, $app) {
  351. $fm->set('lessphp', new Assetic\Filter\LessphpFilter());
  352. return $fm;
  353. })
  354. );
  355. $app['assetic.asset_manager'] = $app->share(
  356. $app->extend('assetic.asset_manager', function($am, $app) {
  357. $am->set('styles', new Assetic\Asset\AssetCache(
  358. new Assetic\Asset\GlobAsset(
  359. $app['assetic.input.path_to_css'],
  360. array($app['assetic.filter_manager']->get('lessphp'))
  361. ),
  362. new Assetic\Cache\FilesystemCache($app['assetic.path_to_cache'])
  363. ));
  364. $am->get('styles')->setTargetPath($app['assetic.output.path_to_css']);
  365. $am->set('scripts', new Assetic\Asset\AssetCache(
  366. new Assetic\Asset\GlobAsset($app['assetic.input.path_to_js']),
  367. new Assetic\Cache\FilesystemCache($app['assetic.path_to_cache'])
  368. ));
  369. $am->get('scripts')->setTargetPath($app['assetic.output.path_to_js']);
  370. return $am;
  371. })
  372. );
  373. }
  374. // Gaufrette service provider (to manage files/dirs) (not used yet)
  375. /*
  376. use Bt51\Silex\Provider\GaufretteServiceProvider\GaufretteServiceProvider;
  377. $app->register(new GaufretteServiceProvider(), array(
  378. 'gaufrette.adapter.class' => 'Local',
  379. 'gaufrette.options' => array(api_get_path(SYS_DATA_PATH))
  380. ));
  381. */
  382. // Use Symfony2 filesystem instead of custom scripts
  383. $app->register(new Neutron\Silex\Provider\FilesystemServiceProvider());
  384. /** Chamilo service provider. */
  385. class ChamiloServiceProvider implements ServiceProviderInterface
  386. {
  387. public function register(Application $app)
  388. {
  389. // Template class
  390. $app['template'] = $app->share(function () use ($app) {
  391. $template = new Template($app);
  392. return $template;
  393. });
  394. $app['paths'] = $app->share(function () use ($app) {
  395. return array(
  396. //'root_web' => $app['root_web'],
  397. 'root_sys' => $app['root_sys'],
  398. 'sys_root' => $app['root_sys'], // just an alias
  399. 'sys_data_path' => $app['sys_data_path'],
  400. 'sys_config_path' => $app['sys_config_path'],
  401. 'sys_temp_path' => $app['sys_temp_path'],
  402. 'sys_log_path' => $app['sys_log_path']
  403. );
  404. });
  405. // Chamilo data filesystem.
  406. $app['chamilo.filesystem'] = $app->share(function () use ($app) {
  407. $filesystem = new ChamiloLMS\Component\DataFilesystem\DataFilesystem($app['paths'], $app['filesystem']);
  408. return $filesystem;
  409. });
  410. // Page controller class.
  411. $app['page_controller'] = $app->share(function () use ($app) {
  412. $pageController = new PageController($app);
  413. return $pageController;
  414. });
  415. // Mail template generator.
  416. $app['mail_generator'] = $app->share(function () use ($app) {
  417. $mailGenerator = new ChamiloLMS\Component\Mail\MailGenerator($app['twig'], $app['mailer']);
  418. return $mailGenerator;
  419. });
  420. // Database.
  421. $app['database'] = $app->share(function () use ($app) {
  422. $db = new Database($app['db'], $app['dbs']);
  423. return $db;
  424. });
  425. }
  426. public function boot(Application $app)
  427. {
  428. }
  429. }
  430. // Registering Chamilo service provider.
  431. $app->register(new ChamiloServiceProvider(), array());
  432. // Controller as services definitions.
  433. /* @todo use Flint to manage controllers, routes */
  434. $app['pages.controller'] = $app->share(
  435. function () use ($app) {
  436. return new PagesController($app['pages.repository']);
  437. }
  438. );
  439. $app['index.controller'] = $app->share(
  440. function () use ($app) {
  441. $controller = new ChamiloLMS\Controller\IndexController($app);
  442. return $controller;
  443. }
  444. );
  445. $app['legacy.controller'] = $app->share(
  446. function () use ($app) {
  447. return new ChamiloLMS\Controller\LegacyController($app);
  448. }
  449. );
  450. $app['userPortal.controller'] = $app->share(
  451. function () use ($app) {
  452. return new ChamiloLMS\Controller\UserPortalController($app);
  453. }
  454. );
  455. $app['learnpath.controller'] = $app->share(
  456. function () use ($app) {
  457. return new ChamiloLMS\Controller\LearnpathController();
  458. }
  459. );
  460. $app['course_home.controller'] = $app->share(
  461. function () use ($app) {
  462. return new ChamiloLMS\Controller\CourseHomeController();
  463. }
  464. );
  465. $app['course_home.controller'] = $app->share(
  466. function () use ($app) {
  467. return new ChamiloLMS\Controller\CourseHomeController();
  468. }
  469. );
  470. $app['introduction_tool.controller'] = $app->share(
  471. function () use ($app) {
  472. return new ChamiloLMS\Controller\IntroductionToolController();
  473. }
  474. );
  475. $app['certificate.controller'] = $app->share(
  476. function () use ($app) {
  477. return new ChamiloLMS\Controller\CertificateController();
  478. }
  479. );
  480. $app['user.controller'] = $app->share(
  481. function () use ($app) {
  482. return new ChamiloLMS\Controller\UserController();
  483. }
  484. );
  485. $app['news.controller'] = $app->share(
  486. function () use ($app) {
  487. return new ChamiloLMS\Controller\NewsController();
  488. }
  489. );
  490. $app['editor.controller'] = $app->share(
  491. function () use ($app) {
  492. return new ChamiloLMS\Controller\EditorController();
  493. }
  494. );
  495. $app['question_manager.controller'] = $app->share(
  496. function () use ($app) {
  497. return new ChamiloLMS\Controller\Admin\QuestionManager\QuestionManagerController();
  498. }
  499. );
  500. $app['exercise_manager.controller'] = $app->share(
  501. function () use ($app) {
  502. return new ChamiloLMS\Controller\ExerciseController($app);
  503. }
  504. );
  505. $app['admin.controller'] = $app->share(
  506. function () use ($app) {
  507. return new ChamiloLMS\Controller\Admin\AdministratorController($app);
  508. }
  509. );
  510. $app['role.controller'] = $app->share(
  511. function () use ($app) {
  512. return new ChamiloLMS\Controller\Admin\Administrator\RoleController($app);
  513. }
  514. );
  515. $app['question_score.controller'] = $app->share(
  516. function () use ($app) {
  517. return new ChamiloLMS\Controller\Admin\Administrator\QuestionScoreController($app);
  518. }
  519. );
  520. $app['question_score_name.controller'] = $app->share(
  521. function () use ($app) {
  522. return new ChamiloLMS\Controller\Admin\Administrator\QuestionScoreNameController($app);
  523. }
  524. );
  525. $app['model_ajax.controller'] = $app->share(
  526. function () use ($app) {
  527. return new ChamiloLMS\Controller\ModelAjaxController();
  528. }
  529. );