SetupController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Typemill\Controllers;
  3. use \Symfony\Component\Yaml\Yaml;
  4. class SetupController extends Controller
  5. {
  6. public function setup($request, $response, $args)
  7. {
  8. $themes = $this->getThemes();
  9. $copyright = $this->getCopyright();
  10. $uri = $request->getUri();
  11. $base_url = $uri->getBaseUrl();
  12. $errors = false;
  13. /* Check, if setting folder is writable */
  14. if(!is_writable($this->c->get('settings')['settingsPath'])){ $errors['folder'] = 'Your settings folder is not writable.'; }
  15. $data = array(
  16. 'themes' => $themes,
  17. 'copyright' => $copyright,
  18. 'inputs' => false,
  19. 'errors' => $errors,
  20. 'base_url' => $base_url
  21. );
  22. $this->c->view->render($response, '/setup.twig', $data);
  23. }
  24. public function save($request, $response, $args)
  25. {
  26. if($request->isPost())
  27. {
  28. $params = $request->getParams();
  29. $copyright = $this->getCopyright();
  30. $themes = $this->getThemes();
  31. $errors = array();
  32. $uri = $request->getUri();
  33. $base_url = $uri->getBaseUrl();
  34. /* Validate Title */
  35. if(!isset($params['title'])){ $errors['title'] = 'Please add a title. '; }
  36. if(strlen($params['title']) < 2){ $errors['title'] = 'Title is too short (< 2). '; }
  37. if(strlen($params['title']) > 20){ $errors['title'] = 'Title is too long (> 20). '; }
  38. /* Validate Author */
  39. if(isset($params['author']) && !empty($params['author']))
  40. {
  41. if(strlen($params['author']) < 2){ $errors['author'] = 'Text is too short (< 2). '; }
  42. if(strlen($params['author']) > 40){ $errors['author'] .= 'Text is too long (> 40). '; }
  43. if(preg_match('/[\(\)\[\]\{\}\?\*\$\"\'\|<>=!;@#%§]/', $params['author'])){ $errors['author'] .= 'Only special chars like a,b a-b a_b a&b are allowed.'; }
  44. }
  45. /* Validate Year */
  46. if(!isset($params['year'])){ $errors['year'] = 'Please add a year, e.g. 2017.'; }
  47. if(!preg_match('/^(\d{4})$/', $params['year'])){ $errors['year'] = 'Use four digits for the year like 2017.'; }
  48. /* Validate Copyright */
  49. if(isset($params['copyright']) AND !in_array($params['copyright'], $copyright )){ $errors['copyright'] = 'Please select a valid copyright.'; }
  50. /* Validate Theme */
  51. if(!isset($params['theme']) AND !in_array($params['theme'], $themes)){ $errors['theme'] = 'Please select a valid theme.'; }
  52. /* Validate Startpage */
  53. if(isset($params['startpage'])){ $params['startpage'] = true; }else{ $params['startpage'] = false; }
  54. /* Validate Folder Writable */
  55. if(!is_writable($this->c->get('settings')['settingsPath'])){ $errors['folder'] = 'Your settings folder is not writable.'; }
  56. /* Prevent Title From Hacking */
  57. $params['title'] = htmlentities(stripslashes($params['title']));
  58. if(!empty($errors))
  59. {
  60. $data = array(
  61. 'themes' => $themes,
  62. 'copyright' => $copyright,
  63. 'errors' => $errors,
  64. 'inputs' => $params,
  65. 'base_url' => $base_url
  66. );
  67. $this->c->view->render($response, '/setup.twig', $data);
  68. }
  69. else
  70. {
  71. $file = $this->c->get('settings')['settingsPath'] . DIRECTORY_SEPARATOR . 'settings.yaml';
  72. $fh = fopen($file, 'w');
  73. $yaml = Yaml::dump($params);
  74. file_put_contents($file, $yaml);
  75. $data = array(
  76. 'inputs' => $params,
  77. 'base_url' => $base_url
  78. );
  79. $this->c->view->render($response, '/welcome.twig', $data);
  80. }
  81. }
  82. }
  83. private function getCopyright()
  84. {
  85. return array(
  86. "©",
  87. "CC-BY",
  88. "CC-BY-NC",
  89. "CC-BY-NC-ND",
  90. "CC-BY-NC-SA",
  91. "CC-BY-ND",
  92. "CC-BY-SA",
  93. "None"
  94. );
  95. }
  96. private function getThemes()
  97. {
  98. $themeFolder = $this->c->get('settings')['rootPath'] . $this->c->get('settings')['themeFolder'];
  99. $themeFolderC = scandir($themeFolder);
  100. $themes = array();
  101. foreach ($themeFolderC as $key => $theme)
  102. {
  103. if (!in_array($theme, array(".","..")))
  104. {
  105. if (is_dir($themeFolder . DIRECTORY_SEPARATOR . $theme))
  106. {
  107. $themes[] = $theme;
  108. }
  109. }
  110. }
  111. return $themes;
  112. }
  113. }