Download the PHP package bazarin/bazarin-php-library without Composer
On this page you can find all versions of the php package bazarin/bazarin-php-library. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bazarin/bazarin-php-library
More information about bazarin/bazarin-php-library
Files in bazarin/bazarin-php-library
Package bazarin-php-library
Short Description A PHP library for database operations, file management, API calls, and security utilities.
License MIT
Informations about the package bazarin-php-library
Bazarin PHP Library - Comprehensive Guide
Bazarin PHP Library is a lightweight and efficient PHP library for database operations, file handling, API interactions, and security functions.
📌 Installation
To install the library using Composer, run:
After installation, include the autoload file in your PHP project:
Usage
1. select()
Retrieve data from a table.
2. insert()
Add new data to a table.
3. update()
Modify existing records.
4. delete()
Remove records from a table.
Error Handling
Use a try-catch
block to handle exceptions:
Here's what you should add to the README file to document the RestClient
and ApiManager
classes:
API Library Documentation
This library provides a simple way to interact with RESTful APIs using PHP's cURL. It supports all standard HTTP methods (GET, POST, PUT, DELETE) and includes features such as custom headers, error handling, and debug logging.
Features
- Perform CRUD operations with RESTful APIs.
- Supports custom headers for authorization or other requirements.
- Handles HTTP errors and invalid JSON responses gracefully.
- Debugging mode for logging requests and responses.
- Easy-to-use methods for common API operations.
Installation
Simply download or clone this repository. Include the RestClient
and ApiManager
classes in your project.
Usage
Initialization
- The first parameter is an array of default headers.
- The second parameter enables or disables debug mode.
Methods
Fetch All Records
Fetches data using a GET request.
Create a Record
Sends a POST request to create a new resource.
Update a Record
Sends a PUT request to update an existing resource.
Delete a Record
Sends a DELETE request to remove a resource.
Advanced Features
Custom Headers
You can pass additional headers for specific requests:
Debugging Mode
Enable debug mode to log detailed information about each request and response:
Error Handling
The library automatically throws exceptions for:
- HTTP errors (status codes 400 and above).
- cURL connection errors.
- Invalid JSON responses.
Use a try-catch block to handle exceptions:
Example
The provided FileGetContent
class is designed to handle incoming HTTP requests with JSON payloads and apply CORS (Cross-Origin Resource Sharing) headers to allow secure access from specified origins. Below is an explanation and usage guide for the class, which you can also include in your README file.
FileGetContent Class Documentation
Features
- Enables CORS support for handling cross-origin requests.
- Processes incoming JSON payloads from HTTP requests.
- Handles
OPTIONS
preflight requests automatically for better CORS compliance.
Methods
Constructor: __construct($origin)
Initializes the class with the allowed origin for CORS requests.
- Parameter:
$origin
(string): The domain allowed to access this API. For example,'https://example.com'
.
cors_auth()
Handles CORS headers to allow secure communication between origins.
-
Adds the following headers:
Access-Control-Allow-Origin
: Matches the provided origin.Access-Control-Allow-Methods
: Specifies allowed HTTP methods (POST, GET, OPTIONS).Access-Control-Allow-Headers
: Lists allowed headers (e.g., Content-Type, Authorization).Access-Control-Allow-Credentials
: Indicates whether cookies and credentials are allowed.
- Automatically responds to
OPTIONS
requests with a 200 status code and exits. This is necessary for preflight requests in modern browsers.
get_content()
Processes the request body and decodes JSON payloads.
-
Returns:
- Associative array: Parsed JSON from the request body.
- If no JSON is sent or it is invalid,
null
is returned.
- Example:
Example Usage
Here is an example of how to use the FileGetContent
class in an API endpoint:
Notes
- CORS Configuration: Update the
$origin
parameter in the constructor to match your frontend domain. Use'*'
to allow all origins (not recommended for production). - Security: This class does not validate or sanitize the incoming JSON payload. Ensure that additional validation is performed as needed.
Great! The provided code uses AES-256-CBC encryption, which is a symmetric encryption method. It generates an encryption key, uses an initialization vector (IV), and supports both encryption and decryption processes. Below is the updated documentation for the Crypt
class using your provided code.
Crypt Class Documentation
Overview
The Crypt
class provides functionality for encrypting and decrypting data using the AES-256-CBC encryption method. The class uses a symmetric encryption system where the same key is used for both encryption and decryption. The key must be securely managed, as anyone with access to the key can decrypt the data.
Methods
__construct($key)
: Initializes the class with an encryption key.encrypt($data)
: Encrypts the provided data.decrypt($data)
: Decrypts the provided encrypted data.
1. __construct($key)
Description:
The constructor method initializes the encryption key used for both encryption and decryption.
Parameters:
$key
(string): The encryption key used for AES-256-CBC. It is crucial that this key is kept secret and secure.
Throws:
InvalidArgumentException
: If the provided key is empty.
Example Usage:
2. encrypt($data)
Description:
Encrypts the provided plaintext data using AES-256-CBC encryption. The method generates a random initialization vector (IV) for encryption to enhance security.
Parameters:
$data
(string): The plaintext data to encrypt.
Returns:
- (string): The encrypted data as a base64-encoded string, containing both the encrypted data and the IV.
Example Usage:
3. decrypt($data)
Description:
Decrypts the provided encrypted data using the AES-256-CBC method and the same encryption key. The method extracts the IV from the base64-encoded string and uses it for decryption.
Parameters:
$data
(string): The base64-encoded encrypted data containing the ciphertext and IV.
Returns:
- (string): The decrypted plaintext data.
Example Usage:
Important Notes
-
Key Management: The encryption key is critical for both encrypting and decrypting data. Ensure the key is kept secure and not exposed.
-
AES-256-CBC: This encryption method is considered very secure. It uses a 256-bit key and a 128-bit initialization vector (IV). The IV is randomly generated for each encryption operation to prevent identical data from having the same ciphertext.
-
Initialization Vector (IV): An IV is required for AES-CBC mode. It is generated randomly for each encryption to ensure that the same plaintext encrypted multiple times produces different ciphertexts. The IV is stored alongside the ciphertext in the base64-encoded string.
- Base64 Encoding: The encrypted data and IV are returned as a base64-encoded string. This is done so that binary data can be safely transmitted over protocols that only support text-based data.
Example Workflow
Encryption
- Encrypt data using the
encrypt()
method:
Decryption
- Decrypt data using the
decrypt()
method:
Conclusion
The Crypt
class provides an easy and secure way to encrypt and decrypt data using AES-256-CBC. The class ensures confidentiality by encrypting data with a secure algorithm and managing the initialization vector. To decrypt the data, the same key used for encryption is required.
Here is the documentation for the FileHelper
class:
FileHelper Class Documentation
The FileHelper
class provides utility functions for handling file uploads in PHP. It allows for uploading files to a specified destination with optional validation for file type and file size.
Methods
upload($file, $destination, $allowedTypes = [], $maxSize = 0)
This method is used to upload a file to the server. It optionally validates the file's type and size before moving it to the specified destination.
Parameters:
-
$file
(array) – The file to be uploaded, typically from the$_FILES
superglobal array (e.g.,$_FILES['file']
). -
$destination
(string) – The target location on the server where the file will be uploaded (e.g.,'uploads/filename.ext'
). -
$allowedTypes
(array, optional) – An array of allowed MIME types for the file. This is an optional parameter. If provided, the function will check that the file type matches one of the allowed MIME types. Example:['image/jpeg', 'image/png']
. $maxSize
(int, optional) – The maximum file size allowed in bytes. If set to a value greater than 0, the file size will be checked before upload. Example:2000000
(2MB).
Return Value:
string
– Returns the destination path of the uploaded file on success (e.g.,'uploads/filename.ext'
).- Throws Exception – If the file fails the type or size validation, or if the upload fails, an exception is thrown with a relevant error message.
Exceptions:
The method may throw the following exceptions:
- File upload error: If the file upload fails due to an internal error (e.g.,
$_FILES['file']['error']
). - Invalid file type: If the uploaded file's MIME type does not match the allowed types.
- File too large: If the uploaded file exceeds the maximum allowed size.
Example Usage:
In this example:
- The file is uploaded with type and size validation.
- If the file is of the wrong type or too large, an exception is thrown with an error message.
- On successful upload, the file path is returned.
Usage Notes:
-
If no validation is required, both
$allowedTypes
and$maxSize
can be left empty or set to default values (empty array and 0, respectively). -
The MIME type of the uploaded file is determined using PHP's
mime_content_type()
function. -
The file is moved using PHP's
move_uploaded_file()
function, and the destination is returned on success. - Exception handling should be implemented to catch errors related to file upload failures, invalid file types, or exceeding the file size limit.
Common Errors:
- File upload failed: This could be due to an invalid file, server misconfiguration, or permission issues.
- Invalid file type: The uploaded file type does not match any of the allowed MIME types.
- File too large: The uploaded file exceeds the maximum allowed size.
Here is the documentation for the DateHelper
class:
DateHelper Class Documentation
The DateHelper
class provides utility functions for working with dates in PHP. It includes a method to format a date according to a specified format.
Methods
format($date, $format = 'Y-m-d')
This method is used to format a given date into a specific format.
Parameters:
-
$date
(string|DateTime) – The date to be formatted. This can either be a string representing a date (e.g.,'2024-11-29'
) or aDateTime
object. $format
(string, optional) – The format in which the date should be returned. This is optional and defaults to'Y-m-d'
(e.g.,2024-11-29
). You can use any format supported by PHP'sDateTime::format
method. For example,'d/m/Y'
for29/11/2024
, or'l, F j, Y'
for'Friday, November 29, 2024'
.
Return Value:
string
– Returns the formatted date as a string.
Exceptions:
- InvalidArgumentException – If the provided
$date
cannot be parsed into a valid date, an exception will be thrown.
Example Usage:
In this example:
- A date string is formatted using the default format (
Y-m-d
). - A custom format (
d/m/Y
) is used to format the date. - A
DateTime
object is passed to theformat
method and is formatted as well.
Usage Notes:
-
The
$date
parameter can be a string (e.g.,'2024-11-29'
) or an instance ofDateTime
. If a string is passed, it will be converted into aDateTime
object internally. -
The
$format
parameter is optional. If not provided, the default format ('Y-m-d'
) will be used. The format string can follow PHP’sDateTime::format
syntax. - If the provided
$date
string is not a valid date, the method will throw an exception.
Common Errors:
- Invalid date format: If the provided date string is invalid, it will cause a parsing error when creating a
DateTime
object.
Requirements
- PHP 7.4 or higher.
- cURL extension enabled.
License
This project is licensed under the MIT License. See the LICENSE file for details.