Jump to content

Need a programmers help troubleshooting an old but good product/plugin for vB


Recommended Posts

G'Day guys, I am sort of cross posting because I am trying to find a place where I can be taught how to troubleshoot my vB modding myself and develop the skills to take my php to the next level.

 

I have an interesting problem that I have gone some way to troubleshooting and intend once I have it whipped to write a step by step update for the existing product that integrates vBulleting with phpadsnew - now openx.  I have the product working but it is smashing my time zone variable and I am certain that it is something to do with calls to external includes in openx - even though the timezone is properly defined everywhere.

 

Please bear with me as I try to explain the problem from the top and then list the troubleshooting I have done to solve it without success so far.

 

Hi all,

 

I am new at building product/plugins for vB - I spent much time with phpBB cutting my teeth on the mods for the 2.x series.

 

I am getting my head around calling php from external programmes and in this case I am using a product from vB.org that integrates phpAdsNew - now openx.

 

The problem I have found there is that it is only supported up to vB3.5 and after playing with it I got it working just fine on 3.8.2 with one major flaw.

 

I needed to use what openx calls local mode invocation since the ad server and vB are on the same physical machine - using javascript is very slow when there are a few ads on the page.

 

This apparently is a simple call to openx server in php called view.raw with the necessary variables set.

 

Having got the product working and the necessary calls into the templates - it uses a new tag <ad /> I got my dearly needed speed increase (further improved by implementing memchached).

 

But suddenly I notice that the post timestamps are skewed into the past (about 10 or 11 hours - the same as taking off the GMT offset - on my board +10).  I checked the server = ok, I sent emails = OK, I checked an unmodded board = ok and therefore started to troubleshoot.

 

I found that in my templates/styles if I removed any reference to the invocation of the phpAdsNew integration the time would revert to normal offset.

 

Therefore the time is not being altered only displayed incorrectly - and only within certain frames - for example the time is incorrect (- 10 hours) in personal message received times and all posts within threads and indexes etc BUT not at the very bottom of the board where the board time is displayed eg:

 

All times are GMT +10. The time now is 1:09pm.

 

So I would see this post if I had lodged it just then as 03:09am and if I took the call to the openx server out it would revert to 13:09 or 01:09pm as it should be.

 

Note that the server is set to Localtime (+10) and the time is correct, the time is also correctly set within openx and I am investigating there what may be happening when the call is made.

 

Interestingly the use of javascript to pull the ad into the board (although slow) does not cause this time skew - only the "local mode" call.

 

 

Here is what I think is happening.

 

When a page is generated the header has a call to another template which contains the "invocation code" for the ad.  In the past I have used Javascript here.

 

so:

 

<td align="$stylevar[left]" id="header_right_cell">
        <if condition="$ad_location['ad_header_logo']">$ad_location[ad_header_logo]<else /> </if>
    </td>

in the header serves an ad up to the right of the site logo.

 

ad_header_logo is now simple:

 

<ad what="zone:1" />

 

where in the past it was the javascript invoation code like this:

<!--/* OpenX Javascript Tag v2.8.0 */-->
<script type='text/javascript'><!--//<![CDATA[
   var m3_u = (location.protocol=='https:'?'https://www.sourcepoint.com.au/openx/www/delivery/ajs.php':'http://www.sourcepoint.com.au/openx/www/delivery/ajs.php');
   var m3_r = Math.floor(Math.random()*99999999999);
   if (!document.MAX_used) document.MAX_used = ',';
   document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
   document.write ("?zoneid=1");
   document.write ('&cb=' + m3_r);
   if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used);
   document.write (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : ''));
   document.write ("&loc=" + escape(window.location));
   if (document.referrer) document.write ("&referer=" + escape(document.referrer));
   if (document.context) document.write ("&context=" + escape(document.context));
   if (document.mmm_fo) document.write ("&mmm_fo=1");
   document.write ("'><\/scr"+"ipt>");
//]]>--></script>

 

So far so good.  The local mode call invokes the following two plugins as part of the product phpAdsNew Integration

 

ad_tag_callback in template_compile execution order 5

