<?php
function follow_redirect($url){
   $redirect_url = null;

   if(function_exists("curl_init")){
	  $ch = curl_init($url);
	  curl_setopt($ch, CURLOPT_HEADER, true);
	  curl_setopt($ch, CURLOPT_NOBODY, true);
	  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	  $response = curl_exec($ch);
	  curl_close($ch);
   }
   else{
	  $url_parts = parse_url($url);
	  $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80));
	  $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
	  $request .= 'Host: ' . $url_parts['host'] . "\r\n";
	  $request .= "Connection: Close\r\n\r\n";
	  fwrite($sock, $request);
	  $response = fread($sock, 2048);
	  fclose($sock);
   }

   $header = "Location: ";
   $pos = strpos($response, $header);
   if($pos === false){
	  return false;
   }
   else{
	  $pos += strlen($header);
	  $redirect_url = substr($response, $pos, strpos($response, "\r\n", $pos)-$pos);
	  return $redirect_url;
   }
}


$info = explode("/", $_SERVER['REQUEST_URI']);
$rsn = str_replace("_", " ", $info[2]);
$rsnget = "http://hiscore.runescape.com/index_lite.ws?player=" . $rsn;
$rsn = str_replace('_', ' ', str_replace("%20", " ", $info[2]));
$hiscores = file_get_contents($rsnget);
include('hiscoressplitvars.php');
///////////////////////////////////////////////////
function text($size, $x, $y, $color, $font, $text) { 
global $done;
global $black;
imagefttext($done, $size, 0, $x -1, $y -1, $black, $font, $text);
imagefttext($done, $size, 0, $x +1, $y +1, $black, $font, $text);
imagefttext($done, $size, 0, $x -1, $y +1, $black, $font, $text);
imagefttext($done, $size, 0, $x +1, $y -1, $black, $font, $text);
imagefttext($done, $size, 0, $x, $y, $color, $font, $text);
}
//////////////////////////////////////////
$highest = array(($atklvl+$strlvl), (1.5 * $rangedlvl), (1.5 * $magiclvl));
rsort($highest, SORT_NUMERIC);
$combat = ($deflvl + $hplvl + ($prayerlvl / 2) + ($sumlvl / 2) +
(1.3 * ($highest[0])))/4;

///////////////////////////////////////////


$font = '../../sig/CarbonBlock.ttf';
$numberfont = '../../sig/SFAtarianSystemBold.ttf';




$primecharurl = "http://services.runescape.com/m=avatar-rs/" . $rsn . "/full.png";
$clanprimeurl = "http://services.runescape.com/m=avatar-rs/c=ToA8ywCJMyw/" . $rsn . "/ownclan.png";
$charimgurl = follow_redirect($primecharurl);
$clanimgurl = follow_redirect($clanprimeurl);




$done = imagecreatetruecolor(467, 138);

$black = imagecolorallocate($done, 0, 0, 0); 
$white = imagecolorallocate($done, 255, 255, 255);
$grey = imagecolorallocate($done, 215, 215, 215);

$background = imagecreatetruecolor(467, 138);

//////////////////////////////////
preg_match('#\/user\/[A-Za-z0-9_]*\/(.*)\/sig\.png#', $_SERVER['REQUEST_URI'], $bg);
$bg = @$bg[1];

$cbg = @strstr($bg, 'cbg');
if ($cbg == true) {
$color = $bg;
$r = hexdec('0x' . $color{4} . $color{5});
$g = hexdec('0x' . $color{6} . $color{7});
$b = hexdec('0x' . $color{8} . $color{9});
$userbgcolor = imagecolorallocate($done, $r, $g, $b);
imagefill($background, 0, 0, $userbgcolor);
}
else {
$yellow = imagecolorallocate($done, 255, 255, 50); 
$blue = imagecolorallocate($done, 0, 50, 204); 
$green = imagecolorallocate($done, 0, 204, 51);
$purple = imagecolorallocate($done, 102, 0, 204);
$colors = array($yellow, $blue, $green, $purple);
$backgroundcolor = $colors[rand(0, count($colors) - 1)]; 
imagefill($background, 0, 0, $backgroundcolor);
}
if ($cbg == false && strrchr($bg, '.') == ".png") {
  $userbg = imagecreatefrompng($bg);
  }
  if ($cbg == false && strrchr($bg, '.') == ".jpg" || $cbg == false && strrchr($bg, '.') == ".jpeg") {
  $userbg = imagecreatefromjpeg($bg);
  }

/////////////////////////////////


