PHP: RBC (Really Bad Code) Posted by: Jordan in PHP on
I decided to make a quick text link addon for OpenX.  OpenX is a powerful banner ad rotator that has a multitude of features for advertisement.  With OpenX a website (or collection of websites) could zone, host and deliver millions of impressions per day.  Needless to say, the project is used by some very large players.

In order to write my addon I needed to see the functions and variables that were at my disposal.  I opened the local delivery file and was appalled by this code.  I'll paste it below.

I've left all code untouched, including indention.  This is from the /www/dlivery/alocal.php and is just a portion of the code but it all generally looks like this.


 
  1. function MAX_marketplaceNeedsId()
  2. {
  3. $aConf = $GLOBALS['_MAX']['CONF'];
  4. if (MAX_marketplaceEnabled()) {
  5. $oxidOnly = $aConf['marketplace']['cacheTime'] == 0;
  6. $viewerId = MAX_cookieGetUniqueViewerId(false, $oxidOnly);
  7. return !isset($viewerId);
  8. }
  9. return false;
  10. }
  11. function MAX_marketplaceGetIdWithRedirect($scriptName = null)
  12. {
  13. $aConf = $GLOBALS['_MAX']['CONF'];
  14. if (MAX_marketplaceEnabled()) {
  15. if (MAX_marketplaceNeedsId() && !isset($_GET['openxid'])) {
  16. $scriptName = isset($scriptName) ? $scriptName : basename($_SERVER['SCRIPT_NAME']);
  17. $oxpUrl = MAX_commonGetDeliveryUrl($scriptName).'?'.$_SERVER['QUERY_STRING'].'&openxid=OPENX_ID';
  18. $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http').'://'.
  19. $url .= $aConf['marketplace']['idHost'].'/redir?r='.urlencode($oxpUrl);
  20. $url .= '&pid=OpenXDemo';
  21. $url .= '&cb='.mt_rand(0, PHP_INT_MAX);
  22. header("Location: {$url}");
  23. exit;
  24. }
  25. }
  26. }


At first glance I see 5 problems in the code above and there is probably more.  What problems do you see?

Trackback(0)
feed4 Comments
mikelbring
October 17, 2008
Votes: +0

Well the first thing I would do is at least use tabbing and better code structure.

report abuse
vote down
vote up
Brandon W
October 17, 2008
Votes: +0

Yer I must agree with above.

It's to messy and way to hard to understand, you get this with a fair bit of downloadable scripts.

report abuse
vote down
vote up
shibbythestoner
October 17, 2008
Votes: +0

$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http').'://'.

lol random full stop at the end of that line.
Nasty bug/s will occur as a result as the script goes on.

Also: "exit;". What.

report abuse
vote down
vote up
WingedPanther
October 21, 2008
Votes: +0

I'm not strong on my PHP, but it appears that variables are being redefined repeatedly. Both functions define $aConf... it's not clear to me if this is just redundant or actually a massive waste of memory.

There also appear to be redundant calls to MAX_marketplaceEnabled(), depending on whether the first function is only called by the second. The entire mess looks like it could use a massive review of the logic.

report abuse
vote down
vote up

Write comment
 
 
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger
 

security image
Write the displayed characters


busy