services.php 20 KB

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