PDA

View Full Version : [FIXED]Programmer help required to update an old but good plugin


stardotstar
05-21-2009, 02:32 AM
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 />&nbsp;</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 ('&amp;cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
document.write ("&amp;loc=" + escape(window.location));
if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&amp;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...

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

stardotstar
05-22-2009, 05:23 AM
I am hot on the trail of this problem but still need help from a competent php programmer.

The problem lies with a bug in openx
https://developer.openx.org/jira/browse/OX-3620

This means that calls out to local mode will result in the board's zone setting (or that of the user's session) being set to UTC.

I am trying to work out how to implement a simple save and reset before and after the plugin call respectively but so far with no success.

Any suggestions or help/coaching would be appreciated.
Will

Something like
$script_tz1 = date_default_timezone_get();


// 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} -->";

date_default_timezone_set($script_tz1);
}
}
}

}

this method suggested by someone experiencing the problem and troubleshooting it on vb.com

http://www.vbulletin.com/forum/showthread.php?p=1738499#post1738499

Can't wait to get this sorted - it should mean we can do an update to phpadsnew integration for openx2.8 and vB3.8.2

Will

stardotstar
05-31-2009, 12:53 PM
I notice when I do a scan of my php info the date section shows correct timezone but not a set value...

date
date/time support enabled
"Olson" Timezone Database Version 2009.1
Timezone Database internal
Default timezone Australia/ACT

Directive Local Value Master Value
date.default_latitude 31.7667 31.7667
date.default_longitude 35.2333 35.2333
date.sunrise_zenith 90.583333 90.583333
date.sunset_zenith 90.583333 90.583333
date.timezone no value no value

does this ring any bells!

Tigerdude
06-01-2009, 02:00 PM
Good luck with this. I'd love to help but my skills are far less than your's.

I'm going to be using OpenX Ad sever with VBulletin 3.8 with VBSEO installed. I'll keep an eye on this thread and if my developers come up with a solution, I'll post it here.

stardotstar
06-02-2009, 11:54 AM
Please do; I am doing my best to wrangle in the kind of help required to troubleshoot this.

stardotstar
06-04-2009, 09:58 AM
I have not solved this yet but managed to most terribly mangle my board in the process of hacking it out :oops: I was able to fix it but it was a close one!

--------------- Added 1244188915 at 1244188915 ---------------

OK, I have a dev board running without any modifications - clean 3.8.2 install; nothing in database except a single test post.

Installed mod and turned it on; as soon as the ad is invoked using local mode = zap! time zone switches from the +10 I had selected in the UCP to UTC.

Very curious.

I am going to troubleshoot this from the dev board after nearly breaking my main ship last night trying to find a solution.

Will

I may as well blog in this thread while I flounder around looking for a solution :)

Will

--------------- Added 1244189068 at 1244189068 ---------------

I have my default time zone defined in php.ini so that is not it:

date
date/time support enabled
"Olson" Timezone Database Version 2009.1
Timezone Database internal
Default timezone Australia/Sydney

(love this automerge recent topic replies mod - must look for that one :) )

stardotstar
06-06-2009, 11:48 AM
In an effort to keep this effort going I have grabbed a screen shot of two renderings of the same index.

This is a vanilla vB 3.8.2 with only the openX integration applied.

The window on the left is a template with the <ad.../> tags invoked. The window on the left shows the same index with a clean style.
http://www.sourcepoint.com.au/outlinks/TimeZoneProb.jpg
Notice that the time is correct in both cases as stated by the board time at the bottom of the main page - this is the native time zone for the board since even though I am logged in my user timezone is the same as the server. Guests would see exactly the same in each case.

The Posts and Most Users Online dates (and PMs etc) all show back at UTC with the view_ad function called - but not the date at the bottom and importantly only for display - not effecting in any way the accuracy of the clock for database purposes (thankfully)

I think therefore that I need to find out how to protect whatever value/variable is being used to calculate the times that are being written out after the view_ad function call.

The way I tried to do this is to save a value, then reset it before exiting the plugin but either it didn't work or it was the wrong variable etc etc etc.

I know I am blogging this but I would still appreciate any insight or assistance from a more skillful hand if one is available to look at the problem.

The board in question can be accessed and is my dev board.

Will

stardotstar
06-15-2009, 02:28 AM
Fixed.

I have reported the fix for php5, openX 2.8, vB3.8.3 in the mod thread:
https://vborg.vbsupport.ru/showthread.php?p=1829889#post1829889

Almost absurdly simple.