<?php
/*
分享表：pre_home_share
*/
if($start <= 1) truncatetable('home_share');

$maxsid = $xc->db['source']->result($xc->db['source']->query("SELECT max(id) FROM {$source_tablepre}share"), 0);
if($start < $maxsid) $converted = 1;

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}share WHERE id >= $start AND id < $start + $rpp") or dexit();
while ($share = $xc->db['source']->fetch_array($query)) {
	$content		=	$share['content'];
	$share			=	daddslashes($share);

	$sid			=	$share['id'];
	switch($share['type']) {
		case 'web' :
			$title_template	=	'分享了一个网址';
			$body_template	=	'{link}';
			$arr			=	unserialize($content);
			foreach($arr as $key => $value){
				${$key} = $value;
			}
			$data			=	$link;
			$link			=	"<a href=\"".$data."\" target=\"_blank\">".$data."</a>";
			$body_data		=	serialize(array("link" => $link, "data" => $data));
			$body_general	=	$descrip;
			$image			=	'';
			$image_link		=	'';
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			$type			=	'link';
			break;
		case 'photo' :
			$title_template	=	'分享了一个图片';
			$body_template	=	'相册: <b>{albumname}</b><br>{username}<br>{title}';
			$arr			=	unserialize($content);
			foreach($arr as $key => $value){
				if(is_array($value)){
					foreach($value as $subkey => $subvalue){
						${$subkey} = $subvalue;
					}
				} else {
					${$key}		=	$value;
				}
			}
			$query_photo = $xc->db['source']->query("select a.aname, p.aid, p.pid from {$source_tablepre}cnphoto p, {$source_tablepre}cnalbum a where p.aid = a.aid and p.path = '$image'");
			$detail			=	$xc->db['source']->fetch_array($query_photo);
			$albumname		=	$detail['aname'];
			$albumid		=	$detail['aid'];
			$picid			=	$detail['pid'];
			
			$albumname		=	"<a href=\"space.php?uid=".$uid."&do=album&id=".$albumid."\">".$albumname."</a>";
			$username		=	"<a href=\"space.php?uid=".$uid."\">".$username."</a>";
			$body_data		=	serialize(array("albumname" => $albumname, "username" => $username, "title" => ""));
			$body_general	=	$descrip;
			$image			=	$image;
			$image_link		=	"space.php?uid=".$uid."&do=album&picid=".$picid;
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			$type			=	'pic';
			break;

		case 'album' :
			$title_template	=	'分享了一个相册';
			$body_template	=	'<b>{albumname}</b><br>{username}';
			$arr			=	unserialize($content);
			foreach($arr as $key => $value){
				if(is_array($value)){
					foreach($value as $subkey => $subvalue){
						${$subkey} = $subvalue;
					}
				}else{
					${$key}		=	$value;
				}
			}
			$query_album = $xc->db['source']->query("select p.aid, a.aname from {$source_tablepre}cnphoto p, {$source_tablepre}cnalbum a where p.aid=a.aid and p.path = '$image'");
			$detail			=	$xc->db['source']->fetch_array($query_album);
			$aid			=	$detail['aid'];
			$name			=	$detail['aname'];

			$albumname		=	"<a href=\"space.php?uid=".$uid."&do=album&id=".$aid."\">".$name."</a>";
			$username		=	"<a href=\"space.php?uid=".$uid."\">".$username."</a>";
			
			$body_data		=	serialize(array("albumname" => $albumname, "username" => $username));
			$body_general	=	$descrip;
			$image			=	$image;
			$image_link		=	"space.php?uid=".$uid."&do=album&id=".$aid;
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			$type			=	'album';
			break;

		case 'diary' :
			$title_template	=	'分享了一篇日志';
			$body_template	=	'<b>{subject}</b><br>{username}<br>{message}';
			$arr			=	unserialize($content);
			foreach($arr as $key => $value){
				if(is_array($value)){
					foreach($value as $subkey => $subvalue){
						${$subkey} = $subvalue;
					}
				}else{
					${$key}		=	$value;
				}
			}
			$query_diary = $xc->db['source']->query("select did, uid, username, content from {$source_tablepre}diary where subject = '$subject'");
			$detail			=	$xc->db['source']->fetch_array($query_diary);
			$did			=	$detail['did'];
			$uid			=	$detail['uid'];
			$username		=	$detail['username'];
			$content		=	$detail['content'];

			$subject		=	"<a href=\"space.php?uid=".$uid."&do=blog&id=".$did."\">".$subject."</a>";
			$username		=	"<a href=\"space.php?uid=".$uid."\">".$username."</a>";
			$message		=	$content;
			
			$body_data		=	serialize(array("subject" => $subject, "username" => $username, "message" => $content));
			$body_general	=	$descrip;
			$image			=	'';
			$image_link		=	'';
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			$type			=	'blog';
			break;

		case 'user' :
			$title_template	=	'分享了一个用户';
			$body_template	=	'<b>{username}</b><br>{reside}<br>{spacenote}';
			$arr			=	unserialize($content);
			foreach($arr as $key => $value){
				if(is_array($value)){
					foreach($value as $subkey => $subvalue){
						${$subkey} = $subvalue;
					}
				}else{
					${$key}		=	$value;
				}
			}
			$uid = $xc->db['source']->result($xc->db['source']->query("select uid from {$source_tablepre}members where username = '$username'"), 0);
			
			$username		=	"<a href=\"space.php?uid=".$uid."\">".$username."</a>";
			
			$body_data		=	serialize(array("username" => $username, "reside" => "", "spacenote" => ""));
			$body_general	=	$descrip;
			$image			=	'';
			$image_link		=	'';
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			$type			=	'space';
			break;
			
		case 'music':
			$title_template	=	'分享一首音乐';
			$body_template	=	' 	{link}';
			$arr			=	unserialize($content);
			foreach($arr as $key => $value){
				${$key} = $value;
			}
			$data			=	$link;
			$link			=	"<a href=\"".$data."\" target=\"_blank\">".$data."</a>";
			$body_data		=	serialize(array("link" => $link, "data" => $data, "musicvar"=>$data));
			$body_general	=	$descrip;
			$image			=	'';
			$image_link		=	'';
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			$type			=	'music';
			break;
	}
	$uid			=	$share['uid'];
	$username		=	$share['username'];
	$dateline		=	$share['postdate'];
	$hot			=	'';
	$hotuser		=	'';

	$field1 = array('sid','type','uid','username','dateline','title_template','body_template','body_data','body_general','image','image_link','hot','hotuser');
	$query1 = getinsertsql("{$discuz_tablepre}home_share", $field1);
	
	if ($xc->db['discuz']->query($query1)) {
		$convertedrows ++;
	} else {
		reportlog("无法转换分享 id = $sid ，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query1."</textarea>");
	}
	$converted = 1;
	$totalrows ++;
}
?>
