LOL. Probably due to the existence of $_COOKIE variables in the code, which, given the security hole in how they are being used, IS pretty darn offensive.
The add_lang.php code will be virtually identical to the add_group.php code, i.e. it’s performing the same steps, just on different input data and a different table name, with the following differences -
- There’s only one input, the name. The ports validation logic and usage in the sql code would be removed.
- The database table name in the sql query would be changed.
- Any add_lang specific error_mess… and success_mess… values would be used.
- The ‘LINK’ tag value in the success display code would be changed.
It should look like this (I marked the areas that are different with a string of *******) -
<?php
// add_lang.php
define("PHPSTATUS_REAL_PATH","./../");
require PHPSTATUS_REAL_PATH . 'common.php';
$errors = []; // an array to hold user/validation error messages
$post = []; // an array to hold a trimmed working copy of the form data
if(!isset($_COOKIE['loged']) || $_COOKIE['loged'] != 'yes')
{
// not logged in
$errors['loged'] = 'error_mess6';
} else {
if(!isset($_COOKIE['user_level']) || $_COOKIE['user_level'] != '1')
{
$errors['user_level'] = 'error_mess5';
}
}
// if no user errors, process the post method form data
if(empty($errors) && $_SERVER['REQUEST_METHOD'] == 'POST')
{
// inputs - name **************** remove the ports input
// trim, than validate all inputs
$post = array_map('_trim',$_POST); // note: _trim is a recursive 'trim' user written function
if($post['name'] == '')
{
$errors['name'] = 'error_mess7'; // ********** change this to the add_lang name empty value
}
// **************** remove the ports validation logic
// if no errors, use the submitted data
if(empty($errors))
{
// *************** change the table name and remove the ports column and place-holder
$sql = "INSERT INTO _lang (name) VALUES (?)";
$stmt = $pdo->prepare($sql);
try { // a 'local' try/catch to handle a specific error type
$stmt->execute([ $post['name'] ]); // **************** remove the ports value
} catch (PDOException $e) {
if($e->errorInfo[1] == 1062) // duplicate key error number
{
$errors['name'] = 'error_mess8'; // *************** change this to the add_lang name already exists value
} else {
throw $e; // re-throw the pdoexception if not handled by this logic
}
}
}
// if no errors, success
if(empty($errors))
{
// you would normally do a redirect to the exact same url of the current page
// to display any success message, you would store it in a session variable
// using existing success code -
require "header.php";
$template->getFile(array(
'success' => 'admin/success.tpl')
);
$template->add_vars(array(
'L_SUCCESS' => $lang['success'],
'DISPLAY' => $lang['success_mess1'], // ************** change this to the add_lang specific value
'LINK' => "groups.php") // ************** change this to the add_lang specific value
);
$template->parse("success");
require "footer.php";
exit();
}
}
// display any errors
if(!empty($errors))
{
// not sure if/how the current code would handle displaying multiple error messages
// for demo purposes, just combine the messages into one <br> separated string
require "header.php";
$template->getFile(array(
'error' => 'admin/error.tpl')
);
$msg = []; // array to hold actual error text
foreach($errors as $error)
{
$msg[] = $lang[$error]; // get the error text
}
$template->add_vars(array(
'L_ERROR' => $lang['error'],
'DISPLAY' => implode('<br>',$msg))
);
$template->parse("error");
require "footer.php";
exit();
}
This begs the question, how many add_… or edit/update action files are there, that only differ in the number/name of inputs, table/column names, and some message values? You would normally use a data driven design, where you have a data structure (array or database table) that defines what general-purpose code on the page does. Once you get to this point, you would move toward having a single, general-purpose, file of code that accepts a ‘module’ input (group, lang, …), that tells it to use the correct defining data structure.