root/branches/2.0/wp-admin/import/rss.php

Revision 5406, 4.7 kB (checked in by ryan, 1 year ago)

Add nonces to importers

  • Property svn:eol-style set to native
Line 
1 <?php
2
3 class RSS_Import {
4
5     var $posts = array ();
6     var $file;
7
8     function header() {
9         echo '<div class="wrap">';
10         echo '<h2>'.__('Import RSS').'</h2>';
11     }
12
13     function footer() {
14         echo '</div>';
15     }
16
17     function unhtmlentities($string) { // From php.net for < 4.3 compat
18         $trans_tbl = get_html_translation_table(HTML_ENTITIES);
19         $trans_tbl = array_flip($trans_tbl);
20         return strtr($string, $trans_tbl);
21     }
22     
23     function greet() {
24         echo '<p>'.__('Howdy! This importer allows you to extract posts from any RSS 2.0 file into your blog. This is useful if you want to import your posts from a system that is not handled by a custom import tool. Pick an RSS file to upload and click Import.').'</p>';
25         wp_import_upload_form("admin.php?import=rss&amp;step=1");
26     }
27
28     function get_posts() {
29         global $wpdb;
30         
31         set_magic_quotes_runtime(0);
32         $datalines = file($this->file); // Read the file into an array
33         $importdata = implode('', $datalines); // squish it
34         $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
35
36         preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
37         $this->posts = $this->posts[1];
38         $index = 0;
39         foreach ($this->posts as $post) {
40             preg_match('|<title>(.*?)</title>|is', $post, $post_title);
41             $post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) ));
42
43             preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt);
44
45             if ($post_date_gmt) {
46                 $post_date_gmt = strtotime($post_date_gmt[1]);
47             } else {
48                 // if we don't already have something from pubDate
49                 preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt);
50                 $post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
51                 $post_date_gmt = str_replace('T', ' ', $post_date_gmt);
52                 $post_date_gmt = strtotime($post_date_gmt);
53             }
54
55             $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
56             $post_date = get_date_from_gmt( $post_date_gmt );
57
58             preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
59             $categories = $categories[1];
60
61             if (!$categories) {
62                 preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
63                 $categories = $categories[1];
64             }
65
66             $cat_index = 0;
67             foreach ($categories as $category) {
68                 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
69                 $cat_index++;
70             }
71
72             preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid);
73             if ($guid)
74                 $guid = $wpdb->escape(trim($guid[1]));
75             else
76                 $guid = '';
77
78             preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
79             $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
80
81             if (!$post_content) {
82                 // This is for feeds that put content in description
83                 preg_match('|<description>(.*?)</description>|is', $post, $post_content);
84                 $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1])));
85             }
86
87             // Clean up content
88             $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
89             $post_content = str_replace('<br>', '<br />', $post_content);
90             $post_content = str_replace('<hr>', '<hr />', $post_content);
91
92             $post_author = 1;
93             $post_status = 'publish';
94             $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
95             $index++;
96         }
97     }
98
99     function import_posts() {
100         echo '<ol>';
101
102         foreach ($this->posts as $post) {
103             echo "<li>".__('Importing post...');
104
105             extract($post);
106
107             if ($post_id = post_exists($post_title, $post_content, $post_date)) {
108                 _e('Post already imported');
109             } else {
110                 $post_id = wp_insert_post($post);
111                 if (!$post_id) {
112                     _e("Couldn't get post ID");
113                     return;
114                 }
115
116                 if (0 != count($categories))
117                     wp_create_categories($categories, $post_id);
118                 _e('Done !');
119             }
120             echo '</li>';
121         }
122
123         echo '</ol>';
124
125     }
126
127     function import() {
128         $file = wp_import_handle_upload();
129         if ( isset($file['error']) ) {
130             echo $file['error'];
131             return;
132         }
133
134         $this->file = $file['file'];
135         $this->get_posts();
136         $this->import_posts();
137         wp_import_cleanup($file['id']);
138         
139         echo '<h3>';
140         printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
141         echo '</h3>';
142     }
143
144     function dispatch() {
145         if (empty ($_GET['step']))
146             $step = 0;
147         else
148             $step = (int) $_GET['step'];
149
150         $this->header();
151         
152         switch ($step) {
153             case 0 :
154                 $this->greet();
155                 break;
156             case 1 :
157                 check_admin_referer('import-upload');
158                 $this->import();
159                 break;
160         }
161         
162         $this->footer();
163     }
164
165     function RSS_Import() {
166         // Nothing.   
167     }
168 }
169
170 $rss_import = new RSS_Import();
171
172 register_importer('rss', __('RSS'), __('Import posts from an RSS feed'), array ($rss_import, 'dispatch'));
173 ?>
174
Note: See TracBrowser for help on using the browser.