无聊手抖写的,PHP最适合用来扒站,做字符串处理了.....

<?php
	class PMail {
		private $mail;
		private $check;
		private $first;
		private $second;

		private function imap_decode($text)
		{
			$text = preg_match("/=\?([\w\-]+)\?(\w)\?([\w\+\/=]+)\?=/i", $text, $preg);
			$type = $preg[1];
			$code = $preg[2];
			$text = $preg[3];
			if (stristr($code, 'B')){
				$text = base64_decode($text);
			} else if (stristr($code, 'Q')) {
				$text = imap_qprint($text);
			}
			if (stristr($type, 'UTF-8')){
				return iconv('UTF-8', 'GB2312', $text);
			} else {
				return $text;
			}
		}

		public function mail_connect($addr, $user, $pass)
		{
			echo "Connection mail server ...\n";
			$this->mail = imap_open($addr, $user, $pass);
			echo "Connected to mail server!\n";
		}

		public function mail_disconnect()
		{
			echo "Disonnection mail server ...\n";
			if ($this->mail) imap_close($this->mail);
			echo "Disonnected to mail server!\n";
		}

		public function mail_check()
		{
			echo "Check mails ...\n";
			$this->check = imap_check($this->mail);
			echo "Current mails: {$this->check->Nmsgs}\n";
			return $this->check->Nmsgs;
		}
		
		public function mail_check_1()
		{
			echo "First check mails ...\n";
			$this->check = imap_check($this->mail);
			echo "Current mails: {$this->check->Nmsgs}\n";
			$first = $this->check->Nmsgs;
		}
		
		public function mail_check_2()
		{
			echo "Second check mails ...\n";
			$this->check = imap_check($this->mail);
			echo "Current mails: {$this->check->Nmsgs}\n";
			$second = $this->check->Nmsgs;
		}
		
		public function get_attach()
		{
			echo "Fetch mail structure ...\n";
			$struct = imap_fetchstructure($this->mail, $this->check->Nmsgs);
			$count = 2;
			
			echo "Fetch mail body ...\n";
			$body = imap_fetchbody($this->mail, $this->check->Nmsgs, $count);
			if (!strlen($body)) {
				echo "No fetch attach file!\n";
				return 0;
			}
			echo "Decode attach file ...\n";
			$body = imap_base64($body);
			return $body;
		}

		public function __construct($addr, $user, $pass)
		{
			$this->mail_connect($addr, $user, $pass);
		}

		public function __destruct()
		{
			$this->mail_disconnect();
		}
	}

	function curl_getdata($array){
		$c = curl_init($array['url']);
		if (isset($array['post']))
			curl_setopt($c, CURLOPT_POST, $array['post']);
		if (isset($array['nobody']))
			curl_setopt($c, CURLOPT_NOBODY, $array['nobody']);
		if (isset($array['header']))
			curl_setopt($c, CURLOPT_HEADER, $array['header']);
		if (isset($array['httpheader']))
			curl_setopt($c, CURLOPT_HTTPHEADER, $array['httpheader']);
		if (isset($array['returntransfer']))
			curl_setopt($c, CURLOPT_RETURNTRANSFER, $array['returntransfer']);
		if (isset($array['cookie']))
			curl_setopt($c, CURLOPT_COOKIE, $array['cookie']);
		if (isset($array['referer']))
			curl_setopt($c, CURLOPT_REFERER, $array['referer']);
		if (isset($array['postfields']))
			curl_setopt($c, CURLOPT_POSTFIELDS, $array['postfields']);
		ob_start();
		$h = '';
		$b = curl_exec($c);
		$e = curl_getinfo($c, CURLINFO_HTTP_CODE);
		$o = ob_get_contents();
		ob_end_clean();
		curl_close($c);
		if ($array['header']){
			$i = strpos($o, "\r\n\r\n");
			$h = substr($o, 0, $i);
			$b = substr($o, $i);
		}
		return array('stat' => $e, 'head' => $h, 'body' => $b);
	}

	function get_sid($user, $pass){
		$res = curl_getdata(array(
			'url'		 => 'http://www.htcdev.com',
			'post'		 => 1,
			'header'	 => 1,
			'httpheader' => array('Content-Type: application/x-www-form-urlencoded'),
			'postfields' => "username={$user}&".
							"password={$pass}&".
							'ACT=9&'
		));

		if ($res['stat'] = 200){
			if (preg_match("/Set-Cookie: exp_sessionid=(\w+;) /", $res['head'], $regs)) {
				return $regs[1];
			} else {
				echo "Login failed!\n";
				return -2;
			}
		} else {
			echo "HTTP transfer error, status code: {$re['status']}.\n";
			return -1;
		}
	}

	function set_cid($sid, $text){
		$res = curl_getdata(array(
			'url'		 => 'http://www.htcdev.com/bootloader/process-unlock-key',
			'post'		 => 1,
			'header'	 => 1,
			'httpheader' => array('Content-Type: application/x-www-form-urlencoded', 'Cookie: exp_sessionid='.$sid),
			'postfields' => 'Submit=Submit&'.
							'bootloader_text='.$text
		));

		if ($res['stat'] = 200){
		//	URL=/bootloader/token-submitted
		//	URL=/bootloader/unlock-failure/page=fail&code=140&msg=Token Decryption Fail
			if (preg_match("/URL=\/bootloader\/token-submitted/", $res['body'], $regs)) {
				return 0;
			}
			if (preg_match("/URL=\/bootloader\/unlock-failure\/page=fail/", $res['body'], $regs)) {
				if (preg_match("/&msg=(.+)\"/", $res['body'], $regs)) {
					return urldecode($regs[1]);
				}
			}
		} else {
			echo "HTTP transfer error, status code: {$re['status']}.\n";
			return -1;
		}
		echo $res['body'];
	}

	function get_token($user, $pass, $txt){
		echo "Get User ID ...\n";
		$sid = get_sid($user, $pass);
		if (!$sid){
			echo "Get user id failed, check your username & password please!\n";
			return -1;
		}
		echo "User ID: ".$sid."\n";

		echo "Submit token ...\n";
		$cid = set_cid($sid, $txt);
		if ($cid != 0){
			echo "Submit token failed!\n";
			echo "$cid";
			return -2;
		}
		echo "Token submitted!\n";
		return 0;
	}
	
	exec('fastboot oem get_identifier_token');

	fgets(STDIN);

	$mail_addr = "{imap.qq.com:993/imap/novalidate-cert/ssl}";
	$mail_user = "Admin@TinyStrong.Com";
	$mail_pass = "XXXX";
	$user ="TinyStrong";
	$pass ="XXXX";
	$text = file_get_contents("token.txt");
	
	$mail = new PMail($mail_addr, $mail_user, $mail_pass);
	$mail->mail_check();
	get_token($user, $pass, $text);
	echo "If you received HTCDEV mail, then press ENTER to continue ...";
	fgets(STDIN);
	sleep(3);
	$mail->mail_check();
	echo "Get mail attach file ...\n";
	$body = $mail->get_attach();
	echo "Write unlock.bin file to disk ...\n";
	file_put_contents ('unlock.bin', $body);
	echo exec('fastboot flash unlocktoken unlock.bin');
	echo "Successed, press ENTER to continue ...";
	fgets(STDIN);
?>

标签: PHP, HTC, 解锁