June 13, 2007

external_example.php

Filed under: ZenMagick — DerManoMann @ 3:53 am

I think it would be good to dwell a bit longer on external_example.php as it seems to be of particular interest.

external_example.php was added in release 0.8.0.1 and is intended to illustrate how folks could execute controller programmatically. While this works (to a degree), there are a few serious limitations I might have forgotten to mention :/

First of all, zen-cart does some really weiyrd things with $_POST and $_GET. Even if posing forms, some values are expected in $_GET. I am not sure whether that is sloppy or a feature. In any case, that is why zm_form(..) and zm_secure_form(..) create get parameter for the action URL and hidden parameter. So, when faking requests, this needs to be considered.

Another thing that is documented, but perhaps not obvious in this context is, that there are only a few ZenMagick controller that actually implement processPost(). The list of supported controller (pages) is inthe setting postRequestEnabledList.

So, for all requests you want to simulate that are not in that list, it is currently required to use zm_call_zc_page(..).

One downside of using zm_call_zc_page(..) is, that zen-cart executes redirects straight away. Even worse, zen_redirect(..) finishes with calling zen_exit(), which wll call exit(), thus ending all code execution. I guess there is not a lot to be done about that, execpt perhaps for patching zen_redirect(..) /zen_exit(). Still, that leaves the problem of additional code/output. However, that might be taken care of by wrapping the call in ob_start() and ob_end_clean() to avoid any output and to clear the generated header (is that possible?)

So, without additional work there are limits as to which requess can be faked. I realize this might be a big disapointment for some people, but that’s how it is.

To add further pain, the cart actions are currently not called when using zm_call_zc_page(..), as they are part of an init script which are not called again…

Well, not all ist lost (yet). As I am writing this, I have a working version of external_example.php that can actually add to the cart - haven’t tested any further, though.

To make it work requires a manual patch to zen-cart’s zen_redirect(..) function. The following line needs to be inserted before the redirect header is added:

if (defined('ZM_EXTERNAL_CALL')) { global $zen_redirect_url; $zen_redirect_url = $url; return; }

I gues this will be it until the next release…

EDIT: One thing I should also mention is that running external_example.php will invalidate any other session the configured user might have open.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.