SetupController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Typemill\Controllers;
  3. use \Symfony\Component\Yaml\Yaml;
  4. use Typemill\Models\Validation;
  5. use Typemill\Models\User;
  6. use Typemill\Models\Write;
  7. class SetupController extends Controller
  8. {
  9. # redirect if visit /setup route
  10. public function redirect($request, $response)
  11. {
  12. return $response->withRedirect($this->c->router->pathFor('setup.show'));
  13. }
  14. public function show($request, $response, $args)
  15. {
  16. /* make some checks befor you install */
  17. $checkFolder = new Write();
  18. $systemcheck = array();
  19. # check folders and create them if possible
  20. try{ $checkFolder->checkPath('settings'); }catch(\Exception $e){ $systemcheck['error'][] = $e->getMessage(); }
  21. try{ $checkFolder->checkPath('settings/users'); }catch(\Exception $e){ $systemcheck['error'][] = $e->getMessage(); }
  22. try{ $checkFolder->checkPath('content'); }catch(\Exception $e){ $systemcheck['error'][] = $e->getMessage(); }
  23. try{ $checkFolder->checkPath('cache'); }catch(\Exception $e){ $systemcheck['error'][] = $e->getMessage(); }
  24. try{ $checkFolder->checkPath('media'); }catch(\Exception $e){ $systemcheck['error'][] = $e->getMessage(); }
  25. # check php-version
  26. if (version_compare(phpversion(), '7.0.0', '<')) {
  27. $systemcheck['error'][] = 'The PHP-version of your server is ' . phpversion() . ' and Typemill needs at least 7.0.0';
  28. }
  29. /* check if mod rewrite is enabled, does not work with PHP-fpm or NGINX
  30. $modules = apache_get_modules();
  31. if(!in_array('mod_rewrite', $modules))
  32. {
  33. $systemcheck['error'][] = 'The apache module "mod_rewrite" is not enabled.';
  34. }
  35. */
  36. # check if GD extension is enabled
  37. if(!extension_loaded('gd')){
  38. $systemcheck['error'][] = 'The php-extension GD for image manipulation is not enabled.';
  39. }
  40. $setuperrors = empty($systemcheck) ? false : 'Some system requirements for Typemill are missing.';
  41. $systemcheck = empty($systemcheck) ? false : $systemcheck;
  42. # Get the translated strings
  43. $labels = $this->getSetupLabels();
  44. return $this->render($response, 'auth/setup.twig', array( 'messages' => $setuperrors, 'systemcheck' => $systemcheck, 'labels' => $labels ));
  45. }
  46. public function getSetupLabels()
  47. {
  48. # Check which languages are available
  49. $langs = [];
  50. $path = __DIR__ . '/../author/languages/*.yaml';
  51. foreach (glob($path) as $filename) {
  52. $langs[] = basename($filename,'.yaml');
  53. }
  54. # Detect browser language
  55. $accept_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  56. $lang = in_array($accept_lang, $langs) ? $accept_lang : 'en';
  57. # At least in the setup phase noon there should be no plugins and the theme should be typemill
  58. $labels = \Typemill\Settings::getLanguageLabels($lang,'typemill',[]);
  59. return $labels;
  60. }
  61. public function create($request, $response, $args)
  62. {
  63. if($request->isPost())
  64. {
  65. $params = $request->getParams();
  66. $validate = new Validation();
  67. $user = new User();
  68. /* set user as admin */
  69. $params['userrole'] = 'administrator';
  70. /* get userroles for validation */
  71. $userroles = $user->getUserroles();
  72. /* validate user */
  73. if($validate->newUser($params, $userroles))
  74. {
  75. $userdata = array('username' => $params['username'], 'email' => $params['email'], 'userrole' => $params['userrole'], 'password' => $params['password']);
  76. /* create initial user */
  77. $username = $user->createUser($userdata);
  78. if($username)
  79. {
  80. /* login user */
  81. $user->login($username);
  82. # create initial settings file
  83. \Typemill\Settings::createSettings();
  84. return $response->withRedirect($this->c->router->pathFor('setup.welcome'));
  85. }
  86. }
  87. $this->c->flash->addMessage('error', 'Please check your input and try again');
  88. return $response->withRedirect($this->c->router->pathFor('setup.show'));
  89. }
  90. }
  91. public function welcome($request, $response, $args)
  92. {
  93. /* store updated settings */
  94. \Typemill\Settings::updateSettings(array('welcome' => false));
  95. # Get the translated strings
  96. $labels = $this->getSetupLabels();
  97. return $this->render($response, 'auth/welcome.twig', array( 'labels' => $labels ));
  98. }
  99. }