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

Revision 5406, 22.7 kB (checked in by ryan, 1 year ago)

Add nonces to importers

  • Property svn:eol-style set to native
Line 
1 <?php
2 /*
3  * DotClear import plugin
4  * by Thomas Quinot - http://thomas.quinot.org/
5  */
6
7 /**
8     Add These Functions to make our lives easier
9 **/
10 if(!function_exists('get_catbynicename'))
11 {
12     function get_catbynicename($category_nicename)
13     {
14     global $wpdb;
15
16     $cat_id -= 0;     // force numeric
17     $name = $wpdb->get_var('SELECT cat_ID FROM '.$wpdb->categories.' WHERE category_nicename="'.$category_nicename.'"');
18
19     return $name;
20     }
21 }
22
23 if(!function_exists('get_comment_count'))
24 {
25     function get_comment_count($post_ID)
26     {
27         global $wpdb;
28         return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID);
29     }
30 }
31
32 if(!function_exists('link_cat_exists'))
33 {
34     function link_cat_exists($catname)
35     {
36         global $wpdb;
37         return $wpdb->get_var('SELECT cat_id FROM '.$wpdb->linkcategories.' WHERE cat_name = "'.$wpdb->escape($catname).'"');
38     }
39 }
40
41 if(!function_exists('link_exists'))
42 {
43     function link_exists($linkname)
44     {
45         global $wpdb;
46         return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$linkname.'"');
47     }
48 }
49
50 /*
51  Identify UTF-8 text
52  Taken from http://www.php.net/manual/fr/function.mb-detect-encoding.php#50087
53 */
54 //
55 //    utf8 encoding validation developed based on Wikipedia entry at:
56 //    http://en.wikipedia.org/wiki/UTF-8
57 //
58 //    Implemented as a recursive descent parser based on a simple state machine
59 //    copyright 2005 Maarten Meijer
60 //
61 //    This cries out for a C-implementation to be included in PHP core
62 //
63
64 function valid_1byte($char) {
65     if(!is_int($char)) return false;
66         return ($char & 0x80) == 0x00;
67 }
68
69 function valid_2byte($char) {
70     if(!is_int($char)) return false;
71         return ($char & 0xE0) == 0xC0;
72 }
73
74 function valid_3byte($char) {
75     if(!is_int($char)) return false;
76         return ($char & 0xF0) == 0xE0;
77 }
78
79 function valid_4byte($char) {
80     if(!is_int($char)) return false;
81         return ($char & 0xF8) == 0xF0;
82 }
83
84 function valid_nextbyte($char) {
85     if(!is_int($char)) return false;
86         return ($char & 0xC0) == 0x80;
87 }
88
89 function valid_utf8($string) {
90     $len = strlen($string);
91     $i = 0;
92     while( $i < $len ) {
93         $char = ord(substr($string, $i++, 1));
94         if(valid_1byte($char)) {    // continue
95             continue;
96         } else if(valid_2byte($char)) { // check 1 byte
97             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
98                 return false;
99         } else if(valid_3byte($char)) { // check 2 bytes
100             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
101                 return false;
102             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
103                 return false;
104         } else if(valid_4byte($char)) { // check 3 bytes
105             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
106                 return false;
107             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
108                 return false;
109             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
110                 return false;
111         } // goto next char
112     }
113     return true; // done
114 }
115
116 function csc ($s) {
117     if (valid_utf8 ($s)) {
118         return $s;
119     } else {
120         return iconv(get_option ("dccharset"),"UTF-8",$s);
121     }
122 }
123
124 function textconv ($s) {
125     return csc (preg_replace ('|(?<!<br />)\s*\n|', ' ', $s));
126 }
127
128 /**
129     The Main Importer Class
130 **/
131 class Dotclear_Import {
132
133     function header()
134     {
135         echo '<div class="wrap">';
136         echo '<h2>'.__('Import DotClear').'</h2>';
137         echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'</p>';
138     }
139
140     function footer()
141     {
142         echo '</div>';
143     }
144
145     function greet()
146     {
147         echo '<div class="narrow"><p>'.__('Howdy! This importer allows you to extract posts from a DotClear database into your blog.  Mileage may vary.').'</p>';
148         echo '<p>'.__('Your DotClear Configuration settings are as follows:').'</p>';
149         echo '<form action="admin.php?import=dotclear&amp;step=1" method="post">';
150         wp_nonce_field('import-dotclear');
151         $this->db_form();
152         echo '<p class="submit"><input type="submit" name="submit" value="'.attribute_escape(__('Import Categories &raquo;')).'" /></p>';
153         echo '</form></div>';
154     }
155
156     function get_dc_cats()
157     {
158         global $wpdb;
159         // General Housekeeping
160         $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
161         set_magic_quotes_runtime(0);
162         $dbprefix = get_option('dcdbprefix');
163
164         // Get Categories
165         return $dcdb->get_results('SELECT * FROM '.$dbprefix.'categorie', ARRAY_A);
166     }
167
168     function get_dc_users()
169     {
170         global $wpdb;
171         // General Housekeeping
172         $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
173         set_magic_quotes_runtime(0);
174         $dbprefix = get_option('dcdbprefix');
175
176         // Get Users
177
178         return $dcdb->get_results('SELECT * FROM '.$dbprefix.'user', ARRAY_A);
179     }
180
181     function get_dc_posts()
182     {
183         // General Housekeeping
184         $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
185         set_magic_quotes_runtime(0);
186         $dbprefix = get_option('dcdbprefix');
187
188         // Get Posts
189         return $dcdb->get_results('SELECT '.$dbprefix.'post.*, '.$dbprefix.'categorie.cat_libelle_url AS post_cat_name
190                         FROM '.$dbprefix.'post INNER JOIN '.$dbprefix.'categorie
191                         ON '.$dbprefix.'post.cat_id = '.$dbprefix.'categorie.cat_id', ARRAY_A);
192     }
193
194     function get_dc_comments()
195     {
196         global $wpdb;
197         // General Housekeeping
198         $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
199         set_magic_quotes_runtime(0);
200         $dbprefix = get_option('dcdbprefix');
201
202         // Get Comments
203         return $dcdb->get_results('SELECT * FROM '.$dbprefix.'comment', ARRAY_A);
204     }
205
206     function get_dc_links()
207     {
208         //General Housekeeping
209         $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
210         set_magic_quotes_runtime(0);
211         $dbprefix = get_option('dcdbprefix');
212
213         return $dcdb->get_results('SELECT * FROM '.$dbprefix.'link ORDER BY position', ARRAY_A);
214     }
215
216     function cat2wp($categories='')
217     {
218         // General Housekeeping
219         global $wpdb;
220         $count = 0;
221         $dccat2wpcat = array();
222         // Do the Magic
223         if(is_array($categories))
224         {
225             echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
226             foreach ($categories as $category)
227             {
228                 $count++;
229                 extract($category);
230
231                 // Make Nice Variables
232                 $name = $wpdb->escape($cat_libelle_url);
233                 $title = $wpdb->escape(csc ($cat_libelle));
234                 $desc = $wpdb->escape(csc ($cat_desc));
235
236                 if($cinfo = category_exists($name))
237                 {
238                     $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title, 'category_description' => $desc));
239                 }
240                 else
241                 {
242                     $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title, 'category_description' => $desc));
243                 }
244                 $dccat2wpcat[$id] = $ret_id;
245             }
246
247             // Store category translation for future use
248             add_option('dccat2wpcat',$dccat2wpcat);
249             echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> categories imported.'), $count).'<br /><br /></p>';
250             return true;
251         }
252         echo __('No Categories to Import!');
253         return false;
254     }
255
256     function users2wp($users='')
257     {
258         // General Housekeeping
259         global $wpdb;
260         $count = 0;
261         $dcid2wpid = array();
262
263         // Midnight Mojo
264         if(is_array($users))
265         {
266             echo '<p>'.__('Importing Users...').'<br /><br /></p>';
267             foreach($users as $user)
268             {
269                 $count++;
270                 extract($user);
271
272                 // Make Nice Variables
273                 $name = $wpdb->escape(csc ($name));
274                 $RealName = $wpdb->escape(csc ($user_pseudo));
275
276                 if($uinfo = get_userdatabylogin($name))
277                 {
278
279                     $ret_id = wp_insert_user(array(
280                                 'ID'        => $uinfo->ID,
281                                 'user_login'    => $user_id,
282                                 'user_nicename'    => $Realname,
283                                 'user_email'    => $user_email,
284                                 'user_url'    => 'http://',
285                                 'display_name'    => $Realname)
286                                 );
287                 }
288                 else
289                 {
290                     $ret_id = wp_insert_user(array(
291                                 'user_login'    => $user_id,
292                                 'user_nicename'    => csc ($user_pseudo),
293                                 'user_email'    => $user_email,
294                                 'user_url'    => 'http://',
295                                 'display_name'    => $Realname)
296                                 );
297                 }
298                 $dcid2wpid[$user_id] = $ret_id;
299
300                 // Set DotClear-to-WordPress permissions translation
301
302                 // Update Usermeta Data
303                 $user = new WP_User($ret_id);
304                 $wp_perms = $user_level + 1;
305                 if(10 == $wp_perms) { $user->set_role('administrator'); }
306                 else if(== $wp_perms) { $user->set_role('editor'); }
307                 else if(<= $wp_perms) { $user->set_role('editor'); }
308                 else if(<= $wp_perms) { $user->set_role('author'); }
309                 else if(<= $wp_perms) { $user->set_role('contributor'); }
310                 else if(<= $wp_perms) { $user->set_role('contributor'); }
311                 else                     { $user->set_role('subscriber'); }
312
313                 update_usermeta( $ret_id, 'wp_user_level', $wp_perms);
314                 update_usermeta( $ret_id, 'rich_editing', 'false');
315                 update_usermeta( $ret_id, 'first_name', csc ($user_prenom));
316                 update_usermeta( $ret_id, 'last_name', csc ($user_nom));
317             }// End foreach($users as $user)
318
319             // Store id translation array for future use
320             add_option('dcid2wpid',$dcid2wpid);
321
322
323             echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
324             return true;
325         }// End if(is_array($users)
326
327         echo __('No Users to Import!');
328         return false;
329
330     }// End function user2wp()
331
332     function posts2wp($posts='')
333     {
334         // General Housekeeping
335         global $wpdb;
336         $count = 0;
337         $dcposts2wpposts = array();
338         $cats = array();
339
340         // Do the Magic
341         if(is_array($posts))
342         {
343             echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
344             foreach($posts as $post)
345             {
346                 $count++;
347                 extract($post);
348
349                 // Set DotClear-to-WordPress status translation
350                 $stattrans = array(0 => 'draft', 1 => 'publish');
351                 $comment_status_map = array (0 => 'closed', 1 => 'open');
352
353                 //Can we do this more efficiently?
354                 $uinfo = ( get_userdatabylogin( $user_id ) ) ? get_userdatabylogin( $user_id ) : 1;
355                 $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
356
357                 $Title = $wpdb->escape(csc ($post_titre));
358                 $post_content = textconv ($post_content);
359                 $post_excerpt = "";
360                 if ($post_chapo != "") {
361                     $post_excerpt = textconv ($post_chapo);
362                     $post_content = $post_excerpt ."\n<!--more-->\n".$post_content;
363                 }
364                 $post_excerpt = $wpdb->escape ($post_excerpt);
365                 $post_content = $wpdb->escape ($post_content);
366                 $post_status = $stattrans[$post_pub];
367
368                 // Import Post data into WordPress
369
370                 if($pinfo = post_exists($Title,$post_content))
371                 {
372                     $ret_id = wp_insert_post(array(
373                             'ID'            => $pinfo,
374                             'post_author'        => $authorid,
375                             'post_date'        => $post_dt,
376                             'post_date_gmt'        => $post_dt,
377                             'post_modified'        => $post_upddt,
378                             'post_modified_gmt'    => $post_upddt,
379                             'post_title'        => $Title,
380                             'post_content'        => $post_content,
381                             'post_excerpt'        => $post_excerpt,
382                             'post_status'        => $post_status,
383                             'post_name'        => $post_titre_url,
384                             'comment_status'    => $comment_status_map[$post_open_comment],
385                             'ping_status'        => $comment_status_map[$post_open_tb],
386                             'comment_count'        => $post_nb_comment + $post_nb_trackback)
387                             );
388                 }
389                 else
390                 {
391                     $ret_id = wp_insert_post(array(
392                             'post_author'        => $authorid,
393                             'post_date'        => $post_dt,
394                             'post_date_gmt'        => $post_dt,
395                             'post_modified'        => $post_modified_gmt,
396                             'post_modified_gmt'    => $post_modified_gmt,
397                             'post_title'        => $Title,
398                             'post_content'        => $post_content,
399                             'post_excerpt'        => $post_excerpt,
400                             'post_status'        => $post_status,
401                             'post_name'        => $post_titre_url,
402                             'comment_status'    => $comment_status_map[$post_open_comment],
403                             'ping_status'        => $comment_status_map[$post_open_tb],
404                             'comment_count'        => $post_nb_comment + $post_nb_trackback)
405                             );
406                 }
407                 $dcposts2wpposts[$post_id] = $ret_id;
408
409                 // Make Post-to-Category associations
410                 $cats = array();
411                 if($cat1 = get_catbynicename($post_cat_name)) { $cats[1] = $cat1; }
412
413                 if(!empty($cats)) { wp_set_post_cats('', $ret_id, $cats); }
414             }
415         }
416         // Store ID translation for later use
417         add_option('dcposts2wpposts',$dcposts2wpposts);
418
419         echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
420         return true;
421     }
422
423     function comments2wp($comments='')
424     {
425         // General Housekeeping
426         global $wpdb;
427         $count = 0;
428         $dccm2wpcm = array();
429         $postarr = get_option('dcposts2wpposts');
430
431         // Magic Mojo
432         if(is_array($comments))
433         {
434             echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
435             foreach($comments as $comment)
436             {
437                 $count++;
438                 extract($comment);
439
440                 // WordPressify Data
441                 $comment_ID = (int) ltrim($comment_id, '0');
442                 $comment_post_ID = (int) $postarr[$post_id];
443                 $comment_approved = "$comment_pub";
444                 $name = $wpdb->escape(csc ($comment_auteur));
445                 $email = $wpdb->escape($comment_email);
446                 $web = "http://".$wpdb->escape($comment_site);
447                 $message = $wpdb->escape(textconv ($comment_content));
448
449                 if($cinfo = comment_exists($name, $comment_dt))
450                 {
451                     // Update comments
452                     $ret_id = wp_update_comment(array(
453                             'comment_ID'        => $cinfo,
454                             'comment_post_ID'    => $comment_post_ID,
455                             'comment_author'    => $name,
456                             'comment_author_email'    => $email,
457                             'comment_author_url'    => $web,
458                             'comment_author_IP'    => $comment_ip,
459<