Common class

There are a number of static functions and variables that you may find useful to use in your scripts which have been included in the common class.

Static variables

Accessible via common::$Variable_Name

Variable/Key
Type
Default
Description
debug
boolean
false
set to true to enable debugging and timing logs

common::timeLog()

Adds a line to a log file for debugging purposesExample

 
common::timeLog($str,$groupKey='default');
 
Variable/Key
Type
Default
Description
$str
string
Null
string/message to add to log
$groupKey
string
default
assigns message to a time group so you can see the time difference between multiple log messages

common::getCacheFile()

Check for a current cache file

Example

 
$output = common::getCacheFile($filesArray,$prefix,$folder,$extraVars,$otherFilesMTime,$ext,$refreshCache);
/*
$output = array(
	'contents'=>(contents string or false),
	'filename'=>(filename string),
	'timestamp'=>(Unix timestamp [filemtime] of the cache file)
)
*/
 
Variable/Key
Type
Default
Description
$filesArray
array
[]
array of the files compiled into the cache file
$prefix
string
bwcore-
cache file prefix
$folder
string
false (bool)
path to cache sub folders to use
$extraVars
array
[]
key-value pairs of data which may have also changed
$otherFilesMTime
array
[]
other files that the files you are caching may use and could change affecting the cache file as well
$ext
string
false (bool)
an extention to add to your file (before the default extension is added of the first file in your array)
$refreshCache
boolean
false
clear the cache file if found

common::saveCacheFile()

Saves a string to a cache file with parameters for checking the cache again in future.

Example

 
common::saveCacheFile($filesArray,$prefix,$content,$folder,$extraVars,$otherFilesMTime,$ext);
 
Variable/Key
Type
Default
Description
$filesArray
array
[]
array of the files being compiled into the cache file
$prefix
string
bwcore-
cache file prefix
$content
string
""
the content to insert into your cache file (you can gzip it before adding the string to your cache file to make the file even smaller if required)
$folder
string
false (bool)
path to cache sub folders to use
$extraVars
array
[]
key-value pairs of data which may have also changed
$otherFilesMTime
array
[]
other files that the files you are caching may use and could change affecting the cache file as well
$ext
string
false (bool)
an extention to add to your file (before the default extension is added of the first file in your array)

common::simplePath()

Get a clean path instead of folder traversing statements

Example

 
$path = 'www/htdocs/../secret/';
$simplePath = common::simplePath($path);
//$simplePath = www/secret/
 
Variable/Key
Type
Default
Description
$path
string
.
the file or folder path to simplify.

common::findFiles()

Returns an array of files matching a given regular expression in a given location

Example

 
$files = common::findFiles($path,$regex);
/*
$files = array(
	array(
		"path"=>(string),
		"filename"=>(string)
	),
	array(
		"path"=>(string),
		"filename"=>(string)
	)
)
*/
 
Variable/Key
Type
Default
Description
$path
string
.
the internal path to a location you want to search
$regex
string
'/\.(?:php|html)$/'
The default regex searches for PHP and HTML files.

common::time_elapsed_string()

Will return a string rounded to the nearest second, minute, hour, day, month or year depending on how much time has elapsed. Similar behaviour to that on a Tweet when using Twitter.

Example

 
$ptime = strtotime("-27 hours");
$niceTime = common::time_elapsed_string($ptime);
//$niceTime = 1 day
 
Variable/Key
Type
Default
Description
$ptime
string
Null
Unix timestamp

common::isBot()

Returns if a robot/spider is detected browsing your website.

Example

 
$isRobot = common::isBot();
//$isRobot = (boolean)
 

common:formatSizeUnits()

Returns a filesize and dynamically chooses what units to use (bytes,KB,MB,GB)

Example

 
$bytes = file_get_contents("/path/to/file");
$filesize = common::formatSizeUnits($bytes);
//$filesize = 1KB
 
Variable/Key
Type
Default
Description
$bytes
integer
Null
filesize in bytes to convert

common::stripFalseArray()

Unsets any array keys that are false (boolean) recursively.

Example

 
$array = array(1, 2, false,array(3, false));
$newArray = common::stripFalseArray($array);
//$newArray = array(1, 2, array(3));
 
Variable/Key
Type
Default
Description
$array
array
[]
array to strip false values from

common::remoteFileTimes()

Performs a Curl request returning the contents and last modified time of multiple URLs

Example

 
$array = array("http://url.com/page","http://url2.com/file.js");
$fileTimes = common::getFileTimes($array);
/*
$fileTimes = array(
	array(
		"contents"=>(string),
		"timestamp"=>(Unix timestamp)
	),
	array(
		"contents"=>(string),
		"timestamp"=>(Unix timestamp)
	)
);
*/
 
Variable/Key
Type
Default
Description
$array
array
Null
array of strings. Each string should be a complete URL.

common::remoteFileTime()

Performs a Curl request returning the contents and last modified time of a single URLs

Example

 
$url = "http://url.com/page";
$fileTime = common::getFileTime($url);
/*
$fileTime = array(
	"contents"=>(string),
	"timestamp"=>(Unix timestamp)
);
*/
 
Variable/Key
Type
Default
Description
$url
string
Null
the URL of the file you want to get

common::pageCacheHeaders()

Add caching headers and detect/return a 301 header and empty page where applicable for a single file. It will also check any files that have been included to see if they have been modified.

Example

 
$file = "/var/www/bw.codes/htdocs/index.php";
$lastModified = common::pageCacheHeaders($file);

// The script may be terminated in this function. If not:
/*
$lastModified = (unix timestamp)
*/
 
Variable/Key
Type
Default
Description
$file
string
""
the file name/path you'd like to md5 hash for the eTag header (eTag also uses layout::$customCacheIDs array to compile the hash string)

common::querystringName()

Cleans a string for use in SEO friendly querystrings/clean URLs

Example

 
$itemName = "Yummy teapot";
$cleanName = common::querystringName($itemName);
/*
$cleanName = "yummy-teapot";
*/
 
Variable/Key
Type
Default
Description
$str
string
""
the string you'd like to clean/simplify

common::getClientIP()

Check all known server variables for the Client's IP address until a valid IP is found.

Example

 
$clientIP = common::getClientIP();
/*
$clientIP = 123.223.32.41;
*/