This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e107-importer.php
executable file
·2033 lines (1771 loc) · 91.3 KB
/
e107-importer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: e107 Importer
Plugin URI: http://github.com/kdeldycke/e107-importer
Description: e107 import plugin for WordPress.
Author: Kevin Deldycke
Author URI: http://kevin.deldycke.com
Version: 1.3.dev
Stable tag: 1.2
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
if ( !defined('WP_LOAD_IMPORTERS') )
return;
// Load Importer API
require_once ABSPATH . 'wp-admin/includes/import.php';
if ( !class_exists( 'WP_Importer' ) ) {
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
if ( file_exists( $class_wp_importer ) )
require_once $class_wp_importer;
}
// Constant
define("E107_IMPORTER_PATH" , WP_PLUGIN_DIR . '/e107-importer/');
define("E107_INCLUDES_FOLDER" , 'e107-includes');
define("E107_REDIRECTOR_PLUGIN", 'e107-importer/e107-redirector.php');
define("BBPRESS_PLUGIN" , 'bbpress/bbpress.php');
// Define a dummy class mimicking e107_handlers/e107_class.php:e107
// This is necessary as it is used by e107_files/bbcode/img.bb to compute some paths
class redefined_e107 {
var $base_path;
function redefined_e107() {
$this->base_path = '';
}
}
if ( class_exists( 'WP_Importer' ) ) {
class e107_Import extends WP_Importer {
// e107 database variables
var $e107_db;
var $e107_db_host;
var $e107_db_user;
var $e107_db_pass;
var $e107_db_name;
var $e107_db_prefix;
// User options
var $e107_content_ownership;
var $e107_mail_user;
var $e107_import_news;
var $e107_extended_news;
var $e107_import_pages;
var $e107_import_forums;
var $e107_import_forum_ids;
var $e107_bbcode_parser;
var $e107_import_images;
var $e107_url_update;
// e107 to WordPress content mapping
var $user_mapping;
var $news_mapping;
var $category_mapping;
var $page_mapping;
var $comment_mapping;
var $forum_mapping;
var $forum_post_mapping;
var $image_mapping;
// Initialized in initImportContext()
var $e107_pref;
var $e107_parser;
// Parse a list of user-provided integers
function parse_id_list($id_list_string) {
$id_list = array();
$cleaned_string = '';
foreach (str_split($id_list_string) as $i => $c) {
$ascii_code = ord($c);
if ($ascii_code >= 48 and $ascii_code <= 57)
$cleaned_string .= $c;
else
$cleaned_string .= ' ';
}
foreach (explode(' ', $cleaned_string) as $s) {
if (!empty($s))
$id_list[] = (int)$s;
}
return array_unique($id_list);
}
// Convert unix timestamp to mysql datetimestamp
function mysql_date($unix_time) {
return date("Y-m-d H:i:s", $unix_time);
}
// Walk a hierarchical MySQL table structure to return the path of a particular node
// Function inspired by http://blogs.sitepoint.com/hierarchical-data-database/
function mysql_get_path($node_id, $table_name, $node_column_name, $parent_column_name) {
$path = array();
// Get the parent of the current node
$sql = "SELECT ".$parent_column_name." FROM `".$table_name."` WHERE ".$node_column_name." = ".$node_id;
$parents = $this->query_e107_db($sql);
foreach ($parents as $parent) {
$parent_id = 0;
if (array_key_exists($parent_column_name, $parent)) {
$parent_id = $parent[$parent_column_name];
}
if (!empty($parent_id)) {
$path[] = $parent_id;
$path = array_merge($this->mysql_get_path($parent_id, $table_name, $node_column_name, $parent_column_name), $path);
}
}
return $path;
}
// Convert hexadecimal IP address string to decimal
function ip_hex2dec($hex_ip) {
if (strlen($hex_ip) != 8)
return '';
$dec_ip = (string) hexdec(substr($hex_ip, 0, 2));
$dec_ip .= '.';
$dec_ip .= (string) hexdec(substr($hex_ip, 2, 2));
$dec_ip .= '.';
$dec_ip .= (string) hexdec(substr($hex_ip, 4, 2));
$dec_ip .= '.';
$dec_ip .= (string) hexdec(substr($hex_ip, 6, 2));
return $dec_ip;
}
// Return a dictionnary of all HTML tags of a given kind and their attributes
function extract_html_tags($html_content, $tag_name, $allowed_protocols=array()) {
// Default list of protocols copied from wp-includes/kses.php:wp_kses()
if (empty($allowed_protocols))
$allowed_protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn');
$tag_list = array();
$tag_name = strtolower($tag_name);
$tag_regexp = '/<\/?\s*'.$tag_name.'\s+(.+?)>/i';
if (preg_match_all($tag_regexp, $html_content, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
// Parse attributes
$attributes = array();
foreach (wp_kses_hair($match[1], $allowed_protocols) as $attr)
$attributes[$attr['name']] = $attr['value'];
// Group all data of the tag in one array
$tag_list[] = array( 'tag_string' => $match[0]
, 'tag_name' => $tag_name
, 'attribute_string' => $match[1]
, 'attributes' => $attributes
);
}
}
return $tag_list;
}
// This method is the mirror of the above as it regenerate an html tag
// This method take as input the same tag structure produced by extract_html_tags()
function build_html_tag($tag_data) {
$new_tag = "<".$tag_data['tag_name'];
foreach ($tag_data['attributes'] as $attribute => $value)
// This condition remove empty tag attributes like class='' and alt=''
if (!empty($value)) {
// Choose the right kind of quote
$quote = '"';
if (strpos($value, $quote) !== False)
$quote = '\'';
$new_tag .= " $attribute=$quote$value$quote";
}
$new_tag .= ">";
return $new_tag;
}
// Return the domain name of a URL, discarding sub-domains
function get_domain_name($url) {
$domain = '';
$host = parse_url($url, PHP_URL_HOST);
$d = array_reverse(explode(".", $host));
if (isset($d[0]))
$domain = $d[0];
if (isset($d[1]))
$domain = $d[1].'.'.$domain;
return $domain;
}
// Build the list of authorized domains (past and current local domains) from which we are allowed to import content
function get_local_domains() {
$domain_list = array();
$url_list = array( $this->e107_pref['siteurl']
, SITEURL
, get_option('siteurl')
, get_option('home')
);
foreach ($url_list as $url)
$domain_list[] = $this->get_domain_name($url);
return array_unique($domain_list);
}
function link_attachment_to_post($attachment_id, $post_id) {
global $wpdb;
return $wpdb->update($wpdb->posts, array('post_parent' => $post_id), array('ID' => $attachment_id));
}
function get_attachment_id_from_url($url) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$url'";
return $wpdb->get_var($query);
}
// Get the list of all posts' IDs
function get_post_id_list($posts_to_ignore = array('attachment', 'revision')) {
$post_id_list = array();
$post_types = array_diff(get_post_types(), $posts_to_ignore);
$posts = get_posts(array('numberposts' => -1, 'post_status' => null, 'post_type' => $post_types));
foreach ($posts as $post)
$post_id_list[] = $post->ID;
return $post_id_list;
}
// Get the list of all comments' IDs
function get_comment_id_list() {
$comment_id_list = array();
$comments = get_comments();
foreach ($comments as $comment)
$comment_id_list[] = $comment->comment_ID;
return $comment_id_list;
}
// Generic code to initialize the e107 context
function init_e107_context() {
/* Some part of the code below is copy of (and/or inspired by) code from the e107 project, licensed
** under the GPL and (c) copyrighted to e107's contributors.
*/
// Global variables used in replaceConstants() method
global $ADMIN_DIRECTORY, $FILES_DIRECTORY, $IMAGES_DIRECTORY, $THEMES_DIRECTORY, $PLUGINS_DIRECTORY, $HANDLERS_DIRECTORY, $LANGUAGES_DIRECTORY, $HELP_DIRECTORY, $DOWNLOADS_DIRECTORY;
// Define e107 original path structure
$ADMIN_DIRECTORY = "e107_admin/";
$FILES_DIRECTORY = "e107_files/";
$IMAGES_DIRECTORY = "e107_images/";
$THEMES_DIRECTORY = "e107_themes/";
$PLUGINS_DIRECTORY = "e107_plugins/";
$HANDLERS_DIRECTORY = "e107_handlers/";
$LANGUAGES_DIRECTORY = "e107_languages/";
$HELP_DIRECTORY = "e107_docs/help/";
$DOWNLOADS_DIRECTORY = "e107_files/downloads/";
// Redifine some globals to match WordPress file hierarchy
define("e_BASE" , ABSPATH);
define("e_FILE" , E107_IMPORTER_PATH);
define("e_PLUGIN" , E107_IMPORTER_PATH);
define("e_HANDLER", E107_IMPORTER_PATH . E107_INCLUDES_FOLDER . '/');
// Don't know why but in certain cases e_IMAGE was not defined
define("e_IMAGE" , $IMAGES_DIRECTORY);
// e107-includes/quote.bb refers to some language files.
// Force it to use the default english translation bundled with this WordPress plugin.
define("e_LANGUAGEDIR", e_HANDLER);
define("e_LANGUAGE" , '');
// Create a dummy e107 instance to accomodate the img BBCode parser
global $e107;
$e107 = new redefined_e107();
// Set user-related globals referenced in e_parse_class.php
define("ADMIN" , True); // Will replace {e_ADMIN} constant with $ADMIN_DIRECTORY
define("USER" , False); // Will not replace {USERID} constant
define("USERID", 0);
// CHARSET is normally set in e107_languages/English/English.php file
define("CHARSET", "utf-8");
/*========== START of code inspired by class2.php file ==========
+ ----------------------------------------------------------------------------+
| e107 website system
|
| Copyright (C) 2001-2002 Steve Dunstan (jalist@e107.org)
| Copyright (C) 2008-2010 e107 Inc (e107.org)
|
| Released under the terms and conditions of the
| GNU General Public License (http://gnu.org).
|
| $URL: https://e107.svn.sourceforge.net/svnroot/e107/trunk/e107_0.7/class2.php $
| $Revision: 11786 $
| $Id: class2.php 11786 2010-09-15 22:12:49Z e107coders $
| $Author: e107coders $
+----------------------------------------------------------------------------+
*/
define("e107_INIT", True);
// Create a new e107 parser
require_once(e_HANDLER.'e_parse_class.php');
$this->e107_parser = new e_parse;
// $tp is required by bbcode_handler.php
global $tp;
$tp = $this->e107_parser;
define("THEME", "");
define("E107_DEBUG_LEVEL", False);
define('E107_DBG_BBSC', False); // Show BBCode / Shortcode usage in postings
function check_class($var, $userclass='', $peer=False, $debug=False) {
return True;
}
function include_lan($path, $force = False) {
$ret = ($force) ? include($path) : include_once($path);
return (isset($ret)) ? $ret : '';
}
// Use these to combine isset() and use of the set value. or defined and use of a constant
// i.e. to fix if($pref['foo']) ==> if ( varset($pref['foo']) ) will use the pref, or ''.
// Can set 2nd param to any other default value you like (e.g. false, 0, or whatever)
// $testvalue adds additional test of the value (not just isset())
// Examples:
// $something = pref; // Bug if pref not set ==> $something = varset(pref);
// $something = isset(pref) ? pref : ""; ==> $something = varset(pref);
// $something = isset(pref) ? pref : default; ==> $something = varset(pref,default);
// $something = isset(pref) && pref ? pref : default; ==> use varsettrue(pref,default)
//
function varset(&$val,$default='') {
if (isset($val)) {
return $val;
}
return $default;
}
function defset($str,$default='') {
if (defined($str)) {
return constant($str);
}
return $default;
}
//
// These variants are like the above, but only return the value if both set AND 'true'
//
function varsettrue(&$val,$default='') {
if (isset($val) && $val) return $val;
return $default;
}
function defsettrue($str,$default='') {
if (defined($str) && constant($str)) return constant($str);
return $default;
}
/*========== END of code inspired by class2.php file ==========*/
// Load preferences if not already loaded
if (!isset($this->e107_pref) || !is_array($this->e107_pref))
$this->load_e107_preferences();
// Override BBCode definition files configuration
$this->e107_pref['bbcode_list'] = array();
$this->e107_pref['bbcode_list'][E107_INCLUDES_FOLDER] = array();
// This $core_bb array come from bbcode_handler.php
$core_bb = array(
'blockquote', 'img', 'i', 'u', 'center',
'_br', 'color', 'size', 'code',
'html', 'flash', 'link', 'email',
'url', 'quote', 'left', 'right',
'b', 'justify', 'file', 'stream',
'textarea', 'list', 'php', 'time',
'spoiler', 'hide', 'youtube', 'sanitised'
);
foreach ($core_bb as $c)
$this->e107_pref['bbcode_list'][E107_INCLUDES_FOLDER][$c] = 'dummy_u_class';
// Don't transform smileys to <img>, WordPress will do it automaticcaly
$this->e107_pref['smiley_activate'] = False;
// Turn-off profanity filter
$this->e107_pref['profanity_filter'] = False;
// Disable all extra HTML rendering hooks like the one coming from e107 Linkwords plugin
$this->e107_pref['tohtml_hook'] = '';
// Set global SITEURL as it's used by replaceConstants() method
$site_url = $this->e107_pref['siteurl'];
// Normalize URL: it must end with a single slash
define("SITEURL", rtrim($site_url, '/').'/');
// Required to make default e107 methods aware of preferences
global $pref;
$pref = $this->e107_pref;
}
// Establish a connection to the e107 database.
// This code is kept in a separate method to not mess with $wpdb ...
function connect_to_e107_db() {
$this->e107_db = mysql_connect($this->e107_db_host, $this->e107_db_user, $this->e107_db_pass) or
wp_die("Can't connect to e107 database server: " . mysql_error());
// Force UTF-8 querying
@mysql_query("SET NAMES `utf8`", $this->e107_db);
//@mysql_query("SET CHARACTER SET `utf8`", $this->e107_db);
$this->e107_db_prefix = $this->e107_db_name.'`.`'.$this->e107_db_prefix;
set_magic_quotes_runtime(0);
}
function query_e107_db($sql) {
$result = mysql_query($sql, $this->e107_db);
if (!$result)
wp_die('Invalid query: ' . mysql_error());
$result_array = array();
$num_rows = 0;
while ($row = @mysql_fetch_object($result)) {
$result_array[$num_rows] = (array) $row;
$num_rows++;
}
@mysql_free_result($result);
return $result_array;
}
function load_e107_preferences() {
$e107_core_table = $this->e107_db_prefix.'core';
$sql = "SELECT e107_value FROM `".$e107_core_table."` WHERE e107_name = 'SitePrefs'";
$site_pref = $this->query_e107_db($sql);
extract($site_pref[0]);
$this->e107_pref = '';
@eval('$this->e107_pref = '.trim($e107_value).';');
}
function get_e107_user_list() {
// Prepare the SQL request
$e107_user_table = $this->e107_db_prefix."user";
$e107_user_extended_table = $this->e107_db_prefix."user_extended";
$sql = "SELECT `".$e107_user_table."`.* FROM `".$e107_user_table."` ";
$sql .= "LEFT JOIN `".$e107_user_extended_table."` ON `".$e107_user_table."`.user_id = `".$e107_user_extended_table."`.user_extended_id ";
// Exclude banned and un-verified users
$sql .= "WHERE user_ban = 0";
// Perform the request and return rows
return $this->query_e107_db($sql);
}
function get_e107_category_list() {
// Prepare the SQL request
$e107_news_category_table = $this->e107_db_prefix."news_category";
$sql = "SELECT * FROM `".$e107_news_category_table."`";
// Perform the request and return rows
return $this->query_e107_db($sql);
}
function get_e107_news_list() {
// Prepare the SQL request
$e107_news_table = $this->e107_db_prefix."news";
$sql = "SELECT * FROM `".$e107_news_table."`";
// Perform the request and return rows
return $this->query_e107_db($sql);
}
function get_e107_page_list() {
// Prepare the SQL request
$e107_pages_table = $this->e107_db_prefix."page";
$sql = "SELECT * FROM `".$e107_pages_table."`";
// Perform the request and return rows
return $this->query_e107_db($sql);
}
function get_e107_comment_list() {
// Prepare the SQL request
$e107_comments_table = $this->e107_db_prefix."comments";
$sql = "SELECT * FROM `".$e107_comments_table."`";
// Perform the request and return rows
return $this->query_e107_db($sql);
}
function get_e107_forum_list($forum_id_list = array()) {
// Prepare the SQL request
$e107_forums_table = $this->e107_db_prefix."forum";
$sql = "SELECT * FROM `".$e107_forums_table."`";
// Only get a subset of forums
if (!empty($forum_id_list)) {
// Get all parents of the forum
$forums_parents = array();
foreach ($forum_id_list as $forum_id) {
$forums_parents = array_merge($forums_parents, $this->mysql_get_path($forum_id, $e107_forums_table, 'forum_id', 'forum_parent'));
}
$forums_to_fetch = array_unique(array_merge($forums_parents, $forum_id_list));
$sql .= " WHERE forum_id IN (".implode(', ', $forums_to_fetch).")";
}
// Forcing the order makes sure we have childrens before their parents
$sql .= " ORDER BY forum_parent, forum_id";
// Perform the request and return rows
return $this->query_e107_db($sql);
}
function get_e107_forum_post_list($forum_id_list = array()) {
// Prepare the SQL request
$e107_posts_table = $this->e107_db_prefix."forum_t";
$sql = "SELECT * FROM `".$e107_posts_table."`";
// Only get posts from a subset of forums
if (!empty($forum_id_list)) {
$sql .= " WHERE thread_forum_id IN (".implode(', ', $forum_id_list).")";
}
// Forcing the order makes sure we have childrens before their parents
$sql .= " ORDER BY thread_parent, thread_id";
// Perform the request and return rows
return $this->query_e107_db($sql);
}
// Get a list of all WordPress's user IDs
function get_wp_user_ids() {
global $wpdb;
return $wpdb->get_col($wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users ORDER BY %s ASC", 'ID'));
}
// Import e107 preferences (aka global config)
function import_preferences() {
update_option('blogname' , $this->e107_pref['sitename']);
update_option('admin_email' , $this->e107_pref['siteadminemail']);
update_option('users_can_register' , $this->e107_pref['user_reg']);
update_option('comment_registration', !$this->e107_pref['anon_post']);
update_option('use_smilies' , $this->e107_pref['smiley_activate']);
update_option('posts_per_page' , $this->e107_pref['newsposts']);
$tag_line = $this->e107_pref['sitetag'];
if (strlen($tag_line) <= 0)
$tag_line = $this->e107_pref['sitedescription'];
update_option('blogdescription', $tag_line);
$gmt_offset = $this->e107_pref['time_offset'];
if (!(empty($this->e107_pref['timezone']) or (strrpos(strtolower($this->e107_pref['timezone']), strtolower('GMT')) === False))) {
$x = 0;
$gmt_offset = (int) $gmt_offset + $x;
}
update_option('gmt_offset', $gmt_offset);
}
// Method to force ownership of all imported content to a single user
function set_global_ownership($new_owner_id) {
// Get the list of all e107 user IDs
$user_list = $this->get_e107_user_list();
// The new user mapping is set to our given global owner
$this->user_mapping = array();
foreach ($user_list as $user) {
extract($user);
$user_id = (int) $user_id;
$this->user_mapping[$user_id] = (int) $new_owner_id;
}
}
// Import all e107 users to WordPress
function import_users() {
// Get user list
$user_list = $this->get_e107_user_list();
foreach ($user_list as $user)
$this->import_user($user);
}
// Migrate one user to WordPress using its e107 data
function import_user($user) {
extract($user);
$user_id = (int) $user_id;
// e107 user details mapping
// $user_loginname => WP login
// $user_name => WP nickname (the one to display)
// $user_login => WP First + Last name
// Try to get first and last name
if (!empty($user_login)) {
$words = explode(" ", $user_login, 2);
$first_name = $words[0];
if (sizeof($words) > 1)
$last_name = $words[1];
}
// Try to get the display name
$display_name = '';
if (!empty($user_name))
$display_name = $user_name;
elseif (!empty($user_login))
$display_name = $user_login;
elseif (!empty($user_loginname))
$display_name = $user_loginname;
$user_data = array(
'first_name' => empty($first_name ) ? '' : esc_sql($first_name)
, 'last_name' => empty($last_name ) ? '' : esc_sql($last_name)
, 'nickname' => empty($user_name ) ? '' : esc_sql($user_name)
, 'display_name' => empty($display_name ) ? '' : esc_sql($display_name)
, 'user_email' => empty($user_email ) ? '' : esc_sql($user_email)
, 'user_registered' => empty($user_join ) ? '' : $this->mysql_date($user_join)
, 'user_url' => empty($user_homepage) ? '' : esc_sql($user_homepage)
, 'aim' => empty($user_aim ) ? '' : esc_sql($user_aim)
, 'yim' => empty($user_msn ) ? '' : esc_sql($user_msn) // Put MSN contact here because they have merged with Yahoo!: http://slashdot.org/articles/05/10/12/0227207.shtml
);
// In case of an update, do not reset previous user profile properties by an empty value
foreach ($user_data as $k=>$v)
if (strlen($v) <= 0)
unset($user_data[$k]);
// Sanitize login string
$user_loginname = sanitize_user($user_loginname, $strict=True);
// Try to find a previous user and its ID
$wp_user_ID = False;
if (email_exists($user_email))
$wp_user_ID = email_exists($user_email);
elseif (username_exists($user_loginname))
$wp_user_ID = username_exists($user_loginname);
// Create a new user
if (!$wp_user_ID) {
// New password is required because we can't decrypt e107 password
$new_password = wp_generate_password(12, False);
$user_data['user_pass'] = esc_sql($new_password);
// Don't reset login name on user update
$user_data['user_login'] = esc_sql($user_loginname);
$ret_id = wp_insert_user($user_data);
// Send mail notification to users to warn them of a new password (and new login because of UTF-8)
if ($this->e107_mail_user)
wp_new_user_notification($ret_id, $new_password);
} else {
// User already exist, update its profile
$user_data['ID'] = $wp_user_ID;
$ret_id = wp_update_user($user_data);
}
// Update user mapping, cast to int
$this->user_mapping[$user_id] = (int) $ret_id;
// Update user's description with remaining parameters like signature, location and birthday.
$extra_info_list = array();
if (!empty($user_signature)) $extra_info_list[] = __("Signature: ").$user_signature;
if (!empty($user_customtitle)) $extra_info_list[] = __("Custom title: ").$user_customtitle;
if (!empty($user_location)) $extra_info_list[] = __("Location: ").$user_location;
if (!empty($user_birthday) && $user_birthday != '0000-00-00') $extra_info_list[] = __("Birthday: ").$user_birthday;
$wp_user = new WP_User($ret_id);
$old_description = $wp_user->description;
$new_description = $old_description;
foreach (array_reverse($extra_info_list) as $extra_info)
if (stristr($new_description, $extra_info) === False)
$new_description = $extra_info."\n".$new_description;
if ($new_description != $old_description)
wp_update_user(array('ID' => $wp_user->ID, 'description' => $new_description));
}
// Import all e107 news and categories to WordPress
function import_news_and_categories() {
// Import categories
$category_list = $this->get_e107_category_list();
foreach ($category_list as $category)
$this->import_category($category);
// Import news
$news_list = $this->get_e107_news_list();
foreach ($news_list as $news)
$this->import_news($news);
}
// Migrate one category to WordPress using its e107 data
function import_category($category) {
extract($category);
$cat_id = category_exists($category_name);
if (!$cat_id) {
$new_cat = array();
$new_cat['cat_name'] = $category_name;
$cat_id = wp_insert_category($new_cat);
}
$this->category_mapping[$category_id] = (int) $cat_id;
}
// Migrate one news to WordPress using its e107 data
function import_news($news) {
extract($news);
$news_id = (int) $news_id;
// Update author role if necessary;
// If the user has the minimum role (aka subscriber) he is not able to post
// news. In this case, we increase his role by one level (aka contributor).
$author_id = $this->user_mapping[$news_author];
$author = new WP_User($author_id);
if (! $author->has_cap('edit_posts'))
$author->set_role('contributor');
// Build post data
$post_data = array(
'post_author' => $author_id // use the new wordpress user ID
, 'post_date' => $this->mysql_date($news_datestamp)
, 'post_date_gmt' => $this->mysql_date($news_datestamp)
, 'post_title' => esc_sql($news_title)
, 'post_status' => 'publish' // News are always published in e107
, 'comment_status' => $news_allow_comments // TODO: get global config to set this value dynamiccaly
, 'ping_status' => 'open' // XXX is there such a concept in e107 ?
, 'comment_count' => $news_comment_total
);
// Put body and extended part of news to either main content or excerpt
$news_body = trim($news_body);
$news_extended = trim($news_extended);
switch ($this->e107_extended_news) {
case 'use_excerpt':
if (empty($news_extended)) {
$post_data['post_content'] = esc_sql($news_body);
} else {
$post_data['post_excerpt'] = esc_sql($news_body);
$post_data['post_content'] = esc_sql($news_extended);
}
break;
case 'body_merge':
if (empty($news_body)) {
$post_data['post_content'] = esc_sql($news_extended);
} elseif (empty($news_extended)) {
$post_data['post_content'] = esc_sql($news_body);
} else {
$post_data['post_content'] = esc_sql($news_body."\n\n<!--more-->\n\n".$news_extended);
}
break;
case 'ignore_body':
if (empty($news_extended)) {
$post_data['post_content'] = esc_sql($news_body);
} else {
$post_data['post_content'] = esc_sql($news_extended);
}
break;
case 'ignore_extended':
if (empty($news_body)) {
$post_data['post_content'] = esc_sql($news_extended);
} else {
$post_data['post_content'] = esc_sql($news_body);
}
break;
}
// Save e107 news in WordPress database
$post_id = wp_insert_post($post_data);
// Link post to category
$news_category = (int) $news_category;
if (array_key_exists($news_category, $this->category_mapping)) {
$cats = array();
$cats[] = $this->category_mapping[$news_category];
wp_set_post_categories($post_id, $cats);
}
// Update post mapping
$this->news_mapping[$news_id] = (int) $post_id;
}
// Import all e107 pages to WordPress
function import_pages() {
$page_list = $this->get_e107_page_list();
foreach ($page_list as $page)
$this->import_page($page);
}
// Migrate one page to WordPress using its e107 data
function import_page($page) {
extract($page);
$page_id = (int) $page_id;
// Set the status of the post to 'publish' or 'private'.
// There is no 'draft' in e107.
$post_status = 'publish';
if ($page_class != '0')
$post_status = 'private';
// Update author role if necessary;
// If the user has the minimum role (aka subscriber) he is not able to post
// pages. In this case, we raise is role by 1 level (aka contributor).
if (array_key_exists($page_author, $this->user_mapping)) {
$author_id = $this->user_mapping[$page_author];
$author = new WP_User($author_id);
} else {
// Can't find the original user, use the current one.
$author = wp_get_current_user();
}
if (!$author->has_cap('edit_posts'))
$author->set_role('contributor');
// If user is the author of a private page give him the 'editor' role else he can't view private pages
if (($post_status == 'private') and (!$author->has_cap('read_private_pages')))
$author->set_role('editor');
// Define comment status
if (!$page_comment_flag) {
$comment_status = 'closed';
} else {
$comment_status = 'open';
}
// Save e107 static page in WordPress database
$ret_id = wp_insert_post(array(
'post_author' => $author->ID
, 'post_date' => $this->mysql_date($page_datestamp)
, 'post_date_gmt' => $this->mysql_date($page_datestamp)
, 'post_content' => esc_sql($page_text)
, 'post_title' => esc_sql($page_title)
, 'post_status' => $post_status
, 'post_type' => 'page'
, 'comment_status' => $comment_status
, 'ping_status' => 'closed' // XXX is there a global variable in WordPress or e107 to guess this ?
));
// Update page mapping
$this->page_mapping[$page_id] = (int) $ret_id;
}
// Import all e107 comments to WordPress
function import_comments() {
$comment_list = $this->get_e107_comment_list();
foreach ($comment_list as $comment)
$this->import_comment($comment);
}
// Migrate one comment to WordPress using its e107 data
function import_comment($comment) {
extract($comment);
$comment_id = (int) $comment_id;
$comment_item_id = (int) $comment_item_id;
// Get the post_id from $news_mapping or $pages_mapping depending of the comment type
if ($comment_type == 'page' and $this->e107_import_pages) {
$post_id = $this->page_mapping[$comment_item_id];
} elseif ($comment_type == '0' and $this->e107_import_news) {
$post_id = $this->news_mapping[$comment_item_id];
} else {
// Do not import this comment: its either a non-news or non-page comment, or the user choosed to skip news and/or pages comments import
return;
}
// Don't import comments not linked with news
$post_status = get_post_status($post_id);
if ($post_status != False) {
// Get author details from WordPress if registered.
$author_name = substr($comment_author, strpos($comment_author, '.') + 1);
$author_id = (int) strrev(substr(strrev($comment_author), strpos(strrev($comment_author), '.') + 1));
$author_ip = $this->ip_hex2dec($comment_ip);
$author_email = $comment_author_email;
unset($author_url);
// Registered user
if (array_key_exists($author_id, $this->user_mapping)) {
$author_id = $this->user_mapping[$author_id];
$author = new WP_User($author_id);
$author_name = $author->display_name;
$author_email = $author->user_email;
$author_url = $author->user_url;
// Unregistered user
} else {
unset($author_id);
// Sometimes $author_name is of given as email address. In this case, try to guess the user name.
if ($author_email == '' and filter_var($author_name, FILTER_VALIDATE_EMAIL)) {
$author_email = $author_name;
$author_name = substr($author_name, 0, strpos($author_name, '@'));
}
}
// Build up comment data array
$comment_data = array(
'comment_post_ID' => empty($post_id ) ? '' : $post_id
, 'comment_author' => empty($author_name ) ? '' : esc_sql($author_name)
, 'comment_author_email' => empty($author_email ) ? '' : esc_sql($author_email)
, 'comment_author_url' => empty($author_url ) ? '' : esc_sql($author_url)
, 'comment_author_IP' => empty($author_ip ) ? '' : $author_ip
, 'comment_date' => empty($comment_datestamp) ? '' : $this->mysql_date($comment_datestamp) //XXX ask or get the time offset ?
, 'comment_date_gmt' => empty($comment_datestamp) ? '' : $this->mysql_date($comment_datestamp) //XXX ask or get the time offset ?
, 'comment_content' => empty($comment_comment ) ? '' : esc_sql($comment_comment)
, 'comment_approved' => empty($comment_blocked ) ? '' : ! (int) $comment_blocked
, 'user_id' => empty($author_id ) ? '' : $author_id
, 'user_ID' => empty($author_id ) ? '' : $author_id
, 'filtered' => True
);
// Clean-up the array
foreach ($comment_data as $k=>$v)
if (strlen($v) <= 0)
unset($comment_data[$k]);
// Save e107 comment in WordPress database
$ret_id = wp_insert_comment($comment_data);
// Update post mapping
$this->comment_mapping[$comment_id] = (int) $ret_id;
}
}
// Import all e107 forums to bbPress plugin
function import_forums($forum_id_list = array()) {
// Group users by class
$user_classes = array();
$user_list = $this->get_e107_user_list();
foreach ($user_list as $user) {
extract($user);
if (!empty($user_class)) {
$user_class = (int) $user_class;
$updated_user_list = array();
if (array_key_exists($user_class, $user_classes))
$updated_user_list = $user_classes[$user_class];
$updated_user_list[] = (int) $user_id;
$user_classes[$user_class] = $updated_user_list;
}
}
// Import all forum
$forum_list = $this->get_e107_forum_list($forum_id_list);
foreach ($forum_list as $forum)
$this->import_forum($forum, $user_classes);
}
// Migrate one forum to bbPress plugin using its e107 data
function import_forum($forum, $user_classes) {
extract($forum);
$forum_id = (int) $forum_id;
$forum_parent = (int) $forum_parent;
$forum_moderators = (int) $forum_moderators;
// Create a list of potential author based on all moderators
$potential_authors = array();
// If moderator ID is not 254 then moderators are defined as a e107 user class.
// Else, do nothing: it means moderators are all e107 admins, which is the default bbPress behaviour.
if ($forum_moderators != 254 and array_key_exists($forum_moderators, $user_classes)) {
// Migrate moderator roles from e107 users to WordPress
foreach ($user_classes[$forum_moderators] as $moderator) {
$mod_id = (int) $this->user_mapping[$moderator];
$potential_authors[] = $mod_id;
$mod_user = new WP_User($mod_id);
$mod_role = array_shift($mod_user->roles);
// Increase user role to forum moderator only if user has no role nor moderate capability
if ((empty($mod_role) or $mod_role == 'subscriber') and !$mod_user->has_cap('moderate'))
$mod_user->set_role('bbp_moderator');
}
}
// Set the author of the forum: the oldest moderator or the oldest admin.
if (!empty($potential_authors)) {
ksort($potential_authors);
$author_id = array_shift($potential_authors);
} else {
$user_ids = $this->get_wp_user_ids();
foreach ($user_ids as $user_id) {
if (user_can($user_id, 'publish_forums')) {
$author_id = $user_id;
break;
}
}
}
// Calculate forum's parent
$updated_parent = 0;
if (array_key_exists($forum_parent, $this->forum_mapping)) {
$updated_parent = (int) $this->forum_mapping[$forum_parent];
}
// Save e107 forum in WordPress database
$ret_id = wp_insert_post(array(
'post_author' => $author_id
, 'post_date' => $this->mysql_date($forum_datestamp) //XXX ask or get the time offset ?
, 'post_date_gmt' => $this->mysql_date($forum_datestamp) //XXX ask or get the time offset ?
, 'post_content' => $forum_description
, 'post_title' => $forum_name
, 'post_name' => sanitize_title($forum_name)
, 'post_type' => bbp_get_forum_post_type()
, 'comment_status' => 'closed'
, 'ping_status' => 'closed'
, 'post_parent' => $updated_parent
, 'menu_order' => (int) $forum_order