request->isPost()) { $params = $this->request->post(); try { validate(\addon\haonav\validate\Link::class)->check($params); } catch (ValidateException $e) { return $this->result->error('数据验证失败: ' . $e->getMessage()); } $enable = (int)ConfigureModel::getVal('enable_submit', '1'); $model = new LinksModel(); $model->cid = $params['cid']; $model->title = $params['title']; $model->url = $params['url']; $model->description = $params['description'] ?? ''; $model->keywords = $params['keywords'] ?? ''; $model->status = $enable ? 2 : 1; // 待审核 or 直接上架 $model->save(); return $this->result->success([], $enable ? '提交成功,等待管理员审核' : '提交成功'); } $this->redirect(url('haonav/apply'), 301); return; } /** * 友链/广告合作自助申请(公开) * GET 渲染表单页;POST 提交入库(待审核),后台「申请审核」处理。 * 防滥用:蜜罐字段 + 同 IP 限频(1小时5条)+ 同 URL 去重。 */ public function apply() { if ($this->request->isPost()) { // 蜜罐:正常用户不可见不填写,机器人常会填 if ((string)$this->request->post('website', '') !== '') { return $this->result->success([], '提交成功,等待管理员审核'); } $type = (int)$this->request->post('type/d', ApplyModel::TYPE_LINK); $title = trim((string)$this->request->post('title', '')); $url = trim((string)$this->request->post('url', '')); $desc = trim((string)$this->request->post('description', '')); $contact = trim((string)$this->request->post('contact', '')); $slot = trim((string)$this->request->post('slot', '')); if (!in_array($type, [ApplyModel::TYPE_LINK, ApplyModel::TYPE_AD], true)) { return $this->result->error('申请类型错误'); } if ($title === '' || mb_strlen($title) > 100) { return $this->result->error('请填写正确的名称(100字以内)'); } if (!filter_var($url, FILTER_VALIDATE_URL) || !preg_match('#^https?://#i', $url)) { return $this->result->error('请填写正确的网址(http/https 开头)'); } if ($contact === '' || mb_strlen($contact) > 100) { return $this->result->error('请留下联系方式,便于审核后联系您'); } if ($type === ApplyModel::TYPE_AD && $slot !== '' && !array_key_exists($slot, AdModel::slots())) { return $this->result->error('意向广告位不存在'); } $ip = (string)$this->request->ip(); if (ApplyModel::ipOverLimit($ip)) { return $this->result->error('提交过于频繁,请稍后再试'); } if (ApplyModel::urlExists($url)) { return $this->result->error('该网址已提交过申请,请勿重复提交'); } $model = new ApplyModel(); $model->type = $type; $model->title = $title; $model->url = $url; $model->description = mb_substr($desc, 0, 500); $model->contact = $contact; $model->slot = $type === ApplyModel::TYPE_AD ? $slot : ''; $model->ip = $ip; $model->status = ApplyModel::STATUS_PENDING; $model->save(); return $this->result->success([], '提交成功,管理员审核后会通过您留下的联系方式回复'); } $this->view->assign('slots', AdModel::slots()); $this->view->assign('config', $this->siteConfig()); $this->assignSeo(); return $this->view->fetch('apply'); } }