<?php

function addbbcode() {
	global $xc,$discuz_tablepre, $bbcodearray, $config_type;

	$flashplayer ='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="{1}" height="{2}"><param name="allowScriptAccess" value="sameDomain"><param name="movie" value="{3}"><param name="quality" value="high"><param name="bgcolor" value="#ffffff"><embed src="{3}" quality="high" bgcolor="#ffffff" width="{1}" height="{2}" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	$flashexplanation = "$bbcodearray[flashexplanation]";
	$flashprompt = "$bbcodearray[flashprompt]";
	$flashexample = '[flash=500,350]http://your.com/example.swf[/flash]';

	$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_bbcode SET replacement = '$flashplayer', example = '$flashexample', prompt ='$flashprompt', params =3, available =1 WHERE tag ='flash'");
}

function ctspecialgroup() {
	global $xc, $source_tablepre, $discuz_tablepre, $config_type;

	$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_usergroup WHERE groupid > 15");
	$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_usergroup_field WHERE groupid > 15");

	$query = $xc->db['source']->query("SELECT usergroupid, usertitle FROM {$source_tablepre}usergroups WHERE parentgid =2");
	while ($g = $xc->db['source']->fetch_array($query)) {
		$g		=	daddslashes($g);
		$groupid	=	$g['usergroupid'] + 15;
		$grouptitle	=	$g['usertitle'];
		$sql1	=	"REPLACE INTO {$discuz_tablepre}common_usergroup (groupid, radminid, type, system, grouptitle, creditshigher, creditslower, stars, color, icon, allowvisit, 				allowsendpm, allowinvite, allowmailinvite, maxinvitenum, inviteprice, maxinviteday) 
					VALUES ('$groupid', 0, 'special', 'private', '$grouptitle', 50,	200, 2,	'',	'',	1, 1, 0, 0, 0, 0, 10)";

		$sql2	=	"REPLACE INTO {$discuz_tablepre}common_usergroup_field (groupid, readaccess, allowpost, allowreply, allowpostpoll, allowpostreward, allowposttrade, allowpostactivity, allowdirectpost, allowgetattach, allowgetimage, allowpostattach, allowpostimage, allowvote, allowsearch, allowcstatus, allowinvisible, allowtransfer, allowsetreadperm, allowsetattachperm, allowposttag, allowhidecode, allowhtml, allowanonymous, allowsigbbcode, allowsigimgcode, allowmagics, disableperiodctrl, reasonpm, maxprice, maxsigsize, maxattachsize, maxsizeperday, maxthreadsperhour, maxpostsperhour, attachextensions, raterange, mintradeprice, maxtradeprice, minrewardprice, maxrewardprice, magicsdiscount, maxmagicsweight, allowpostdebate, tradestick, exempt, maxattachnum, allowposturl, allowrecommend, allowpostrushreply, maxfriendnum, maxspacesize, allowcomment, allowcommentarticle, searchinterval, searchignore, allowblog, allowdoing, allowupload, allowshare, allowblogmod, allowdoingmod, allowuploadmod, allowsharemod, allowcss, allowpoke, allowfriend, allowclick, allowmagic, allowstat, allowstatdata, videophotoignore, allowviewvideophoto, allowmyop, magicdiscount, domainlength, seccode, disablepostctrl, allowbuildgroup, allowgroupdirectpost, allowgroupposturl, edittimelimit, allowpostarticle, allowdownlocalimg, allowdownremoteimg, allowpostarticlemod, allowspacediyhtml, allowspacediybbcode, allowspacediyimgcode, allowcommentpost, allowcommentitem, allowcommentreply, allowreplycredit, ignorecensor, allowsendallpm, allowsendpmmaxnum, maximagesize, allowmediacode, allowat, allowsetpublishdate, allowfollowcollection, allowcommentcollection, allowcreatecollection) 
					VALUES ('$groupid', 150, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 31, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 2, 1, 0, 20, 300, 2048000, 0, 0, 0, 'chm, pdf, zip, rar, tar, gz, bzip2, gif, jpg, jpeg, png', '', 1, 0, 1, 0, 0, 180, 1, 5, 255, 0, 3, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 15, 0, 0, 15, 0, 0, 0, 1, 1, 2, 1, 1,  1, 0, 1, 1, 2048000, 0, 0, 0, 0, 0, 0);";
		$xc->db['discuz']->query($sql1);
		$xc->db['discuz']->query($sql2);
	}
}

