2018-07-13 18:39:55 +02:00
|
|
|
<?php
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) Codiad & Kent Safranski (codiad.com), distributed
|
|
|
|
* as-is and without warranty under the MIT License. See
|
|
|
|
* [root]/license.txt for more. This information must remain intact.
|
|
|
|
*/
|
2018-07-13 18:39:55 +02:00
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
require_once('../../common.php');
|
|
|
|
require_once('class.user.php');
|
2018-07-13 18:39:55 +02:00
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
if( ! isset( $_GET['action'] ) ) {
|
|
|
|
|
|
|
|
die( formatJSEND( "error", "Missing parameter" ) );
|
2018-07-13 18:39:55 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Verify Session or Key
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2018-07-13 18:39:55 +02:00
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
if( $_GET['action'] != 'authenticate' ) {
|
|
|
|
|
|
|
|
checkSession();
|
|
|
|
}
|
2018-07-13 18:39:55 +02:00
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
$User = new User();
|
2018-07-13 18:39:55 +02:00
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Authenticate
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2018-07-13 18:39:55 +02:00
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
if($_GET['action']=='authenticate') {
|
|
|
|
|
|
|
|
if( ! isset( $_POST['username'] ) || ! isset( $_POST['password'] ) ) {
|
|
|
|
|
|
|
|
die( formatJSEND( "error", "Missing username or password" ) );
|
|
|
|
}
|
|
|
|
|
2019-10-16 16:20:09 +02:00
|
|
|
$username = User::CleanUsername( $_POST['username'] );
|
2019-10-18 21:58:01 +02:00
|
|
|
$password = $_POST['password'];
|
2019-07-09 04:34:39 +02:00
|
|
|
|
|
|
|
// check if the asked languages exist and is registered in languages/code.php
|
|
|
|
require_once '../../languages/code.php';
|
|
|
|
if( isset( $languages[$_POST['language']] ) ) {
|
|
|
|
|
2019-10-16 16:20:09 +02:00
|
|
|
$lang = $_POST['language'];
|
2019-07-09 04:34:39 +02:00
|
|
|
} else {
|
|
|
|
|
2019-10-16 16:20:09 +02:00
|
|
|
$lang = 'en';
|
2019-07-09 04:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// theme
|
2019-10-16 16:20:09 +02:00
|
|
|
$theme = $_POST['theme'];
|
|
|
|
$permissions = array(
|
|
|
|
"755",
|
|
|
|
"0755"
|
|
|
|
);
|
|
|
|
|
|
|
|
if( ! is_dir( SESSIONS_PATH ) ) {
|
|
|
|
|
|
|
|
mkdir( SESSIONS_PATH, 00755 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$server_user = getmyuid();
|
|
|
|
$sessions_permissions = substr( sprintf( '%o', fileperms( SESSIONS_PATH ) ), -4 );
|
|
|
|
$sessions_owner = fileowner( SESSIONS_PATH );
|
|
|
|
|
|
|
|
if( is_array( $server_user ) ) {
|
|
|
|
|
|
|
|
$server_user = $server_user["uid"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! ( $sessions_owner === $server_user ) ) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
chown( SESSIONS_PATH, $server_user );
|
|
|
|
} catch( Exception $e ) {
|
|
|
|
|
|
|
|
exit( formatJSEND("error", "Error, incorrect owner of sessions folder. Expecting: $server_user, Recieved: " . $sessions_owner ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! in_array( $sessions_permissions, $permissions ) ) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
chmod( SESSIONS_PATH, 00755 );
|
|
|
|
} catch( Exception $e ) {
|
|
|
|
|
|
|
|
exit( formatJSEND("error", "Error, incorrect permissions on sessions folder. Expecting: 0755, Recieved: " . $sessions_permissions ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$pass = $User->Authenticate( $username, $password );
|
|
|
|
|
|
|
|
if( $pass ) {
|
|
|
|
|
|
|
|
$_SESSION['lang'] = $lang;
|
|
|
|
$_SESSION['theme'] = $theme;
|
2019-10-18 21:58:01 +02:00
|
|
|
exit( formatJSEND( "success", array( "username" => $username ) ) );
|
2019-10-16 16:20:09 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
exit( formatJSEND( "error", "Incorrect Username or Password" ) );
|
|
|
|
}
|
2018-07-13 18:39:55 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Logout
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2018-07-13 18:39:55 +02:00
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
if( $_GET['action'] == 'logout' ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
|
|
|
logout();
|
2018-07-13 18:39:55 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Create User
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
if( $_GET['action'] == 'create' ) {
|
|
|
|
|
|
|
|
if( checkAccess() ) {
|
|
|
|
|
|
|
|
if ( ! isset( $_POST['username'] ) || ! isset( $_POST['password'] ) ) {
|
|
|
|
|
|
|
|
exit( formatJSEND( "error", "Missing username or password" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! ( $_POST['password'] === $_POST['password2'] ) ) {
|
|
|
|
|
|
|
|
exit( formatJSEND( "error", "Passwords do not match" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( preg_match( '/[^\w\-\._@]/', $_POST['username'] ) ) {
|
|
|
|
|
|
|
|
exit( formatJSEND( "error", "Invalid characters in username" ) );
|
|
|
|
}
|
|
|
|
|
2019-10-25 18:46:23 +02:00
|
|
|
$result = $User->Create( $_POST['username'], $_POST['password'] );
|
|
|
|
|
|
|
|
if( $result ) {
|
|
|
|
|
|
|
|
exit( formatJSEND( "success", "User successfully created." ) );
|
|
|
|
}
|
2019-07-09 04:34:39 +02:00
|
|
|
}
|
2018-07-13 18:39:55 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Delete User
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
if( $_GET['action'] == 'delete' ) {
|
|
|
|
|
|
|
|
if( checkAccess() ) {
|
|
|
|
|
|
|
|
if( ! isset( $_GET['username'] ) ) {
|
|
|
|
|
2019-10-25 18:46:23 +02:00
|
|
|
exit( formatJSEND( "error", "Missing username" ) );
|
2019-07-09 04:34:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 18:46:23 +02:00
|
|
|
$return = $User->Delete( $_GET['username'] );
|
|
|
|
exit( json_encode( $return ) );
|
2019-07-09 04:34:39 +02:00
|
|
|
}
|
2018-07-13 18:39:55 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Change Password
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
if( $_GET['action'] == 'password' ) {
|
|
|
|
|
|
|
|
if( ! isset( $_POST['username']) || ! isset( $_POST['password'] ) ) {
|
|
|
|
|
|
|
|
die( formatJSEND( "error", "Missing username or password" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $_POST['username'] == $_SESSION['user'] || is_admin() ) {
|
|
|
|
|
|
|
|
$User->username = User::CleanUsername( $_POST['username'] );
|
|
|
|
$User->password = $_POST['password'];
|
|
|
|
$User->Password();
|
|
|
|
}
|
2018-07-13 18:39:55 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Change Project
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
if( $_GET['action'] == 'project' ) {
|
|
|
|
|
|
|
|
if( ! isset( $_GET['project'] ) ) {
|
|
|
|
|
|
|
|
die( formatJSEND( "error", "Missing project" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$User->username = $_SESSION['user'];
|
|
|
|
$User->project = $_GET['project'];
|
|
|
|
$User->Project();
|
2018-07-13 18:39:55 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Search Users
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
if( $_GET['action'] == 'search_users' ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
if( ! isset( $_GET['search_term'] ) ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
|
|
|
die( formatJSEND( "error", "Missing search term" ) );
|
|
|
|
}
|
2019-07-09 04:34:39 +02:00
|
|
|
|
2018-11-19 19:30:49 +01:00
|
|
|
search_users( $_GET['search_term'], "exit", true );
|
2018-11-10 06:41:28 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Verify User Account
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2018-07-13 18:39:55 +02:00
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
if( $_GET['action'] == 'verify' ) {
|
|
|
|
|
|
|
|
$User->username = $_SESSION['user'];
|
|
|
|
checkSession();
|
2018-07-13 18:39:55 +02:00
|
|
|
}
|
2019-02-10 06:35:15 +01:00
|
|
|
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
if( $_GET['action'] == 'update_access' ) {
|
2019-02-10 06:35:15 +01:00
|
|
|
|
|
|
|
checkSession();
|
2019-02-11 00:10:21 +01:00
|
|
|
|
2019-10-16 16:20:09 +02:00
|
|
|
if( ! isset( $_POST['access'] ) || ! isset( $_POST['user'] ) ) {
|
2019-02-11 00:10:21 +01:00
|
|
|
|
|
|
|
die( formatJSEND( "error", "Could not update access." ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! is_admin() ) {
|
|
|
|
|
2019-07-09 04:34:39 +02:00
|
|
|
die( formatJSEND( "error", "You do not have permission to update user's access." ) );
|
2019-02-11 00:10:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-16 16:20:09 +02:00
|
|
|
if( ! in_array( $_POST["access"], array_keys( Permissions::SYSTEM_LEVELS ) ) ) {
|
|
|
|
|
|
|
|
exit( formatJSEND( "error", "Invalid access level specified." ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$User->update_access( $_POST["user"], $_POST["access"] );
|
2019-02-10 06:35:15 +01:00
|
|
|
}
|