2015-05-14 22:03:00 +02:00
|
|
|
<?php
|
2015-09-23 16:54:39 +02:00
|
|
|
function tellLock($pCommand, $pUser, $pPass, $pToken, $pIp){
|
2015-05-14 22:03:00 +02:00
|
|
|
|
|
|
|
$json = '{
|
|
|
|
"user":' . json_encode( $pUser ) . ',
|
|
|
|
"password":' . json_encode( $pPass ) . ',
|
2015-09-22 21:31:26 +02:00
|
|
|
"command":' . json_encode( $pCommand ) . ',
|
2015-05-14 22:03:00 +02:00
|
|
|
"token":' . json_encode( $pToken ) . ',
|
2015-05-18 22:23:41 +02:00
|
|
|
"ip":' . json_encode( $pIp ) . '
|
2015-05-14 22:03:00 +02:00
|
|
|
}'."\n";
|
|
|
|
|
|
|
|
$address = "127.0.0.1";
|
|
|
|
$port = "5555";
|
|
|
|
|
|
|
|
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
|
|
|
if ($socket === false) {
|
|
|
|
echo "socket_create() failed: " . socket_strerror(socket_last_error()) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = socket_connect($socket, $address, $port);
|
|
|
|
if ($result === false) {
|
|
|
|
echo "socket_connect() failed: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
socket_write($socket, $json, strlen($json));
|
|
|
|
|
|
|
|
$result = socket_read($socket, 1024);
|
|
|
|
|
|
|
|
socket_close($socket);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
$showLoginForm = false;
|
|
|
|
$showSuccess = false;
|
|
|
|
$showFailure = false;
|
2015-10-02 20:37:44 +02:00
|
|
|
$showTokenForm = false;
|
2015-05-18 22:23:41 +02:00
|
|
|
$isApi = false;
|
2015-05-14 22:03:00 +02:00
|
|
|
|
|
|
|
$pIp = $_SERVER[ 'REMOTE_ADDR' ];
|
2015-05-18 22:23:41 +02:00
|
|
|
|
2015-05-14 22:03:00 +02:00
|
|
|
if( $_SERVER[ 'REQUEST_METHOD' ] == "POST" ) {
|
|
|
|
|
2015-05-18 22:23:41 +02:00
|
|
|
if (array_key_exists("user", $_POST)
|
|
|
|
&& array_key_exists('pass', $_POST)
|
|
|
|
&& array_key_exists('token', $_POST)
|
2015-09-22 21:31:26 +02:00
|
|
|
&& array_key_exists('command', $_POST)
|
2015-05-18 22:23:41 +02:00
|
|
|
&& array_key_exists('api', $_POST))
|
|
|
|
{
|
|
|
|
$pUser = $_POST[ 'user' ];
|
|
|
|
$pPass = $_POST[ 'pass' ];
|
|
|
|
$pToken = $_POST[ 'token' ];
|
2015-09-22 21:31:26 +02:00
|
|
|
$pCommand = $_POST[ 'command' ];
|
2015-05-18 22:23:41 +02:00
|
|
|
$pApi = $_POST[ 'api' ];
|
|
|
|
|
|
|
|
if ($pApi == "true")
|
|
|
|
{
|
|
|
|
$isApi = true;
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:54:39 +02:00
|
|
|
$jsonResponse = json_decode(tellLock($pCommand, $pUser, $pPass, $pToken, $pIp), true);
|
|
|
|
if ($jsonResponse == null || !isset($jsonResponse['message']) || !isset($jsonResponse['code'])) {
|
2015-05-18 22:23:41 +02:00
|
|
|
$showFailure = true;
|
2015-09-23 16:54:39 +02:00
|
|
|
$failureMsg = 'Error parsing JSON response';
|
|
|
|
} else {
|
|
|
|
$failureMsg = $jsonResponse['message'];
|
|
|
|
$code = $jsonResponse['code'];
|
|
|
|
$showSuccess = ($code == 0);
|
|
|
|
$showFailure = !$showSuccess;
|
2015-05-18 22:23:41 +02:00
|
|
|
}
|
2015-05-14 22:03:00 +02:00
|
|
|
} else {
|
2015-05-18 22:23:41 +02:00
|
|
|
$failureMsg = 'Invalid Request';
|
2015-05-14 22:03:00 +02:00
|
|
|
$showFailure = true;
|
|
|
|
}
|
2015-05-18 22:23:41 +02:00
|
|
|
|
2015-05-14 22:03:00 +02:00
|
|
|
} else {
|
|
|
|
// This is done by apache mod_rewrite
|
2015-10-02 20:37:44 +02:00
|
|
|
$pToken = $_GET['token'];
|
|
|
|
$lToken = $str= ltrim ($pToken, '/');
|
2015-05-18 22:23:41 +02:00
|
|
|
|
2015-10-02 20:37:44 +02:00
|
|
|
if(strlen($lToken) == 0) {
|
|
|
|
$showLoginForm = true;
|
|
|
|
$showTokenForm = true;
|
2015-05-18 22:23:41 +02:00
|
|
|
} else {
|
|
|
|
$showLoginForm = true;
|
2015-05-14 22:03:00 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-18 22:23:41 +02:00
|
|
|
if ($isApi == false) {
|
2015-05-14 22:03:00 +02:00
|
|
|
?>
|
2015-05-18 22:23:41 +02:00
|
|
|
<!DOCTYPE html>
|
2015-05-14 22:03:00 +02:00
|
|
|
<html>
|
|
|
|
|
|
|
|
<head>
|
|
|
|
<title>Login</title>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
* {
|
|
|
|
font: normal 30px Arial,sans-serif;
|
|
|
|
}
|
|
|
|
body {
|
|
|
|
background-color: #037;
|
|
|
|
color: white;
|
|
|
|
background-image: url('logo.svg' );
|
|
|
|
background-repeat: repeat;
|
|
|
|
background-size: 300%;
|
|
|
|
background-position: -200px -100px;
|
|
|
|
}
|
|
|
|
form {
|
|
|
|
position: relative;
|
|
|
|
display: block;
|
|
|
|
width: auto;
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
input {
|
|
|
|
position: relative;
|
|
|
|
display: block;
|
|
|
|
width: auto;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
button {
|
|
|
|
width: 100%;
|
|
|
|
margin-top: 40px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
2015-10-02 20:37:44 +02:00
|
|
|
<?php if ($showLoginForm): ?>
|
2015-05-14 22:03:00 +02:00
|
|
|
|
|
|
|
<form name="login" method="post" action="/">
|
2015-10-02 20:37:44 +02:00
|
|
|
<?php if ($showTokenForm): ?>
|
|
|
|
<label for="token">Token</label>
|
|
|
|
<input type="text" name="token" value="">
|
|
|
|
<?php else: ?>
|
|
|
|
<input type="hidden" name="token" value="<?php echo $lToken;?>">
|
|
|
|
<?php endif; ?>
|
|
|
|
|
|
|
|
<input type="hidden" name="api" value="false">
|
|
|
|
|
2015-05-14 22:03:00 +02:00
|
|
|
<label for="user">User</label>
|
|
|
|
<input id="user" type="text" name="user">
|
|
|
|
|
|
|
|
<label for="pass">Pass</label>
|
|
|
|
<input id="pass" type="password" name="pass">
|
|
|
|
|
2015-09-22 21:31:26 +02:00
|
|
|
<button name="command" value="unlock">Open</button>
|
2015-05-14 22:03:00 +02:00
|
|
|
<hr/>
|
2015-09-22 21:31:26 +02:00
|
|
|
<button name="command" value="lock">Lock</button>
|
2015-05-14 22:03:00 +02:00
|
|
|
</form>
|
|
|
|
|
2015-09-22 21:27:07 +02:00
|
|
|
<?php elseif( $showSuccess ): ?>
|
2015-05-14 22:03:00 +02:00
|
|
|
|
|
|
|
<h1>Welcome Cpt. Cook</h1>
|
|
|
|
|
2015-09-22 21:27:07 +02:00
|
|
|
<?php elseif( $showFailure ): ?>
|
2015-05-14 22:03:00 +02:00
|
|
|
|
|
|
|
<h1>Something went wrong: <?php echo $failureMsg; ?></h1>
|
|
|
|
|
|
|
|
<?php endif; ?>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
|
|
|
|
</html>
|
2015-05-18 22:23:41 +02:00
|
|
|
<?php
|
|
|
|
} else {
|
2015-09-23 16:54:39 +02:00
|
|
|
echo $code;
|
2015-05-18 22:23:41 +02:00
|
|
|
}
|
|
|
|
?>
|