root/wp2tt/wp2ttxml.php @ 9

Revision 4, 12.9 KB (checked in by lacovnk, 4 years ago)

wp2tt 기존 파일 추가

Line 
1<?php
2error_reporting(E_ALL);
3session_start();
4require_once('lib.php');
5require_once('ttxmllib.php');
6if(isset($_POST['stage'])){$stage = $_POST['stage'];}
7else{$stage = 0;}
8?>
9<?php if($stage == 0)
10{
11?>
12<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
13<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
14<head>
15    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16    <title>wordpress -> tattertools</title>
17</head>
18<body>
19<form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
20<div>
21<input type="hidden" name="stage" value="1" />
22<h1>Wordpress Database Setting</h1>
23<table>
24<tr><td>database host</td><td><input type="text" name="db[host]" value="localhost" /></td></tr>
25<tr><td>database name</td><td><input type="text" name="db[db]" /></td></tr>
26<tr><td>database user</td><td><input type="text" name="db[user]" /></td></tr>
27<tr><td>database password</td><td><input type="password" name="db[password]" /></td></tr>
28<tr><td>database prefix</td><td><input type="text" name="db[prefix]" value="wp_" /></td></tr>
29<tr><td>encoding</td><td><input type="text" name="db[encoding]" value="utf8" /></td></tr>
30</table>
31<input type="submit" value="Go Stage 2" />
32</div>
33</form>
34</body>
35</html>
36<?php
37}
38else if($stage == 1)
39{
40?>
41<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
42<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
43<head>
44    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
45    <title>wordpress -> tattertools</title>
46</head>
47<body>
48<h1>Check Setting</h1>
49<form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
50<input type="hidden" name="stage" value="2" />
51<?php
52$db = $_POST['db'];
53
54if(!($wpdb = dbconnect($db)))
55{
56    print '<h2>wordpress database connect error</h2>';
57    $result = false;
58}
59else
60{
61$config['attach_base_url'] = addslashes(fetch_query("SELECT option_value FROM {$db['prefix']}options WHERE option_name='siteurl';")."/wp_content/");
62$config['attach_base_path'] = addslashes(fetch_query("SELECT option_value FROM {$db['prefix']}options WHERE option_name='fileupload_realpath';"));
63if(!$config['attach_base_path']){$config['attach_base_path'] = getcwd()."/wp-content/";}
64
65$array_user = array();
66$res_user = query("SELECT * FROM {$db['prefix']}users;");
67while($cur_user = mysql_fetch_assoc($res_user))
68{
69    $array_user[$cur_user['ID']] = $cur_user['user_login'];
70}
71
72$array_static = array();
73$res_static = query("SELECT * FROM {$db['prefix']}posts WHERE post_status='static';");
74while($cur_static = mysql_fetch_assoc($res_static))
75{
76    $array_static[$cur_static['ID']] = $cur_static['post_title'];
77}
78?>
79<h2>Select</h2>
80<table>
81<tr><td><td>tag</td><td><?php htmlInputRadio("config[select][tag]",array("1"=>"yes","0"=>"no"),1);?></td></tr>
82<tr><td><td>comment</td><td><?php htmlInputRadio("config[select][comment]",array("1"=>"yes","0"=>"no"),1);?></td></tr>
83<tr><td><td>trackback</td><td><?php htmlInputRadio("config[select][trackback]",array("1"=>"yes","0"=>"no"),1);?></td></tr>
84<tr><td><td>attachment</td><td><?php htmlInputRadio("config[select][attachment]",array("1"=>"yes","0"=>"no"),1);?></td></tr>
85<tr><td><td>category</td><td><?php htmlInputRadio("config[select][category]",array("1"=>"yes","0"=>"no"),1);?></td></tr>
86<tr><td><td>guestbook</td><td><?php htmlInputRadio("config[select][guestbook]",array("1"=>"yes","0"=>"no"),1);?></td></tr>
87</table>
88<h2>Settings</h2>
89<table>
90<tr><td>user ID</td><td><?php htmlInputSelect('config[userId]',$array_user); ?></td></tr>
91<tr><td>guestbook</td><td><?php htmlInputSelect('config[guestbookId]',$array_static);?></td></tr>
92<tr><td>attach base url</td><td><input type="text" name="config[attach_base_url]" value="<?=$config['attach_base_url']?>" /></td></tr>
93<tr><td>attach base path</td><td><input type="text" name="config[attach_base_path]" value="<?=$config['attach_base_path']?>" /></td></tr>
94</table>
95<h2>wordpress categories setting</h2>
96If two categories' weight values are same, only random-selected one of them will be used.<br />
97<table>
98<thead><tr><td>no</td><td>Name</td><td>weight</td></tr></thead>
99<tbody>
100<?php
101$res = query("SELECT * FROM wp_categories");
102$cat_num = mysql_num_rows($res);
103$categories = array();
104while($cur_category = mysql_fetch_assoc($res))
105{
106    print "<tr><td>{$cur_category['cat_ID']}</td><td>{$cur_category['cat_name']}</td><td>";
107    htmlInputSelect("config[weight][{$cur_category['cat_ID']}]",range(0,$cat_num-1));
108    print "</td></tr>";
109}
110$_SESSION['config'] = $config;
111$_SESSION['db'] = $db;
112?>
113</tbody>
114</table>
115<input type="submit" value="Go Stage 3" />
116</form>
117<?php
118}
119?>
120</body>
121</html>
122<?php
123}
124else if($stage == 2)
125{
126?>
127<?php
128$outputFileName = "wp2ttxml.".date("Y-m-d_H-m-s").".xml";
129$config = array_merge($_SESSION['config'],$_POST['config']);
130$db = $_SESSION['db'];
131$wpdb = dbconnect($db);
132$config['attach_base_path'] = stripslashes($config['attach_base_path']);
133$config['attach_base_url'] = stripslashes($config['attach_base_url']);
134
135header('Content-Type: text/xml');
136header("Content-Disposition: attachment; filename=\"{$outputFileName}\"");
137print '<?xml version="1.0" encoding="utf-8" ?>';
138print '<blog type="tattertools/1.0" migrational="false">';
139
140/* Categories */
141if($config['select']['category'])
142{
143    $res = query("SELECT * FROM {$db['prefix']}categories");
144    $ttCategories = new TTCategories();
145    while($cur_category = mysql_fetch_assoc($res))
146    {
147        $ttCategory = new TTCategory();
148        $ttCategory->name = $cur_category['cat_name'];
149        $ttCategory->parentId = ($cur_category['category_parent'] == 0)?NULL:$cur_category['category_parent'];
150
151        $ttCategories->add($ttCategory,$cur_category['cat_ID']);
152    }
153    $ttCategories->printXML();
154
155    asort($config['weight']);
156}
157
158$attachmentCount = 0;
159
160/* Entries */
161$res = query("SELECT * FROM {$db['prefix']}posts WHERE post_status IN ('publish','private','draft');");
162while($cur_post = mysql_fetch_assoc($res))
163{
164    /* for visibility */
165    if($cur_post['post_status'] == 'publish'){$cur_post['post_status'] = 'public';}
166    else if($cur_post['post_status'] == 'private' || $cur_post['post_status'] == 'draft'){$cur_post['post_status'] = 'private';}
167
168    /* for category */
169    $cur_category = "";
170    if($config['select']['category'])
171    {
172        $cur_weight = -1;
173        $cur_cat_id = 0;
174        $res_post2cat = query("SELECT * FROM {$db['prefix']}post2cat WHERE post_id='{$cur_post['ID']}'");
175        while($cur_post2cat = mysql_fetch_assoc($res_post2cat))
176        {
177            if($cur_weight < $config['weight'][$cur_post2cat['category_id']]){$cur_cat_id = $cur_post2cat['category_id'];}
178        }
179        $cur_category = $ttCategories->getPath($cur_cat_id);
180    }
181
182    /* for comment */
183    $ttComments = new TTComments();
184    if($config['select']['comment'])
185    {
186        $res_comment = query("SELECT * FROM {$db['prefix']}comments WHERE comment_approved='1' AND comment_type='' AND comment_post_ID='{$cur_post['ID']}';");
187        while($cur_comment = mysql_fetch_assoc($res_comment))
188        {
189            if($cur_comment['user_id'] != $config['userId']){$cur_comment['user_id'] = NULL;}
190            if(isset($cur_comment['comment_reply_ID']))
191            {
192                if(0 == $cur_comment['comment_reply_ID']){$cur_comment['comment_reply_ID'] == NULL;}
193            }
194            else{$cur_comment['comment_reply_ID'] = NULL;}
195
196            $ttComment = new TTComment();
197            $ttCommenter = new TTCommenter();
198            $ttComment->id = $cur_comment['comment_ID'];
199            $ttCommenter->id = $cur_comment['user_id'];
200            $ttCommenter->name = $cur_comment['comment_author'];
201            $ttCommenter->homepage = $cur_comment['comment_author_url'];
202            $ttCommenter->ip = $cur_comment['comment_author_IP'];
203            $ttComment->commenter = $ttCommenter;
204            $ttComment->content = htmlspecialchars($cur_comment['comment_content']);
205            $ttComment->written = strtotime($cur_comment['comment_date']);
206            $ttComment->parentId = $cur_comment['comment_reply_ID'];
207            $ttComments->add($ttComment,$cur_comment['comment_ID']);
208        }
209    }
210
211    /* for trackbacks */
212    $cur_trackbacks = array();
213    if($config['select']['trackback'])
214    {
215        $res_trackback = query("SELECT * FROM {$db['prefix']}comments WHERE comment_approved='1' AND comment_type='trackback' AND comment_post_ID='{$cur_post['ID']}';");
216        while($cur_trackback = mysql_fetch_assoc($res_trackback))
217        {
218            $cur_trackback['title'] = explode("\n",$cur_trackback['comment_content']);
219            if($cur_trackback['title'][0][0] != "<")
220            {
221                $cur_trackback['title'] = 'no title';
222            }
223            else
224            {
225                $cur_trackback['title'] = trim(str_replace(array("<strong>","</strong>"),"",$cur_trackback['title'][0]));
226                $cur_trackback['comment_content'] = substr($cur_trackback['comment_content'],strpos($cur_trackback['comment_content'],"\n",1)+2);
227            }
228
229            $ttTrackback = new TTTrackback();
230            $ttTrackback->url = $cur_trackback['comment_author_url'];
231            $ttTrackback->site = htmlspecialchars($cur_trackback['comment_author']);
232            $ttTrackback->title= htmlspecialchars($cur_trackback['title']);
233            $ttTrackback->excerpt = htmlspecialchars($cur_trackback['comment_content']);
234            $ttTrackback->ip = $cur_trackback['comment_author_IP'];
235            $ttTrackback->received = strtotime($cur_trackback['comment_date']);
236            array_push($cur_trackbacks,$ttTrackback);
237        }
238    }
239
240    /* for attachment */
241    $cur_attachments = array();
242    if($config['select']['attachment'])
243    {
244        preg_match_all("/<img ([^>]+)\/>/",$cur_post['post_content'],$matches);
245        for($i=0;$i<sizeof($matches[1]);$i++)
246        {
247            $cur_match = $matches[1][$i];
248            $cur_attr = htmlAttributeArray($cur_match);
249            $cur_attach = str_replace("{$config['attach_base_url']}","",$cur_attr['src']);
250            $cur_attach_decoded = urldecode($cur_attach);
251            if(strrpos($cur_attach,"/")){$filename = substr($cur_attach,strrpos($cur_attach,"/")+1);}
252            else{$filename = $cur_attach;}
253            $filename = urldecode($filename);
254            $fileext = substr($filename,strrpos($filename,".")+1);
255            $newfilename = "{$attachmentCount}.{$fileext}";
256
257            $fp = fopen("{$config['attach_base_path']}/{$cur_attach_decoded}","r");
258            $fstat = fstat($fp);
259            $img = getimagesize("{$config['attach_base_path']}/{$cur_attach_decoded}");
260
261            $ttAttachment = new TTAttachment();
262            $ttAttachment->mime = $img['mime'];
263            $ttAttachment->size = $fstat['size'];
264            $ttAttachment->width = $img[0];
265            $ttAttachment->height = $img[1];
266            $ttAttachment->name = $newfilename;
267            $ttAttachment->label = $filename;
268            $ttAttachment->attached = $fstat['ctime'];
269            $ttAttachment->content = base64_encode(fread($fp,$ttAttachment->size));
270            array_push($cur_attachments,$ttAttachment);
271
272            $cur_post['post_content'] = str_replace($cur_attr['src'],"[##_ATTACH_PATH_##]/{$newfilename}",$cur_post['post_content']);
273            $attachmentCount++;
274            fclose($fp);
275        }
276    }
277
278    /* for tag */
279    $cur_tags = array();
280    if($config['select']['tag'])
281    {
282        $res_tag = query("SELECT * FROM {$db['prefix']}postmeta WHERE meta_key='keywords' AND post_id='{$cur_post['ID']}';");
283        $cur_tags = mysql_fetch_assoc($res_tag);
284        $cur_tags = explode(",",$cur_tags['meta_value']);
285        array_walk($cur_tags,'trim');
286    }
287
288    $ttPost = new TTPost();
289    $ttPost->slogan = urldecode($cur_post['post_name']);
290    $ttPost->id = $cur_post['ID'];
291    $ttPost->visibility = $cur_post['post_status'];
292    $ttPost->title = htmlspecialchars($cur_post['post_title']);
293    $ttPost->content = htmlspecialchars($cur_post['post_content']);
294    $ttPost->acceptComment = ($cur_post['comment_status'] == 'open')?1:0;
295    $ttPost->acceptTrackback = ($cur_post['comment_status'] == 'open')?1:0;
296    $ttPost->published = strtotime($cur_post['post_date']);
297    $ttPost->created = strtotime($cur_post['post_date']);
298    $ttPost->modified = strtotime($cur_post['post_modified']);
299    $ttPost->category = $cur_category;
300    $ttPost->tag = $cur_tags;
301    $ttPost->comments = $ttComments;
302    $ttPost->trackbacks = $cur_trackbacks;
303    $ttPost->attachments = $cur_attachments;
304
305    $ttPost->printXML();
306}
307/* Guestbook */
308
309// TODO refactoring.. duplicate code :(
310$ttGuestbook = new TTGuestbook();
311$res_comment = query("SELECT * FROM {$db['prefix']}comments WHERE comment_approved='1' AND comment_type='' AND comment_post_ID='{$config['guestbookId']}';");
312while($cur_comment = mysql_fetch_assoc($res_comment))
313{
314    if($cur_comment['user_id'] != $config['userId']){$cur_comment['user_id'] = NULL;}
315    $ttComment = new TTComment();
316    $ttCommenter = new TTCommenter();
317    $ttComment->id = $cur_comment['comment_ID'];
318    $ttCommenter->id = $cur_comment['user_id'];
319    $ttCommenter->email = $cur_comment['comment_author_email'];
320    $ttCommenter->name = $cur_comment['comment_author'];
321    $ttCommenter->homepage = $cur_comment['comment_author_url'];
322    $ttCommenter->ip = $cur_comment['comment_author_IP'];
323    $ttComment->commenter = $ttCommenter;
324    $ttComment->content = htmlspecialchars($cur_comment['comment_content']);
325    $ttComment->written = strtotime($cur_comment['comment_date']);
326    $ttComment->parentId = ($cur_comment['comment_reply_ID'] == 0)?NULL:$cur_comment['comment_reply_ID'];
327    $ttGuestbook->add($ttComment,$cur_comment['comment_ID']);
328}
329$ttGuestbook->printXML();
330
331print '</blog>';
332?>
333<?php
334}
335else
336{
337?>
338<html>
339<head>
340    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
341    <title>wordpress -> tattertools</title>
342</head>
343<body>
344<h1>Wrong Access</h1>
345Go <a href="<?=$_SEVER['PHP_SELF']?>">Stage1</a>
346</body>
347</html>
348<?php
349}
350?>
Note: See TracBrowser for help on using the browser.