AutoPulse v 1.0.1
 
Loading...
Searching...
No Matches
C:/Users/ivans/OneDrive/Документы/ZWA/sem/process-publishInsertion.php File Reference

This script provides validation of inputs of user publish-insertion form, including the attached files. More...

Functions

 terminatePublishInsertionWithError ($errorParam)
 
 terminateWithRemove ($errorParam, $mediaFiles, $avatarFile)
 
 is_valid_number ($value, $min_value, $max_value)
 
 is_valid_text ($value, $max_length)
 
 is_valid_select ($value)
 
 resizeImage ($sourceImage, $newWidth, $newHeight)
 

Variables

 $make = $_POST['make'] ?? ''
 
 $model = $_POST['model'] ?? ''
 
 $short_description = $_POST['short_description'] ?? ''
 
 $price = $_POST['price'] ?? ''
 
 $year = $_POST['year'] ?? ''
 
 $mileage = $_POST['mileage'] ?? ''
 
 $power = $_POST['power'] ?? ''
 
 $fuel = $_POST['fuel'] ?? ''
 
 $engine_capacity = $_POST['engine_capacity'] ?? ''
 
 $description = $_POST['description'] ?? ''
 
 $number_fields_min_values
 
 $number_fields_max_values
 
 $text_fields_max_lengths
 
 $isFormValid = true
 
if(! $isFormValid) if(empty($_FILES)) $uploadDir = __DIR__ . '/media/'
 
 $avatar_uploadDir = __DIR__ . '/avatar/'
 
if(!is_dir( $uploadDir) &&!mkdir( $uploadDir, 0777, true)) if(!is_dir($avatar_uploadDir) &&!mkdir($avatar_uploadDir, 0777, true)) $maxFileSize = 40 * 1024 * 1024
 
 $allowedExtension = 'jpg'
 
 $savedFiles = []
 
 $avatar_uniqueFileName = "filename"
 
 for ( $i=1;$i<=5;$i++)
 Includes the database connection file.
 
if(isset($error) && $error==="db_access_failed") try
 
 $insertion_id = $pdo->lastInsertId()
 Gets the last inserted ID for the insertions table.
 
for( $i=1;$i<=5;$i++) catch(PDOException $e) $pdo = null
 Closes the database connection.
 
 exit
 < Redirects to the main page after successful insertion.
 

Detailed Description

This script provides validation of inputs of user publish-insertion form, including the attached files.

Validates every field of the form. Resizes images and saves them in the local /media/ folder by newly generated names. Avatar is resized and saved separately by the newly generated name in the local /avatar/ folder. Creates new record in the 'insertions' table of the database and saves insertion data, including the address of the avatar. Addresses of the regular images are saved into the 'images' table by insertion_id and their order_number among the images of this particular insertion. Terminates the scripts, deletes all the recently-saved images of current insertion in /media/ and /avatar/ folders and redirects back to publish-insertion form with error context parameter and all the user inputs except files and values of select fields as parameters ofthe redirecting GET request in case of error of data validation or database communication.

Function Documentation

◆ is_valid_number()

is_valid_number ( $value,
$min_value,
$max_value )

Validates if a number is within a specified range.

Parameters
mixed$valueThe value to validate.
int$min_valueThe minimum allowed value.
int$max_valueThe maximum allowed value.
Returns
bool True if valid, false otherwise.

◆ is_valid_select()

is_valid_select ( $value)

Validates if a selection is valid (not default or unset).

Parameters
string$valueThe value to validate.
Returns
bool True if valid, false otherwise.

◆ is_valid_text()

is_valid_text ( $value,
$max_length )

Validates the length of a text field.

Parameters
string$valueThe text value to validate.
int$max_lengthThe maximum allowed length.
Returns
bool True if valid, false otherwise.

◆ resizeImage()

resizeImage ( $sourceImage,
$newWidth,
$newHeight )

Resizes an image to the specified dimensions while maintaining aspect ratio.

Parameters
resource$sourceImageThe source image resource.
int$newWidthThe new width of the image.
int$newHeightThe new height of the image.
Returns
resource The resized image resource.

Checks if there was a database access error. If so, terminates the operation and removes temporary files.

Parameters
string$errorCodeError code to pass.
array$savedFilesList of saved files to be removed.
string$avatar_uniqueFileNameAvatar file name to be removed.

Handles PDO exceptions during insertion into insertions table. Terminates the operation and removes temporary files.

Handles PDO exceptions during insertion into images table. Terminates the operation and removes temporary files.

◆ terminatePublishInsertionWithError()

terminatePublishInsertionWithError ( $errorParam)

Redirects to the publish-insertion page with error context parameter and all the user inputs except files and values of select fields as parameters ofthe redirecting GET request in case of error of data validation or database communication.

Parameters
string$errorParamThe error parameter to append to the query string.

◆ terminateWithRemove()

terminateWithRemove ( $errorParam,
$mediaFiles,
$avatarFile )

Cleans up uploaded files and redirects with the specified error parameter.

Parameters
string$errorParamThe error parameter to append to the query string.
array$mediaFilesThe list of media files to remove.
string$avatarFileThe avatar file to remove.

Variable Documentation

◆ $allowedExtension

$allowedExtension = 'jpg'

◆ $avatar_uniqueFileName

$avatar_uniqueFileName = "filename"

◆ $avatar_uploadDir

$avatar_uploadDir = __DIR__ . '/avatar/'

◆ $description

$description = $_POST['description'] ?? ''

◆ $engine_capacity

$engine_capacity = $_POST['engine_capacity'] ?? ''

◆ $fuel

$fuel = $_POST['fuel'] ?? ''

◆ $insertion_id

$insertion_id = $pdo->lastInsertId()

Gets the last inserted ID for the insertions table.

◆ $isFormValid

$isFormValid = true

◆ $make

$make = $_POST['make'] ?? ''

◆ $maxFileSize

if(!is_dir($uploadDir) &&!mkdir($uploadDir, 0777, true)) if (!is_dir( $avatar_uploadDir) &&!mkdir( $avatar_uploadDir, 0777, true)) $maxFileSize = 40 * 1024 * 1024

◆ $mileage

$mileage = $_POST['mileage'] ?? ''

◆ $model

$model = $_POST['model'] ?? ''

◆ $number_fields_max_values

$number_fields_max_values
Initial value:
= [
'price' => 20000000,
'year' => 2026,
'mileage' => 3000000,
'power' => 5000,
'engine_capacity' => 8000,
]

◆ $number_fields_min_values

$number_fields_min_values
Initial value:
= [
'price' => 10000,
'year' => 1980,
'mileage' => 0,
'power' => 30,
'engine_capacity' => 1000,
]

◆ $pdo

for($i=1; $i<=5; $i++) catch (PDOException $e) $pdo = null

Closes the database connection.

◆ $power

$power = $_POST['power'] ?? ''

◆ $price

$price = $_POST['price'] ?? ''

◆ $savedFiles

$savedFiles = []

◆ $short_description

$short_description = $_POST['short_description'] ?? ''

◆ $text_fields_max_lengths

$text_fields_max_lengths
Initial value:
= [
'short_description' => 50,
'description' => 600
]

◆ $uploadDir

if(! $isFormValid) if (empty( $_FILES)) $uploadDir = __DIR__ . '/media/'

◆ $year

$year = $_POST['year'] ?? ''

◆ exit

exit

< Redirects to the main page after successful insertion.

Terminates the script.

◆ for

for($i=1; $i<=5; $i++) ( $i = 1; $i <= 5; $i++)

Includes the database connection file.

◆ try

catch (PDOException $e) try