kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file BitBase.php

Documentation is available at BitBase.php

  1. <?php
  2. /**
  3.  * Virtual bitweaver base class
  4.  *
  5.  * @package kernel
  6.  * @version $Header: /cvsroot/bitweaver/_bit_kernel/BitBase.php,v 1.32 2007/01/11 09:18:10 squareing Exp $
  7.  *
  8.  *  Copyright (c) 2004 bitweaver.org
  9.  *  All Rights Reserved. See copyright.txt for details and a complete list of authors.
  10.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details
  11.  *
  12.  *  Virtual base class (as much as one can have such things in PHP) for all
  13.  *  derived tikiwiki classes that require database access.
  14.  *
  15.  *  created 2004/8/15
  16.  *
  17.  * @author spider <spider@steelsun.com>
  18.  */
  19.  
  20. /**
  21.  * required setup
  22.  */
  23. require_once KERNEL_PKG_PATH.'BitDbBase.php' );
  24. include_once KERNEL_PKG_PATH.'BitCache.php' );
  25.  
  26. define'STORAGE_BINARY');
  27. define'STORAGE_IMAGE');
  28.  
  29. /**
  30.  * Virtual base class (as much as one can have such things in PHP) for all
  31.  * derived bitweaver classes that require database access.
  32.  *
  33.  * @package kernel
  34.  */
  35. class BitBase {
  36.     /**
  37.      * Error hash that will contain an error codes we encounter along
  38.      * the way this hash can be used by presentation layer ti give feedback
  39.      * to the user.
  40.      * @todo not used yet
  41.      * @private
  42.      */
  43.     var $mErrors;
  44.  
  45.     /**
  46.      * Same idea as the error hash but this is for successful operations
  47.      * @private
  48.      */
  49.     var $mSuccess;
  50.  
  51.     /**
  52.      * String used to refer to preference caching and database table
  53.      * @private
  54.      */
  55.     var $mName;
  56.  
  57.     /**
  58.      * Used to store database mechanism
  59.      * @private
  60.      */
  61.     var $mDb;
  62.  
  63.     /**
  64.      * Used to store database type
  65.      * @private
  66.      */
  67.     var $dType;
  68.  
  69.     /**
  70.      * Standard Query Cache Time. Variable can be set to 0 to flush particular queries
  71.      * @private
  72.      */
  73.     var $mCacheTime;
  74.  
  75.     /**
  76.      * Data hash that represents this classes row(s) in the db
  77.      **/
  78.  
  79.     var $mInfo = array();
  80.  
  81.     /**
  82.      * Data hash that contains logging information relevant to database operations
  83.      **/
  84.  
  85.     var $mLogs = array();
  86.  
  87.     /**
  88.      * During initialisation, we assign a name which is used by the class.
  89.      * @param pName a unique identified used in caching and database
  90.      *  mechanisms
  91.      ***/
  92.     function BitBase$pName '' {
  93.         global $gBitDb;
  94.         $this->mName = $pName;
  95.         $this->mCacheTime = BIT_QUERY_CACHE_TIME;
  96.         ifis_object$gBitDb ) ) {
  97.             $this->setDatabase($gBitDb);
  98.         }
  99.         $this->mErrors = array();
  100.         $this->mInfo = array();
  101.     }
  102.  
  103.     /**
  104.      * Sets database mechanism for the instance
  105.      * @param pDB the instance of the database mechanism
  106.      ***/
  107.     function setDatabase&$pDB {
  108.         // set internal db and retrieve values
  109.         $this->mDb = &$pDB;
  110.         $this->dType = $this->mDb->mType;
  111.     }
  112.  
  113.     /**
  114.      * Determines if there is a valide database connection
  115.      **/
  116.  
  117.     function isDatabaseValid({
  118.         return!empty$this->mDb && $this->mDb->isValid() );
  119.     }
  120.  
  121.     /**
  122.      * Return pointer to current Database
  123.      **/
  124.  
  125.     function getDb({
  126.         return !empty$this->mDb $this->mDb : NULL  );
  127.     }
  128.  
  129.     /**
  130.      * Switch debug level in database
  131.      *
  132.      **/
  133.  
  134.     function debug$pLevel 99 {
  135.         ifis_object$this->mDb ) ) {
  136.             $this->mDb->debug$pLevel );
  137.         }
  138.     }
  139.  
  140.     // =-=-=-=-=-=-=-=-=-=-=- Non-DB related functions =-=-=-=-=-=-=-=-=-=-=-=-=
  141.  
  142.     
  143.  
  144.     /**
  145.      * Determines if any given variable exists and is a number
  146.      **/
  147.  
  148.     function verifyId$pId {
  149.         if empty$pId ) ) {
  150.             return false;
  151.         }
  152.         if is_array$pId ) ) {
  153.             foreach ($pId as $id{
  154.                 if !is_numeric$id ) )
  155.                     return false;
  156.             }
  157.             return true;
  158.         }
  159.         returnis_numeric$pId ) );
  160.     }
  161.  
  162.     /**
  163.      * This method should be THE method used to display a template. php files should not
  164.      * access $gBitSmarty directly.
  165.      *
  166.      * @param string pMsg error message to be displayed
  167.      * @return none this function will DIE DIE DIE!!!
  168.      * @access public
  169.      ***/
  170.     function display$pPackage$pTemplate {
  171.         global $gBitSmarty$gBitLanguage$style$style_base;
  172.         if (isset($style&& isset($style_base)) {
  173.             if (file_exists(BIT_THEME_PATH."styles/$style_base/$pTemplate")) {
  174.                 // Theme has overriden template
  175.                 $_smarty_tpl_file 'file:'.BIT_STYLES_PATH."/$style_base/$pTemplate";
  176.             else {
  177.                 // Use default
  178.                 $_smarty_tpl_file 'file:'.BIT_ROOT_PATH."$pPackage/templates/$pTemplate";
  179.             }
  180.         }
  181. /*
  182.         global $gBitLanguage, $style, $style_base;
  183.         if (isset($style) && isset($style_base)) {
  184.             if (file_exists(BIT_STYLES_PATH."/$style_base/$_smarty_tpl_file")) {
  185.                 $_smarty_tpl_file = BIT_STYLES_PATH."/$style_base/$_smarty_tpl_file";
  186.             }
  187.         }
  188.  */
  189.         $gBitSmarty->display$_smarty_tpl_file );
  190.         //        $gBitSmarty->display( 'bitpackage:'.$pPackage.$pTemplate );
  191.     }
  192.  
  193.     /**
  194.      * Returns entry from the mInfo hash if field exists
  195.      * @param pFieldName the instance of the database mechanism
  196.      ***/
  197.     function getField$pFieldName$pDefault NULL {
  198.         return!empty$this->mInfo[$pFieldName$this->mInfo[$pFieldName$pDefault );
  199.     }
  200.  
  201.     /**
  202.      * Prepares parameters with default values for any getList function
  203.      * @param pParamHash hash of parameters for any getList() function
  204.      * @return the link to display the page.
  205.      */
  206.     function prepGetList&$pListHash {
  207.         global $gBitSmarty$gBitSystem;
  208.  
  209.         // If offset is set use it if not then use offset =0
  210.         // use the max_records php variable to set the limit
  211.         // if sortMode is not set then use last_modified_desc
  212.         if empty$pListHash['sort_mode') ) {
  213.             if empty$_REQUEST["sort_mode") ) {
  214.                 $pListHash['sort_mode''last_modified_desc';
  215.             else {
  216.                 $pListHash['sort_mode'$_REQUEST['sort_mode'];
  217.             }
  218.         }
  219.  
  220.         ifempty$pListHash['max_records') ) {
  221.             global $gBitSystem;
  222.             $pListHash['max_records'$gBitSystem->getConfig"max_records"10 );
  223.         }
  224.  
  225.         if!isset$pListHash['offset') ) {
  226.             ifisset($pListHash['page') ) {
  227.                 $pListHash['offset'($pListHash['page'1$pListHash['max_records'];
  228.             else {
  229.                 if isset$_REQUEST["offset") ) {
  230.                     $pListHash['offset'$_REQUEST['offset'];
  231.                 elseifisset$_REQUEST['page'&& is_numeric$_REQUEST['page') ) {
  232.                     $pListHash['offset'($_REQUEST['page'1$pListHash['max_records'];
  233.                 elseifisset($_REQUEST['list_page']&& is_numeric$_REQUEST['list_page') ) {
  234.                     $pListHash['offset'($_REQUEST['list_page'1$pListHash['max_records'];
  235.                 else {
  236.                     $pListHash['offset'0;
  237.                 }
  238.             }
  239.         }
  240.  
  241. // Don't use  $_REQUEST["find"] as it can really screw with modules on search pages
  242.         if!empty$pListHash["find") ) {
  243.             $pListHash['find']$pListHash["find"];
  244.         else {
  245.             $pListHash['find'NULL;
  246.         }
  247.         $gBitSmarty->assign'find'$pListHash['find');
  248.  
  249.         ifisset$_REQUEST['date') ) {
  250.             $pListHash['date']$_REQUEST['date'];
  251.         else {
  252.             $pListHash['date'$gBitSystem->getUTCTime();
  253.         }
  254.  
  255.         ifempty$pListHash['load_comments') ) {
  256.             $pListHash['load_comments'FALSE;
  257.         }
  258.         ifempty$pListHash['load_num_comments') ) {
  259.             $pListHash['load_num_comments'FALSE;
  260.         }
  261.         ifempty$pListHash['parse_data') ) {
  262.             $pListHash['parse_data'FALSE;
  263.         }
  264.     }
  265. }
  266.  
  267.  
  268.  
  269.  
  270.  
  271. /**
  272.  * Translate a string
  273.  * 
  274.  * @param string $pString String that needs to be translated
  275.  * @access public
  276.  * @return void 
  277.  */
  278. function tra$pString {
  279.     global $gBitLanguage;
  280.     return$gBitLanguage->translate$pString ) );
  281. }
  282.  
  283. /**
  284.  * recursively remove files and directories
  285.  * 
  286.  * @param string $pPath directory we want to remove
  287.  * @param boolean $pFollowLinks follow symlinks or not
  288.  * @access public
  289.  * @return TRUE on success, FALSE on failure
  290.  */
  291. function unlink_r$pPath$pFollowLinks FALSE {
  292.     ifis_dir$pPath ) ) {
  293.         $dir opendir$pPath ;
  294.         whileFALSE !== $entry readdir$dir ) ) ) {
  295.             ifis_file"$pPath/$entry|| !$pFollowLinks && is_link"$pPath/$entry) ) ) {
  296.                 unlink"$pPath/$entry);
  297.             elseifis_dir"$pPath/$entry&& $entry != '.' && $entry != '..' {
  298.                 unlink_r"$pPath/$entry;
  299.             }
  300.         }
  301.         closedir$dir ;
  302.         return rmdir$pPath );
  303.     }
  304. }
  305.  
  306. /**
  307.  * tp_http_request
  308.  * 
  309.  * @param array $pUrl 
  310.  * @access public
  311.  * @return TRUE on success, FALSE on failure
  312.  */
  313. function tp_http_request$pUrl {
  314.     global $site_use_proxy,$site_proxy_host,$site_proxy_port;
  315.  
  316.     $data FALSE;
  317.  
  318.     // test url :
  319.     $pUrl trim$pUrl );
  320.     if (!preg_match("/^[-_a-zA-Z0-9:\/\.\?&;=\+]*$/",$pUrl)) {
  321.         return FALSE;
  322.     }
  323.     // rewrite url if sloppy # added a case for https urls
  324.     if ( (substr($pUrl,0,7<> "http://"&& (substr($pUrl,0,8<> "https://") ) {
  325.         $pUrl "http://" $pUrl;
  326.     }
  327.     if (substr_count($pUrl"/"3{
  328.         $pUrl .= "/";
  329.     }
  330.  
  331.     iffunction_exists'curl_init' ) ) {
  332.         $curl_obj curl_init();
  333.         curl_setopt($curl_objCURLOPT_URL$pUrl);
  334.         curl_setopt($curl_objCURLOPT_HEADER0);
  335.         curl_setopt($curl_objCURLOPT_USERAGENT$_SERVER['HTTP_USER_AGENT']);
  336.         curl_setopt($curl_objCURLOPT_FOLLOWLOCATION1);
  337.         curl_setopt($curl_objCURLOPT_RETURNTRANSFER1);
  338.         curl_setopt($curl_objCURLOPT_TIMEOUT5);
  339.  
  340.         // Proxy settings
  341.         if ($site_use_proxy == 'y'{
  342.             curl_setopt($curl_objCURLOPT_PROXY$site_proxy_host);
  343.             curl_setopt($curl_objCURLOPT_PROXYPORT$site_proxy_port);
  344.             curl_setopt($curl_objCURLOPT_HTTPPROXYTUNNEL1);
  345.         }
  346.  
  347.         $data curl_exec($curl_obj);
  348.         curl_close($curl_obj);
  349.     }
  350.  
  351.     return $data;
  352. }
  353.  
  354.  
  355. /**
  356.  * Parse XML Attributes and return an array
  357.  *
  358.  * this function has a whopper of a RegEx.
  359.  * I nabbed it from http://www.phpbuilder.com/annotate/message.php3?id=1000234 - XOXO spiderr
  360.  * 
  361.  * @param array $pString XML type string of parameters
  362.  * @access public
  363.  * @return TRUE on success, FALSE on failure
  364.  */
  365. function parse_xml_attributes$pString {
  366.     $parameters array'''' );
  367.     $regexp_str "/([A-Za-z0-9_-]+)(?:\\s*=\\s*(?:(\"|\')((?:[\\\\].|[^\\\\]?)*?)(?:\\2)|([^=\\s]*)))?/";
  368.     preg_match_all$regexp_str$pString$matchesPREG_SET_ORDER );
  369.     whilelist$key$match each$matches ) ) {
  370.         $attrib $match[1];
  371.         $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.
  372.         $value preg_replace"/\\\\(.)/","\\1",$value );
  373.         $parameters[$attribtrim$value'\"' );
  374.     }
  375.     return $parameters;
  376. }
  377.  
  378.  
  379. /**
  380.  * XML Entity Mandatory Escape Characters
  381.  * 
  382.  * @param array $string 
  383.  * @param array $quote_style 
  384.  * @access public
  385.  * @return TRUE on success, FALSE on failure
  386.  */
  387. function xmlentities$string$quote_style=ENT_QUOTES {
  388.     static $trans;
  389.     if (!isset($trans)) {
  390.         $trans get_html_translation_table(HTML_ENTITIES$quote_style);
  391.         foreach ($trans as $key => $value)
  392.             $trans[$key'&#'.ord($key).';';
  393.         // dont translate the '&' in case it is part of &xxx;
  394.         $trans[chr(38)'&';
  395.     }
  396.     // after the initial translation, _do_ map standalone '&' into '&#38;'
  397.     return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,5};)/","&#38;" strtr($string$trans));
  398. }
  399.  
  400. /**
  401.  * Redirect to another page or site
  402.  * @param string The url to redirect to
  403.  */
  404. function bit_redirect$pUrl {
  405.     // clean up URL before executing it
  406.     whilestrstr$pUrl'&&' ) ) {
  407.         $pUrl str_replace'&&''&'$pUrl );
  408.     }
  409.  
  410.     whilestrstr$pUrl'&amp;&amp;' ) ) {
  411.         $pUrl str_replace'&amp;&amp;''&amp;'$pUrl );
  412.     }
  413.  
  414.     // header locates should not have the &amp; in the address it breaks things
  415.     whilestrstr$pUrl'&amp;' ) ) {
  416.         $pUrl str_replace'&amp;''&'$pUrl );
  417.     }
  418.  
  419.     header('Location: ' $pUrl);
  420.     exit();
  421. }
  422.  
  423. function array_diff_keys({
  424.     $args func_get_args();
  425.  
  426.     $res $args[0];
  427.     if(!is_array($res)) {
  428.         return array();
  429.     }
  430.  
  431.     for($i=1;$i<count($args);$i++{
  432.         if(!is_array($args[$i])) {
  433.             continue;
  434.         }
  435.         foreach ($args[$ias $key => $data{
  436.             unset($res[$key]);
  437.         }
  438.     }
  439.     return $res;
  440. }
  441.  
  442. function trim_array&$pArray {
  443.     ifis_array$pArray ) ) {
  444.         foreacharray_keys$pArray as $key {
  445.             ifis_string$pArray[$key) ) {
  446.                 $pArray[$keytrim$pArray[$key);
  447.             }
  448.         }
  449.     }
  450. }
  451.  
  452.  
  453. function ordinalize$num {
  454.     $ord '';
  455.     ifis_numeric$num ) ) {
  456.         if ($num >= 11 and $num <= 19{
  457.            $ord tra"th" );
  458.         elseif $num 10 == {
  459.            $ord tra"st" );
  460.         elseif $num 10 == {
  461.            $ord tra"nd" );
  462.         elseif $num 10 == {
  463.            $ord tra"rd" );
  464.         else {
  465.            $ord tra"th" );
  466.         }
  467.     }
  468.  
  469.     return $num.$ord;
  470. }
  471.  
  472. /**
  473.  * Cleans file path according to system we're on
  474.  * 
  475.  * @param array $pPath 
  476.  * @access public
  477.  * @return TRUE on success, FALSE on failure
  478.  */
  479. function clean_file_path$pPath {
  480.     $pPath (!empty($_SERVER["SERVER_SOFTWARE"]&& strpos($_SERVER["SERVER_SOFTWARE"],"IIS"str_replace'\/''\\'$pPath$pPath);
  481.     return $pPath;
  482. }
  483.  
  484. function chkgd2({
  485.     if (!isset($_SESSION['havegd2'])) {
  486. #    TODO test this logic in PHP 4.3
  487. #    if (version_compare(phpversion(), "4.3.0") >= 0) {
  488. #        $_SESSION['havegd2'] = true;
  489. #    } else {
  490.         ob_start();
  491.  
  492.         phpinfo (INFO_MODULES);
  493.         $_SESSION['havegd2'preg_match('/GD Version.*2.0/'ob_get_contents());
  494.         ob_end_clean();
  495. #    }
  496.     }
  497.  
  498.     return $_SESSION['havegd2'];
  499. }
  500.  
  501. function httpScheme({
  502.     return 'http'.( ( isset($_SERVER['HTTPS'&& $_SERVER['HTTPS'== 'on' ) ) 's' '' );
  503. }
  504.  
  505. function httpPrefix({
  506. /*
  507.     if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) {
  508.         $rv = 'https://' . $_SERVER['HTTP_HOST'];
  509.  
  510.         if ($_SERVER['SERVER_PORT'] != 443)
  511.             $rv .= ':' . $_SERVER['SERVER_PORT'];
  512.     } else {
  513.         $rv = 'http://' . $_SERVER['HTTP_HOST'];
  514.  
  515.         if ($_SERVER['SERVER_PORT'] != 80)
  516.             $rv .= ':' . $_SERVER['SERVER_PORT'];
  517.     }
  518.  
  519.     return $rv;
  520. */
  521.     /* Warning by zaufi: as far as I saw in my apache 1.3.27
  522.      * there is no need to add port if it is non default --
  523.      * $_SERVER['HTTP_HOST'] already contain it ...
  524.      */
  525.     return 'http'.((isset($_SERVER['HTTPS']&& ($_SERVER['HTTPS'== 'on')) 's' '').'://'.$_SERVER['HTTP_HOST'];
  526. }
  527.  
  528. if!function_exists'file_get_contents' ) ) {
  529.     /**
  530.      * PHP version compatability function
  531.      * 
  532.      * @param array $pFile File
  533.      * @access public
  534.      * @return TRUE on success, FALSE on failure
  535.      */
  536.     function file_get_contents$pFile {
  537.         ob_start();
  538.  
  539.         $retval @readfile( </