PageRenderTime 69ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/import/blogger.php

https://github.com/alx/barceloneta
PHP | 1017 lines | 849 code | 131 blank | 37 comment | 153 complexity | 2f762e4df635c4fb38b3c24fe1b297b0 MD5 | raw file
  1. <?php
  2. define( 'MAX_RESULTS', 50 ); // How many records per GData query
  3. define( 'MAX_EXECUTION_TIME', 20 ); // How many seconds to let the script run
  4. define( 'STATUS_INTERVAL', 3 ); // How many seconds between status bar updates
  5. class Blogger_Import {
  6. // Shows the welcome screen and the magic auth link.
  7. function greet() {
  8. $next_url = get_option('siteurl') . '/wp-admin/index.php?import=blogger&noheader=true';
  9. $auth_url = "https://www.google.com/accounts/AuthSubRequest";
  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. $prereqs = __('To use this importer, you must have a Google account and an upgraded (New, was Beta) blog hosted on blogspot.com or a custom domain (not FTP).');
  13. $stepone = __('The first thing you need to do is tell Blogger to let WordPress access your account. You will be sent back here after providing authorization.');
  14. $auth = __('Authorize');
  15. echo "
  16. <div class='wrap'><h2>$title</h2><p>$welcome</p><p>$prereqs</p><p>$stepone</p>
  17. <form action='$auth_url' method='get'>
  18. <p class='submit' style='text-align:left;'>
  19. <input type='submit' class='button' value='$auth' />
  20. <input type='hidden' name='scope' value='http://www.blogger.com/feeds/' />
  21. <input type='hidden' name='session' value='1' />
  22. <input type='hidden' name='secure' value='0' />
  23. <input type='hidden' name='next' value='$next_url' />
  24. </p>
  25. </form>
  26. </div>\n";
  27. }
  28. function uh_oh($title, $message, $info) {
  29. echo "<div class='wrap'><h2>$title</h2><p>$message</p><pre>$info</pre></div>";
  30. }
  31. function auth() {
  32. // We have a single-use token that must be upgraded to a session token.
  33. $token = preg_replace( '/[^-_0-9a-zA-Z]/', '', $_GET['token'] );
  34. $headers = array(
  35. "GET /accounts/AuthSubSessionToken HTTP/1.0",
  36. "Authorization: AuthSub token=\"$token\""
  37. );
  38. $request = join( "\r\n", $headers ) . "\r\n\r\n";
  39. $sock = $this->_get_auth_sock( );
  40. if ( ! $sock ) return false;
  41. $response = $this->_txrx( $sock, $request );
  42. preg_match( '/token=([-_0-9a-z]+)/i', $response, $matches );
  43. if ( empty( $matches[1] ) ) {
  44. $this->uh_oh(
  45. __( 'Authorization failed' ),
  46. __( 'Something went wrong. If the problem persists, send this info to support:' ),
  47. htmlspecialchars($response)
  48. );
  49. return false;
  50. }
  51. $this->token = $matches[1];
  52. wp_redirect( remove_query_arg( array( 'token', 'noheader' ) ) );
  53. }
  54. function get_token_info() {
  55. $headers = array(
  56. "GET /accounts/AuthSubTokenInfo HTTP/1.0",
  57. "Authorization: AuthSub token=\"$this->token\""
  58. );
  59. $request = join( "\r\n", $headers ) . "\r\n\r\n";
  60. $sock = $this->_get_auth_sock( );
  61. if ( ! $sock ) return;
  62. $response = $this->_txrx( $sock, $request );
  63. return $this->parse_response($response);
  64. }
  65. function token_is_valid() {
  66. $info = $this->get_token_info();
  67. if ( $info['code'] == 200 )
  68. return true;
  69. return false;
  70. }
  71. function show_blogs($iter = 0) {
  72. if ( empty($this->blogs) ) {
  73. $headers = array(
  74. "GET /feeds/default/blogs HTTP/1.0",
  75. "Host: www.blogger.com",
  76. "Authorization: AuthSub token=\"$this->token\""
  77. );
  78. $request = join( "\r\n", $headers ) . "\r\n\r\n";
  79. $sock = $this->_get_blogger_sock( );
  80. if ( ! $sock ) return;
  81. $response = $this->_txrx( $sock, $request );
  82. // Quick and dirty XML mining.
  83. list( $headers, $xml ) = explode( "\r\n\r\n", $response );
  84. $p = xml_parser_create();
  85. xml_parse_into_struct($p, $xml, $vals, $index);
  86. xml_parser_free($p);
  87. $this->title = $vals[$index['TITLE'][0]]['value'];
  88. // Give it a few retries... this step often flakes out the first time.
  89. if ( empty( $index['ENTRY'] ) ) {
  90. if ( $iter < 3 ) {
  91. return $this->show_blogs($iter + 1);
  92. } else {
  93. $this->uh_oh(
  94. __('Trouble signing in'),
  95. __('We were not able to gain access to your account. Try starting over.'),
  96. ''
  97. );
  98. return false;
  99. }
  100. }
  101. foreach ( $index['ENTRY'] as $i ) {
  102. $blog = array();
  103. while ( ( $tag = $vals[$i] ) && ! ( $tag['tag'] == 'ENTRY' && $tag['type'] == 'close' ) ) {
  104. if ( $tag['tag'] == 'TITLE' ) {
  105. $blog['title'] = $tag['value'];
  106. } elseif ( $tag['tag'] == 'SUMMARY' ) {
  107. $blog['summary'] == $tag['value'];
  108. } elseif ( $tag['tag'] == 'LINK' ) {
  109. if ( $tag['attributes']['REL'] == 'alternate' && $tag['attributes']['TYPE'] == 'text/html' ) {
  110. $parts = parse_url( $tag['attributes']['HREF'] );
  111. $blog['host'] = $parts['host'];
  112. } elseif ( $tag['attributes']['REL'] == 'edit' )
  113. $blog['gateway'] = $tag['attributes']['HREF'];
  114. }
  115. ++$i;
  116. }
  117. if ( ! empty ( $blog ) ) {
  118. $blog['total_posts'] = $this->get_total_results('posts', $blog['host']);
  119. $blog['total_comments'] = $this->get_total_results('comments', $blog['host']);
  120. $blog['mode'] = 'init';
  121. $this->blogs[] = $blog;
  122. }
  123. }
  124. if ( empty( $this->blogs ) ) {
  125. $this->uh_oh(
  126. __('No blogs found'),
  127. __('We were able to log in but there were no blogs. Try a different account next time.'),
  128. ''
  129. );
  130. return false;
  131. }
  132. }
  133. //echo '<pre>'.print_r($this,1).'</pre>';
  134. $start = js_escape( __('Import') );
  135. $continue = js_escape( __('Continue') );
  136. $stop = js_escape( __('Importing...') );
  137. $authors = js_escape( __('Set Authors') );
  138. $loadauth = js_escape( __('Preparing author mapping form...') );
  139. $authhead = js_escape( __('Final Step: Author Mapping') );
  140. $nothing = js_escape( __('Nothing was imported. Had you already imported this blog?') );
  141. $title = __('Blogger Blogs');
  142. $name = __('Blog Name');
  143. $url = __('Blog URL');
  144. $action = __('The Magic Button');
  145. $posts = __('Posts');
  146. $comments = __('Comments');
  147. $noscript = __('This feature requires Javascript but it seems to be disabled. Please enable Javascript and then reload this page. Don\'t worry, you can turn it back off when you\'re done.');
  148. $interval = STATUS_INTERVAL * 1000;
  149. foreach ( $this->blogs as $i => $blog ) {
  150. if ( $blog['mode'] == 'init' )
  151. $value = $start;
  152. elseif ( $blog['mode'] == 'posts' || $blog['mode'] == 'comments' )
  153. $value = $continue;
  154. else
  155. $value = $authors;
  156. $blogtitle = js_escape( $blog['title'] );
  157. $pdone = isset($blog['posts_done']) ? (int) $blog['posts_done'] : 0;
  158. $cdone = isset($blog['comments_done']) ? (int) $blog['comments_done'] : 0;
  159. $init .= "blogs[$i]=new blog($i,'$blogtitle','{$blog['mode']}'," . $this->get_js_status($i) . ');';
  160. $pstat = "<div class='ind' id='pind$i'>&nbsp;</div><div id='pstat$i' class='stat'>$pdone/{$blog['total_posts']}</div>";
  161. $cstat = "<div class='ind' id='cind$i'>&nbsp;</div><div id='cstat$i' class='stat'>$cdone/{$blog['total_comments']}</div>";
  162. $rows .= "<tr id='blog$i'><td class='blogtitle'>$blogtitle</td><td class='bloghost'>{$blog['host']}</td><td class='bar'>$pstat</td><td class='bar'>$cstat</td><td class='submit'><input type='submit' class='button' id='submit$i' value='$value' /><input type='hidden' name='blog' value='$i' /></td></tr>\n";
  163. }
  164. echo "<div class='wrap'><h2>$title</h2><noscript>$noscript</noscript><table cellpadding='5px'><thead><td>$name</td><td>$url</td><td>$posts</td><td>$comments</td><td>$action</td></thead>\n$rows</table></form></div>";
  165. echo "
  166. <script type='text/javascript'>
  167. var strings = {cont:'$continue',stop:'$stop',stopping:'$stopping',authors:'$authors',nothing:'$nothing'};
  168. var blogs = {};
  169. function blog(i, title, mode, status){
  170. this.blog = i;
  171. this.mode = mode;
  172. this.title = title;
  173. this.status = status;
  174. this.button = document.getElementById('submit'+this.blog);
  175. };
  176. blog.prototype = {
  177. start: function() {
  178. this.cont = true;
  179. this.kick();
  180. this.check();
  181. },
  182. kick: function() {
  183. ++this.kicks;
  184. var i = this.blog;
  185. jQuery.post('admin.php?import=blogger&noheader=true',{blog:this.blog},function(text,result){blogs[i].kickd(text,result)});
  186. },
  187. check: function() {
  188. ++this.checks;
  189. var i = this.blog;
  190. jQuery.post('admin.php?import=blogger&noheader=true&status=true',{blog:this.blog},function(text,result){blogs[i].checkd(text,result)});
  191. },
  192. kickd: function(text, result) {
  193. if ( result == 'error' ) {
  194. // TODO: exception handling
  195. if ( this.cont )
  196. setTimeout('blogs['+this.blog+'].kick()', 1000);
  197. } else {
  198. if ( text == 'done' ) {
  199. this.stop();
  200. this.done();
  201. } else if ( text == 'nothing' ) {
  202. this.stop();
  203. this.nothing();
  204. } else if ( text == 'continue' ) {
  205. this.kick();
  206. } else if ( this.mode = 'stopped' )
  207. jQuery(this.button).attr('value', strings.cont);
  208. }
  209. --this.kicks;
  210. },
  211. checkd: function(text, result) {
  212. if ( result == 'error' ) {
  213. // TODO: exception handling
  214. } else {
  215. eval('this.status='+text);
  216. jQuery('#pstat'+this.blog).empty().append(this.status.p1+'/'+this.status.p2);
  217. jQuery('#cstat'+this.blog).empty().append(this.status.c1+'/'+this.status.c2);
  218. this.update();
  219. if ( this.cont || this.kicks > 0 )
  220. setTimeout('blogs['+this.blog+'].check()', $interval);
  221. }
  222. --this.checks;
  223. },
  224. update: function() {
  225. jQuery('#pind'+this.blog).width(((this.status.p1>0&&this.status.p2>0)?(this.status.p1/this.status.p2*jQuery('#pind'+this.blog).parent().width()):1)+'px');
  226. jQuery('#cind'+this.blog).width(((this.status.c1>0&&this.status.c2>0)?(this.status.c1/this.status.c2*jQuery('#cind'+this.blog).parent().width()):1)+'px');
  227. },
  228. stop: function() {
  229. this.cont = false;
  230. },
  231. done: function() {
  232. this.mode = 'authors';
  233. jQuery(this.button).attr('value', strings.authors);
  234. },
  235. nothing: function() {
  236. this.mode = 'nothing';
  237. jQuery(this.button).remove();
  238. alert(strings.nothing);
  239. },
  240. getauthors: function() {
  241. if ( jQuery('div.wrap').length > 1 )
  242. jQuery('div.wrap').gt(0).remove();
  243. jQuery('div.wrap').empty().append('<h2>$authhead</h2><h3>' + this.title + '</h3>');
  244. jQuery('div.wrap').append('<p id=\"auth\">$loadauth</p>');
  245. jQuery('p#auth').load('index.php?import=blogger&noheader=true&authors=1',{blog:this.blog});
  246. },
  247. init: function() {
  248. this.update();
  249. var i = this.blog;
  250. jQuery(this.button).bind('click', function(){return blogs[i].click();});
  251. this.kicks = 0;
  252. this.checks = 0;
  253. },
  254. click: function() {
  255. if ( this.mode == 'init' || this.mode == 'stopped' || this.mode == 'posts' || this.mode == 'comments' ) {
  256. this.mode = 'started';
  257. this.start();
  258. jQuery(this.button).attr('value', strings.stop);
  259. } else if ( this.mode == 'started' ) {
  260. return false; // let it run...
  261. this.mode = 'stopped';
  262. this.stop();
  263. if ( this.checks > 0 || this.kicks > 0 ) {
  264. this.mode = 'stopping';
  265. jQuery(this.button).attr('value', strings.stopping);
  266. } else {
  267. jQuery(this.button).attr('value', strings.cont);
  268. }
  269. } else if ( this.mode == 'authors' ) {
  270. document.location = 'index.php?import=blogger&authors=1&blog='+this.blog;
  271. //this.mode = 'authors2';
  272. //this.getauthors();
  273. }
  274. return false;
  275. }
  276. };
  277. $init
  278. jQuery.each(blogs, function(i, me){me.init();});
  279. </script>\n";
  280. }
  281. // Handy function for stopping the script after a number of seconds.
  282. function have_time() {
  283. global $importer_started;
  284. if ( time() - $importer_started > MAX_EXECUTION_TIME )
  285. die('continue');
  286. return true;
  287. }
  288. function get_total_results($type, $host) {
  289. $headers = array(
  290. "GET /feeds/$type/default?max-results=1&start-index=2 HTTP/1.0",
  291. "Host: $host",
  292. "Authorization: AuthSub token=\"$this->token\""
  293. );
  294. $request = join( "\r\n", $headers ) . "\r\n\r\n";
  295. $sock = $this->_get_blogger_sock( $host );
  296. if ( ! $sock ) return;
  297. $response = $this->_txrx( $sock, $request );
  298. $response = $this->parse_response( $response );
  299. $parser = xml_parser_create();
  300. xml_parse_into_struct($parser, $response['body'], $struct, $index);
  301. xml_parser_free($parser);
  302. $total_results = $struct[$index['OPENSEARCH:TOTALRESULTS'][0]]['value'];
  303. return (int) $total_results;
  304. }
  305. function import_blog($blogID) {
  306. global $importing_blog;
  307. $importing_blog = $blogID;
  308. if ( isset($_GET['authors']) )
  309. return print($this->get_author_form());
  310. header('Content-Type: text/plain');
  311. if ( isset($_GET['status']) )
  312. die($this->get_js_status());
  313. if ( isset($_GET['saveauthors']) )
  314. die($this->save_authors());
  315. $blog = $this->blogs[$blogID];
  316. $total_results = $this->get_total_results('posts', $blog['host']);
  317. $this->blogs[$importing_blog]['total_posts'] = $total_results;
  318. $start_index = $total_results - MAX_RESULTS + 1;
  319. if ( isset( $this->blogs[$importing_blog]['posts_start_index'] ) )
  320. $start_index = (int) $this->blogs[$importing_blog]['posts_start_index'];
  321. elseif ( $total_results > MAX_RESULTS )
  322. $start_index = $total_results - MAX_RESULTS + 1;
  323. else
  324. $start_index = 1;
  325. // This will be positive until we have finished importing posts
  326. if ( $start_index > 0 ) {
  327. // Grab all the posts
  328. $this->blogs[$importing_blog]['mode'] = 'posts';
  329. $query = "start-index=$start_index&max-results=" . MAX_RESULTS;
  330. do {
  331. $index = $struct = $entries = array();
  332. $headers = array(
  333. "GET /feeds/posts/default?$query HTTP/1.0",
  334. "Host: {$blog['host']}",
  335. "Authorization: AuthSub token=\"$this->token\""
  336. );
  337. $request = join( "\r\n", $headers ) . "\r\n\r\n";
  338. $sock = $this->_get_blogger_sock( $blog['host'] );
  339. if ( ! $sock ) return; // TODO: Error handling
  340. $response = $this->_txrx( $sock, $request );
  341. $response = $this->parse_response( $response );
  342. // Extract the entries and send for insertion
  343. preg_match_all( '/<entry[^>]*>.*?<\/entry>/s', $response['body'], $matches );
  344. if ( count( $matches[0] ) ) {
  345. $entries = array_reverse($matches[0]);
  346. foreach ( $entries as $entry ) {
  347. $entry = "<feed>$entry</feed>";
  348. $AtomParser = new AtomParser();
  349. $AtomParser->parse( $entry );
  350. $result = $this->import_post($AtomParser->entry);
  351. if ( is_wp_error( $result ) )
  352. return $result;
  353. unset($AtomParser);
  354. }
  355. } else break;
  356. // Get the 'previous' query string which we'll use on the next iteration
  357. $query = '';
  358. $links = preg_match_all('/<link([^>]*)>/', $response['body'], $matches);
  359. if ( count( $matches[1] ) )
  360. foreach ( $matches[1] as $match )
  361. if ( preg_match('/rel=.previous./', $match) )
  362. $query = html_entity_decode( preg_replace('/^.*href=[\'"].*\?(.+)[\'"].*$/', '$1', $match) );
  363. if ( $query ) {
  364. parse_str($query, $q);
  365. $this->blogs[$importing_blog]['posts_start_index'] = (int) $q['start-index'];
  366. } else
  367. $this->blogs[$importing_blog]['posts_start_index'] = 0;
  368. $this->save_vars();
  369. } while ( !empty( $query ) && $this->have_time() );
  370. }
  371. $total_results = $this->get_total_results( 'comments', $blog['host'] );
  372. $this->blogs[$importing_blog]['total_comments'] = $total_results;
  373. if ( isset( $this->blogs[$importing_blog]['comments_start_index'] ) )
  374. $start_index = (int) $this->blogs[$importing_blog]['comments_start_index'];
  375. elseif ( $total_results > MAX_RESULTS )
  376. $start_index = $total_results - MAX_RESULTS + 1;
  377. else
  378. $start_index = 1;
  379. if ( $start_index > 0 ) {
  380. // Grab all the comments
  381. $this->blogs[$importing_blog]['mode'] = 'comments';
  382. $query = "start-index=$start_index&max-results=" . MAX_RESULTS;
  383. do {
  384. $index = $struct = $entries = array();
  385. $headers = array(
  386. "GET /feeds/comments/default?$query HTTP/1.0",
  387. "Host: {$blog['host']}",
  388. "Authorization: AuthSub token=\"$this->token\""
  389. );
  390. $request = join( "\r\n", $headers ) . "\r\n\r\n";
  391. $sock = $this->_get_blogger_sock( $blog['host'] );
  392. if ( ! $sock ) return; // TODO: Error handling
  393. $response = $this->_txrx( $sock, $request );
  394. $response = $this->parse_response( $response );
  395. // Extract the comments and send for insertion
  396. preg_match_all( '/<entry[^>]*>.*?<\/entry>/s', $response['body'], $matches );
  397. if ( count( $matches[0] ) ) {
  398. $entries = array_reverse( $matches[0] );
  399. foreach ( $entries as $entry ) {
  400. $entry = "<feed>$entry</feed>";
  401. $AtomParser = new AtomParser();
  402. $AtomParser->parse( $entry );
  403. $this->import_comment($AtomParser->entry);
  404. unset($AtomParser);
  405. }
  406. }
  407. // Get the 'previous' query string which we'll use on the next iteration
  408. $query = '';
  409. $links = preg_match_all('/<link([^>]*)>/', $response['body'], $matches);
  410. if ( count( $matches[1] ) )
  411. foreach ( $matches[1] as $match )
  412. if ( preg_match('/rel=.previous./', $match) )
  413. $query = html_entity_decode( preg_replace('/^.*href=[\'"].*\?(.+)[\'"].*$/', '$1', $match) );
  414. parse_str($query, $q);
  415. $this->blogs[$importing_blog]['comments_start_index'] = (int) $q['start-index'];
  416. $this->save_vars();
  417. } while ( !empty( $query ) && $this->have_time() );
  418. }
  419. $this->blogs[$importing_blog]['mode'] = 'authors';
  420. $this->save_vars();
  421. if ( !$this->blogs[$importing_blog]['posts_done'] && !$this->blogs[$importing_blog]['comments_done'] )
  422. die('nothing');
  423. do_action('import_done', 'blogger');
  424. die('done');
  425. }
  426. function convert_date( $date ) {
  427. preg_match('#([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.[0-9]+)?(Z|[\+|\-][0-9]{2,4}){0,1}#', $date, $date_bits);
  428. $offset = iso8601_timezone_to_offset( $date_bits[7] );
  429. $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
  430. $timestamp -= $offset; // Convert from Blogger local time to GMT
  431. $timestamp += get_option('gmt_offset') * 3600; // Convert from GMT to WP local time
  432. return gmdate('Y-m-d H:i:s', $timestamp);
  433. }
  434. function no_apos( $string ) {
  435. return str_replace( '&apos;', "'", $string);
  436. }
  437. function min_whitespace( $string ) {
  438. return preg_replace( '|\s+|', ' ', $string );
  439. }
  440. function import_post( $entry ) {
  441. global $wpdb, $importing_blog;
  442. // The old permalink is all Blogger gives us to link comments to their posts.
  443. if ( isset( $entry->draft ) )
  444. $rel = 'self';
  445. else
  446. $rel = 'alternate';
  447. foreach ( $entry->links as $link ) {
  448. if ( $link['rel'] == $rel ) {
  449. $parts = parse_url( $link['href'] );
  450. $entry->old_permalink = $parts['path'];
  451. break;
  452. }
  453. }
  454. $post_date = $this->convert_date( $entry->published );
  455. $post_content = trim( addslashes( $this->no_apos( html_entity_decode( $entry->content ) ) ) );
  456. $post_title = trim( addslashes( $this->no_apos( $this->min_whitespace( $entry->title ) ) ) );
  457. $post_status = isset( $entry->draft ) ? 'draft' : 'publish';
  458. // Clean up content
  459. $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
  460. $post_content = str_replace('<br>', '<br />', $post_content);
  461. $post_content = str_replace('<hr>', '<hr />', $post_content);
  462. // Checks for duplicates
  463. if ( isset( $this->blogs[$importing_blog]['posts'][$entry->old_permalink] ) ) {
  464. ++$this->blogs[$importing_blog]['posts_skipped'];
  465. } elseif ( $post_id = post_exists( $post_title, $post_content, $post_date ) ) {
  466. $this->blogs[$importing_blog]['posts'][$entry->old_permalink] = $post_id;
  467. ++$this->blogs[$importing_blog]['posts_skipped'];
  468. } else {
  469. $post = compact('post_date', 'post_content', 'post_title', 'post_status');
  470. $post_id = wp_insert_post($post);
  471. if ( is_wp_error( $post_id ) )
  472. return $post_id;
  473. wp_create_categories( array_map( 'addslashes', $entry->categories ), $post_id );
  474. $author = $this->no_apos( strip_tags( $entry->author ) );
  475. add_post_meta( $post_id, 'blogger_blog', $this->blogs[$importing_blog]['host'], true );
  476. add_post_meta( $post_id, 'blogger_author', $author, true );
  477. add_post_meta( $post_id, 'blogger_permalink', $entry->old_permalink, true );
  478. $this->blogs[$importing_blog]['posts'][$entry->old_permalink] = $post_id;
  479. ++$this->blogs[$importing_blog]['posts_done'];
  480. }
  481. $this->save_vars();
  482. return;
  483. }
  484. function import_comment( $entry ) {
  485. global $importing_blog;
  486. // Drop the #fragment and we have the comment's old post permalink.
  487. foreach ( $entry->links as $link ) {
  488. if ( $link['rel'] == 'alternate' ) {
  489. $parts = parse_url( $link['href'] );
  490. $entry->old_permalink = $parts['fragment'];
  491. $entry->old_post_permalink = $parts['path'];
  492. break;
  493. }
  494. }
  495. $comment_post_ID = (int) $this->blogs[$importing_blog]['posts'][$entry->old_post_permalink];
  496. preg_match('#<name>(.+?)</name>.*(?:\<uri>(.+?)</uri>)?#', $entry->author, $matches);
  497. $comment_author = addslashes( $this->no_apos( strip_tags( (string) $matches[1] ) ) );
  498. $comment_author_url = addslashes( $this->no_apos( strip_tags( (string) $matches[2] ) ) );
  499. $comment_date = $this->convert_date( $entry->updated );
  500. $comment_content = addslashes( $this->no_apos( html_entity_decode( $entry->content ) ) );
  501. // Clean up content
  502. $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content);
  503. $comment_content = str_replace('<br>', '<br />', $comment_content);
  504. $comment_content = str_replace('<hr>', '<hr />', $comment_content);
  505. // Checks for duplicates
  506. if (
  507. isset( $this->blogs[$importing_blog]['comments'][$entry->old_permalink] ) ||
  508. comment_exists( $comment_author, $comment_date )
  509. ) {
  510. ++$this->blogs[$importing_blog]['comments_skipped'];
  511. } else {
  512. $comment = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_date', 'comment_content');
  513. $comment_id = wp_insert_comment($comment);
  514. $this->blogs[$importing_blog]['comments'][$entry->old_permalink] = $comment_id;
  515. ++$this->blogs[$importing_blog]['comments_done'];
  516. }
  517. $this->save_vars();
  518. }
  519. function get_js_status($blog = false) {
  520. global $importing_blog;
  521. if ( $blog === false )
  522. $blog = $this->blogs[$importing_blog];
  523. else
  524. $blog = $this->blogs[$blog];
  525. $p1 = isset( $blog['posts_done'] ) ? (int) $blog['posts_done'] : 0;
  526. $p2 = isset( $blog['total_posts'] ) ? (int) $blog['total_posts'] : 0;
  527. $c1 = isset( $blog['comments_done'] ) ? (int) $blog['comments_done'] : 0;
  528. $c2 = isset( $blog['total_comments'] ) ? (int) $blog['total_comments'] : 0;
  529. return "{p1:$p1,p2:$p2,c1:$c1,c2:$c2}";
  530. }
  531. function get_author_form($blog = false) {
  532. global $importing_blog, $wpdb, $current_user;
  533. if ( $blog === false )
  534. $blog = & $this->blogs[$importing_blog];
  535. else
  536. $blog = & $this->blogs[$blog];
  537. if ( !isset( $blog['authors'] ) ) {
  538. $post_ids = array_values($blog['posts']);
  539. $authors = (array) $wpdb->get_col("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'blogger_author' AND post_id IN (" . join( ',', $post_ids ) . ")");
  540. $blog['authors'] = array_map(null, $authors, array_fill(0, count($authors), $current_user->ID));
  541. $this->save_vars();
  542. }
  543. $directions = __('All posts were imported with the current user as author. Use this form to move each Blogger user\'s posts to a different WordPress user. You may <a href="users.php">add users</a> and then return to this page and complete the user mapping. This form may be used as many times as you like until you activate the "Restart" function below.');
  544. $heading = __('Author mapping');
  545. $blogtitle = "{$blog['title']} ({$blog['host']})";
  546. $mapthis = __('Blogger username');
  547. $tothis = __('WordPress login');
  548. $submit = js_escape( __('Save Changes') );
  549. foreach ( $blog['authors'] as $i => $author )
  550. $rows .= "<tr><td><label for='authors[$i]'>{$author[0]}</label></td><td><select name='authors[$i]' id='authors[$i]'>" . $this->get_user_options($author[1]) . "</select></td></tr>";
  551. return "<div class='wrap'><h2>$heading</h2><h3>$blogtitle</h3><p>$directions</p><form action='index.php?import=blogger&noheader=true&saveauthors=1' method='post'><input type='hidden' name='blog' value='$importing_blog' /><table cellpadding='5'><thead><td>$mapthis</td><td>$tothis</td></thead>$rows<tr><td></td><td class='submit'><input type='submit' class='button authorsubmit' value='$submit' /></td></tr></table></form></div>";
  552. }
  553. function get_user_options($current) {
  554. global $wpdb, $importer_users;
  555. if ( ! isset( $importer_users ) )
  556. $importer_users = (array) get_users_of_blog();
  557. foreach ( $importer_users as $user ) {
  558. $sel = ( $user->user_id == $current ) ? " selected='selected'" : '';
  559. $options .= "<option value='$user->user_id'$sel>$user->display_name</option>";
  560. }
  561. return $options;
  562. }
  563. function save_authors() {
  564. global $importing_blog, $wpdb;
  565. $authors = (array) $_POST['authors'];
  566. $host = $this->blogs[$importing_blog]['host'];
  567. // Get an array of posts => authors
  568. $post_ids = (array) $wpdb->get_col( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'blogger_blog' AND meta_value = %s", $host) );
  569. $post_ids = join( ',', $post_ids );
  570. $results = (array) $wpdb->get_results("SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = 'blogger_author' AND post_id IN ($post_ids)");
  571. foreach ( $results as $row )
  572. $authors_posts[$row->post_id] = $row->meta_value;
  573. foreach ( $authors as $author => $user_id ) {
  574. $user_id = (int) $user_id;
  575. // Skip authors that haven't been changed
  576. if ( $user_id == $this->blogs[$importing_blog]['authors'][$author][1] )
  577. continue;
  578. // Get a list of the selected author's posts
  579. $post_ids = (array) array_keys( $authors_posts, $this->blogs[$importing_blog]['authors'][$author][0] );
  580. $post_ids = join( ',', $post_ids);
  581. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE id IN ($post_ids)", $user_id) );
  582. $this->blogs[$importing_blog]['authors'][$author][1] = $user_id;
  583. }
  584. $this->save_vars();
  585. wp_redirect('edit.php');
  586. }
  587. function _get_auth_sock() {
  588. // Connect to https://www.google.com
  589. if ( !$sock = @ fsockopen('ssl://www.google.com', 443, $errno, $errstr) ) {
  590. $this->uh_oh(
  591. __('Could not connect to https://www.google.com'),
  592. __('There was a problem opening a secure connection to Google. This is what went wrong:'),
  593. "$errstr ($errno)"
  594. );
  595. return false;
  596. }
  597. return $sock;
  598. }
  599. function _get_blogger_sock($host = 'www2.blogger.com') {
  600. if ( !$sock = @ fsockopen($host, 80, $errno, $errstr) ) {
  601. $this->uh_oh(
  602. sprintf( __('Could not connect to %s'), $host ),
  603. __('There was a problem opening a connection to Blogger. This is what went wrong:'),
  604. "$errstr ($errno)"
  605. );
  606. return false;
  607. }
  608. return $sock;
  609. }
  610. function _txrx( $sock, $request ) {
  611. fwrite( $sock, $request );
  612. while ( ! feof( $sock ) )
  613. $response .= @ fread ( $sock, 8192 );
  614. fclose( $sock );
  615. return $response;
  616. }
  617. function revoke($token) {
  618. $headers = array(
  619. "GET /accounts/AuthSubRevokeToken HTTP/1.0",
  620. "Authorization: AuthSub token=\"$token\""
  621. );
  622. $request = join( "\r\n", $headers ) . "\r\n\r\n";
  623. $sock = $this->_get_auth_sock( );
  624. if ( ! $sock ) return false;
  625. $this->_txrx( $sock, $request );
  626. }
  627. function restart() {
  628. global $wpdb;
  629. $options = get_option( 'blogger_importer' );
  630. if ( isset( $options['token'] ) )
  631. $this->revoke( $options['token'] );
  632. delete_option('blogger_importer');
  633. $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_key = 'blogger_author'");
  634. wp_redirect('?import=blogger');
  635. }
  636. // Returns associative array of code, header, cookies, body. Based on code from php.net.
  637. function parse_response($this_response) {
  638. // Split response into header and body sections
  639. list($response_headers, $response_body) = explode("\r\n\r\n", $this_response, 2);
  640. $response_header_lines = explode("\r\n", $response_headers);
  641. // First line of headers is the HTTP response code
  642. $http_response_line = array_shift($response_header_lines);
  643. if(preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line, $matches)) { $response_code = $matches[1]; }
  644. // put the rest of the headers in an array
  645. $response_header_array = array();
  646. foreach($response_header_lines as $header_line) {
  647. list($header,$value) = explode(': ', $header_line, 2);
  648. $response_header_array[$header] .= $value."\n";
  649. }
  650. $cookie_array = array();
  651. $cookies = explode("\n", $response_header_array["Set-Cookie"]);
  652. foreach($cookies as $this_cookie) { array_push($cookie_array, "Cookie: ".$this_cookie); }
  653. return array("code" => $response_code, "header" => $response_header_array, "cookies" => $cookie_array, "body" => $response_body);
  654. }
  655. // Step 9: Congratulate the user
  656. function congrats() {
  657. $blog = (int) $_GET['blog'];
  658. 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>';
  659. if ( count($this->import['blogs']) > 1 )
  660. echo '<li>'.__('In case you haven\'t done it already, you can import the posts from your other blogs:'). $this->show_blogs() . '</li>';
  661. if ( $n = count($this->import['blogs'][$blog]['newusers']) )
  662. 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>';
  663. echo '<li>'.__('For security, click the link below to reset this importer.').'</li>';
  664. echo '</ul>';
  665. }
  666. // Figures out what to do, then does it.
  667. function start() {
  668. if ( isset($_POST['restart']) )
  669. $this->restart();
  670. $options = get_option('blogger_importer');
  671. if ( is_array($options) )
  672. foreach ( $options as $key => $value )
  673. $this->$key = $value;
  674. if ( isset( $_REQUEST['blog'] ) ) {
  675. $blog = is_array($_REQUEST['blog']) ? array_shift( $keys = array_keys( $_REQUEST['blog'] ) ) : $_REQUEST['blog'];
  676. $blog = (int) $blog;
  677. $result = $this->import_blog( $blog );
  678. if ( is_wp_error( $result ) )
  679. echo $result->get_error_message();
  680. } elseif ( isset($_GET['token']) )
  681. $this->auth();
  682. elseif ( $this->token && $this->token_is_valid() )
  683. $this->show_blogs();
  684. else
  685. $this->greet();
  686. $saved = $this->save_vars();
  687. if ( $saved && !isset($_GET['noheader']) ) {
  688. $restart = __('Restart');
  689. $message = __('We have saved some information about your Blogger account in your WordPress database. Clearing this information will allow you to start over. Restarting will not affect any posts you have already imported. If you attempt to re-import a blog, duplicate posts and comments will be skipped.');
  690. $submit = __('Clear account information');
  691. echo "<div class='wrap'><h2>$restart</h2><p>$message</p><form method='post' action='?import=blogger&noheader=true'><p class='submit' style='text-align:left;'><input type='submit' class='button' value='$submit' name='restart' /></p></form></div>";
  692. }
  693. }
  694. function save_vars() {
  695. $vars = get_object_vars($this);
  696. update_option( 'blogger_importer', $vars );
  697. return !empty($vars);
  698. }
  699. function admin_head() {
  700. ?>
  701. <style type="text/css">
  702. td { text-align: center; line-height: 2em;}
  703. thead td { font-weight: bold; }
  704. .bar {
  705. width: 200px;
  706. text-align: left;
  707. line-height: 2em;
  708. padding: 0px;
  709. }
  710. .ind {
  711. position: absolute;
  712. background-color: #83B4D8;
  713. width: 1px;
  714. z-index: 9;
  715. }
  716. .stat {
  717. z-index: 10;
  718. position: relative;
  719. text-align: center;
  720. }
  721. </style>
  722. <?php
  723. }
  724. function Blogger_Import() {
  725. global $importer_started;
  726. $importer_started = time();
  727. if ( isset( $_GET['import'] ) && $_GET['import'] == 'blogger' ) {
  728. wp_enqueue_script('jquery');
  729. add_action('admin_head', array(&$this, 'admin_head'));
  730. }
  731. }
  732. }
  733. $blogger_import = new Blogger_Import();
  734. register_importer('blogger', __('Blogger'), __('Import posts, comments, and users from a Blogger blog.'), array ($blogger_import, 'start'));
  735. class AtomEntry {
  736. var $links = array();
  737. var $categories = array();
  738. }
  739. class AtomParser {
  740. var $ATOM_CONTENT_ELEMENTS = array('content','summary','title','subtitle','rights');
  741. var $ATOM_SIMPLE_ELEMENTS = array('id','updated','published','draft','author');
  742. var $depth = 0;
  743. var $indent = 2;
  744. var $in_content;
  745. var $ns_contexts = array();
  746. var $ns_decls = array();
  747. var $is_xhtml = false;
  748. var $skipped_div = false;
  749. var $entry;
  750. function AtomParser() {
  751. $this->entry = new AtomEntry();
  752. $this->map_attrs_func = create_function('$k,$v', 'return "$k=\"$v\"";');
  753. $this->map_xmlns_func = create_function('$p,$n', '$xd = "xmlns"; if(strlen($n[0])>0) $xd .= ":{$n[0]}"; return "{$xd}=\"{$n[1]}\"";');
  754. }
  755. function parse($xml) {
  756. global $app_logging;
  757. array_unshift($this->ns_contexts, array());
  758. $parser = xml_parser_create_ns();
  759. xml_set_object($parser, $this);
  760. xml_set_element_handler($parser, "start_element", "end_element");
  761. xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
  762. xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
  763. xml_set_character_data_handler($parser, "cdata");
  764. xml_set_default_handler($parser, "_default");
  765. xml_set_start_namespace_decl_handler($parser, "start_ns");
  766. xml_set_end_namespace_decl_handler($parser, "end_ns");
  767. $contents = "";
  768. xml_parse($parser, $xml);
  769. xml_parser_free($parser);
  770. return true;
  771. }
  772. function start_element($parser, $name, $attrs) {
  773. $tag = array_pop(split(":", $name));
  774. array_unshift($this->ns_contexts, $this->ns_decls);
  775. $this->depth++;
  776. if(!empty($this->in_content)) {
  777. $attrs_prefix = array();
  778. // resolve prefixes for attributes
  779. foreach($attrs as $key => $value) {
  780. $attrs_prefix[$this->ns_to_prefix($key)] = $this->xml_escape($value);
  781. }
  782. $attrs_str = join(' ', array_map($this->map_attrs_func, array_keys($attrs_prefix), array_values($attrs_prefix)));
  783. if(strlen($attrs_str) > 0) {
  784. $attrs_str = " " . $attrs_str;
  785. }
  786. $xmlns_str = join(' ', array_map($this->map_xmlns_func, array_keys($this->ns_contexts[0]), array_values($this->ns_contexts[0])));
  787. if(strlen($xmlns_str) > 0) {
  788. $xmlns_str = " " . $xmlns_str;
  789. }
  790. // handle self-closing tags (case: a new child found right-away, no text node)
  791. if(count($this->in_content) == 2) {
  792. array_push($this->in_content, ">");
  793. }
  794. array_push($this->in_content, "<". $this->ns_to_prefix($name) ."{$xmlns_str}{$attrs_str}");
  795. } else if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS) || in_array($tag, $this->ATOM_SIMPLE_ELEMENTS)) {
  796. $this->in_content = array();
  797. $this->is_xhtml = $attrs['type'] == 'xhtml';
  798. array_push($this->in_content, array($tag,$this->depth));
  799. } else if($tag == 'link') {
  800. array_push($this->entry->links, $attrs);
  801. } else if($tag == 'category') {
  802. array_push($this->entry->categories, $attrs['term']);
  803. }
  804. $this->ns_decls = array();
  805. }
  806. function end_element($parser, $name) {
  807. $tag = array_pop(split(":", $name));
  808. if(!empty($this->in_content)) {
  809. if($this->in_content[0][0] == $tag &&
  810. $this->in_content[0][1] == $this->depth) {
  811. array_shift($this->in_content);
  812. if($this->is_xhtml) {
  813. $this->in_content = array_slice($this->in_content, 2, count($this->in_content)-3);
  814. }
  815. $this->entry->$tag = join('',$this->in_content);
  816. $this->in_content = array();
  817. } else {
  818. $endtag = $this->ns_to_prefix($name);
  819. if (strpos($this->in_content[count($this->in_content)-1], '<' . $endtag) !== false) {
  820. array_push($this->in_content, "/>");
  821. } else {
  822. array_push($this->in_content, "</$endtag>");
  823. }
  824. }
  825. }
  826. array_shift($this->ns_contexts);
  827. #print str_repeat(" ", $this->depth * $this->indent) . "end_element('$name')" ."\n";
  828. $this->depth--;
  829. }
  830. function start_ns($parser, $prefix, $uri) {
  831. #print str_repeat(" ", $this->depth * $this->indent) . "starting: " . $prefix . ":" . $uri . "\n";
  832. array_push($this->ns_decls, array($prefix,$uri));
  833. }
  834. function end_ns($parser, $prefix) {
  835. #print str_repeat(" ", $this->depth * $this->indent) . "ending: #" . $prefix . "#\n";
  836. }
  837. function cdata($parser, $data) {
  838. #print str_repeat(" ", $this->depth * $this->indent) . "data: #" . $data . "#\n";
  839. if(!empty($this->in_content)) {
  840. // handle self-closing tags (case: text node found, need to close element started)
  841. if (strpos($this->in_content[count($this->in_content)-1], '<') !== false) {
  842. array_push($this->in_content, ">");
  843. }
  844. array_push($this->in_content, $this->xml_escape($data));
  845. }
  846. }
  847. function _default($parser, $data) {
  848. # when does this gets called?
  849. }
  850. function ns_to_prefix($qname) {
  851. $components = split(":", $qname);
  852. $name = array_pop($components);
  853. if(!empty($components)) {
  854. $ns = join(":",$components);
  855. foreach($this->ns_contexts as $context) {
  856. foreach($context as $mapping) {
  857. if($mapping[1] == $ns && strlen($mapping[0]) > 0) {
  858. return "$mapping[0]:$name";
  859. }
  860. }
  861. }
  862. }
  863. return $name;
  864. }
  865. function xml_escape($string)
  866. {
  867. return str_replace(array('&','"',"'",'<','>'),
  868. array('&amp;','&quot;','&apos;','&lt;','&gt;'),
  869. $string );
  870. }
  871. }
  872. ?>