<commit_before><?php

namespace App\Controller\Admin;

use vxPHP\Template\SimpleTemplate;
use vxPHP\Form\HtmlForm;
use vxPHP\Form\FormElement\FormElementFactory;
use vxPHP\Controller\Controller;
use vxPHP\Http\Response;
use vxPHP\Application\Application;
use vxPHP\User\User;
use vxPHP\Util\Rex;
use vxPHP\Http\JsonResponse;
use vxPHP\Routing\Router;
use vxPHP\Webpage\MenuGenerator;
use vxPHP\Constraint\Validator\RegularExpression;
use vxPHP\Constraint\Validator\Email;
use vxPHP\Security\Password\PasswordEncrypter;

use vxWeb\User\Util;

class UsersController extends Controller {

	protected function execute() {

		$app = Application::getInstance();
		$admin = $app->getCurrentUser();
		$db = $app->getDb();
		
		$connection = $app->getDb()->getConnection();
		$redirectUrl = Router::getRoute('users', 'admin.php')->getUrl();
		$action = $this->route->getPathParameter('action');

		// editing or deleting something? Ensure user exists

		if(($id = $this->request->query->get('id'))) {

			// editing own record is not allowed
			
			if($admin->getUsername() == $id) {
				return $this->redirect($redirectUrl);
			}
				
			$userRows = $db->doPreparedQuery("SELECT a.* FROM admin a WHERE a.username = ?", [$id]);
				
			if(!count($userRows)) {
				return $this->redirect($redirectUrl);
			}

			$userRow = $userRows[0];

		}
		
		// delete user
		
		if($id && $action === 'del') {
		
			$db->deleteRecord('admin', ['username' => $id]);

			return $this->redirect($redirectUrl);
			
		}

		// edit or add user
		
		if($id || $action === 'new') {

			MenuGenerator::setForceActiveMenu(TRUE);

			$form = HtmlForm::create('admin_edit_user.htm')->setAttribute('class', 'editUserForm');
			
			if($id) {

				$form->setInitFormValues($userRow);
				$submitLabel = 'Änderungen übernehmen';
			
			}
			
			else {
			
				$submitLabel = 'User anlegen';
				$form->initVar('is_add', 1);
			}

			$admingroups = $connection->query('SELECT admingroupsID, name FROM admingroups ORDER BY privilege_level')->fetchAll(\PDO::FETCH_KEY_PAIR);

			$form
				->addElement(FormElementFactory::create('button', 'submit_user', '', ['type' => 'submit'])->setInnerHTML($submitLabel))
				->addElement(FormElementFactory::create('input',	'username'))
				->addElement(FormElementFactory::create('input',	'email'))
				->addElement(FormElementFactory::create('input',	'name'))
				->addElement(FormElementFactory::create('password',	'<PASSWORD>'))
				->addElement(FormElementFactory::create('password',	'<PASSWORD>'))
				->addElement(FormElementFactory::create('select',	'admingroupsid', NULL, [], $admingroups));
			
			$form->bindRequestParameters();
			
			return new Response(
				SimpleTemplate::create('admin/users_edit.php')
					->assign('user', isset($userRow) ? $userRow : NULL)
					->assign('form', $form->render())
					->display()
			);
		}
		
		$users	= $db->doPreparedQuery("SELECT a.*, ag.alias FROM admin a LEFT JOIN admingroups ag ON ag.admingroupsID = a.admingroupsID", []);

		return new Response(
			SimpleTemplate::create('admin/users_list.php')
				->assign('users', $users)
				->display()
		);
	}
	
