/*************************************************************************
* Copyright (c) 2011 im007boy@gmail.com. All rights reserved.            *
* Use of this source code is governed by a BSD-style license that can be *
* found in the LICENSE file.                                             *
*************************************************************************/

var regExps = {
	"thunder":new RegExp("(thunder:\\/\\/){1}[a-zA-Z0-9/=+]+","ig"),
	"flashget":new RegExp("(flashget:\\/\\/){1}[a-zA-Z0-9/=+]+","ig"),
	"qqdl":new RegExp("(qqdl:\\/\\/){1}[a-zA-Z0-9/=+]+","ig"),
	"fs2you":new RegExp("(fs2you:\\/\\/){1}[a-zA-Z0-9/=+]+","ig")
};
var Filename_Extension ='\\.srt|\\.mkv|\\.jad|\\.bis|\\.bin|\\.sw|\\.reg|\\.sisx|\\.sis|\\.eet|\\.xls|\\.nh|\\.kdh|\\.ppt|\\.mdf|\\.wmp|\\.doc|\\.pdf|\\.chm|\\.asf|\\.avi|\\.exe|\\.mp3|\\.cue|\\.mpeg|\\.mpg|\\.mpga|\\.ra|\\.rar|\\.rm|\\.rmvb|\\.tar|\\.wma|\\.wmv|\\.zip|\\.7z|\\.gz|\\.bz2|\\.b|\\.swf|\\.mp4|\\.3gp|\\.torrent|\\.txt|\\.jar|\\.mov|\\.wav|\\.eip|\\.iso|\\.mds|\\.mdx|\\.isz';

var phpLinkReg = [{"RegExp":"convertFgURL[ ]*[(][ ]*(\'|\")[^\'\"]+(?=\\1)","PreRegExp":"convertFgURL[ ]*[(][ ]*(\'|\")"},
					{"RegExp":"FlashGet_Down[ ]*[(][ ]*(\'|\")[^\'\"]+(?=\\1)","PreRegExp":"FlashGet_Down[ ]*[(][ ]*(\'|\")"}];

function compress(g){
	/**
	 * 接受一个数组参数，合并数组中的重复项并返回此数组
	 *
	 *
	 **/
	var i = 0;
	var t = 0;
	for (i = 0;i < g.length;i++)
	{
		for (t = i+1;t < g.length;t++)
		{
			if (g[i] == g[t])
			{
				g.splice(t,1);
				t --;
			}	
		}			
	}
	 
	return g; 
}

