Article 1CWYN CodeSOD: What A Load

CodeSOD: What A Load

by
Ellis Morning
from The Daily WTF on (#1CWYN)

256px-JCB_3CX_Backhoe_loader.jpg

In the mid-2000s, Amani was contracted to refactor a legacy codebase. He enjoyed breathing new life into old garbage, until the fateful day he came upon something completely unexpected.

One of the webpages he tended to was making calls to a database. Amani couldn't figure out why at first, especially since there were no Ajax scripts on the page. But then he paid careful attention to the CSS details in the webpage's header:

<link rel="stylesheet" href="/style/pictures.css.php"/>

The stylesheet was a PHP file?

Agape, Amani hunted down the file in question and unearthed the horror below:

/* snip */.popup-yellow .popup-innerHtml { background-color: #FFCC00;}.top-down { font-size: 12px; cursor: pointer; text-align: left;}.grid-a { cursor: pointer; text-decoration: none; color: #00C;}<?php include("../DB/db.class.php"); $db = new DB(); $sql = "SELECT * FROM layout_images"; $result = $db->query($sql);while($row = $db->fetchNextObject($result)){ $id = $row->id; $name = "shape".$id; $image = "../images/layouts/".$row->image; $img = createfrompng($image); $width = imageX($img); $height = imageY($img); echo "#".$name."{ position: relative; width: ".$width."px; height: ".$height."px; background-color: #ccc; border: 2px solid #FFFFFF; visibility: visible; background-image: url('/images/layouts/".$row->image."'); cursor: pointer; margin: 0; padding: 0; } ";}?>

The progenitor of this hell-born abomination had written PHP to query a database, load the retrieved images in memory, assign them unique IDs, generate unique CSS style classes for each one, then throw them out-every single time the webpage was requested.

Amani replaced this with a single class of style attributes for all images to share, setting widths and heights to auto. He also removed the unnecessary position: relative; attribute, as nothing on the page was being anchored on the elements.

release50.png[Advertisement] Release!is a light card game about software and the people who make it. Play with 2-5 people, or up to 10 with two copies - only $9.95 shipped! TheDailyWtf?d=yIl2AUoC8zAgG-eAUsYp-Q
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments