Post by rph » Sun Sep 07, 2014 12:43 pm

I'm not sure how this will be received but I'd like to start putting together some more formal development-centric RFCs to go along with the general improvement suggestions at http://opencart.uservoice.com/forums/52387-general

RFC: Request Class Superglobal Default Values/Methods

This is a proposal to modify OpenCart's Request class to return a default value should the superglobal value not exist. Currently superglobal values are stored as Request class properties and directly accessed by the cart. This change is best achieved by modifying the Request class to return superglobals through a method rather than object properties.

Proof of Concept

Code: Select all

class Request {
    private $get = array();
    ...

    public function __construct() {
        $this->get = $_GET;
        ...
        unset($_GET);
    }

    public function get($value, $default = null, $raw = false) {
        return $this->getValue($this->get, $value, $default, $raw);
    }

    public function post($value, $default = null, $raw = false) { ... }

    public function cookie($value, $default = null, $raw = false) { ... }

    public function files($value, $default = null, $raw = false) { ... }

    public function server($value, $default = null, $raw = false) { ... }

    public function env($value, $default = null, $raw = false) { ... }

    public function clean($data) { ... }

    private function getValue($global, $value, $default, $raw) {
        $data = $default;

        if (isset($global[$value])) {
            $data = $global[$value];
        }

        if ($raw !== true) {
            $data = $this->clean($data);
        }

        return $data;
    }
}
Full PoC code hosted on gist.

Advantages

This would allow existing code:

Code: Select all

if (isset($this->request->get['filter_price'])) {
    $filter_price = $this->request->get['filter_price'];
} else {
    $filter_price = null;
}

if (isset($this->request->get['order'])) {
    $order = $this->request->get['order'];
} else {
    $order = 'ASC';
}
To be significantly shortened to:

Code: Select all

$filter_price = $this->request->get('filter_price'); // default null
$order = $this->request->get('order', 'ASC');  // default ASC
Additionally an optional $raw parameter allows superglobal values to be accessed without htmlspecialchars() encoding.

Code: Select all

$this->request->post('password', null, true);
This would be useful in those instances where full encoding may not be appropriate such as submitting a password to a third-party API.

This feature will allow for the return of superglobal unsetting which was removed in commit 360788e5. Unsetting superglobals increases security by ensuring third-party mods are not accidentally using unsanitized data.

Impact

The two superglobal access approaches can coexist. A staggered removal of Request properties could occur (deprecation in OC 2.0, removal in 2.1) but with with major backward compatibility breaks of OpenCart 2.0 it would be advisable to plan for a full removal in that release. Third-party mods will already need to be updated at that time.

The following areas of the OpenCart core will be affected:
  • All $this->request->{superglobal}[] properties would need to be updated to $this->request->{superglobal}() methods
  • if/else assignments such as the examples should be updated to use default returns
  • Logic relying on an isset() check of a superglobal would need to be changed to !is_null()
References

The following frameworks/ecommerce platforms use similar methodologies:

Laravel - http://laravel.com/docs/requests
Phalcon - http://docs.phalconphp.com/en/latest/ap ... quest.html
CodeIgniter - https://ellislab.com/codeigniter/user-g ... input.html
Symfony2 - http://symfony.com/doc/current/componen ... ction.html
Prestashop - http://doc.prestashop.com/display/PS15/ ... ols.php%29

Updates

2014-09-07: Added superglobal unsetting, full PoC code, and references

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by Johnathan » Sun Sep 07, 2014 10:54 pm

Great suggestion -- if you haven't already, I'd try marking it in the OpenCart bug tracker for James, to see if he'll incorporate it for 2.0, just in case he doesn't see this post.

Image
Image Image Image Image


User avatar
Global Moderator

Posts

Joined
Fri Dec 18, 2009 3:08 am


Post by rph » Mon Sep 08, 2014 4:23 am

Thanks Johnathan! I went and added it on UserVoice since these suggestions sometimes go a bit less than ideal on GitHub. It would be nice to have a more formal space for these topics. UserVoice is more end-user feature requests and GitHub more for bugs.

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by Qphoria » Mon Sep 08, 2014 11:46 am

+1 this idea

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am
Who is online

Users browsing this forum: No registered users and 8 guests