Uuid = $Uuid;
$this->HomeUri = $HomeUri;
$this->WorkingPath = realpath(dirname(__FILE__));
$this->ImagesPath = $ImagesPath;
$this->MySql = new MySql($GLOBALS["CONFIG"]["mysql"]["user"], $GLOBALS["CONFIG"]["mysql"]["pass"], $GLOBALS["CONFIG"]["mysql"]["db"], $GLOBALS["CONFIG"]["mysql"]["server"]);
//Process OpenSim header fields
foreach ($_SERVER as $k => $v)
{
if (substr($k, 5, 13) == "X_SECONDLIFE_")
{$this->OSHeaders[substr($k, 18)] = $v;}
}
//Remove cached profile images after 60 days but process this cleanup every 5 days only
if ($this->HelpValue("IMAGE.LAST.CLEANTIME") + 86400 * 5 < time())
{
$this->HelpValue("IMAGE.LAST.CLEANTIME", time());
$Files = glob($this->ImagesPath."/UserImage/*.jpg");
foreach ($Files as $File)
{
if (is_file($File) && time() - filemtime($File) >= 86400 * 60)
{unlink($File);}
}
//Also delete the cached metadata
$this->MySql->query("DELETE FROM userdata WHERE lastreq < {0}", array(time() - 86400 * 60));
}
//Process pre defined images
if (!file_exists($this->ImagesPath."/UserImage/00000000-0000-0000-0000-000000000000.jpg"))
{file_put_contents($this->ImagesPath."/UserImage/00000000-0000-0000-0000-000000000000.jpg", base64_decode($this->ErrorImage));}
if (!file_exists($this->ImagesPath."/UserImage/00000000-0000-0000-0000-000000000001.jpg"))
{file_put_contents($this->ImagesPath."/UserImage/00000000-0000-0000-0000-000000000001.jpg", base64_decode($this->BlockedImage));}
if (!file_exists($this->ImagesPath."/UserImage/00000000-0000-0000-0000-000000000002.jpg"))
{file_put_contents($this->ImagesPath."/UserImage/00000000-0000-0000-0000-000000000002.jpg", base64_decode($this->UpdateImage));}
//Do the magic
$this->LastRequest = time();
if ($this->ExistsInCache()) //User exists in cache?
{
$this->GetFromCache(); //Load everything from cache
if ($this->LastUpdate + 3600 * 4 < time())
{
$this->GetFromInternet(); //Get all the data from the users grid
if ($this->HomeGrid == "Unknown" &&
$this->FirstName == "Unknown" &&
$this->LastName == "User")
{$this->GetFromCache();}
}
}
else
{$this->GetFromInternet();}
}
//The user might be unwanted in our system. See mysql table "bans"
private function IsUnwanted($CheckList)
{
$AllBans = $this->MySql->query("SELECT * FROM bans");
while ($Ban = $AllBans->fetch())
{
for ($i = 0; $i < count($CheckList); ++$i)
{
if (stripos($CheckList[$i], $Ban["banvalue"]) !== false)
{
$this->MySql->query("UPDATE bans SET nums = {1}, lastseen = {2} WHERE banvalue = {0} LIMIT 1", array($Ban["banvalue"], $Ban["nums"] + 1, $this->OSHeaders["OBJECT_KEY"]));
return true;
}
}
}
return false;
}
private function ExistsInCache()
{
$InCache = $this->MySql->query("SELECT * FROM userdata WHERE id = {0} LIMIT 1", array($this->Uuid))->fetch();
return !empty($InCache["id"]);
}
private function GetFromCache()
{
$Cache = $this->MySql->query("SELECT * FROM userdata WHERE id = {0} LIMIT 1", array($this->Uuid))->fetch();
$this->FromJson($Cache["userdata"]);
$this->LastUpdate = $Cache["lastreq"];
if (file_exists($this->ImagesPath."/UserImage/".$this->Uuid.".jpg"))
{$this->ProfileImageBlob = file_get_contents($this->ImagesPath."/UserImage/".$this->Uuid.".jpg");}
}
//Magic function
private function GetFromInternet()
{
//First of all, we need to find out what uris the gridservices are having for this user (might be different for each user)
$this->ServiceUris = $this->RequestUserSeviceUris();
if ($this->ServiceUris !== false)
{
//IF this was successfull, we are requesting all required user info from the services
$UserInfo = $this->RequestUserInfo();
if ($UserInfo !== false)
{
$this->FirstName = $UserInfo["user_firstname"];
$this->LastName = $UserInfo["user_lastname"];
/* Ungewollte User in der Auflistung ausschließen */
if ($this->IsUnwanted(array($this->Uuid, $this->HomeUri, $this->FirstName." ".$this->LastName)))
{
$this->BirthUnix = 0;
$this->UserTitle = "Blacklisted";
$this->HomeGrid = "Somewhere";
$this->ProfileImageUuid = "00000000-0000-0000-0000-000000000001";
$this->ProfileAbout = "";
$this->HomeRegionUuid = "00000000-0000-0000-0000-000000000000";
$this->HomeRegionName = "";
$this->HomeRegionUri = "";
$this->HomeRegionPos = "";
$this->HomeRegionUuid = "00000000-0000-0000-0000-000000000000";
$this->HomeRegionName = "Unknown";
$this->HomeRegionUri = "";
$this->HomeRegionPos = "<0, 0, 0>";
$this->ProfileImageBlob = base64_decode($this->BlockedImage);
$this->AddToCache();
return;
}
//We can do more magic: Request the entire user profile, its image and all other cool stuff
$ProfileData = $this->RequestUserProfile();
$GridInfo = $this->RequestHomeGrid();
$this->BirthUnix = $UserInfo["user_created"];
$this->UserTitle = $UserInfo["user_title"];
if (empty($this->UserTitle))
{$this->UserTitle = "Unknown";}
$this->HomeGrid = $GridInfo["gridname"];
$this->ProfileImageUuid = $ProfileData["result"]["ImageId"];
$this->ProfileAbout = $ProfileData["result"]["AboutText"];
$HomeRegion = $this->RequestHomeRegion();
$this->HomeRegionUuid = $HomeRegion["uuid"];
$this->HomeRegionName = $HomeRegion["region_name"];
$this->HomeRegionUri = $HomeRegion["server_uri"];
$this->HomeRegionPos = $HomeRegion["position"];
$this->ProfileImageBlob = $this->RequestUserImageBlob();
$this->AddToCache();
return;
}
}
$this->FirstName = "Unknown";
$this->LastName = "User";
$this->BirthUnix = 0;
$this->UserTitle = "Wrong configured grid";
$this->HomeGrid = "Unknown";
$this->ProfileImageUuid = "00000000-0000-0000-0000-000000000000";
$this->ProfileAbout = "";
$this->HomeRegionUuid = "00000000-0000-0000-0000-000000000000";
$this->HomeRegionName = "";
$this->HomeRegionUri = "";
$this->HomeRegionPos = "";
$this->HomeRegionUuid = "00000000-0000-0000-0000-000000000000";
$this->HomeRegionName = "Unknown";
$this->HomeRegionUri = "";
$this->HomeRegionPos = "<0, 0, 0>";
if (file_exists($this->ImagesPath."/UserImage/".$this->Uuid.".jpg"))
{$this->ProfileImageBlob = file_get_contents($this->ImagesPath."/UserImage/".$this->Uuid.".jpg");}
else
{$this->ProfileImageBlob = base64_decode($this->ErrorImage);}
}
private function RequestUserSeviceUris()
{
$Res = HttpRequest($this->HomeUri, "get_server_urlsuserID".$this->Uuid."", "", 3, "application/xml");
if (strlen($Res) < 200)
{return false;}
$ServiceURLs = ParseXml($Res);
$ServiceURLList = array();
for ($i = 0; $i < count($ServiceURLs["params"]["param"]["value"]["struct"]["member"]); ++$i)
{
$ServiceURL = $ServiceURLs["params"]["param"]["value"]["struct"]["member"][$i];
$ServiceURLList[$ServiceURL["name"]] = $ServiceURL["value"]["string"];
}
if (count($ServiceURLList) < 7)
{return false;}
return $ServiceURLList;
}
private function RequestUserProfile()
{return json_decode(HttpRequest($this->ServiceUris["SRV_ProfileServerURI"], "{\"jsonrpc\":\"2.0\",\"id\":\"00000000-0000-0000-0000-000000000000\",\"method\":\"avatar_properties_request\",\"params\":{\"UserId\":\"".$this->Uuid."\"}}", "", 3, "application/json-rpc"), true);}
private function RequestUserInfo()
{
$UserInfos = ParseXml(HttpRequest($this->HomeUri, "get_user_infouserID".$this->Uuid."", "", 3, "application/xml"));
$UserInfosList = array();
for ($i = 0; $i < count($UserInfos["params"]["param"]["value"]["struct"]["member"]); ++$i)
{
$UserInfo = $UserInfos["params"]["param"]["value"]["struct"]["member"][$i];
$UserInfosList[$UserInfo["name"]] = $UserInfo["value"]["string"]?$UserInfo["value"]["string"]:$UserInfo["value"]["i4"];
}
if (count($UserInfosList) < 6)
{return false;}
return $UserInfosList;
}
private function RequestHomeGrid()
{return ParseXml(HttpRequest($this->HomeUri.((substr($this->HomeUri, -1, 1) == "/")?"":"/")."get_grid_info", "", "", 3), true);}
private function RequestHomeRegion()
{
$HomeInfos = ParseXml(HttpRequest($this->HomeUri, "get_home_regionuserID".$this->Uuid."", "", 3, "application/xml"));
$HomeInfosList = array();
for ($i = 0; $i < count($HomeInfos["params"]["param"]["value"]["struct"]["member"]); ++$i)
{
$HomeInfo = $HomeInfos["params"]["param"]["value"]["struct"]["member"][$i];
$HomeInfosList[$HomeInfo["name"]] = $HomeInfo["value"]["string"];
}
if (count($HomeInfosList) < 13)
{return false;}
return $HomeInfosList;
}
private function RequestUserImageBlob()
{
if (!empty($this->ProfileImageUuid) && $this->ProfileImageUuid != "00000000-0000-0000-0000-000000000000")
{
$AssetURI = $this->ServiceUris["SRV_AssetServerURI"].((substr($this->ServiceUris["SRV_AssetServerURI"], -1, 1) == "/")?"":"/")."assets/".$this->ProfileImageUuid."/data";
$AssetImageData = HttpRequest($AssetURI, "", "", 3);
$TempName = $this->MySql->id(8);
file_put_contents("binary_helper/".$TempName.".jp2", $AssetImageData);
//Should be the same for Windows and Unix... If not, only God can help you... or Google (which is the same)
shell_exec("binary_helper".DIRECTORY_SEPARATOR."magick convert binary_helper/".$TempName.".jp2 binary_helper/".$TempName.".jpg");
$AssetImageData = file_get_contents("binary_helper/".$TempName.".jpg");
@unlink("binary_helper/".$TempName.".jp2");
@unlink("binary_helper/".$TempName.".jpg");
if (strlen($AssetImageData) < 512)
{return base64_decode($this->ErrorImage);}
$SrcImg = imagecreatefromstring($AssetImageData);
$DstImg = ImageCreateTrueColor(200, 200);
imagecopyresampled($DstImg, $SrcImg, 0, 0, 0, 0, 200, 200, imageSX($SrcImg), imageSY($SrcImg));
ob_start();
imagejpeg($DstImg, null, 90);
$Res = ob_get_contents();
ob_end_clean();
}
else
{$Res = base64_decode($this->ErrorImage);}
return $Res;
}
private function AddToCache()
{
$this->MySql->query("REPLACE INTO userdata(id, userdata, lastreq)VALUES({0}, {1}, {2})", array($this->Uuid, $this->ToJson(), $this->LastRequest));
file_put_contents($this->ImagesPath."/UserImage/".$this->Uuid.".jpg", $this->ProfileImageBlob);
}
public function ToArray()
{
if (!empty($this->FirstName))
{
return array("uuid" => $this->Uuid,
"firstname" => $this->FirstName,
"lastname" => $this->LastName,
"homegrid" => $this->HomeGrid,
"birthunix" => $this->BirthUnix,
"profileimage" => $this->ProfileImageUuid,
"about" => $this->ProfileAbout,
"title" => $this->UserTitle,
"home" => $this->HomeRegionName,
"homelocation" => $this->HomeRegionPos,
"homeuri" => $this->HomeRegionUri);
}
else
{return false;}
}
public function ToJson()
{
if (!empty($this->FirstName))
{return json_encode($this->ToArray());}
else
{return false;}
}
public function FromJson($Json)
{
$Json = json_decode($Json, true);
$this->Uuid = $Json["uuid"];
$this->FirstName = $Json["firstname"];
$this->LastName = $Json["lastname"];
$this->HomeGrid = $Json["homegrid"];
$this->BirthUnix = $Json["birthunix"];
$this->ProfileImageUuid = $Json["profileimage"];
$this->ProfileAbout = $Json["about"];
$this->UserTitle = $Json["title"];
$this->HomeRegionName = $Json["home"];
$this->HomeRegionPos = $Json["homelocation"];
$this->HomeRegionUri = $Json["homeuri"];
}
public function ToLsl($Glue)
{
if (!empty($this->FirstName))
{
return implode($Glue, array($this->Uuid,
$this->FirstName,
$this->LastName,
($this->HomeGrid==null)?"Unknown":$this->HomeGrid,
($this->BirthUnix==null)?"0":$this->BirthUnix,
($this->ProfileImageUuid==null)?"00000000-0000-0000-0000-000000000000":$this->ProfileImageUuid,
base64_encode($this->ProfileAbout),
($this->UserTitle==null)?"":(is_array($this->UserTitle)?"":$this->UserTitle),
($this->HomeRegionName==null)?"":$this->HomeRegionName,
($this->HomeRegionPos==null)?"<0, 0, 0>":$this->HomeRegionPos,
($this->HomeRegionUri==null)?"":$this->HomeRegionUri));
}
else
{return false;}
}
private function HelpValue($Name, $Value = null)
{
if ($Value === null)
{
$Res = $this->MySql->query("SELECT * FROM helpvals WHERE name = {0} LIMIT 1", array($Name))->fetch();
return $Res["val"];
}
else
{$this->MySql->query("REPLACE INTO helpvals (name, val)VALUES({0}, {1})", array($Name, $Value));}
}
}
?>