心理咨询网
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.

FormController.php 3.6KB

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