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