if (!function_exists('_tag_callback'))
{
    /**
    * Callback-function for process_template_tag() which "parses" arguments and returns PHP code for eval
    *
    * @param    string    Options/arguments of tag
    * @param    string    Name of function to call
    * @param    array    Associative array of argument names and default values
    *
    * @return    string
    */
    function _tag_callback($options, $functionhandle, $arglist)
    {
        $options = stripslashes($options);
        trim($options);
        trim($functionhandle);

        if (!function_exists('replace_template_variables'))
        {
            require_once(DIR . '/includes/functions_misc.php');
        }

        $param = array();
        if (is_array($arglist))
        {
            reset($arglist);
            foreach ($arglist AS $key => $val)
            {
                if (preg_match('#'.$key.'=([\\\]["\'])(.*?)\1#', $options, $matches))
                {
                    $param[] = $matches[2];
                }
                else
                {
                    // default argument
                    $param[] = $val;
                }
            }
        }
       
        foreach ($param AS $argument)
        {
            if ($return == '')
            {
                $return = '" . ' . $functionhandle . '(';
            }
            else
            {
                $return .= ', ';
            }
            // Surround variables with {} - not failsafe, but should do the job in most cases
            $argument = preg_replace('#\$([a-z0-9_>-]+)((\[\'.*?\'\])+)#i', '{$\\1\\2}', $argument);
            // {{$foo}} --> {$foo}
            $argument = preg_replace('#(\{+)\$(.*?)(\}+)#i', '{$\\2}', $argument);

            // Replace legacy variable names in templates with their modern equivalents
            $argument = replace_template_variables($argument, true);

            $return .= '"' . $argument . '"';
        }
        $return .= ') . "';
        return $return;
    }
}

if (!function_exists('process_template_tag'))
{
    /**
    * Processes user-defined tags <tagname [argument="foo" ...]/> into myfunction() PHP code for eval
    *
    * @param    string    Title of tag
    * @param    string    Un-parsed template HTML
    * @param    string    Name of function to call
    * @param    array    Associative array of argument names and default values, order must be the same as for the function specified previously
    *
    * @return    string
    */
    function process_template_tag($tagname, $text, $functionhandle='', $arglist)
    {
        if ($functionhandle == '')
        {
            // No function specified - remove tag
            return preg_replace("#<{$tagname}\s?/?>|<{$tagname}\s.*?/?>#si", '', $text);
        }
        else
        {
            return preg_replace("#<({$tagname})(\s+(.*?)/?)>#sie", "_tag_callback('\\3', \$functionhandle, \$arglist)", $text);
        }
    }
}

$template = process_template_tag('ad', $template, 'view_ad', array('what' => '', 'clientid' => 0, 'target' => '', 'source' => '', 'withtext' => 0)  );

 

Then we have

view_ad function in init_startup also at 5

 

