You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.3 KiB
C

#include "controller/admin.h"
// Model
#include "src/model/server.h"
#include "src/model/owner.h"
#include "src/model/account.h"
// View
// Controller
#include "src/controller/api/client_apps.h"
// Submodules
#include "form.h"
#include "http/server/request.h"
// Platform Headers
#include <string.h>
#include <stdlib.h>
const char* view_checkbox( bool value )
{
return value ? "checked" : "";
}
bool route_admin_request( struct http_request* req )
{
// TODO: authenticate
if( !check_authentication_header(req) ) {
printf( "User-Agent: %s\n", http_request_get_header(req,"user-agent") );
http_request_send_headers( req, 401, "text/plain", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "Not authorized to use this endpoint.\n" );
return true;
}
if( http_request_route_term( req, "/server-setup" ) ) {
return handle_admin_server_setup(req);
}
return false;
}
// Special: /, step=1
bool handle_admin_initial_owner_setup( struct http_request* req )
{
if( http_request_route_method( req, "POST" ) ) {
// TODO: handle post
FILE* body = http_request_get_request_data(req);
struct form_parser* fp = form_pull_parser_new(body);
if( !fp ) { goto show_owner_setup; }
struct owner* o = owner_new();
// Create owner account
struct account* owner = account_new();
owner->id = owner_account_id;
account_save(owner);
// Create home timeline account
{
struct account* home = account_new();
home->id = home_timeline_id;
home->handle = strdup("%home-timeline");
home->server = strdup("localhost");
account_save(home);
account_free(home);
}
// Create public timeline account
{
struct account* public = account_new();
public->id = public_timeline_id;
public->handle = strdup("%public-timeline");
public->server = strdup("localhost");
account_save(public);
account_free(public);
}
bool success = false;
char* password = NULL;
char* confirm = NULL;
char* key = NULL;
while( (key=form_pull_parser_read_key(fp)) ) {
if( 0 == strcmp(key,"password") ) {
password = strdup(form_pull_parser_read_value(fp));
} else if( 0 == strcmp(key,"confirm") ) {
confirm = strdup(form_pull_parser_read_value(fp));
} else if( 0 == strcmp(key,"handle") ) {
owner->handle = strdup(form_pull_parser_read_value(fp));
account_save(owner);
}
}
if( owner->handle && *owner->handle && password && confirm && ( 0 == strcmp(password,confirm) ) ) {
owner_set_password( o, password );
success = true;
}
form_pull_parser_release(fp);
if( success ) {
owner_save(o);
}
owner_free(o);
account_free(owner);
if( success ) {
// TODO: generate crypto keys
// Advance wizard to next step
g_server->configured = true;
app_args_save();
http_request_begin_send_headers( req, 302, false );
http_request_send_header( req, "Location", "/?complete" );
http_request_end_send_headers( req, false );
return true;
}
}
show_owner_setup:
http_request_send_headers( req, 200, "text/html", true );
FILE* f = http_request_get_response_body( req );
#include "view/admin/owner-setup.html.inc"
return true;
}
// Route: /admin/server-setup
// Special: /, step=0 (when server hasn't been configured)
bool handle_admin_server_setup( struct http_request* req )
{
if( http_request_route_method( req, "POST" ) ) {
// TODO: handle post
FILE* body = http_request_get_request_data(req);
struct form_parser* fp = form_pull_parser_new(body);
if( !fp ) { return false; }
app_args_load_from_form( g_server, fp );
form_pull_parser_release(fp);
// Advance wizard to next step
if( g_server->setup_wizard_step == 0 ) {
g_server->setup_wizard_step = 1;
}
app_args_save();
app_args_load();
// Redirect
http_request_begin_send_headers( req, 302, false );
http_request_send_header( req, "Location", "/?account" );
http_request_end_send_headers( req, false );
return true;
} else {
http_request_send_headers( req, 200, "text/html", true );
FILE* f = http_request_get_response_body( req );
#include "view/admin/server-setup.html.inc"
return true;
}
}
bool handle_admin_server_setup_wizard( struct http_request* req )
{
switch(g_server->setup_wizard_step) {
case 0: return handle_admin_server_setup(req);
case 1: return handle_admin_initial_owner_setup(req);
}
return false;
}