index.php 18 KB

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