1 | <?php |
---|
2 | /** |
---|
3 | * @brief importExport, a 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 dcImportFeed extends dcIeModule |
---|
15 | { |
---|
16 | protected $status = false; |
---|
17 | protected $feed_url = ''; |
---|
18 | |
---|
19 | // IPv6 functions (from https://gist.github.com/tbaschak/7866688) |
---|
20 | private function gethostbyname6($host, $try_a = false) |
---|
21 | { |
---|
22 | // get AAAA record for $host |
---|
23 | // if $try_a is true, if AAAA fails, it tries for A |
---|
24 | // the first match found is returned |
---|
25 | // otherwise returns false |
---|
26 | |
---|
27 | $dns = $this->gethostbynamel6($host, $try_a); |
---|
28 | if ($dns == false) { |
---|
29 | return false; |
---|
30 | } else { |
---|
31 | return $dns[0]; |
---|
32 | } |
---|
33 | } |
---|
34 | private function gethostbynamel6($host, $try_a = false) |
---|
35 | { |
---|
36 | // get AAAA records for $host, |
---|
37 | // if $try_a is true, if AAAA fails, it tries for A |
---|
38 | // results are returned in an array of ips found matching type |
---|
39 | // otherwise returns false |
---|
40 | |
---|
41 | $dns6 = dns_get_record($host, DNS_AAAA); |
---|
42 | if ($try_a == true) { |
---|
43 | $dns4 = dns_get_record($host, DNS_A); |
---|
44 | $dns = array_merge($dns4, $dns6); |
---|
45 | } else { |
---|
46 | $dns = $dns6; |
---|
47 | } |
---|
48 | $ip6 = []; |
---|
49 | $ip4 = []; |
---|
50 | foreach ($dns as $record) { |
---|
51 | if ($record["type"] == "A") { |
---|
52 | $ip4[] = $record["ip"]; |
---|
53 | } |
---|
54 | if ($record["type"] == "AAAA") { |
---|
55 | $ip6[] = $record["ipv6"]; |
---|
56 | } |
---|
57 | } |
---|
58 | if (count($ip6) < 1) { |
---|
59 | if ($try_a == true) { |
---|
60 | if (count($ip4) < 1) { |
---|
61 | return false; |
---|
62 | } else { |
---|
63 | return $ip4; |
---|
64 | } |
---|
65 | } else { |
---|
66 | return false; |
---|
67 | } |
---|
68 | } else { |
---|
69 | return $ip6; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | public function setInfo() |
---|
74 | { |
---|
75 | $this->type = 'import'; |
---|
76 | $this->name = __('RSS or Atom feed import'); |
---|
77 | $this->description = __('Add a feed content to the blog.'); |
---|
78 | } |
---|
79 | |
---|
80 | public function process($do) |
---|
81 | { |
---|
82 | if ($do == 'ok') { |
---|
83 | $this->status = true; |
---|
84 | return; |
---|
85 | } |
---|
86 | |
---|
87 | if (empty($_POST['feed_url'])) { |
---|
88 | return; |
---|
89 | } |
---|
90 | |
---|
91 | $this->feed_url = $_POST['feed_url']; |
---|
92 | |
---|
93 | // Check feed URL |
---|
94 | if ($this->core->blog->settings->system->import_feed_url_control) { |
---|
95 | // Get IP from URL |
---|
96 | $bits = parse_url($this->feed_url); |
---|
97 | if (!$bits || !isset($bits['host'])) { |
---|
98 | throw new Exception(__('Cannot retrieve feed URL.')); |
---|
99 | } |
---|
100 | $ip = gethostbyname($bits['host']); |
---|
101 | if ($ip == $bits['host']) { |
---|
102 | $ip = $this->gethostbyname6($bits['host']); |
---|
103 | if (!$ip) { |
---|
104 | throw new Exception(__('Cannot retrieve feed URL.')); |
---|
105 | } |
---|
106 | } |
---|
107 | // Check feed IP |
---|
108 | $flag = FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6; |
---|
109 | if ($this->core->blog->settings->system->import_feed_no_private_ip) { |
---|
110 | $flag |= FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE; |
---|
111 | } |
---|
112 | if (!filter_var($ip, $flag)) { |
---|
113 | throw new Exception(__('Cannot retrieve feed URL.')); |
---|
114 | } |
---|
115 | // IP control (white list regexp) |
---|
116 | if ($this->core->blog->settings->system->import_feed_ip_regexp != '') { |
---|
117 | if (!preg_match($this->core->blog->settings->system->import_feed_ip_regexp, $ip)) { |
---|
118 | throw new Exception(__('Cannot retrieve feed URL.')); |
---|
119 | } |
---|
120 | } |
---|
121 | // Port control (white list regexp) |
---|
122 | if ($this->core->blog->settings->system->import_feed_port_regexp != '' && isset($bits['port'])) { |
---|
123 | if (!preg_match($this->core->blog->settings->system->import_feed_port_regexp, $bits['port'])) { |
---|
124 | throw new Exception(__('Cannot retrieve feed URL.')); |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | $feed = feedReader::quickParse($this->feed_url); |
---|
130 | if ($feed === false) { |
---|
131 | throw new Exception(__('Cannot retrieve feed URL.')); |
---|
132 | } |
---|
133 | if (count($feed->items) == 0) { |
---|
134 | throw new Exception(__('No items in feed.')); |
---|
135 | } |
---|
136 | |
---|
137 | $cur = $this->core->con->openCursor($this->core->prefix . 'post'); |
---|
138 | $this->core->con->begin(); |
---|
139 | foreach ($feed->items as $item) { |
---|
140 | $cur->clean(); |
---|
141 | $cur->user_id = $this->core->auth->userID(); |
---|
142 | $cur->post_content = $item->content ?: $item->description; |
---|
143 | $cur->post_title = $item->title ?: text::cutString(html::clean($cur->post_content), 60); |
---|
144 | $cur->post_format = 'xhtml'; |
---|
145 | $cur->post_status = -2; |
---|
146 | $cur->post_dt = strftime('%Y-%m-%d %H:%M:%S', $item->TS); |
---|
147 | |
---|
148 | try { |
---|
149 | $post_id = $this->core->blog->addPost($cur); |
---|
150 | } catch (Exception $e) { |
---|
151 | $this->core->con->rollback(); |
---|
152 | throw $e; |
---|
153 | } |
---|
154 | |
---|
155 | foreach ($item->subject as $subject) { |
---|
156 | $this->core->meta->setPostMeta($post_id, 'tag', dcMeta::sanitizeMetaID($subject)); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | $this->core->con->commit(); |
---|
161 | http::redirect($this->getURL() . '&do=ok'); |
---|
162 | |
---|
163 | } |
---|
164 | |
---|
165 | public function gui() |
---|
166 | { |
---|
167 | if ($this->status) { |
---|
168 | dcPage::success(__('Content successfully imported.')); |
---|
169 | } |
---|
170 | |
---|
171 | echo |
---|
172 | '<form action="' . $this->getURL(true) . '" method="post">' . |
---|
173 | '<p>' . sprintf(__('Add a feed content to the current blog: <strong>%s</strong>.'), html::escapeHTML($this->core->blog->name)) . '</p>' . |
---|
174 | |
---|
175 | '<p><label for="feed_url">' . __('Feed URL:') . '</label>' . |
---|
176 | form::url('feed_url', 50, 300, html::escapeHTML($this->feed_url)) . '</p>' . |
---|
177 | |
---|
178 | '<p>' . |
---|
179 | $this->core->formNonce() . |
---|
180 | form::hidden(['do'], 1) . |
---|
181 | '<input type="submit" value="' . __('Import') . '" /></p>' . |
---|
182 | |
---|
183 | '</form>'; |
---|
184 | } |
---|
185 | } |
---|