<?php
/*-----------------------------------------------------
*                本程序为zchain.net提供
*            零点留言簿多用户版 ZGB version 3.00
* ----------------------------------------------------*/
require "setup.php";
require "core/class_basic_record_file.php";
require "core/class_book_list.php";
require "core/class_message_list.php";

if($action == "upgrade"){
 upgrade($f_srcpath, $f_despath);
 exit;
}

// prompt to input source and destination file path
?>

<HTML><HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>零点留言本v3.0升级程序</title></HEAD>
<BODY>
注意：<br>
1、此程序用于将零点留言本旧版本(v2.5及以前)数据转换为新版本数据；<br>
2、旧版本与新版本请不要使用相同数据目录，以避免数据冲突；<br>
3、此版本为测试版，转换完成后，请保留就系统与数据，作为备份；<br>
4、转换后数据将添加到新版本数据中，如果要再次转换，请先清除以避免重复。<br>
请输入旧版本与新版本的数据目录($filepath):<BR>
<form method=post action='upgrade.php'>
<input type=hidden name=action value="upgrade">
<table cellspacing=1 bgcolor=#000000 cellpadding=3>
<tr><td width=100% bgcolor=#F0F0F0 align=right style="font-size:9pt;">
旧版本数据目录：<input type=input name=f_srcpath size=20><br>
新版本数据目录：<input type=input name=f_despath size=20><br>
<input type=submit value="开始转换">
</td></tr></table></form>
</BODY></HTML>

<?php
function upgrade($srcpath, $despath) {
 global $filepath;
 
 $filepath=$despath;

 print "开始转换:<br>";
 print "旧版本数据目录：[$srcpath]<br>";
 print "新版本数据目录：[$despath]<br><br>";

 //transfer books
 $oBooks=new CBookList;
 $oBooks->checkSystem();
 $oBooks->open();
 $lines=file("$srcpath/data/list");
 foreach($lines as $line) {
  $line=trim($line);
  $tmpLines=file("$srcpath/list/$line.cgi");
  $tmpLine=trim($tmpLines[0]);
  list($nouse,$pass,$email,$url,$title,$urlname,$regtime,$htmlt,$htmlb,$desc)
    = explode("\t",$tmpLine);

  $htmlt=str_replace("\r","",$htmlt);
  $htmlt=str_replace("\n","",$htmlt);
  $htmlb=str_replace("\r","",$htmlb);
  $htmlb=str_replace("\n","",$htmlb);
  $desc =str_replace("\r","",$desc);
  $desc =str_replace("\n","",$desc);
 
  $bid=$oBooks->appendNew();
  $oBooks->name=$line; $oBooks->pass=$pass; $oBooks->title=$title;
  $oBooks->email=$email;
  $oBooks->url=$url;  $oBooks->urlname=$urlname; $oBooks->regtime=$regtime;
  $oBooks->htmlt=$htmlt;
  $oBooks->htmlb=$htmlb;
  $oBooks->desc=$desc;
  $oBooks->update();
  print "留言本资料 [$line] 已经处理！<br>";
 }
 $oBooks->close();
 print "<br>所有留言本资料转换完毕！<br><br>";
  
 //for each book, transfer messages
 $oMsgs=new CMessageList;
 foreach($lines as $line) {
  $line=trim($line); if(!$line)continue;
  $oMsgs->create($line);
  $oMsgs->open($line);

  $msgLines=file("$srcpath/data/$line/$line.cgi");
  $msgLines=array_reverse($msgLines);
  foreach($msgLines as $msgLine){
   $msgLine=trim($msgLine);
   $tmpLines=file("$srcpath/data/$line/$msgLine");
   $tmpLine=trim($tmpLines[0]);
   list($user,$email,$url,$comment,$ip,$time,$secret,$reply,$replysecret,$icon)
     = explode("\t",$tmpLine);

   $comment=str_replace("\r","",$comment);
   $comment=str_replace("\n","",$comment);
   $reply=str_replace("\r","",$reply);
   $reply=str_replace("\n","",$reply);

   $oMsgs->appendNew();
   $oMsgs->user=$user; $oMsgs->email=$email; $oMsgs->icon=$icon;
   $oMsgs->url=(trim($url)!="http://") ? $url : "";
   $oMsgs->comment=$comment; $oMsgs->ip=$ip; $oMsgs->time=$time;
   $oMsgs->secret=$secret; $oMsgs->reply=$reply; $oMsgs->replysecret=$replysecret;
   $oMsgs->update();
  }
  $oMsgs->close();
  $size=count($msgLines);
  print "留言本 [$line] 中的留言消息 ($size 条) 已经处理！<br>";
 }

 print "<br>数据转换完毕！";
}