servnest/sftpgo-auth.php

47 lines
1.3 KiB
PHP

<?php
const DEBUG = false;
!DEBUG or ob_start();
require 'router.php';
function deny($reason) {
!DEBUG or file_put_contents(ROOT_PATH . '/db/debug.txt', ob_get_contents() . $reason . LF);
http_response_code(403);
exit();
}
if (CONF['common']['services']['ht'] !== 'enabled')
deny('Service not enabled.');
$auth_data = json_decode(file_get_contents('php://input'), true, flags: JSON_THROW_ON_ERROR);
$username = hashUsername($auth_data['username']);
if (usernameExists($username) !== true)
deny('This username doesn\'t exist.');
if (!in_array('ht', explode(',', query('select', 'users', ['username' => $username], 'services')[0]), true))
deny('Service not enabled for this user.');
$id = query('select', 'users', ['username' => $username], 'id')[0];
if (checkPassword($id, $auth_data['password']) !== true)
deny('Wrong password.');
echo '
{
"status": 1,
"username": ' . json_encode($auth_data['username']) . ',
"home_dir": "' . CONF['ht']['ht_path'] . '/fs/' . $id . '",
"quota_size": ' . ((query('select', 'users', ['id' => $id], 'type')[0] === 'approved') ? CONF['ht']['user_quota_approved'] : CONF['ht']['user_quota_testing']) . ',
"permissions": {
"/": [
"*"
]
}
}
';
!DEBUG or file_put_contents(ROOT_PATH . '/db/debug.txt', ob_get_contents());
http_response_code(200);