if (!function_exists('view_ad'))
{
    /**
    * Calls view_raw function from phpAdsNew either locally or via xml-rpc to get HTML advertisement code, see phpAdsNew for infos.
    *
    * @param    string    Zone name (zone:x) or keywords (keyword1|keyword2|...)
    * @param    integer    Client-ID
    * @param    string    Target
    * @param    string    Source
    * @param    integer    Withtext
    *
    * @return    string
    */
    function view_ad($what, $clientid = 0, $target = '', $source = '', $withtext = 0)
    {
        global $vbulletin;
        static $panpath;
       
        if (!$panpath)
        {
            trim($vbulletin->options['panpath']);
            $panpath = @parse_url($vbulletin->options['panpath']);
        }
       
        // Calculate an identifier for the requested ad - this could be used to cache banners retrieved via xmlrpc in future versions
        $adid = sprintf('%u', crc32($what . $clientid . $target . $source . $withtext));
       
        if ($vbulletion->options['addtemplatename'] or $vbulletin->config['Misc']['debug'])
        {
            // Be verbose
            $adcomment = 'id="' . $adid . '" what="' . htmlspecialchars($what) . '" clientid="' . htmlspecialchars($clientid)
                    . '" target="' . htmlspecialchars($target) . '" source="' . htmlspecialchars($source)
                    . '" withtext="' . htmlspecialchars($withtext) . '"';
        }
        else
        {
            $adcomment = $adid;
        }
       
        if ($vbulletin->options['panpath'] == '')
        {
            // Path to phpAdsNew is empty - disable ads
            return "<!-- ad {$adcomment} / -->";
        }
        elseif (strtolower($panpath['scheme']) == 'http')
        {
            // get banner from phpAdsNew via xml-rpc
            /*
            //    ATTN:
            //    In phpAdsNew 2.0.6 (and maybe earlier versions) is a tiny bug which prevents xml-rpc from working cleanly.
            //   
            //    Patch:
            //    --- phpAdsNew/misc/samples/xmlrpc/php/lib-xmlrpc.inc.php.orig    Tue Aug 16 10:51:26 2005
            //    +++ phpAdsNew/misc/samples/xmlrpc/php/lib-xmlrpc.inc.php    Thu Oct 27 17:12:26 2005
            //    @@ -76,6 +76,7 @@
            //            global $xmlrpcStruct;
            //     
            //            global $xmlrpcTypes;
            //    +        global $xmlrpc_valid_parents;
            //            global $xmlEntities;
            //            global $xmlrpcerr;
            //            global $xmlrpcstr;
            //           
            //    See https://sourceforge.net/tracker/?func=detail&atid=111386&aid=1339623&group_id=11386 for details.
            */
   
            global $xmlrpcbanner, $phpAds_remoteInfo;
            require_once(DIR . '/includes/lib-xmlrpc-class.inc.php');  // see misc/samples/xmlrpc/php/ directory of your phpAdsNew installation
   
            if (!$xmlrpcbanner)
            {
                $xmlrpcbanner = new phpAds_XmlRpc($panpath['host'], $panpath['path'], (intval($panpath['port']) > 0 ? $panpath['port'] : 80));
            }
   
            $ad = $xmlrpcbanner->view_raw($what, $clientid, $target, $source, $withtext);
   
            return "<!-- BEGIN ad {$adcomment} -->{$ad['html']}<!-- END ad {$adid} -->";
        }
        else
        {
            // get banner via direct invocation of phpAdsNew
            // this is basically taken from the invocationcode generated by phpAdsNew - it does its own checks to prevent multiple inclusion
   
            global $phpAds_context;
           
            if (@require($vbulletin->options['panpath'] . (strstr($vbulletin->options['panpath'] , '/phpadsnew.inc.php') ? '' : '/phpadsnew.inc.php' )))
            {
                if (!isset($phpAds_context))
                {
                    $phpAds_context = array();
                }
   
                $ad = view_raw($what, $clientid, $target, $source, $withtext, $phpAds_context);
               
                if ($vbulletin->options['panbandupes'])
                {
                    // Remember bannerid to prevent showing banner multiple times on same page
                    $phpAds_context[] = array('!=' => 'bannerid:'.$ad['bannerid']);
                }
   
                return "<!-- BEGIN ad {$adcomment} -->{$ad['html']}<!-- END ad {$adid} -->";
            }
        }
    }
}

 

 

The important part in this as far as I can see is the local mode not xmlrpc

 

