Entity/Attribute PHP Sample Code

This page contains PHP sample code for the following tasks:

For specific information regarding each task, including excerpts from the PHP code below, click on the task.

Following is the PHP sample code:

<?php
$username = "cn=admin,o=novell";
echo "Enter Password: ";
$pwd = preg_replace('/\r?\n$/', '', `stty -echo; head -n1`);
echo "\n";
`stty echo`;
$sessionSecret = null;
$URL = getSessionURL($username, $pwd, $sessionSecret);
$strSessionURL = ereg_replace("^http://", "", $URL);
$sessionURL = strstr($strSessionURL, "/");

//Make a new entity
$postData = postRequest($sessionURL, $sessionSecret, "Entity");
echo getRequest($sessionURL, $sessionSecret, "Entity/example");
//Delete an attribute directly
echo "\n\nDeleted the State attribute\n\n";
deleteRequest($sessionURL, $sessionSecret, "Entity/example?attr=http://schemas.xmlsoap.org/ws/2005/05/identity/claims/stateorprovince");
//Update an entity: add a country attribute, add an email value, and delete the locality attribute
updateRequest($sessionURL, $sessionSecret, "Entity/example");
echo "Updated Entity to have a contain a country and another email, and deleted the locality attribute\n\n";
//Update an attribute (Noun is in the form of AuthenticatedSession/$sessionID/Entity/$entityID/Attribute/$attrID)
attributeRequest($URL, $sessionSecret, "Entity/example/Attribute/http%3A%252F%252Fschemas.xmlsoap.org%252Fws%252F2005%252F05%252Fidentity%252Fclaims%252Femailaddress");
//View the entity to see the changes
echo getRequest($sessionURL, $sessionSecret, "Entity/example");

//Delete the example entity
$deleteData = deleteRequest($sessionURL, $sessionSecret, "Entity/example");
echo "\n\n";

function attributeRequest($URL, $secret, $URI){
	$xml = 
        '<idas:AttributeChange xmlns:idas="http://www.eclipse.org/higgins/idas/rest-xml/2008/6">
		<idas:AttributeValue valType="http://www.w3.org/2001/XMLSchema#string">
                	<idas:SimpleData>quinw@hotmail.com</idas:SimpleData>
		</idas:AttributeValue>
	</idas:AttributeChange>';
	$length = strlen($xml);
	$putHeader =
	"PUT $URL/$URI HTTP/1.1\n".
	"Host: wag.bandit-project.org\n".
	(($secret) ? ("SessionSecret: " . $secret . "\n") : "") .
	"Content-Type: application/x-www-form-urlencoded\n".
	"Content-Length: $length\n".
	"Connection: Close\n\n".
	"$xml\n";
	$socket = fsockopen("wag.bandit-project.org", 80);
	fputs($socket, $putHeader);
	$content = "";
	while(!feof($socket))$content .= fgets($socket, 2048);
	return $content;
}
function updateRequest($URL, $secret, $URI){
	$xml = 
	'<idas:EntityChange xmlns:idas="http://www.eclipse.org/higgins/idas/rest-xml/2008/6">
        <!--
                Add a new attribute/value. If this attribute already exists, the new
                value(s) will be added. If not, the Attribute is created with the
                specified attribute(s)
        -->
        <idas:Attribute attrID="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/country">
		<idas:AttributeValue valType="http://www.w3.org/2001/XMLSchema#string">
                        <idas:SimpleData>us</idas:SimpleData>
                </idas:AttributeValue>
        </idas:Attribute>
	<!--
                Add a new value to an existing attribute
        -->
	<idas:AttributeChange attrID="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress">
		<idas:AttributeValue valType="http://www.w3.org/2001/XMLSchema#string">
                	<idas:SimpleData>quinw@gmail.com</idas:SimpleData>
		</idas:AttributeValue>
	</idas:AttributeChange>
        <!-- Remove an entire attribute and all its values -->
        <idas:AttributeRemove>http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality</idas:AttributeRemove>
	</idas:EntityChange>';

	$length = strlen($xml);
	$updateHeader = 
	"PUT $URL/$URI HTTP/1.1\n".
	"Host: wag.bandit-project.org\n".
	(($secret) ? ("SessionSecret: " . $secret . "\n") : "") .
	"Content-Type: application/x-www-form-urlencoded\n".
	"Content-Length: $length\n".
	"Connection: Close\n\n".
	"$xml\n";
	$socket = fsockopen("wag.bandit-project.org", 80);
	fputs($socket, $updateHeader);
	$content = "";
	while(!feof($socket)) $content .= fgets($socket, 2048);
	return($content);
}

function deleteRequest($URL, $secret, $URI){
	$deleteHeader = 
	"DELETE $URL/$URI HTTP/1.1\n".
	"Host: wag.bandit-project.org\n".
	(($secret) ? ("SessionSecret: " . $secret . "\n") : "") .
	"Connection: Close\n\n";
	$socket = fsockopen("wag.bandit-project.org", 80);
	fputs($socket, $deleteHeader);
	$content = "";
	while(!feof($socket)) $content .= fgets($socket, 2048);
	return $content;
}

