1 | <?php |
---|
2 | /** |
---|
3 | * @brief fairTrackbacks, an antispam filter plugin for Dotclear 2 |
---|
4 | * |
---|
5 | * @package Dotclear |
---|
6 | * @subpackage Plugins |
---|
7 | * |
---|
8 | * @copyright Olivier Meunier & Association Dotclear |
---|
9 | * @copyright GPL-2.0-only |
---|
10 | */ |
---|
11 | |
---|
12 | if (!defined('DC_RC_PATH')) {return;} |
---|
13 | |
---|
14 | class dcFilterFairTrackbacks extends dcSpamFilter |
---|
15 | { |
---|
16 | public $name = 'Fair Trackbacks'; |
---|
17 | public $has_gui = false; |
---|
18 | public $active = true; |
---|
19 | public $order = -10; |
---|
20 | |
---|
21 | public function __construct($core) |
---|
22 | { |
---|
23 | parent::__construct($core); |
---|
24 | } |
---|
25 | |
---|
26 | protected function setInfo() |
---|
27 | { |
---|
28 | $this->description = __('Checks trackback source for a link to the post'); |
---|
29 | } |
---|
30 | |
---|
31 | public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) |
---|
32 | { |
---|
33 | if ($type != 'trackback') { |
---|
34 | return; |
---|
35 | } |
---|
36 | |
---|
37 | try |
---|
38 | { |
---|
39 | $default_parse = ['scheme' => '', 'host' => '', 'path' => '', 'query' => '']; |
---|
40 | $S = array_merge($default_parse, parse_url($site)); |
---|
41 | |
---|
42 | if ($S['scheme'] != 'http' || !$S['host'] || !$S['path']) { |
---|
43 | throw new Exception('Invalid URL'); |
---|
44 | } |
---|
45 | |
---|
46 | # Check incomink link page |
---|
47 | $post = $this->core->blog->getPosts(['post_id' => $post_id]); |
---|
48 | $post_url = $post->getURL(); |
---|
49 | $P = array_merge($default_parse, parse_url($post_url)); |
---|
50 | |
---|
51 | if ($post_url == $site) { |
---|
52 | throw new Exception('Same source and destination'); |
---|
53 | } |
---|
54 | |
---|
55 | $o = netHttp::initClient($site, $path); |
---|
56 | $o->setTimeout(3); |
---|
57 | $o->get($path); |
---|
58 | |
---|
59 | # Trackback source does not return 200 status code |
---|
60 | if ($o->getStatus() != 200) { |
---|
61 | throw new Exception('Invalid Status Code'); |
---|
62 | } |
---|
63 | |
---|
64 | $tb_page = $o->getContent(); |
---|
65 | |
---|
66 | # Do we find a link to post in trackback source? |
---|
67 | if ($S['host'] == $P['host']) { |
---|
68 | $pattern = $P['path'] . ($P['query'] ? '?' . $P['query'] : ''); |
---|
69 | } else { |
---|
70 | $pattern = $post_url; |
---|
71 | } |
---|
72 | $pattern = preg_quote($pattern, '/'); |
---|
73 | |
---|
74 | if (!preg_match('/' . $pattern . '/', $tb_page)) { |
---|
75 | throw new Exception('Unfair'); |
---|
76 | } |
---|
77 | } catch (Exception $e) { |
---|
78 | throw new Exception('Trackback not allowed for this URL.'); |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|