1 | <?php |
---|
2 | |
---|
3 | class Blogger_Import { |
---|
4 | |
---|
5 | var $lump_authors = false; |
---|
6 | var $import = array(); |
---|
7 | |
---|
8 | // Shows the welcome screen and the magic iframe. |
---|
9 | function greet() { |
---|
10 | $title = __('Import Blogger'); |
---|
11 | $welcome = __('Howdy! This importer allows you to import posts and comments from your Blogger account into your WordPress blog.'); |
---|
12 | $noiframes = __('This feature requires iframe support.'); |
---|
13 | $warning = __('This will delete everything saved by the Blogger importer except your posts and comments. Are you sure you want to do this?'); |
---|
14 | $reset = __('Reset this importer'); |
---|
15 | echo "<div class='wrap'><h2>$title</h2><p>$welcome</p><iframe src='admin.php?import=blogger&noheader=true' height='350px' width = '99%'>$noiframes</iframe><p><a href='admin.php?import=blogger&restart=true&noheader=true' onclick='return confirm(\"$warning\")'>$reset</a></p></div>\n"; |
---|
16 | } |
---|
17 | |
---|
18 | // Deletes saved data and redirect. |
---|
19 | function restart() { |
---|
20 | delete_option('import-blogger'); |
---|
21 | header("Location: admin.php?import=blogger"); |
---|
22 | die(); |
---|
23 | } |
---|
24 | |
---|
25 | // Generates a string that will make the page reload in a specified interval. |
---|
26 | function refresher($msec) { |
---|
27 | if ( $msec ) |
---|
28 | return "<html><head><script type='text/javascript'>window.onload=setTimeout('window.location.reload()', $msec);</script>\n</head>\n<body>\n"; |
---|
29 | else |
---|
30 | return "<html><head><script type='text/javascript'>window.onload=window.location.reload();</script>\n</head>\n<body>\n"; |
---|
31 | } |
---|
32 | |
---|
33 | // Returns associative array of code, header, cookies, body. Based on code from php.net. |
---|
34 | function parse_response($this_response) { |
---|
35 | // Split response into header and body sections |
---|
36 | list($response_headers, $response_body) = explode("\r\n\r\n", $this_response, 2); |
---|
37 | $response_header_lines = explode("\r\n", $response_headers); |
---|
38 | |
---|
39 | // First line of headers is the HTTP response code |
---|
40 | $http_response_line = array_shift($response_header_lines); |
---|
41 | if(preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line, $matches)) { $response_code = $matches[1]; } |
---|
42 | |
---|
43 | // put the rest of the headers in an array |
---|
44 | $response_header_array = array(); |
---|
45 | foreach($response_header_lines as $header_line) { |
---|
46 | list($header,$value) = explode(': ', $header_line, 2); |
---|
47 | $response_header_array[$header] .= $value."\n"; |
---|
48 | } |
---|
49 | |
---|
50 | $cookie_array = array(); |
---|
51 | $cookies = explode("\n", $response_header_array["Set-Cookie"]); |
---|
52 | foreach($cookies as $this_cookie) { array_push($cookie_array, "Cookie: ".$this_cookie); } |
---|
53 | |
---|
54 | return array("code" => $response_code, "header" => $response_header_array, "cookies" => $cookie_array, "body" => $response_body); |
---|
55 | } |
---|
56 | |
---|
57 | // Prints a form for the user to enter Blogger creds. |
---|
58 | function login_form($text='') { |
---|
59 | echo '<h1>' . __('Log in to Blogger') . "</h1>\n$text\n"; |
---|
60 | echo '<form method="post" action="admin.php?import=blogger&noheader=true&step=0"><table><tr><td>' . __('Username') . ':</td><td><input type="text" name="user" /></td></tr><tr><td>' . __('Password') . ':</td><td><input type="password" name="pass" /></td><td><input type="submit" value="' . __('Start') . '" /></td></tr></table></form>'; |
---|
61 | die; |
---|
62 | } |
---|
63 | |
---|
64 | // Sends creds to Blogger, returns the session cookies an array of headers. |
---|
65 | function login_blogger($user, $pass) { |
---|
66 | $_url = 'http://www.blogger.com/login.do'; |
---|
67 | $params = "username=$user&password=$pass"; |
---|
68 | $ch = curl_init(); |
---|
69 | curl_setopt($ch, CURLOPT_POST,1); |
---|
70 | curl_setopt($ch, CURLOPT_POSTFIELDS,$params); |
---|
71 | curl_setopt($ch, CURLOPT_URL,$_url); |
---|
72 | curl_setopt($ch, CURLOPT_USERAGENT, 'Blogger Exporter'); |
---|
73 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
---|
74 | curl_setopt($ch, CURLOPT_HEADER,1); |
---|
75 | curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); |
---|
76 | $response = curl_exec ($ch); |
---|
77 | |
---|
78 | $response = $this->parse_response($response); |
---|
79 | |
---|
80 | sleep(1); |
---|
81 | |
---|
82 | return $response['cookies']; |
---|
83 | } |
---|
84 | |
---|
85 | // Requests page from Blogger, returns the response array. |
---|
86 | function get_blogger($url, $header = '', $user=false, $pass=false) { |
---|
87 | $ch = curl_init(); |
---|
88 | if ($user && $pass) curl_setopt($ch, CURLOPT_USERPWD,"{$user}:{$pass}"); |
---|
89 | curl_setopt($ch, CURLOPT_URL,$url); |
---|
90 | curl_setopt($ch, CURLOPT_TIMEOUT, 10); |
---|
91 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
---|
92 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
---|
93 | curl_setopt($ch, CURLOPT_USERAGENT, 'Blogger Exporter'); |
---|
94 | curl_setopt($ch, CURLOPT_HEADER,1); |
---|
95 | if (is_array($header)) curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
---|
96 | $response = curl_exec ($ch); |
---|
97 | |
---|
98 | $response = $this->parse_response($response); |
---|
99 | $response['url'] = $url; |
---|
100 | |
---|
101 | if (curl_errno($ch)) { |
---|
102 | print curl_error($ch); |
---|
103 | } else { |
---|
104 | curl_close($ch); |
---|
105 | } |
---|
106 | |
---|
107 | return $response; |
---|
108 | } |
---|
109 | |
---|
110 | // Posts data to Blogger, returns response array. |
---|
111 | function post_blogger($url, $header = false, $paramary = false, $parse=true) { |
---|
112 | $params = ''; |
---|
113 | if ( is_array($paramary) ) { |
---|
114 | foreach($paramary as $key=>$value) |
---|
115 | if($key && $value != '') |
---|
116 | $params.=$key."=".urlencode(stripslashes($value))."&"; |
---|
117 | } |
---|
118 | if ($user && $pass) $params .= "username=$user&password=$pass"; |
---|
119 | $params = trim($params,'&'); |
---|
120 | $ch = curl_init(); |
---|
121 | curl_setopt($ch, CURLOPT_POST,1); |
---|
122 | curl_setopt($ch, CURLOPT_POSTFIELDS,$params); |
---|
123 | if ($user && $pass) curl_setopt($ch, CURLOPT_USERPWD,"{$user}:{$pass}"); |
---|
124 | curl_setopt($ch, CURLOPT_URL,$url); |
---|
125 | curl_setopt($ch, CURLOPT_USERAGENT, 'Blogger Exporter'); |
---|
126 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
---|
127 | curl_setopt($ch, CURLOPT_HEADER,$parse); |
---|
128 | curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); |
---|
129 | if ($header) curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
---|
130 | $response = curl_exec ($ch); |
---|
131 | |
---|
132 | if ($parse) { |
---|
133 | $response = $this->parse_response($response); |
---|
134 | $response['url'] = $url; |
---|
135 | return $response; |
---|
136 | } |
---|
137 | |
---|
138 | return $response; |
---|
139 | } |
---|
140 | |
---|
141 | // Prints the list of blogs for import. |
---|
142 | function show_blogs() { |
---|
143 | global $import; |
---|
144 | echo '<h1>' . __('Selecting a Blog') . "</h1>\n<ul>"; |
---|
145 | foreach ( $this->import['blogs'] as $blog ) { |
---|
146 | if (9 == $blog['nextstep']) $status = "100%"; |
---|
147 | elseif (8 == $blog['nextstep']) $status = "90%"; |
---|
148 | elseif (7 == $blog['nextstep']) $status = "82.5%"; |
---|
149 | elseif (6 == $blog['nextstep']) $status = "75%"; |
---|
150 | elseif (5 == $blog['nextstep']) $status = "57%"; |
---|
151 | elseif (4 == $blog['nextstep']) $status = "28%"; |
---|
152 | elseif (3 == $blog['nextstep']) $status = "14%"; |
---|
153 | else $status = "0%"; |
---|
154 | echo "\t<li><a href='admin.php?import=blogger&noheader=true&blog={$blog['id']}'>{$blog['title']}</a> $status</li>\n"; |
---|
155 | } |
---|
156 | die("</ul>\n"); |
---|
157 | } |
---|
158 | |
---|
159 | // Publishes. |
---|
160 | function publish_blogger($i, $text) { |
---|
161 | $head = $this->refresher(2000) . "<h1>$text</h1>\n"; |
---|
162 | if ( ! strstr($this->import['blogs'][$_GET['blog']]['publish'][$i], 'http') ) { |
---|
163 | // First call. Start the publish process with a fresh set of cookies. |
---|
164 | $this->import['cookies'] = $this->login_blogger($this->import['user'], $this->import['pass']); |
---|
165 | update_option('import-blogger', $this->import); |
---|
166 | $paramary = array('blogID' => $_GET['blog'], 'all' => '1', 'republishAll' => 'Republish Entire Blog', 'publish' => '1', 'redirectUrl' => "/publish.do?blogID={$_GET['blog']}&inprogress=true"); |
---|
167 | |
---|
168 | $response = $this->post_blogger("http://www.blogger.com/publish.do?blogID={$_GET['blog']}", $this->import['cookies'], $paramary); |
---|
169 | if ( $response['code'] == '302' ) { |
---|
170 | $url = str_replace('publish.g', 'publish-body.g', $response['header']['Location']); |
---|
171 | $this->import['blogs'][$_GET['blog']]['publish'][$i] = $url; |
---|
172 | update_option('import-blogger', $this->import); |
---|
173 | $response = $this->get_blogger($url, $this->import['cookies']); |
---|
174 | preg_match('#<p class="progressIndicator">.*</p>#U', $response['body'], $matches); |
---|
175 | $progress = $matches[0]; |
---|
176 | die($head . $progress); |
---|
177 | } else { |
---|
178 | $this->import['blogs'][$_GET['blog']]['publish'][$i] = false; |
---|
179 | update_option('import-blogger', $this->import); |
---|
180 | die($head); |
---|
181 | } |
---|
182 | } else { |
---|
183 | // Subsequent call. Keep checking status until Blogger reports publish complete. |
---|
184 | $url = $this->import['blogs'][$_GET['blog']]['publish'][$i]; |
---|
185 | $response = $this->get_blogger($url, $this->import['cookies']); |
---|
186 | if ( preg_match('#<p class="progressIndicator">.*</p>#U', $response['body'], $matches) ) { |
---|
187 | $progress = $matches[0]; |
---|
188 | if ( strstr($progress, '100%') ) { |
---|
189 | $this->set_next_step($i); |
---|
190 | $progress .= '<p>'.__('Moving on...').'</p>'; |
---|
191 | } |
---|
192 | die($head . $progress); |
---|
193 | } else { |
---|
194 | $this->import['blogs'][$_GET['blog']]['publish'][$i] = false; |
---|
195 | update_option('import-blogger', $this->import); |
---|
196 | die("$head<p>" . __('Trying again...') . '</p>'); |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | // Sets next step, saves options |
---|
202 | function set_next_step($step) { |
---|
203 | $this->import['blogs'][$_GET['blog']]['nextstep'] = $step; |
---|
204 | update_option('import-blogger', $this->import); |
---|
205 | } |
---|
206 | |
---|
207 | // Redirects to next step |
---|
208 | function do_next_step() { |
---|
209 | header("Location: admin.php?import=blogger&noheader=true&blog={$_GET['blog']}"); |
---|
210 | die(); |
---|
211 | } |
---|
212 | |
---|
213 | // Step 0: Do Blogger login, get blogid/title pairs. |
---|
214 | function do_login() { |
---|
215 | if ( ( ! $this->import['user'] && ! is_array($this->import['cookies']) ) ) { |
---|
216 | // The user must provide a Blogger username and password. |
---|
217 | if ( ! ( $_POST['user'] && $_POST['pass'] ) ) { |
---|
218 | $this->login_form(__('The script will log into your Blogger account, change some settings so it can read your blog, and restore the original settings when it\'s done. Here\'s what you do:</p><ol><li>Back up your Blogger template.</li><li>Back up any other Blogger settings you might need later.</li><li>Log out of Blogger</li><li>Log in <em>here</em> with your Blogger username and password.</li><li>On the next screen, click one of your Blogger blogs.</li><li>Do not close this window or navigate away until the process is complete.</li></ol>')); |
---|
219 | } |
---|
220 | |
---|
221 | // Try logging in. If we get an array of cookies back, we at least connected. |
---|
222 | $this->import['cookies'] = $this->login_blogger($_POST['user'], $_POST['pass']); |
---|
223 | if ( !is_array( $this->import['cookies'] ) ) { |
---|
224 | $this->login_form("Login failed. Please enter your credentials again."); |
---|
225 | } |
---|
226 | |
---|
227 | // Save the password so we can log the browser in when it's time to publish. |
---|
228 | $this->import['pass'] = $_POST['pass']; |
---|
229 | $this->import['user'] = $_POST['user']; |
---|
230 | |
---|
231 | // Get the Blogger welcome page and scrape the blog numbers and names from it |
---|
232 | $response = $this->get_blogger('http://www.blogger.com/home', $this->import['cookies']); |
---|
233 | if (! stristr($response['body'], 'signed in as') ) $this->login_form("Login failed. Please re-enter your username and password."); |
---|
234 | $blogsary = array(); |
---|
235 | preg_match_all('#posts\.g\?blogID=(\d+)">([^<]+)</a>#U', $response['body'], $blogsary); |
---|
236 | if ( ! count( $blogsary[1] < 1 ) ) |
---|
237 | die(__('No blogs found for this user.')); |
---|
238 | $this->import['blogs'] = array(); |
---|
239 | $template = '<MainPage><br /><br /><br /><p>'.__('Are you looking for %title%? It is temporarily out of service. Please try again in a few minutes. Meanwhile, discover <a href="http://wordpress.org">a better blogging tool</a>.').'</p><BloggerArchives><a class="archive" href="<$BlogArchiveURL$>"><$BlogArchiveName$></a><br /></BloggerArchives></MainPage><ArchivePage><Blogger><wordpresspost><$BlogItemDateTime$>|W|P|<$BlogItemAuthorNickname$>|W|P|<$BlogItemBody$>|W|P|<$BlogItemNumber$>|W|P|<$BlogItemTitle$>|W|P|<$BlogItemAuthorEmail$><BlogItemCommentsEnabled><BlogItemComments><wordpresscomment><$BlogCommentDateTime$>|W|P|<$BlogCommentAuthor$>|W|P|<$BlogCommentBody$></BlogItemComments></BlogItemCommentsEnabled></Blogger></ArchivePage>'; |
---|
240 | foreach ( $blogsary[1] as $key => $id ) { |
---|
241 | // Define the required Blogger options. |
---|
242 | $blog_opts = array( |
---|
243 | 'blog-options-basic' => false, |
---|
244 | 'blog-options-archiving' => array('archiveFrequency' => 'm'), |
---|
245 | 'blog-publishing' => array('publishMode'=>'0', 'blogID' => "$id", 'subdomain' => mt_rand().mt_rand(), 'pingWeblogs' => 'false'), |
---|
246 | 'blog-formatting' => array('timeStampFormat' => '0', 'encoding'=>'UTF-8', 'convertLineBreaks'=>'false', 'floatAlignment'=>'false'), |
---|
247 | 'blog-comments' => array('commentsTimeStampFormat' => '0'), |
---|
248 | 'template-edit' => array( 'templateText' => str_replace('%title%', trim($blogsary[2][$key]), $template) ) |
---|
249 | ); |
---|
250 | |
---|
251 | // Build the blog options array template |
---|
252 | foreach ($blog_opts as $blog_opt => $modify) |
---|
253 | $new_opts["$blog_opt"] = array('backup'=>false, 'modify' => $modify, 'error'=>false); |
---|
254 | |
---|
255 | $this->import['blogs']["$id"] = array( |
---|
256 | 'id' => $id, |
---|
257 | 'title' => trim($blogsary[2][$key]), |
---|
258 | 'options' => $new_opts, |
---|
259 | 'url' => false, |
---|
260 | 'publish_cookies' => false, |
---|
261 | 'published' => false, |
---|
262 | 'archives' => false, |
---|
263 | 'lump_authors' => false, |
---|
264 | 'newusers' => array(), |
---|
265 | 'nextstep' => 2 |
---|
266 | ); |
---|
267 | } |
---|
268 | update_option('import-blogger', $this->import); |
---|
269 | header("Location: admin.php?import=blogger&noheader=true&step=1"); |
---|
270 | } |
---|
271 | die(); |
---|
272 | } |
---|
273 | |
---|
274 | // Step 1: Select one of the blogs belonging to the user logged in. |
---|
275 | function select_blog() { |
---|
276 | if ( is_array($this->import['blogs']) ) { |
---|
277 | $this->show_blogs(); |
---|
278 | die(); |
---|
279 | } else { |
---|
280 | $this->restart(); |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | // Step 2: Backup the Blogger options pages, updating some of them. |
---|
285 | function backup_settings() { |
---|
286 | $output.= "<h1>Backing up Blogger options</h1>\n"; |
---|
287 | $form = false; |
---|
288 | foreach ($this->import['blogs'][$_GET['blog']]['options'] as $blog_opt => $optary) { |
---|
289 | if ( $blog_opt == $_GET['form'] ) { |
---|
290 | // Save the posted form data |
---|
291 | $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['backup'] = $_POST; |
---|
292 | update_option('import-blogger',$this->import); |
---|
293 | |
---|
294 | // Post the modified form data to Blogger |
---|
295 | if ( $optary['modify'] ) { |
---|
296 | $posturl = "http://www.blogger.com/{$blog_opt}.do"; |
---|
297 | $headers = array_merge($this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['cookies'], $this->import['cookies']); |
---|
298 | if ( 'blog-publishing' == $blog_opt ) { |
---|
299 | if ( $_POST['publishMode'] > 0 ) { |
---|
300 | $response = $this->get_blogger("http://www.blogger.com/blog-publishing.g?blogID={$_GET['blog']}&publishMode=0", $headers); |
---|
301 | if ( $response['code'] >= 400 ) |
---|
302 | die('<h2>'.__('Failed attempt to change publish mode from FTP to BlogSpot.').'</h2><pre>' . addslashes(print_r($headers, 1)) . addslashes(print_r($response, 1)) . '</pre>'); |
---|
303 | $this->import['blogs'][$_GET['blog']]['url'] = 'http://' . $optary['modify']['subdomain'] . '.blogspot.com/'; |
---|
304 | sleep(2); |
---|
305 | } else { |
---|
306 | $this->import['blogs'][$_GET['blog']]['url'] = 'http://' . $_POST['subdomain'] . '.blogspot.com/'; |
---|
307 | update_option('import-blogger', $this->import); |
---|
308 | $output .= "<del><p>$blog_opt</p></del>\n"; |
---|
309 | continue; |
---|
310 | } |
---|
311 | $paramary = $optary['modify']; |
---|
312 | } else { |
---|
313 | $paramary = array_merge($_POST, $optary['modify']); |
---|
314 | } |
---|
315 | $response = $this->post_blogger($posturl, $headers, $paramary); |
---|
316 | if ( $response['code'] >= 400 || strstr($response['body'], 'There are errors on this form') ) |
---|
317 | die('<p>'.__('Error on form submission. Retry or reset the importer.').'</p>' . addslashes(print_r($response, 1))); |
---|
318 | } |
---|
319 | $output .= "<del><p>$blog_opt</p></del>\n"; |
---|
320 | } elseif ( is_array($this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['backup']) ) { |
---|
321 | // This option set has already been backed up. |
---|
322 | $output .= "<del><p>$blog_opt</p></del>\n"; |
---|
323 | } elseif ( ! $form ) { |
---|
324 | // This option page needs to be downloaded and given to the browser for submission back to this script. |
---|
325 | $response = $this->get_blogger("http://www.blogger.com/{$blog_opt}.g?blogID={$_GET['blog']}", $this->import['cookies']); |
---|
326 | $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['cookies'] = $response['cookies']; |
---|
327 | update_option('import-blogger',$this->import); |
---|
328 | $body = $response['body']; |
---|
329 | $body = preg_replace("|\<!DOCTYPE.*\<body[^>]*>|ms","",$body); |
---|
330 | $body = preg_replace("|/?{$blog_opt}.do|","admin.php?import=blogger&noheader=true&step=2&blog={$_GET['blog']}&form={$blog_opt}",$body); |
---|
331 | $body = str_replace("name='submit'","name='supermit'",$body); |
---|
332 | $body = str_replace('name="submit"','name="supermit"',$body); |
---|
333 | $body = str_replace('</body>','',str_replace('</html>','',$body)); |
---|
334 | $form = "<div style='height:0px;width:0px;overflow:hidden;'>"; |
---|
335 | $form.= $body; |
---|
336 | $form.= "</div><script type='text/javascript'>forms=document.getElementsByTagName('form');for(i=0;i<forms.length;i++){if(forms[i].action.search('{$blog_opt}')){forms[i].submit();break;}}</script>"; |
---|
337 | $output.= "<p><strong>$blog_opt</strong> in progress, please wait...</p>\n"; |
---|
338 | } else { |
---|
339 | $output.= "<p>$blog_opt</p>\n"; |
---|
340 | } |
---|
341 | } |
---|
342 | if ( $form ) |
---|
343 | die($output . $form); |
---|
344 | |
---|
345 | $this->set_next_step(4); |
---|
346 | $this->do_next_step(); |
---|
347 | } |
---|
348 | |
---|
349 | // Step 3: Cancelled :-) |
---|
350 | |
---|
351 | // Step 4: Publish with the new template and settings. |
---|
352 | function publish_blog() { |
---|
353 | $this->publish_blogger(5, __('Publishing with new template and options')); |
---|
354 | } |
---|
355 | |
---|
356 | // Step 5: Get the archive URLs from the new blog. |
---|
357 | function get_archive_urls() { |
---|
358 | $bloghtml = $this->get_blogger($this->import['blogs'][$_GET['blog']]['url']); |
---|
359 | if (! strstr($bloghtml['body'], '<a class="archive"') ) |
---|
360 | die(__('Your Blogger blog did not take the new template or did not respond.')); |
---|
361 | preg_match_all('#<a class="archive" href="([^"]*)"#', $bloghtml['body'], $archives); |
---|
362 | foreach ($archives[1] as $archive) { |
---|
363 | $this->import['blogs'][$_GET['blog']]['archives'][$archive] = false; |
---|
364 | } |
---|
365 | $this->set_next_step(6); |
---|
366 | $this->do_next_step(); |
---|
367 | } |
---|
368 | |
---|
369 | // Step 6: Get each monthly archive, import it, mark it done. |
---|
370 | function get_archive() { |
---|
371 | global $wpdb; |
---|
372 | $output = '<h2>'.__('Importing Blogger archives into WordPress').'</h2>'; |
---|
373 | $did_one = false; |
---|
374 | $post_array = $posts = array(); |
---|
375 | foreach ( $this->import['blogs'][$_GET['blog']]['archives'] as $url => $status ) { |
---|
376 | $archivename = substr(basename($url),0,7); |
---|
377 | if ( $status || $did_one ) { |
---|
378 | $foo = 'bar'; |
---|
379 | // Do nothing. |
---|
380 | } else { |
---|
381 | // Import the selected month |
---|
382 | $postcount = 0; |
---|
383 | $skippedpostcount = 0; |
---|
384 | $commentcount = 0; |
---|
385 | $skippedcommentcount = 0; |
---|
386 | $status = __('in progress...'); |
---|
387 | $this->import['blogs'][$_GET['blog']]['archives']["$url"] = $status; |
---|
388 | update_option('import-blogger', $import); |
---|
389 | $archive = $this->get_blogger($url); |
---|
390 | if ( $archive['code'] > 200 ) |
---|
391 | continue; |
---|
392 | $posts = explode('<wordpresspost>', $archive['body']); |
---|
393 | for ($i = 1; $i < count($posts); $i = $i + 1) { |
---|
394 | $postparts = explode('<wordpresscomment>', $posts[$i]); |
---|
395 | $postinfo = explode('|W|P|', $postparts[0]); |
---|
396 | $post_date = $postinfo[0]; |
---|
397 | $post_content = $postinfo[2]; |
---|
398 | // Don't try to re-use the original numbers |
---|
399 | // because the new, longer numbers are too |
---|
400 | // big to handle as ints. |
---|
401 | //$post_number = $postinfo[3]; |
---|
402 | $post_title = ( $postinfo[4] != '' ) ? $postinfo[4] : $postinfo[3]; |
---|
403 | $post_author_name = $wpdb->escape(trim($postinfo[1])); |
---|
404 | $post_author_email = $postinfo[5] ? $postinfo[5] : 'user@wordpress.org'; |
---|
405 | |
---|
406 | if ( $this->lump_authors ) { |
---|
407 | // Ignore Blogger authors. Use the current user_ID for all posts imported. |
---|
408 | $post_author = $GLOBALS['user_ID']; |
---|
409 | } else { |
---|
410 | // Add a user for each new author encountered. |
---|
411 | if (! username_exists($post_author_name) ) { |
---|
412 | $user_login = $wpdb->escape($post_author_name); |
---|
413 | $user_email = $wpdb->escape($post_author_email); |
---|
414 | $user_password = substr(md5(uniqid(microtime())), 0, 6); |
---|
415 | $result = wp_create_user( $user_login, $user_password, $user_email ); |
---|
416 | $status.= "Registered user <strong>$user_login</strong>. "; |
---|
417 | $this->import['blogs'][$_GET['blog']]['newusers'][] = $user_login; |
---|
418 | } |
---|
419 | $userdata = get_userdatabylogin( $post_author_name ); |
---|
420 | $post_author = $userdata->ID; |
---|
421 | } |
---|
422 | $post_date = explode(' ', $post_date); |
---|
423 | $post_date_Ymd = explode('/', $post_date[0]); |
---|
424 | $postyear = $post_date_Ymd[2]; |
---|
425 | $postmonth = zeroise($post_date_Ymd[0], 2); |
---|
426 | $postday = zeroise($post_date_Ymd[1], 2); |
---|
427 | $post_date_His = explode(':', $post_date[1]); |
---|
428 | $posthour = zeroise($post_date_His[0], 2); |
---|
429 | $postminute = zeroise($post_date_His[1], 2); |
---|
430 | $postsecond = zeroise($post_date_His[2], 2); |
---|
431 | |
---|
432 | if (($post_date[2] == 'PM') && ($posthour != '12')) |
---|
433 | $posthour = $posthour + 12; |
---|
434 | else if (($post_date[2] == 'AM') && ($posthour == '12')) |
---|
435 | $posthour = '00'; |
---|
436 | |
---|
437 | $post_date = "$postyear-$postmonth-$postday $posthour:$postminute:$postsecond"; |
---|
438 | |
---|
439 | $post_content = addslashes($post_content); |
---|
440 | $post_content = str_replace(array('<br>','<BR>','<br/>','<BR/>','<br />','<BR />'), "\n", $post_content); // the XHTML touch... ;) |
---|
441 | |
---|
442 | $post_title = addslashes($post_title); |
---|
443 | |
---|
444 | $post_status = 'publish'; |
---|
445 | |
---|
446 | if ( $ID = post_exists($post_title, '', $post_date) ) { |
---|
447 | $post_array[$i]['ID'] = $ID; |
---|
448 | $skippedpostcount++; |
---|
449 | } else { |
---|
450 | $post_array[$i]['post'] = compact('post_author', 'post_content', 'post_title', 'post_category', 'post_author', 'post_date', 'post_status'); |
---|
451 | $post_array[$i]['comments'] = false; |
---|
452 | } |
---|
453 | |
---|
454 | // Import any comments attached to this post. |
---|
455 | if ($postparts[1]) : |
---|
456 | for ($j = 1; $j < count($postparts); $j = $j + 1) { |
---|
457 | $commentinfo = explode('|W|P|', $postparts[$j]); |
---|
458 | $comment_date = explode(' ', $commentinfo[0]); |
---|
459 | $comment_date_Ymd = explode('/', $comment_date[0]); |
---|
460 | $commentyear = $comment_date_Ymd[2]; |
---|
461 | $commentmonth = zeroise($comment_date_Ymd[0], 2); |
---|
462 | $commentday = zeroise($comment_date_Ymd[1], 2); |
---|
463 | $comment_date_His = explode(':', $comment_date[1]); |
---|
464 | $commenthour = zeroise($comment_date_His[0], 2); |
---|
465 | $commentminute = zeroise($comment_date_His[1], 2); |
---|
466 | $commentsecond = '00'; |
---|
467 | if (($comment_date[2] == 'PM') && ($commenthour != '12')) |
---|
468 | $commenthour = $commenthour + 12; |
---|
469 | else if (($comment_date[2] == 'AM') && ($commenthour == '12')) |
---|
470 | $commenthour = '00'; |
---|
471 | $comment_date = "$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond"; |
---|
472 | $comment_author = addslashes(strip_tags(html_entity_decode($commentinfo[1]))); |
---|
473 | if ( strpos($commentinfo[1], 'a href') ) { |
---|
474 | $comment_author_parts = explode('"', htmlentities($commentinfo[1])); |
---|
475 | $comment_author_url = $comment_author_parts[1]; |
---|
476 | } else $comment_author_url = ''; |
---|
477 | $comment_content = $commentinfo[2]; |
---|
478 | $comment_content = str_replace(array('<br>','<BR>','<br/>','<BR/>','<br />','<BR />'), "\n", $comment_content); |
---|
479 | $comment_approved = 1; |
---|
480 | if ( comment_exists($comment_author, $comment_date) ) { |
---|
481 | $skippedcommentcount++; |
---|
482 | } else { |
---|
483 | $comment = compact('comment_author', 'comment_author_url', 'comment_date', 'comment_content', 'comment_approved'); |
---|
484 | $post_array[$i]['comments'][$j] = wp_filter_comment($comment); |
---|
485 | } |
---|
486 | $commentcount++; |
---|
487 | } |
---|
488 | endif; |
---|
489 | $postcount++; |
---|
490 | } |
---|
491 | if ( count($post_array) ) { |
---|
492 | krsort($post_array); |
---|
493 | foreach($post_array as $post) { |
---|
494 | if ( ! $comment_post_ID = $post['ID'] ) |
---|
495 | $comment_post_ID = wp_insert_post($post['post']); |
---|
496 | if ( $post['comments'] ) { |
---|
497 | foreach ( $post['comments'] as $comment ) { |
---|
498 | $comment['comment_post_ID'] = $comment_post_ID; |
---|
499 | wp_insert_comment($comment); |
---|
500 | } |
---|
501 | } |
---|
502 | } |
---|
503 | } |
---|
504 | $status = "$postcount ".__('post(s) parsed,')." $skippedpostcount ".__('skipped...')." $commentcount ".__('comment(s) parsed,')." $skippedcommentcount ".__('skipped...').' <strong>'.__('Done').'</strong>'; |
---|
505 | $import = $this->import; |
---|
506 | $import['blogs'][$_GET['blog']]['archives']["$url"] = $status; |
---|
507 | update_option('import-blogger', $import); |
---|
508 | $did_one = true; |
---|
509 | } |
---|
510 | $output.= "<p>$archivename $status</p>\n"; |
---|
511 | } |
---|
512 | if ( ! $did_one ) |
---|
513 | $this->set_next_step(7); |
---|
514 | die( $this->refresher(1000) . $output ); |
---|
515 | } |
---|
516 | |
---|
517 | // Step 7: Restore the backed-up settings to Blogger |
---|
518 | function restore_settings() { |
---|
519 | $output = '<h1>'.__('Restoring your Blogger options')."</h1>\n"; |
---|
520 | $did_one = false; |
---|
521 | // Restore options in reverse order. |
---|
522 | if ( ! $this->import['reversed'] ) { |
---|
523 | $this->import['blogs'][$_GET['blog']]['options'] = array_reverse($this->import['blogs'][$_GET['blog']]['options'], true); |
---|
524 | $this->import['reversed'] = true; |
---|
525 | update_option('import-blogger', $this->import); |
---|
526 | } |
---|
527 | foreach ( $this->import['blogs'][$_GET['blog']]['options'] as $blog_opt => $optary ) { |
---|
528 | if ( $did_one ) { |
---|
529 | $output .= "<p>$blog_opt</p>\n"; |
---|
530 | } elseif ( $optary['restored'] || ! $optary['modify'] ) { |
---|
531 | $output .= "<p><del>$blog_opt</del></p>\n"; |
---|
532 | } else { |
---|
533 | $posturl = "http://www.blogger.com/{$blog_opt}.do"; |
---|
534 | $headers = array_merge($this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['cookies'], $this->import['cookies']); |
---|
535 | if ( 'blog-publishing' == $blog_opt) { |
---|
536 | if ( $optary['backup']['publishMode'] > 0 ) { |
---|
537 | $response = $this->get_blogger("http://www.blogger.com/blog-publishing.g?blogID={$_GET['blog']}&publishMode={$optary['backup']['publishMode']}", $headers); |
---|
538 | sleep(2); |
---|
539 | if ( $response['code'] >= 400 ) |
---|
540 | die('<h1>Error restoring publishMode.</h1><p>Please tell the devs.</p>' . addslashes(print_r($response, 1)) ); |
---|
541 | } |
---|
542 | } |
---|
543 | if ( $optary['backup'] != $optary['modify'] ) { |
---|
544 | $response = $this->post_blogger($posturl, $headers, $optary['backup']); |
---|
545 | if ( $response['code'] >= 400 || strstr($response['body'], 'There are errors on this form') ) { |
---|
546 | $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['error'] = true; |
---|
547 | update_option('import-blogger', $this->import); |
---|
548 | $output .= "<p><strong>$blog_opt</strong> ".__('failed. Trying again.').'</p>'; |
---|
549 | } else { |
---|
550 | $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['restored'] = true; |
---|
551 | update_option('import-blogger', $this->import); |
---|
552 | $output .= "<p><strong>$blog_opt</strong> ".__('restored.').'</p>'; |
---|
553 | } |
---|
554 | } |
---|
555 | $did_one = true; |
---|
556 | } |
---|
557 | } |
---|
558 | |
---|
559 | if ( $did_one ) { |
---|
560 | die( $this->refresher(1000) . $output ); |
---|
561 | } elseif ( $this->import['blogs'][$_GET['blog']]['options']['blog-publishing']['backup']['publishMode'] > 0 ) { |
---|
562 | $this->set_next_step(9); |
---|
563 | } else { |
---|
564 | $this->set_next_step(8); |
---|
565 | } |
---|
566 | |
---|
567 | $this->do_next_step(); |
---|
568 | } |
---|
569 | |
---|
570 | // Step 8: Republish, all back to normal |
---|
571 | function republish_blog() { |
---|
572 | $this->publish_blogger(9, __('Publishing with original template and options')); |
---|
573 | } |
---|
574 | |
---|
575 | // Step 9: Congratulate the user |
---|
576 | function congrats() { |
---|
577 | echo '<h1>'.__('Congratulations!').'</h1><p>'.__('Now that you have imported your Blogger blog into WordPress, what are you going to do? Here are some suggestions:').'</p><ul><li>'.__('That was hard work! Take a break.').'</li>'; |
---|
578 | if ( count($this->import['blogs']) > 1 ) |
---|
579 | echo '<li>'.__('In case you haven\'t done it already, you can import the posts from your other blogs:'). $this->show_blogs() . '</li>'; |
---|
580 | if ( $n = count($this->import['blogs'][$_GET['blog']]['newusers']) ) |
---|
581 | echo '<li>'.__('Go to <a href="users.php" target="_parent">Authors & Users</a>, where you can modify the new user(s) or delete them. If you want to make all of the imported posts yours, you will be given that option when you delete the new authors.').'</li>'; |
---|
582 | echo '<li>'.__('For security, click the link below to reset this importer. That will clear your Blogger credentials and options from the database.').'</li>'; |
---|
583 | echo '</ul>'; |
---|
584 | } |
---|
585 | |
---|
586 | // Figures out what to do, then does it. |
---|
587 | function start() { |
---|
588 | if ( $_GET['restart'] == 'true' ) { |
---|
589 | $this->restart(); |
---|
590 | } |
---|
591 | |
---|
592 | if ( isset($_GET['noheader']) ) { |
---|
593 | $this->import = get_settings('import-blogger'); |
---|
594 | |
---|
595 | if ( false === $this->import ) { |
---|
596 | $step = 0; |
---|
597 | } elseif ( isset($_GET['step']) ) { |
---|
598 | $step = (int) $_GET['step']; |
---|
599 | } elseif ( isset($_GET['blog']) && isset($this->import['blogs'][$_GET['blog']]['nextstep']) ) { |
---|
600 | $step = $this->import['blogs'][$_GET['blog']]['nextstep']; |
---|
601 | } elseif ( is_array($this->import['blogs']) ) { |
---|
602 | $step = 1; |
---|
603 | } else { |
---|
604 | $step = 0; |
---|
605 | } |
---|
606 | //echo "Step $step."; |
---|
607 | //die('<pre>'.print_r($this->import,1).'</pre'); |
---|
608 | switch ($step) { |
---|
609 | case 0 : |
---|
610 | $this->do_login(); |
---|
611 | break; |
---|
612 | case 1 : |
---|
613 | $this->select_blog(); |
---|
614 | break; |
---|
615 | case 2 : |
---|
616 | $this->backup_settings(); |
---|
617 | break; |
---|
618 | case 3 : |
---|
619 | $this->wait_for_blogger(); |
---|
620 | break; |
---|
621 | case 4 : |
---|
622 | $this->publish_blog(); |
---|
623 | break; |
---|
624 | case 5 : |
---|
625 | $this->get_archive_urls(); |
---|
626 | break; |
---|
627 | case 6 : |
---|
628 | $this->get_archive(); |
---|
629 | break; |
---|
630 | case 7 : |
---|
631 | $this->restore_settings(); |
---|
632 | break; |
---|
633 | case 8 : |
---|
634 | $this->republish_blog(); |
---|
635 | break; |
---|
636 | case 9 : |
---|
637 | $this->congrats(); |
---|
638 | break; |
---|
639 | } |
---|
640 | die; |
---|
641 | |
---|
642 | } else { |
---|
643 | $this->greet(); |
---|
644 | } |
---|
645 | } |
---|
646 | |
---|
647 | function Blogger_Import() { |
---|
648 | // This space intentionally left blank. |
---|
649 | } |
---|
650 | } |
---|
651 | |
---|
652 | $blogger_import = new Blogger_Import(); |
---|
653 | |
---|
654 | register_importer('blogger', 'Blogger', __('Import posts and comments from a Blogger account'), array ($blogger_import, 'start')); |
---|
655 | |
---|
656 | ?> |
---|