function postRequest($URL, $secret, $URI){
	if($URI == "Entity")
	$xml = '<?xml version="1.0" encoding="UTF-8"?>
	<idas:Entity xmlns:idas="http://www.eclipse.org/higgins/idas/rest-xml/2008/6" entityID="example" entityType="http://www.eclipse.org/higgins/ontologies/2006/higgins#Person">
	   <idas:Attribute attrID="http://www.eclipse.org/higgins/ontologies/2008/6/higgins#entityId">
		<idas:AttributeValue valType="http://www.w3.org/2001/XMLSchema#string">
                        <idas:SimpleData>example</idas:SimpleData>
                </idas:AttributeValue>
	   </idas:Attribute>
	   <idas:Attribute attrID="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname">
		<idas:AttributeValue valType="http://www.w3.org/2001/XMLSchema#string">
                        <idas:SimpleData>Quin</idas:SimpleData>
                </idas:AttributeValue>
	   </idas:Attribute>
	   <idas:Attribute attrID="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname">
		<idas:AttributeValue valType="http://www.w3.org/2001/XMLSchema#string">
                        <idas:SimpleData>Williams</idas:SimpleData>
                </idas:AttributeValue>
	   </idas:Attribute>
	   <idas:Attribute attrID="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress">
		<idas:AttributeValue valType="http://www.w3.org/2001/XMLSchema#string">
                        <idas:SimpleData>qwilliams@novell.com</idas:SimpleData>
                </idas:AttributeValue>
	   </idas:Attribute>
	   <idas:Attribute attrID="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/stateorprovince">
		<idas:AttributeValue valType="http://www.w3.org/2001/XMLSchema#string">
                        <idas:SimpleData>Utah</idas:SimpleData>
                </idas:AttributeValue>
	   </idas:Attribute>
	   <idas:Attribute attrID="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality">
		<idas:AttributeValue valType="http://www.w3.org/2001/XMLSchema#string">
                        <idas:SimpleData>Provo</idas:SimpleData>
                </idas:AttributeValue>
	   </idas:Attribute>
	</idas:Entity>';

	$length = strlen($xml);
	$postHeader = 
	"POST $URL/$URI HTTP/1.1\n".
	"Host: wag.bandit-project.org\n".
	(($secret) ? ("SessionSecret: " . $secret . "\n") : "") .
	"Content-Type: application/x-www-form-urlencoded\n".
	"Content-Length: $length\n".
	"Connection: Close\n\n".
	"$xml\n";
	$socket = fsockopen("wag.bandit-project.org", 80);
	fputs($socket, $postHeader);
	$content = "";
	while(!feof($socket))
	{
		$content .= fgets($socket, 32768);
	}
	return $content;
}

function getRequest($URL, $secret, $URI){
	//Generate the header to get the entity
	$getHeader = 
	"GET $URL/$URI HTTP/1.1\n".
	"Host: wag.bandit-project.org\n".
	(($secret) ? ("SessionSecret: " . $secret . "\n") : "") .
	"Connection: Close\n\n";
	$socket = fsockopen("wag.bandit-project.org", 80);
	fputs($socket, $getHeader);
	$xml = "";
	while (!feof($socket)) 
	{
		$content = fgets($socket, 256);
		//If the line contains an xml tag, add it to the xml var
		if(strpos($content, ">")){
			$xml .= $content;
		}
	}
	//Return the xml var
	return($xml);
}


function getSessionURL($login, $password, &$strSessionSecret){
	$xml =
	'<?xml version="1.0" encoding="UTF-8"?>
	'<otis:Authenticate xmlns:otis="http://code.bandit-project.org/schemas/2008/otis" methodID="urn:bandit-project:otis:authmethod:1.0:usernamePassword">' .
		'<otis:AuthMaterial materialID="urn:bandit-project:otis:authmaterial:1.0:usernamePassword" mustGatherAllChildren="true">' .
			'<otis:AuthChildMaterials>' .
				'<otis:AuthMaterial materialID="urn:bandit-project:otis:authmaterial:1.0:username">' .
					'<otis:AuthMaterialValue Type="string">' . $login . '</otis:AuthMaterialValue>' .
				'</otis:AuthMaterial>' .
				'<otis:AuthMaterial materialID="urn:bandit-project:otis:authmaterial:1.0:password">' .
					'<otis:AuthMaterialValue Type="string">' . $password . '</otis:AuthMaterialValue>' .
				'</otis:AuthMaterial>' .
			'</otis:AuthChildMaterials>' .
		'</otis:AuthMaterial>' .
	'</otis:Authenticate>';

	$length = strlen($xml);
	$Host = "wag.bandit-project.org";

	// Generate the request header using an xml 
	$ReqHeader =
	"POST /otis/AuthenticatedSession HTTP/1.1\n".
	"Host: $Host\n".
	"Content-Type: application/x-www-form-urlencoded\n".
	"Content-Length: $length\n".
	"Connection: Close\n\n".
	"$xml\n";

	// Open the connection to the host
	$socket = fsockopen("wag.bandit-project.org", 80);

	// Do the POST; store in "Result"
	fputs($socket, $ReqHeader);
	while (!feof($socket)) 
	{
		$tmp = fgets($socket, 128);
		if(strpos($tmp, ">"))
		{
			$Result .= $tmp;
		}
	}

	// Parse the resultingxmlns:tns="http://www.eclipse.org/higgins/idas/rest-xml/2008/6" xml to find the session URL
	$responseDoc = new DOMDocument();
	$responseDoc->preserveWhiteSpace = false;	
	if ($responseDoc->loadXML( $Result) == FALSE)
	{
		echo ERROR1;
	}
	$sessionElement = $responseDoc->documentElement;
	$childNode = $sessionElement->firstChild;
	while ($childNode)
	{
		switch ( $childNode->nodeName)
		{
			case "otis:sessionURL":
				$strSessionURL = $childNode->nodeValue;   //set URL here
				break;
			case "otis:sessionSecret":
				$strSessionSecret = $childNode->nodeValue;   //set session secret here
				break;
			default:
				break;
		}
		$childNode = $childNode->nextSibling;
	}
	fclose($socket);
	return($strSessionURL);
}
?>