index.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. set_time_limit(0);
  4. /**
  5. * Chamilo installation
  6. * This script could be loaded via browser using the URL: main/install/index.php
  7. * or via CM
  8. *
  9. * @package chamilo.install
  10. */
  11. require_once __DIR__.'/../../vendor/autoload.php';
  12. require_once 'install.lib.php';
  13. require_once '../inc/lib/api.lib.php';
  14. error_reporting(-1);
  15. use Symfony\Component\Console\Output\Output;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use ChamiloLMS\Component\Console\Output\BufferedOutput;
  18. $app = new Silex\Application();
  19. // Setting Chamilo paths
  20. $app['root_sys'] = dirname(dirname(__DIR__)).'/';
  21. $app['sys_root'] = $app['root_sys'];
  22. $app['sys_data_path'] = isset($_configuration['sys_data_path']) ? $_configuration['sys_data_path'] : $app['root_sys'].'data/';
  23. $app['sys_config_path'] = isset($_configuration['sys_config_path']) ? $_configuration['sys_config_path'] : $app['root_sys'].'config/';
  24. $app['sys_course_path'] = isset($_configuration['sys_course_path']) ? $_configuration['sys_course_path'] : $app['sys_data_path'].'/course';
  25. $app['sys_log_path'] = isset($_configuration['sys_log_path']) ? $_configuration['sys_log_path'] : $app['root_sys'].'logs/';
  26. $app['sys_temp_path'] = isset($_configuration['sys_temp_path']) ? $_configuration['sys_temp_path'] : $app['sys_data_path'].'temp/';
  27. // Registering services
  28. $app['debug'] = false;
  29. $app->register(new Silex\Provider\UrlGeneratorServiceProvider());
  30. $app->register(new Silex\Provider\FormServiceProvider());
  31. $app->register(new Silex\Provider\SessionServiceProvider());
  32. $app->register(new Silex\Provider\DoctrineServiceProvider());
  33. $app->register(new Silex\Provider\TranslationServiceProvider());
  34. $app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
  35. /*$translator->addLoader('pofile', new PoFileLoader());
  36. $file = 'main/locale/'.$locale.'.po';
  37. $translator->addResource('pofile', $file, $locale);*/
  38. /*$translator->addLoader('yaml', new Symfony\Component\Translation\Loader\YamlFileLoader());
  39. $translator->addResource('yaml', __DIR__.'/lang/fr.yml', 'fr');
  40. $translator->addResource('yaml', __DIR__.'/lang/en.yml', 'en');
  41. $translator->addResource('yaml', __DIR__.'/lang/es.yml', 'es');*/
  42. return $translator;
  43. }));
  44. $app->register(
  45. new Silex\Provider\TwigServiceProvider(),
  46. array(
  47. 'twig.path' => array(
  48. 'templates'
  49. ),
  50. // twitter bootstrap form twig templates
  51. //'twig.form.templates' => array('form_div_layout.html.twig', '../template/default/form/form_custom_template.tpl'),
  52. 'twig.options' => array(
  53. 'debug' => $app['debug'],
  54. 'charset' => 'utf-8',
  55. 'strict_variables' => false,
  56. 'autoescape' => true,
  57. //'cache' => $app['debug'] ? false : $app['twig.cache.path'],
  58. 'cache' => false, // no cache during installation sorry
  59. 'optimizations' => -1, // turn on optimizations with -1
  60. )
  61. )
  62. );
  63. use Knp\Provider\ConsoleServiceProvider;
  64. $app->register(new ConsoleServiceProvider(), array(
  65. 'console.name' => 'Chamilo',
  66. 'console.version' => '1.0.0',
  67. 'console.project_directory' => __DIR__.'/..'
  68. ));
  69. // Adding commands.
  70. /** @var Knp\Console\Application $console */
  71. $console = $app['console'];
  72. $console->addCommands(
  73. array(
  74. // DBAL Commands.
  75. new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(),
  76. new \Doctrine\DBAL\Tools\Console\Command\ImportCommand(),
  77. // Migrations Commands.
  78. new \Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand(),
  79. new \Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand(),
  80. new \Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand(),
  81. new \Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand(),
  82. new \Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand(),
  83. new \Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand(),
  84. // Chash commands.
  85. new Chash\Command\Installation\UpgradeCommand(),
  86. new Chash\Command\Installation\InstallCommand(),
  87. new Chash\Command\Files\CleanDataFilesCommand(),
  88. new Chash\Command\Files\CleanTempFolderCommand(),
  89. new Chash\Command\Files\CleanConfigFilesCommand(),
  90. new Chash\Command\Files\MailConfCommand(),
  91. new Chash\Command\Files\SetPermissionsAfterInstallCommand(),
  92. new Chash\Command\Files\GenerateTempFileStructureCommand(),
  93. )
  94. );
  95. $helpers = array(
  96. 'configuration' => new Chash\Helpers\ConfigurationHelper()
  97. );
  98. $helperSet = $console->getHelperSet();
  99. foreach ($helpers as $name => $helper) {
  100. $helperSet->set($helper, $name);
  101. }
  102. $blockInstallation = function() use($app) {
  103. if (file_exists($app['root_sys'].'config/configuration.php') ||
  104. file_exists($app['root_sys'].'config/configuration.yml')
  105. ) {
  106. return $app->abort(500, "A Chamilo installation was found. You can't reinstall.");
  107. }
  108. };
  109. // Controllers
  110. $app->match('/', function() use($app) {
  111. // in order to get a list of countries
  112. //var_dump(Symfony\Component\Intl\Intl::getRegionBundle()->getCountryNames());
  113. $languages = array(
  114. 'english' => 'english',
  115. 'spanish' => 'spanish',
  116. 'french' => 'french'
  117. );
  118. $request = $app['request'];
  119. $form = $app['form.factory']->createBuilder('form')
  120. ->add('languages', 'choice', array(
  121. 'choices' => $languages,
  122. 'required' => true,
  123. ))
  124. ->add('continue', 'submit')
  125. ->getForm();
  126. if ('POST' == $request->getMethod()) {
  127. $url = $app['url_generator']->generate('requirements');
  128. return $app->redirect($url);
  129. }
  130. return $app['twig']->render('index.tpl', array('form' => $form->createView()));
  131. })
  132. ->bind('welcome')
  133. ->before($blockInstallation);
  134. $app->match('/requirements', function() use($app) {
  135. $request = $app['request'];
  136. $form = $app['form.factory']->createBuilder('form')
  137. ->add('continue', 'submit')
  138. ->getForm();
  139. $req = display_requirements($app, 'new');
  140. if ('POST' == $request->getMethod()) {
  141. $url = $app['url_generator']->generate('check-database');
  142. return $app->redirect($url);
  143. }
  144. return $app['twig']->render(
  145. 'requirements.tpl',
  146. array(
  147. 'form' => $form->createView(),
  148. 'requirements' => $req
  149. )
  150. );
  151. })->bind('requirements');
  152. $app->match('/check-database', function() use($app) {
  153. /** @var Request $request */
  154. $request = $app['request'];
  155. $command = $app['console']->get('chamilo:install');
  156. $data = $command->getDatabaseSettingsParams();
  157. $builder = $app['form.factory']->createBuilder('form');
  158. foreach ($data as $key => $value) {
  159. $value['attributes'] = isset($value['attributes']) && is_array($value['attributes']) ? $value['attributes'] : array();
  160. $builder->add($key, $value['type'], $value['attributes']);
  161. }
  162. $builder->add('check', 'submit');
  163. $form = $builder->getForm();
  164. if ('POST' == $request->getMethod()) {
  165. $form->bind($request);
  166. if ($form->isValid()) {
  167. $parameters = $form->getData();
  168. /** @var \Chash\Command\Installation\InstallCommand $command */
  169. $command = $app['console']->get('chamilo:install');
  170. $command->setDatabaseSettings($parameters);
  171. $connection = $command->getUserAccessConnectionToHost();
  172. try {
  173. $connect = $connection->connect();
  174. $sm = $connection->getSchemaManager();
  175. $databases = $sm->listDatabases();
  176. if (in_array($parameters['dbname'], $databases)) {
  177. $message = $app['translator']->trans(
  178. 'The database "%s" being used and is going to be deleted!!',
  179. array('%s' => $parameters['dbname'])
  180. );
  181. $app['session']->getFlashBag()->add('warning', $message);
  182. } else {
  183. $message = $app['translator']->trans(
  184. 'A database "%s" is going to be created',
  185. array('%s' => $parameters['dbname'])
  186. );
  187. $app['session']->getFlashBag()->add('warning', $message);
  188. }
  189. $app['session']->getFlashBag()->add('success', 'Connection ok!');
  190. $app['session']->set('database_settings', $parameters);
  191. $url = $app['url_generator']->generate('portal-settings');
  192. return $app->redirect($url);
  193. } catch (Exception $e) {
  194. $app['session']->getFlashBag()->add('success', 'Connection error !'.$e->getMessage());
  195. }
  196. }
  197. }
  198. return $app['twig']->render('check-database.tpl', array('form' => $form->createView()));
  199. })->bind('check-database');
  200. $app->match('/portal-settings', function() use($app) {
  201. /** @var Request $request */
  202. $request = $app['request'];
  203. /** @var \Chash\Command\Installation\InstallCommand $command */
  204. $command = $app['console']->get('chamilo:install');
  205. $builder = $app['form.factory']->createBuilder('form');
  206. $data = $command->getPortalSettingsParams();
  207. $data['institution_url']['attributes']['data'] = str_replace('main/install/', '', $request->getUriForPath('/'));
  208. $permissionNewDir = $app['session']->get('permissions_for_new_directories');
  209. if ($permissionNewDir) {
  210. $data['permissions_for_new_directories']['attributes']['data'] = $permissionNewDir;
  211. }
  212. $permissionNewFiles = $app['session']->get('permissions_for_new_files');
  213. if ($permissionNewFiles) {
  214. $data['permissions_for_new_files']['attributes']['data'] = $permissionNewFiles;
  215. }
  216. foreach ($data as $key => $value) {
  217. $value['attributes'] = isset($value['attributes']) && is_array($value['attributes']) ? $value['attributes'] : array();
  218. $builder->add($key, $value['type'], $value['attributes']);
  219. }
  220. $builder->add('continue', 'submit');
  221. $form = $builder->getForm();
  222. if ('POST' == $request->getMethod()) {
  223. $form->bind($request);
  224. if ($form->isValid()) {
  225. $data = $form->getData();
  226. $app['session']->set('portal_settings', $data);
  227. $url = $app['url_generator']->generate('admin-settings');
  228. return $app->redirect($url);
  229. }
  230. }
  231. return $app['twig']->render('settings.tpl', array('form' => $form->createView()));
  232. })->bind('portal-settings');
  233. // Admin settings.
  234. $app->match('/admin-settings', function() use($app) {
  235. $request = $app['request'];
  236. /** @var Chash\Command\Installation\InstallCommand $command */
  237. $command = $app['console']->get('chamilo:install');
  238. $data = $command->getAdminSettingsParams();
  239. $builder = $app['form.factory']->createBuilder('form', $data);
  240. foreach ($data as $key => $value) {
  241. $builder->add($key, $value['type'], $value['attributes']);
  242. }
  243. $builder->add('continue', 'submit');
  244. $form = $builder->getForm();
  245. if ('POST' == $request->getMethod()) {
  246. $form->bind($request);
  247. if ($form->isValid()) {
  248. $data = $form->getData();
  249. $app['session']->set('admin_settings', $data);
  250. $url = $app['url_generator']->generate('resume');
  251. return $app->redirect($url);
  252. }
  253. }
  254. return $app['twig']->render('settings.tpl', array('form' => $form->createView()));
  255. })->bind('admin-settings');
  256. // Resume before installing.
  257. $app->match('/resume', function() use($app) {
  258. $request = $app['request'];
  259. $data = array();
  260. $portalSettings = $app['session']->get('portal_settings');
  261. $databaseSettings = $app['session']->get('database_settings');
  262. $adminSettings = $app['session']->get('admin_settings');
  263. if (!empty($portalSettings) && !empty($databaseSettings) && !empty($adminSettings)) {
  264. $form = $app['form.factory']->createBuilder('form', $data)
  265. ->add('install', 'submit', array('label' => 'Install'))
  266. ->getForm();
  267. if ('POST' == $request->getMethod()) {
  268. $url = $app['url_generator']->generate('installing');
  269. return $app->redirect($url);
  270. }
  271. return $app['twig']->render(
  272. 'resume.tpl',
  273. array(
  274. 'form' => $form->createView(),
  275. 'portal_settings' => $portalSettings,
  276. 'database_settings' => $databaseSettings,
  277. 'admin_settings' => $adminSettings
  278. )
  279. );
  280. } else {
  281. $url = $app['url_generator']->generate('check-database');
  282. return $app->redirect($url);
  283. }
  284. })->bind('resume');
  285. // Installation process.
  286. $app->match('/installing', function() use($app) {
  287. $portalSettings = $app['session']->get('portal_settings');
  288. $adminSettings = $app['session']->get('admin_settings');
  289. $databaseSettings = $app['session']->get('database_settings');
  290. /** @var Chash\Command\Installation\InstallCommand $command */
  291. $command = $app['console']->get('chamilo:install');
  292. $def = $command->getDefinition();
  293. $input = new Symfony\Component\Console\Input\ArrayInput(
  294. array(
  295. 'name',
  296. 'path' => realpath(__DIR__.'/../../').'/',
  297. 'version' => '1.10.0'
  298. ),
  299. $def
  300. );
  301. $output = new BufferedOutput();
  302. $command->setPortalSettings($portalSettings);
  303. $command->setDatabaseSettings($databaseSettings);
  304. $command->setAdminSettings($adminSettings);
  305. $result = $command->run($input, $output);
  306. if ($result == 1) {
  307. $output = $output->getBuffer();
  308. $app['session']->getFlashBag()->add('success', 'Installation finished');
  309. $app['session']->set('output', $output);
  310. $url = $app['url_generator']->generate('finish');
  311. return $app->redirect($url);
  312. } else {
  313. $app['session']->getFlashBag()->add('error', 'There was an error during installation, please check your settings.');
  314. $app['session']->getFlashBag()->add('error', $output->lastMessage);
  315. $url = $app['url_generator']->generate('check-database');
  316. return $app->redirect($url);
  317. }
  318. })->bind('installing');
  319. // Finish installation.
  320. $app->get('/finish', function() use($app) {
  321. $output = $app['session']->get('output');
  322. $message = $app['translator']->trans(
  323. 'To protect your site, make the whole %s directory read-only (chmod 0555 on Unix/Linux)',
  324. array('%s' => $app['root_sys'].'config')
  325. );
  326. $app['session']->getFlashBag()->add('warning', $message);
  327. $message = $app['translator']->trans(
  328. 'Delete the %s directory.',
  329. array('%s' => $app['root_sys'].'install')
  330. );
  331. $app['session']->getFlashBag()->add('warning', $message);
  332. return $app['twig']->render('finish.tpl', array('output' => $output));
  333. })->bind('finish');
  334. // Middlewares.
  335. $app->before(
  336. function () use ($app) {
  337. }
  338. );
  339. // Errors
  340. $app->error(function (\Exception $e, $code) use ($app) {
  341. switch ($code) {
  342. case 404:
  343. $message = 'The requested page could not be found.';
  344. break;
  345. default:
  346. // $message = 'We are sorry, but something went terribly wrong.';
  347. $message = $e->getMessage();
  348. }
  349. $app['twig']->addGlobal('code', $code);
  350. $app['twig']->addGlobal('message', $message);
  351. return $app['twig']->render('error.tpl');
  352. });
  353. if (PHP_SAPI == 'cli') {
  354. $console->run();
  355. } else {
  356. $app->run();
  357. }