This class will display a random product from your CafePress store. You can supply a simple template to control style, and have the output link directly to the shown product or simply to your main account. You can cache the results so you don't have to connect to the CafePress website all the time. Images can also be cached locally, and can be resized before caching.
By : amnuts
<?php
//
// class.cafepressbox.php
// version 1.2.0 - 22nd November, 2003
//
// Description
//
// This class will display a random product from your CafePress store.
// You can supply a simple template to control style, and have the
// output link directly to the shown product or simply to your main
// account. You can cache the results so you don't have to connect to
// the CafePress website all the time. Images can be cached and work
// on the same time length as the product list caching. Images can
// also be resized as desired before saved into the cache.
//
// This has only been tested with the free accounts from CafePress,
// and requires PHP 4.1.0+
//
// CafePress.com is © Copyright 1999-2003 CafePress.com.
//
// Author
//
// Andrew Collington, 2003
// php@amnuts.com, http://php.amnuts.com/
//
// Feedback
//
// There is message board at the following address:
//
// http://php.amnuts.com/forums/index.php
//
// Please use that to post up any comments, questions, bug reports, etc. You
// can also use the board to show off your use of the script.
//
// Support
//
// If you like this script, or any of my others, then please take a moment
// to consider giving a donation. This will encourage me to make updates and
// create new scripts which I would make available to you. If you would like
// to donate anything, then there is a link from my website to PayPal.
//
// Example of use
//
// $cp = new cafePressBox('collingtons');
// $cp->setCacheDir('./cache');
// $cp->setCacheTime(86400);
// $cp->setCacheImages(true);
// $cp->displayItem(true);
//
// With a REALLY simple template
//
// $cp = new cafePressBox('amnuts');
// $cp->setTemplate('<div align="center">[IMG]<br>[TXT]</div>');
// $cp->displayItem(false);
//
// [IMG] is where the product image will be displayed
// [TXT] is where the product text will be displayed
//
// Example with using a category with a paid-for account
//
// $cp = new cafePressBox('amnuts', '123456');
// $cp->displayItem(true);
//
class cafePressBox
{
var $storeID;
var $categoryID;
var $products;
var $template;
var $cacheTime;
var $cacheDir;
var $cacheImages;
var $cpPattern;
var $resizeImage;
//
// Constructor
//
/**
* @return cafePressBox
* @param bool $id
* @param bool $category
* @desc The main class constructor
*/
function cafePressBox($id = false, $category = false)
{
$this->setStore($id, $category);
$this->products = array();
$this->setCacheTime();
$this->setCacheDir();
$this->setTemplate();
$this->setCacheImages();
$this->setResizeCachedImages();
}
//
// Public Methods - User called functions
//
/**
* @return void
* @param string $id
* @param mixed $category
* @desc Even though you can pass the store id in the constructor, you can use this function to change the store id.
*/
function setStore($id, $category = false)
{
$this->storeID = $id;
$this->categoryID = $category;
$this->setPattern();
$this->products = array();
}
/**
* @return void
* @param int $len
* @desc Set the time out for the cache file. If you do not with the results to be cached, then set this to 0.
* Time length is in seconds.
*/
function setCacheTime($len = 0)
{
$this->cacheTime = $len;
}
/**
* @return void
* @param string $dir Path of directory
* @desc Assign the directory to where the cache file will be saved
*/
function setCacheDir($dir = '.')
{
$this->cacheDir = $dir . ($dir[strlen($dir)-1] != '/' ? '/' : '');
}
/**
* @return void
* @param bool $okay
* @desc Sets whether or not we should store images locally. Image caching is based on cache time.
*/
function setCacheImages($okay = false)
{
$this->cacheImages = ($okay === true) ? true : false;
}
/**
* @return void
* @param int $size
* @desc To resize cached images, set the width size with this function
*/
function setResizeCachedImages($size = 0)
{
$this->resizeImage = ($size > 0) ? $size : false;
}
/**
* @return void
* @param string $pattern
* @desc The pattern string to find the products. This is likely to change if/when CafePress update their output.
*/
function setPattern($pattern = '')
{
if ($pattern == '')
{
$this->cpPattern = "<td align=\"center\" valign=\"top\"><p><a href=\"\\/{$this->storeID}\\.(\\d+)\">" .
"<img class=\"imageborder\" border=\"0\" height=\"150\"" .
" alt=\"(.*?)\" src=\"http:\\/\\/storetn\\.cafepress\\.com\\/(\\d)\\/(.*?)\\.jpg\">" .
"<\\/a><br><br><a href=\"\\/{$this->storeID}\\.(\\d+)\">(.*?)<\\/a><br>\\$(\d+)\\.(\\d+)<\\/td>";
}
else
{
$this->cpPattern = $pattern;
}
}
/**
* @return void
* @param string $template
* @desc You can pass a string which has some special place holders.
* These place holders will then be replaced with the output from the displayItem function.
* The place holders are:
* [IMG] = the image
* [TXT] = the text
*/
function setTemplate($template = '')
{
static $default = '<table border="0" cellpadding="5" cellspacing="0" width="150" style="border:1px dashed black;"><tr><td align="center" style="font-family : Verdana, Arial, sans-serif; font-size : 11px; text-decoration : none;">[IMG]<br>[TXT]</td></tr></table>';
if ($template == '' || (!strstr($template, '[IMG]') || !strstr($template, '[TXT]')))
{
$this->template = $default;
}
else
{
$this->template = $template;
}
}
/**
* @return void
* @param bool $link_to_product If true then add link directly to product information.
* @param int $id Use this to display a particular product. If the product is not listed then a random one will be chosen.
* @desc Display a random image from those in your inventory.
*/
function displayItem($link_to_product = false, $id = false)
{
$got = false;
$out = $txt = $img = '';
// get products
if (empty($this->products))
{
$got = $this->getProducts();
}
// if no products now, we couldn't get any
if (empty($this->products))
{
if (!$got)
{
$txt .= 'could not connect';
}
else
{
$txt .= 'no products found';
}
}
else
{
if (!$id || !array_key_exists($id, $this->products))
{
$id = array_rand($this->products);
}
$img .= '<a target="_blank" href="http://www.cafeshops.com/' . $this->storeID . ($link_to_product ? ".$id" : '/') . '">';
if ($this->cacheImages && @file_exists("{$this->cacheDir}{$this->products[$id]['image']}.jpg"))
{
$sizes = getimagesize("{$this->cacheDir}{$this->products[$id]['image']}.jpg");
$img .= '<img src="' . $this->cacheDir . $this->products[$id]['image'] . '.jpg" ' . $sizes[3] . ' border="0" title="' . $this->products[$id]['description'] . '">';
}
else
{
$img .= '<img src="' . $this->products[$id]['image_url'] . '" height="150" width="150" border="0" title="' . $this->products[$id]['description'] . '">';
}
$img .= "</a>\n";
if ($link_to_product)
{
$product = '<a target="_blank" href="http://www.cafeshops.com/' . $this->storeID . ".$id\">" . strtolower($this->products[$id]['description']) . '</a>';
}
else
{
$product = strtolower($this->products[$id]['description']);
}
$txt .= "\n<p>Check out our $product and other great products at <a target=\"_blank\" href=\"http://www.cafeshops.com/{$this->storeID}/\">Cafe Press</a></p>\n";
}
$out = str_replace('[IMG]', $img, $this->template);
$out = str_replace('[TXT]', $txt, $out);
echo $out;
}
//
// Private Methods - not called directly by user
//
/**
* @return bool
* @desc Saves parsed results to a cache file
*/
function saveCache()
{
$cache_file = $this->cacheDir . 'cache.' . $this->storeID;
if ($this->categoryID)
{
$cache_file .= '-' . $this->categoryID;
}
$fp = @fopen($cache_file, 'w');
if ($fp)
{
fwrite($fp, serialize($this->products));
fclose($fp);
return true;
}
else
{
return false;
}
}
/**
* @return bool
* @param string $name file name of image
* @param string $data image data
* @desc Saves image to a file
*/
function saveImage($name, $data)
{
$fp = @fopen("{$this->cacheDir}{$name}.jpg", 'w');
if ($fp)
{
fwrite($fp, $data);
fclose($fp);
if ($this->resizeImage)
{
$this->doImageResize("{$this->cacheDir}{$name}.jpg");
}
return true;
}
else
{
return false;
}
}
/**
* @return void
* @param string $filename
* @desc Resize an image based on class variable resizeImage
*/
function doImageResize($filename)
{
$version = $this->getGDVersion();
if (!empty($version))
{
$sizes = array();
$sizes = @getimagesize($filename);
if ($sizes[0] != $this->resizeImage)
{
$imIn = imagecreatefromjpeg($filename);
$height = $sizes[1] / ($sizes[0] / $this->resizeImage);
if ($version[0] == 1)
{
$imOut = @ImageCreate($this->resizeImage, $height);
@ImageCopy($imOut, $imIn, 0, 0, 0, 0, $this->resizeImage, $height, $sizes[0], $sizes[1]);
}
else if ($version[0] == 2)
{
$imOut = @ImageCreateTrueColor($this->resizeImage, $height);
@ImageCopyResampled($imOut, $imIn, 0, 0, 0, 0, $this->resizeImage, $height, $sizes[0], $sizes[1]);
}
imagejpeg($imOut, $filename, 100);
imagedestroy($imIn);
imagedestroy($imOut);
}
}
}
/**
* @return mixed
* @desc Get the version of GD being used
*/
function getGDVersion()
{
static $version = array();
if (empty($version))
{
ob_start();
phpinfo();
$buffer = ob_get_contents();
ob_end_clean();
if (preg_match("|<B>GD Version</B></td><TD ALIGN=\"left\">([^<]*)</td>|i", $buffer, $matches))
{
$version = explode('.', $matches[1]);
}
else if (preg_match("|GD Version </td><td class=\"v\">bundled \(([^ ]*)|i", $buffer, $matches))
{
$version = explode('.', $matches[1]);
}
else if (preg_match("|GD Version </td><td class=\"v\">([^ ]*)|i", $buffer, $matches))
{
$version = explode('.', $matches[1]);
}
}
return $version;
}
/**
* @return bool Success of getting product list
* @desc Get the product information either from cache file or from CafePress website.
*/
function getProducts()
{
$this->products = array();
$cache_file = $this->cacheDir . 'cache.' . $this->storeID;
if ($this->categoryID)
{
$cache_file .= '-' . $this->categoryID;
}
if ($this->cacheTime && ((time() - @filemtime($cache_file)) < $this->cacheTime))
{
$this->products = @unserialize(@join('', @file($cache_file)));
}
else
{
// connect to the website
if ($this->categoryID === false)
{
$reqheader = "GET /cp/store.aspx?s={$this->storeID} HTTP/1.0\r\nHost: www.cafeshops.com\r\nUser-Agent: RandomProduct\r\n\r\n";
}
else
{
$reqheader = "GET /cp/store.aspx?s={$this->storeID}.{$this->categoryID} HTTP/1.0\r\nHost: www.cafeshops.com\r\nUser-Agent: RandomProduct\r\n\r\n";
}
$socket = @fsockopen("www.cafeshops.com", 80, $errno, $errstr, 60);
// we have the socket open
if ($socket)
{
fputs($socket, $reqheader);
while (!feof($socket))
{
$line = fgets($socket, 4096);
$matches = array();
if (preg_match("/{$this->cpPattern}/i", $line, $matches))
{
// item # :: item desc :: directory :: image name :: item # :: item desc :: price dollar :: price cents
// e.g.
// 5956381 :: White T-Shirt :: 1 :: 5956381_F_store :: 5956381 :: White T-Shirt :: 14 :: 99
$this->products[$matches[1]]['description'] = trim($matches[2]);
$this->products[$matches[1]]['image'] = trim($matches[4]);
$this->products[$matches[1]]['image_url'] = "http://storetn.cafepress.com/{$matches[3]}/" . trim($matches[4]) . '.jpg';
$this->products[$matches[1]]['price'] = "{$matches[7]}.{$matches[8]}";
}
}
fclose($socket);
if ($this->cacheTime)
{
$this->saveCache();
}
if ($this->cacheImages && $this->cacheTime)
{
// as we're caching, grab the files too
foreach($this->products as $product)
{
set_time_limit(120);
$data = '';
$fp = @fopen($product['image_url'], 'r');
if ($fp)
{
while(!feof($fp))
{
$data .= fgets($fp, 4096);
}
fclose($fp);
$this->saveImage($product['image'], $data);
}
}
}
}
// there was a problem
else
{
return false;
}
}
return true;
}
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations. FREE! Go There Now!
|
|
|
|
Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change. FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
|
|
|
|
Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
|
|
|
|
As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security. FREE! Go There Now!
|
|
|
|
Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |