X-Cart Product Images with a Static Cookieless Domain


Written by

So today, I was working on an older x-cart site after implementing some of the previous CDN / static domain hacks, I looked at the product images which were being displayed via the main domain. Because CleanURLs were enabled, I couldn’t just edit the thumbnail template file but rather edit the CleanURL creator function. So here’s my edits for X-Cart 4.3 and xcart 4.5.

Edit /func/func.files.php

approx. line 254 (xcart 4.3) or line 374 (xcart 4.5)
from:

return $current_location.str_replace(DIRECTORY_SEPARATOR, "/", substr($image_path, strlen(preg_replace("/".preg_quote(DIRECTORY_SEPARATOR, "/")."$/S", "", $xcart_dir))));

to:

if ($_SERVER['HTTPS'] != 'on') {
    return "http://cdn.domain.tld".str_replace(DIRECTORY_SEPARATOR, "/", substr($image_path, strlen(preg_replace("/".preg_quote(DIRECTORY_SEPARATOR, "/")."$/S", "", $xcart_dir))));
} else {
    return $current_location.str_replace(DIRECTORY_SEPARATOR, "/", substr($image_path, strlen(preg_replace("/".preg_quote(DIRECTORY_SEPARATOR, "/")."$/S", "", $xcart_dir))));
}

The issue that needs to be then addressed is how to balance the previous edits with this one over x number of domains so the file requests can be parallel and not blocking, after looking, putting javascripts and css files on one domain, catalogue images on anotherand possibly any remaining requests manually written in via a third? in this scenario, {$skin}/{$alt_skinDir} and {$images}/{$alt_imagesDir} (being remembered from memory) could be over two domains and then the catalogue images on the third.

Ideally you want to even out the requests (no less than 5 to 10 requests) over the domains with upto 3 sub-domains maximum to reduce the number of DNS lookups but depending on what’s being serviced and checkign with online tools will help test which is the best layout.

PHP: Find text between two strings without regex


Written by

So today, my friend asked me for some regex to get text between two given strings; I can’t do regex so I came up with a simple line of code and then turned it into a function so it can be re-used.

I thought I’d pop the code here for others too.

/***************************************************************
 * @title:        Lazy Regex
 * @descr:        Find any text between two strings.
 * @author:        Adam Boutcher
 * @email:        admin@aboutcher.co.uk
 * @web:        www.aboutcher.co.uk
 * @usage:
 *                string lazy_regex (string $haystack, string $needle_first, string $needle_last)
 * @Params:        
 *                $haystack - the text you which to search
 *                $needle_first - Find this text as the start of the pointer.
 *                $needle_first - Find this text as the end of the pointer.
 * @notes:        
 *                Returns false on failure, this maybe non-bool false, use === to test (see PHP: substr).
 *                Returns the start and end needles including the text beween on success.
 **************************************************************/

function lazy_regex($haystack, $needle_first, $needle_last) {
    if ((empty($haystack))||(empty($needle_first))||(empty($needle_last))) { return false; }
    return substr($haystack, stripos($haystack, $needle_first), ((stripos($haystack, $needle_last)+strlen($needle_last))-stripos($haystack, $needle_first)));
}

A weekend of Linux fiddling…


Written by

So this weekend I’ve been fiddling getting Linux as my main OS on my laptop again; this post is just some odds and ends I’ve done.

Conky BOFH Excuses

I wanted some BOFH style excuses being displayed on my conky, so I took an excuses list and then popped it on my webserver and it outputs a single line at random. The conky script reloads once every 6 hours.

