Hey guys gals, I just switched from hosting from my home to a dedicated hosting firm. One of my custom scripts is now broke, why might you ask? Because they don't allow url_fopen. I can understand this, as it is a problematic security flaw in php. Ok, they suggested using cUrl to solve the issues with my script. I don't have any experience with cUrl, so I am leaning on the great community of XOOPS to help a fellow Xoopser out. I am going to provide the existing code so that you can look at it and ponder on it some. If there is a class within XOOPS that can handle the incompatibilities that url_fopen is causing on my script, could someone point me in the right direction on where I might find it, and maybe an example of usage.
Guys/gals, I really appreciate your time and any answers or solutions you might have to help me out.
Here is the code that I am currently using. (which doesn't work... obviously... LOL)
Basically what this portion of the script does is fetch data from Gamespy's stats server and creates an xml file from it.
I then parse the code and stick the values in the database so that I can display the stats in the users profile. Thats the brief explanation.
include("config.php");
if (isset($_GET['type'])){
$type = $_GET['type'];
if($type != "png" && $type != "gif" && $type != "jpg" && $type != "jpeg"){
$type = "jpg"; }
}else{
$type = "jpg";
}
//#########################################################################################################################################//
header("Expires: ".date("D, d M Y H:i:s",time()+$cachetime)." GMT");
//#########################################################################################################################################//
header('Content-Control: cache');
if ($type == "jpg" || $type == "jpeg"){
header("Content-type: image/jpeg"); // expire in 6 hours; );
imagejpeg($image_out,"",$jpeg_quality);
}elseif ($type == "gif"){
header("Content-type: image/gif"); // expire in 6 hours; );
imagegif($image_out);
}else{
header('Content-Type: image/png'); // expire in 6 hours;
imagepng($image_out);
}
header("Content-Length: " . ob_get_length());
//header("Content-type: image/png");
function xml2array($text) {
$reg_exp = '/<(.*?)>(.*?)</\1>/s';
preg_match_all($reg_exp, $text, $match);
foreach ($match[1] as $key=>$val) {
if ( preg_match($reg_exp, $match[2][$key]) ) {
$array[$val][] = xml2array($match[2][$key]);
} else {
$array[$val] = $match[2][$key];
}
}
return $array;
}
//handy dandy little function for making the string centered, and making it fit.
function imagestringcentered ($img,$font,$xoffset,$cy,$text,$color) {
while (strlen($text) * imagefontwidth($font) > imagesx($img)-$xoffset) {
if ($font > 1) { $font--; }
else { break; }
}
imagestring($img,$font,(imagesx($img)-$xoffset) / 2 - strlen($text) * imagefontwidth($font) / 2 + $xoffset,$cy,$text,$color);
}
//Map converter
function mapconverter($mapnumber) {
$mapname[0] = "Kubra Dam";
$mapname[1] = "Mashtuur City";
$mapname[2] = "Operation Clean Sweep";
$mapname[3] = "Zatar Wetlands";
$mapname[4] = "Strike at Karkand";
$mapname[5] = "Sharqi Peninsula";
$mapname[6] = "Gulf of Oman";
$mapname[100] = "Daqing Oilfields";
$mapname[101] = "Dalian Plant";
$mapname[102] = "Dragon Valley";
$mapname[103] = "FuShe Pass";
$mapname[104] = "Wake Island";
$mapname[105] = "Songhua Stalemate";
//Special Forces Maps
$mapname[300] = "Devil's Perch (BF2SF)";
$mapname[301] = "The Iron Gator (BF2SF)";
$mapname[302] = "Night Flight (BF2SF)";
$mapname[303] = "Warlord (BF2SF)";
$mapname[304] = "Leviathan (BF2SF)";
$mapname[305] = "Mass Destruction (BF2SF)";
$mapname[306] = "Surge (BF2SF)";
$mapname[307] = "Ghost Town (BF2SF)";
//Euro Forces Maps (EF)
$mapname[10] = "Operation Smoke Screen (EF)";
$mapname[11] = "The Great Wall of China (EF)";
$mapname[110] = "Tabara Quarry (EF)";
//Armoured Fury Maps (AF)
/*
# US Theatre
"midnight_sun" : "200",
"operationroadrage" : "201",
"operationharvest" : "202",
*/
$mapname[200] = "Midnight Sun (AF)";
$mapname[201] = "Operation Road Rage (AF)";
$mapname[202] = "Operation Harvest (AF)";
return $mapname[$mapnumber];
}
//Kit converter
function kitconverter($kitnumber) {
$kitname[0] = "Anti-Tank";
$kitname[1] = "Assault";
$kitname[2] = "Engineer";
$kitname[3] = "Medic";
$kitname[4] = "Spec-Ops";
$kitname[5] = "Support";
$kitname[6] = "Sniper";
return $kitname[$kitnumber];
}
//Vehicle Converter
function vehicleconverter($vehiclenumber) {
$vehiclename[0] = "Tank";
$vehiclename[1] = "Plane";
$vehiclename[2] = "Air Defense";
$vehiclename[3] = "Helicopter";
$vehiclename[4] = "Transport";
$vehiclename[5] = "Artillery";
$vehiclename[6] = "Ground Defense";
return $vehiclename[$vehiclenumber];
}
//Weapon Converter
function weaponconverter($weaponnumber) {
$weaponname[0] = "Assault Rifles";
$weaponname[1] = "Grenade Launchers";
$weaponname[2] = "Carbines";
$weaponname[3] = "Light Machine Guns";
$weaponname[4] = "Sniper Rifles";
$weaponname[5] = "Pistols";
$weaponname[6] = "Rockets";
$weaponname[7] = "Submachine Guns";
$weaponname[8] = "Shotguns";
$weaponname[9] = "Knife";
$weaponname[10] = "Defibrillator";
$weaponname[11] = "Explosives";
$weaponname[12] = "Hand Grenade";
$weaponname[13] = "Tear Gas";
return $weaponname[$weaponnumber];
}
$pid = $_GET["id"];
$mode = $_GET["image"];
$theme = $_GET["theme"];
if ($mode == "") {
$mode = $defaultmode;
}
if ($pid == "") {
/*$error = "No PID Specified";
$mode = "error";
include("themes/baseline/theme.php");
exit;*/
$pid = $defaultpid; //Player ID
}
if ($AllowedPID[0] != "any") {
if (!in_array($pid, $AllowedPID)) {
$error = "PID $pid Not Authorized";
$mode = "error";
include("themes/baseline/theme.php");
exit;
}
}
$cachefilename = $statdir.$pid.".xml";
if ((!file_exists($cachefilename)) || ((time() - filemtime($cachefilename)) > $cachetime)) {
//$filename = "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?weapon=".$weaponnumbers."&kit=".$kitnumbers."&map=".$mapnumbers."&army=".
$filename = "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=per*,cmb*,twsc,cpcp,cacp,dfcp,kila,heal,rviv,rsup,rpar,tgte,dkas,dsab,cdsc,rank,cmsc,kick,kill,deth,suic,ospm,klpm,klpr,dtpr,bksk,wdsk,bbrs,tcdr,ban,dtpm,lbtl,osaa,vrk,tsql,tsqm,tlwf,mvks,vmks,mvn*,vmr*,fkit,fmap,fveh,fwea,wtm-,wkl-,wdt-,wac-,wkd-,vtm-,vkl-,vdt-,vkd-,vkr-,atm-,awn-,alo-,abr-,ktm-,kkl-,kdt-,kkd-";
//$armynumbers."&vehicle=".$vehiclenumbers."&pid=".$pid."&info=".$infotoget.",rank";
ini_set("user_agent","GameSpyHTTP/1.0");
$fp = fopen ($filename, "r");
if (!$fp) {
//Old Error Handler left incase someone wants it to do this instead of just reading the cache
/*$error = "BF2 Stats Site Down.";
$mode = error;
include("themes/baseline/theme.php");
exit;
*/
if (!file_exists($cachefilename)) {
$error = "BF2Web Down and no cache file exists.";
$mode = error;
include("themes/".$theme."/theme.php");
exit;
}
//read the cache for the stats
$xmlfile = @fopen($cachefilename, "r");
if (!$xmlfile) {
$error = "Error Reading $cachefilename";
$mode = "error";
include("themes/".$theme."/theme.php");
exit;
}
while(($xmlfile)) {
$xmldoc .= fread($xmlfile,4096);
}
fclose($xmlfile);
$thestats = xml2array($xmldoc);
//end code for reading stat cache
$namedstats = $thestats['stats'][0]['info'][0];
$namedawards = $thestats['stats'][0]['awards'][0];
$namedleader = $thestats['stats'][0]['leaderboard'][0];
$namedunlocks = $thestats['stats'][0]['unlocks'][0];
$namedmaps = $thestats['stats'][0]['maps'][0];
//end code for reading stat cache
} else {
$_xml ="<?xml version="1.0"?>rn <stats>rn <info>rn";
while (($fp)) {
$content = fgets( $fp, 4096 );
//get names
if (ereg("Et107", $content) == true ) {
$error = "Error Invalid PID";
$mode = "error";
include("themes/".$theme."/theme.php");
exit;
}
if (ereg("Htpid", $content) == true ) {
$statnames = explode("t",$content); //load up the names of the stats so that they can be used for the naming of the array
}
//real stats
//start the first part of the xml file
if (ereg($pid, $content) == true ) {
$stats = explode("t",$content); //make the array with the stats
//it goes to one less than the count becuase the last one is one i added since the last one always gets messed up for some reason
//it also skips the first one because that is H (not a stat) from using explode it just picks this one up
for($i = 1; $i<count($stats)-1; $i++ ) {
$namedstats[$statnames[$i]] = "$stats[$i]"; //add the stat into a named array so its more easily configured.
//begin xml generation code
$_xml .="t ".$statnames[$i].">" . $stats[$i] . "</".$statnames[$i].">rn";
}
}
}
fclose ($fp);
$_xml .=" </info>rn <awards>rn";
}
//award part
$awardfilename = "http://bf2web.gamespy.com/ASP/getawardsinfo.aspx?pid=".$pid;
$afp = @fopen ($awardfilename, "r");
if (!$afp) {
} else {
while ( ($afp)) {
$content = fgets( $afp, 4096 );
if (ereg("Dt([0-9]{7})t([0-9]*)", $content, $awards)) {
//echo "1:".$awards[1]." 2:".$awards[2]."n";
$namedawards[$awards[1]] = $awards[2]; //add the stat into a named array so its more easily configured.
//begin xml generation code
$_xml .="t <".$awards[1].">" . $awards[2] . "</".$awards[1].">rn";
}
}
}
fclose ($afp);
$_xml .=" </awards>rn <maps>rn";
######################################################################################################################################
//maps part
$mapfilename = "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=mtm-,mwn-,mls-";
$mfp = @fopen ($mapfilename, "r");
while (($mfp)) {
$mapcontent = @fgets( $mfp, 4096 );
//get names
if (ereg("Et107", $mapcontent) == true ) {
$error = "Error Invalid PID";
$mode = "error";
include("themes/".$theme."/theme.php");
exit;
}
if (ereg("Htpid", $mapcontent) == true ) {
$mapnames = explode("t",$mapcontent); //load up the names of the stats so that they can be used for the naming of the array
}
//real stats
//start the first part of the xml file
if (ereg($pid, $mapcontent) == true ) {
$mapstats = explode("t",$mapcontent); //make the array with the stats
//it goes to one less than the count becuase the last one is one a added since the last one always gets messed up for some reason
//it also skips the first one because that is H (not a stat) from using explode it just picks this one up
for($a = 1; $a<count($mapstats)-1; $a++ ) {
$namedmaps[$mapnames[$a]] = "$mapstats[$a]"; //add the stat into a named array so its more easily configured.
//begin xml generation code
$_xml .="t ".$mapnames[$a].">" . $mapstats[$a] . "</".$mapnames[$a].">rn";
}
}
}
fclose ($mfp);
$_xml .=" </maps>rn <unlocks>rn";
##############################################################################################################################
//Unlocks part
$unlocksfilename = "http://bf2web.gamespy.com/ASP/getunlocksinfo.aspx?pid=".$pid;
$ufp = @fopen ($unlocksfilename, "r");
if (!$ufp) {
} else {
while (($ufp)) {
$content = fgets( $ufp, 4096 );
if (ereg("Dt([0-9]{2}|[0-9]{3})t([ns])", $content, $unlocks)) {
$namedunlocks[$unlocks[1]] = $unlocks[2]; //add the stat into a named array so its more easily configured.
//begin xml generation code
$_xml .="t <".$unlocks[1].">" . $unlocks[2] . "</".$unlocks[1].">rn";
}
}
}
fclose ($ufp);
$_xml .=" </unlocks>rn <leaderboard>rn";
//end awards
//leaderboard part
$leaderfilename = "http://bf2web.gamespy.com/ASP/getleaderboard.aspx?pid=".$pid."&type=score&id=overall";
$lfp = @fopen ($leaderfilename, "r");
if (!$lfp) {
} else {
while (($lfp)) {
$content = fgets( $lfp, 4096 );
if (ereg("Htntpid", $content) == true ) {
$leadernames = explode("t",$content); //load up the names of the stats so that they can be used for the naming of the array
}
//real stats
//start the first part of the xml file
if (ereg($pid, $content) == true ) {
$leader = explode("t",$content); //make the array with the stats
//it also skips the first one because that is H (not a stat) from using explode it just picks this one up
for($i = 1; $i<count($leader); $i++ ) {
if ($i == (count($leader)-1)) {
//this gets rid of the line break the last one has
$leader[$i] = str_replace("n", "", $leader[$i]);
$leadernames[$i] = str_replace("n", "", $leadernames[$i]);
}
$namedleader[$leadernames[$i]] = "$leader[$i]"; //add the stat into a named array so its more easily configured.
//begin xml generation code
$_xml .="t ".$leadernames[$i].">" . $leader[$i] . "</".$leadernames[$i].">rn";
}
}
}
}
fclose ($lfp);
//end leaderboard
$_xml .=" </leaderboard>rn </stats>rn</xml>";
$xmlcachefile = @fopen($cachefilename, "w");
if (!$xmlcachefile) {
$error = "Error Opening $cachefilename for writing";
$mode = "error";
include("themes/".$theme."/theme.php");
exit;
}
if(fwrite($xmlcachefile, $_xml) === False) {
$error = "Error Writing $cachefilename";
$mode = "error";
include("themes/".$theme."/theme.php");
exit;
}
fclose($xmlcachefile);
} else {
//read the cache for the stats
$xmlfile = @fopen($cachefilename, "r");
if (!$xmlfile) {
$error = "Error Reading $cachefilename";
$mode = "error";
include("themes/".$theme."/theme.php");
exit;
}
while(($xmlfile)) {
$xmldoc .= fread($xmlfile,4096);
}
fclose($xmlfile);
$thestats = xml2array($xmldoc);
//end code for reading stat cache
$namedstats = $thestats['stats'][0]['info'][0];
$namedawards = $thestats['stats'][0]['awards'][0];
$namedleader = $thestats['stats'][0]['leaderboard'][0];
$namedunlocks = $thestats['stats'][0]['unlocks'][0];
$namedmaps = $thestats['stats'][0]['maps'][0];
}
This code also includes a security measure to prevent people from getting the stats without having a membership, which is handy.
If you find any solutions to the problem, please get ahold of me. I will definately add you to the credits.
Thanks again fellow Xoopsers, I really appreciate your help.
Tim