| 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 = __('RSS or Atom feed import'); |
|---|
| 23 | $this->description = __('Add a feed content to the blog.'); |
|---|
| 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 | dcPage::success(__('Content successfully imported.')); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | echo |
|---|
| 83 | '<form action="'.$this->getURL(true).'" method="post">'. |
|---|
| 84 | '<p>'.sprintf(__('Add a feed content to the current blog: <strong>%s</strong>.'),html::escapeHTML($this->core->blog->name)).'</p>'. |
|---|
| 85 | |
|---|
| 86 | '<p><label for="feed_url">'.__('Feed URL:').'</label>'. |
|---|
| 87 | form::field('feed_url',50,300,html::escapeHTML($this->feed_url)).'</p>'. |
|---|
| 88 | |
|---|
| 89 | '<p>'. |
|---|
| 90 | $this->core->formNonce(). |
|---|
| 91 | form::hidden(array('do'),1). |
|---|
| 92 | '<input type="submit" value="'.__('Import').'" /></p>'. |
|---|
| 93 | |
|---|
| 94 | '</form>'; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|