0 Replies - 1179 Views - Last Post: 19 April 2011 - 07:22 PM

#1 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Wapdam Browser

Posted 19 April 2011 - 07:22 PM

Description: BrowseDownload from and Search Wapdam as if you were on regular phone browser. This script uses cURL to fetch the pages html and the DOMDocument class to parse the html to it's liking.
<?php 

#This must be set as the filename!!
define('SELF', 'index.php'); 

#Set Your Phone's User Agent Here
define('USERAGENT', 'Nokia6120c/6.51 (SymbianOS/9.2; U; Series60/3.1 Nokia6120c/6.51; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) Mozilla/5.0 AppleWebKit/413 (KHTML, like Gecko) Safari/413');

define('DEBUG', 0);

session_start();

function folder_level($url) 
{
	if(preg_match('#^([^?]+?)?(.+?)$#', $url, $matches)) {
		$array = explode('/', $matches[1]);
		if(count($array = array_filter($array, "strlen")) == 0) 
			return NULL;
		$level = array_pop($array); 
		if(preg_match('#([^.]+?)..+?$#', $level)) {
			return $_SESSION['folder']; 
		}
		else {
			$_SESSION['folder'] = rtrim($matches[1], '/'); 
			return NULL; 
		}
	} 
	return NULL;
}

function encode_page(DOMDocument &$dom) 
{
	foreach($dom->getElementsByTagName('a') as $value)
		$value->attributes->getNamedItem('href')->nodeValue = SELF . '?url=' . urlencode(base64_encode($value->attributes->getNamedItem('href')->nodeValue));
	unset($_SESSION['form_action']);
	foreach($dom->getElementsByTagName('form') as $form) {
		$_SESSION['form_action'] = strtoupper(ltrim($form->attributes->getNamedItem('action')->nodeValue, '/'));
		$form->attributes->getNamedItem('action')->nodeValue = SELF . '?url=' . urlencode(base64_encode($form->attributes->getNamedItem('action')->nodeValue));
		$ele = $dom->createElement('input', 'form');
		$ele->appendChild(new DOMAttr('value', 'form'));
		$ele->appendChild(new DOMAttr('id', 'identity'));
		$ele->appendChild(new DOMAttr('name', 'identity'));
		$ele->appendChild(new DOMAttr('type', 'hidden'));
		$form->appendChild($ele);
	}
}

function prepare_url() 
{
	$url = 'http://wapdam.com/';
	if(isset($_GET['url'])) 
	{
		$ext = ltrim(base64_decode(urldecode($_GET['url'])), '/');
		$folder = folder_level($ext);
		$url = $url . ($folder ? $folder . '/' : '') . $ext;
		$url = preg_replace('#http://wapdam.com/http://#', 'http://', $url);
	}
	elseif(isset($_GET['identity']))
	{
		handle_form();
	}
	return $url;
}

function is_downloadable_content(&$url) 
{
	if(preg_match('#jpg$|mp3$|3gp$|gif$|bmp$|mp4$#', $url))
		return true;
	elseif (preg_match('#jad$#', $url)) 
	{
		$jar = curl_init();
		curl_setopt($jar, CURLOPT_URL, $url);
		curl_setopt($jar, CURLOPT_USERAGENT, USERAGENT);
		curl_setopt($jar, CURLOPT_RETURNTRANSFER, true);
		$data = curl_exec($jar);
		curl_close($jar);
		if(preg_match("#URL:.([^s].+?)s.*?#", $data, $matches))
			$url = $matches[1];
		else
			die("Could not retrieve JAR file");
		unset($data);
		return true;
	}
	return false;
}

function get_file_name(&$url) 
{
	$array = explode('/',  $url);
	return end($array);
}

function get_file_size(&$url) 
{
	if($file = fopen($url, "r")) 
	{
		fseek($file, 0, SEEK_END);
		$size = ftell($file);
		fclose($file);
		return $size;
	}
	return NULL;
}

function handle_form()
{
	$params = NULL;
	foreach($_GET as $key => $val)
		$params .= "$key=$val&";
	if(isset($_SESSION['form_action']))
	{
		cURLRun('http://www.wapdam.com/' . $_SESSION['form_action'] . '?' . $params);
		exit();
	}
}

function cURLRun($url)
{
	/************************************/
	if(DEBUG)
		var_dump($url);
	/************************************/
	
	$waptrick = curl_init();
	curl_setopt($waptrick, CURLOPT_URL, $url);
	curl_setopt($waptrick, CURLOPT_USERAGENT, USERAGENT);
	curl_setopt($waptrick, CURLOPT_RETURNTRANSFER, true);
	
	if($downloadable = is_downloadable_content($url)) 
		curl_setopt($waptrick, CURLOPT_BINARYTRANSFER, true);
	else
		curl_setopt($waptrick, CURLOPT_REFERER, $url);
		
	if($downloadable) 
	{
		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename="" . get_file_name($url) . "";");
		header("User-Agent: " . USERAGENT);
		header("Pragma: no-cache");
		readfile($url);
	}
	else 
	{
		$data = curl_exec($waptrick);
		$dom = new DOMDocument();
		$dom->loadHTML($data);
		encode_page($dom);
		echo $dom->saveHTML();
		unset($dom, $data);
	}
	curl_close($waptrick);
}

cURLRun(prepare_url());
?>


Is This A Good Question/Topic? 0
  • +

Page 1 of 1