<?
function remote_read($urlstr)
{
	$url				= parse_url($urlstr);
	$domain		= str_replace("www.", "", $url[host]);									// 도메인
	$file				= substr($url[path], strrpos($url[path], "/")+1);					// 파일명
	$basename	= str_replace("%", "", substr($file, 0, strrpos($file, ".")));	// 파일명 (확장자 제외)
	if (empty($basename)) {
		$basename = $file;
		$extension = "";
	} else	$extension	= substr($file, strrpos($file,".") + 1);						// 확장자

	$data			= "";
	$res				= fsockopen("$domain", 80, $strErrorNo, $strErrStr, 2);
	if($res) {
		$headerstr = "GET $urlstr HTTP/1.0\r\n"; 
		$headerstr.= "Host:$domain:80\r\n"; 
		$headerstr.= "\r\n";
		fputs($res, $headerstr);
		while (!feof($res)) {
			$data.= fgets($res, 1024); 
		} 
		fclose($res);
		if (strpos($data, "Not Found") > 0) return false;
	}
	
	if (strpos($data,"GIF89a")) {
		$data = substr($data, strpos($data,"GIF89a"));
	}
	else {
		$dataset = explode("\r\n\r\n", $data);
		$data = $dataset[1];
	}

	return array("data"=>$data, "domain"=>$domain, "file"=>$file, "basename"=>$basename, "extension"=>$extension, "url"=>$urlstr);
}

function image_save($file, $data)
{
	global $g4;
	@mkdir("$g4[path]/data/$g4[cheditor4]/", 0707);
	@chmod("$g4[path]/data/$g4[cheditor4]/", 0707);
	$ym = date("ym", $g4[server_time]);
	define('SAVE_AS_DIRECTORY', 	"$g4[path]/data/$g4[cheditor4]/$ym/");
	@mkdir(SAVE_AS_DIRECTORY, 0707);
	@chmod(SAVE_AS_DIRECTORY, 0707);

	if (!file_exists($file)) {
		$fp = @fopen ($file, 'w');
		if(!$fp) return false;
		fwrite ($fp, $data);
		fclose ($fp);
	}
	return true;
}

function image_save_run() {
	global $g4;
	$img_content = stripslashes($_POST[wr_content]);
	$img_content = str_replace("&lt;", "<", $img_content);
	$img_content = str_replace("&gt;", ">", $img_content);
	$patten = "/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i";
	preg_match_all($patten, $img_content, $match); 
	$dest_file = "data/$g4[cheditor4]/".date("ym")."/";		//저장경로

	if ($match) {
		foreach ($match[1] as $link) {
			$url = parse_url($link);
			if ($url[host] && $url[host] != $_SERVER['HTTP_HOST']) {
				$image = remote_read($link);				
				$saveFileName = $dest_file.$image[basename]."_".random_generator(8, 15) . ($image[extension]?".".$image[extension]:"");
				if (image_save($g4[path]."/".$saveFileName, $image[data]))
					$img_content = str_replace($link, $g4[path]."/".$saveFileName, $img_content);
			}
		}
		return $img_content;
	}
	else
		return false;
}

function random_generator ($min=8, $max=32, $special=NULL, $chararray=NULL) { 
    $random_chars = array(); 
    
    if ($chararray == NULL) { 
        $str = "abcdefghijklmnopqrstuvwxyz"; 
        $str .= strtoupper($str); 
        $str .= "1234567890"; 

        if ($special) { 
            $str .= "!@#$%"; 
        } 
    } 
    else { 
        $str = $charray; 
    } 

    for ($i=0; $i<strlen($str)-1; $i++) { 
        $random_chars[$i] = $str[$i]; 
    } 

    srand((float)microtime()*1000000); 
    shuffle($random_chars); 

    $length = rand($min, $max); 
    $rdata = ''; 
    
    for ($i=0; $i<$length; $i++) { 
        $char = rand(0, count($random_chars) - 1); 
        $rdata .= $random_chars[$char]; 
    } 
    return $rdata; 
} 

$img_content = image_save_run();
if ($img_content) {
	$_POST[wr_content] = addslashes($img_content);
	$wr_content = $_POST[wr_content];
}

?>