	protected function xhrExecute() {

		// id comes either via URL or as an extra form field

		$id = $this->request->query->get('id', $this->request->request->get('id'));

		$app = Application::getInstance();
		$admin = $app->getCurrentUser();
		$db = $app->getDb();

		// editing own record is not allowed
		
		if($admin->getUsername() === $id) {
			return new Response('', Response::HTTP_FORBIDDEN);
		}
		
		if($id) {
<commit_msg>Bugfix: User names containing whitespace were considered invalid and editing triggered a HTTP 403<commit_after><?php

namespace App\Controller\Admin;

use vxPHP\Template\SimpleTemplate;
use vxPHP\Form\HtmlForm;
use vxPHP\Form\FormElement\FormElementFactory;
use vxPHP\Controller\Controller;
use vxPHP\Http\Response;
use vxPHP\Application\Application;
use vxPHP\User\User;
use vxPHP\Util\Rex;
use vxPHP\Http\JsonResponse;
use vxPHP\Routing\Router;
use vxPHP\Webpage\MenuGenerator;
use vxPHP\Constraint\Validator\RegularExpression;
use vxPHP\Constraint\Validator\Email;
use vxPHP\Security\Password\PasswordEncrypter;

use vxWeb\User\Util;

class UsersController extends Controller {

	protected function execute() {

		$app = Application::getInstance();
		$admin = $app->getCurrentUser();
		$db = $app->getDb();
		
		$connection = $app->getDb()->getConnection();
		$redirectUrl = Router::getRoute('users', 'admin.php')->getUrl();
		$action = $this->route->getPathParameter('action');

		// editing or deleting something? Ensure user exists

		if(($id = urldecode($this->request->query->get('id')))) {

			// editing own record is not allowed
			
			if($admin->getUsername() == $id) {
				return $this->redirect($redirectUrl);
			}
				
			$userRows = $db->doPreparedQuery("SELECT a.* FROM admin a WHERE a.username = ?", [$id]);
				
			if(!count($userRows)) {
				return $this->redirect($redirectUrl);
			}

			$userRow = $userRows[0];

		}
		
		// delete user
		
		if($id && $action === 'del') {
		
			$db->deleteRecord('admin', ['username' => $id]);

			return $this->redirect($redirectUrl);
			
		}

		// edit or add user
		
		if($id || $action === 'new') {

			MenuGenerator::setForceActiveMenu(TRUE);

			$form = HtmlForm::create('admin_edit_user.htm')->setAttribute('class', 'editUserForm');
			
			if($id) {

				$form->setInitFormValues($userRow);
				$submitLabel = 'Änderungen übernehmen';
			
			}
			
			else {
			
				$submitLabel = 'User anlegen';
				$form->initVar('is_add', 1);
			}

			$admingroups = $connection->query('SELECT admingroupsID, name FROM admingroups ORDER BY privilege_level')->fetchAll(\PDO::FETCH_KEY_PAIR);

			$form
				->addElement(FormElementFactory::create('button', 'submit_user', '', ['type' => 'submit'])->setInnerHTML($submitLabel))
				->addElement(FormElementFactory::create('input',	'username'))
				->addElement(FormElementFactory::create('input',	'email'))
				->addElement(FormElementFactory::create('input',	'name'))
				->addElement(FormElementFactory::create('password',	'<PASSWORD>'))
				->addElement(FormElementFactory::create('password',	'<PASSWORD>'))
				->addElement(FormElementFactory::create('select',	'admingroupsid', NULL, [], $admingroups));
			
			$form->bindRequestParameters();
			
			return new Response(
				SimpleTemplate::create('admin/users_edit.php')
					->assign('user', isset($userRow) ? $userRow : NULL)
					->assign('form', $form->render())
					->display()
			);
		}
		
		$users	= $db->doPreparedQuery("SELECT a.*, ag.alias FROM admin a LEFT JOIN admingroups ag ON ag.admingroupsID = a.admingroupsID", []);

		return new Response(
			SimpleTemplate::create('admin/users_list.php')
				->assign('users', $users)
				->display()
		);
	}
	
	protected function xhrExecute() {

		// id comes either via URL or as an extra form field

		$id = urldecode($this->request->query->get('id', $this->request->request->get('id')));

		$app = Application::getInstance();
		$admin = $app->getCurrentUser();
		$db = $app->getDb();

		// editing own record is not allowed
		
		if($admin->getUsername() === $id) {
			return new Response('', Response::HTTP_FORBIDDEN);
		}
		
		if($id) {

{'fraction_non_alphanumeric': 0.1751530328324986, 'fraction_numerical': 0.0009738452977184196, 'mean_word_length': 4.247445255474452, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5887463', 'n_tokens_mistral': 2694, 'n_tokens_neox': 2285, 'n_words': 443}