services.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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 Doctrine\Common\Persistence\AbstractManagerRegistry;
  9. use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
  11. use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
  12. use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
  13. use FranMoreno\Silex\Provider\PagerfantaServiceProvider;
  14. use Silex\Application;
  15. use Silex\ServiceProviderInterface;
  16. use Silex\Provider\SecurityServiceProvider;
  17. use MediaAlchemyst\Alchemyst;
  18. use MediaAlchemyst\MediaAlchemystServiceProvider;
  19. use MediaVorus\MediaVorusServiceProvider;
  20. use FFMpeg\FFMpegServiceProvider;
  21. use PHPExiftool\PHPExiftoolServiceProvider;
  22. use Knp\Provider\ConsoleServiceProvider;
  23. use ChamiloLMS\Component\Auth\LoginSuccessHandler;
  24. use ChamiloLMS\Component\Auth\LogoutSuccessHandler;
  25. use ChamiloLMS\Component\Auth\LoginListener;
  26. use ChamiloLMS\Component\Editor\Connector;
  27. use ChamiloLMS\Component\Validator\ConstraintValidatorFactory;
  28. use ChamiloLMS\Component\Mail\MailGenerator;
  29. use ChamiloLMS\Component\DataFilesystem\DataFilesystem;
  30. // Flint
  31. $app->register(new Flint\Provider\ConfigServiceProvider());
  32. $app['root_dir'] = $app['root_sys'];
  33. $app->register(new Flint\Provider\RoutingServiceProvider(), array(
  34. 'routing.resource' => $app['sys_config_path'].'routing.yml',
  35. 'routing.options' => array(
  36. //'cache_dir' => $app['debug'] == true ? null : $app['sys_temp_path']
  37. //'cache_dir' => $app['sys_temp_path']
  38. ),
  39. ));
  40. if (isset($app['configuration']['services']['media-alchemyst'])) {
  41. $unoconv = null;
  42. if (isset($app['configuration']['services']['unoconv']['unoconv.binaries'])) {
  43. $unoconv = $app['configuration']['services']['unoconv']['unoconv.binaries'];
  44. }
  45. $app->register(new MediaAlchemystServiceProvider());
  46. $app->register(new PHPExiftoolServiceProvider());
  47. $app->register(new FFMpegServiceProvider());
  48. $app->register(new MediaVorusServiceProvider(), array(
  49. 'media-alchemyst.configuration' => array(
  50. 'ffmpeg.threads' => 4,
  51. 'ffmpeg.ffmpeg.timeout' => 3600,
  52. 'ffmpeg.ffprobe.timeout' => 60,
  53. 'ffmpeg.ffmpeg.binaries' => '/path/to/custom/ffmpeg',
  54. 'ffmpeg.ffprobe.binaries' => '/path/to/custom/ffprobe',
  55. 'imagine.driver' => 'imagick',
  56. 'gs.timeout' => 60,
  57. 'gs.binaries' => '/path/to/custom/gs',
  58. 'mp4box.timeout' => 60,
  59. 'mp4box.binaries' => '/path/to/custom/MP4Box',
  60. 'swftools.timeout' => 60,
  61. 'swftools.pdf2swf.binaries' => '/path/to/custom/pdf2swf',
  62. 'swftools.swfrender.binaries' => '/path/to/custom/swfrender',
  63. 'swftools.swfextract.binaries' => '/path/to/custom/swfextract',
  64. 'unoconv.binaries' => $unoconv,
  65. 'unoconv.timeout' => 60,
  66. //'exiftool.reader' => '/path/to/custom/exiftool.reader',
  67. //'exiftool.writer' => '/path/to/custom/exiftool.writer'
  68. ),
  69. //'media-alchemyst.logger' => $logger, // A PSR Logger
  70. ));
  71. }
  72. $app->register(new ConsoleServiceProvider(), array(
  73. 'console.name' => 'Chamilo',
  74. 'console.version' => '1.0.0',
  75. 'console.project_directory' => __DIR__.'/..'
  76. ));
  77. // Monolog.
  78. if (is_writable($app['sys_temp_path'])) {
  79. /**
  80. * Adding Monolog service provider.
  81. * Examples:
  82. * $app['monolog']->addDebug('Testing the Monolog logging.');
  83. * $app['monolog']->addInfo('Testing the Monolog logging.');
  84. * $app['monolog']->addError('Testing the Monolog logging.');
  85. */
  86. if ($app['debug']) {
  87. $app->register(
  88. new Silex\Provider\MonologServiceProvider(),
  89. array(
  90. 'monolog.logfile' => $app['chamilo.log'],
  91. 'monolog.name' => 'chamilo',
  92. )
  93. );
  94. }
  95. }
  96. //Setting HttpCacheService provider in order to use do: $app['http_cache']->run();
  97. /*
  98. $app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
  99. 'http_cache.cache_dir' => $app['http_cache.cache_dir'].'/',
  100. ));*/
  101. $app->register(new SecurityServiceProvider, array(
  102. 'security.firewalls' => array(
  103. 'login' => array(
  104. 'pattern' => '^/login$',
  105. 'anonymous' => true
  106. ),
  107. 'secured' => array(
  108. 'pattern' => '^/.*$',
  109. 'form' => array(
  110. 'login_path' => '/login',
  111. 'check_path' => '/secured/login_check',
  112. 'default_target_path' => '/userportal',
  113. 'username_parameter' => 'username',
  114. 'password_parameter' => 'password',
  115. ),
  116. 'logout' => array(
  117. 'logout_path' => '/secured/logout',
  118. 'target' => '/'
  119. ),
  120. 'users' => $app->share(function() use ($app) {
  121. return $app['orm.em']->getRepository('ChamiloLMS\Entity\User');
  122. }),
  123. 'switch_user' => true,
  124. 'anonymous' => true
  125. )
  126. )
  127. ));
  128. // Registering Password encoder.
  129. $app['security.encoder.digest'] = $app->share(function($app) {
  130. // use the sha1 algorithm
  131. // don't base64 encode the password
  132. // use only 1 iteration
  133. return new MessageDigestPasswordEncoder(
  134. $app['configuration']['password_encryption'],
  135. false,
  136. 1
  137. );
  138. });
  139. // What to do when login success?
  140. $app['security.authentication.success_handler.secured'] = $app->share(function($app) {
  141. return new LoginSuccessHandler($app['url_generator'], $app['security']);
  142. });
  143. // What to do when logout?
  144. $app['security.authentication.logout_handler.secured'] = $app->share(function($app) {
  145. return new LogoutSuccessHandler($app['url_generator'], $app['security']);
  146. });
  147. // What to do when switch user?
  148. $app['security.authentication_listener.switch_user.secured'] = $app->share(function($app) {
  149. return new LoginListener();
  150. });
  151. // Role hierarchy
  152. $app['security.role_hierarchy'] = array(
  153. // the admin that belongs to the portal #1 can affect all portals
  154. 'ROLE_GLOBAL_ADMIN' => array('ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH'),
  155. // the default admin
  156. 'ROLE_ADMIN' => array(
  157. 'ROLE_QUESTION_MANAGER',
  158. 'ROLE_SESSION_MANAGER',
  159. 'ROLE_TEACHER',
  160. 'ROLE_DIRECTOR',
  161. 'ROLE_JURY_PRESIDENT'
  162. ),
  163. 'ROLE_RRHH' => array('ROLE_TEACHER'),
  164. 'ROLE_TEACHER' => array('ROLE_STUDENT'),
  165. 'ROLE_QUESTION_MANAGER' => array('ROLE_STUDENT', 'ROLE_QUESTION_MANAGER'),
  166. 'ROLE_SESSION_MANAGER' => array('ROLE_STUDENT', 'ROLE_SESSION_MANAGER', 'ROLE_ALLOWED_TO_SWITCH'),
  167. 'ROLE_STUDENT' => array('ROLE_STUDENT'),
  168. 'ROLE_ANONYMOUS' => array('ROLE_ANONYMOUS')
  169. );
  170. // Role rules
  171. $app['security.access_rules'] = array(
  172. array('^/admin/administrator', 'ROLE_ADMIN'),
  173. array('^/main/admin/.*', 'ROLE_ADMIN'),
  174. array('^/admin/questionmanager', 'ROLE_QUESTION_MANAGER'),
  175. array('^/main/auth/inscription.php', 'IS_AUTHENTICATED_ANONYMOUSLY'),
  176. array('^/main/auth/lostPassword.php', 'IS_AUTHENTICATED_ANONYMOUSLY'),
  177. array('^/courses/.*/curriculum/category', 'ROLE_TEACHER'),
  178. array('^/courses/.*/curriculum/item', 'ROLE_TEACHER'),
  179. array('^/courses/.*/curriculum/user', 'ROLE_STUDENT'),
  180. array('^/courses/.*/curriculum', 'ROLE_STUDENT'),
  181. //array('^/main/.*', array('ROLE_STUDENT')),
  182. );
  183. // Roles that have an admin toolbar
  184. $app['allow_admin_toolbar'] = array(
  185. 'ROLE_ADMIN',
  186. 'ROLE_QUESTION_MANAGER',
  187. 'ROLE_SESSION_MANAGER'
  188. );
  189. /*
  190. use ChamiloLMS\Component\Auth\CourseVoter;
  191. use ChamiloLMS\Component\Auth\CourseAccessDecisionManager;
  192. $app['course_decision_manager'] = $app->share(function($app) {
  193. return new CourseAccessDecisionManager();
  194. });
  195. $app['course_voter'] = $app->share(function($app) {
  196. return new CourseVoter($app['course_decision_manager']);
  197. });
  198. $app['security.voters'] = $app->extend('security.voters', function($voters) use ($app) {
  199. $voters[] = $app['course_voter'];
  200. return $voters;
  201. });
  202. $app['security.access_manager'] = $app->share(function($app) {
  203. return new AccessDecisionManager($app['security.voters'], 'unanimous');
  204. });
  205. */
  206. use SilexOpauth\OpauthExtension;
  207. $strategies = isset($_configuration['strategies']) ? $_configuration['strategies'] : null;
  208. if (!empty($strategies)) {
  209. $app['opauth'] = array(
  210. 'login' => '/auth/login',
  211. 'callback' => '/auth/callback',
  212. 'config' => array(
  213. 'security_salt' => $_configuration['security_key'],
  214. 'Strategy' => array(
  215. $strategies
  216. )
  217. )
  218. );
  219. $app->register(new OpauthExtension());
  220. }
  221. /*
  222. $app['security.access_manager'] = $app->share(function($app) {
  223. return new AccessDecisionManager($app['security.voters'], 'unanimous');
  224. });*/
  225. // Setting Controllers as services provider.
  226. $app->register(new Silex\Provider\ServiceControllerServiceProvider());
  227. // Implements Symfony2 translator.
  228. $app->register(new Silex\Provider\TranslationServiceProvider(), array(
  229. 'locale' => 'en',
  230. 'locale_fallback' => 'en',
  231. 'translator.domains' => array()
  232. ));
  233. // Validator provider.
  234. $app->register(new Silex\Provider\ValidatorServiceProvider());
  235. // Form provider.
  236. $app->register(new Silex\Provider\FormServiceProvider(), array(
  237. 'form.secret' => sha1(__DIR__)
  238. ));
  239. // URL generator provider.
  240. //$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
  241. // Needed to use the "entity" option in symfony forms
  242. class ManagerRegistry extends AbstractManagerRegistry
  243. {
  244. protected $container;
  245. protected function getService($name)
  246. {
  247. return $this->container[$name];
  248. }
  249. protected function resetService($name)
  250. {
  251. unset($this->container[$name]);
  252. }
  253. /**
  254. * @param string $alias
  255. * @return string|void
  256. * @throws BadMethodCallException
  257. */
  258. public function getAliasNamespace($alias)
  259. {
  260. throw new \BadMethodCallException('Namespace aliases not supported.');
  261. }
  262. public function setContainer(Application $container)
  263. {
  264. $this->container = $container;
  265. }
  266. }
  267. // Setting up the Manager registry in order to use entity in forms.
  268. $app['manager_registry'] = $app->share(function() use ($app) {
  269. $managerRegistry = new ManagerRegistry(
  270. null,
  271. array('db'),
  272. array('orm.em'),
  273. null,
  274. null,
  275. $app['orm.proxies_namespace']
  276. );
  277. $managerRegistry->setContainer($app);
  278. return $managerRegistry;
  279. });
  280. // Needed to use the "entity" option in Symfony forms.
  281. $app['form.extensions'] = $app->share($app->extend('form.extensions', function ($extensions, $app) {
  282. $extensions[] = new DoctrineOrmExtension($app['manager_registry']);
  283. return $extensions;
  284. }));
  285. // Needed to use the "UniqueEntity" validator.
  286. $app['validator.validator_factory'] = $app->share(function ($app) {
  287. $uniqueValidator = new UniqueEntityValidator($app['manager_registry']);
  288. $factory = new ConstraintValidatorFactory();
  289. $factory->addInstance('doctrine.orm.validator.unique', $uniqueValidator);
  290. return $factory;
  291. });
  292. // Setting Doctrine service provider (DBAL).
  293. if (isset($app['configuration']['main_database'])) {
  294. /* The database connection can be overwritten if you set $_configuration['db.options']
  295. in configuration.php like this : */
  296. $dbPort = isset($app['configuration']['db_port']) ? $app['configuration']['db_port'] : 3306;
  297. $dbDriver = isset($app['configuration']['db_driver']) ? $app['configuration']['db_driver'] : 'pdo_mysql';
  298. $host = $app['configuration']['db_host'];
  299. // Accepts that db_host can have a port part like: localhost:6666;
  300. $hostParts = explode(':', $app['configuration']['db_host']);
  301. if (isset($hostParts[1]) && !empty($hostParts[1])) {
  302. $dbPort = $hostParts[1];
  303. $host = str_replace(':'.$dbPort, '', $app['configuration']['db_host']);
  304. }
  305. $defaultDatabaseOptions = array(
  306. 'db_read' => array(
  307. 'driver' => $dbDriver,
  308. 'host' => $host,
  309. 'port' => $dbPort,
  310. 'dbname' => $app['configuration']['main_database'],
  311. 'user' => $app['configuration']['db_user'],
  312. 'password' => $app['configuration']['db_password'],
  313. 'charset' => 'utf8',
  314. //'priority' => '1'
  315. ),
  316. 'db_write' => array(
  317. 'driver' => $dbDriver,
  318. 'host' => $host,
  319. 'port' => $dbPort,
  320. 'dbname' => $app['configuration']['main_database'],
  321. 'user' => $app['configuration']['db_user'],
  322. 'password' => $app['configuration']['db_password'],
  323. 'charset' => 'utf8',
  324. //'priority' => '2'
  325. ),
  326. );
  327. // Could be set in the $_configuration array
  328. if (isset($app['configuration']['db.options'])) {
  329. $defaultDatabaseOptions = $app['configuration']['db.options'];
  330. }
  331. // Doctrine service provider.
  332. $app->register(
  333. new Silex\Provider\DoctrineServiceProvider(),
  334. array(
  335. 'dbs.options' => $defaultDatabaseOptions
  336. )
  337. );
  338. $mappings = array(
  339. array(
  340. /* If true, only simple notations like @Entity will work.
  341. If false, more advanced notations and aliasing via use will work.
  342. (Example: use Doctrine\ORM\Mapping AS ORM, @ORM\Entity)*/
  343. 'use_simple_annotation_reader' => false,
  344. 'type' => 'annotation',
  345. 'namespace' => 'ChamiloLMS\Entity',
  346. 'path' => api_get_path(SYS_PATH).'src/ChamiloLMS/Entity',
  347. // 'orm.default_cache' =>
  348. ),
  349. array(
  350. 'use_simple_annotation_reader' => false,
  351. 'type' => 'annotation',
  352. 'namespace' => 'Gedmo',
  353. 'path' => api_get_path(SYS_PATH).'vendors/gedmo/doctrine-extensions/lib/Gedmo',
  354. )
  355. );
  356. // Setting Doctrine ORM.
  357. $app->register(
  358. new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider,
  359. array(
  360. // Doctrine2 ORM cache
  361. /*'orm.default_cache' => 'apc', // array, apc, xcache, memcache, memcached
  362. 'metadata_cache' => 'apc',
  363. 'result_cache' => 'apc',*/
  364. // Proxies
  365. 'orm.auto_generate_proxies' => true,
  366. 'orm.proxies_dir' => $app['db.orm.proxies_dir'],
  367. 'orm.proxies_namespace' => 'Doctrine\ORM\Proxy\Proxy',
  368. 'orm.ems.default' => 'db_read',
  369. 'orm.ems.options' => array(
  370. 'db_read' => array(
  371. 'connection' => 'db_read',
  372. 'mappings' => $mappings,
  373. ),
  374. 'db_write' => array(
  375. 'connection' => 'db_write',
  376. 'mappings' => $mappings,
  377. ),
  378. ),
  379. )
  380. );
  381. }
  382. // Setting Twig as a service provider.
  383. $app->register(
  384. new Silex\Provider\TwigServiceProvider(),
  385. array(
  386. 'twig.path' => array(
  387. $app['sys_root'].'src/ChamiloLMS/Resources/views', //template folder
  388. $app['sys_root'].'plugin' //plugin folder
  389. ),
  390. // twitter bootstrap form twig templates
  391. 'twig.form.templates' => array('form_div_layout.html.twig', 'default/form/form_custom_template.tpl'),
  392. 'twig.options' => array(
  393. 'debug' => $app['debug'],
  394. 'charset' => 'utf-8',
  395. 'strict_variables' => false,
  396. 'autoescape' => false,
  397. 'cache' => $app['debug'] ? false : $app['twig.cache.path'],
  398. 'optimizations' => -1, // turn on optimizations with -1
  399. )
  400. )
  401. );
  402. // Setting Twig options.
  403. $app['twig'] = $app->share(
  404. $app->extend('twig', function ($twig) {
  405. $twig->addFilter('get_lang', new Twig_Filter_Function('get_lang'));
  406. $twig->addFilter('get_path', new Twig_Filter_Function('api_get_path'));
  407. $twig->addFilter('get_setting', new Twig_Filter_Function('api_get_setting'));
  408. $twig->addFilter('var_dump', new Twig_Filter_Function('var_dump'));
  409. $twig->addFilter('return_message', new Twig_Filter_Function('Display::return_message_and_translate'));
  410. $twig->addFilter('display_page_header', new Twig_Filter_Function('Display::page_header_and_translate'));
  411. $twig->addFilter('display_page_subheader', new Twig_Filter_Function('Display::page_subheader_and_translate'));
  412. $twig->addFilter('icon', new Twig_Filter_Function('Template::get_icon_path'));
  413. $twig->addFilter('format_date', new Twig_Filter_Function('Template::format_date'));
  414. return $twig;
  415. })
  416. );
  417. // Developer tools.
  418. if (is_writable($app['sys_temp_path'])) {
  419. if ($app['show_profiler']) {
  420. // Adding Symfony2 web profiler (memory, time, logs, etc)
  421. $app->register(
  422. $p = new Silex\Provider\WebProfilerServiceProvider(),
  423. array(
  424. 'profiler.cache_dir' => $app['profiler.cache_dir'],
  425. )
  426. );
  427. $app->mount('/_profiler', $p);
  428. // PHP errors for cool kids
  429. //$app->register(new Whoops\Provider\Silex\WhoopsServiceProvider);
  430. }
  431. }
  432. // Pagerfanta settings (Pagination using Doctrine2, arrays, etc)
  433. $app->register(new PagerfantaServiceProvider());
  434. // Custom route params see https://github.com/franmomu/silex-pagerfanta-provider/pull/2
  435. //$app['pagerfanta.view.router.name']
  436. //$app['pagerfanta.view.router.params']
  437. $app['pagerfanta.view.options'] = array(
  438. 'routeName' => null,
  439. 'routeParams' => array(),
  440. 'pageParameter' => '[page]',
  441. 'proximity' => 3,
  442. 'next_message' => '&raquo;',
  443. 'prev_message' => '&laquo;',
  444. 'default_view' => 'twitter_bootstrap' // the pagination style
  445. );
  446. // Registering Menu service provider (too gently creating menus with the URLgenerator provider)
  447. $app->register(new \Knp\Menu\Silex\KnpMenuServiceProvider());
  448. // @todo use a app['image_processor'] setting
  449. define('IMAGE_PROCESSOR', 'gd'); // imagick or gd strings
  450. // Setting the Imagine service provider to deal with image transformations used in social group.
  451. $app->register(new Grom\Silex\ImagineServiceProvider(), array(
  452. 'imagine.factory' => 'Gd'
  453. ));
  454. // Email service provider.
  455. $app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
  456. 'swiftmailer.options' => array(
  457. 'host' => isset($platform_email['SMTP_HOST']) ? $platform_email['SMTP_HOST'] : null,
  458. 'port' => isset($platform_email['SMTP_PORT']) ? $platform_email['SMTP_PORT'] : null,
  459. 'username' => isset($platform_email['SMTP_USER']) ? $platform_email['SMTP_USER'] : null,
  460. 'password' => isset($platform_email['SMTP_PASS']) ? $platform_email['SMTP_PASS'] : null,
  461. 'encryption' => null,
  462. 'auth_mode' => null
  463. )
  464. ));
  465. // Mailer
  466. $app['mailer'] = $app->share(function ($app) {
  467. return new \Swift_Mailer($app['swiftmailer.transport']);
  468. });
  469. // Assetic service provider.
  470. if ($app['assetic.enabled']) {
  471. $app->register(new SilexAssetic\AsseticServiceProvider(), array(
  472. 'assetic.options' => array(
  473. 'debug' => $app['debug'],
  474. 'auto_dump_assets' => $app['assetic.auto_dump_assets'],
  475. )
  476. ));
  477. // Less filter
  478. $app['assetic.filter_manager'] = $app->share(
  479. $app->extend('assetic.filter_manager', function($fm, $app) {
  480. $fm->set('lessphp', new Assetic\Filter\LessphpFilter());
  481. return $fm;
  482. })
  483. );
  484. $app['assetic.asset_manager'] = $app->share(
  485. $app->extend('assetic.asset_manager', function($am, $app) {
  486. $am->set('styles', new Assetic\Asset\AssetCache(
  487. new Assetic\Asset\GlobAsset(
  488. $app['assetic.input.path_to_css'],
  489. array($app['assetic.filter_manager']->get('lessphp'))
  490. ),
  491. new Assetic\Cache\FilesystemCache($app['assetic.path_to_cache'])
  492. ));
  493. $am->get('styles')->setTargetPath($app['assetic.output.path_to_css']);
  494. $am->set('scripts', new Assetic\Asset\AssetCache(
  495. new Assetic\Asset\GlobAsset($app['assetic.input.path_to_js']),
  496. new Assetic\Cache\FilesystemCache($app['assetic.path_to_cache'])
  497. ));
  498. $am->get('scripts')->setTargetPath($app['assetic.output.path_to_js']);
  499. return $am;
  500. })
  501. );
  502. }
  503. // Gaufrette service provider (to manage files/dirs) (not used yet)
  504. /*
  505. use Bt51\Silex\Provider\GaufretteServiceProvider\GaufretteServiceProvider;
  506. $app->register(new GaufretteServiceProvider(), array(
  507. 'gaufrette.adapter.class' => 'Local',
  508. 'gaufrette.options' => array(api_get_path(SYS_DATA_PATH))
  509. ));
  510. */
  511. // Use Symfony2 filesystem instead of custom scripts.
  512. $app->register(new Neutron\Silex\Provider\FilesystemServiceProvider());
  513. /** Chamilo service provider. */
  514. class ChamiloServiceProvider implements ServiceProviderInterface
  515. {
  516. public function register(Application $app)
  517. {
  518. // Database.
  519. $app['database'] = $app->share(function () use ($app) {
  520. $db = new Database($app['db'], $app['dbs']);
  521. return $db;
  522. });
  523. $database = $app['database'];
  524. $app['html_editor'] = $app->share(function($app) {
  525. $editor = new ChamiloLMS\Component\Editor\CkEditor\CkEditor(
  526. $app['translator'],
  527. $app['url_generator'],
  528. $app['template'],
  529. $app['course']
  530. );
  531. $editor->setJavascriptToInclude();
  532. return $editor;
  533. /*return new ChamiloLMS\Component\Editor\TinyMce\TinyMce(
  534. $app['translator'], $app['url_generator']
  535. );*/
  536. });
  537. $app['editor_connector'] = $app->share(function ($app) {
  538. $token = $app['security']->getToken();
  539. $user = $token->getUser();
  540. return new Connector(
  541. $app['orm.em'],
  542. $app['paths'],
  543. $app['url_generator'],
  544. $app['translator'],
  545. $app['security'],
  546. $user,
  547. $app['course']
  548. );
  549. });
  550. // Template class
  551. $app['template'] = $app->share(function () use ($app) {
  552. $template = new Template(
  553. $app,
  554. $app['database'],
  555. $app['security'],
  556. $app['translator'],
  557. $app['url_generator']
  558. );
  559. return $template;
  560. });
  561. // Paths
  562. $app['paths'] = $app->share(function () use ($app) {
  563. return array(
  564. 'root_sys' => $app['root_sys'],
  565. 'sys_root' => $app['root_sys'], // just an alias
  566. 'sys_data_path' => $app['sys_data_path'],
  567. 'sys_config_path' => $app['sys_config_path'],
  568. 'sys_temp_path' => $app['sys_temp_path'],
  569. 'sys_log_path' => $app['sys_log_path']
  570. );
  571. });
  572. $app['course'] = $app->share(function () use ($app) {
  573. $request = $app['request'];
  574. $session = $request->getSession();
  575. $courseCode = $request->get('course');
  576. if (empty($courseCode)) {
  577. $courseCode = $session->get('_cid');
  578. }
  579. if (!empty($courseCode)) {
  580. // Converting /courses/XXX/ to a Entity/Course object.
  581. return $app['orm.em']->getRepository('ChamiloLMS\Entity\Course')->findOneByCode($courseCode);
  582. //$app['template']->assign('course', $course);
  583. return $course;
  584. }
  585. return null;
  586. });
  587. $app['course_session'] = $app->share(function () use ($app) {
  588. $request = $app['request'];
  589. $session = $request->getSession();
  590. $sessionId = $request->get('id_session');
  591. if (empty($sessionId)) {
  592. $sessionId = $session->get('id_session');
  593. }
  594. if (!empty($sessionId)) {
  595. return $app['orm.em']->getRepository('ChamiloLMS\Entity\Session')->findOneById($sessionId);
  596. // $app['template']->assign('course_session', $courseSession);
  597. return $courseSession;
  598. }
  599. return null;
  600. });
  601. // Chamilo data filesystem.
  602. $app['chamilo.filesystem'] = $app->share(function () use ($app) {
  603. $mediaConverter = null;
  604. if (isset($app['configuration']['services']['media-alchemyst'])) {
  605. $mediaConverter = $app['media-alchemyst'];
  606. }
  607. $filesystem = new DataFilesystem(
  608. $app['paths'],
  609. $app['filesystem'],
  610. $app['editor_connector'],
  611. $mediaConverter
  612. );
  613. return $filesystem;
  614. });
  615. // Page controller class.
  616. $app['page_controller'] = $app->share(function () use ($app) {
  617. $pageController = new PageController($app);
  618. return $pageController;
  619. });
  620. // Mail template generator.
  621. $app['mail_generator'] = $app->share(function () use ($app) {
  622. $mailGenerator = new MailGenerator($app['twig'], $app['mailer']);
  623. return $mailGenerator;
  624. });
  625. // Setting up name conventions
  626. $conventions = require_once $app['sys_root'].'main/inc/lib/internationalization_database/name_order_conventions.php';
  627. if (isset($configuration['name_order_conventions']) && !empty($configuration['name_order_conventions'])) {
  628. $conventions = array_merge($conventions, $configuration['name_order_conventions']);
  629. }
  630. $search1 = array('FIRST_NAME', 'LAST_NAME', 'TITLE');
  631. $replacement1 = array('%F', '%L', '%T');
  632. $search2 = array('first_name', 'last_name', 'title');
  633. $replacement2 = array('%f', '%l', '%t');
  634. $keyConventions = array_keys($conventions);
  635. foreach ($keyConventions as $key) {
  636. $conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
  637. $conventions[$key]['format'] = _api_validate_person_name_format(
  638. _api_clean_person_name(
  639. str_replace('%', ' %', str_ireplace($search2, $replacement2, $conventions[$key]['format']))
  640. )
  641. );
  642. $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
  643. }
  644. $app['name_order_conventions'] = $conventions;
  645. }
  646. /**
  647. * @param Application $app
  648. */
  649. public function boot(Application $app)
  650. {
  651. }
  652. }
  653. // Registering Chamilo service provider.
  654. $app->register(new ChamiloServiceProvider(), array());
  655. // Controller as services definitions.
  656. $app['pages.controller'] = $app->share(
  657. function () use ($app) {
  658. return new PagesController($app['pages.repository']);
  659. }
  660. );
  661. //@todo improve loading of controllers.
  662. $app['index.controller'] = $app->share(
  663. function () use ($app) {
  664. $controller = new ChamiloLMS\Controller\IndexController($app);
  665. return $controller;
  666. }
  667. );
  668. $app['legacy.controller'] = $app->share(
  669. function () use ($app) {
  670. return new ChamiloLMS\Controller\LegacyController($app);
  671. }
  672. );
  673. $app['userPortal.controller'] = $app->share(
  674. function () use ($app) {
  675. return new ChamiloLMS\Controller\UserPortalController($app);
  676. }
  677. );
  678. $app['learnpath.controller'] = $app->share(
  679. function () use ($app) {
  680. return new ChamiloLMS\Controller\LearnpathController();
  681. }
  682. );
  683. $app['certificate.controller'] = $app->share(
  684. function () use ($app) {
  685. return new ChamiloLMS\Controller\CertificateController();
  686. }
  687. );
  688. $app['profile.controller'] = $app->share(
  689. function () use ($app) {
  690. return new ChamiloLMS\Controller\User\ProfileController($app);
  691. }
  692. );
  693. $app['user.controller'] = $app->share(
  694. function () use ($app) {
  695. return new ChamiloLMS\Controller\User\UserController($app);
  696. }
  697. );
  698. $app['news.controller'] = $app->share(
  699. function () use ($app) {
  700. return new ChamiloLMS\Controller\NewsController();
  701. }
  702. );
  703. $app['editor.controller'] = $app->share(
  704. function () use ($app) {
  705. return new ChamiloLMS\Controller\EditorController($app);
  706. }
  707. );
  708. $app['question_manager.controller'] = $app->share(
  709. function () use ($app) {
  710. return new ChamiloLMS\Controller\Admin\QuestionManager\QuestionManagerController();
  711. }
  712. );
  713. $app['exercise_manager.controller'] = $app->share(
  714. function () use ($app) {
  715. return new ChamiloLMS\Controller\ExerciseController($app);
  716. }
  717. );
  718. $app['admin.controller'] = $app->share(
  719. function () use ($app) {
  720. return new ChamiloLMS\Controller\Admin\AdminController($app);
  721. }
  722. );
  723. $app['role.controller'] = $app->share(
  724. function () use ($app) {
  725. return new ChamiloLMS\Controller\Admin\Administrator\RoleController($app);
  726. }
  727. );
  728. $app['question_score.controller'] = $app->share(
  729. function () use ($app) {
  730. return new ChamiloLMS\Controller\Admin\Administrator\QuestionScoreController($app);
  731. }
  732. );
  733. $app['question_score_name.controller'] = $app->share(
  734. function () use ($app) {
  735. return new ChamiloLMS\Controller\Admin\Administrator\QuestionScoreNameController($app);
  736. }
  737. );
  738. $app['model_ajax.controller'] = $app->share(
  739. function () use ($app) {
  740. return new ChamiloLMS\Controller\ModelAjaxController();
  741. }
  742. );
  743. // Curriculum tool
  744. $app['curriculum.controller'] = $app->share(
  745. function () use ($app) {
  746. return new ChamiloLMS\Controller\Tool\Curriculum\CurriculumController($app);
  747. }
  748. );
  749. $app['curriculum_category.controller'] = $app->share(
  750. function () use ($app) {
  751. return new ChamiloLMS\Controller\Tool\Curriculum\CurriculumCategoryController($app);
  752. }
  753. );
  754. $app['curriculum_item.controller'] = $app->share(
  755. function () use ($app) {
  756. return new ChamiloLMS\Controller\Tool\Curriculum\CurriculumItemController($app);
  757. }
  758. );
  759. $app['curriculum_user.controller'] = $app->share(
  760. function () use ($app) {
  761. return new ChamiloLMS\Controller\Tool\Curriculum\CurriculumUserController($app);
  762. }
  763. );
  764. $app['session_path.controller'] = $app->share(
  765. function () use ($app) {
  766. return new ChamiloLMS\Controller\App\SessionPath\SessionPathController($app);
  767. }
  768. );
  769. $app['session_tree.controller'] = $app->share(
  770. function () use ($app) {
  771. return new ChamiloLMS\Controller\App\SessionPath\SessionTreeController($app);
  772. }
  773. );
  774. $app['upgrade.controller'] = $app->share(
  775. function () use ($app) {
  776. return new ChamiloLMS\Controller\Admin\Administrator\UpgradeController($app);
  777. }
  778. );
  779. $app['course_home.controller'] = $app->share(
  780. function () use ($app) {
  781. return new ChamiloLMS\Controller\Tool\CourseHome\CourseHomeController($app);
  782. }
  783. );
  784. $app['introduction.controller'] = $app->share(
  785. function () use ($app) {
  786. return new ChamiloLMS\Controller\Tool\Introduction\IntroductionController($app);
  787. }
  788. );
  789. /*if (isset($app['configuration']['unoconv.binaries'])) {
  790. $app->register(new Unoconv\UnoconvServiceProvider(), array(
  791. 'unoconv.configuration' => array(
  792. 'unoconv.binaries' => $_configuration['services']['unoconv']['unoconv.binaries'],
  793. 'timeout' => 42,
  794. ),
  795. 'unoconv.logger' => $app->share(function () use ($app) {
  796. return $app['monolog']; // use Monolog service provider
  797. }),
  798. ));
  799. }
  800. */
  801. /*
  802. $app->register(
  803. new ChamiloLMS\Provider\BootstrapSilexProvider(),
  804. array(
  805. )
  806. );*/