John Tiernan

 
Navigation

Home
About
Computing Projects
Microcontroller Platforms
Art
Blog
Demos
Everything Else
Contact Me

Username:

Password:


Forgotten password? | Register!
Code Sample - Extension Converter
<?php
ob_start();
//echo "<h6>Input:</h6>\n";
if(isset($_POST['input']))
{
	if(isset($_POST['source']))
	{
		$source = strip_tags($_POST['source']);
	}
	else
	{
		$source = "signature";
	}
	$input = $_POST['input'];
	$input = stripslashes($input);
	
	//echo "<hr />" . $input . "<hr />";
	
	//Change method depending on original extension. Was designed to handle more than signature
	if($source=="signature")
	{
		//Define values that need to be changed
		$inArray = array(0=>"<a",1=>"</a>",
			2=>"<strong>",3=>"</strong>",
			4=>"<b>",5=>"</b>",
			6=>"<i>",7=>"</i>",
			8=>"<u>",9=>"</u>",
			10=>"<ul>",11=>"</ul>",
			12=>"<li>",13=>"</li>",
			14=>"<acronym",15=>"</acronym>");
			
		$outArray = array(0=>"[a",1=>"[/a]",2=>"[strong]",
			3=>"[/strong]",4=>"[b]",
			5=>"[/b]",6=>"[i]",7=>"[/i]",
			8=>"[u]",9=>"[/u]",10=>"[ul]",
			11=>"[/ul]",12=>"[li]",
			13=>"[/li]",14=>"[acronym",
			15=>"[/acronym]");
			
		//Replace relevant values in original data to avoid issues during parsing
		$input = str_replace("<name>","<entry><name>",$input);
		$input = str_replace("</value>","</value></entry>",$input);
		$input = str_replace("<signatures>","<?xml version=\"1.0\" 
				encoding=\"utf-8\"?>\n<signatures
				name=\"sig\"><signature id=\"text\">",$input);
		$input = str_replace("</signatures>",
				"</signature></signatures>",$input);
		//$input = str_replace("<signatures>","<?xml 
				version=\"1.0\" encoding=\"utf-8\"><signatures>",$input);
		$input = str_replace("=\"","='",$input);
		$input = str_replace("\"","'",$input);
		$input = str_replace("&","and",$input);
		$input = str_replace($inArray,$outArray,$input);

		//Load XML to be converted
		$xml = simplexml_load_string($input);
		
		//echo $input;
		
		//echo $xml->getName() . "<br />";

		//$data = array(0 => array("body" => "body","author" => "author","date" => "date"));
		
		//For each item in data, add to array
		$pos = 0;
		$data = null;
		foreach($xml->children() as $child)
		{
			//echo $child->getName() . "<br />";
			foreach($child->children() as $spawn)
			{
				$data[$pos] = array("name" => $spawn->name,
						"value" => $spawn->value);
				//echo $spawn->getName() . "-" . $spawn->name . "<br />";
				$pos++;
			}
		}
		
		//Debugging junk
		//echo "<h6>Output (Midway):</h6>\n";
		/*for($i=0;$i<count($data);$i++)
		{
			echo $data[$i]["name"] . "<br />\n" . $data[$i]["value"]. "<hr />\n";
		}*/
		
		//Generate header of new file
		//echo "<h6>Output (Final?)</h6>\n";
		$output = "<?xml version=\"1.0\"?>\n<RDF:RDF
			xmlns:NS1=\"http://clippings.mozdev.org/ns/rdf#\"
			\nxmlns:NC=\"http://home.netscape.com/NC-rdf#\"
			\nxmlns:RDF=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n";
		
		//Generate data based on old file.
		$id = array(0 => "");
		
		//Define data for reverting data (After earlier change for protection)
		$preArray = array(0=>"[a",1=>"[/a]",
				2=>"[strong]",3=>"[/strong]",4=>"[b]",
				5=>"[/b]",6=>"[i]",7=>"[/i]",
				8=>"[u]",9=>"[/u]",10=>"[ul]",
				11=>"[/ul]",12=>"[li]",13=>"[/li]",
				14=>"[acronym",15=>"[/acronym]");
				
		$postArray = array(0=>"<a",1=>"</a>",
				2=>"<strong>",
				3=>"</strong>",
				4=>"<b>",5=>"</b>",
				6=>"<i>",7=>"</i>",
				8=>"<u>",9=>"</u>",
				10=>"<ul>",11=>"</ul>",
				12=>"<li>",13=>"</li>",
				14=>"<acronym",15=>"</acronym>");
				
		for($i=0;$i<count($data);$i++)
		{
			$letter = chr($i + 65);
			$id[$i] = $letter . "sq6d2";
			$o = str_replace("[a","<a",$data[$i]['value']);
			$o = str_replace($preArray,$postArray,$o);
			$output .= "  <NS1:clipping RDF:about=\"rdf:#\$".$id[$i]."\"
					\nNS1:name=\"".htmlspecialchars($data[$i]["name"])."\"
					\nNS1:text=\"".$o."\" />\n";
		}
		
		$output .= "  <RDF:Seq RDF:
			about=\"http://clippings.mozdev.org/rdf/user-clippings-v2\">\n";
			
		//Create list of items for the extension
		//This section may be adding duplicates of data
		for($i=0;$i<count($id);$i++)
		{
			$output .= "<RDF:li RDF:resource=\"rdf:#\$".$id[$i]."\"/>\n";
		}
		
		//Final lines
		$output .= "  </RDF:Seq>\n</RDF:RDF>";
		
  		//Prepare to provide results for download by sending new headers
    	header('Content-type: text/plain');
    	header('Content-Disposition: attachment; filename="result.rdf"');
    
    	//Show contents of file for viewing
    	echo($output);
	    
	    // stop processing the page
	    exit;
	}
}else{
	//If there is no data, do nothing
	echo "<h3>Error: No data submitted</h3>";
}

//Empty buffer to allow changing of headers
ob_end_flush();
?>
		
 

© John Tiernan 2019 | This site uses 960 Grid System as a layout support. | Valid XHTML 1.0 Strict | Valid CSS