Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Strange PHP problem (preg_match_all)

  • 01-02-2013 2:43pm
    #1
    Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭


    Hey folks, unusual issue here and I thought I would bounce it off ye to see if it was something any of you had come across before.

    So, I have a site that uses realex for payment, in case you don't know, you send realex some xml, and they sent you back a response which contains information like weather or not the payment was successful which we can then process.

    Last night, this website, which was using a commercially available plugin for realex that's available for a popular e-commerce platform, started rejecting every order.

    So I took a little dig. The code was processing the order response with this

    [PHP]function get_tag( $tag, $xml ) {
    $tag = preg_quote($tag);
    preg_match_all('{<'.$tag.'>(.*?)</'.$tag.'>.}', $xml, $matches, PREG_PATTERN_ORDER);

    return $matches[1];
    }[/PHP]The return value takes the form of an array too, so where the code might call $var = get_tags('request', $response); you would access the value found with <response> tags in the sql with $var[0].

    It's a little unusual, but as I said, it was a commercially available plugin, and I had not even looked at the code when I bought it.

    So I tested this locally (xampp on windows), I mocked up a fake respnse request and passed it through that function to see if I got matches. It worked fine.

    I put the same file up on the server, and the arrays came back empty.

    Keep in mind, this script was working fine until some stage yesterday, the site has not been touched in months (It's on it's own server, so nothing new has been installed there, and I can't see any update notifications from plesk).

    Now, I was able to fix the problem and get the site working easilly enough by doing a quick hack to get it working by changing it to the following
    [PHP]function get_tag($tag, $xml) {
    $xml = (array) simplexml_load_string($xml);
    return array($xml[$tag]);
    }[/PHP]But I am still curious, have any of you come across a similar issue before? Where preg_match works fine on one server, and not on another (no errors) or, more importantly, randomly stops working?


Comments

  • Moderators, Society & Culture Moderators Posts: 17,643 Mod ✭✭✭✭Graham


    As you've ruled out changes to the site, and general realex problems would have generated more noise in general. Has there been any recent changes to the platform the site is hosted on?


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭oeb


    Graham wrote: »
    As you've ruled out changes to the site, and general realex problems would have generated more noise in general. Has there been any recent changes to the platform the site is hosted on?


    Not that I am aware of, plesk happilly runs auto-updates, I was under the impression that they were to the core plesk install rather than the deamons running on the server though. It's not a managed server, and I have not even logged into that instance since before xmas.


  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    Are you passing the exact response locally as you are getting on the server? Can you post it up for the laugh? Mocking up anything sensitive

    Are they the same version of PHP on the server as you're testing locally?


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭oeb


    Dave! wrote: »
    Are you passing the exact response locally as you are getting on the server? Can you post it up for the laugh? Mocking up anything sensitive

    Are they the same version of PHP on the server as you're testing locally?


    I'll lob it up on Monday, the test script is on my work machine.


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭oeb


    In case anyone was wondering, I managed to track down what was causing this.

    Realex recently changed their production servers from windows to linux, so the xml response that was generated previously had windows line endings, now it has linux ones, and the regular expression was not matching on those.

    I don't know why the developer (Of the only commercial realex plugin for opencart) chose to parse the xml using a regular expression rather than using a library in the first place, and the fix that he has supplied still uses a regular expression, just a different one.

    The fix I posted in the first post still works and is more forward compatible, I have also supplied a code fix to realex and they will probably pass it on to anyone who needs it.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,657 ✭✭✭komodosp


    Ha, I was having the exact same problem a while back - I was on to Realex asking if they'd changed anything and they didn't know what I was on about...

    I put a question mark at the end of that regex you have there and it worked fine.

    [PHP]
    preg_match_all('{<'.$tag.'>(.*?)</'.$tag.'>.?}', $xml, $matches, PREG_PATTERN_ORDER);
    [/PHP]


Advertisement