111
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

104 lines
3.4 KiB

  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2018年6月1日
  7. * 表单控制器
  8. */
  9. namespace app\home\controller;
  10. use app\home\model\ParserModel;
  11. use core\basic\Controller;
  12. class FormController extends Controller
  13. {
  14. protected $model;
  15. public function __construct()
  16. {
  17. $this->model = new ParserModel();
  18. }
  19. // 表单提交
  20. public function add()
  21. {
  22. if ($_POST) {
  23. if (time() - session('lastsub') < 10) {
  24. alert_back('您提交太频繁了,请稍后再试!');
  25. }
  26. if (! $fcode = get('fcode', 'var')) {
  27. alert_back('传递的表单编码有误!');
  28. }
  29. if ($fcode == 1) {
  30. alert_back('表单提交地址有误,留言提交请使用留言专用地址!');
  31. }
  32. // 验证码验证
  33. $checkcode = strtolower(post('checkcode', 'var'));
  34. if ($this->config('form_check_code')) {
  35. if (! $checkcode) {
  36. alert_back('验证码不能为空!');
  37. }
  38. if ($checkcode != session('checkcode')) {
  39. alert_back('验证码错误!');
  40. }
  41. }
  42. // 读取字段
  43. if (! $form = $this->model->getFormField($fcode)) {
  44. alert_back('接收表单不存在任何字段,请核对后重试!');
  45. }
  46. // 接收数据
  47. $mail_body = '';
  48. foreach ($form as $value) {
  49. $field_data = post($value->name);
  50. if (is_array($field_data)) { // 如果是多选等情况时转换
  51. $field_data = implode(',', $field_data);
  52. }
  53. $field_data = preg_replace_r('pboot:if', '', $field_data);
  54. if ($value->required && ! $field_data) {
  55. alert_back($value->description . '不能为空!');
  56. } else {
  57. $data[$value->name] = $field_data;
  58. $mail_body .= $value->description . ':' . $field_data . '<br>';
  59. }
  60. }
  61. // 设置创建时间
  62. if ($data) {
  63. $data['create_time'] = get_datetime();
  64. }
  65. // 写入数据
  66. if ($this->model->addForm($value->table_name, $data)) {
  67. session('lastsub', time()); // 记录最后提交时间
  68. $this->log('提交表单数据成功!');
  69. if ($this->config('form_send_mail') && $this->config('message_send_to')) {
  70. $mail_subject = "【PbootCMS】您有新的" . $value->form_name . "信息,请注意查收!";
  71. $mail_body .= '<br>来自网站 ' . get_http_url() . ' (' . date('Y-m-d H:i:s') . ')';
  72. sendmail($this->config(), $this->config('message_send_to'), $mail_subject, $mail_body);
  73. }
  74. alert_location('提交成功!', '-1', 1);
  75. } else {
  76. $this->log('提交表单数据失败!');
  77. alert_back('提交失败!');
  78. }
  79. } else {
  80. error('提交失败,请使用POST方式提交!');
  81. }
  82. }
  83. // 空拦截
  84. public function _empty()
  85. {
  86. error('您访问的地址有误,请核对后重试!');
  87. }
  88. }