flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.6k stars 407 forks source link

get_input_data #565

Closed planework closed 3 months ago

planework commented 3 months ago

Sure! Here's the GitHub pull request with the complete code including comments in English:


Title: Proposal: Addition of IO function for versatile input/output handling

Description: This pull request proposes the incorporation of an IO function to facilitate flexible input/output operations within the project. The IO function simplifies data retrieval and sanitization from various sources, including GET, POST, JSON, cookies, files, and the request superglobal.

Changes:

Code:

<?php
// Function to handle input/output operations
function IO($field = '', $default = null, $filter = FILTER_SANITIZE_STRING) {
    // If field is empty, return all data from either GET or POST
    if ($field === '') {
        $request_method = strtolower($_SERVER['REQUEST_METHOD']);
        switch ($request_method) {
            case 'get':
                return filter_input_array(INPUT_GET, $filter);
            case 'post':
                return filter_input_array(INPUT_POST, $filter);
            default:
                return $default;
        }
    }

    // Parse field name and request type
    $request_type = null;
    if (strpos($field, 'get.') === 0) {
        $request_type = 'get';
        $field = substr($field, 4);
    } elseif (strpos($field, 'post.') === 0) {
        $request_type = 'post';
        $field = substr($field, 5);
    } elseif (strpos($field, 'json.') === 0) {
        $request_type = 'json';
        $field = substr($field, 5);
    } elseif (strpos($field, 'cookie.') === 0) {
        $request_type = 'cookie';
        $field = substr($field, 7);
    } elseif (strpos($field, 'file.') === 0) {
        $request_type = 'file';
        $field = substr($field, 5);
    } elseif (strpos($field, 'request.') === 0) {
        $request_type = 'request';
        $field = substr($field, 8);
    } else {
        $request_type = strtolower($_SERVER['REQUEST_METHOD']);
    }

    // Determine request data
    switch ($request_type) {
        case 'get':
            $data = $_GET;
            break;
        case 'post':
            $data = $_POST;
            break;
        case 'json':
            $json_data = file_get_contents('php://input');
            $data = json_decode($json_data, true);
            break;
        case 'cookie':
            $data = $_COOKIE;
            break;
        case 'file':
            $data = $_FILES;
            break;
        case 'request':
            $data = $_REQUEST;
            break;
        default:
            // If request data is not found, return default value
            return $default;
    }

    // Handle nested field names
    if (!empty($field)) {
        $keys = explode('.', $field);
        foreach ($keys as $key) {
            if (is_array($data) && isset($data[$key])) {
                $data = $data[$key];
            } else {
                // If data is not found, return default value
                return $default;
            }
        }
    }

    // Check if data is empty, if so return default value
    if ($data === '' || $data === null) {
        return $default;
    }

    // Sanitize data
    if (is_string($data)) {
        $data = filter_var($data, $filter);
    } elseif (is_array($data)) {
        // Sanitize each value in the array
        foreach ($data as $key => $value) {
            $data[$key] = filter_var($value, $filter);
        }
    }

    // Return sanitized data
    return $data;
}

// Usage examples
$field1 = 'get.user.name';
$field2 = 'post.user.file.name';
$field3 = 'json.data.user.name';
$field4 = 'cookie.session_id';
$field5 = 'file.uploaded_file.name';
$field6 = 'request.password';
$field7 = 'notfound'; // Default value is 'Not Found'

$data1 = IO($field1, 'Default Name');
$data2 = IO($field2, 'Default File Name');
$data3 = IO($field3, 'Default JSON Name');
$data4 = IO($field4, 'Default Session ID');
$data5 = IO($field5, 'Default File Name');
$data6 = IO($field6, 'Default Password');
$data7 = IO($field7, 'Custom Default Value');

echo $data1 . "\n";
echo $data2 . "\n";
echo $data3 . "\n";
echo $data4 . "\n";
echo $data5 . "\n";
echo $data6 . "\n";
echo $data7 . "\n";
?>

Usage:

$field1 = 'get.user.name';
$data1 = IO($field1, 'Default Name');
echo $data1; // Outputs the sanitized user name from the GET request or 'Default Name' if not found or invalid
// Similar usage examples for other request types

Testing:

Notes:

Related Issue: [Link to any related issue or discussion if applicable]


Please review the proposed changes and consider integrating them into the project. If you have any feedback or questions, feel free to share them. Thank you for your attention to this enhancement proposal!

n0nag0n commented 3 months ago

Hey thank you for the suggestion, I guess my only question is I'm not sure where this is coming from nor what it's meant to replace? Do you have a use case using Flight where you ran into problems and why this solves that problem?

planework commented 3 months ago

Hey thank you for the suggestion, I guess my only question is I'm not sure where this is coming from nor what it's meant to replace? Do you have a use case using Flight where you ran into problems and why this solves that problem?

No, just offering some suggestions. This is inspired by the usage in other frameworks, and it's a piece of code written using ChatGPT. I found it quite good, so I'm sharing it unconditionally.

n0nag0n commented 3 months ago

Looking this over, I see the intention, but half of it is already solved in Flights implementation, and the sanitizing bit I feel should be an intentional decision by the end coder. When the framework starts manipulating the incoming data it causes confusion and endless bug tracing to figure out what's really happening. There is another repo with sanitizing functionality that could be incorporated into flight https://github.com/Lawrence72/sanitizer