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

Revision 5406, 5.2 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 LJ_Import {
4
5     var $file;
6
7     function header() {
8         echo '<div class="wrap">';
9         echo '<h2>'.__('Import LiveJournal').'</h2>';
10     }
11
12     function footer() {
13         echo '</div>';
14     }
15
16     function unhtmlentities($string) { // From php.net for < 4.3 compat
17         $trans_tbl = get_html_translation_table(HTML_ENTITIES);
18         $trans_tbl = array_flip($trans_tbl);
19         return strtr($string, $trans_tbl);
20     }
21     
22     function greet() {
23         echo '<p>'.__('Howdy! This importer allows you to extract posts from LiveJournal XML export file into your blog.  Pick a LiveJournal file to upload and click Import.').'</p>';
24         wp_import_upload_form("admin.php?import=livejournal&amp;step=1");
25     }
26
27     function import_posts() {
28         global $wpdb, $current_user;
29         
30         set_magic_quotes_runtime(0);
31         $importdata = file($this->file); // Read the file into an array
32         $importdata = implode('', $importdata); // squish it
33         $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
34
35         preg_match_all('|<entry>(.*?)</entry>|is', $importdata, $posts);
36         $posts = $posts[1];
37         unset($importdata);
38         echo '<ol>';       
39         foreach ($posts as $post) {
40             flush();
41             preg_match('|<subject>(.*?)</subject>|is', $post, $post_title);
42             $post_title = $wpdb->escape(trim($post_title[1]));
43             if ( empty($post_title) ) {
44                 preg_match('|<itemid>(.*?)</itemid>|is', $post, $post_title);
45                 $post_title = $wpdb->escape(trim($post_title[1]));
46             }
47
48             preg_match('|<eventtime>(.*?)</eventtime>|is', $post, $post_date);
49             $post_date = strtotime($post_date[1]);
50             $post_date = gmdate('Y-m-d H:i:s', $post_date);
51
52             preg_match('|<event>(.*?)</event>|is', $post, $post_content);
53             $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
54             $post_content = $this->unhtmlentities($post_content);
55
56             // Clean up content
57             $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
58             $post_content = str_replace('<br>', '<br />', $post_content);
59             $post_content = str_replace('<hr>', '<hr />', $post_content);
60             $post_content = $wpdb->escape($post_content);
61
62             $post_author = $current_user->ID;
63             $post_status = 'publish';
64
65             echo '<li>';
66             if ($post_id = post_exists($post_title, $post_content, $post_date)) {
67                 printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
68             } else {
69                 printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
70                 $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
71                 $post_id = wp_insert_post($postdata);
72                 if (!$post_id) {
73                     _e("Couldn't get post ID");
74                     echo '</li>';
75                     break;
76                 }
77             }
78
79             preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
80             $comments = $comments[1];
81             
82             if ( $comments ) {
83                 $comment_post_ID = (int) $post_id;
84                 $num_comments = 0;
85                 foreach ($comments as $comment) {
86                     preg_match('|<event>(.*?)</event>|is', $comment, $comment_content);
87                     $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
88                     $comment_content = $this->unhtmlentities($comment_content);
89
90                     // Clean up content
91                     $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content);
92                     $comment_content = str_replace('<br>', '<br />', $comment_content);
93                     $comment_content = str_replace('<hr>', '<hr />', $comment_content);
94                     $comment_content = $wpdb->escape($comment_content);
95
96                     preg_match('|<eventtime>(.*?)</eventtime>|is', $comment, $comment_date);
97                     $comment_date = trim($comment_date[1]);
98                     $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
99
100                     preg_match('|<name>(.*?)</name>|is', $comment, $comment_author);
101                     $comment_author = $wpdb->escape(trim($comment_author[1]));
102
103                     preg_match('|<email>(.*?)</email>|is', $comment, $comment_author_email);
104                     $comment_author_email = $wpdb->escape(trim($comment_author_email[1]));
105
106                     $comment_approved = 1;
107                     // Check if it's already there
108                     if (!comment_exists($comment_author, $comment_date)) {
109                         $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
110                         $commentdata = wp_filter_comment($commentdata);
111                         wp_insert_comment($commentdata);
112                         $num_comments++;
113                     }
114                 }
115             }
116             if ( $num_comments ) {
117                 echo ' ';
118                 printf(__('(%s comments)'), $num_comments);
119             }
120             echo '</li>';
121             flush();
122             ob_flush();
123         }
124         echo '</ol>';
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->import_posts();
136         wp_import_cleanup($file['id']);
137         
138         echo '<h3>';
139         printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
140         echo '</h3>';
141     }
142
143     function dispatch() {
144         if (empty ($_GET['step']))
145             $step = 0;
146         else
147             $step = (int) $_GET['step'];
148
149         $this->header();
150         
151         switch ($step) {
152             case 0 :
153                 $this->greet();
154                 break;
155             case 1 :
156                 check_admin_referer('import-upload');
157                 $this->import();
158                 break;
159         }
160         
161         $this->footer();
162     }
163
164     function LJ_Import() {
165         // Nothing.   
166     }
167 }
168
169 $livejournal_import = new LJ_Import();
170
171 register_importer('livejournal', __('LiveJournal'), __('Import posts from LiveJournal'), array ($livejournal_import, 'dispatch'));
172 ?>
173
Note: See TracBrowser for help on using the browser.