$charimg = imagecreatefrompng($charimgurl); 
$clanimg = @imagecreatefrompng($clanimgurl); 
$border = imagecreatefrompng('../../sig/border.png');
$border2 = imagecreatefrompng('../../sig/border2.png');
$skills = imagecreatefrompng('../../sig/skills.png');
$gloss = imagecreatefrompng('../../sig/gloss.png');

if (!$clanimg) {
	$clanprimeurl = "http://services.runescape.com/m=avatar-rs/c=ToA8ywCJMyw/" . $rsn . "/ownclan.png";
	$clanimgrenew = follow_redirect($clanprimeurl);
	$clanimg = imagecreatefrompng($clanimgurl);
	}
imagecopy($done, $background, 0, 0, 0, 0, 467, 138);
if (isset($userbg)) { imagecopy($done, $userbg, 0, 0, 0, 0, 467, 138); }
imagecopy($done, $gloss, 2, 1, 0, 0, 465, 134);
imagecopy($done, $skills, 8, 10, 0, 0, 265, 120);

imagecopyresized($done, $clanimg, 320, 2, 0, 0, 76, 35, 280, 126);
if ($charimgurl != "http://services.runescape.com/m=avatar-rs/default_full.png") { 
imagecopy($done, $charimg, 410, 30, 0, 0, 60, 100);
}
///////////////////////////////

$totallvltxt = "Total: " . number_format($totallvl);
$totalxptxt = "Total XP: " . number_format($totalxp);
$ranktxt = "Rank: " . number_format($totalrank);
//////////////////////////////
if ($clanimgurl != "http://services.runescape.com/m=avatar-rs/default_ownclan.png") {
imagecopy($done, $border, 0, 0, 0, 0, 467, 138);
text(15, 319, 61, $white, $font, $rsn);
text(12.5, 335, 81, $grey, $numberfont, $combat);
text(12.5, 335, 103, $grey, $numberfont, $totallvltxt);
text(12.5, 335, 116, $grey, $numberfont, $totalxptxt);
text(12.5, 335, 129, $grey, $numberfont, $ranktxt);
}
else {
imagecopy($done, $border2, 0, 0, 0, 0, 467, 138);
text(15, 320, 27, $white, $font, $rsn);
text(12.5, 335, 61, $grey, $numberfont, $combat);
text(12.5, 335, 91, $grey, $numberfont, $totallvltxt);
text(12.5, 335, 106, $grey, $numberfont, $totalxptxt);
text(12.5, 335, 121, $grey, $numberfont, $ranktxt);
}
///////////////////////////////////////////
$row1 = 13;
$row2 = 39;
$row3 = 63;
$row4 = 90;
$row5 = 113;

$col1 = 35;
$col2 = 95;
$col3 = 156;
$col4 = 215;
$col5 = 275;

$skillarray = array("atk", "str", "def", "ranged", "magic", "hp", "prayer", "sum", "slayer", "herb", "agile", "hunter", "fm", "cooking", "thief", "farming", "fishing", "mining", "rc", "wc", "craft", "fletch", "con", "smith", "dung");
$rowcnt = 1;
$colcnt = 1;
foreach ($skillarray as $skill) {
	if ($rowcnt == 6) { $rowcnt = 1; $colcnt++;}
	$rows = 'row' . $rowcnt;
	$cols = 'col' . $colcnt;
	$count01 = 1;
	while ($count01 < 3) {
	$sdigit = "$skill" . 'lvldigit' . "$count01";
	$path = "../../sig/whitenum/white" . $$sdigit  .".png";
	$skillnum = imagecreatefrompng($path);
	if ($count01 == 1) { imagecopy($done, $skillnum, $$cols, $$rows, 0, 0, 11, 14);  }
	if ($count01 == 2) { imagecopy($done, $skillnum, $$cols + 10, $$rows, 0, 0, 11, 14); }
	$count01++;
	}
	$rowcnt += 1;
}
$dungthird = @imagecreatefrompng("../../sig/whitenum/white". $dunglvldigit[2]  .".png");
if ($dunglvldigit[2] != "") { $count01 == "3"; imagecopy($done, $dungthird, $$cols + 20, $$rows, 0, 0, 11, 14); }


if ($rsn == "Donniey" || $rsn == "SSonic" || $rsn == "Shing" || $rsn == "PC_Loony" || $rsn == "Hentai_Freak") { 
$heart = imagecreatefrompng('../../sig/fav.png');
imagecopy($done, $heart, 448, 121, 0, 0, 10, 10);
 }

header("Content-Type: image/png");
 imagepng($done);
 imagedestroy($border);
 imagedestroy($skills);