FormController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Typemill\Models\Validation;
  4. use Typemill\Models\WriteYaml;
  5. class FormController extends Controller
  6. {
  7. /*************************************
  8. ** SAVE THEME- AND PLUGIN-SETTINGS **
  9. *************************************/
  10. public function savePublicForm($request, $response, $args)
  11. {
  12. if($request->isPost())
  13. {
  14. $params = $request->getParams();
  15. reset($params);
  16. $pluginName = key($params);
  17. $referer = $request->getHeader('HTTP_REFERER');
  18. # simple bot check with honeypot
  19. if(isset($params[$pluginName]['personal-mail']))
  20. {
  21. if($params[$pluginName]['personal-mail'] != '')
  22. {
  23. $this->c->flash->addMessage('publicform', 'bot');
  24. return $response->withRedirect($referer[0]);
  25. }
  26. unset($params[$pluginName]['personal-mail']);
  27. }
  28. #recaptcha check
  29. if(isset($params['g-recaptcha-response']))
  30. {
  31. $recaptchaApi = 'https://www.google.com/recaptcha/api/siteverify';
  32. $settings = $this->c->get('settings');
  33. $secret = isset($settings['plugins'][$pluginName]['recaptcha_secretkey']) ? $settings['plugins'][$pluginName]['recaptcha_secretkey'] : false;
  34. $recaptchaRequest = ['secret' => $secret, 'response' => $params['g-recaptcha-response']];
  35. # use key 'http' even if you send the request to https://...
  36. $options = array(
  37. 'http' => array(
  38. 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  39. 'method' => 'POST',
  40. 'content' => http_build_query($recaptchaRequest),
  41. 'timeout' => 5
  42. )
  43. );
  44. $context = stream_context_create($options);
  45. $result = file_get_contents($recaptchaApi, false, $context);
  46. $result = json_decode($result);
  47. if ($result === FALSE || $result->success === FALSE)
  48. {
  49. $this->c->flash->addMessage('publicform', 'bot');
  50. return $response->withRedirect($referer[0]);
  51. }
  52. }
  53. if(isset($params[$pluginName]))
  54. {
  55. # validate the user-input
  56. $this->validateInput('plugins', $pluginName, $params[$pluginName]);
  57. }
  58. # check for errors and redirect to path, if errors found
  59. if(isset($_SESSION['errors']))
  60. {
  61. $this->c->flash->addMessage('error', 'Please correct the errors');
  62. return $response->withRedirect($referer[0]);
  63. }
  64. # clean up and make sure that only validated data are stored
  65. $data = [ $pluginName => $params[$pluginName]];
  66. # create write object
  67. $writeYaml = new WriteYaml();
  68. # write the form data into yaml file
  69. $writeYaml->updateYaml('settings', 'formdata.yaml', $data);
  70. # add message and return to original site
  71. $this->c->flash->addMessage('formdata', $pluginName);
  72. return $response->withRedirect($referer[0]);
  73. }
  74. }
  75. private function validateInput($objectType, $objectName, $userInput)
  76. {
  77. # get settings and start validation
  78. $originalSettings = \Typemill\Settings::getObjectSettings($objectType, $objectName);
  79. $userSettings = \Typemill\Settings::getUserSettings();
  80. $validate = new Validation();
  81. if(isset($originalSettings['public']['fields']))
  82. {
  83. /* flaten the multi-dimensional array with fieldsets to a one-dimensional array */
  84. $originalFields = array();
  85. foreach($originalSettings['public']['fields'] as $fieldName => $fieldValue)
  86. {
  87. if(isset($fieldValue['fields']))
  88. {
  89. foreach($fieldValue['fields'] as $subFieldName => $subFieldValue)
  90. {
  91. $originalFields[$subFieldName] = $subFieldValue;
  92. }
  93. }
  94. else
  95. {
  96. $originalFields[$fieldName] = $fieldValue;
  97. }
  98. }
  99. /* take the user input data and iterate over all fields and values */
  100. foreach($userInput as $fieldName => $fieldValue)
  101. {
  102. /* get the corresponding field definition from original plugin settings */
  103. $fieldDefinition = isset($originalFields[$fieldName]) ? $originalFields[$fieldName] : false;
  104. if($fieldDefinition)
  105. {
  106. /* validate user input for this field */
  107. $validate->objectField($fieldName, $fieldValue, $objectName, $fieldDefinition);
  108. }
  109. if(!$fieldDefinition && $fieldName != 'active')
  110. {
  111. $_SESSION['errors'][$objectName][$fieldName] = array('This field is not defined!');
  112. }
  113. }
  114. }
  115. }
  116. }