<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');

InitGP(array('link','descrip','ifhidden','type'),'P',1);
$share = array();

$link	= str_replace('&#61;','=',$link);
$ifhidden = (int)$ifhidden;

if ($type && in_array($type,array('user','photo','album','group','diary'))) {
	define('AJAX','1');
	$id	= (int)GetGP('id');
	!$id && Showmsg('data_error');
	if ($type=='user') {
		if ($id==$winduid) showmsg('data_error');
		$userdb = getOneInfo($id);
		empty($userdb) && Showmsg('data_error');
		$link	= $db_bbsurl.'/mode.php?m=o&q=user&u='.$id;
		$type	= 'user';
		$share['user']['username']	= $userdb['username'];
		$share['user']['image']	= $userdb['face'];
	} elseif ($type=='photo') {
		$photo = $db->get_one("SELECT p.pid,p.aid,p.path,p.uploader,p.ifthumb,a.aname,a.private,a.ownerid FROM pw_cnphoto p LEFT JOIN pw_cnalbum a ON p.aid=a.aid WHERE p.pid=" . pwEscape($id) . " AND a.atype='0'");
		empty($photo) && Showmsg('data_error');
		$link	= $db_bbsurl.'/mode.php?m=o&q=photos&a=view&pid='.$id;
		$type	= 'photo';
		$share['photo']['uid']	= $photo['ownerid'];
		$share['photo']['username']	= $photo['uploader'];
		$share['photo']['image']	= getphotourl($photo['path'],$photo['ifthumb']);
	} elseif ($type=='album') {
		$album 	= $db->get_one("SELECT aname,ownerid,owner,lastphoto FROM pw_cnalbum WHERE atype='0' AND aid=" . pwEscape($id));
		empty($album) && Showmsg('data_error');
		$link	= $db_bbsurl.'/mode.php?m=o&q=photos&a=album&aid='.$id;
		$type	= 'album';
		$share['album']['uid']	= $album['ownerid'];
		$share['album']['username']	= $album['owner'];
		$share['album']['image']	= getphotourl($album['lastphoto']);
	} elseif ($type=='group') {
		$group 	= $db->get_one("SELECT id,cname,cnimg FROM pw_colonys WHERE id=" . pwEscape($id));
		empty($group) && Showmsg('data_error');
		if ($group['cnimg']) {
			list($cnimg) = geturl("cn_img/$group[cnimg]",'lf');
		} else {
			$cnimg = $pwModeImg.'/nophoto.gif';
		}
		$link	= $db_bbsurl.'/mode.php?m=o&q=group&cyid='.$id;
		$type	= 'group';
		$share['group']['name']	= $group['cname'];
		$share['group']['image']	= $cnimg;
	} elseif ($type=='diary') {
		$diary 	= $db->get_one("SELECT did,subject,uid FROM pw_diary WHERE did=" . pwEscape($id));
		empty($diary) && Showmsg('data_error');
		
		$link	= $db_bbsurl.'/mode.php?m=o&q=diary&u='.$diary['uid'].'&did='.$id;
		$type	= 'diary';
		$share['diary']['subject'] = $diary['subject'];
	}
}

$ifhidden!=0 && $ifhidden!=1 && $ifhidden = 0;
if (!$link) {
	ObHeader('mode.php?m=$m&q=share');
}
!preg_match("/^https?\:\/\/.{4,255}$/i", $link) && Showmsg('mode_share_link_error');

if (strlen($descrip)>300) Showmsg('mode_share_descrip_toolang');

if (@include(D_P."data/bbscache/wordsfb.php")) {
	if ($wordsfb) {
		foreach ($wordsfb as $key => $value) {
			$banword = (string) stripslashes($key);
			if (strpos($descrip,$banword)!==false) {
				Showmsg('title_wordsfb');
			}
		}
	}
	if ($replace) {
		foreach ($replace as $key => $value) {
			$banword = (string) stripslashes($key);
			if (strpos($descrip,$banword)!==false) {
				Showmsg('post_wordsfb');
			}
		}
	}
}

$share['link'] = $link;
$share['descrip'] = $descrip;


