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

MessageController.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. use core\basic\Url;
  13. class MessageController extends Controller
  14. {
  15. protected $model;
  16. public function __construct()
  17. {
  18. $this->model = new ParserModel();
  19. }
  20. // 留言新增
  21. public function index()
  22. {
  23. if ($_POST) {
  24. if ($this->config('message_status') === '0') {
  25. error('系统已经关闭留言功能,请到后台开启再试!');
  26. }
  27. if (time() - session('lastsub') < 10) {
  28. alert_back('您提交太频繁了,请稍后再试!');
  29. }
  30. // 需登录
  31. if ($this->config('message_rqlogin') && ! session('pboot_uid')) {
  32. if (! ! $backurl = $_SERVER['HTTP_REFERER']) {
  33. alert_location("请先注册登录后再留言!", Url::home('member/login', null, "backurl=" . urlencode($backurl)));
  34. } else {
  35. alert_location("请先注册登录后再留言!", Url::home('member/login'));
  36. }
  37. }
  38. // 验证码验证
  39. $checkcode = strtolower(post('checkcode', 'var'));
  40. if ($this->config('message_check_code') !== '0') {
  41. if (! $checkcode) {
  42. alert_back('验证码不能为空!');
  43. }
  44. if ($checkcode != session('checkcode')) {
  45. alert_back('验证码错误!');
  46. }
  47. }
  48. // 读取字段
  49. if (! $form = $this->model->getFormField(1)) {
  50. alert_back('留言表单不存在任何字段,请核对后重试!');
  51. }
  52. // 接收数据
  53. $mail_body = '';
  54. foreach ($form as $value) {
  55. $field_data = post($value->name);
  56. if (is_array($field_data)) { // 如果是多选等情况时转换
  57. $field_data = implode(',', $field_data);
  58. }
  59. $field_data = preg_replace_r('/pboot:if/i', '', $field_data);
  60. if ($value->required && ! $field_data) {
  61. alert_back($value->description . '不能为空!');
  62. } else {
  63. $data[$value->name] = $field_data;
  64. $mail_body .= $value->description . ':' . $field_data . '<br>';
  65. }
  66. }
  67. $status = $this->config('message_verify') === '0' ? 1 : 0;
  68. // 设置额外数据
  69. if ($data) {
  70. $data['acode'] = get_lg();
  71. $data['user_ip'] = ip2long(get_user_ip());
  72. $data['user_os'] = get_user_os();
  73. $data['user_bs'] = get_user_bs();
  74. $data['recontent'] = '';
  75. $data['status'] = $status;
  76. $data['create_user'] = 'guest';
  77. $data['update_user'] = 'guest';
  78. $data['uid'] = session('pboot_uid');
  79. }
  80. if ($this->model->addMessage($data)) {
  81. session('lastsub', time()); // 记录最后提交时间
  82. $this->log('留言提交成功!');
  83. if ($this->config('message_send_mail') && $this->config('message_send_to')) {
  84. $mail_subject = "【" . CMSNAME . "】您有新的" . $value->form_name . "信息,请注意查收!";
  85. $mail_body .= '<br>来自网站 ' . get_http_url() . ' (' . date('Y-m-d H:i:s') . ')';
  86. sendmail($this->config(), $this->config('message_send_to'), $mail_subject, $mail_body);
  87. }
  88. alert_location('提交成功!', '-1', 1);
  89. } else {
  90. $this->log('留言提交失败!');
  91. alert_back('提交失败!');
  92. }
  93. } else {
  94. alert_back('提交失败,请使用POST方式提交!');
  95. }
  96. }
  97. }