<html>
 <head>
  <title>Pandora App Store</title>
 </head>
 <body>
<?php
// Setup various directory locations.
define( "kRootDir",			dirname( __FILE__ ) );
define( "kUploadDir",		kRootDir . "/upload" );
define( "kUploadingDir",	kRootDir . "/uploading" );
define( "kDownloadDir",		kRootDir . "/download" );
define( "kIconDir",			kRootDir . "/icon" );
function GrabFromAppStore( $url )
{
	$xmlDoc = new DOMDocument( );
	libxml_use_internal_errors( true );
	$xmlDoc->loadHTMLFile( $url ) or die( "Failed to loadHTMLFile" );
	
	$htmls = $xmlDoc->getElementsByTagName( "html" );
	foreach ( $htmls as $html )
	{
		$bodys = $html->getElementsByTagName( "body" );
		foreach ( $bodys as $body )
		{
			$divs = $body->getElementsByTagName( "div" );
			foreach ( $divs as $div )
			{
				$class = $div->getAttribute( "class" );
				if ( $class == "itemlist" )
				{
					$lis = $div->getElementsByTagName( "li" );
					foreach ( $lis as $li )
					{
						$onClick = $li->getAttribute( "onclick" );
						$toStrip = "location.href='..";
						if ( strpos( $onClick, $toStrip ) === 0 )
						{
							$url = substr( $onClick, strlen( $toStrip ), strlen( $onClick ) - strlen( $toStrip ) - 2 );
							echo '<p>' . 'http://apps.open-pandora.org' . $url . '</p>';
	
							$appPage = new DOMDocument( );
							$appPage->loadHTMLFile( 'http://apps.open-pandora.org' . $url );
							$appDivs = $appPage->getElementsByTagName( "div" );
							foreach ( $appDivs as $appDiv )
							{
								$appId = $appDiv->getAttribute( "id" );
								if ( $appId === "appinfo" )
								{
									$appInfoDivs = $appDiv->getElementsByTagName( "div" );
									foreach ( $appInfoDivs as $appInfoDiv )
									{
										$appClass = $appInfoDiv->getAttribute( "class" );
										if ( $appClass === "install" )
										{
											$aTags = $appInfoDiv->getElementsByTagName( "a" );
											foreach ( $aTags as $aTag )
											{
												$href = $aTag->getAttribute( "href" );
	
												if ( $href && strlen( $href ) > 0 )
												{
													$href = 'http://apps.open-pandora.org/cgi-bin/' . $href;
	
													$questionMarkPos = strpos( $href, '?' );
													$andPos = strpos( $href, '&' );
													if ( $questionMarkPos >= 0 && $andPos >= 0 )
													{
														$questionMarkPos += 1;
														$filename = trim( substr( $href, $questionMarkPos, $andPos - $questionMarkPos ) );
	
														echo '<p>filename : ' . htmlspecialchars( $filename ) . '</p>';
														echo '<p>href : ' . htmlspecialchars( $href ) . '</p>';
														$data = file_get_contents( $href );
	
														echo '<p>Data : ' . htmlspecialchars( substr( $data, 0, 100 ) ) . '</p>';
	
														$toFind = '<META HTTP-EQUIV="Refresh" CONTENT="';
														$ix = strpos( $data, $toFind );
														if ( $ix >= 0 )
														{
															$endQuote = strpos( $data, '"', $ix + strlen( $toFind ) );
															$text = substr( $data, $ix + strlen( $toFind ), $endQuote - ( $ix + strlen( $toFind ) ) );
															$parts = split( ';', $text );
															foreach ( $parts as $part )
															{
																echo '<p>Part : ' . $part . '</p>';
	
																if ( strpos( $part, 'URL=' ) === 0 )
																{
																	$newUrl = substr( $part, strlen( 'URL=' ) );
																	while ( $newUrl[ strlen( $newUrl ) - 1 ] == '_' )
																	{
																		$newUrl = substr( $newUrl, 0, strlen( $newUrl ) - 1 );
																	}
																	echo '<p>New Url : ' . $newUrl . '</p>';
																	$src = 'http://apps.open-pandora.org/cgi-bin/' . $newUrl;
																	$dst = kUploadingDir . "/" . basename( $filename );
																	echo '<p>src : ' . $src . '</p>';
																	echo '<p>dst : ' . $dst . '</p>';
	
																	if( !
																		copy(
																			$src,
																			$dst
																		)
																	)
																	{
																		echo("failed to copy file");
																	}
																}
															}
	
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
	
					// Done here.
					break;
				}
			}
		}
	}
}
GrabFromAppStore( 'http://apps.open-pandora.org/cgi-bin/viewarea.pl?Emulators' );
GrabFromAppStore( 'http://apps.open-pandora.org/cgi-bin/viewarea.pl?Games' );
GrabFromAppStore( 'http://apps.open-pandora.org/cgi-bin/viewarea.pl?Applications' );
GrabFromAppStore( 'http://apps.open-pandora.org/cgi-bin/viewarea.pl?Other' );
?>
 </body>
</html>