<?php
require_once (dirname(__FILE__).'/global.php');

if($action=='copyimg'){
	$picurl=$_POST[picurl];
	$strcode = file_get_contents($picurl);
	
	$name = $lfjuid.'_'.rands(4).'_'.$timestamp.'.jpg';
	if(!is_dir(ROOT_PATH."$webdb[updir]/$updir")){
		makepath(ROOT_PATH."$webdb[updir]/$updir");
	}
	write_file(ROOT_PATH."$webdb[updir]/$updir/$name",$strcode);
	die("<img src=\"$webdb[www_url]/$webdb[updir]/$updir/$name\" style='width:200px;'>");
}

if(!$lfjid){
	echo '<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
		 <body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0">';
	die("<A HREF='$webdb[www_url]/do/login.php' target='_blank'  onclick='window.self.close();'>请先在前台登录</A>");
}

print <<<EOT

<html>
<head>
<title>$titleDB[title]</title>
<meta name='keywords' content=''>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
body{
	margin:0px;
	padding:0px;
	font-size:9pt;
	background:#FFF;
	position:relative;
}
input { vertical-align:middle;}
input,select{color:navy;font:normal 12px/120% Tahoma,Verdana,sans-serif; height:19px;}
input,textarea,hr{color:navy;font:normal 12px/120% Verdana,sans-serif;padding:2px;border:1px solid #ddd; height:27px;}
.global_file { width:150px;}
.btn { background-color:#e1e1e1; border-radius:3px; padding:0 12px; border:1px solid #ccc; line-height:18px; font-size:12px; font-weight:bold; color:#404040; cursor:pointer;position:absolute;right:0;top:0;}
.btn:hover { color:#fff; border:1px solid #1b57aa;background-color:#1b57aa;}
.notes{text-align:center;line-height:30px;}
</style>
<script type="text/javascript" src="$webdb[www_url]/images/default/hf/jquery-v1.7.2.js"></script>
<script type="text/javascript" src="$webdb[www_url]/images/exif/selectimg.js"></script>
<script type="text/javascript" src="$webdb[www_url]/images/exif/exif.js"></script>
<SCRIPT LANGUAGE='JavaScript'>
<!--
if('$showDomain'=='1'){
	if('$webdb[cookieDomain]'!='')document.domain = '$webdb[cookieDomain]';
}else{
	window.onerror=function (){
		url = '$WEBURL';
		url +=url.indexOf('?')>0?'&':'?';
		window.location.href=url+'showDomain=1';
		return true;
	};
	obj = (self==top) ? window.opener : window.parent ;
	obj.document.body;
}
function uploadImages(picurl){
	$('#testaaaa').val(picurl);
	$('.showimg').html('<img src="'+picurl+'" style="width:200px;">');
}
//-->
</SCRIPT>
</head>
<body>
EOT;
if($postfile){

	//其中..与/开头都是不允许的
	if( !ereg("^[0-9a-z_/]+$",$dir)||ereg("^/",$dir) ){
		$dir="other";
	}
	$array[name]=is_array($postfile)?$_FILES[postfile][name]:$postfile_name;
	$array[path]=$webdb[updir]."/".$dir;
	$array[size]=is_array($postfile)?$_FILES[postfile][size]:$postfile_size;
	
	$array[updateTable]=1;	//统计用户上传的文件占用空间大小
	$filename=upfile(is_array($postfile)?$_FILES[postfile][tmp_name]:$postfile,$array);
	//删除用户反复上传的图片
	if($ISone){
		delete_attachment($lfjuid,tempdir("$oldfile"));
	}
	$newfile="$dir/$filename";
	echo "上传成功,<A HREF='?fn=$fn&dir=$dir&label=$_GET[label]&ISone=$_GET[ISone]&oldfile=$newfile&showDomain=$showDomain'>你可以继续或重新上传</A>";
	$fn || $fn="upfile";
	$weburl=tempdir($newfile);
	echo "<script>
				if(self==top){
					window.opener.$fn('$newfile','$array[name]','$array[size]','$_GET[label]','$weburl');
					window.self.close();
				}else{
					window.parent.$fn('$newfile','$array[name]','$array[size]','$_GET[label]','$weburl');
				}
		 </script>";
			
	exit;
}
print <<<EOT



<form name="form2" method="post" action="?action=copyimg&updir=$updir" enctype="multipart/form-data">
  <div style="padding:5px;"><input type="file" onChange="selectFileImage(this);"></div>
  <div style="padding:5px;"><textarea name="picurl" cols="" rows="" id="testaaaa" style="width:600px;height:400px;"></textarea></div>
  <div style="padding:5px;"><input  type="submit" name="Submit" value="上传"></div>
</form>
<div class='showimg'></div>
</body>
</html>

EOT;
?>