53 lines
1.3 KiB
PHP
53 lines
1.3 KiB
PHP
<?php
|
|
|
|
if (!isset($_SERVER['PHP_AUTH_USER'])) {
|
|
header('WWW-Authenticate: Basic realm="NCBGW"');
|
|
header('HTTP/1.0 401 Unauthorized');
|
|
die('provide credentials');
|
|
}
|
|
|
|
require_once 'config.php';
|
|
$config = new Config($_ENV);
|
|
|
|
// check basic auth
|
|
if (!$config->getUsername() == $_SERVER['PHP_AUTH_USER']
|
|
|| !$config->getPassword() == $_SERVER['PHP_AUTH_PW']) {
|
|
die('invalid credentials');
|
|
}
|
|
|
|
if (!array_key_exists('action', $_GET)
|
|
|| !array_key_exists('token', $_GET)) {
|
|
die("required parameters not given");
|
|
}
|
|
|
|
//$message = file_get_contents('php://stdin');
|
|
$message = file_get_contents('php://input');
|
|
$token = $_GET['token'];
|
|
$url = $config->getUrl() . '/bot/' . $token . '/message';
|
|
$random = bin2hex(random_bytes(32));
|
|
|
|
$digest = hash_hmac('sha256', $random . $message, $config->getSecret());
|
|
|
|
$data = json_encode(
|
|
array(
|
|
'message' => $message
|
|
)
|
|
);
|
|
|
|
$opts = array('http' =>
|
|
array(
|
|
'method' => 'POST',
|
|
'header' => array(
|
|
'X-Nextcloud-Talk-Bot-Random: ' . $random,
|
|
'X-Nextcloud-Talk-Bot-Signature: ' . $digest,
|
|
'OCS-APIRequest: true',
|
|
'Content-Type: application/json'
|
|
),
|
|
'content' => $data
|
|
)
|
|
);
|
|
|
|
$context = stream_context_create($opts);
|
|
|
|
$result = file_get_contents($url, false, $context);
|
|
var_dump($result); |