Skip to content

Commit

Permalink
refactor(users.php): Move user management functions
Browse files Browse the repository at this point in the history
Put the `addUser()`, `deleteUser()`, `changeUserPassword()`, and
`getUsers()` functions into their own file, out of misc/functions.php.
  • Loading branch information
coldacid authored and Chris Charabaruk committed Apr 13, 2022
1 parent 65d3692 commit 11c0296
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 50 deletions.
1 change: 1 addition & 0 deletions PodcastGenerator/core/include_admin.php
Expand Up @@ -36,6 +36,7 @@
// This file is wizard to convert old password to a more secure algorithm
// Load useful functions
include 'misc/functions.php';
include 'users.php';
// Load HTML helper functions
include 'html_helpers.php';
// Load translations
Expand Down
50 changes: 0 additions & 50 deletions PodcastGenerator/core/misc/functions.php
Expand Up @@ -36,56 +36,6 @@ function checkLogin($username, $password_plain)
return false;
}

function addUser($username, $password_plain)
{
global $config;
$users = json_decode($config['users_json'], true);
// Check if user exists
if (array_key_exists($username, $users)) {
return false;
}
$users[$username] = password_hash($password_plain, PASSWORD_DEFAULT);
return updateConfig(
$config['absoluteurl'] . 'config.php',
'users_json',
str_replace('"', '\"', json_encode($users))
);
}

function deleteUser($username)
{
global $config;
$users = json_decode($config['users_json'], true);
unset($users[$username]);
return updateConfig(
$config['absoluteurl'] . 'config.php',
'users_json',
str_replace('"', '\"', json_encode($users))
);
}

function changeUserPassword($username, $new_password_plain)
{
global $config;
$users = json_decode($config['users_json'], true);
// Check if user exists
if (!array_key_exists($username, $users)) {
return false;
}
$users[$username] = password_hash($new_password_plain, PASSWORD_DEFAULT);
return updateConfig(
$config['absoluteurl'] . 'config.php',
'users_json',
str_replace('"', '\"', json_encode($users))
);
}

function getUsers()
{
global $config;
return json_decode($config['users_json'], true);
}

function randomString($length = 8)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Expand Down
60 changes: 60 additions & 0 deletions PodcastGenerator/core/users.php
@@ -0,0 +1,60 @@
<?php

############################################################
# PODCAST GENERATOR
#
# Created by Alberto Betella and Emil Engler
# http://www.podcastgenerator.net
#
# This is Free Software released under the GNU/GPL License.
############################################################

function addUser($username, $password_plain)
{
global $config;
$users = json_decode($config['users_json'], true);
// Check if user exists
if (array_key_exists($username, $users)) {
return false;
}
$users[$username] = password_hash($password_plain, PASSWORD_DEFAULT);
return updateConfig(
$config['absoluteurl'] . 'config.php',
'users_json',
str_replace('"', '\"', json_encode($users))
);
}

function deleteUser($username)
{
global $config;
$users = json_decode($config['users_json'], true);
unset($users[$username]);
return updateConfig(
$config['absoluteurl'] . 'config.php',
'users_json',
str_replace('"', '\"', json_encode($users))
);
}

function changeUserPassword($username, $new_password_plain)
{
global $config;
$users = json_decode($config['users_json'], true);
// Check if user exists
if (!array_key_exists($username, $users)) {
return false;
}
$users[$username] = password_hash($new_password_plain, PASSWORD_DEFAULT);
return updateConfig(
$config['absoluteurl'] . 'config.php',
'users_json',
str_replace('"', '\"', json_encode($users))
);
}

function getUsers()
{
global $config;
return json_decode($config['users_json'], true);
}
1 change: 1 addition & 0 deletions contrib/recover/reset.php
@@ -1,6 +1,7 @@
<?php
require '../core/misc/configsystem.php';
require '../core/misc/functions.php';
require '../core/users.php';

$config = getConfig('../config.php');
$users = getUsers();
Expand Down

0 comments on commit 11c0296

Please sign in to comment.