header("Location: " . getUrl() . "admin");
}
$Logs = new cLogs("editroles.php");
$Logs->on();
$aErrors = array();
$aAlerts = array();
global $DB;
if (isset($_POST['update_role'])) {
    foreach ($_POST as $k => $v) {
        $_POST[$k] = get_magic_quotes_gpc() ? trim($v) : trim(addslashes($v));
    }
    $Check = new cCheckForm();
    $Check->check('General', 'is_numeric($test) && ' . ($_POST['roleid'] == $_GET['id']), 'There is no correct role!', $_POST['roleid']);
    if ($Check->check('name', 'strlen($test) > 0 && strlen($test) < 101', 'The name of role must be max. 100 symbols!')) {
        /// check if name already exists ///
        if ($exists = admin_roleExists($_POST['name'])) {
            $Logs->addLog($exists, 'role EXISTS');
            $exists = !($exists['id'] == $_POST['roleid']);
        }
        $Logs->addLog($exists, 'role EXISTS');
        $Check->check('name', '$test==false', 'The name of role already exists!', $exists);
    }
    $Check->check('description', '$test != "" && strlen($test) <= 255', 'Description of role is required; with a maximum length 255!');
    $Check->check('parentid', 'is_numeric($test) || $test=="null"', 'The parent role is in wrong type');
    $Check->check('sort', 'is_numeric($test) || strlen($test)==0', 'The parent role is in wrong type');
    $Logs->addLog($Check->isValid(), 'form valid');
    if (!$Check->isValid()) {
        foreach ($Check->getErrors() as $k => $error) {
            $aErrors[] = admin_getErrorToPrint($k, $error);
        }
        //throw new cException("Form is not valid!");
    header("Location: " . getUrl() . "admin");
}
$Logs = new cLogs("addrole.php");
$Logs->on();
$aErrors = array();
$aAlerts = array();
global $DB;
if (isset($_POST['add_role'])) {
    foreach ($_POST as $k => $v) {
        $_POST[$k] = get_magic_quotes_gpc() ? trim($v) : trim(addslashes($v));
    }
    $Check = new cCheckForm();
    /// max. 100 symbols ///
    if ($Check->check('name', 'strlen($test) > 0 && strlen($test) < 101', 'The name of role must be max. 100 symbols!')) {
        /// check if name already exists ///
        $Check->check('name', '$test==false', 'The name of role already exists!', admin_roleExists($_POST['name']));
    }
    $Check->check('description', '$test != "" && strlen($test) <= 255', 'Description of role is required; with a maximum length 255!');
    $Check->check('parentid', 'is_numeric($test) || $test=="null"', 'The parent role is in wrong type');
    $Check->check('sort', 'is_numeric($test) || strlen($test)==0', 'The parent role is in wrong type');
    $Logs->addLog($Check->isValid(), 'add new one valid');
    if (!$Check->isValid()) {
        foreach ($Check->getErrors() as $k => $error) {
            $aErrors[] = admin_getErrorToPrint($k, $error);
        }
    } else {
        try {
            $aVals = array();
            $aVals[] = array('name', $_POST['name']);
            $aVals[] = array('description', $_POST['description']);
            $sort = $_POST['sort'];
 foreach ($_POST as $k => $v) {
     $_POST[$k] = get_magic_quotes_gpc() ? trim($v) : trim(addslashes($v));
 }
 $aVals = array();
 $Check = new cCheckForm();
 $Check->check('General', 'is_numeric($test) && ' . ($_POST['userid'] == $_GET['id']), 'There is no correct user!', $_POST['userid']);
 $Check->check('name', 'strlen($test) > 0 && strlen($test) < 101', 'The name must be max. 100 symbols!');
 $Check->check('surname', 'strlen($test) > 0 && strlen($test) < 101', 'The surname must be max. 100 symbols!');
 $Check->check('role', 'is_numeric($test)', 'The role is in wrong format!');
 /// change login ///
 if (strlen($_POST['login'])) {
     /// max. 50 symbols ///
     $loginPat = '/^[a-zA-Z0-9_-]{1,50}$/';
     if ($Check->check('login', 'preg_match("' . $loginPat . '",$test)', 'The login must be without whitespaces and diacritical marks and max. 50 symbols!')) {
         /// check if login already exists ///
         if ($exists = admin_roleExists($_POST['login'])) {
             $Logs->addLog($exists, 'user login EXISTS');
             $exists = !($exists['id'] == $_POST['userid']);
         }
         $Logs->addLog($exists, 'user login EXISTS');
         $Check->check('login', '$test==false', 'This login already exists!', $exists);
     }
     $aVals[] = array('login', $_POST['login']);
 }
 /// change password ///
 if (strlen($_POST['passw'])) {
     $passwPat = '/^[a-zA-Z0-9_-]{1,50}$/';
     if ($Check->check('password', 'strlen($test)>5 && strlen($test)<51', 'The password must have at least 6 symbols and max. 50, without diacritical marks and whitespaces!', $_POST['passw'])) {
         $Check->check('password', '$test==true', 'Verification of password is not correct!', $_POST['passw'] == $_POST['passw_ver']);
     }
     $aVals[] = array('password', cAuthentication::cyphrePassword($_POST['passw']));