OSDN Git Service

ADD/FIX/CHANGE/REMOVE: スキン・テンプレート表示処理の改良
[nucleus-jp/nucleus-next.git] / nucleus / convert / greymatter.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2006 The Nucleus Group
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * (see nucleus/documentation/index.html#license for more info)
11  */
12
13 /**
14  * This script will convert a GreyMatter blog to a Nucleus blog, using
15  * a easy to use wizard.
16  *
17  * Last Modified: August 20, 2002
18  *
19  * Notes:
20  *   - templates are not converted
21  *   - Nucleus should already be installed
22  *
23  * @license http://nucleuscms.org/license.txt GNU General Public License
24  * @copyright Copyright (C) 2002-2006 The Nucleus Group
25  * @version $Id: greymatter.php 1624 2012-01-09 11:36:20Z sakamocchi $
26  */
27
28 include("../../config.php");
29 include("functions.inc.php");
30 include($DIR_LIBS . "ADMIN.php");
31 include($DIR_LIBS . "MEDIA.php");
32
33 if (!$member->isLoggedIn()) {
34         convert_showLogin('greymatter.php');
35 }
36
37 if (!$member->isAdmin()) {
38         convert_doError('Only Super-Admins are allowed to perform blog conversions');
39 }
40
41 $ver = convert_getNucleusVersion();
42 if ($ver > 210)
43         convert_doError("You should check the Nucleus website for updates to this convert tool. This one might not work with your current Nucleus installation.");
44
45 switch($action) {
46         case "assignMembers":
47                 gmc_assignMembers(); break;
48         case "showOverview":
49                 gmc_showOverview(); break;
50         case "doConversion":
51                 gmc_doConversion(); break;
52         case "login": // drop through
53         default:
54                 gmc_askGreyPath();
55 }
56
57 function gmc_askGreyPath() {
58         global $HTTP_SERVER_VARS, $PHP_SELF;
59
60         convert_head();
61
62         // try to guess greymatter path
63         $guess = $HTTP_SERVER_VARS["PATH_TRANSLATED"];
64         $guess = str_replace("/nucleus/convert","",$guess);
65         $guess = str_replace("greymatter.php","",$guess);
66         $guess = str_replace("\\","/",$guess);
67         // add slash at end if necessary
68         if (!endsWithSlash($guess)) $guess .= '/';
69
70         if (file_exists($guess . 'gm-authors.cgi'))
71                 $guess = $guess;
72         else if (file_exists($guess . 'gm/gm-authors.cgi'))
73                 $guess = $guess . 'gm/';
74         else if (file_exists($guess . 'greymatter/gm-authors.cgi'))
75                 $guess = $guess . 'greymatter/';
76         else if (file_exists($guess . '../gm-authors.cgi'))
77                 $guess = $guess . '../';
78         else if (file_exists($guess . '../gm/gm-authors.cgi'))
79                 $guess = $guess . '../gm/';
80         else if (file_exists($guess . '../greymatter/gm-authors.cgi'))
81                 $guess = $guess . '../greymatter/';
82
83
84         ?>
85                 <h1>Before you start</h1>
86
87                 <p>
88                 Before you start converting, make sure that you have created Nucleus Member accounts for all your GreyMatter authors. You'll be asked to assign Nucleus member accounts to GreyMatter authors in the next step.
89                 </p>
90
91                 <p>
92                 If you used {{popup}} tags in GreyMatter to create popup windows with images, make sure you've set the mediadir correctly and that has the correct filepermissions to allow upload. (Nucleus will copy your images to that directory)
93                 </p>
94
95                 <h1>Greymatter Path</h1>
96
97                 <form method="post" action="greymatter.php">
98
99                 <p>
100                 Please enter the path in which the GreyMatter script is installed below. The current value is a guess by Nucleus. The GreyMatter data path will be extracted from the GM config in the next step.
101                 <br />
102                 <b>Note:</b> The path should end with a slash!
103                 </p>
104
105                 <ul>
106                         <li>GreyMatter path: <input name="grey_scriptpath" size="60" value="<?php echo Entity::hsc($guess)?>" /></li>
107                 </ul>
108
109                 <p>
110                 <input type="submit" value="Next Step: Assign Members to Authors" />
111                 <input type="hidden" name="action" value="assignMembers" />
112                 </p>
113
114                 </form>
115         <?php
116         convert_foot();
117 }
118
119 function gmc_assignMembers() {
120         global $HTTP_POST_VARS, $CONF;
121
122         // get data from form
123         $grey_scriptpath = stripslashes($HTTP_POST_VARS['grey_scriptpath']);
124         $grey_scriptpath = str_replace("\\","/",$grey_scriptpath);
125
126         // some checks
127         if (!is_dir($grey_scriptpath))
128                 convert_doError("Given GreyMatter Path does not exist");
129         if (!endsWithSlash($grey_scriptpath))
130                 convert_doError("Given GreyMatter Path does not end with a slash");
131         $grey_configfile = $grey_scriptpath . 'gm-config.cgi';
132         if (!is_readable($grey_configfile))
133                 convert_doError("GreyMatter configfile ($grey_configfile) is not readable. Make sure the path is correct and the file permissions are set correctly so PHP can access it.");
134
135         // get datapath out of configfile
136         $filehandle = fopen($grey_configfile,'r');
137                 $skip = fgets($filehandle,4096);
138                 $grey_datapath = chop(fgets($filehandle,4096)) . '/';
139                 $skip = fgets($filehandle,4096);
140                 $grey_dataurl = chop(fgets($filehandle,4096)) . '/';
141         fclose($filehandle);
142
143         if (!is_dir($grey_datapath))
144                 convert_doError("GreyMatter Data Path extracted from GreyMatter configuration does not exist. Please check your GreyMatter settings.");
145
146         $grey_authorfile = $grey_scriptpath . 'gm-authors.cgi';
147
148         if (!is_readable($grey_authorfile))
149                 convert_doError("GreyMatter authorfile ($grey_authorfile) is not readable. Make sure the path is correct and the file permissions are set correctly so PHP can access it.");
150
151         convert_head();
152
153         ?>
154                 <form method="post" action="greymatter.php">
155
156
157                 <h1>Assign Members to Authors</h1>
158
159                 <p>
160                 Below is a list of all the GreyMatter authors that Nucleus could discover. Please assign a Nucleus Member to all of these authors.
161                 </p>
162
163
164                 <table>
165                 <tr>
166                         <th>GreyMatter Author</th>
167                         <th>Nucleus Member</th>
168                         <th>Blog Admin?</th>
169                 </tr>
170
171                 <?php
172 $filehandle = fopen($grey_authorfile,'r');
173         $idx = 1;
174 while ($author = fgets($filehandle,4096)) {
175         list ($a_name, $a_pwd, $a_email, $a_url, $a_data, $a_entries, $a_entryaccess, $a_entryeditaccess, $a_configureaccess, $templateaccess, $a_authoraccess, $a_rebuildaccess, $a_cplogaccess, $bookmarkletaccess, $uploadaccess, $loginaccess) = explode ("|", $author);
176         ?>
177                 <tr>
178                         <td>
179                                 <b><?php echo $a_name?></b>
180                                 <input name="author[<?php echo $idx?>]" value="<?php echo Entity::hsc($a_name)?>" type="hidden" />
181                         </td>
182                         <td>
183                 <?php                   // TODO: avoid doing this query multiple times
184                         $query =  'SELECT mname as text, mnumber as value FROM '.sql_table('member');
185
186                         $template['name'] = 'memberid[' . $idx . ']';
187                         echo showlist($query,'select',$template);
188                 ?>
189                         </td>
190                         <td>
191                                 <input name="admin[<?php echo $idx?>]" type="checkbox" value="1" id="admin<?php echo $idx?>" <?php                                      if ($a_configureaccess == 'Y') echo "checked='checked'";
192                                 ?>
193                                 /><label for="admin<?php echo $idx?>">Blog Admin</label>
194                         </td>
195                 </tr>
196         <?php   $idx++;
197 } // while
198
199 fclose($filehandle);
200
201                 ?>
202                 <tr>
203                         <td>
204                                 <i>(users not listed in gm-authors.cgi)</i>
205                                 <input name="author[0]" value="_other_" type="hidden" />
206                         </td>
207                         <td>
208                                 <?php                                   // TODO: avoid doing this query multiple times
209                                         $query =  'SELECT mname as text, mnumber as value FROM '.sql_table('member');
210
211                                         $template['name'] = 'memberid[0]';
212                                         echo showlist($query,'select',$template);
213                                 ?>
214                         </td>
215                         <td>
216                                 <input name="admin[0]" type="checkbox" value="1" id="admin0" /><label for="admin0">Blog Admin</label>
217                         </td>
218                 </tr>
219                 </table>
220
221
222                 <h1>Choose Destination Weblog</h1>
223
224                 <p>
225                 There are two options: you can either choose an existing blog to add the greymatter entries into, or you can choose to create a new weblog.
226                 </p>
227
228                 <div>
229                         <input name="createnew" value="0" type="radio" checked='checked' label="createnew_no" /><label for="createnew_no">Choose existing weblog to add to</label>:
230
231                         <?php                                   $query =  'SELECT bname as text, bnumber as value FROM '.sql_table('blog');
232                                         $template['name'] = 'blogid';
233                                         $template['selected'] = $CONF['DefaultBlog'];
234                                         echo showlist($query,'select',$template);
235                         ?>
236                 </div>
237                 <div>
238                         <input name="createnew" value="1" type="radio" id="createnew_yes" /><label for="createnew_yes">Create new weblog</label>
239                         <ul>
240                                 <li>New blog name: <input name="newblogname" /></li>
241                                 <li>Blog owner:
242                                 <?php                                   // TODO: avoid doing this query multiple times
243                                         $query =  'SELECT mname as text, mnumber as value FROM '.sql_table('member');
244
245                                         $template['name'] = 'newowner';
246                                         echo showlist($query,'select',$template);
247                                 ?>
248                                 </li>
249                         </ul>
250                 </div>
251
252                 <h1>Do the conversion!</h1>
253                 <p>
254                 <input type="hidden" name="authorcount" value="<?php echo $idx?>" />
255                 <input type="hidden" name="grey_scriptpath" value="<?php echo Entity::hsc($grey_scriptpath)?>" />
256                 <input type="hidden" name="grey_datapath" value="<?php echo Entity::hsc($grey_datapath)?>" />
257                 <input type="hidden" name="grey_dataurl" value="<?php echo Entity::hsc($grey_dataurl)?>" />
258                 <input type="submit" value="Do the conversion!" />
259                 <input type="hidden" name="action" value="doConversion" />
260                 </p>
261
262                 </form>
263         <?php
264         convert_foot();
265
266 }
267
268 function gmc_doConversion() {
269         global $HTTP_POST_VARS, $manager;
270
271         // 1. get all data
272
273         $authorcount = intval($HTTP_POST_VARS['authorcount']);
274         $author = $HTTP_POST_VARS['author'];
275
276         for ($i=0;$i<$authorcount;$i++) {
277                 $key = $author[$i];
278                 $memberid[$key] = intval($HTTP_POST_VARS['memberid'][$i]);
279                 $isadmin[$key] = intval($HTTP_POST_VARS['admin'][$i]);
280         }
281
282         $grey_scriptpath = stripslashes($HTTP_POST_VARS['grey_scriptpath']);
283         $grey_datapath = stripslashes($HTTP_POST_VARS['grey_datapath']);
284         $grey_dataurl = stripslashes($HTTP_POST_VARS['grey_dataurl']);
285
286         // needed in gm_popup
287         global $grey_datapath;
288
289         $createnew = intval($HTTP_POST_VARS['createnew']);
290         $newblogname = stripslashes($HTTP_POST_VARS['newblogname']);
291         $newowner = intval($HTTP_POST_VARS['newowner']);
292
293         $nucleus_blogid = intval($HTTP_POST_VARS['blogid']);
294
295
296         // 2. check the data
297
298         convert_head();
299
300         ?>
301                 <h1>Converting...</h1>
302
303                 <p>
304                 Please be patient. Don't hit reload! The conversion progress should be showing below.
305                 </p>
306         <?php
307         // try to extend time limit
308         // surpress error messages when not allowed to do so
309         @set_time_limit(1200);
310
311         echo "<pre>";
312
313         echo "Authors: <br/>";
314         for ($i=0;$i<$authorcount;$i++) {
315                 echo  "\tAuthor=" . $author[$i];
316                 echo " ID=" . $memberid[$author[$i]];
317                 echo " ADMIN=" . $isadmin[$author[$i]];
318                 echo "<br />";
319         }
320         echo "Create New Weblog = $createnew (name='$newblogname', owner=$newowner, dest=$nucleus_blogid)<br />";
321
322         echo "</pre>";
323
324         // create blog
325         if ($createnew == 1) {
326                 // choose unique name
327                 $shortname = 'greymatter';
328                 if (Blog::exists($shortname)) {
329                         $idx = 1;
330                         while (Blog::exists($shortname . $idx))
331                                 $idx++;
332                         $shortname = $shortname . $idx;
333                 }
334
335                 $nucleus_blogid = convert_addToBlog($newblogname, $shortname, $newowner);
336                 echo "<pre>New blog created</pre>";
337         }
338
339         // add authors to blog team
340         $blog =& $manager->getBlog($nucleus_blogid);
341         global $catid;
342         $catid = $blog->getDefaultCategory();
343         for ($i=0;$i<$authorcount;$i++)
344                 $blog->addTeamMember($memberid[$author[$i]],$isadmin[$author[$i]]);
345
346         // go through all files
347         $dirhandle = opendir($grey_datapath);
348         while ($filename = readdir($dirhandle)) {
349                 if (ereg("\.cgi$",$filename)) {
350                         gmc_convertOneFile($grey_datapath . $filename, $nucleus_blogid, $memberid);
351                 }
352         }
353         closedir($dirhandle);
354
355         echo "<pre>All done!</pre>";
356
357         convert_foot();
358
359 }
360
361 /**
362   * Converts one GreyMatter entry into a Nucleus Item
363   */
364 function gmc_convertOneFile($filename, $nucleus_blogid, $memberid) {
365         global $catid;
366
367         echo "<pre>";
368
369         $filehandle = fopen($filename,'r');
370
371         $entry_info = fgets($filehandle,4096);  // entry info
372         $entry_karma = fgets($filehandle,4096); // ips of karma voters (ignore)
373         $entry_body = fgets($filehandle,4096);  // body
374         $entry_more = fgets($filehandle,4096);  // extended
375
376         // decode information
377         $entry_info = str_replace("|*|","\n",$entry_info);
378         $entry_body = str_replace("|*|","\n",$entry_body);
379         $entry_more = str_replace("|*|","\n",$entry_more);
380
381         list ($e_number, $e_author, $e_title, $e_wday, $e_mon, $e_mday, $e_year, $e_hour, $e_min, $e_sec, $e_ampm, $e_karmapos, $e_karmaneg, $e_commentcount, $e_allowkarma, $e_allowcomments, $e_openclosed) = explode ("|", $entry_info);
382
383         if (($e_ampm == "PM") && ($e_hour != 12 ))
384                 $e_hour = $e_hour + 12;
385
386         if ($e_allowcomments == "yes")
387                 $e_closed = 0;
388         else
389                 $e_closed = 1;
390
391         $e_timestamp = mktime($e_hour, $e_min, $e_sec, $e_mon, $e_mday, $e_year);
392         $e_timestamp = date("Y-m-d H:i:s",$e_timestamp);
393
394
395
396         $nucl_id = $memberid[$e_author];
397         if (intval($nucl_id) == 0)
398                 $nucl_id = $memberid['_other_'];
399
400         // handle {{link, {{linkmo, {{email and {{emailmo
401         $e_title = gm_parsecommands($e_title,$nucl_id);
402         $entry_body = gm_parsecommands($entry_body,$nucl_id);
403         $entry_more = gm_parsecommands(trim($entry_more),$nucl_id);
404
405         echo "Title: <b>$e_title</b><br />";
406         echo "\tGreyMatter Author = $e_author<br />";
407         echo "\tNucleus Memberid = $nucl_id<br />";
408         echo "\tKarma: $e_karmapos+/$e_karmaneg-<br />";
409
410
411         // add to nucleus_item table
412         $nucleus_itemid = convert_addToItem($e_title, $entry_body, $entry_more, $nucleus_blogid, $nucl_id, $e_timestamp, $e_closed, $catid, $e_karmapos, $e_karmaneg);
413
414
415         echo "\tComments: <br/>";
416
417         while ($comment = fgets($filehandle,4096)) {
418                 // echo "\t\tConverting comment: $comment<br />";
419
420                 // decode
421                 $comment = str_replace("|*|","\n",$comment);
422                 list ($c_author, $c_ip, $c_email, $c_url, $c_wday, $c_mon, $c_mday, $c_year, $c_hour, $c_min, $c_sec, $c_ampm, $c_body) = explode("|",$comment);
423
424                 echo "\t\tConverting comment by <i>$c_author</i><br />";
425
426                 // make hrefs out of links
427                 $c_body = eregi_replace("(http://([a-zA-Z0-9]|\.|/|~|%|&|\?|\@|\=|_|\+|\:|;|-)*)","<a href='\\0'>http://.../</a>",$c_body);
428
429                 // special markup commands
430                 $c_body = gm_parsecommands($c_body,0);
431
432                 if ($c_ampm == "PM")
433                         $c_hour = $c_hour + 12;
434                 $c_timestamp = mktime($c_hour, $c_min, $c_sec, $c_mon, $c_mday, $c_year);
435                 $c_timestamp = date("Y-m-d H:i:s",$c_timestamp);
436
437                 // choose url or email to pass as userid
438                 if (stristr($c_url,'http://') != false)
439                         $c_userid = $c_url;
440                 else
441                         $c_userid = $c_email;
442
443                 // add to comments
444                 convert_addToComments($c_author, $c_userid, $c_body, $nucleus_blogid, $nucleus_itemid, 0, $c_timestamp, $c_ip);
445
446         }
447
448
449         fclose($filehandle);
450
451         echo "</pre>";
452 }
453
454 // returns $text with special greymatter markup commands expanded.
455 function gm_parsecommands($text, $authorid ) {
456         // special markup characters
457         //  **bold text** -> <b>bold text</b>
458         //  __underlined text__ -> <u>underlined text</u>
459         //  \\italic text\\ -> <i>italic text</i>
460         $to_replace = array(
461                 "/\*\*(.*?)\*\*/is",
462                 "/\\\\(.*?)\\\\/is",
463                 "/__(.*?)__/is"
464         );
465         $replace_by = array(
466                 "<b>\\1</b>",
467                 "<i>\\1</i>",
468                 "<u>\\1</u>"
469         );
470         $text = preg_replace($to_replace, $replace_by, $text);
471
472         // {{link url}}
473         // {{link url text}}
474         // {{linkmo url text|mouseover text}}
475         // {{email address}}
476         // {{email address text}}
477         // {{emailmo address text|mouseover text}}
478         // {{popup
479         if (i18n::strpos($text,"{{link") !== FALSE || stristr($text,"{{email") || stristr($text,"{{popup")) {
480                 $to_replace = array(
481                         "/({{linkmo) (http|https|ftp)(:\/\/\S+?) (.+?)(\|)(.+?)(}})/ies",
482                         "/({{link) (http|https|ftp)(:\/\/\S+?)(}})/is",
483                         "/({{link) (http|https|ftp)(:\/\/\S+?) (.+?)(}})/is",
484                         "/({{emailmo) (\S+\@\S+?) (.+?)(\|)(.+?)(}})/ies",
485                         "/({{email) (\S+\@\S+?)(}})/is",
486                         "/({{email) (\S+\@\S+?) (.+?)(}})/is",
487                         "/{{popup (\S+) (.+?) (\d+)x(\d+)}}(.*?)<\/a>/ies"
488                 );
489                 $replace_by = array(
490                         // linkmo
491                         'gm_linkmo("\\2\\3","\\6","\\4")',
492                         // link
493                         "<a href='\\2\\3'>\\2\\3</a>",
494                         "<a href='\\2\\3'>\\4</a>",
495                         // emailmo
496                         'gm_linkmo("mailto:\\2","\\5","\\3")',
497                         // email
498                         "<a href='mailto:\\2'>\\2</a>",
499                         "<a href='mailto:\\2'>\\3</a>",
500                         // popup
501                         'gm_popup("\\1",$authorid,"\\5",\\3,\\4)'
502                 );
503                 $text = preg_replace($to_replace, $replace_by, $text);
504         }
505
506         // newlines (greymatter renders newlines as linebreaks)
507         $text = ereg_replace("\n","<br />",$text); // newlines
508
509         return $text;
510 }
511
512 // makes sure quotes are escaped in javascript strings
513 // php seems to quote the arguments as the get passed to this method by preg_replace
514 // with /e modifier
515 function gm_linkmo($url, $mouseover, $text) {
516         // remove slashes from text (not needed there)
517         $text = stripslashes($text);
518
519         // replace " by \' in mouseover (to avoid error)
520         $mouseover = str_replace('"',"\'",$mouseover);
521
522         return '<a href="' . $url . '" onmouseover="window.status=\'' . $mouseover .  '\';return true" onmouseout="window.status=\'\';return true">'.$text.'</a>';
523 }
524
525 // converts GM {{popup command into a link to the image
526 function gm_popup($filename, $authorid, $text, $width, $height) {
527         global $grey_datapath, $manager;
528
529         $res = Media::addMediaObject($manager->getMember($authorid), "$grey_datapath$filename", $filename);
530
531         if ($res != "")
532                 die("error copying media files: $res");
533
534         // TODO: copy file to media directory
535         // TODO: create %popup(...)% code instead
536
537         $text = Entity::hsc(stripslashes($text));
538
539         return "<%popup($filename|$width|$height|$text)%>";
540 }
541
542
543
544 ?>