$Date: 2007-06-19 11:26:29 -0400 (Tue, 19 Jun 2007) $ | > $Revision: 1046 $ | > $Author: matt $ +--------------------------------------------------------------------------- | | > Admin Cache functions library | > Script written by Matt Mecham | > Date started: 20th January 2004 | | > DBA Checked: Tue 25th May 2004 +-------------------------------------------------------------------------- */ if ( ! defined( 'IN_ACP' ) ) { print "

Incorrect access

You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'."; exit(); } class admin_cache_functions { var $master_set = 1; var $messages = array(); var $no_rebuild = 0; var $template = ""; //----------------------------------------- // Constructor //----------------------------------------- function admin_cache_functions() { //----------------------------------------- // Get the libraries //----------------------------------------- require_once( KERNEL_PATH.'class_template_engine.php' ); $this->template = new class_template(); $this->template->root_path = CACHE_PATH; $this->template->allow_php_code = intval( IPB_ACP_ALLOW_TEMPLATE_PHP ); } //============================================================================================================ // WRAPPER FUNCTIONS //============================================================================================================ //----------------------------------------- // WRAPPER: Update internal cache //----------------------------------------- function _recache_wrapper($id, $parent="", $root='1') { //----------------------------------------- // Get wrapper from tree //----------------------------------------- $parent_in = $parent ? ','.$parent : ''; $this->ipsclass->DB->cache_add_query( 'cache_wrapper', array( 'id' => $id, 'parent_in' => $parent_in, 'root' => $root ) ); $this->ipsclass->DB->cache_exec_query(); $this_set = $this->ipsclass->DB->fetch_row(); $wrapper = $this_set['set_wrapper']; //----------------------------------------- // Get children details //----------------------------------------- $ids = array( 0 => array( 'set_skin_set_id' => $id ) ); if ( $id == 1 and IN_DEV ) { $id = -1; } $this->ipsclass->DB->cache_add_query( 'cache_empty_wrapper', array( 'parent_id' => $id ) ); $this->ipsclass->DB->cache_exec_query(); while ( $r = $this->ipsclass->DB->fetch_row() ) { $ids[] = $r; } if ( $id == -1 and IN_DEV ) { $id = 1; } //----------------------------------------- // Update this and child db // caches //----------------------------------------- $theids = array(); foreach( $ids as $d ) { $theids[] = $d['set_skin_set_id']; } $this->ipsclass->DB->do_update( 'skin_sets', array( 'set_cache_wrapper' => $wrapper ), 'set_skin_set_id IN ('.implode(',',$theids).') AND set_skin_set_id != 1' ); if( is_object($this->ipsclass->cachelib) AND !$this->no_rebuild ) { if( count( $theids ) ) { foreach( $theids as $id ) { if( $id == 1 ) { continue; } $this->ipsclass->cachelib->do_remove( 'Skin_Store_' . $id ); } } } $this->messages[] = "Done wrapper rebuild... (id: ".implode(',',$theids).")"; } //============================================================================================================ // TEMPLATE FUNCTIONS //============================================================================================================ //----------------------------------------- // TEMPLATE: Recache and splash //----------------------------------------- function _recache_templates( $id, $parent="", $group_only="", $root=1, $master_only=0 ) { //----------------------------------------- // INIT //----------------------------------------- $ids = array( 0 => array( 'id' => $id, 'parent' => $parent ) ); //----------------------------------------- // Make sure master templates are written //----------------------------------------- if ( $id == 1 ) { $master_only = 1; } //----------------------------------------- // Just doing root? //----------------------------------------- if ( $master_only ) { $ids = array( 0 => array( 'id' => 1, 'parent' => -1 ) ); } else { //----------------------------------------- // Get any children //----------------------------------------- if ( $id == 1 and IN_DEV ) { $id = -1; } $this->ipsclass->DB->simple_construct( array( 'select' => 'set_skin_set_id, set_skin_set_parent', 'from' => 'skin_sets', 'where' => "set_skin_set_parent=$id" ) ); $this->ipsclass->DB->simple_exec(); while ( $r = $this->ipsclass->DB->fetch_row() ) { $ids[] = array( 'id' => $r['set_skin_set_id'], 'parent' => $r['set_skin_set_parent'] ); } if ( $id == -1 and IN_DEV ) { $id = 1; } } //----------------------------------------- // SPIN TO WIN! //----------------------------------------- foreach ( $ids as $i => $tid ) { $id = $tid['id']; $parent = $tid['parent']; //----------------------------------------- // Get template set titles //----------------------------------------- $group_titles = $this->_get_templates( $id, $parent, 'groups' ); foreach ( $group_titles as $name => $group ) { //----------------------------------------- // Skip if we're only updating // one group.. //----------------------------------------- if ( $group_only != '' ) { if ( $group_only != $group['group_name'] ) { continue; } } $this_set_list = array(); $out = "class {$group['group_name']}_{$id} {\n\n"; $out .= ' var $ipsclass;'."\n"; $templates = $this->_get_templates( $id, $parent, 'groups', $group['group_name'] ); foreach ($templates as $func_name => $data ) { $out .= $this->template->convert_html_to_php( $data['func_name'], $data['func_data'], $data['section_content'], "", "", $master_only ? 0 : 1 ) ."\n"; if ( $data['set_id'] == $id ) { $this_set_list[] = $data['func_name']; } } $out .= "\n\n}"; //----------------------------------------- // Write to the DB... //----------------------------------------- $this->ipsclass->DB->simple_exec_query( array( 'delete' => 'skin_templates_cache', 'where' => "template_group_name='{$group['group_name']}' and template_set_id={$id}" ) ); $this->ipsclass->DB->do_insert( 'skin_templates_cache', array( 'template_id' => md5(uniqid(rand(), true)), 'template_group_name' => $group['group_name'], 'template_group_content' => $out, 'template_set_id' => $id, ) ); //----------------------------------------- // Write to the flatfile //----------------------------------------- $start = '<'.'?'."php\n"; $start .= "/*--------------------------------------------------*/\n"; $start .= "/* FILE GENERATED BY INVISION POWER BOARD */\n"; $start .= "/* CACHE FILE: Skin set id: {$id} */\n"; $start .= "/* CACHE FILE: Generated: ".gmdate( "D, d M Y H:i:s \G\M\T" )." */\n"; $start .= "/* DO NOT EDIT DIRECTLY - THE CHANGES WILL NOT BE */\n"; $start .= "/* WRITTEN TO THE DATABASE AUTOMATICALLY */\n"; $start .= "/*--------------------------------------------------*/\n\n"; $end = "\n\n/*--------------------------------------------------*/\n"; $end .= "/*\n".implode(',',$this_set_list)."\n*/\n"; $end .= "/* END OF FILE */\n"; $end .= "/*--------------------------------------------------*/\n"; $end .= "\n?".">"; $this->_write_template_to_file( $id, $group['group_name'], $start.$out.$end ); } $this->messages[] = "Done HTML templates rebuild... (id: $id)"; } } //----------------------------------------- // TEMPLATE: write flat-file //----------------------------------------- function _write_template_to_file($id, $group_name, $content) { //----------------------------------------- // Check.. //----------------------------------------- if ( $id == 1 AND ! IN_DEV) { return 0; } $return = 0; $good_to_go = 0; if ( ! SAFE_MODE_ON ) { $good_to_go = 1; if ( is_writeable( CACHE_PATH.'cache/skin_cache' ) ) { $good_to_go = 1; if ( ! is_dir( CACHE_PATH.'cache/skin_cache/cacheid_'.$id ) ) { if ( ! @ mkdir( CACHE_PATH.'cache/skin_cache/cacheid_'.$id, 0777 ) ) { $good_to_go = 0; } else { @chmod( CACHE_PATH.'cache/skin_cache/cacheid_'.$id, 0777 ); $good_to_go = 1; } } else { if ( file_exists( CACHE_PATH.'cache/skin_cache/cacheid_'.$id.'/'.$group_name.'.php' ) ) { if ( ! is_writeable( CACHE_PATH.'cache/skin_cache/cacheid_'.$id.'/'.$group_name.'.php' ) ) { $this->messages[] = "cache/skin_cache/cacheid_{$id}/{$group_name}.php not writeable - cannot cache to PHP file"; $good_to_go = 0; } else { $good_to_go = 1; } } else { $good_to_go = 1; } } } else { $this->messages[] = "cache/skin_cache/cacheid_{$id} not writeable - cannot cache to PHP file"; } } //----------------------------------------- // Write... //----------------------------------------- if ( $good_to_go ) { if ( $FH = @fopen( CACHE_PATH.'cache/skin_cache/cacheid_'.$id.'/'.$group_name.'.php', 'w' ) ) { fwrite( $FH, $content, strlen($content) ); fclose( $FH ); @chmod( CACHE_PATH.'cache/skin_cache/cacheid_'.$id.'/'.$group_name.'.php', 0777 ); $return = 1; $this->messages[] = "Wrote skin_cache/cacheid_{$id}/{$group_name}.php"; } } return $return; } //----------------------------------------- // TEMPLATE: Return templates from tree //----------------------------------------- function _get_templates($id, $parent, $type='all', $group='', $root=1) { $templates = array(); $parent_in = $parent > 0 ? ','.$parent : ''; $return = 'all'; if ( $type == 'groups' and $group == '' ) { //----------------------------------------- // Just return group titles //----------------------------------------- $this->ipsclass->DB->cache_add_query( 'cache_templates_titles', array( 'id' => $id, 'parent_in' => $parent_in, 'root' => $root ) ); $newq = $this->ipsclass->DB->cache_exec_query(); $return = 'titles'; } else if ( $type == 'groups' and $group != '' ) { //----------------------------------------- // Return group template bits //----------------------------------------- $this->ipsclass->DB->cache_add_query( 'cache_templates_bits', array( 'id' => $id, 'parent_in' => $parent_in, 'root' => $root, 'group' => $group ) ); $newq = $this->ipsclass->DB->cache_exec_query(); $return = 'group'; } else { //----------------------------------------- // Return all... //----------------------------------------- $this->ipsclass->DB->cache_add_query( 'cache_templates_all', array( 'id' => $id, 'parent_in' => $parent_in, 'root' => $root ) ); $newq = $this->ipsclass->DB->cache_exec_query(); } //----------------------------------------- // Get all results //----------------------------------------- while ( $r = $this->ipsclass->DB->fetch_row($newq) ) { if ( $return == 'titles' ) { $templates[ $r['group_name'] ] = $r; $this->template_count[ $r['set_id'] ] = isset($this->template_count[ $r['set_id'] ]) ? $this->template_count[ $r['set_id'] ] : array(); $this->template_count[ $r['set_id'] ][ $r['group_name'] ] = isset($this->template_count[ $r['set_id'] ][ $r['group_name'] ]) ? $this->template_count[ $r['set_id'] ][ $r['group_name'] ] : array( 'count' => 0 ); $this->template_count[ $r['set_id'] ][ $r['group_name'] ]['count']++; } else if ( $return == 'group' ) { $templates[ strtolower($r['func_name']) ] = $r; } else { $templates[ $r['group_name'] ][ strtolower($r['func_name']) ] = $r; } } ksort($templates); return $templates; } //----------------------------------------- // TEMPLATE: Rebuild templates from PHP files //----------------------------------------- function _rebuild_templates_from_php($id) { $insert = 1; $script_token = 0; $script_jump = 0; //----------------------------------------- // Rebuilds the data editable files from the PHP source files //----------------------------------------- $this->template->cache_id = $id; $skin_dir = CACHE_PATH."cache/skin_cache/cacheid_".$id; $errors = array(); $flag = 0; //----------------------------------------- // Is this a safe mode only skinny poos? //----------------------------------------- if ( ! file_exists( $skin_dir ) ) { $this->messages[] = "This template set is a safe mode only skin and no PHP skin files exist, there is no need to run this tool on this template set."; return 0; } if ( ! is_readable($skin_dir) ) { $this->messages[] = "FAILD: Cannot write into '$skin_dir', please check the CHMOD value, and if needed, CHMOD to 0777 via FTP. IBF cannot do this for you."; return 0; } if ( is_dir($skin_dir) ) { if ( $handle = opendir($skin_dir) ) { while (($filename = readdir($handle)) !== false) { if (($filename != ".") && ($filename != "..")) { if ( preg_match( "/\.php$/", $filename ) ) { $name = preg_replace( "/^(\S+)\.(\S+)$/", "\\1", $filename ); # Ignore bugtracker if ( $name == 'skin_bugtracker' ) { $this->messages[] = "Skipping skin_bugtracker.php..."; continue; } if ($FH = fopen($skin_dir."/".$filename, 'r') ) { $fdata = fread( $FH, filesize($skin_dir."/".$filename) ); fclose($FH); } else { $this->messages[] = "Could not open $filename for reading, skipping file..."; continue; } $fdata = str_replace( "\r", "\n", $fdata ); $fdata = str_replace( "\n\n", "\n", $fdata ); if ( ! preg_match( "/\n/", $fdata ) ) { $this->messages[] = "Could not find any line endings in $filename, skipping file..."; continue; } $farray = explode( "\n", $fdata ); //----------------------------------------- $functions = array(); foreach($farray as $f) { //----------------------------------------- // Skip javascript functions... //----------------------------------------- #print htmlspecialchars( $f )."
"; if ( preg_match( "/