<?php
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\WebDriverCapabilityType;

$current_dir = getcwd();
$current_dir = str_replace("\\", "/", $current_dir);
require $current_dir.'/vendor/autoload.php';
require $current_dir.'/requires/deathbycaptcha.php';
require $current_dir."/functions.php";

$c = new \DeathByCaptcha_SocketClient('khabougharf', 'SAlutmed123');
$c->is_verbose = true;

$proxys = file($current_dir."/results/proxy-ok-raw.txt");
foreach($proxys as $proxy) {
	
	$proxyTab = explode(":",$proxy);
	$ipProxy = $proxyTab[0];
	$portProxy = $proxyTab[1];
	
	$host = 'http://localhost:4444/wd/hub'; 
	putenv("webdriver.chrome.driver=".$current_dir."/requires/chromedriver.exe");
	$capabilities = [
    	WebDriverCapabilityType::BROWSER_NAME => 'chrome',
    	WebDriverCapabilityType::PROXY => [
        	'proxyType' => 'manual',
        	'httpProxy' => $ipProxy.':'.$portProxy,
        	'sslProxy' => $ipProxy.':'.$portProxy,
    	],
	];
	$driver = RemoteWebDriver::create($host, $capabilities);

	function wait($elementBY,$content,$action = null) {
		global $driver;
		$tmpElement = null;
		switch ($elementBY) {
			case 'id':
				$d = $driver->wait()->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::id($content)));
			break;
			case 'css':
				$d = $driver->wait()->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::cssSelector($content)));
			break;
			case 'xpath':
				$d = $driver->wait()->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::xpath($content)));
			break;
			default:
			break;
		}
		if(!is_null($action)) {
			$d->click();
		} else {
			return $d;
		}
	}
	function submit() {
		global $driver;
		two();
		$driver->executeScript('$("#iSignupAction").click();');
	}
	function sendCaptcha($res) {
		$res = str_replace(' ', '', $res);
		wait('xpath',"//input[@type='text']")->sendKeys($res);
		submit();
	}
	function initCaptcha($img) {
		global $c;
		if ($captcha = $c->decode($img, DeathByCaptcha_Client::DEFAULT_TIMEOUT)) {
		    sendCaptcha($captcha['text']);
		}
	}
	
	$driver->get('https://signup.live.com/signup?lcid=1036&wa=wsignin1.0&rpsnv=13&ct=1557653977&rver=7.0.6731.0&wp=MBI_SSL&wreply=https%3a%2f%2fsway.office.com%2fauthredir%3furl%3dhttps%253a%252f%252fsway.office.com%253a443%252fmy%26hurl%3dAkcdieCWHh9C%252b66rdS4Ja2wweK9s29lr0G718z%252f4NOo%253d.tfNs%252b0rRZ4RUap4%252b3BZoGq9fI%252bm80nT3lQ%252bR2gVcXB8%253d%26ipt%3d0%26sn%3dalternate%26si%3d1%26wctx%3dfa13f646-e2b9-4a50-839a-3270680080b7&lc=1036&id=296906&lw=1&fl=easi2&wsucxt=2&lic=1&uaid=6dba73cf4277463592d526c5f2d7bfef');
	
	$emailTXT = randomName().rand(0,100)."@hotmail.com";
	$email = wait('id','MemberName')->sendKeys($emailTXT);
	submit();

	$passwordTXT = randomPassword();
	$password = wait('id','PasswordInput')->sendKeys($passwordTXT);
	submit();

	two();
	$BirthDay = new WebDriverSelect($driver->findElement(WebDriverBy::id('BirthDay')));
	$BirthDay->selectByValue(rand(1,31));
	$BirthMonth = new WebDriverSelect($driver->findElement(WebDriverBy::id('BirthMonth')));
	$BirthMonth->selectByValue(rand(1,12));
	$BirthYear = new WebDriverSelect($driver->findElement(WebDriverBy::id('BirthYear')));
	$BirthYear->selectByValue(rand(1968,2008));
	submit();
	sleep(5);
	
	$imgCaptcha = wait('xpath',"//img[@class='text-body']")->getAttribute("src");
	initCaptcha($imgCaptcha);
	file_put_contents($current_dir.'/results/boites.txt', $emailTXT.';'.$passwordTXT."\r\n", FILE_APPEND | LOCK_EX);
}