<?php

define('PATH_ROOT', '../');
include_once(PATH_ROOT . '.config/config_global.php');
include_once(PATH_FNC . 'fnc_portal.php');

$db = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME);
$db->set_charset('utf8');

$users = $db->query('SELECT * FROM users;');

while ($dat_user = $users->fetch_assoc())
{
	$update = '';

	if ($dat_user ['userUsername'] === '')
	{
		$update .= 'userUsername = "' . mb_strtolower(substr($dat_user ['userFirstname'], 0, 1) . $dat_user ['userLastname']) . '"';
	}
	if ($dat_user ['userPasswordOrg'] === '')
	{
		$update .= ($update === '')
			? ''
			: ' , ';
		$newPassword = fnc_generatePW();
		$update .= 'userPasswordOrgCrypt = "' . md5($newPassword) . '" , userPasswordOrg = "' . $newPassword . '"';
	}

	if ($update !== '')
	{
		$user_update = $db->query('UPDATE users SET ' . $update . ' WHERE userId = "' . $dat_user ['userId'] . '" LIMIT 1;');
	}
}