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.
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>';
/** * @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"; }
/** * @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();
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.