<?php
if (!defined('MOD')) die("access die");
require_once(R_P . '/include/class.string.php');
require_once(R_P . '/include/class.splitword.php');
require_once(R_P . '/include/class.charset.php');
require_once(R_P . '/include/inc.editor.php');
if (!$channelid) qbase :: purviewCheckDie("error request");
$path = "collect";
$work = $_GET['work'];

adminGetCookie();
function ReplaceRemoteUrl($s_Content, $sExt = "jpg|jpeg|png|gif") {
	global $_SET, $sSaveDir;
	$s_Match = "/http\:\/\/(.*?)\.(" . $sExt . ")/ies";
	if (!preg_match_all($s_Match, $s_Content, $a_Matches)) {
		return $s_Content;
	} 
	for ($i = 0; $i < count($a_Matches[0]); $i++) {
		if (strpos($a_Matches[0][$i], $_SERVER['HTTO_HOST']) !== false) continue;
		$a_RepeatRemote[] = $a_Matches[0][$i];
	} 
	$a_RemoteUrl = array_unique($a_RepeatRemote);
	$nFileNum = 0;
	for ($i = 0; $i < count($a_RemoteUrl); $i++) {
		$SaveFileType = substr($a_RemoteUrl[$i], strrpos($a_RemoteUrl[$i], ".") + 1);
		$SaveFileName = $sSaveDir . GetRndFileName($SaveFileType);
		if (SaveRemoteFile($SaveFileName, $a_RemoteUrl[$i])) {
			if ($nFileNum > 0) {
				$GLOBALS["sPathFileName"] .= "|";
				$GLOBALS["sPreFileName"] .= "|";
			} 
			$GLOBALS["sPathFileName"] .= $SaveFileName;
			$GLOBALS["sPreFileName"] .= $a_RemoteUrl[$i];
			$s_Content = str_replace($a_RemoteUrl[$i], W_P . $SaveFileName, $s_Content);
			$nFileNum = $nFileNum + 1;
		} 
	} 
	return $s_Content;
} 

function formatlink($url, $path){
	if(substr($url, 0, 4) == 'http') return $url;
	$temp = parse_url($path);
	if(substr($url, 0, 1) == '/') return $temp['scheme'] .'://'. $temp['host'] . $url;
	return $temp['scheme'] .'://'. $temp['host'] . $temp['path'] . '/' . $url;
}

function formaturl($l1, $l2) {
	if (preg_match_all("/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i", $l1, $regs)) {
		foreach($regs[0] as $num => $url) {
			$l1 = str_replace($url, formaturlreplace($url, $l2), $l1);
		} 
	} 
	return $l1;
} 
function formaturlreplace($l1, $l2) {
	if (preg_match("/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i", $l1, $regs)) {
		$I2 = $regs[3];
	} 
	if (strlen($I2) > 0) {
		$I1 = str_replace(chr(34), "", $I2);
		$I1 = str_replace(chr(39), "", $I1);
	} else {
		return $l1;
	} 
	$url_parsed = parse_url($l2);
	$scheme = $url_parsed["scheme"];
	if ($scheme != "") {
		$scheme = $scheme . "://";
	} 
	$host = $url_parsed["host"];
	$l3 = $scheme . $host;
	if (strlen($l3) == 0) {
		return $l1;
	} 
	$path = dirname($url_parsed["path"]);
	if ($path[0] == "\\") {
		$path = "";
	} 
	$pos = strpos($I1, "#");
	if ($pos > 0) $I1 = substr($I1, 0, $pos);

	if (preg_match("/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&amp;)|&)+/i", $I1)) {
		return $l1;
	} //http开头的url类型要跳过
	elseif ($I1[0] == "/") {
		$I1 = $l3 . $I1;
	} //绝对路径
	elseif (substr($I1, 0, 3) == "../") { // 相对路径
		while (substr($I1, 0, 3) == "../") {
			$I1 = substr($I1, strlen($I1) - (strlen($I1)-3), strlen($I1)-3);
			if (strlen($path) > 0) {
				$path = dirname($path);
			} 
		} 
		$I1 = $l3 . $path . "/" . $I1;
	} elseif (substr($I1, 0, 2) == "./") {
		$I1 = $l3 . $path . substr($I1, strlen($I1) - (strlen($I1)-1), strlen($I1)-1);
	} elseif (strtolower(substr($I1, 0, 7)) == "mailto:" || strtolower(substr($I1, 0, 11)) == "javascript:") {
		return $l1;
	} else {
		$I1 = $l3 . $path . "/" . $I1;
	} 
	return str_replace($I2, "\"$I1\"", $l1);
} 
function SaveRemoteFile($s_LocalFileName, $s_RemoteFileUrl) {
	$cont = qbase :: load($s_RemoteFileUrl);
	if (strlen($cont) > 1024 * 1024 || strlen($cont) < 3 * 1024) {
		return false;
	} 
	qbase :: writetofile(R_P.'/'. $s_LocalFileName, $cont);
	return true;
} 

function Cleartrim($string) {
	$string = trim($string);
	if ($string[0] == '|') {
		$string = substr($string, 1);
	} 
	if ($string[strlen($string)-1] == '|') {
		$string = substr($string, 0, -1);
	} 
	return $string;
} 
function HtmlGo(&$cp, $filter) {
	$fArray = array("\r", "\n", "\t");
	$cp = str_replace($fArray, "", $cp);
	$cp = preg_replace("@\<script(.*?)\</script\>@is", "", $cp);
	$cp = preg_replace("@\<iframe(.*?)\</iframe\>@is", "", $cp);
	$cp = preg_replace("@\<style(.*?)\</style\>@is", "", $cp);
	$cp = preg_replace("@\<!\-\-(.*?)\-\-\>@is", "", $cp);
	if ($filter) {
		$filter = explode(',', $filter);
		foreach($filter as $val) {
			$cp = preg_replace("@\<{$val}(.*?)\<\/{$val}\>@is", "", $cp);
		} 
	} 
	return $cp;
} 

function textFilterClean(&$cp, $filter) {
	if (!$cp || !$filter) return $cp; 
	// 支持通用匹配，[*] 所有, [d] 任意连续数字, [a]任意连续字母
	$replace = array(
		array('[*]', '[d]', '[a]'),
		array(md5('[*]'), md5('[d]'), md5('[a]')),
		array('(.*)', '\d', '[a-zA-Z]')
		);
	$filter = str_replace($replace[0], $replace[1], $filter);
	$filter = explode("\n", $filter);
	foreach($filter as $val) {
		$val = trim($val);
		if (!$val) continue;
		$val = str_replace(array('"', '\''), array('\"', "\'"), str_replace($replace[1], $replace[2], preg_quote($val, '/')));
		$cp = preg_replace("/" . $val . "/is", "", $cp);
	} 
	return $cp;
} 

function getDescription($cp, $len) {
	$arr = array("\n", "\r", "\t", " ", "　", "&nbsp;");
	$cp = str_replace($arr, "", strip_tags($cp));
	return qbase :: substrs($cp, $len);
} 

function rules_export($data) {
	unset($data['channelid'], $data['cid'], $data['id'], $data['last_update'], $data['dateline']);
	$string = base64_encode(qbase :: array2string($data));
	header("Content-type: application/txt");
	header("Content-Disposition: attachment; filename={$data['name']}.txt");
	echo $string;
} 

?>