Difference between revisions of "User:Craftish Resident/php/SLRequestData"

From Second Life Wiki
Jump to navigation Jump to search
(PHP script for reading SL http request headers)
 
 
Line 6: Line 6:


slrequestdata.php
slrequestdata.php
<php>
<source lang="php">
<?php
<?php


Line 130: Line 130:


?>
?>
</php>
</source>

Latest revision as of 01:30, 18 April 2015

Optimized Second Life HTTP request header-reading class for php

Doesn't process headers unless they are used


slrequestdata.php

<?php

/* *** -------------------------------------------------- *** */
/* ***      Copyright (c) 2013 by Craftish Resident       *** */
/* *** Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) *** */
/* ***   http://creativecommons.org/licenses/by-sa/3.0/   *** */
/* *** -------------------------------------------------- *** */

class SLRequestData{
	private static $object_grid=FALSE;
	private static $object_name=FALSE;
	private static $object_key=FALSE;
	private static $object_pos=FALSE;
	private static $object_vel=FALSE;
	private static $object_rot=FALSE;
	private static $owner_key=FALSE;
	private static $owner_name=FALSE;
	private static $region_data=FALSE;
	private static $region_name=FALSE;
	private static $region_pos=FALSE;
	//
	private static $apachecheck=FALSE;
	private static $apache=FALSE;
	private static $headers;
	//
	private static function header_get($x){
		return array_key_exists($x,self::$headers)?self::$headers[$x]:'';
	}
	public static function header($x) {
		if(self::$apachecheck===FALSE){
			self::$apache=function_exists('apache_request_headers');
			self::$headers=self::$apache?apache_request_headers():$GLOBALS['HTTP_ENV_VARS'];
			self::$apachecheck=TRUE;
		}
		switch($x):default:
			return '';
		break;case 'object_grid':
			return (self::$object_grid!==FALSE)?(self::$object_grid):(self::$object_grid=self::header_get(self::$apache?'X-SecondLife-Shard':'HTTP_X_SECONDLIFE_SHARD'));
		break;case 'object_name':
			return (self::$object_name!==FALSE)?(self::$object_name):(self::$object_name=self::header_get(self::$apache?'X-SecondLife-Object-Name':'HTTP_X_SECONDLIFE_OBJECT_NAME'));
		break;case 'object_key':
			return (self::$object_key!==FALSE)?(self::$object_key):(self::$object_key=self::header_get(self::$apache?'X-SecondLife-Object-Key':'HTTP_X_SECONDLIFE_OBJECT_KEY'));
		break;case 'object_pos':
			if(self::$object_pos!==FALSE)return self::$object_pos;
			$temp1=self::header_get(self::$apache?'X-SecondLife-Local-Position':'HTTP_X_SECONDLIFE_LOCAL_POSITION');
			$temp2;
			preg_match_all('#\(([\d.]+), ([\d.]+), ([\d.]+)\)#i',$temp1,$temp2);
			return self::$object_pos=@array('x'=>(float)$temp2[1][0],'y'=>(float)$temp2[2][0],'z'=>(float)$temp2[3][0]);
		break;case 'object_vel':
			if(self::$object_vel!==FALSE)return self::$object_vel;
			$temp1=self::header_get(self::$apache?'X-SecondLife-Local-Velocity':'HTTP_X_SECONDLIFE_LOCAL_VELOCITY');
			$temp2;
			preg_match_all('#\(([\d.]+), ([\d.]+), ([\d.]+)\)#i',$temp1,$temp2);
			return self::$object_vel=@array('x'=>(float)$temp2[1][0],'y'=>(float)$temp2[2][0],'z'=>(float)$temp2[3][0]);
		break;case 'object_rot':
			if(self::$object_rot!==FALSE)return self::$object_rot;
			$temp1=self::header_get(self::$apache?'X-SecondLife-Local-Rotation':'HTTP_X_SECONDLIFE_LOCAL_ROTATION');
			$temp2;
			preg_match_all('#\(([\d.]+), ([\d.]+), ([\d.]+), ([\d.]+)\)#i',$temp1,$temp2);
			return self::$object_rot=@array('x'=>(float)$temp2[1][0],'y'=>(float)$temp2[2][0],'z'=>(float)$temp2[3][0],'s'=>(float)$temp2[4][0]);
		break;case 'owner_key':
			return (self::$owner_key!==FALSE)?(self::$owner_key):(self::$owner_key=self::header_get(self::$apache?'X-SecondLife-Owner-Key':'HTTP_X_SECONDLIFE_OWNER_KEY'));
		break;case 'owner_name':
			return (self::$owner_name!==FALSE)?(self::$owner_name):(self::$owner_name=self::header_get(self::$apache?'X-SecondLife-Owner-Name':'HTTP_X_SECONDLIFE_OWNER_NAME'));
		break;case 'region_data':
			return (self::$region_data!==FALSE)?(self::$region_data):(self::$region_data=self::header_get(self::$apache?'X-SecondLife-Region':'HTTP_X_SECONDLIFE_REGION'));
		break;case 'region_name':
			if(self::$region_name!==FALSE)return self::$region_name;
			$temp1=self::header('region_data');
			$temp2;
			preg_match_all('#([\w ]*)\s\((\d+), (\d+)\)#i',$temp1,$temp2);
			self::$region_pos=@array('x'=>(float)$temp2[2][0],'y'=>(float)$temp2[3][0]);
			return self::$region_name=@$temp2[1][0];
		break;case 'region_pos':
			if(self::$region_pos!==FALSE)return self::$region_pos;
			$temp1=self::header('region_data');
			$temp2;
			preg_match_all('#([\w ]*)\s\((\d+), (\d+)\)#i',$temp1,$temp2);
			self::$region_name=@$temp2[1][0];
			return self::$region_pos=@array('x'=>(float)$temp2[2][0],'y'=>(float)$temp2[3][0]);
		break;case 'print_r':
			return print_r(array(
				'object_grid'=>self::header('object_grid'),
				'object_name'=>self::header('object_name'),
				'object_key'=>self::header('object_key'),
				'object_pos'=>self::header('object_pos'),
				'object_vel'=>self::header('object_vel'),
				'object_rot'=>self::header('object_rot'),
				'owner_key'=>self::header('owner_key'),
				'owner_name'=>self::header('owner_name'),
				'region_data'=>self::header('region_data'),
				'region_name'=>self::header('region_name'),
				'region_pos'=>self::header('region_pos'),
			),TRUE);
		break;case 'headers':
			return print_r(self::$headers,TRUE);
		break;endswitch;
		return '';
	}
}

?>
</php>

Example:

<php>
<?php

header('Content-Type: text/plain');

include 'slrequestdata.php';

echo "Hello " . SLRequestData::header('owner_name') . "\n";

// Outputs: Hello Craftish Resident

$pos = SLRequestData::header('object_pos');
echo SLRequestData::header('object_name') . "'s z position is {$pos['z']}";

// Outputs: Object's z position is 1400.5

?>