1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of importExport, a plugin for DotClear2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2012 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | class dcImportFeed extends dcIeModule |
---|
15 | { |
---|
16 | protected $status = false; |
---|
17 | protected $feed_url = ''; |
---|
18 | |
---|
19 | public function setInfo() |
---|
20 | { |
---|
21 | $this->type = 'import'; |
---|
22 | $this->name = __('Feed import'); |
---|
23 | $this->description = __('Imports a feed as new entries.'); |
---|
24 | } |
---|
25 | |
---|
26 | public function process($do) |
---|
27 | { |
---|
28 | if ($do == 'ok') { |
---|
29 | $this->status = true; |
---|
30 | return; |
---|
31 | } |
---|
32 | |
---|
33 | if (empty($_POST['feed_url'])) { |
---|
34 | return; |
---|
35 | } |
---|
36 | |
---|
37 | $this->feed_url = $_POST['feed_url']; |
---|
38 | |
---|
39 | $feed = feedReader::quickParse($this->feed_url); |
---|
40 | if ($feed === false) { |
---|
41 | throw new Exception(__('Cannot retrieve feed URL.')); |
---|
42 | } |
---|
43 | if (count($feed->items) == 0) { |
---|
44 | throw new Exception(__('No items in feed.')); |
---|
45 | } |
---|
46 | |
---|
47 | $cur = $this->core->con->openCursor($this->core->prefix.'post'); |
---|
48 | $this->core->con->begin(); |
---|
49 | foreach ($feed->items as $item) |
---|
50 | { |
---|
51 | $cur->clean(); |
---|
52 | $cur->user_id = $this->core->auth->userID(); |
---|
53 | $cur->post_content = $item->content ? $item->content : $item->description; |
---|
54 | $cur->post_title = $item->title ? $item->title : text::cutString(html::clean($cur->post_content),60); |
---|
55 | $cur->post_format = 'xhtml'; |
---|
56 | $cur->post_status = -2; |
---|
57 | $cur->post_dt = strftime('%Y-%m-%d %H:%M:%S',$item->TS); |
---|
58 | |
---|
59 | try { |
---|
60 | $post_id = $this->core->blog->addPost($cur); |
---|
61 | } catch (Exception $e) { |
---|
62 | $this->core->con->rollback(); |
---|
63 | throw $e; |
---|
64 | } |
---|
65 | |
---|
66 | foreach ($item->subject as $subject) { |
---|
67 | $this->core->meta->setPostMeta($post_id,'tag',dcMeta::sanitizeMetaID($subject)); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | $this->core->con->commit(); |
---|
72 | http::redirect($this->getURL().'&do=ok'); |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | public function gui() |
---|
77 | { |
---|
78 | if ($this->status) { |
---|
79 | echo '<p class="message">'.__('Content successfully imported.').'</p>'; |
---|
80 | } |
---|
81 | |
---|
82 | echo |
---|
83 | '<form action="'.$this->getURL(true).'" method="post">'. |
---|
84 | '<fieldset><legend>'.__('Single blog').'</legend>'. |
---|
85 | '<p>'.sprintf(__('This will import a feed (RSS or Atom) a as new content in the current blog: %s.'),html::escapeHTML($this->core->blog->name)).'</p>'. |
---|
86 | |
---|
87 | '<p><label for="feed_url">'.__('Feed URL:').'</label>'. |
---|
88 | form::field('feed_url',50,300,html::escapeHTML($this->feed_url)).'</p>'. |
---|
89 | |
---|
90 | '<p>'. |
---|
91 | $this->core->formNonce(). |
---|
92 | form::hidden(array('do'),1). |
---|
93 | '<input type="submit" value="'.__('Import').'" /></p>'. |
---|
94 | |
---|
95 | '</fieldset>'. |
---|
96 | '</form>'; |
---|
97 | } |
---|
98 | } |
---|
99 | ?> |
---|