Source for file BitBase.php
Documentation is available at BitBase.php
* Virtual bitweaver base class
* @version $Header: /cvsroot/bitweaver/_bit_kernel/BitBase.php,v 1.32 2007/01/11 09:18:10 squareing Exp $
* Copyright (c) 2004 bitweaver.org
* All Rights Reserved. See copyright.txt for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details
* Virtual base class (as much as one can have such things in PHP) for all
* derived tikiwiki classes that require database access.
* @author spider <spider@steelsun.com>
require_once ( KERNEL_PKG_PATH. 'BitDbBase.php' );
include_once ( KERNEL_PKG_PATH. 'BitCache.php' );
define( 'STORAGE_BINARY', 1 );
* Virtual base class (as much as one can have such things in PHP) for all
* derived bitweaver classes that require database access.
* Error hash that will contain an error codes we encounter along
* the way this hash can be used by presentation layer ti give feedback
* Same idea as the error hash but this is for successful operations
* String used to refer to preference caching and database table
* Used to store database mechanism
* Used to store database type
* Standard Query Cache Time. Variable can be set to 0 to flush particular queries
* Data hash that represents this classes row(s) in the db
* Data hash that contains logging information relevant to database operations
* During initialisation, we assign a name which is used by the class.
* @param pName a unique identified used in caching and database
* Sets database mechanism for the instance
* @param pDB the instance of the database mechanism
// set internal db and retrieve values
* Determines if there is a valide database connection
return( !empty( $this->mDb ) && $this->mDb->isValid() );
* Return pointer to current Database
return ( !empty( $this->mDb ) ? $this->mDb : NULL );
* Switch debug level in database
function debug( $pLevel = 99 ) {
$this->mDb->debug( $pLevel );
// =-=-=-=-=-=-=-=-=-=-=- Non-DB related functions =-=-=-=-=-=-=-=-=-=-=-=-=
* Determines if any given variable exists and is a number
* This method should be THE method used to display a template. php files should not
* access $gBitSmarty directly.
* @param string pMsg error message to be displayed
* @return none this function will DIE DIE DIE!!!
function display( $pPackage, $pTemplate ) {
global $gBitSmarty, $gBitLanguage, $style, $style_base;
if (isset ($style) && isset ($style_base)) {
if (file_exists(BIT_THEME_PATH. "styles/$style_base/$pTemplate")) {
// Theme has overriden template
$_smarty_tpl_file = 'file:'. BIT_STYLES_PATH. "/$style_base/$pTemplate";
$_smarty_tpl_file = 'file:'. BIT_ROOT_PATH. "$pPackage/templates/$pTemplate";
global $gBitLanguage, $style, $style_base;
if (isset($style) && isset($style_base)) {
if (file_exists(BIT_STYLES_PATH."/$style_base/$_smarty_tpl_file")) {
$_smarty_tpl_file = BIT_STYLES_PATH."/$style_base/$_smarty_tpl_file";
$gBitSmarty->display( $_smarty_tpl_file );
// $gBitSmarty->display( 'bitpackage:'.$pPackage.$pTemplate );
* Returns entry from the mInfo hash if field exists
* @param pFieldName the instance of the database mechanism
function getField( $pFieldName, $pDefault = NULL ) {
return( !empty( $this->mInfo[$pFieldName] ) ? $this->mInfo[$pFieldName] : $pDefault );
* Prepares parameters with default values for any getList function
* @param pParamHash hash of parameters for any getList() function
* @return the link to display the page.
global $gBitSmarty, $gBitSystem;
// If offset is set use it if not then use offset =0
// use the max_records php variable to set the limit
// if sortMode is not set then use last_modified_desc
if ( empty( $pListHash['sort_mode'] ) ) {
if ( empty( $_REQUEST["sort_mode"] ) ) {
$pListHash['sort_mode'] = 'last_modified_desc';
$pListHash['sort_mode'] = $_REQUEST['sort_mode'];
if( empty( $pListHash['max_records'] ) ) {
$pListHash['max_records'] = $gBitSystem->getConfig( "max_records", 10 );
if( !isset ( $pListHash['offset'] ) ) {
if( isset ($pListHash['page'] ) ) {
$pListHash['offset'] = ($pListHash['page'] - 1) * $pListHash['max_records'];
if ( isset ( $_REQUEST["offset"] ) ) {
$pListHash['offset'] = $_REQUEST['offset'];
} elseif( isset ( $_REQUEST['page'] ) && is_numeric( $_REQUEST['page'] ) ) {
$pListHash['offset'] = ($_REQUEST['page'] - 1) * $pListHash['max_records'];
} elseif( isset ($_REQUEST['list_page']) && is_numeric( $_REQUEST['list_page'] ) ) {
$pListHash['offset'] = ($_REQUEST['list_page'] - 1) * $pListHash['max_records'];
$pListHash['offset'] = 0;
// Don't use $_REQUEST["find"] as it can really screw with modules on search pages
if( !empty( $pListHash["find"] ) ) {
$pListHash['find']= $pListHash["find"];
$pListHash['find'] = NULL;
$gBitSmarty->assign( 'find', $pListHash['find'] );
if( isset ( $_REQUEST['date'] ) ) {
$pListHash['date']= $_REQUEST['date'];
$pListHash['date'] = $gBitSystem->getUTCTime();
if( empty( $pListHash['load_comments'] ) ) {
$pListHash['load_comments'] = FALSE;
if( empty( $pListHash['load_num_comments'] ) ) {
$pListHash['load_num_comments'] = FALSE;
if( empty( $pListHash['parse_data'] ) ) {
$pListHash['parse_data'] = FALSE;
* @param string $pString String that needs to be translated
function tra( $pString ) {
return( $gBitLanguage->translate( $pString ) );
* recursively remove files and directories
* @param string $pPath directory we want to remove
* @param boolean $pFollowLinks follow symlinks or not
* @return TRUE on success, FALSE on failure
function unlink_r( $pPath, $pFollowLinks = FALSE ) {
while( FALSE !== ( $entry = readdir( $dir ) ) ) {
if( is_file( "$pPath/$entry" ) || ( !$pFollowLinks && is_link( "$pPath/$entry" ) ) ) {
} elseif( is_dir( "$pPath/$entry" ) && $entry != '.' && $entry != '..' ) {
* @return TRUE on success, FALSE on failure
global $site_use_proxy,$site_proxy_host,$site_proxy_port;
if (!preg_match("/^[-_a-zA-Z0-9:\/\.\?&;=\+]*$/",$pUrl)) {
// rewrite url if sloppy # added a case for https urls
if ( (substr($pUrl,0,7) <> "http://") && (substr($pUrl,0,8) <> "https://") ) {
$pUrl = "http://" . $pUrl;
curl_setopt($curl_obj, CURLOPT_URL, $pUrl);
curl_setopt($curl_obj, CURLOPT_HEADER, 0);
curl_setopt($curl_obj, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl_obj, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_obj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_obj, CURLOPT_TIMEOUT, 5);
if ($site_use_proxy == 'y') {
curl_setopt($curl_obj, CURLOPT_PROXY, $site_proxy_host);
curl_setopt($curl_obj, CURLOPT_PROXYPORT, $site_proxy_port);
curl_setopt($curl_obj, CURLOPT_HTTPPROXYTUNNEL, 1);
$data = curl_exec($curl_obj);
* Parse XML Attributes and return an array
* this function has a whopper of a RegEx.
* I nabbed it from http://www.phpbuilder.com/annotate/message.php3?id=1000234 - XOXO spiderr
* @param array $pString XML type string of parameters
* @return TRUE on success, FALSE on failure
$parameters = array( '', '' );
$regexp_str = "/([A-Za-z0-9_-]+)(?:\\s*=\\s*(?:(\"|\')((?:[\\\\].|[^\\\\]?)*?)(?:\\2)|([^=\\s]*)))?/";
while( list ( $key, $match ) = each( $matches ) ) {
$value = $match[sizeof( $match )- 1]; // The value can be at different indexes because of optional quotes, but we know it's always at the end.
$parameters[$attrib] = trim( $value, '\"' );
* XML Entity Mandatory Escape Characters
* @param array $quote_style
* @return TRUE on success, FALSE on failure
function xmlentities( $string, $quote_style= ENT_QUOTES ) {
foreach ($trans as $key => $value)
$trans[$key] = '&#'. ord($key). ';';
// dont translate the '&' in case it is part of &xxx;
// after the initial translation, _do_ map standalone '&' into '&'
return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,5};)/","&" , strtr($string, $trans));
* Redirect to another page or site
* @param string The url to redirect to
// clean up URL before executing it
while( strstr( $pUrl, '&&' ) ) {
while( strstr( $pUrl, '&&' ) ) {
// header locates should not have the & in the address it breaks things
while( strstr( $pUrl, '&' ) ) {
for($i= 1;$i< count($args);$i++ ) {
foreach ($args[$i] as $key => $data) {
$pArray[$key] = trim( $pArray[$key] );
if ($num >= 11 and $num <= 19) {
} elseif ( $num % 10 == 1 ) {
} elseif ( $num % 10 == 2 ) {
} elseif ( $num % 10 == 3 ) {
* Cleans file path according to system we're on
* @return TRUE on success, FALSE on failure
$pPath = (!empty($_SERVER["SERVER_SOFTWARE"]) && strpos($_SERVER["SERVER_SOFTWARE"],"IIS") ? str_replace( '\/', '\\', $pPath) : $pPath);
if (!isset ($_SESSION['havegd2'])) {
# TODO test this logic in PHP 4.3
# if (version_compare(phpversion(), "4.3.0") >= 0) {
# $_SESSION['havegd2'] = true;
return $_SESSION['havegd2'];
return 'http'. ( ( isset ($_SERVER['HTTPS'] ) && ( $_SERVER['HTTPS'] == 'on' ) ) ? 's' : '' );
if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) {
$rv = 'https://' . $_SERVER['HTTP_HOST'];
if ($_SERVER['SERVER_PORT'] != 443)
$rv .= ':' . $_SERVER['SERVER_PORT'];
$rv = 'http://' . $_SERVER['HTTP_HOST'];
if ($_SERVER['SERVER_PORT'] != 80)
$rv .= ':' . $_SERVER['SERVER_PORT'];
/* Warning by zaufi: as far as I saw in my apache 1.3.27
* there is no need to add port if it is non default --
* $_SERVER['HTTP_HOST'] already contain it ...
return 'http'. ((isset ($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) ? 's' : ''). '://'. $_SERVER['HTTP_HOST'];
* PHP version compatability function
* @param array $pFile File
* @return TRUE on success, FALSE on failure
|