Код:
// обработка и кодирование GET, POST
$post=mass($HTTP_POST_VARS,1,"\$perem=\$value&");
if ($post && substr($post,-1)=="&"){$post=substr($post,0,-1);}
if ($method=="GET" && $post){
if ($get){$get="$get&$post";} else {$get="$post";}
$post="";
}
if ($get){$get="?$get";}
$len=strlen($post);
// обработка и кодирование COOKIE
$tmp=explode(".",$host); $host_cookie=$tmp[count($tmp)-2]."_".$tmp[count($tmp)-1];
$mass=$HTTP_COOKIE_VARS[$host_cookie];
$cookie=mass($mass,1,"\$perem=\$value; ");
if (substr($cookie,-2)=="; "){$cookie=substr($cookie,0,-2);}
// обработка multipart/form-data
$type=getenv('CONTENT_TYPE');
if ($type && stristr($type,"multipart/form-data")){
preg_match("#boundary=(.*)#si",$type,$boundary);
$boundary=$boundary[1];
$post=mass($HTTP_POST_VARS,0,"--$boundary\r\nContent-Disposition: form-data; name=\"\$perem\"\r\n\r\n\$value\r\n");
// поддержка upload
$mass=$HTTP_POST_FILES;
if ($mass){
reset($mass);
for ($i=0; $i<count($mass); $i++){
$perem=key($mass);
$filename=$mass[$perem][name];
$filetype=$mass[$perem][type];
if ($filename){
global ${"$perem"};
$filecontent=implode("",file(${"$perem"}));
$post.="--$boundary\r\nContent-Disposition: form-data; name=\"$perem\"; filename=\"$filename\"\r\nContent-Type: $filetype\r\n\r\n$filecontent\r\n";
}
}
}
$post.="--$boundary--";
$len=strlen($post)+2;
}
// формирование заголовка запроса
$message="$method $path$get HTTP/1.0\r\n";
$message.="Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*\r\n";
if ($referer){$message.="Referer: $referer\r\n";}
$message.="Accept-Language: ru\r\n";
if ($charset){$message.="Accept-Charset: $charset\r\n";}
if ($auth){$message.="Authorization: Basic $auth\r\n";}
if ($post && $boundary){$message.="Content-Type: multipart/form-data; boundary=$boundary\r\n";}
if ($post && !$boundary){$message.="Content-Type: application/x-www-form-urlencoded\r\n";}
$message.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n";
$message.="Host: $host\r\n";
if ($post){$message.="Content-length: $len\r\n";}
$message.="Connection: close\r\n";
$message.="Cache-Control: no-cache\r\n";
$message.="Pragma: no-cache\r\n";
if ($cookie){$message.="Cookie: $cookie\r\n";}
if ($post){$message.="\r\n$post\r\n";}
$message.="\r\n";
// echo $message; exit;
// запрос
if ($scheme=="http"){
$fp=fsockopen($host,$port,$errno,$errstr);
if (!$fp){echo "Нет связи с сервером. Повторите попытку позднее. $errno ($errstr).";}
}
if ($scheme=="https"){
$fp=pfsockopen("ssl://$host",$port,$errno,$errstr);
if (!$fp){echo "Невозможно соединиться с сервером. Нет поддержки SSL.";}
}
if (!$fp){exit;}
fputs($fp,$message);
while(!feof($fp)){$fgets=fgets($fp,2048); if ($fgets=="\r\n" || $fgets=="\n"){break;} $head.=$fgets;}
while(!feof($fp)){$content.=fread($fp,2048);}
fclose($fp);
// echo $head;
// echo $content;
return array($message,$head,$content);
}
############################ функция обработки адреса ###############################
function url($location,$save){
if (!$location){return;}
global $url_default,$scheme_new,$method_new,$host_new,$port_new,$path_new,$preffix_new;
// установки по умолчанию
$url=@parse_url($url_default);
$scheme_default=$url[scheme];
$host_default=$url[host];
$path_default=$url[path];
$method_default="GET";
$port_default=80;
// разбор адреса
$url=preg_replace("#://?#si","://",$location,1);
$url=@parse_url($url);
$scheme=$url[scheme];
if (!$scheme){$scheme=$scheme_new;}
if (!$scheme){$scheme=$scheme_default;}
$method=$method_new;
if (!$method){$method=$method_new;}
if (!$method){$method=$method_default;}
$host=$url[host];
if (!$host){$host=$host_new;}
if (!$host){$host=$host_default;}
$port=$url[port];
if (!$port){$port=$port_new;}
if (!$port){$port=$port_default;}
$path=$url[path];
$path=str_replace("\\","/",$path);
$path="/$path";
$path=preg_replace("'/{2,100}'si","/",$path);
// обработка /./ и /../
for ($i=0;$i<10;$i++){$path=preg_replace("#/[^\./][^/]+/\.\.?/#si","/",$path);}
for ($i=0;$i<10;$i++){$path=preg_replace("#/\.\.?/#si","/",$path);}
$get=$url[query];
$get=get($get);
$adr=$scheme."://".$host;
if (($scheme=="http" && $port!=80) || ($scheme=="https" && $port!=443)){$adr.=":".$port;}
$preffix=$adr.substr($path,0,strrpos($path,"/"))."/";
$location=$adr.$path;
if ($get){$location.="?$get";}
// обновление переменных
if ($save){
$scheme_new=$scheme;
$method_new=$method;
$host_new=$host;
$port_new=$port;
$path_new=$path;
$preffix_new=$preffix;
$location_new=$location;
}
// вывод
return array($location,$scheme,$method,$host,$port,$path,$get,$preffix);
}
############################ функция обработки ссылок ###############################
function parse($url){
global $scheme_new,$host_new,$port_new,$path_new,$preffix_new,$base_href;
// получение полного адреса
if (!$url || $url=="http://" || $url=="https://" || $url=="\\"){return $preffix_new;}
if ($base_href){$adr=$base_href;} else {$adr=$scheme_new."://".$host_new;}
if ($port_new && (($scheme_new=="http" && $port_new!=80) || ($scheme_new=="https" && $port_new!=443))){$adr.=":".$port_new;}
if (substr($url,0,1)=="/"){
$url=$adr.$url;
} elseif (substr($url,0,1)=="?"){
if ($base_href){$url=$adr.$url;} else {$url=$adr.$path_new.$url;}
} else {
$tmp=@parse_url($url);
if ($tmp[scheme]!="http" && $tmp[scheme]!="https" && $tmp[scheme]!="ftp"){$url=$preffix_new.$url;}
}
return $url;
}
################################## функция кодирования ##############################
function code($str){
if (!$str){return;}
$str=parse($str);
$str=base64_encode($str);
$str=str_replace("=","$",$str);
$code="$str?";
return $code;
}
################################ функция декодирования ##############################
function decode($code){
if (!$code){return;}
if (substr($code,0,7)=="http://" || substr($code,0,8)=="https://"){return $code;}
if (substr($code,-1)=="?"){$code=substr($code,0,-1);}
$code=str_replace("$","=",$code);
$str=base64_decode($code);
return $str;
}
################################# функция замены ссылок #############################
function replace($mass1,$mass2,$mass3,$left,$right){
global $content,$no_replace_all_array,$no_replace_left_array;
for ($i=0;$i<count($mass1);$i++){
$mass3[$i]=trim($mass3[$i]);
if ($mass3[$i]){
$sovp=0;
for ($j=0;$j<count($no_replace_all_array);$j++){if ($no_replace_all_array[$j]==$mass3[$i]){$sovp=1;}}
for ($j=0;$j<count($no_replace_left_array);$j++){if ($no_replace_left_array[$j]==substr($mass3[$i],0,strlen($no_replace_left_array[$j]))){$sovp=1;}}
if (!$sovp){
$replace=$mass3[$i];
$replace=$left.code($replace).$right;
$replace=str_replace($mass3[$i],$replace,$mass2[$i]);
$content=str_replace($mass1[$i],$replace,$content);
}
}
}
}
################################# функция обработки GET #############################
function get($get){
if (!$get){return;}
global $charset_default,$charset_new;
// декодирование HTML-мнемоник
$get=str_replace("&","&",$get);
$get=str_replace(""","\"",$get);
$get=str_replace("'","'",$get);
$get=str_replace("<","<",$get);
$get=str_replace(">",">",$get);
// url-кодирование
preg_match_all("#=([^&;]+)#is",$get,$mass);
$mass=$mass[1];
for ($i=0; $i<count($mass); $i++){
$find=$replace=$mass[$i];
$replace=urldecode($replace);
if ($charset_default){$replace=decode_charset($replace,$charset_default,$charset_new);}
$replace=urlencode(stripslashes($replace));
$get=str_replace("=$find","=$replace",$get);
}
// декодирование символов
$get=str_replace("%3A",":",$get);
$get=str_replace("%40","@",$get);
return $get;
}
######################## поиск ключей многомерного массива #########################
function keys($mass,$c,$d){
global $a,$b;
$keys=array_keys($mass);
for($i=0;$i<count($keys);$i++){
$perem=$d[$c]=$b[$a][$c]=$keys[$i];
$value=$mass[$perem];
for($j=1;$j<$c;$j++){$b[$a][$j]=$d[$j];}
if (is_array($value)){$c++; keys($value,$c,$d); $c--;} else {$a++;}
}
}
############################ функция обработки POST/COOKIE ##########################
function mass($mass,$code,$repl){
if (!$mass){return;}
global $charset_default,$charset_new;
global $a,$b;
$a=0; $b="";
// поиск ключей массива
keys($mass,1,null);
// формирование запроса
for($i=0;$i<count($b);$i++){
$perem="";
$value=$mass;
for($j=1;$j<=count($b[$i]);$j++){
if ($j==1){$perem.=$b[$i][$j];} else {$perem.="[".$b[$i][$j]."]";}
$value=$value[$b[$i][$j]];
}
if ($perem!="session_new" && $perem!="referer_new" && $perem!="method_new" && $perem!="charset_new"){
$value=urldecode($value);
if ($charset_default){$value=decode_charset($value,$charset_default,$charset_new);}
if ($code){$value=urlencode(stripslashes($value));}
$new=str_replace("\$perem",$perem,$repl);
$new=str_replace("\$value",$value,$new);
$zapr.=$new;
}
}
// декодирование символов
$zapr=str_replace("%3A",":",$zapr);
$zapr=str_replace("%40","@",$zapr);
return $zapr;
}
######################### функция конвертирования кирилицы #########################
function decode_charset($content,$code,$decode){
if (!$content || !$code || !$decode || $code==$decode){return $content;}
$charset=array("koi8-r"=>"k","windows-1251"=>"w","iso8859-5"=>"i","x-cp866"=>"a","x-mac-cyrillic"=>"m","utf-8"=>"u");
$from=$charset[$code];
$to=$charset[$decode];
if (!$from || !$to){return $content;}
if ($from=="u"){
$content=convert_utf_string($content,"w");
$content=convert_cyr_string ($content,"w",$to);
} elseif ($to=="u"){
$content=convert_cyr_string ($content,$from,"w");
$content=convert_utf_string($content,"u");
} else {
$content=convert_cyr_string ($content,$from,$to);
}
return $content;
}
########################### функция конвертирования utf-8 ##########################
function convert_utf_string($str,$type){
// w - encodes from UTF to win ; u - encodes from win to UTF
static $conv='';
if(!is_array($conv)){
$conv=array();
for ($x=128;$x<=143; $x++){
$conv['utf'][]=chr(209).chr($x);
$conv['win'][]=chr($x+112);
}
for ($x=144; $x<=191; $x++){
$conv['utf'][]=chr(208).chr($x);
$conv['win'][]=chr($x+48);
}
$conv['utf'][]=chr(208).chr(129);
$conv['win'][]=chr(168);
$conv['utf'][]=chr(209).chr(145);
$conv['win'][]=chr(184);
}
if ($type=='w'){
return str_replace($conv['utf'],$conv['win'],$str );
} elseif ($type=='u'){
return str_replace ($conv['win'],$conv['utf'],$str);
} else {
return $str;
}
}
#####################################################################################
?>