Code snippets, tech tricks and other bits and bobs

'Deprecated' error warnings - PHP 5.3

Just reviewing some sites in my portfolio, and I notice that a CubeCart installation on a shared host is broken. Oh dear...

Deprecated: Function set_magic_quotes_runtime() is deprecated in /path/to/domain/shop/ini.inc.php on line 114
Warning: ini_set(): Cannot change zlib.output_compression - headers already sent in /path/to/domain/shop/ini.inc.php on line 118 
Warning: Cannot modify header information - headers already sent by (output started at /path/to/domain/shop/ini.inc.php:114) in /path/to/domain/shop/index_enc_ion.php on line 31 
Warning: Cannot modify header information - headers already sent by (output started at /path/to/domain/shop/ini.inc.php:114) in /path/to/domain/shop/index_enc_ion.php on line 32
Deprecated: Function eregi() is deprecated in /path/to/domain/shop/includes/functions.inc.php on line 408

Ah, great. Without warning, we've been upgraded to PHP 5.3

Thanks for that...

Anyway, it's easily solved.

With 5.3 a new error level has been introduced - E_DEPRECATED, so it's easy to suppress deprecated errors:

In your scripts:

error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

In the case of CubeCart 4 this can be acheived in ini.inc.php line 101.

In php.ini:

error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED

in .htaccess:

php_value error_reporting 1

.htaccess requires the correct integer value equivalent of your chosen reporting level - these are a touch obscure, but setting it to 1 means report fatal run-time errors and unrecoverable errors, which will do the trick.

Filed under  //   .htaccess   CubeCart   PHP   PHP 5.3   php.ini  

Rotating background image (or other PHP function) in CubeCart templates

I have a site where the main site picks an image from a particular folder for the background every time you hit a page, rotating through the available images in a loop. We also have a CubeCart store, which has had the template skinned to fit the main site, so obviously enough, I wanted to have the same background image feature for the shop.

The thing with CubeCart templates is that you can’t just add php to the .tpl files – you have to do all your PHP jiggery pokery in include files, and assign values to object properties, and include a placeholder in the tpl.

There are two key CubeCart files to make this change over the whole shop:

{www,yourdomain.com}/{shop_location}/includes/global/index.inc.php {www,yourdomain.com}/{shop_location}/skins/{your_skin}/styleTemplates/global/index.tpl

First off you want to get your functions together from the main site. Mine all live in a lib.php file, but as I don’t need to load all of them into CubeCart, I pulled the relevant ones into a new file, /lib/php/rotatebg_lib.php. Now I can include this file in my original lib.php as well as in CubeCart, and reduce unnecessary memory overheads.

The system relies on three functions:

files_in_dir($start_dir) creates an array of the files in a directory. This is one of my standard ‘off the shelf’ functions

index_background_images() checks for files with a .jpg extentsion, creates an array of them and stores it in a $_SESSION variable, called only when a session is started. There are other ways of checking for file types – the most reliable is to mime_content_type() function, but if you trust the files on your server, my method is fine. If you are dealing with user uploaded files, remember you should NEVER trust the file to be what it says it is!

rotate_background() checks for a session, calls the indexing function if necessary and otherwise increments the index counter.

rotatebg_lib.php

$background_image_path = '/img/backgrounds/';

function rotate_background(){
/* check for session - if none, index bg files, else increment counter */
    if(isset($_SESSION['backgrounds'])){
        $_SESSION['backgrounds']['index']++;
        if($_SESSION['backgrounds']['index'] >= count($_SESSION['backgrounds']['files'])){
            $_SESSION['backgrounds']['index'] = 0;
        }
        $background = $_SESSION['backgrounds']['index'];
    }else{
        $_SESSION['backgrounds']['files'] = index_background_images();
        $_SESSION['backgrounds']['index'] = 0;
        $background = 0;
    }
    return $background;
}

function index_background_images(){
/* return array of files with .jpg extension */
    global $background_image_path;
    $files = files_in_dir($_SERVER['DOCUMENT_ROOT'] . $background_image_path);
    foreach($files as $file){
        if(substr($file,0,10) == 'background' && substr($file,-4,4) == '.jpg'){
            $bg_images[]=$file;
        }
    }
    return $bg_images;
}

function files_in_dir($start_dir){
/* returns an array of files in $start_dir (not recursive) */
    $files = array();
    $dir = opendir($start_dir);
    while(($myfile = readdir($dir)) !== false){
        if($myfile != '.' && $myfile != '..' && !is_file($myfile) && $myfile != 'resource.frk' && !eregi('^Icon',$myfile) ){
            $files[] = $myfile;
        }
    }
    closedir($dir);
    return $files;
}

Now we need to include and run these functions in the CubeCart global index include. Line numbers may vary dependant on your template, but essentially you want to add this just after the $body template has been defined

{your_skin}/includes/global/index.inc.php

Replace:

$body = new XTemplate ("global".CC_DS."index.tpl");

With:

$body = new XTemplate ("global".CC_DS."index.tpl");

include($_SERVER['DOCUMENT_ROOT'].'/inc/php/rotatebg_lib.php');
$background = rotate_background();
$body->assign('VAL_CUSTOM_BACKGROUND', $_SESSION['backgrounds']['files'][$background]);

And finally, insert VAL_CUSTOM_BACKGROUND into the index.tpl file. This needs to be added in the head section, I prefer the end of the head section as CSS latency will ensure that the rule is obeyed. Again, line numbers may vary.

{your_skin}/styleTemplates/global/index.tpl

Replace:

</head>

With

<style type="text/css">
#pageSurround{
    background-image:url("/img/backgrounds/{VAL_CUSTOM_BACKGROUND}");
}
</style>
</head>

Shazzamwhazzam. We are done.

Filed under  //   CSS   CubeCart   PHP   Templates