FormController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if(isset($params[$pluginName]))
  29. {
  30. # validate the user-input
  31. $this->validateInput('plugins', $pluginName, $params[$pluginName]);
  32. }
  33. # check for errors and redirect to path, if errors found
  34. if(isset($_SESSION['errors']))
  35. {
  36. $this->c->flash->addMessage('error', 'Please correct the errors');
  37. return $response->withRedirect($referer[0]);
  38. }
  39. # clean up and make sure that only validated data are stored
  40. $data = [ $pluginName => $params[$pluginName]];
  41. # create write object
  42. $writeYaml = new WriteYaml();
  43. # write the form data into yaml file
  44. $writeYaml->updateYaml('settings', 'formdata.yaml', $data);
  45. # add message and return to original site
  46. $this->c->flash->addMessage('formdata', $pluginName);
  47. return $response->withRedirect($referer[0]);
  48. }
  49. }
  50. private function validateInput($objectType, $objectName, $userInput)
  51. {
  52. # get settings and start validation
  53. $originalSettings = \Typemill\Settings::getObjectSettings($objectType, $objectName);
  54. $userSettings = \Typemill\Settings::getUserSettings();
  55. $validate = new Validation();
  56. if(isset($originalSettings['public']['fields']))
  57. {
  58. /* flaten the multi-dimensional array with fieldsets to a one-dimensional array */
  59. $originalFields = array();
  60. foreach($originalSettings['public']['fields'] as $fieldName => $fieldValue)
  61. {
  62. if(isset($fieldValue['fields']))
  63. {
  64. foreach($fieldValue['fields'] as $subFieldName => $subFieldValue)
  65. {
  66. $originalFields[$subFieldName] = $subFieldValue;
  67. }
  68. }
  69. else
  70. {
  71. $originalFields[$fieldName] = $fieldValue;
  72. }
  73. }
  74. /* take the user input data and iterate over all fields and values */
  75. foreach($userInput as $fieldName => $fieldValue)
  76. {
  77. /* get the corresponding field definition from original plugin settings */
  78. $fieldDefinition = isset($originalFields[$fieldName]) ? $originalFields[$fieldName] : false;
  79. if($fieldDefinition)
  80. {
  81. /* validate user input for this field */
  82. $validate->objectField($fieldName, $fieldValue, $objectName, $fieldDefinition);
  83. }
  84. if(!$fieldDefinition && $fieldName != 'active')
  85. {
  86. $_SESSION['errors'][$objectName][$fieldName] = array('This field is not defined!');
  87. }
  88. }
  89. }
  90. }
  91. }