function convertbbcode($message) {
	global $dvp, $source_bbsurl;
	$pregfind = array(
		"/\[uploadimage\](\d+),.*\[\/uploadimage\]/is",
		"/\[uploadfile\](\d+),.*\[\/uploadfile\]/is",
		"/\[upload=([a-z]+)\].*viewFile.asp?.*ID=(\d+).*\[\/upload\]/is",
		"/\[upload=[\d\w]*?,[^\n\r]*?\]viewFile\.asp\?ID=(\d*?)\[\/upload\]/is",
		"/\[upload=(jpg|gif|bmp|png),.*?\]\s*UploadFile\/([a-zA-Z0-9_\-\/\.]+)\s*\[\/upload\]/ies",
		"/\[upload=(jpg|gif|bmp|png)\]\s*UploadFile\/([a-zA-Z0-9_\-\/\.]+)\s*\[\/upload\]/eis",
		"/\[upload=([a-z]+)\]\s*UploadFile\/([a-zA-Z0-9_\-\/\.]+)\s*\[\/upload\]/eis",
		"/\[(\w{2})=(\d{1,4}),(\d{1,4}),(true|false|0|1)\]\s*([^\[\<\r\n]+?)\s*\[\/\\1\]/ies",
		"/\[sound\]\s*([^\[\<\r\n]+?)\s*\[\/sound\]/is",
		"/\[qt=(\d{1,4}),(\d{1,4})\]\s*([^\[\<\r\n]+?)\s*\[\qt\]/is",
		"/\[(center|left|right)\]/i",
		"/\[\/(center|left|right)\]/i",
		"/\[face/i",
		"/\[\/face\]/i",
		"/<div class=\"quote\">/i",
	);
	$pregreplace = array(
		"[attach]\\1[/attach]",
		"[attach]\\1[/attach]",
		"[attach]\\2[/attach]",
		"[attach]\\1[/attach]",
		"parseattach('\\2', $dvp[announceid], $dvp[isupload], 1)",
		"parseattach('\\2', $dvp[announceid], $dvp[isupload], 1)",
		"parseattach('\\2', $dvp[announceid], $dvp[isupload])",
		"parsemedia('\\2', '\\3', '\\4', '\\5')",
		"[media=wma,200,64,0]\\1[/media]",
		"[media=rm,\\1,\\2,0]\\3[/media]",
		"[align=\\1]",
		"[/align]",
		"[font",
		"[/font]",
		"<div class=\"msgheader\">QUOTE:</div><div class=\"msgborder\">",
	);

	$bbsurl = str_replace(' ', '', $source_bbsurl);//去掉空格
	if($bbsurl){
		$bbsurl = substr($bbsurl, 0, 1) == '|' ? substr($bbsurl, 1) : $bbsurl;//去掉开头的|
		$bbsurl = substr($bbsurl, -1) == '|' ? substr($bbsurl, 0, -1) : $bbsurl;//去掉尾部的|
		$bbsurltemp = str_replace(array('/', '.'), array('\/', '\.'), $bbsurl);//转义
		$urlfind = array(
			"/($bbsurltemp)\/index.asp\?boardid=(\d+)/is",
			"/($bbsurltemp)\/list.asp\?boardid=(\d+)/is",
			"/($bbsurltemp)\/dispbbs.asp\?(boardid=\d+)&amp;id=(\d+)(&amp;star=(\d+))(&amp;page=\d{0,}){0,1}/is",
			"/($bbsurltemp)\/dispbbs.asp\?(boardid=\d+)&amp;id=(\d+)(&amp;page=\d{0,}){0,1}/is",
			"/($bbsurltemp)\/dispbbs.asp\?(boardid=\d+)(&amp;page=\d{0,}){0,1}&amp;id=(\d+)(&amp;star=(\d+))/is",
			"/($bbsurltemp)\/dispbbs.asp\?(boardid=\d+)(&amp;page=\d{0,}){0,1}&amp;id=(\d+)/is",
		);
		$urlreplace = array(
			"\\1/forumdisplay.php?fid=\\2",
			"\\1/forumdisplay.php?fid=\\2",
			"\\1/viewthread.php?tid=\\3&extra=&page=\\5",
			"\\1/viewthread.php?tid=\\3",
			"\\1/viewthread.php?tid=\\4&extra=&page=\\6",
			"\\1/viewthread.php?tid=\\4",
		);
		$pregfind = array_merge($pregfind, $urlfind);
		$pregreplace = array_merge($pregreplace, $urlreplace);
	}
	return daddslashes(str_replace(array('[replyview]', '[/replyview]', '[B]', '[/B]'), array('[hide]', '[/hide]', '[b]', '[/b]'), preg_replace($pregfind, $pregreplace, stripslashes($message))));
}

function parsemedia($width, $height, $autostart, $url) {
	if($autostart == 'true' || $autostart == '1') {
		$autostart = '1';
	} else {
		$autostart = '0';
	}
	return '[media=wmv,'.$width.','.$height.','.$autostart.']'.$url.'[/media]';
}

function parseattach($filename, $pid, $haveattach, $isimage = 0) {
	global $discuz_tablepre,$xc;
	$filename = 'dvbbs/'.$filename;
	if($haveattach && ($aid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT aid FROM {$discuz_tablepre}forum_attachment WHERE pid='$pid' AND attachment='$filename'"), 0))) {
		return '[attach]'.$aid.'[/attach]';
	} else {
		if($isimage) {
			return "<img src=\"attachments/$filename\" border=\"0\" onclick=\"zoom(this)\" onload=\"if(this.width>document.body.clientWidth*0.5) {this.resized=true;this.width=document.body.clientWidth*0.5;this.style.cursor='pointer';} else {this.onclick=null}\" alt=\"\" />";
		} else {
			return "<a href=\"attachments/$filename\" target=\"_blank\">打开附件</a>";
		}
	}
}

function random($length, $numeric = 0) {
	mt_srand((double)microtime() * 1000000);
	if($numeric) {
		$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	} else {
		$hash = '';
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++) {
			$hash .= $chars[mt_rand(0, $max)];
		}
	}
	return $hash;
}

function relationship($fromuid, $touid) {
	if($fromuid < $touid) {
		return $fromuid.'_'.$touid;
	} elseif($fromuid > $touid) {
		return $touid.'_'.$fromuid;
	} else {
		return '';
	}
}
/**
 * 修正字符串中的特殊字符 去掉字符串末尾的‘\’
 *
 * 功能添加中
 *
*/
function formatstr($str) {
	while(ereg('\\\$',$str)){
		$str = substr($str,0,-1);
	}
	return 	$str;
}

/**
 * 修正字符串中的特殊字符 去掉字符串末尾的‘\’
 *
 * 功能添加中
 *
*/
function formatstr($str) {
	while(ereg('\\\$',$str)){
		$str = substr($str,0,-1);
	}
	return 	$str;
}

?>