${execi 21600 curl http://bofh.aboutcher.co.uk | fold -w35 }

 

Fix Legend of Grimrock Error

I was getting this error with Legend of Grimrock on my fresh install on debian:

GL_INVALID_ENUM 0x0500

so I took to the internet and had a search about, turns out I’m missing a dependency that the installer doesn’t check for but there was a helpful post on their forums and I installed it, if you have the same issue then install this:

root@DebianBased:$ apt-get install libtxc-dxtn-s2tc0
root@Fedora:$ yum install libtxc_dxtn

 

Debian Wheezy x64 and WINE…

So as you’ve seen my preferred distro is Debian and I have previously written a post on installing a newer version of WINE that what is in the Debian repo’s for better computability. Today I followed my own instructions and to my surprise it didn’t work, mainly because the guide was aimed at squeeze and there are some big differences in the way Debian implements multi-arch and wine depends on some 32bit libs.

So here’s my attempt at a newer guide.

  1. Install libpng12-0 and libpng12-dev
  2. Download Wine-Gecko from the Mepis Repo for Mepis 8.0
  3. Download Wine from the Mepis Repo for Mepis 8.0
  4. Install Wine-Gecko
  5. For x64 download ia32libs from Debian Repo for Squeeze
  6. Install ia32libs
  7. Install Wine

Now that should all work, Wheezy’s method of installing ia32libs it to include i386 in the architecture list in dpkg which results in better compatibility but also a larger system which isn’t always wanted.

 

Firefox and Debian

Linux Mint also provides a small repo for Debian which includes newer or non-free software that isn’t in the Debian repo. From here you can download and install a Debian version of Firefox instead of using Iceweasel which isn’t always upto date.

  1. Download firefox-l10n-en-gb (or your lanaguage).
  2. Download firefox
  3. Install firefox-l10n-en-gb (or your lanaguage).
  4. Install firefox

You should now have firefox setup correctly even in update-alternatives and gnome etc.

 

Debian and MATE Desktop

This one’s fairly simple but strangely not documented on their website. If you liked Gnome2 but not Gnome3 then presumably you don’t want to have to install all of Gnome3’s libs to run the ‘classic’ or fallback desktop. This is where MATE comes in which is a fork of the Gnome2 code with some updates and changes. To install it just do the following:

root@debian:$ echo "deb http://packages.mate-desktop.org/repo/debian wheezy main" >> /etc/apt/sources.list
apt-get update
apt-get install mate-archive-keyring
apt-get update
apt-get install mate-core mate-desktop-environment

and there you go, you should now have mate installed.

 

That’s all for now but I do have more stuff for another day.

X-Cart 4.5 and a CDN / Cookieless Static Domain


Written by

So today I was working on a site based on xcart 4.5 with an aim to speed up page load times. I had previously migrated all the background images into CSS base64 encoded data to save on the number of requests and file sizes etc.

Part of x-carts SEO tools and optimisation is that it will combine all CSS files into one but while it does that, it craftily rewrites specific parts mainly so URLs are correct which plays havoc with base64 encoded CSS images as it attempts to rewrite it all, so this file needs to be separate and defined manually in the template via the <link /> tag and not with the load_defer() function.

Anyway in regards to using a cookie-less Static domain or a Content Delivery Network for CSS, Javascripts, Images etc then you need to do some minor rewriting of x-cart.

Firstly, you need to setup your domain and relevant services to have the correct resources and the domain to work. I was using apache and included the following lines in the vhosts.

RequestHeader unset Cookie    
Header unset Set-Cookie

Which removes any cookies on that domain.

Once the domain is all setup and ready then we have to edit x-cart a little, I’ll try and write this as simply as possible.

Edit /smarty.php

approx. line 92
from:

$smarty->assign('AltImagesDir', $alt_skin_info['web_path'].'/images');
$smarty->assign('AltSkinDir', $alt_skin_info['web_path']);

to:

if ($_SERVER['HTTPS'] != 'on') {
    $smarty->assign('AltImagesDir', "http://cdn.domain.tld".$alt_skin_info['web_path'].'/images');
    $smarty->assign('AltSkinDir', "http://cdn.domain.tld".$alt_skin_info['web_path']);
} else {
    $smarty->assign('AltImagesDir', $alt_skin_info['web_path'].'/images');
    $smarty->assign('AltSkinDir', $alt_skin_info['web_path']);
}

and approx. line 107
from:

$smarty->assign('ImagesDir', $xcart_web_dir.$smarty_skin_dir.'/images');
$smarty->assign('SkinDir', $xcart_web_dir.$smarty_skin_dir);

to:

if ($_SERVER['HTTPS'] != 'on') {
    $smarty->assign('ImagesDir', "http://cdn.domain.tld".$smarty_skin_dir.'/images');
    $smarty->assign('SkinDir', "http://cdn.domain.tld".$smarty_skin_dir);
} else {
    $smarty->assign('ImagesDir', $xcart_web_dir.$smarty_skin_dir.'/images');
    $smarty->assign('SkinDir', $xcart_web_dir.$smarty_skin_dir);
}

then edit: /include/template/plugins/function.load_defer_code.php

at approx. line 111
from:

$cacheWebFile = $var_dirs_web['cache'].'/'.$label.'.'.$md5Suffix.'.'.$type;

to:

if ($_SERVER['HTTPS'] != 'on') {
    $cacheWebFile = "http://cdn.domain.tld/var/cache".'/' .$label.'.'.$md5Suffix.'.'.$type;
} else {
    $cacheWebFile = $var_dirs_web['cache'].'/'.$label.'.'.$md5Suffix.'.'.$type;
}

We have to check for SSL so we don’t get certificate errors and alerts about unencrypted content. There isn’t much information in regards to x-cart 4.5 and CDN’s but there’s a snippet on the wiki about it in older versions.

 

Responsive Websites and Static Headers


Written by

So one of the websites I am developing has a static header, there are a few examples of this being used such as Next; the issue comes, when you require lots of information in this header but use a responsive layout, the header is then taking up real estate for your main content (even more so in landscape mode).

So this is where hiding the less vital information such as the company logo and search could come in useful; so I developed a small hacky jquery base script to do this. It isn’t perfect and it’s my first serious javascript on this site so please be kind!

The script hides a div on scroll and displays another to allow you to have a small “show div” button available at all times. if then shows and hides when the display is resizes to a desktop resolution.

Have a go of the demo and feel free to steal the code; if you find any better ways of doing it then don’t hesitate to contact me.