else
        {
            // get banner via direct invocation of phpAdsNew
            // this is basically taken from the invocationcode generated by phpAdsNew - it does its own checks to prevent multiple inclusion
   
            global $phpAds_context;
           
            if (@require($vbulletin->options['panpath'] . (strstr($vbulletin->options['panpath'] , '/phpadsnew.inc.php') ? '' : '/phpadsnew.inc.php' )))
            {
                if (!isset($phpAds_context))
                {
                    $phpAds_context = array();
                }
   
                $ad = view_raw($what, $clientid, $target, $source, $withtext, $phpAds_context);
               
                if ($vbulletin->options['panbandupes'])
                {
                    // Remember bannerid to prevent showing banner multiple times on same page
                    $phpAds_context[] = array('!=' => 'bannerid:'.$ad['bannerid']);
                }
   
                return "<!-- BEGIN ad {$adcomment} -->{$ad['html']}<!-- END ad {$adid} -->";
            }

 

Can anyone see within this code what might cause the date/time stamp of the board to be replaced but only on each page display not within the actual time keeping itself?

 

You can see this phenomenon for you self without needing to log in at my board.  http://www.archeli.com.au/forums

 

It is also interesting to note that this does not effect the display of the time when "detailed" is used - I guess because that function doesn't care if the time is "zoned" as such since it is a "difference" not a literal stamp.

 

I have just tried to change the time to "Normal" and edited the time format to "h:i A e" which shows the time zone - and what do you know - UTC Is what is being delivered back to the pages as they render:

18-05-2009 06:07 AM UTC

instead of

18-05-2009 04:07 PM Australia/ACT

which is what is shown at the base of the pages.

 

The time of the post from the postbit is

 

$post[posttime]

 

and somehow that is getting transformed back to UTC...

 

 

I have found this code in a script called when openx is invoked.

 

Basically if I am right the product calls for a return value of ad$ thus:

return "<!-- BEGIN ad {$adcomment} -->{$ad['html']}<!-- END ad {$adid} -->";

 

This is done by including the phpadsnew.inc.php which in turn includes the variables.php

 

in variables.php there is a section where the timezone is played with - could this have something to do with the problem - resetting the timezone to UTC for the build of the page after the ad is served and not elsewhere?

 

How would I protect the timezone variable within vB>? if this is the case - and I know I am in way over my head!

 

 

/**
* Set a timezone
*
* @param string $timezone
*/
function OA_setTimeZone($timezone)
{
    // Set the new time zone
    date_default_timezone_set($timezone);

    // Set PEAR::Date_TimeZone default as well
    //
    // Ideally this should be a Date_TimeZone::setDefault() call, but for optimization
    // purposes, we just override the global variable
    $GLOBALS['_DATE_TIMEZONE_DEFAULT'] = $timezone;
}

/**
* Set the current default timezone to UTC
*
* @see OA_setTimeZone()
*/
function OA_setTimeZoneUTC()
{
    OA_setTimeZone('UTC');
}

/**
* Set the current default timezone to local
*
* @see OA_setTimeZone()
*/
function OA_setTimeZoneLocal()
{
    $tz = !empty($GLOBALS['_MAX']['PREF']['timezone']) ? $GLOBALS['_MAX']['PREF']['timezone'] : 'GMT';
    OA_setTimeZone($tz);
}

 

This is what got me thinking:

 

   // Ideally this should be a Date_TimeZone::setDefault() call, but for optimization
    // purposes, we just override the global variable
    $GLOBALS['_DATE_TIMEZONE_DEFAULT'] = $timezone;

 

Maybe I am barking up the wrong tree because a script I put in my root where the openx server is serves up the timezone correctly when called upon:

Your server timezone is: Australia/ACT

that would surely be the default across all the services included then?

 

That was using this:

<?php
    $calculated = false;
    if (version_compare(phpversion(), '5.1.0', '>=')) {
        // Great! The PHP version is >= 5.1.0, so simply
        // use the built in date_default_timezone_get()
        // function, and know it's all good
        $tz = date_default_timezone_get();
    } else {
        // Boo, we have to rely on the dodgy old TZ
        // environment variable stuff
        $tz = getenv('TZ');
        if ($tz === false || $tz === '') {
            // Even worse! The user doesn't have a TZ
            // variable, so we have to try and calcuate
            // the timezone for the user
            $calculated = true;
            unset($tz);
            $diff = date('O');
            $diffSign = substr($diff, 0, 1);
            if ($diffSign == "+") {
                $diffHour = (int) substr($diff, 1, 2) - date('I'); // minus 1 hour if date in DST
            } else {
                $diffHour = (int) substr($diff, 1, 2) + date('I'); // add 1 hour if date in DST
            }
            $diffMin  = (int) substr($diff, 3, 2);
            $offset = (($diffHour * 60) + ($diffMin)) * 60 * 1000; // Milliseconds
            $offset = $diffSign . $offset;
            global $_DATE_TIMEZONE_DATA;
            if (!isset($_DATE_TIMEZONE_DATA)) {
                include('Date/TimeZone.php');
            }
            reset($_DATE_TIMEZONE_DATA);
            foreach (array_keys($_DATE_TIMEZONE_DATA) as $key) {
                if ($_DATE_TIMEZONE_DATA[$key]['offset'] == $offset) {
                    $tz = $key;
                    break;
                }
            }
        }
    }
    echo "Your server timezone is: $tz<br />\n";
    if ($calculated) {
        echo "Note that this timezone is calculated. You should set up your timzone correctly.";
    }
?>

 

Any help would be appreciated, I am starting to get somewhere as a developer of this kind of code and will continue to ponder and submerge myself in the workings of vB and the products/openx.

 

Cheers

Will

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.