|
<?php |
|
define("SESSION_STAGE_START",0); |
|
define("SESSION_STAGE_VERIFY",1); |
|
define("SESSION_STAGE_FINISHED",2); |
|
define("SESSION_STAGE_ERROR",-1); |
|
|
|
function cleanNumber($number) { |
|
$cleanNumber = preg_replace("/\(0\)/", "", $number); |
|
$cleanNumber = preg_replace("/[^0-9\+]/", "", $cleanNumber); |
|
$cleanNumber = preg_replace("/^00/", "+", $cleanNumber); |
|
$cleanNumber = preg_replace("/^0/", "+49", $cleanNumber); |
|
$cleanNumber = preg_replace("/^([0-9])/", "+$1", $cleanNumber); |
|
if (strlen($cleanNumber) !== 14) return -1; |
|
return $cleanNumber; |
|
} |
|
|
|
function sendSms($recipient, $message) { |
|
$c = curl_init(); |
|
curl_setopt($c,CURLOPT_URL, 'https://api.sipgate.com/v1/sessions/sms'); |
|
curl_setopt($c,CURLOPT_HTTPHEADER, array( |
|
'Authorization: Bearer '.$_ENV['TOKEN'], |
|
'Content-Type: application/json', |
|
'Accept: application/json')); |
|
curl_setopt($c,CURLOPT_POST,true); |
|
curl_setopt($c,CURLOPT_POSTFIELDS, json_encode(array( |
|
"smsId"=>"s0", |
|
"recipient"=> $recipient, |
|
"message"=> $message |
|
))); |
|
$result = curl_exec($c); |
|
$resultcode = curl_getinfo($c,CURLINFO_HTTP_CODE); |
|
curl_close($c); |
|
} |
|
|
|
$stage = !!$_POST['tel'] + !!$_POST['verify']; |
|
session_start(); |
|
$verified = false; |
|
$error = "Es ist ein Fehler aufgetreten"; |
|
switch ($stage) { |
|
case SESSION_STAGE_START: |
|
$_SESSION["number"] = null; |
|
$_SESSION["verification"] = rand(10000, 99999); |
|
break; |
|
case SESSION_STAGE_VERIFY: |
|
$tel = cleanNumber($_POST['tel']); |
|
if ($tel === -1) { |
|
$error = "Die von Ihnen angegebene Rufnummer ist ungültig."; |
|
$stage = SESSION_STAGE_ERROR; |
|
break; |
|
} |
|
$_SESSION["number"] = $tel; |
|
sendSms($tel, "Verifikationscode: ".$_SESSION["verification"]); |
|
break; |
|
case SESSION_STAGE_FINISHED: |
|
if ($_POST['verify'] == $_SESSION["verification"] && $_POST["tel"] == $_SESSION["number"]) { |
|
$verified = true; |
|
} else { |
|
$stage = SESSION_STAGE_ERROR; |
|
$error = "Die Nummer kann nicht verifiziert werden."; |
|
} |
|
default: |
|
break; |
|
} |
|
?> |
|
<html> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
|
<title>Verify Number by SMS</title> |
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> |
|
</head> |
|
<body> |
|
<div class="container"> |
|
<h1>Rufnummer verifizieren</h1> |
|
<?php if ($stage == SESSION_STAGE_START): ?> |
|
<p>Bitte geben Sie Ihre Handynummer ein.</p> |
|
<form action="" method="post" class="form-inline"> |
|
<input type="tel" name="tel" placeholder="Handynummer" class="form-control"/> |
|
<button type="submit" class="btn btn-default">Nummer prüfen</button> |
|
</form> |
|
<?php elseif ($stage == SESSION_STAGE_VERIFY): ?> |
|
<p>Bitte geben Sie den Code ein, den wir Ihnen per SMS geschickt haben.</p> |
|
<form action="" method="post" class="form-inline"> |
|
<input type="text" name="verify" placeholder="Verification Code" class="form-control"/> |
|
<input type="hidden" name="tel" value="<?php echo $_SESSION["number"] ?>"/> |
|
<button type="submit" class="btn btn-default">Verifizieren</button> |
|
</form> |
|
<?php elseif ($stage == SESSION_STAGE_FINISHED): ?> |
|
<div class="alert alert-success" role="alert">Ihre Nummer <strong><?php echo $_SESSION["number"] ?></strong> wurde verifiziert.</div> |
|
<?php else: ?> |
|
<div class="alert alert-danger" role="alert">Fehler: <?php echo $error;?> <a href="">Erneut versuchen</a></div> |
|
<?php endif ?> |
|
</div> |
|
</body> |
|
</html> |