Public paste
IPB
By: Joost | Date: May 6 2007 15:51 | Format: PHP | Expires: never | Size: 64 KB | Hits: 2842

  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. |   Invision Power Board
  5. |   =============================================
  6. |   by Matthew Mecham
  7. |   (c) 2001 - 2006 Invision Power Services, Inc.
  8. |   http://www.invisionpower.com
  9. |   =============================================
  10. |   Web: http://www.invisionboard.com
  11. |   Licence Info: http://www.invisionboard.com/?license
  12. +---------------------------------------------------------------------------
  13. |   > $Date: 2007-02-05 15:21:24 -0500 (Mon, 05 Feb 2007) $
  14. |   > $Revision: 838 $
  15. |   > $Author: bfarber $
  16. +---------------------------------------------------------------------------
  17. |
  18. |   > Multi function library
  19. |   > Module written by Matt Mecham
  20. |   > Date started: 14th February 2002
  21. |
  22. |       > Module Version Number: 1.0.0
  23. |   > Quality Checked: Wed 15 Sept. 2004
  24. +--------------------------------------------------------------------------
  25. */
  26.  
  27. /**
  28. * Main IPSclass class.
  29. *
  30. * This is the main class which is referenced via $ipsclass throughout
  31. * all the IPB sub-classes and modules. It holds amongst others:
  32. * <code>
  33. * // Object from class_forums.php class
  34. * $ipsclass->forums
  35. * // Object from class_display.php class
  36. * $ipsclass->print
  37. * // Object from SQL classes
  38. * $ipsclass->db
  39. * // Array of parsed caches  (from table cache_store)
  40. * $ipsclass->cache
  41. * // Array of settings variables (taken from parsed setting cache)
  42. * $ipsclass->vars
  43. * // Array of member variables (taken from class_session::$member)
  44. * $ipsclass->member
  45. * // Array of sanitized _GET _POST data (run via parse_clean_value)
  46. * $ipsclass->input
  47. * // Array of language strings loaded via separate lang files
  48. * $ipsclass->lang
  49. * // Variable: Full base URL with session ID if required
  50. * $ipsclass->base_url
  51. * // Array of compiled template objects
  52. * $ipsclass->compiled_templates
  53. * </code>
  54. * Recommended method of passing $ipsclass through modules
  55. * <code>
  56. * $module           = new module();
  57. * $module->ipsclass =& $ipsclass; // Create reference
  58. * </code>
  59. *
  60. * @package              InvisionPowerBoard
  61. * @author               Matt Mecham
  62. * @copyright    Invision Power Services, Inc.
  63. * @version              2.1
  64. */
  65.  
  66. if ( ! defined( 'IPSCLASS_LOADED' ) )
  67. {
  68.         /**
  69.         * IPSCLASS loaded Flag
  70.         */
  71.         define( 'IPSCLASS_LOADED', 1 );
  72. }
  73.  
  74. /**
  75. * Main ipsclass class.
  76. *
  77. * This class holds all the non-class specific functions
  78. *
  79. * @package      InvisionPowerBoard
  80. * @author   Matt Mecham
  81. * @version      2.1
  82. */
  83. class ipsclass {
  84.  
  85.         /**
  86.         * HUMAN version string (Eg: v2.1 BETA 1)
  87.         *
  88.         * @var string
  89.         */
  90.         var $version      = IPBVERSION;
  91.        
  92.         /**
  93.         * LONG version number (Eg: 21000.BUILD_DATE.REASON)
  94.         *
  95.         * @var string
  96.         */
  97.         var $acpversion      = IPB_LONG_VERSION;
  98.         var $vn_full         = '';
  99.         var $vn_build_date   = '';
  100.         var $vn_build_reason = '';
  101.        
  102.         /**
  103.         * Member Array
  104.         *
  105.         * @var array
  106.         */
  107.         var $member             = array();
  108.        
  109.         /**
  110.         * _GET _POST Input Array
  111.         *
  112.         * @var array
  113.         */
  114.         var $input              = array( 'st'                   => 0,
  115.                                                                          't'                    => 0,
  116.                                                                          'f'                    => 0,
  117.                                                                          'id'                   => 0,
  118.                                                                          'tid'                  => 0,
  119.                                                                          'p'                    => 0,
  120.                                                                          'pid'                  => 0,
  121.                                                                          'gopid'                => 0,
  122.                                                                          'qpid'                 => 0,
  123.                                                                          'b'                    => 0,
  124.                                                                          'cal_id'               => 0,
  125.                                                                          'act'                  => NULL,
  126.                                                                          'L'                    => 0,
  127.                                                                          'adsess'               => NULL,
  128.                                                                          'code'                 => NULL,
  129.                                                                          'CODE'                 => NULL,
  130.                                                                          'mode'                 => NULL,
  131.                                                                          'modfilter'    => NULL,
  132.                                                                          'preview'              => NULL,
  133.                                                                          'attachgo'     => NULL,
  134.                                                                          'nocp'                 => NULL,
  135.                                                                          'tab'                  => NULL,
  136.                                                                          'sort'                 => NULL,
  137.                                                                          'dsterror'     => 0,
  138.                                                                          'MID'                  => 0,
  139.                                                                          'MSID'                 => 0,
  140.                                                                          'uid'                  => 0,
  141.                                                                          'ip'                   => NULL,
  142.                                                                          'selectedpids' => NULL,
  143.                                                                          'nav'                  => NULL,
  144.                                                                          'calendar_id'  => 0,  );
  145.        
  146.         /**
  147.         * Setting variables array
  148.         *
  149.         * @var array
  150.         */
  151.         var $vars               = array( );
  152.        
  153.         /**
  154.         * Language strings array
  155.         *
  156.         * @var array
  157.         */
  158.         var $lang               = array();
  159.        
  160.         /**
  161.         * Skin variables array
  162.         *
  163.         * @var array
  164.         */
  165.         var $skin               = array();
  166.        
  167.         /**
  168.         * Compiled loaded templates
  169.         *
  170.         * @var array
  171.         */
  172.         var $compiled_templates = array();
  173.        
  174.         /**
  175.         * Loaded templates inc. ID
  176.         *
  177.         * @var array
  178.         */
  179.         var $loaded_templates = array();
  180.        
  181.         /**
  182.         * Cache array
  183.         *
  184.         * @var array
  185.         */
  186.         var $cache              = array();
  187.        
  188.         /**
  189.         * Cache Library
  190.         *
  191.         * @var array
  192.         */     
  193.         var $cachelib;
  194.        
  195.         /**
  196.         * Session ID
  197.         *
  198.         * @var string
  199.         */
  200.         var $session_id         = "";
  201.        
  202.         /**
  203.         * Base URL
  204.         *
  205.         * @var string
  206.         */
  207.         var $base_url           = "";
  208.        
  209.         /**
  210.         * Language ID; corresponds to cache/lang_cache/{folder}/
  211.         *
  212.         * @var string
  213.         */
  214.         var $lang_id            = "";
  215.        
  216.         /**
  217.         * Session type (cookie or url)
  218.         *
  219.         * @var string
  220.         */
  221.         var $session_type       = "";
  222.        
  223.         /**#@+
  224.         * @access public
  225.         * @var string
  226.         */
  227.         var $lastclick               = "";
  228.         var $location                = "";
  229.         var $debug_html              = "";
  230.         var $perm_id                 = "";
  231.         var $offset                  = "";
  232.         var $num_format              = "";
  233.         var $query_string_safe       = '';
  234.         var $query_string_formatted  = '';
  235.        
  236.         /**
  237.         * MD5 Check variable
  238.         */
  239.         var $md5_check          = '';
  240.         /**#@-*/
  241.        
  242.         var $server_load        = 0;
  243.         var $can_use_fancy_js   = 0;
  244.         var $force_editor_change = 0;
  245.         var $offset_set         = 0;
  246.         var $allow_unicode      = 1;
  247.         var $get_magic_quotes   = 0;
  248.         var $no_print_header    = 0;
  249.        
  250.         var $today_time;
  251.         var $yesterday_time;
  252.        
  253.         /**#@+
  254.         * @access public
  255.         * @var object
  256.         */
  257.         var $converge;
  258.         var $print;
  259.         var $sess;
  260.         var $forums;
  261.         var $class_convert_charset;
  262.         /**#@-*/
  263.  
  264.         /**#@+
  265.         * @access public
  266.         * @var array
  267.         */
  268.         var $topic_cache      = array();
  269.         var $time_formats     = array();
  270.         var $time_options     = array();
  271.         var $today_array      = array();
  272.         var $forum_read           = array();
  273.         var $perm_id_array    = array();
  274.        
  275.         /**
  276.         * User's browser array (version, browser)
  277.         */
  278.         var $browser;
  279.         var $is_bot                             = 0;
  280.         /**#@-*/
  281.        
  282.         var $main_msg                   = '';
  283.         var $html_help_msg              = '';
  284.         var $html_help_title    = '';
  285.         var $kill_menu                  = 0;
  286.         var $body_extra                 = '';
  287.        
  288.         var $work_classes       = array( 'class_template_engine' => null,
  289.                                                                          'class_captcha'         => null );
  290.        
  291.         /**
  292.         * Sensitive cookies
  293.         * var array
  294.         */
  295.         var $sensitive_cookies  = array( 'ipb_stronghold', 'session_id', 'ipb_admin_session_id', 'member_id', 'pass_hash' );
  296.                                                                        
  297.         /*-------------------------------------------------------------------------*/
  298.         // Set up some standards to save CPU later
  299.         /*-------------------------------------------------------------------------*/
  300.        
  301.         /**
  302.         * Initial ipsclass, set up some variables for later
  303.         * Populates:
  304.         * $this->time_options, $this->num_format, $this->get_magic_quotes, $this->ip_address
  305.         * $this->user_agent, $this->browser, $this->operating_system
  306.         *
  307.         * @return void;
  308.         */
  309.         function initiate_ipsclass()
  310.         {
  311.                 //-----------------------------------------
  312.                 // Version numbers
  313.                 //-----------------------------------------
  314.                
  315.                 //$this->acpversion = '210015.060501.u';
  316.                
  317.                 if ( strstr( $this->acpversion , '.' ) )
  318.                 {
  319.                         list( $n, $b, $r ) = explode( ".", $this->acpversion );
  320.                 }
  321.                 else
  322.                 {
  323.                         $n = $b = $r = '';
  324.                 }
  325.                
  326.                 $this->vn_full         = $this->acpversion;
  327.                 $this->acpversion      = $n;
  328.                 $this->vn_build_date   = $b;
  329.                 $this->vn_build_reason = $r;
  330.                
  331.                 //-----------------------------------------
  332.                 // Time options
  333.                 //-----------------------------------------
  334.                
  335.                 $this->time_options = array( 'JOINED' => $this->vars['clock_joined'],
  336.                                                                          'SHORT'  => $this->vars['clock_short'],
  337.                                                                          'LONG'   => $this->vars['clock_long'],
  338.                                                                          'TINY'   => isset($this->vars['clock_tiny']) ? $this->vars['clock_tiny'] : 'j M Y - G:i',
  339.                                                                          'DATE'   => isset($this->vars['clock_date']) ? $this->vars['clock_date'] : 'j M Y',
  340.                                                                    );
  341.                                                                    
  342.                 $this->num_format = ( $this->vars['number_format'] == 'space' ) ? ' ' : $this->vars['number_format'];
  343.                
  344.                 //-----------------------------------------
  345.                 // Sort out the accessing IP
  346.                 // (Thanks to Cosmos and schickb)
  347.                 //-----------------------------------------
  348.                
  349.                 $addrs = array();
  350.                
  351.                 if ( $this->vars['xforward_matching'] )
  352.                 {
  353.                         foreach( array_reverse( explode( ',', $this->my_getenv('HTTP_X_FORWARDED_FOR') ) ) as $x_f )
  354.                         {
  355.                                 $x_f = trim($x_f);
  356.                                
  357.                                 if ( preg_match( '/^d{1,3}.d{1,3}.d{1,3}.d{1,3}$/', $x_f ) )
  358.                                 {
  359.                                         $addrs[] = $x_f;
  360.                                 }
  361.                         }
  362.                        
  363.                         $addrs[] = $this->my_getenv('HTTP_CLIENT_IP');
  364.                         $addrs[] = $this->my_getenv('HTTP_PROXY_USER');
  365.                 }
  366.                
  367.                 $addrs[] = $this->my_getenv('REMOTE_ADDR');
  368.                
  369.                 //-----------------------------------------
  370.                 // Do we have one yet?
  371.                 //-----------------------------------------
  372.                
  373.                 foreach ( $addrs as $ip )
  374.                 {
  375.                         if ( $ip )
  376.                         {
  377.                                 preg_match( "/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/", $ip, $match );
  378.  
  379.                                 $this->ip_address = $match[1].'.'.$match[2].'.'.$match[3].'.'.$match[4];
  380.                                
  381.                                 if ( $this->ip_address AND $this->ip_address != '...' )
  382.                                 {
  383.                                         break;
  384.                                 }
  385.                         }
  386.                 }
  387.                
  388.                 //-----------------------------------------
  389.                 // Make sure we take a valid IP address
  390.                 //-----------------------------------------
  391.                  
  392.                 if ( ( ! $this->ip_address OR $this->ip_address == '...' ) AND ! isset( $_SERVER['SHELL'] ) )
  393.                 {
  394.                         print "Could not determine your IP address";
  395.                         exit();
  396.                 }
  397.                
  398.                 #Backwards compat:
  399.                 $this->input["IP_ADDRESS"] = $this->ip_address;
  400.                
  401.                 //-----------------------------------------
  402.                 // Make a safe query string
  403.                 //-----------------------------------------
  404.                
  405.                 $this->query_string_safe = str_replace( '&amp;amp;', '&amp;', $this->parse_clean_value( urldecode($this->my_getenv('QUERY_STRING')) ) );
  406.                 $this->query_string_real = str_replace( '&amp;'    , '&'    , $this->query_string_safe );
  407.                
  408.                 //-----------------------------------------
  409.                 // Format it..
  410.                 //-----------------------------------------
  411.                
  412.                 $this->query_string_formatted = str_replace( $this->vars['board_url'] . '/index.'.$this->vars['php_ext'].'?', '', $this->query_string_safe );
  413.                 $this->query_string_formatted = preg_replace( "#s=([a-z0-9]){32}#", '', $this->query_string_safe );
  414.                
  415.                 //-----------------------------------------
  416.                 // Admin dir
  417.                 //-----------------------------------------
  418.                
  419.                 $this->vars['_admin_link'] = $this->vars['board_url'] . '/' . IPB_ACP_DIRECTORY . '/index.php';
  420.                
  421.                 //-----------------------------------------
  422.                 //  Upload dir?
  423.                 //-----------------------------------------
  424.                
  425.                 $this->vars['upload_dir'] = $this->vars['upload_dir'] ? $this->vars['upload_dir'] : ROOT_PATH.'uploads';
  426.                
  427.                 //-----------------------------------------
  428.                 // Char set
  429.                 //-----------------------------------------
  430.                
  431.                 $this->vars['gb_char_set'] = $this->vars['gb_char_set'] ? $this->vars['gb_char_set'] : 'iso-8859-1';
  432.                
  433.                 //-----------------------------------------
  434.                 // Max display name length
  435.                 //-----------------------------------------
  436.                
  437.                 $this->vars['max_user_name_length'] = $this->vars['max_user_name_length'] ? $this->vars['max_user_name_length'] : 26;
  438.                
  439.                 //-----------------------------------------
  440.                 // PHP API
  441.                 //-----------------------------------------
  442.                
  443.                 define('IPB_PHP_SAPI', php_sapi_name() );
  444.                
  445.                 //-----------------------------------------
  446.                 // IPS CLASS INITIATED
  447.                 //-----------------------------------------
  448.                
  449.                 if ( ! defined( 'IPSCLASS_INITIATED' ) )
  450.                 {
  451.                         define( 'IPSCLASS_INITIATED', 1 );
  452.                 }
  453.                
  454.                 //-----------------------------------------
  455.                 // Get user-agent, browser and OS
  456.                 //-----------------------------------------
  457.                
  458.                 $this->user_agent       = $this->parse_clean_value($this->my_getenv('HTTP_USER_AGENT'));
  459.                 $this->browser          = $this->fetch_browser();
  460.                 $this->operating_system = $this->fetch_os();
  461.                        
  462.                 //-----------------------------------------
  463.                 // Can we use fancy JS? IE6, Safari, Moz
  464.                 // and opera 7.6
  465.                 //-----------------------------------------
  466.                
  467.                 if ( $this->browser['browser'] == 'ie' AND $this->browser['version'] >= 6.0 )
  468.                 {
  469.                         $this->can_use_fancy_js = 1;
  470.                 }
  471.                 else if ( $this->browser['browser'] == 'gecko' AND $this->browser['version'] >= 20030312 )
  472.                 {
  473.                         $this->can_use_fancy_js = 1;
  474.                 }
  475.                 else if ( $this->browser['browser'] == 'mozilla' AND $this->browser['version'] >= 1.3 )
  476.                 {
  477.                         $this->can_use_fancy_js = 1;
  478.                 }
  479.                 else if ( $this->browser['browser'] == 'opera' AND $this->browser['version'] >= 7.6 )
  480.                 {
  481.                         $this->can_use_fancy_js = 1;
  482.                 }
  483.                 else if ( $this->browser['browser'] == 'safari' AND $this->browser['version'] >= 120)
  484.                 {
  485.                         $this->can_use_fancy_js = 1;
  486.                 }
  487.        
  488.                 //$this->can_use_fancy_js = 0;
  489.         }
  490.        
  491.        
  492.         /*-------------------------------------------------------------------------*/
  493.         // INIT: Establish Cache Abstraction
  494.         /*-------------------------------------------------------------------------*/
  495.        
  496.         /**
  497.         * Determines and establishes cache class
  498.         *
  499.         * @todo         Memcache, mmcache, diskcache
  500.         * @param        void
  501.         * @return       void
  502.         */     
  503.        
  504.         function init_cache_setup()
  505.         {
  506.                 //--------------------------------
  507.                 // Eaccelerator...
  508.                 //--------------------------------
  509.                
  510.                 if( function_exists('eaccelerator_get') AND isset($this->vars['use_eaccelerator']) AND $this->vars['use_eaccelerator'] == 1 )
  511.                 {
  512.                         require KERNEL_PATH.'class_cache_eaccelerator.php';
  513.                         $this->cachelib = new cache_lib( $this->vars['board_url'] );
  514.                 }
  515.                
  516.                 //--------------------------------
  517.                 // Turck-mmcache...
  518.                 //--------------------------------
  519.                
  520.                 if( function_exists('mmcache_get') AND isset($this->vars['use_mmcache']) AND $this->vars['use_mmcache'] == 1 )
  521.                 {
  522.                         require KERNEL_PATH.'class_cache_mmcache.php';
  523.                         $this->cachelib = new cache_lib( $this->vars['board_url'] );
  524.                 }              
  525.                
  526.                 //--------------------------------
  527.                 // Memcache
  528.                 //--------------------------------     
  529.                        
  530.                 else if( function_exists('memcache_connect') AND isset($this->vars['use_memcache']) AND $this->vars['use_memcache'] == 1 )
  531.                 {
  532.                         require KERNEL_PATH.'class_cache_memcache.php';
  533.                         $this->cachelib = new cache_lib( $this->vars['board_url'] );
  534.                        
  535.                         $this->cachelib->connect( $this->vars );
  536.                 }
  537.                
  538.                 //--------------------------------
  539.                 // XCache...
  540.                 //--------------------------------
  541.                
  542.                 else if( function_exists('xcache_get') AND isset($this->vars['use_xcache']) AND $this->vars['use_xcache'] == 1 )
  543.                 {
  544.                         require KERNEL_PATH.'class_cache_xcache.php';
  545.                         $this->cachelib = new cache_lib( $this->vars['board_url'] );
  546.                 }              
  547.                
  548.                 //--------------------------------
  549.                 // APC...
  550.                 //--------------------------------
  551.                
  552.                 else if( function_exists('apc_fetch') AND isset($this->vars['use_apc']) AND $this->vars['use_apc'] == 1 )
  553.                 {
  554.                         require KERNEL_PATH.'class_cache_apc.php';
  555.                         $this->cachelib = new cache_lib( $this->vars['board_url'] );
  556.                 }              
  557.                
  558.                 //--------------------------------
  559.                 // Diskcache
  560.                 //--------------------------------     
  561.                
  562.                 else if( isset($this->vars['use_diskcache']) AND $this->vars['use_diskcache'] == 1 )
  563.                 {
  564.                         require KERNEL_PATH.'class_cache_diskcache.php';
  565.                         $this->cachelib = new cache_lib( $this->vars['board_url'] );
  566.                 }
  567.                
  568.                 if( is_object($this->cachelib) AND $this->cachelib->crashed )
  569.                 {
  570.                         // There was a problem - not installed maybe?
  571.                        
  572.                         unset($this->cachelib);
  573.                         $this->cachelib = NULL;
  574.                 }
  575.         }
  576.        
  577.         /*-------------------------------------------------------------------------*/
  578.         // INIT: Load cache
  579.         /*-------------------------------------------------------------------------*/
  580.        
  581.         /**
  582.         * Loads and parses the required cache elements from the DB
  583.         *
  584.         * @todo         Add in methods to save and write to disk file
  585.         * @param        array   Array of caches to load
  586.         * @return       void
  587.         */
  588.        
  589.         function init_load_cache( $cachearray=array('settings', 'group_cache', 'systemvars', 'skin_id_cache', 'forum_cache', 'rss_export') )
  590.         {
  591.                 //--------------------------------
  592.                 // Eaccelerator...
  593.                 //--------------------------------
  594.                
  595.                 if( is_object($this->cachelib) )
  596.                 {
  597.                         $temp_cache      = array();
  598.                         $new_cache_array = array();
  599.                        
  600.                         foreach( $cachearray as $key )
  601.                         {
  602.                                 $temp_cache[$key] = $this->cachelib->do_get( $key );
  603.                                
  604.                                 if( !$temp_cache[$key] )
  605.                                 {
  606.                                         $new_cache_array[] = $key;
  607.                                 }
  608.                                 else
  609.                                 {
  610.                                         if ( $key == 'settings' )
  611.                                         {
  612.                                                 $tmp = unserialize( $temp_cache[$key] );
  613.                                                
  614.                                                 if ( is_array( $tmp ) and count( $tmp ) )
  615.                                                 {
  616.                                                         foreach( $tmp as $k => $v )
  617.                                                         {
  618.                                                                 $this->vars[ $k ] = $v;
  619.                                                         }
  620.                                                 }
  621.                                                
  622.                                                 if( !isset($this->vars['blog_default_view']) )
  623.                                                 {
  624.                                                         $this->vars['blog_default_view'] = '';
  625.                                                 }
  626.                                                
  627.                                                 unset( $tmp );
  628.                                         }
  629.                                         else
  630.                                         {
  631.                                                 if ( strstr( $temp_cache[$key], "a:" ) )
  632.                                                 {
  633.                                                         $this->cache[ $key ] = unserialize( $temp_cache[$key] );
  634.                                                 }
  635.                                                 else if( $temp_cache[$key] == "EMPTY" )
  636.                                                 {
  637.                                                         $this->cache[ $key ] = NULL;
  638.                                                 }
  639.                                                 else
  640.                                                 {
  641.                                                         $this->cache[ $key ] = $temp_cache[$key];
  642.                                                 }
  643.                                         }
  644.                                 }
  645.                         }
  646.                        
  647.                         $cachearray = $new_cache_array;
  648.                        
  649.                         unset($new_cache_array, $temp_cache);
  650.                 }
  651.                
  652.                 //echo "<pre>";print_r($this->cache);exit;
  653.                
  654.                 //--------------------------------
  655.                 // Generate cache list
  656.                 //--------------------------------
  657.                
  658.                 $cachelist = "";
  659.                
  660.                 if( count($cachearray) )
  661.                 {
  662.                         $cachelist = "'".implode( "','", $cachearray )."'";
  663.                 }
  664.                
  665.                 //--------------------------------
  666.                 // Get from DB...
  667.                 //--------------------------------
  668.                
  669.                 if ( $cachelist )
  670.                 {
  671.                         $this->DB->simple_construct( array( 'select' => '*', 'from' => 'cache_store', 'where' => "cs_key IN ( $cachelist )" ) );
  672.                         $this->DB->simple_exec();
  673.                        
  674.                         while ( $r = $this->DB->fetch_row() )
  675.                         {
  676.                                 if ( $r['cs_key'] == 'settings' )
  677.                                 {
  678.                                         $tmp = unserialize( $r['cs_value'] );
  679.                                        
  680.                                         if ( is_array( $tmp ) and count( $tmp ) )
  681.                                         {
  682.                                                 foreach( $tmp as $k => $v )
  683.                                                 {
  684.                                                         $this->vars[ $k ] = $v;
  685.                                                 }
  686.                                         }
  687.                                        
  688.                                         if ( ! isset($this->vars['blog_default_view']) )
  689.                                         {
  690.                                                 $this->vars['blog_default_view'] = '';
  691.                                         }
  692.                                        
  693.                                         unset( $tmp );
  694.                                 }
  695.                                 else
  696.                                 {
  697.                                         if ( $r['cs_array'] )
  698.                                         {
  699.                                                 $this->cache[ $r['cs_key'] ] = unserialize( $r['cs_value'] );
  700.                                         }
  701.                                         else
  702.                                         {
  703.                                                 $this->cache[ $r['cs_key'] ] = $r['cs_value'];
  704.                                         }
  705.                                 }
  706.                                
  707.                                 if( is_object($this->cachelib) )
  708.                                 {
  709.                                         if( !$r['cs_value'] )
  710.                                         {
  711.                                                 $r['cs_value'] = "EMPTY";
  712.                                         }
  713.                                        
  714.                                         $this->cachelib->do_put( $r['cs_key'], $r['cs_value'] );
  715.                                 }
  716.                         }
  717.                 }
  718.                
  719.                 if ( ! isset( $this->cache['systemvars'] ) OR ! isset( $this->cache['systemvars']['task_next_run']) )
  720.                 {
  721.                         $this->update_cache( array( 'deletefirst' => 1, 'donow' => 1, 'name' => 'systemvars', 'array' => 1, 'value' => array( 'task_next_run' => time() ) ) );
  722.                 }
  723.                
  724.                 if( ! isset( $this->cache['forum_cache'] ) OR empty( $this->cache['forum_cache']) )
  725.                 {
  726.                         $this->update_forum_cache();
  727.                 }
  728.                
  729.                 if( ! isset( $this->cache['group_cache'] ) OR empty( $this->cache['group_cache']) )
  730.                 {
  731.                         $this->cache['group_cache'] = array();
  732.                
  733.                         $this->DB->simple_construct( array( 'select' => "*",
  734.                                                                                                  'from'   => 'groups'
  735.                                                                                 )      );
  736.                        
  737.                         $this->DB->simple_exec();
  738.                        
  739.                         while ( $i = $this->DB->fetch_row() )
  740.                         {
  741.                                 $this->cache['group_cache'][ $i['g_id'] ] = $i;
  742.                         }
  743.                        
  744.                         $this->update_cache( array( 'name' => 'group_cache', 'array' => 1, 'deletefirst' => 1 ) );
  745.                 }      
  746.                
  747.                 //--------------------------------
  748.                 // Set up cache path
  749.                 //--------------------------------
  750.                
  751.                 if( ! defined( 'CACHE_PATH' ) )
  752.                 {
  753.                         if ( $this->vars['ipb_cache_path'] )
  754.                         {
  755.                                 define( 'CACHE_PATH', $this->vars['ipb_cache_path'] );
  756.                         }
  757.                         else
  758.                         {
  759.                                 define( 'CACHE_PATH', ROOT_PATH );
  760.                         }
  761.                 }
  762.                
  763.                 //-----------------------------------------
  764.                 // IPS CACHE LOADED
  765.                 //-----------------------------------------
  766.                
  767.                 if ( ! defined( 'IPSCLASS_CACHE_LOADED' ) )
  768.                 {
  769.                         define( 'IPSCLASS_CACHE_LOADED', 1 );
  770.                 }
  771.                
  772.                 //--------------------------------
  773.                 // Set up defaults
  774.                 //--------------------------------
  775.                
  776.                 $this->vars['topic_title_max_len'] = $this->vars['topic_title_max_len'] ? $this->vars['topic_title_max_len'] : 50;
  777.                 #$this->vars['gb_char_set'] = 'UTF-8';
  778.         }
  779.        
  780.         /*-------------------------------------------------------------------------*/
  781.         // INIT: DB Connection
  782.         /*-------------------------------------------------------------------------*/
  783.        
  784.         /**
  785.         * Initializes the database connection and populates $ipsclass->DB
  786.         *
  787.         * @return void
  788.         */
  789.        
  790.         function init_db_connection()
  791.         {
  792.                 $this->vars['sql_driver'] = ! $this->vars['sql_driver'] ? 'mysql' : strtolower($this->vars['sql_driver']);
  793.                
  794.                 if ( ! class_exists( 'db_main' ) )
  795.                 {
  796.                         require_once( KERNEL_PATH.'class_db.php' );
  797.                         require_once( KERNEL_PATH.'class_db_'.$this->vars['sql_driver'].".php" );
  798.                 }
  799.                
  800.                 $classname = "db_driver_".$this->vars['sql_driver'];
  801.                
  802.                 $this->DB = new $classname;
  803.                
  804.                 $this->DB->obj['sql_database']         = $this->vars['sql_database'];
  805.                 $this->DB->obj['sql_user']             = $this->vars['sql_user'];
  806.                 $this->DB->obj['sql_pass']             = $this->vars['sql_pass'];
  807.                 $this->DB->obj['sql_host']             = $this->vars['sql_host'];
  808.                 $this->DB->obj['sql_tbl_prefix']       = $this->vars['sql_tbl_prefix'];
  809.                 $this->DB->obj['force_new_connection'] = isset($this->vars['sql_force_new_connection']) ? $this->vars['sql_force_new_connection'] : 0;
  810.                 $this->DB->obj['use_shutdown']         = USE_SHUTDOWN;
  811.                 # Error log
  812.                 $this->DB->obj['error_log']            = ROOT_PATH . 'cache/sql_error_log_'.date('m_d_y').'.cgi';
  813.                 $this->DB->obj['use_error_log']        = IN_DEV ? 0 : 1;
  814.                 # Debug log - Don't use this on a production board!
  815.                 $this->DB->obj['debug_log']            = ROOT_PATH . 'cache/sql_debug_log_'.date('m_d_y').'.cgi';
  816.                 $this->DB->obj['use_debug_log']        = 0;
  817.                
  818.                 //-----------------------------------
  819.                 // Load query file
  820.                 //-----------------------------------
  821.                
  822.                 if ( defined( 'IPB_LOAD_SQL' ) )
  823.                 {
  824.                         $this->DB->obj['query_cache_file'] = ROOT_PATH.'sources/sql/'.$this->vars['sql_driver'].'_'. IPB_LOAD_SQL .'.php';
  825.                 }
  826.                 else if ( IPB_THIS_SCRIPT == 'admin' )
  827.                 {
  828.                         $this->DB->obj['query_cache_file'] = ROOT_PATH.'sources/sql/'.$this->vars['sql_driver'].'_admin_queries.php';
  829.                 }
  830.                 else
  831.                 {
  832.                         $this->DB->obj['query_cache_file'] = ROOT_PATH.'sources/sql/'.$this->vars['sql_driver'].'_queries.php';
  833.                 }
  834.                        
  835.                 //-----------------------------------
  836.                 // Required vars?
  837.                 //-----------------------------------
  838.                
  839.                 if ( is_array( $this->DB->connect_vars ) and count( $this->DB->connect_vars ) )
  840.                 {
  841.                         foreach( $this->DB->connect_vars as $k => $v )
  842.                         {
  843.                                 $this->DB->connect_vars[ $k ] = isset($this->vars[ $k ]) ? $this->vars[ $k ] : '';
  844.                         }
  845.                 }
  846.                
  847.                 //--------------------------------
  848.                 // Backwards compat
  849.                 //--------------------------------
  850.                
  851.                 if ( !isset($this->DB->connect_vars['mysql_tbl_type']) OR !$this->DB->connect_vars['mysql_tbl_type'] )
  852.                 {
  853.                         $this->DB->connect_vars['mysql_tbl_type'] = 'myisam';
  854.                 }
  855.                
  856.                 //--------------------------------
  857.                 // Make CONSTANT
  858.                 //--------------------------------
  859.                
  860.                 define( 'SQL_PREFIX'              , $this->DB->obj['sql_tbl_prefix'] );
  861.                 define( 'SQL_DRIVER'              , $this->vars['sql_driver']        );
  862.                 define( 'IPS_MAIN_DB_CLASS_LOADED', TRUE );
  863.                
  864.                 //--------------------------------
  865.                 // Get a DB connection
  866.                 //--------------------------------
  867.                
  868.                 $this->DB->connect();
  869.                
  870.                 //-----------------------------------------
  871.                 // IPS DB LOADED
  872.                 //-----------------------------------------
  873.                
  874.                 if ( ! defined( 'IPSCLASS_DB_LOADED' ) )
  875.                 {
  876.                         define( 'IPSCLASS_DB_LOADED', 1 );
  877.                 }
  878.         }
  879.        
  880.         /*-------------------------------------------------------------------------*/
  881.         // Get browser
  882.         // Return: unknown, windows, mac
  883.         /*-------------------------------------------------------------------------*/
  884.        
  885.         /**
  886.         * Fetches the user's operating system
  887.         *
  888.         * @return       string
  889.         */
  890.        
  891.         function fetch_os()
  892.         {
  893.                 $useragent = strtolower($this->my_getenv('HTTP_USER_AGENT'));
  894.                
  895.                 if ( strstr( $useragent, 'mac' ) )
  896.                 {
  897.                         return 'mac';
  898.                 }
  899.                
  900.                 if ( preg_match( '#wi(n|n32|ndows)#', $useragent ) )
  901.                 {
  902.                         return 'windows';
  903.                 }
  904.                
  905.                 return 'unknown';
  906.         }
  907.        
  908.         /*-------------------------------------------------------------------------*/
  909.         // Get browser
  910.         // Return: unknown, opera, IE, mozilla, konqueror, safari
  911.         /*-------------------------------------------------------------------------*/
  912.        
  913.         /**
  914.         * Fetches the user's browser from their user-agent
  915.         *
  916.         * @return       array [ browser, version ]
  917.         */
  918.        
  919.         function fetch_browser()
  920.         {
  921.                 $version   = 0;
  922.                 $browser   = "unknown";
  923.                 $useragent = strtolower($this->my_getenv('HTTP_USER_AGENT'));
  924.                
  925.                 //-----------------------------------------
  926.                 // Opera...
  927.                 //-----------------------------------------
  928.                
  929.                 if ( strstr( $useragent, 'opera' ) )
  930.                 {
  931.                         preg_match( "#opera[ /]([0-9.]{1,10})#", $useragent, $ver );
  932.                        
  933.                         return array( 'browser' => 'opera', 'version' => $ver[1] );
  934.                 }
  935.                
  936.                 //-----------------------------------------
  937.                 // IE...
  938.                 //-----------------------------------------
  939.                
  940.                 if ( strstr( $useragent, 'msie' ) )
  941.                 {
  942.                         preg_match( "#msie[ /]([0-9.]{1,10})#", $useragent, $ver );
  943.                        
  944.                         return array( 'browser' => 'ie', 'version' => $ver[1] );
  945.                 }
  946.                
  947.                 //-----------------------------------------
  948.                 // Safari...
  949.                 //-----------------------------------------
  950.                
  951.                 if ( strstr( $useragent, 'safari' ) )
  952.                 {
  953.                         preg_match( "#safari/([0-9.]{1,10})#", $useragent, $ver );
  954.                        
  955.                         return array( 'browser' => 'safari', 'version' => $ver[1] );
  956.                 }
  957.                
  958.                 //-----------------------------------------
  959.                 // Mozilla browsers...
  960.                 //-----------------------------------------
  961.                
  962.                 if ( strstr( $useragent, 'gecko' ) )
  963.                 {
  964.                         preg_match( "#gecko/(d+)#", $useragent, $ver );
  965.                        
  966.                         return array( 'browser' => 'gecko', 'version' => $ver[1] );
  967.                 }
  968.                
  969.                 //-----------------------------------------
  970.                 // Older Mozilla browsers...
  971.                 //-----------------------------------------
  972.                
  973.                 if ( strstr( $useragent, 'mozilla' ) )
  974.                 {
  975.                         preg_match( "#^mozilla/[5-9].[0-9.]{1,10}.+rv:([0-9a-z.+]{1,10})#", $useragent, $ver );
  976.                        
  977.                         return array( 'browser' => 'mozilla', 'version' => $ver[1] );
  978.                 }
  979.                
  980.                 //-----------------------------------------
  981.                 // Konqueror...
  982.                 //-----------------------------------------
  983.                
  984.                 if ( strstr( $useragent, 'konqueror' ) )
  985.                 {
  986.                         preg_match( "#konqueror/([0-9.]{1,10})#", $useragent, $ver );
  987.                        
  988.                         return array( 'browser' => 'konqueror', 'version' => $ver[1] );
  989.                 }
  990.                
  991.                 //-----------------------------------------
  992.                 // Still here?
  993.                 //-----------------------------------------
  994.                
  995.                 return array( 'browser' => $browser, 'version' => $version );
  996.         }
  997.        
  998.         /*-------------------------------------------------------------------------*/
  999.         //
  1000.         // LOAD CLASS: Wrapper to load ..er.. classes
  1001.         //
  1002.         /*-------------------------------------------------------------------------*/
  1003.        
  1004.         /**
  1005.         * Wrapper function to load a class and pass $this automagically
  1006.         *
  1007.         * @param        string  File name
  1008.         * @param        string  Class Name
  1009.         * @param        string  Constructor variables
  1010.         * @return       object
  1011.         */
  1012.        
  1013.         function load_class( $file_name, $class_name, $pass_var="" )
  1014.         {
  1015.                 if ( ! $class_name )
  1016.                 {
  1017.                         $class_name = $file_name;
  1018.                 }
  1019.                
  1020.                 require_once( $file_name );
  1021.                
  1022.                 if ( $pass_var )
  1023.                 {
  1024.                         $class = new $class_name ( $pass_var );
  1025.                 }
  1026.                 else
  1027.                 {
  1028.                         $class = new $class_name;
  1029.                 }
  1030.                
  1031.                 $class->ipsclass =& $this;
  1032.                 return $class;
  1033.         }
  1034.        
  1035.         /*-------------------------------------------------------------------------*/
  1036.         // Stronghold: Check cookie
  1037.         /*-------------------------------------------------------------------------*/
  1038.        
  1039.         /**
  1040.         * Checks auto-log in strong hold cookie
  1041.         *
  1042.         * @param        int     Member's ID
  1043.         * @param        string  Member's log in key
  1044.         * @return       boolean
  1045.         */
  1046.        
  1047.         function stronghold_check_cookie( $member_id, $member_log_in_key )
  1048.         {
  1049.                 //-----------------------------------------
  1050.                 // Check...
  1051.                 //-----------------------------------------
  1052.                
  1053.                 if ( ! isset($this->vars['cookie_stronghold']) OR ! $this->vars['cookie_stronghold'] )
  1054.                 {
  1055.                         return TRUE;
  1056.                 }
  1057.                
  1058.                 //-----------------------------------------
  1059.                 // INIT
  1060.                 //-----------------------------------------
  1061.                
  1062.                 $ip_octets  = explode( ".", $this->my_getenv('REMOTE_ADDR') );
  1063.                 $crypt_salt = md5( $this->vars['sql_pass'].$this->vars['sql_user'] );
  1064.                 $cookie     = $this->my_getcookie( 'ipb_stronghold' );
  1065.                
  1066.                 //-----------------------------------------
  1067.                 // Check
  1068.                 //-----------------------------------------
  1069.                
  1070.                 if ( ! $cookie )
  1071.                 {
  1072.                         return FALSE;
  1073.                 }
  1074.                
  1075.                 //-----------------------------------------
  1076.                 // Put it together....
  1077.                 //-----------------------------------------
  1078.                
  1079.                 $stronghold = md5( md5( $member_id . "-" . $ip_octets[0] . '-' . $ip_octets[1] . '-' . $member_log_in_key ) . $crypt_salt );
  1080.                
  1081.                 //-----------------------------------------
  1082.                 // Check against cookie
  1083.                 //-----------------------------------------
  1084.                
  1085.                 return $cookie == $stronghold ? TRUE : FALSE;
  1086.         }
  1087.        
  1088.         /*-------------------------------------------------------------------------*/
  1089.         // Stronghold: Create and set cookie
  1090.         /*-------------------------------------------------------------------------*/
  1091.        
  1092.         /**
  1093.         * Creates an auto-log in strong hold cookie
  1094.         *
  1095.         * @param        int     Member's ID
  1096.         * @param        string  Member's log in key
  1097.         * @return       boolean
  1098.         */
  1099.        
  1100.         function stronghold_set_cookie( $member_id, $member_log_in_key )
  1101.         {
  1102.                 //-----------------------------------------
  1103.                 // Check...
  1104.                 //-----------------------------------------
  1105.                
  1106.                 if ( ! isset($this->vars['cookie_stronghold']) OR ! $this->vars['cookie_stronghold'] )
  1107.                 {
  1108.                         return FALSE;
  1109.                 }
  1110.                
  1111.                 //-----------------------------------------
  1112.                 // INIT
  1113.                 //-----------------------------------------
  1114.                
  1115.                 $ip_octets  = explode( ".", $this->my_getenv('REMOTE_ADDR') );
  1116.                 $crypt_salt = md5( $this->vars['sql_pass'].$this->vars['sql_user'] );
  1117.                
  1118.                 //-----------------------------------------
  1119.                 // Put it together....
  1120.                 //-----------------------------------------
  1121.                
  1122.                 $stronghold = md5( md5( $member_id . "-" . $ip_octets[0] . '-' . $ip_octets[1] . '-' . $member_log_in_key ) . $crypt_salt );
  1123.                
  1124.                 //-----------------------------------------
  1125.                 // Set cookie
  1126.                 //-----------------------------------------
  1127.                
  1128.                 $this->my_setcookie( 'ipb_stronghold', $stronghold, 1 );
  1129.        
  1130.                 return TRUE;
  1131.         }
  1132.        
  1133.         /*-------------------------------------------------------------------------*/
  1134.         //
  1135.         // Check mod queue status
  1136.         //
  1137.         /*-------------------------------------------------------------------------*/
  1138.        
  1139.         /**
  1140.         * Determine if this user / forum combo can manage mod queue
  1141.         *
  1142.         * @param        integer Forum ID
  1143.         * @return       integer Boolean
  1144.         */
  1145.        
  1146.         function can_queue_posts($fid=0)
  1147.         {
  1148.                 $return = 0;
  1149.                
  1150.                 if ( $this->member['g_is_supmod'] )
  1151.                 {
  1152.                         $return = 1;
  1153.                 }
  1154.                 else if ( $fid AND isset($this->member['is_mod']) AND $this->member['is_mod'] AND isset($this->member['_moderator'][ $fid ]['post_q']) AND $this->member['_moderator'][ $fid ]['post_q'] )
  1155.                 {
  1156.                         $return = 1;
  1157.                 }
  1158.                
  1159.                 return $return;
  1160.         }
  1161.        
  1162.         /*-------------------------------------------------------------------------*/
  1163.         //
  1164.         // Check multi mod status
  1165.         //
  1166.         /*-------------------------------------------------------------------------*/
  1167.        
  1168.         /**
  1169.         * Determine if this user / forum combo can manage multi moderation tasks
  1170.         * and return mm_array of allowed tasks
  1171.         *
  1172.         * @param        integer Forum ID
  1173.         * @return       array   Allowed tasks
  1174.         */
  1175.        
  1176.         function get_multimod($fid)
  1177.         {
  1178.                 $mm_array = array();
  1179.                
  1180.                 $pass_go = FALSE;
  1181.                
  1182.                 if ( $this->member['id'] )
  1183.                 {
  1184.                         if ( $this->member['g_is_supmod'] )
  1185.                         {
  1186.                                 $pass_go = TRUE;
  1187.                         }
  1188.                         else if ( isset($this->member['_moderator'][ $fid ]['can_mm']) AND  $this->member['_moderator'][ $fid ]['can_mm'] == 1 )
  1189.                         {
  1190.                                 $pass_go = TRUE;
  1191.                         }
  1192.                 }
  1193.                
  1194.                 if ( $pass_go != TRUE )
  1195.                 {
  1196.                         return $mm_array;
  1197.                 }
  1198.                
  1199.                 if ( ! array_key_exists( 'multimod', $this->cache ) )
  1200.         {
  1201.                 $this->cache['multimod'] = array();
  1202.                
  1203.                         $this->DB->simple_construct( array(
  1204.                                                                                            'select' => '*',
  1205.                                                                                            'from'   => 'topic_mmod',
  1206.                                                                                            'order'  => 'mm_title'
  1207.                                                                            )      );
  1208.                                                                
  1209.                         $this->DB->simple_exec();
  1210.                                                
  1211.                         while ($i = $this->DB->fetch_row())
  1212.                         {
  1213.                                 $this->cache['multimod'][ $i['mm_id'] ] = $i;
  1214.                         }
  1215.                        
  1216.                         $this->update_cache( array( 'name' => 'multimod', 'array' => 1, 'deletefirst' => 1 ) );
  1217.         }
  1218.                
  1219.                 //-----------------------------------------
  1220.                 // Get the topic mod thingies
  1221.                 //-----------------------------------------
  1222.                
  1223.                 if( count( $this->cache['multimod'] ) AND is_array( $this->cache['multimod'] ) )
  1224.                 {
  1225.                         foreach( $this->cache['multimod'] as $r )
  1226.                         {
  1227.                                 if ( $r['mm_forums'] == '*' OR strstr( ",".$r['mm_forums'].",", ",".$fid."," ) )
  1228.                                 {
  1229.                                         $mm_array[] = array( $r['mm_id'], $r['mm_title'] );
  1230.                                 }
  1231.                         }
  1232.                 }
  1233.                
  1234.                 return $mm_array;
  1235.         }
  1236.        
  1237.         /*-------------------------------------------------------------------------*/
  1238.         // UNPACK MEMBER CACHE
  1239.         /*-------------------------------------------------------------------------*/
  1240.         /**
  1241.         * Unpacks a member's cache
  1242.         *
  1243.         * Left as a function for any other processing
  1244.         *
  1245.         * @param        string  Serialized cache array
  1246.         * @return       array   Unpacked array
  1247.         */
  1248.         function unpack_member_cache( $cache_serialized_array="" )
  1249.         {
  1250.                 return unserialize( $cache_serialized_array );
  1251.         }
  1252.        
  1253.         /*-------------------------------------------------------------------------*/
  1254.         // PACK MEMBER CACHE
  1255.         /*-------------------------------------------------------------------------*/
  1256.         /**
  1257.         * Packs up member's cache
  1258.         *
  1259.         * Takes an existing array and updates member's DB row
  1260.         * This will overwrite any existing entries by the same
  1261.         * key and create new entries for non-existing rows
  1262.         *
  1263.         * @param        integer Member ID
  1264.         * @param        array   New array
  1265.         * @param        array   Current Array (optional)
  1266.         * @return       boolean
  1267.         */
  1268.         function pack_and_update_member_cache( $member_id, $new_cache_array, $current_cache_array='' )
  1269.         {
  1270.                 //-----------------------------------------
  1271.                 // INIT
  1272.                 //-----------------------------------------
  1273.                
  1274.                 $member_id = intval( $member_id );
  1275.                
  1276.                 //-----------------------------------------
  1277.                 // Got a member ID?
  1278.                 //-----------------------------------------
  1279.                
  1280.                 if ( ! $member_id )
  1281.                 {
  1282.                         return FALSE;
  1283.                 }
  1284.                
  1285.                 //-----------------------------------------
  1286.                 // Got anything to update?
  1287.                 //-----------------------------------------
  1288.                
  1289.                 if ( ! is_array( $new_cache_array ) OR ! count( $new_cache_array ) )
  1290.                 {
  1291.                         return FALSE;
  1292.                 }
  1293.                
  1294.                 //-----------------------------------------
  1295.                 // Got a current cache?
  1296.                 //-----------------------------------------
  1297.                
  1298.                 if ( ! is_array( $current_cache_array ) )
  1299.                 {
  1300.                         $member = $this->DB->build_and_exec_query( array( 'select' => "members_cache", 'from' => 'members', 'where' => 'id='.$member_id ) );
  1301.                        
  1302.                         $member['members_cache'] = $member['members_cache'] ? $member['members_cache'] : array();
  1303.                        
  1304.                         $current_cache_array = @unserialize( $member['members_cache'] );
  1305.                 }
  1306.                
  1307.                 //-----------------------------------------
  1308.                 // Overwrite...
  1309.                 //-----------------------------------------
  1310.                
  1311.                 foreach( $new_cache_array as $k => $v )
  1312.                 {
  1313.                         $current_cache_array[ $k ] = $v;
  1314.                 }
  1315.                
  1316.                 //-----------------------------------------
  1317.                 // Update...
  1318.                 //-----------------------------------------
  1319.                
  1320.                 $this->DB->do_update( 'members', array( 'members_cache' => serialize( $current_cache_array ) ), 'id='.$member_id );
  1321.                
  1322.                 //-----------------------------------------
  1323.                 // Set member array right...
  1324.                 //-----------------------------------------
  1325.                
  1326.                 $this->member['_cache'] = $current_cache_array;
  1327.         }
  1328.        
  1329.         /*-------------------------------------------------------------------------*/
  1330.         // UPDATE FORUM CACHE
  1331.         /*-------------------------------------------------------------------------*/
  1332.         /**
  1333.         * Updates forum cache (loads all, recaches all)
  1334.         *
  1335.         * @return       void
  1336.         */
  1337.         function update_forum_cache()
  1338.         {
  1339.                 $ignore_me = array( 'redirect_url', 'redirect_loc', 'rules_text', 'permission_custom_error', 'notify_modq_emails' );
  1340.                
  1341.                 if ( isset($this->vars['forum_cache_minimum']) AND $this->vars['forum_cache_minimum'] )
  1342.                 {
  1343.                         $ignore_me[] = 'description';
  1344.                         $ignore_me[] = 'rules_title';
  1345.                 }
  1346.                
  1347.                 $this->cache['forum_cache'] = array();
  1348.                        
  1349.                 $this->DB->simple_construct( array( 'select' => '*',
  1350.                                                                                         'from'   => 'forums',
  1351.                                                                                         'order'  => 'parent_id, position'
  1352.                                                                    )      );
  1353.                 $this->DB->simple_exec();
  1354.                
  1355.                 while( $f = $this->DB->fetch_row() )
  1356.                 {
  1357.                         $fr = array();
  1358.                        
  1359.                         $perms = unserialize(stripslashes($f['permission_array']));
  1360.                        
  1361.                         //-----------------------------------------
  1362.                         // Stuff we don't need...
  1363.                         //-----------------------------------------
  1364.                        
  1365.                         if ( $f['parent_id'] == -1 )
  1366.                         {
  1367.                                 $fr['id']                                   = $f['id'];
  1368.                                 $fr['sub_can_post']         = $f['sub_can_post'];
  1369.                                 $fr['name']                     = $f['name'];
  1370.                                 $fr['parent_id']                = $f['parent_id'];
  1371.                                 $fr['show_perms']               = $perms['show_perms'];
  1372.                                 $fr['skin_id']                  = $f['skin_id'];
  1373.                                 $fr['permission_showtopic'] = $f['permission_showtopic'];
  1374.                         }
  1375.                         else
  1376.                         {
  1377.                                 foreach( $f as $k => $v )
  1378.                                 {
  1379.                                         if ( in_array( $k, $ignore_me ) )
  1380.                                         {
  1381.                                                 continue;
  1382.                                         }
  1383.                                         else
  1384.                                         {
  1385.                                                 if ( $v != "" )
  1386.                                                 {
  1387.                                                         $fr[ $k ] = $v;
  1388.                                                 }
  1389.                                         }
  1390.                                 }
  1391.                                
  1392.                                 $fr['read_perms']       = isset($perms['read_perms'])           ? $perms['read_perms']          : '';
  1393.                                 $fr['reply_perms']      = isset($perms['reply_perms'])          ? $perms['reply_perms']         : '';
  1394.                                 $fr['start_perms']      = isset($perms['start_perms'])          ? $perms['start_perms']         : '';
  1395.                                 $fr['upload_perms']     = isset($perms['upload_perms'])         ? $perms['upload_perms']        : '';
  1396.                                 $fr['download_perms']   = isset($perms['download_perms'])       ? $perms['download_perms']      : '';
  1397.                                 $fr['show_perms']       = isset($perms['show_perms'])           ? $perms['show_perms']          : '';
  1398.                                
  1399.                                 unset($fr['permission_array']);
  1400.                         }
  1401.                        
  1402.                         $this->cache['forum_cache'][ $fr['id'] ] = $fr;
  1403.                 }
  1404.                
  1405.                 $this->update_cache( array( 'name' => 'forum_cache', 'array' => 1, 'deletefirst' => 0, 'donow' => 0 ) );
  1406.         }
  1407.        
  1408.         /*-------------------------------------------------------------------------*/
  1409.         //
  1410.         // UPDATE CACHE
  1411.         //
  1412.         /*-------------------------------------------------------------------------*/
  1413.        
  1414.         /**
  1415.         * Updates cache
  1416.         *
  1417.         * @param        array   Cache values (name, value, deletefirst, donow)
  1418.         * @todo         remove some of the MySQL specific code
  1419.         * @return       void
  1420.         */
  1421.        
  1422.         function update_cache( $v=array() )
  1423.         {
  1424.                 //-----------------------------------------
  1425.                 // Don't cache forums?
  1426.                 //-----------------------------------------
  1427.                
  1428.                 if ( $v['name'] == 'forum_cache' AND isset($this->vars['no_cache_forums']) AND $this->vars['no_cache_forums'] )
  1429.                 {
  1430.                         return;
  1431.                 }
  1432.                
  1433.                 $v['donow'] = isset($v['donow']) ? $v['donow'] : 0;
  1434.                
  1435.                 //-----------------------------------------
  1436.                 // Next...
  1437.                 //-----------------------------------------
  1438.                
  1439.                 if ( $v['name'] )
  1440.                 {
  1441.                         if ( ! isset($v['value']) OR !$v['value'] )
  1442.                         {
  1443.                                 if ( isset($v['array']) AND $v['array'] )
  1444.                                 {
  1445.                                         $value = serialize($this->cache[ $v['name'] ]);
  1446.                                 }
  1447.                                 else
  1448.                                 {
  1449.                                         $value = $this->cache[ $v['name'] ];
  1450.                                 }
  1451.                         }
  1452.                         else
  1453.                         {
  1454.                                 if ( isset($v['array']) AND $v['array'] )
  1455.                                 {                              
  1456.                                         $value = serialize($v['value']);
  1457.                                 }
  1458.                                 else
  1459.                                 {
  1460.                                         $value = $v['value'];
  1461.                                 }
  1462.                         }
  1463.                        
  1464.                         $this->DB->no_escape_fields['cs_key'] = 1;
  1465.                        
  1466.                         if ( $v['deletefirst'] == 1 )
  1467.                         {
  1468.                                 if ( $v['donow'] )
  1469.                                 {
  1470.                                         if ( $this->vars['sql_driver'] == 'mysql' )
  1471.                                         {
  1472.                                                 $this->DB->query( "REPLACE INTO ".SQL_PREFIX."cache_store SET cs_key='{$v['name']}', cs_value='".$this->DB->add_slashes($value)."', cs_array=".intval($v['array']) );
  1473.                                         }
  1474.                                         else
  1475.                                         {
  1476.                                                 $this->DB->simple_construct( array( 'delete' => 'cache_store', 'where' => "cs_key='{$v['name']}'" ) );
  1477.                                                 $this->DB->simple_exec();
  1478.                                        
  1479.                                                 $this->DB->do_insert( 'cache_store', array( 'cs_array' => intval($v['array']), 'cs_key' => $v['name'], 'cs_value' => $value ) );
  1480.                                         }
  1481.                                 }
  1482.                                 else
  1483.                                 {
  1484.                                         if ( $this->vars['sql_driver'] == 'mysql' )
  1485.                                         {
  1486.                                                 $this->DB->query( "REPLACE INTO ".SQL_PREFIX."cache_store SET cs_key='{$v['name']}', cs_value='".$this->DB->add_slashes($value)."', cs_array=".intval($v['array']) );
  1487.                                         }
  1488.                                         else
  1489.                                         {
  1490.                                                 $this->DB->simple_construct( array( 'delete' => 'cache_store', 'where' => "cs_key='{$v['name']}'" ) );
  1491.                                                 $this->DB->simple_shutdown_exec();
  1492.                                        
  1493.                                                 $this->DB->do_shutdown_insert( 'cache_store', array( 'cs_array' => intval($v['array']), 'cs_key' => $v['name'], 'cs_value' => $value ) );
  1494.                                         }
  1495.                                 }
  1496.                         }
  1497.                         else
  1498.                         {
  1499.                                 if ( $v['donow'] )
  1500.                                 {
  1501.                                         $this->DB->do_update( 'cache_store', array( 'cs_array' => intval($v['array']), 'cs_value' => $value ), "cs_key='{$v['name']}'" );
  1502.                                 }
  1503.                                 else
  1504.                                 {
  1505.                                         $this->DB->do_shutdown_update( 'cache_store', array( 'cs_array' => intval($v['array']), 'cs_value' => $value ), "cs_key='{$v['name']}'" );
  1506.                                 }
  1507.                         }
  1508.                        
  1509.                         if( is_object($this->cachelib) )
  1510.                         {
  1511.                                 if( !$value )
  1512.                                 {
  1513.                                         $value = "EMPTY";
  1514.                                 }
  1515.                                
  1516.                                 $this->cachelib->do_remove( $v['name'] );
  1517.                                 $this->cachelib->do_put( $v['name'], $value );
  1518.                         }
  1519.                 }
  1520.         }
  1521.        
  1522.         /*-------------------------------------------------------------------------*/
  1523.         //
  1524.         // MY DECONSTRUCTOR
  1525.         //
  1526.         /*-------------------------------------------------------------------------*/
  1527.        
  1528.         /**
  1529.         * Manual deconstructor
  1530.         *
  1531.         * Runs any SQL shutdown queries and mail tasks
  1532.         *
  1533.         * @return       void
  1534.         */
  1535.        
  1536.         function my_deconstructor()
  1537.         {
  1538.                 //-----------------------------------------
  1539.                 // Update Server Load
  1540.                 //-----------------------------------------
  1541.                                
  1542.         if ($this->vars['load_limit'] > 0)
  1543.         {
  1544.                 $server_load_found = 0;
  1545.                
  1546.                 //-----------------------------------------
  1547.                 // Check cache first...
  1548.                 //-----------------------------------------
  1549.                
  1550.                 if ( $this->cache['systemvars']['loadlimit'] )
  1551.                 {
  1552.                         $loadinfo = explode( "-", $this->cache['systemvars']['loadlimit'] );
  1553.                        
  1554.                         if ( intval($loadinfo[1]) > (time() - 10) )
  1555.                         {
  1556.                                 //-----------------------------------------
  1557.                                 // Cache is less than 1 minute old
  1558.                                 //-----------------------------------------
  1559.                                
  1560.                                 $server_load_found = 1;
  1561.                         }
  1562.                         }
  1563.                        
  1564.                 //-----------------------------------------
  1565.                 // No cache or it's old, check real time
  1566.                 //-----------------------------------------
  1567.                        
  1568.                         if ( ! $server_load_found )
  1569.                         {
  1570.                         # @ supressor fixes warning in >4.3.2 with open_basedir restrictions
  1571.                        
  1572.                         if ( @file_exists('/proc/loadavg') )
  1573.                         {
  1574.                                 if ( $fh = @fopen( '/proc/loadavg', 'r' ) )
  1575.                                 {
  1576.                                         $data = @fread( $fh, 6 );
  1577.                                         @fclose( $fh );
  1578.                                        
  1579.                                         $load_avg = explode( " ", $data );
  1580.                                        
  1581.                                         $this->server_load = trim($load_avg[0]);
  1582.                                 }
  1583.                         }
  1584.                         else if( strstr( strtolower(PHP_OS), 'win' ) )
  1585.                         {
  1586.                                 /*---------------------------------------------------------------
  1587.                                 | typeperf is an exe program that is included with Win NT,
  1588.                                 |       XP Pro, and 2K3 Server.  It can be installed on 2K from the
  1589.                                 |       2K Resource kit.  It will return the real time processor
  1590.                                 |       Percentage, but will take 1 second processing time to do so.
  1591.                                 |       This is why we shall cache it, and check every 10 secs.
  1592.                                 |
  1593.                                 |       Can also be obtained from COM, but it's extremely slow...
  1594.                                 ---------------------------------------------------------------*/
  1595.                                
  1596.                                 $serverstats = @shell_exec("typeperf "Processor(_Total)% Processor Time" -sc 1");
  1597.                                
  1598.                                 if( $serverstats )
  1599.                                 {
  1600.                                                 $server_reply = explode( "n", str_replace( "r", "", $serverstats ) );
  1601.                                                 $serverstats = array_slice( $server_reply, 2, 1 );
  1602.                                                
  1603.                                                 $statline = explode( ",", str_replace( '"', '', $serverstats[0] ) );
  1604.                                                
  1605.                                                 $this->server_load = round( $statline[1], 2 );
  1606.                                         }
  1607.                                 }
  1608.                         else
  1609.                         {
  1610.                                         if ( $serverstats = @exec("uptime") )
  1611.                                         {
  1612.                                                 preg_match( "/(?:averages)?: ([0-9.]+),[s]+([0-9.]+),[s]+([0-9.]+)/", $serverstats, $load );
  1613.                                                
  1614.                                                 $this->server_load = $load[1];
  1615.                                         }
  1616.                                 }
  1617.                                
  1618.                                 if ( $this->server_load )
  1619.                                 {
  1620.                                         $this->cache['systemvars']['loadlimit'] = $this->server_load."-".time();
  1621.                                        
  1622.                                         $this->update_cache( array( 'array' => 1, 'name' => 'systemvars', 'donow' => 1, 'deletefirst' => 0 ) );
  1623.                                 }
  1624.                         }
  1625.                 }
  1626.                
  1627.                 //-----------------------------------------
  1628.                 // Process mail queue
  1629.                 //-----------------------------------------
  1630.                        
  1631.                 $this->process_mail_queue();           
  1632.                
  1633.                 //-----------------------------------------
  1634.                 // Any shutdown queries
  1635.                 //-----------------------------------------
  1636.                
  1637.                 $this->DB->return_die = 0;
  1638.                
  1639.                 if ( count( $this->DB->obj['shutdown_queries'] ) )
  1640.                 {
  1641.                         foreach( $this->DB->obj['shutdown_queries'] as $q )
  1642.                         {
  1643.                                 $this->DB->query( $q );
  1644.                         }
  1645.                 }
  1646.                
  1647.                 $this->DB->return_die = 1;
  1648.                
  1649.                 $this->DB->obj['shutdown_queries'] = array();
  1650.                
  1651.                 $this->DB->close_db();
  1652.                
  1653.                 if( is_object($this->cachelib) )
  1654.                 {
  1655.                         // memcache, primarily, though disconnect will
  1656.                         // happen automatically just like DB anyways
  1657.                        
  1658.                         $this->cachelib->disconnect();
  1659.                 }              
  1660.         }
  1661.        
  1662.         /*-------------------------------------------------------------------------*/
  1663.         //
  1664.         // Process Mail Queue
  1665.         //
  1666.         /*-------------------------------------------------------------------------*/
  1667.        
  1668.         /**
  1669.         * Process mail queue
  1670.         *
  1671.         * @return       void
  1672.         */
  1673.        
  1674.         function process_mail_queue()
  1675.         {
  1676.                 //-----------------------------------------
  1677.                 // SET UP
  1678.                 //-----------------------------------------
  1679.                
  1680.                 $this->vars['mail_queue_per_blob']       = isset($this->vars['mail_queue_per_blob']) ? $this->vars['mail_queue_per_blob'] : 5;
  1681.                
  1682.                 $this->cache['systemvars']['mail_queue'] = isset( $this->cache['systemvars']['mail_queue'] ) ? intval( $this->cache['systemvars']['mail_queue'] ) : 0;
  1683.                
  1684.                 $sent_ids = array();
  1685.                
  1686.                 if ( $this->cache['systemvars']['mail_queue'] > 0 )
  1687.                 {
  1688.                         //-----------------------------------------
  1689.                         // Require the emailer...
  1690.                         //-----------------------------------------
  1691.                        
  1692.                         require_once( ROOT_PATH . 'sources/classes/class_email.php' );
  1693.                         $emailer = new emailer(ROOT_PATH);
  1694.                         $emailer->ipsclass =& $this;
  1695.                         $emailer->email_init();
  1696.                        
  1697.                         //-----------------------------------------
  1698.                         // Get the mail stuck in the queue
  1699.                         //-----------------------------------------
  1700.                        
  1701.                         $this->DB->simple_construct( array( 'select' => '*', 'from' => 'mail_queue', 'order' => 'mail_id', 'limit' => array( 0, $this->vars['mail_queue_per_blob'] ) ) );
  1702.                         $this->DB->simple_exec();
  1703.                        
  1704.                         while ( $r = $this->DB->fetch_row() )
  1705.                         {
  1706.                                 $data[]     = $r;
  1707.                                 $sent_ids[] = $r['mail_id'];
  1708.                         }
  1709.                        
  1710.                         if ( count($sent_ids) )
  1711.                         {
  1712.                                 //-----------------------------------------
  1713.                                 // Delete sent mails and update count
  1714.                                 //-----------------------------------------
  1715.                                
  1716.                                 $this->cache['systemvars']['mail_queue'] = $this->cache['systemvars']['mail_queue'] - count($sent_ids);
  1717.                                
  1718.                                 $this->DB->simple_exec_query( array( 'delete' => 'mail_queue', 'where' => 'mail_id IN ('.implode(",", $sent_ids).')' ) );
  1719.                        
  1720.                                 foreach( $data as $mail )
  1721.                                 {
  1722.                                         if ( $mail['mail_to'] and $mail['mail_subject'] and $mail['mail_content'] )
  1723.                                         {
  1724.                                                 $emailer->to      = $mail['mail_to'];
  1725.                                                 $emailer->from    = $mail['mail_from'] ? $mail['mail_from'] : $this->vars['email_out'];
  1726.                                                 $emailer->subject = $mail['mail_subject'];
  1727.                                                 $emailer->message = $mail['mail_content'];
  1728.                                                
  1729.                                                 $emailer->send_mail();
  1730.                                         }
  1731.                                 }
  1732.                         }
  1733.                         else
  1734.                         {
  1735.                                 //-----------------------------------------
  1736.                                 // No mail after all?
  1737.                                 //-----------------------------------------
  1738.                                
  1739.                                 $this->cache['systemvars']['mail_queue'] = 0;
  1740.                         }
  1741.                        
  1742.                         //-----------------------------------------
  1743.                         // Update cache with remaning email count
  1744.                         //-----------------------------------------
  1745.                        
  1746.                         $this->update_cache( array( 'array' => 1, 'name' => 'systemvars', 'donow' => 1, 'deletefirst' => 0 ) );
  1747.                 }
  1748.         }
  1749.        
  1750.         /*-------------------------------------------------------------------------*/
  1751.         // Load a ACP template file
  1752.         /*-------------------------------------------------------------------------*/
  1753.        
  1754.         /**
  1755.         * Load an ACP skin template file for use
  1756.         *
  1757.         * @return       object  Class object
  1758.         */
  1759.        
  1760.         function acp_load_template( $template )
  1761.         {
  1762.                 if ( ! $this->skin_acp )
  1763.                 {
  1764.                         $this->skin_acp = 'IPB2_Standard';
  1765.                 }
  1766.                
  1767.                 require_once( ROOT_PATH."skin_acp/".$this->skin_acp."/acp_skin_html/".$template.".php" );
  1768.                 $tmp           = new $template();
  1769.                 $tmp->ipsclass =& $this;
  1770.                 return $tmp;
  1771.         }
  1772.        
  1773.         /*-------------------------------------------------------------------------*/
  1774.     // Require, parse and return an array containing the language stuff                
  1775.     /*-------------------------------------------------------------------------*/
  1776.    
  1777.     /**
  1778.         * Load an ACP language file. Populates $this->lang
  1779.         *
  1780.         * @param        string  File name
  1781.         * @return       void
  1782.         * @since        2.1
  1783.         */
  1784.     function acp_load_language( $file="" )
  1785.     {
  1786.         if ( ! $this->lang_id )
  1787.         {
  1788.                 $this->lang_id = $this->member['language'] ? $this->member['language'] : $this->vars['default_language'];
  1789.  
  1790.                         if ( ($this->lang_id != $this->vars['default_language']) and ( ! is_dir( ROOT_PATH."cache/lang_cache/".$this->lang_id ) ) )
  1791.                         {
  1792.                                 $this->lang_id = $this->vars['default_language'];
  1793.                         }
  1794.                        
  1795.                         //-----------------------------------------
  1796.                         // Still nothing?
  1797.                         //-----------------------------------------
  1798.                        
  1799.                         if ( ! $this->lang_id )
  1800.                         {
  1801.                                 $this->lang_id = 'en';
  1802.                         }
  1803.                 }
  1804.        
  1805.         //-----------------------------------------
  1806.         // Load it
  1807.         //-----------------------------------------
  1808.  
  1809.         require_once( ROOT_PATH."cache/lang_cache/".$this->lang_id."/".$file.".php" );
  1810.        
  1811.         if ( is_array( $lang ) )
  1812.         {
  1813.                         foreach ($lang as $k => $v)
  1814.                         {
  1815.                                 $this->acp_lang[ $k ] = $v;
  1816.                         }
  1817.         }
  1818.        
  1819.         unset($lang);
  1820.     }
  1821.        
  1822.         /*-------------------------------------------------------------------------*/
  1823.         //
  1824.         // Load a template file from DB or from PHP file
  1825.         //
  1826.         /*-------------------------------------------------------------------------*/
  1827.        
  1828.         /**
  1829.         * Load a normal template file from either cached PHP file or
  1830.         * from the DB. Populates $this->compiled_templates[ _template_name_ ]
  1831.         *
  1832.         * @param        string  Template name
  1833.         * @param        integer Template set ID
  1834.         * @return       void
  1835.         */
  1836.        
  1837.         function load_template( $name, $id='' )
  1838.         {
  1839.                 $tags = 1;
  1840.                
  1841.                 //-----------------------------------------
  1842.                 // Select ID
  1843.                 //-----------------------------------------
  1844.                
  1845.                 if ( ! $id )
  1846.                 {
  1847.                         $id = $this->skin['_skincacheid'];
  1848.                 }
  1849.        
  1850.                 //-----------------------------------------
  1851.                 // Full name
  1852.                 //-----------------------------------------
  1853.                
  1854.                 $full_name        = $name.'_'.intval($id);
  1855.                 $skin_global_name = 'skin_global_'.$id;
  1856.                 $_name            = $name;
  1857.                
  1858.                 //-----------------------------------------
  1859.                 // Already got this template loaded?
  1860.                 //-----------------------------------------
  1861.                
  1862.                 if ( in_array( $full_name, $this->loaded_templates ) )
  1863.                 {
  1864.                         return;
  1865.                 }
  1866.        
  1867.                 //-----------------------------------------
  1868.                 // Not running safemode skins?
  1869.                 //-----------------------------------------
  1870.                
  1871.                 if ( $this->vars['safe_mode_skins'] == 0 AND $this->vars['safe_mode'] == 0 )
  1872.                 {
  1873.                         //-----------------------------------------
  1874.                         // Simply require and return
  1875.                         //-----------------------------------------
  1876.                        
  1877.                         if ( $name != 'skin_global')
  1878.                         {
  1879.                                 if ( ! in_array( $skin_global_name, $this->loaded_templates ) )
  1880.                                 {
  1881.                                         //-----------------------------------------
  1882.                                         // Suck in skin global..
  1883.                                         //-----------------------------------------
  1884.                                        
  1885.                                         $this->load_template_from_php( 'skin_global', 'skin_global_'.$id, $id );
  1886.                                        
  1887.                                         //-----------------------------------------
  1888.                                         // Suck in normal file...
  1889.                                         //-----------------------------------------
  1890.                                        
  1891.                                         $this->load_template_from_php( $_name, $name.'_'.$id, $id );
  1892.                                 }
  1893.                                 else
  1894.                                 {
  1895.                                         //-----------------------------------------
  1896.                                         // Suck in normal file...
  1897.                                         //-----------------------------------------
  1898.                                        
  1899.                                         $this->load_template_from_php( $_name, $name.'_'.$id, $id );
  1900.                                 }
  1901.                         }
  1902.                         else
  1903.                         {
  1904.                                 if ( $name == 'skin_global' )
  1905.                                 {
  1906.                                         //-----------------------------------------
  1907.                                         // Suck in skin global..
  1908.                                         //-----------------------------------------
  1909.                                        
  1910.                                         $this->load_template_from_php( 'skin_global', 'skin_global_'.$id, $id );
  1911.                                         return;
  1912.                                 }
  1913.                                 else
  1914.                                 {
  1915.                                         //-----------------------------------------
  1916.                                         // Suck in normal file...
  1917.                                         //-----------------------------------------
  1918.                                        
  1919.                                         $this->load_template_from_php( $_name, $name.'_'.$id, $id );
  1920.                                 }
  1921.                         }
  1922.                 }
  1923.                 else
  1924.                 {
  1925.                         //-----------------------------------------
  1926.                         // We're using safe mode skins, yippee
  1927.                         // Load the data from the DB
  1928.                         //-----------------------------------------
  1929.                        
  1930.                         $skin_global = "";
  1931.                         $other_skin  = "";
  1932.                         $this->skin['_type'] = 'Database Skins';
  1933.                                
  1934.                         if ( $this->loaded_templates[ $skin_global_name ] == "" and $name != 'skin_global')
  1935.                         {
  1936.                                 //-----------------------------------------
  1937.                                 // Skin global not loaded...
  1938.                                 //-----------------------------------------
  1939.                                
  1940.                                 $this->DB->simple_construct( array( 'select' => '*',
  1941.                                                                                                         'from'   => 'skin_templates_cache',
  1942.                                                                                                         'where'  => "template_set_id=".$id." AND template_group_name IN ('skin_global', '$name')"
  1943.                                                                                    )      );
  1944.                                                                          
  1945.                                 $this->DB->simple_exec();
  1946.                                
  1947.                                 while ( $r = $this->DB->fetch_row() )
  1948.                                 {
  1949.                                         if ( $r['template_group_name'] == 'skin_global' )
  1950.                                         {
  1951.                                                 $skin_global = $r['template_group_content'];
  1952.                                         }
  1953.                                         else
  1954.                                         {
  1955.                                                 $other_skin  = $r['template_group_content'];
  1956.                                         }
  1957.                                 }
  1958.                                
  1959.                                 if ( IN_DEV AND $id == 1 )
  1960.                                 {
  1961.                                         //-----------------------------------------
  1962.                                         // Get template class
  1963.                                         //-----------------------------------------
  1964.                                
  1965.                                         if ( ! is_object( $this->work_classes['class_template_engine'] ) )
  1966.                                         {
  1967.                                                 require_once( KERNEL_PATH . 'class_template_engine.php' );
  1968.                
  1969.                                                 $this->work_classes['class_template_engine'] = new class_template();
  1970.                                         }
  1971.                
  1972.                                         if( $skin_global )
  1973.                                         {
  1974.                                                 $skin_global = $this->work_classes['class_template_engine']->convert_cache_to_eval( $skin_global, 'skin_global_1' );
  1975.                                         }
  1976.                                        
  1977.                                         if( $other_skin )
  1978.                                         {
  1979.                                                 $other_skin = $this->work_classes['class_template_engine']->convert_cache_to_eval( $other_skin, $name.'_'.$id );
  1980.                                         }
  1981.                                 }
  1982.                                
  1983.                                 //print $other_skin;exit;
  1984.  
  1985.                                 eval($skin_global);
  1986.                                
  1987.                                 $this->compiled_templates['skin_global']           =  new $skin_global_name();
  1988.                                 $this->compiled_templates['skin_global']->ipsclass =& $this;
  1989.                                
  1990.                                 # Add to loaded templates
  1991.                                 $this->loaded_templates[ $skin_global_name ] = $skin_global_name;
  1992.                         }
  1993.                         else
  1994.                         {
  1995.                                 //-----------------------------------------
  1996.                                 // Skin global is loaded..
  1997.                                 //-----------------------------------------
  1998.                                
  1999.                                 if ( $name == 'skin_global' and in_array( $skin_global_name, $this->loaded_templates ) )
  2000.                                 {
  2001.                                         return;
  2002.                                 }
  2003.                                
  2004.                                 //-----------------------------------------
  2005.                                 // Load the skin, man
  2006.                                 //-----------------------------------------
  2007.                                
  2008.                                 $this->DB->simple_construct( array( 'select' => '*',
  2009.                                                                                                         'from'   => 'skin_templates_cache',
  2010.                                                                                                         'where'  => "template_set_id=".$id." AND template_group_name='$name'"
  2011.                                                                                    )      );
  2012.                                                                          
  2013.                                 $this->DB->simple_exec();
  2014.                                
  2015.                                 $r = $this->DB->fetch_row();
  2016.                                
  2017.                                 $other_skin  = $r['template_group_content'];
  2018.                                
  2019.                                 if ( IN_DEV AND $id == 1 )
  2020.                                 {
  2021.                                         //-----------------------------------------
  2022.                                         // Get template class
  2023.                                         //-----------------------------------------
  2024.                                
  2025.                                         if ( ! is_object( $this->work_classes['class_template_engine'] ) )
  2026.                                         {
  2027.                                                 require_once( KERNEL_PATH . 'class_template_engine.php' );
  2028.                
  2029.                                                 $this->work_classes['class_template_engine'] = new class_template();
  2030.                                         }
  2031.                
  2032.                                         if( $other_skin )
  2033.                                         {
  2034.                                                 $other_skin = $this->work_classes['class_template_engine']->convert_cache_to_eval( $other_skin, $name.'_'.$id );
  2035.                                         }
  2036.                                 }                              
  2037.                         }
  2038.                        
  2039.                         eval($other_skin);
  2040.                        
  2041.                         if ( $name == 'skin_global' )
  2042.                         {
  2043.                                 $this->compiled_templates['skin_global']           =  new $skin_global_name();
  2044.                                 $this->compiled_templates['skin_global']->ipsclass =& $this;
  2045.                                
  2046.                                 # Add to loaded templates
  2047.                                 $this->loaded_templates[ $skin_global_name ] = $skin_global_name;
  2048.                         }
  2049.                         else
  2050.                         {
  2051.                                 $this->compiled_templates[ $name ]           =  new $full_name();
  2052.                                 $this->compiled_templates[ $name ]->ipsclass =& $this;
  2053.                                
  2054.                                 # Add to loaded templates
  2055.                                 $this->loaded_templates[ $full_name ] = $full_name;
  2056.                         }
  2057.                 }
  2058.                
  2059.                 //-----------------------------------------
  2060.                 // LEGACY
  2061.                 //-----------------------------------------
  2062.                
  2063.                 if ( defined('LEGACY_MODE') && LEGACY_MODE == 1 )
  2064.                 {
  2065.                         if ( $name )
  2066.                         {
  2067.                                 return $this->compiled_templates[ $name ];
  2068.                         }
  2069.                 }
  2070.         }
  2071.        
  2072.         /*-------------------------------------------------------------------------*/
  2073.     // Load the template bit from the PHP file              
  2074.     /*-------------------------------------------------------------------------*/
  2075.    
  2076.     /**
  2077.         * Load the template bit from the PHP file      
  2078.         *
  2079.         * @var          string  Name of the PHP file (sans .php)
  2080.         * @var          string  Name of the class
  2081.         * @return       boolean
  2082.         */
  2083.        
  2084.         function load_template_from_php( $name='skin_global', $full_name='skin_global_1', $id='1' )
  2085.         {      
  2086.                 //-----------------------------------------
  2087.                 // IN_DEV?
  2088.                 //-----------------------------------------
  2089.                
  2090.                 if ( IN_DEV AND $id == 1 )
  2091.                 {
  2092.                         //-----------------------------------------
  2093.                         // Get data...
  2094.                         //-----------------------------------------
  2095.                
  2096.                         $data = implode( '', file( CACHE_PATH."cache/skin_cache/cacheid_".$id."/".$name.".php" ) );
  2097.                
  2098.                         //-----------------------------------------
  2099.                         // Get template class
  2100.                         //-----------------------------------------
  2101.                
  2102.                         if ( ! is_object( $this->work_classes['class_template_engine'] ) )
  2103.                         {
  2104.                                 require_once( KERNEL_PATH . 'class_template_engine.php' );
  2105.  
  2106.                                 $this->work_classes['class_template_engine'] = new class_template();
  2107.                         }
  2108.                        
  2109.                         $toeval = $this->work_classes['class_template_engine']->convert_cache_to_eval( $data, $full_name );
  2110.                        
  2111.                         #if ( $name == 'skin_profile' ) { print $toeval; }
  2112.                         eval( $toeval );
  2113.                 }
  2114.                 else
  2115.                 {
  2116.                         require_once( CACHE_PATH."cache/skin_cache/cacheid_".$id."/".$name.".php" );
  2117.                 }
  2118.  
  2119.                 $this->compiled_templates[ $name ]           =  new $full_name();
  2120.                 $this->compiled_templates[ $name ]->ipsclass =& $this;
  2121.                
  2122.                 # Add to loaded templates
  2123.                 $this->loaded_templates[ $full_name ] = $full_name;
  2124.                
  2125.                 return TRUE;
  2126.         }
  2127.        
  2128.         /*-------------------------------------------------------------------------*/
  2129.     // SKIN, sort out the skin stuff                
  2130.     /*-------------------------------------------------------------------------*/
  2131.    
  2132.     /**
  2133.         * Load a skin, macro, settings, etc
  2134.         *
  2135.         * @return       array   Database row (not really used anymore)
  2136.         */
  2137.        
  2138.     function load_skin()
  2139.     {
  2140.         //-----------------------------------------
  2141.         // INIT
  2142.         //-----------------------------------------
  2143.        
  2144.         $id                    = -1;
  2145.         $skin_set              = 0;
  2146.         $from_forum            = 0;
  2147.         $this->input['skinid'] = isset($this->input['skinid']) ? intval($this->input['skinid']) : 0;
  2148.         $this->member['skin']  = isset($this->member['skin'])  ? intval($this->member['skin'])  : 0;
  2149.        
  2150.         //-----------------------------------------
  2151.         // Do we have a cache?
  2152.         //-----------------------------------------
  2153.        
  2154.         if ( ! is_array( $this->cache['skin_id_cache'] ) OR !count( $this->cache['skin_id_cache'] ) )
  2155.         {
  2156.                 require_once( ROOT_PATH.'sources/lib/admin_cache_functions.php' );
  2157.                 $admin           = new admin_cache_functions();
  2158.                 $admin->ipsclass =& $this;
  2159.                
  2160.                 $this->cache['skin_id_cache'] = $admin->_rebuild_skin_id_cache();
  2161.         }
  2162.        
  2163.         //-----------------------------------------
  2164.         // Search bot?
  2165.         //-----------------------------------------
  2166.        
  2167.         if ( ( $this->is_bot == 1 ) and ( $this->vars['spider_suit'] != "" ) )
  2168.         {
  2169.                 $skin_set = 1;
  2170.                 $id       = $this->vars['spider_suit'];
  2171.         }
  2172.         else
  2173.         {
  2174.                         //-----------------------------------------
  2175.                         // Do we have a skin for a particular forum?
  2176.                         //-----------------------------------------
  2177.                        
  2178.                         if ( (isset($this->input['f']) AND $this->input['f']) AND (isset($this->input['act']) AND $this->input['act'] != 'UserCP') )
  2179.                         {
  2180.                                 if ( isset($this->cache['forum_cache'][ $this->input['f'] ]['skin_id']) AND $this->cache['forum_cache'][ $this->input['f'] ]['skin_id'] > 0 )
  2181.                                 {
  2182.                                         $id         = $this->cache['forum_cache'][ $this->input['f'] ]['skin_id'];
  2183.                                         $skin_set   = 1;
  2184.                                         $from_forum = 1;
  2185.                                 }
  2186.                         }
  2187.                        
  2188.                         //-----------------------------------------
  2189.                         // Are we allowing user chooseable skins?
  2190.                         //-----------------------------------------
  2191.                        
  2192.                         if ($skin_set != 1 and $this->vars['allow_skins'] == 1)
  2193.                         {
  2194.                                 if ( $this->input['skinid'] )
  2195.                                 {
  2196.                                         $id        = $this->input['skinid'];
  2197.                                         $skin_set  = 1;
  2198.                                 }
  2199.                                 else if ( $this->member['skin'] )
  2200.                                 {
  2201.                                         $id       = $this->member['skin'];
  2202.                                         $skin_set = 1;
  2203.                                 }
  2204.                         }
  2205.         }
  2206.        
  2207.         //-----------------------------------------
  2208.                 // Nothing set / hidden and not admin? Choose the default
  2209.                 //-----------------------------------------
  2210.                
  2211.                 if ( isset($this->cache['skin_id_cache'][ $id ]['set_hidden']) AND $this->cache['skin_id_cache'][ $id ]['set_hidden'] )
  2212.                 {
  2213.                         if ( $from_forum )
  2214.                         {
  2215.                                 $skin_set = 1;
  2216.                         }
  2217.                         else if ( $this->member['g_access_cp'] )
  2218.                         {
  2219.                                 $skin_set = 1;
  2220.                         }
  2221.                         else if ( $this->is_bot )
  2222.                         {
  2223.                                 $skin_set = 1;
  2224.                         }
  2225.                         else
  2226.                         {
  2227.                                 $skin_set = 0;
  2228.                         }
  2229.                 }
  2230.                        
  2231.                 if ( ! $id OR ! $skin_set OR ! is_array($this->cache['skin_id_cache'][ $id ]) )
  2232.                 {
  2233.                         foreach( $this->cache['skin_id_cache'] as $data )
  2234.                         {
  2235.                                 if ( $data['set_default'] )
  2236.                                 {
  2237.                                         $id       = $data['set_skin_set_id'];
  2238.                                         $skin_set = 1;
  2239.                                 }
  2240.                         }
  2241.                 }
  2242.                
  2243.         //------------------------------------------
  2244.         // Still no skin? - no default skin set
  2245.         //------------------------------------------
  2246.        
  2247.         if( ! $id OR ! $skin_set )
  2248.         {
  2249.             $skin_data = reset($this->cache['skin_id_cache']);
  2250.             $id = $skin_data['skin_set_id'];
  2251.             $skin_set = 1;
  2252.         }              
  2253.                
  2254.                 //-----------------------------------------
  2255.                 // Get the skin
  2256.                 //-----------------------------------------
  2257.        
  2258.                 $db_skin = array();
  2259.                
  2260.                 if( is_object($this->cachelib) )
  2261.                 {
  2262.                         $db_skin = $this->cachelib->do_get( 'Skin_Store_' . $id );
  2263.                 }
  2264.                
  2265.                 //print_r($db_skin);
  2266.                
  2267.                 if( !is_array($db_skin) OR !count($db_skin) )
  2268.                 {
  2269.                         $db_skin = $this->DB->simple_exec_query( array( 'select' => 'set_cache_css,set_cache_wrapper,set_cache_macro,set_image_dir,set_emoticon_folder,set_skin_set_id,set_name,set_css_method', 'from' => 'skin_sets', 'where' => 'set_skin_set_id='.$id ) );
  2270.                
  2271.                         if( is_object($this->cachelib) )
  2272.                         {
  2273.                                 $this->cachelib->do_put( 'Skin_Store_' . $id, $db_skin, 86400 );
  2274.                         }
  2275.                 }              
  2276.                
  2277.                 $this->skin['_css']         = $db_skin['set_cache_css'];
  2278.                 $this->skin['_wrapper']     = $db_skin['set_cache_wrapper'];
  2279.                 $this->skin['_macro']       = $db_skin['set_cache_macro'];
  2280.                 $this->skin['_imagedir']    = $db_skin['set_image_dir'];
  2281.                 $this->skin['_emodir']      = $db_skin['set_emoticon_folder'];
  2282.                 $this->skin['_setid']       = $db_skin['set_skin_set_id'];
  2283.         $this->skin['_setname']     = $db_skin['set_name'];
  2284.         $this->skin['_usecsscache'] = $db_skin['set_css_method'] ? 1 : 0;
  2285.         $this->skin['_macros']      = unserialize(stripslashes($this->skin['_macro']));
  2286.         $this->skin['_skincacheid'] = $db_skin['set_skin_set_id'];
  2287.                 $this->skin['_csscacheid']  = $db_skin['set_skin_set_id'];
  2288.                        
  2289.         if ( IN_DEV )
  2290.         {
  2291.                 $this->skin['_skincacheid'] = file_exists( CACHE_PATH.'cache/skin_cache/cacheid_1' ) ? 1 : $db_skin['set_skin_set_id'];
  2292.                         $this->skin['_usecsscache'] = file_exists( CACHE_PATH.'style_images/css_'. $this->skin['_csscacheid'] .'.css' ) ? 1 : 0;
  2293.                         $this->s