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

Revision 5406, 10.3 kB (checked in by ryan, 2 years ago)

Add nonces to importers

  • Property svn:eol-style set to native
Line 
1 <?php
2
3 class GM_Import {
4
5     var $gmnames = array ();
6
7     function header() {
8         echo '<div class="wrap">';
9         echo '<h2>'.__('Import GreyMatter').'</h2>';
10     }
11
12     function footer() {
13         echo '</div>';
14     }
15
16     function greet() {
17         $this->header();
18 ?>
19 <p><?php _e('This is a basic GreyMatter to WordPress import script.') ?></p>
20 <p><?php _e('What it does:') ?></p>
21 <ul>
22 <li><?php _e('Parses gm-authors.cgi to import (new) authors. Everyone is imported at level 1.') ?></li>
23 <li><?php _e('Parses the entries cgi files to import posts, comments, and karma on posts (although karma is not used on WordPress yet).<br />If authors are found not to be in gm-authors.cgi, imports them at level 0.') ?></li>
24 <li><?php _e("Detects duplicate entries or comments. If you don't import everything the first time, or this import should fail in the middle, duplicate entries will not be made when you try again.") ?></li>
25 </ul>
26 <p><?php _e('What it does not:') ?></p>
27 <ul>
28 <li><?php _e('Parse gm-counter.cgi, gm-banlist.cgi, gm-cplog.cgi (you can make a CP log hack if you really feel like it, but I question the need of a CP log).') ?></li>
29 <li><?php _e('Import gm-templates.') ?></li>
30 <li><?php _e("Doesn't keep entries on top.")?></li>
31 </ul>
32 <p>&nbsp;</p>
33
34 <form name="stepOne" method="get">
35 <input type="hidden" name="import" value="greymatter" />
36 <input type="hidden" name="step" value="1" />
37 <?php wp_nonce_field('import-greymatter'); ?>
38 <h3><?php _e('Second step: GreyMatter details:') ?></h3>
39 <p><table cellpadding="0">
40 <tr>
41 <td><?php _e('Path to GM files:') ?></td>
42 <td><input type="text" style="width:300px" name="gmpath" value="/home/my/site/cgi-bin/greymatter/" /></td>
43 </tr>
44 <tr>
45 <td><?php _e('Path to GM entries:') ?></td>
46 <td><input type="text" style="width:300px" name="archivespath" value="/home/my/site/cgi-bin/greymatter/archives/" /></td>
47 </tr>
48 <tr>
49 <td colspan="2"><br /><?php _e("This importer will search for files 00000001.cgi to 000-whatever.cgi,<br />so you need to enter the number of the last GM post here.<br />(if you don't know that number, just log into your FTP and look it out<br />in the entries' folder)") ?></td>
50 </tr>
51 <tr>
52 <td><?php _e("Last entry's number:") ?></td>
53 <td><input type="text" name="lastentry" value="00000001" /></td>
54 </tr>
55 </table>
56 </p>
57 <p><?php _e("When you're ready, click OK to start importing: ") ?><input type="submit" name="submit" value="<?php _e('OK') ?>" class="search" /></p>
58 </form>
59 <p>&nbsp</p>
60 <?php
61         $this->footer();
62     }
63
64
65
66     function gm2autobr($string) { // transforms GM's |*| into b2's <br />\n
67         $string = str_replace("|*|","<br />\n",$string);
68         return($string);
69     }
70
71     function import() {
72         global $wpdb;
73
74         $wpvarstoreset = array('gmpath', 'archivespath', 'lastentry');
75         for ($i=0; $i<count($wpvarstoreset); $i += 1) {
76             $wpvar = $wpvarstoreset[$i];
77             if (!isset($$wpvar)) {
78                 if (empty($_POST["$wpvar"])) {
79                     if (empty($_GET["$wpvar"])) {
80                         $$wpvar = '';
81                     } else {
82                         $$wpvar = $_GET["$wpvar"];
83                     }
84                 } else {
85                     $$wpvar = $_POST["$wpvar"];
86                 }
87             }
88         }
89
90         if (!chdir($archivespath))
91             wp_die(__("Wrong path, the path to the GM entries does not exist on the server"));
92
93         if (!chdir($gmpath))
94             wp_die(__("Wrong path, the path to the GM files does not exist on the server"));
95
96         $lastentry = (int) $lastentry;
97
98         $this->header();
99 ?>
100 <p><?php _e('The importer is running...') ?></p>
101 <ul>
102 <li><?php _e('importing users...') ?><ul><?php
103
104     chdir($gmpath);
105     $userbase = file("gm-authors.cgi");
106
107     foreach($userbase as $user) {
108         $userdata=explode("|", $user);
109
110         $user_ip="127.0.0.1";
111         $user_domain="localhost";
112         $user_browser="server";
113
114         $s=$userdata[4];
115         $user_joindate=substr($s,6,4)."-".substr($s,0,2)."-".substr($s,3,2)." 00:00:00";
116
117         $user_login=$wpdb->escape($userdata[0]);
118         $pass1=$wpdb->escape($userdata[1]);
119         $user_nickname=$wpdb->escape($userdata[0]);
120         $user_email=$wpdb->escape($userdata[2]);
121         $user_url=$wpdb->escape($userdata[3]);
122         $user_joindate=$wpdb->escape($user_joindate);
123
124         $user_id = username_exists($user_login);
125         if ($user_id) {
126             printf('<li>'.__('user %s').'<strong>'.__('Already exists').'</strong></li>', "<em>$user_login</em>");
127             $this->gmnames[$userdata[0]] = $user_id;
128             continue;
129         }
130
131         $user_info = array("user_login"=>"$user_login", "user_pass"=>"$pass1", "user_nickname"=>"$user_nickname", "user_email"=>"$user_email", "user_url"=>"$user_url", "user_ip"=>"$user_ip", "user_domain"=>"$user_domain", "user_browser"=>"$user_browser", "dateYMDhour"=>"$user_joindate", "user_level"=>"1", "user_idmode"=>"nickname");
132         $user_id = wp_insert_user($user_info);
133         $this->gmnames[$userdata[0]] = $user_id;
134
135         printf('<li>'.__('user %s...').' <strong>'.__('Done').'</strong></li>', "<em>$user_login</em>");
136     }
137
138 ?></ul><strong><?php _e('Done') ?></strong></li>
139 <li><?php _e('importing posts, comments, and karma...') ?><br /><ul><?php
140
141     chdir($archivespath);
142
143     for($i = 0; $i <= $lastentry; $i = $i + 1) {
144
145         $entryfile = "";
146
147         if ($i<10000000) {
148             $entryfile .= "0";
149             if ($i<1000000) {
150                 $entryfile .= "0";
151                 if ($i<100000) {
152                     $entryfile .= "0";
153                     if ($i<10000) {
154                         $entryfile .= "0";
155                         if ($i<1000) {
156                             $entryfile .= "0";
157                             if ($i<100) {
158                                 $entryfile .= "0";
159                                 if ($i<10) {
160                                     $entryfile .= "0";
161         }}}}}}}
162
163         $entryfile .= "$i";
164
165         if (is_file($entryfile.".cgi")) {
166
167             $entry=file($entryfile.".cgi");
168             $postinfo=explode("|",$entry[0]);
169             $postmaincontent=$this->gm2autobr($entry[2]);
170             $postmorecontent=$this->gm2autobr($entry[3]);
171
172             $post_author=trim($wpdb->escape($postinfo[1]));
173
174             $post_title=$this->gm2autobr($postinfo[2]);
175             printf('<li>'.__('entry # %s : %s : by %s'), $entryfile, $post_title, $postinfo[1]);
176             $post_title=$wpdb->escape($post_title);
177
178             $postyear=$postinfo[6];
179             $postmonth=zeroise($postinfo[4],2);
180             $postday=zeroise($postinfo[5],2);
181             $posthour=zeroise($postinfo[7],2);
182             $postminute=zeroise($postinfo[8],2);
183             $postsecond=zeroise($postinfo[9],2);
184
185             if (($postinfo[10]=="PM") && ($posthour!="12"))
186                 $posthour=$posthour+12;
187
188             $post_date="$postyear-$postmonth-$postday $posthour:$postminute:$postsecond";
189
190             $post_content=$postmaincontent;
191             if (strlen($postmorecontent)>3)
192                 $post_content .= "<!--more--><br /><br />".$postmorecontent;
193             $post_content=$wpdb->escape($post_content);
194
195             $post_karma=$postinfo[12];
196
197             $post_status = 'publish'; //in greymatter, there are no drafts
198             $comment_status = 'open';
199             $ping_status = 'closed';
200
201             if ($post_ID = post_exists($post_title, '', $post_date)) {
202                 echo ' ';
203                 _e('(already exists)');
204             } else {
205                 //just so that if a post already exists, new users are not created by checkauthor
206                 // we'll check the author is registered, or if it's a deleted author
207                 $user_id = username_exists($post_author);
208                 if (!$user_id) {    // if deleted from GM, we register the author as a level 0 user
209                     $user_ip="127.0.0.1";
210                     $user_domain="localhost";
211                     $user_browser="server";
212                     $user_joindate="1979-06-06 00:41:00";
213                     $user_login=$wpdb->escape($post_author);
214                     $pass1=$wpdb->escape("password");
215                     $user_nickname=$wpdb->escape($post_author);
216                     $user_email=$wpdb->escape("user@deleted.com");
217                     $user_url=$wpdb->escape("");
218                     $user_joindate=$wpdb->escape($user_joindate);
219
220                     $user_info = array("user_login"=>$user_login, "user_pass"=>$pass1, "user_nickname"=>$user_nickname, "user_email"=>$user_email, "user_url"=>$user_url, "user_ip"=>$user_ip, "user_domain"=>$user_domain, "user_browser"=>$user_browser, "dateYMDhour"=>$user_joindate, "user_level"=>0, "user_idmode"=>"nickname");
221                     $user_id = wp_insert_user($user_info);
222                     $this->gmnames[$postinfo[1]] = $user_id;
223
224                     echo ': ';
225                     printf(__('registered deleted user %s at level 0 '), "<em>$user_login</em>");
226                 }
227
228                 if (array_key_exists($postinfo[1], $this->gmnames)) {
229                     $post_author = $this->gmnames[$postinfo[1]];
230                 } else {
231                     $post_author = $user_id;
232                 }
233
234                 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt');
235                 $post_ID = wp_insert_post($postdata);
236             }
237
238             $c=count($entry);
239             if ($c>4) {
240                 $numAddedComments = 0;
241                 $numComments = 0;
242                 for ($j=4;$j<$c;$j++) {
243                     $entry[$j]=$this->gm2autobr($entry[$j]);
244                     $commentinfo=explode("|",$entry[$j]);
245                     $comment_post_ID=$post_ID;
246                     $comment_author=$wpdb->escape($commentinfo[0]);
247                     $comment_author_email=$wpdb->escape($commentinfo[2]);
248                     $comment_author_url=$wpdb->escape($commentinfo[3]);
249                     $comment_author_IP=$wpdb->escape($commentinfo[1]);
250
251                     $commentyear=$commentinfo[7];
252                     $commentmonth=zeroise($commentinfo[5],2);
253                     $commentday=zeroise($commentinfo[6],2);
254                     $commenthour=zeroise($commentinfo[8],2);
255                     $commentminute=zeroise($commentinfo[9],2);
256                     $commentsecond=zeroise($commentinfo[10],2);
257                     if (($commentinfo[11]=="PM") && ($commenthour!="12"))
258                         $commenthour=$commenthour+12;
259                     $comment_date="$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond";
260
261                     $comment_content=$wpdb->escape($commentinfo[12]);
262
263                     if (!comment_exists($comment_author, $comment_date)) {
264                         $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_approved');
265                         $commentdata = wp_filter_comment($commentdata);
266                         wp_insert_comment($commentdata);
267                         $numAddedComments++;
268                     }
269                     $numComments++;
270                 }
271                 if ($numAddedComments > 0) {
272                     echo ': ';
273                     printf(__('imported %d comment(s)'), $numAddedComments);
274                 }
275                 $preExisting = $numComments - numAddedComments;
276                 if ($preExisting > 0) {
277                     echo ' ';
278                     printf(__('ignored %d pre-existing comments'), $preExisting);
279                 }
280             }
281             echo '... <strong>'.__('Done').'</strong></li>';
282         }
283     }
284     ?>
285 </ul><strong><?php _e('Done') ?></strong></li></ul>
286 <p>&nbsp;</p>
287 <p><?php _e('Completed GreyMatter import!') ?></p>
288 <?php
289     $this->footer();
290     }
291
292     function dispatch() {
293         if (empty ($_GET['step']))
294             $step = 0;
295         else
296             $step = (int) $_GET['step'];
297
298         switch ($step) {
299             case 0 :
300                 $this->greet();
301                 break;
302             case 1:
303                 check_admin_referer('import-greymatter');
304                 $this->import();
305                 break;
306         }
307     }
308
309     function GM_Import() {
310         // Nothing.
311     }
312 }
313
314 $gm_import = new GM_Import();
315
316 register_importer('greymatter', __('GreyMatter'), __('Import users, posts, and comments from a Greymatter blog'), array ($gm_import, 'dispatch'));
317 ?>
318
Note: See TracBrowser for help on using the browser.