$parselink = parse_url($link);
if (!$type) {
	if(preg_match("/(youku.com|youtube.com|5show.com|ku6.com|sohu.com|sina.com.cn)$/i",$parselink['host'],$hosts)) {
		$hash = getVideo($link,$hosts[1]);
		if(!empty($hash)) {
			$type = $share['type'] = 'video';
			$share['video']['hash'] = $f_hash = $hash;
			$share['video']['host'] = $hosts[1];
		} else {
			$type = $share['type'] = 'web';
		}
	} elseif (preg_match("/\.(mp3|wma)$/i",$link)) {
		$type = $share['type'] = 'music';
		$f_hash = $share['link'];
	} elseif (preg_match("/\.swf$/i",$link)) {
		$type = $share['type'] = 'flash';
		$f_hash = $share['link'];
	} else {
		$type = $share['type'] = 'web';
	}
}
$content = serialize($share);
$arr = array($type,$winduid,$windid,$timestamp,$content,$ifhidden);
$db->update("INSERT INTO pw_share(type,uid,username,postdate,content,ifhidden) VALUES(".pwImplode($arr).")");
if (!$ifhidden) {
	$f_id = $db->insert_id();

	if ($type != 'web' && $f_hash) {
		$share_code = '[share]'.($type=='video'?$share['video']['host']:$type).','.$f_hash.','.$f_id.'[/share]';
	} elseif ($type == 'user') {
		$share_code = '[url='.$share['link'].'][img]'.$share['user']['image'].'[/img][/url]';
		$title	= $share['user']['username'];
	} elseif ($type == 'photo') {
		$belong	= getLangInfo('o','photo_belong',false,1);
		$image_link	= $link;
		$share['link']	= $db_bbsurl.'/mode.php?m=o&q=user&u='.$photo['ownerid'];
		$title	= $share['photo']['username'];
		$share_code = '[url='.$image_link.'][img]'.$share['photo']['image'].'[/img][/url]'.$belong;
	} elseif ($type == 'album') {
		$belong	= getLangInfo('o','photo_belong',false,1);
		$image_link	= $link;
		$share['link']	= $db_bbsurl.'/mode.php?m=o&q=user&u='.$album['ownerid'];
		$title	= $share['album']['username'];
		$share_code = '[url='.$image_link.'][img]'.$share['album']['image'].'[/img][/url]'.$belong;
	} elseif ($type == 'group') {
		$title	= $share['group']['name'];
		$share_code = '[url='.$share['link'].'][img]'.$share['group']['image'].'[/img][/url]'.$belong;
	} elseif ($type == 'diary') {
		$title	= $share['diary']['subject'];
		$share_code = '';
	} else {
		$share_code = '';
	}
	$type_name	= getLangInfo('o',$type,false,1);
	
	!$title && $title = substrs($share['link'],40);
	
	pwAddFeed($winduid,'share',$f_id,array(
								'lang'=>'share_view',
								'link'=>$share['link'],
								'title'=>$title,
								'descrip'=>$descrip,
								'share_code'=>$share_code,
								'uid'=>$winduid,
								'username'=>$windid,
								'type_name'=>$type_name)
			);
	$see = 'self';
} else {
	$see = 'favor';
}

countPosts('+1');
if (defined('AJAX')) {
	echo 'success';ajax_footer();
} else {
	refreshto("mode.php?m=$m&q=share&see=$see",'operate_success');
}


function getVideo($link, $host) {
	switch ($host) {
		case 'youku.com':
			preg_match("/id\_(\w+)\=/",$link,$matches);
			break;
		case 'ku6.com':
			preg_match("/\/([\w\-]+)\.html/",$link,$matches);
			break;
		case 'youtube.com':
			preg_match("/v\=([\w\-]+)/",$link,$matches);
			break;
		case 'sina.com.cn':
			preg_match("/\/(\d+)-(\d+)\.html/",$link,$matches);
			break;
		case 'sohu.com':
			preg_match("/\/(\d+)\/*$/",$link,$matches);
			break;
	}
	if(!empty($matches[1])) {
		$return = $matches[1];
	} else {
		$return = '';
	}
	return $return;
}
?>