function decodeOnce(str){
	//返回输入字符串中的一个加密链及其破解结果
	var i = 0;
	var newstr = {  'ifchange':0,
					'fromStr':'',
					'toStr':''};
	var fromStr = [];				
	var toStr = "";	
	if (typeof str != "string")
		return newstr;
		
	str = str.replace(/\n/g," ").replace(/&nbsp;/g," ").replace(/&quot;/g,"\"").replace(/&#34;/g,"\"").replace(/&apos;/g,"\'").replace(/&#39;/g,"\'").replace(/&amp;/g,"&").replace(/&#38;/g,"&").replace(/&lt;/g,"<").replace(/&#60;/g,"<").replace(/&gt;/g,">").replace(/&#62;/g,">").replace(/&ldquo;/g,"“").replace(/&rdquo;/g,"”").replace(/&lsquo;/g,"‘").replace(/&rsquo;/g,"’").replace(/&mdash;/g,"—").replace(/&ndash;/g,"–");
	
	try{
		str = decodeURIComponent(str);
	}catch(e){}
	
	/* thunder */
	fromStr = str.match(regExps['thunder']) || [];
	for (i = 0;i < fromStr.length;i++){
		toStr = decode64(fromStr[i].slice(10).replace(/ +$|\/$/g,"")).slice(2,-2);;
		
		newstr.toStr = toStr;
		newstr.fromStr = fromStr[i];
		newstr.ifchange = 1;
		return newstr;
	}
	fromStr.length = 0;
	
	/* flashget */
	fromStr = str.match(regExps['flashget']) || [];
	for (i = 0;i < fromStr.length;i++){
		toStr = decode64(fromStr[i].slice(11).replace(/ +$|\/$/g,"")).slice(10,-10);
		
		newstr.toStr = toStr;
		newstr.fromStr = fromStr[i];
		newstr.ifchange = 1;
		return newstr;
	}
	
	/* qqdl */
	fromStr = str.match(regExps['qqdl']) || [];
	for (i = 0;i < fromStr.length;i++){
		toStr = decode64(fromStr[i].slice(7).replace(/ +$|\/$/g,""));
		
		newstr.toStr = toStr;
		newstr.fromStr = fromStr[i];
		newstr.ifchange = 1;
		return newstr;
	}
	
	/*fs2you beta~ */
	fromStr = str.match(regExps['fs2you']) || [];
	for (i = 0;i < fromStr.length;i++){
		toStr = decode64(fromStr[i].slice(9).replace(/ +$|\/$/g,""));
		
		newstr.toStr = "http://"+toStr.split("|")[0];
		newstr.fromStr = fromStr[i];
		newstr.ifchange = 1;
		return newstr;
	}
	return newstr;
}
function detectSoftLink(str,n){
	/**
	 * 接受一段字符串和返回值个数（1或不限）
	 * 返回真实链接数组(长度为一定数组)或空数组
	 * 数据格式为http:// ftp:// mms:// ../....
	 **/
	var ifg = "g";
	var i = 0;
	if (n == 1)
		ifg = "";
	if(typeof str != "string") 
		return [];
	//处理
	//定义链接扩展名

	//文件下载地址正则.引号增强匹配
	var urlReg = new RegExp("[\"\']((\\.){1,2}|http:\\/\\/|https:\\/\\/|ftp:\\/\\/|mms:\\/\\/){1}[^\'\"]*?("+Filename_Extension+"){1}(?=[\"\'])","i"+ifg);
	 
	//  "http://...zip
	str = '\"'+str+'\"';
	var fileurl = str.match(urlReg) || [];
	
	if (n == 1)
		fileurl.splice(1);
		
	//合并重复链接
	if (fileurl.length > 1)
		fileurl = compress(fileurl);	
	//处理链接的第一个引号
	for(i = 0;i < fileurl.length;i++)
		fileurl[i] = fileurl[i].substring(1);
		
	//返回数组
	return fileurl;	
}
function detectPhpLink(str){
	/**
	 * 读取正则表达式库
	 * 
	 * 格式为
	 * localStorage["RegExpDateLibrary"] = '{RegExpDateLibrary:[{"RegExp":'',"Prefix":""},{"RegExp":'',"Prefix":""}]}'
	 * friendlist[i].urlExp = ""; 匹配页面网址的正则表达式
	 * friendlist[i].fileExp = ""; /http/ig  直接量	 
	 * convertFgURL('http://www.wenku8.cn/modules/article/down.php?type=txt&id=700&fname=%B8%D6%D6%AE%D3%F0',19571)
	 **/
	var Prefix;
	var hrefarray = [];
	var i = 0;
	var t = 0;
	if (typeof str != "string")
		return [];
	try{
		/** 
			RegExp  	convertFgURL[(](\'|\")[^\'\"]+(?=\\1)
			PrefixLen	convertFgURL('
		**/
		for (i = 0;i < phpLinkReg.length;i++) 
		{
			//特征库确保能够正确生成正则表达式
			hrefarray = str.match(new RegExp(phpLinkReg[i].RegExp,"ig"));
			if (hrefarray)
			{
				for (t = 0;t < hrefarray.length;t++)	
				{
					//去掉前缀
					Prefix = hrefarray[t].match(new RegExp(phpLinkReg[i].PreRegExp,"i"))[0];
					hrefarray[t] = hrefarray[t].substring(Prefix.length); //由于前面多了个引号
				}
				break;
			}
		}		
		if (hrefarray == null)
			hrefarray = [];	
		return hrefarray;
	}catch(e){
		return [];
	}	

}