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

InitGP(array('link','descrip','ifhidden'),'P',1);
$link = str_replace('&#61;','=',$link);
$ifhidden = (int)$ifhidden;
$ifhidden!=0 && $ifhidden!=1 && $ifhidden = 0;
if (!$link) {
	ObHeader('mode.php?m=$m&q=share');
}
if (!preg_match("/^http\:\/\/.{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")) {
	foreach ($wordsfb as $key => $value) {
		$banword = (string) stripslashes($key);
		if (strpos($descrip,$banword)!==false) {
			Showmsg('title_wordsfb');
		}
	}
	foreach ($replace as $key => $value) {
		$banword = (string) stripslashes($key);
		if (strpos($descrip,$banword)!==false) {
			Showmsg('post_wordsfb');
		}
	}
}
$share = array();
$share['link'] = $link;
$share['descrip'] = $descrip;


$parselink = parse_url($link);

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).")");
$f_id = $db->insert_id();

if ($type != 'web' && $f_hash) {
	$share_code = '[share]'.($type=='video'?$share['video']['host']:$type).','.$f_hash.','.$f_id.'[/share]';
} else {
	$share_code = '';
}
$type_name = getLangInfo('o',$type,false,1);

$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)
		);
if ($ifhidden) {
	$see = 'favor';
} else {
	$see = 'self';
}

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;
}
?>