Admin Cache functions library
| > Script written by Matt Mecham
| > Date started: 20th January 2004
|
| > DBA Checked: Tue 25th May 2004
+--------------------------------------------------------------------------
*/
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.php' );
$this->template = new class_template();
$this->template->root_path = CACHE_PATH;
}
//============================================================================================================
// WRAPPER FUNCTIONS
//============================================================================================================
//-----------------------------------------
// WRAPPER: Update internal cache
//-----------------------------------------
function _recache_wrapper($id, $parent="", $root='1')
{
global $ibforums, $std, $DB;
//-----------------------------------------
// Get wrapper from tree
//-----------------------------------------
$parent_in = $parent ? ','.$parent : '';
$DB->cache_add_query( 'cache_wrapper', array( 'id' => $id, 'parent_in' => $parent_in, 'root' => $root ) );
$DB->cache_exec_query();
$this_set = $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;
}
$DB->cache_add_query( 'cache_empty_wrapper', array( 'parent_id' => $id ) );
$DB->cache_exec_query();
while ( $r = $DB->fetch_row() )
{
$ids[] = $r;
}
if ( $id == -1 and IN_DEV )
{
$id = 1;
}
//-----------------------------------------
// Update this and child db
// caches
//-----------------------------------------
$theids = array();
foreach( $ids as $i => $d )
{
$theids[] = $d['set_skin_set_id'];
}
$DB->do_update( 'skin_sets', array( 'set_cache_wrapper' => $wrapper ), 'set_skin_set_id IN ('.implode(',',$theids).') AND set_skin_set_id != 1' );
$this->messages[] = "Done wrapper rebuild... (id: ".implode(',',$theids).")";
}
//============================================================================================================
// TEMPLATE FUNCTIONS
//============================================================================================================
//-----------------------------------------
// TEMPLATE: Recache and splash
//-----------------------------------------
function _recache_templates( $id, $parent="", $group_only="", $root=1 )
{
global $std, $DB, $ibforums;
$ids = array( 0 => array( 'id' => $id, 'parent' => $parent ) );
//-----------------------------------------
// Get any children
//-----------------------------------------
if ( $id == 1 and IN_DEV )
{
$id = -1;
}
$DB->simple_construct( array( 'select' => 'set_skin_set_id, set_skin_set_parent', 'from' => 'skin_sets', 'where' => "set_skin_set_parent=$id" ) );
$DB->simple_exec();
while ( $r = $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']} {\n\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'] ) ."\n";
if ( $data['set_id'] == $id )
{
$this_set_list[] = $data['func_name'];
}
}
$out .= "\n\n}";
//-----------------------------------------
// Write to the DB...
//-----------------------------------------
$DB->simple_exec_query( array( 'delete' => 'skin_templates_cache', 'where' => "template_group_name='{$group['group_name']}' and template_set_id={$id}" ) );
$DB->do_insert( 'skin_templates_cache', array(
'template_id' => md5(microtime()),
'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)
{
global $DB, $ibforums;
//-----------------------------------------
// 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.'skin_cache' ) )
{
$good_to_go = 1;
if ( ! is_dir( CACHE_PATH.'skin_cache/cacheid_'.$id ) )
{
if ( ! @ mkdir( CACHE_PATH.'skin_cache/cacheid_'.$id, 0777 ) )
{
$good_to_go = 0;
}
else
{
@chmod( CACHE_PATH.'skin_cache/cacheid_'.$id, 0777 );
$good_to_go = 1;
}
}
else
{
if ( file_exists( CACHE_PATH.'skin_cache/cacheid_'.$id.'/'.$group_name.'.php' ) )
{
if ( ! is_writeable( CACHE_PATH.'skin_cache/cacheid_'.$id.'/'.$group_name.'.php' ) )
{
$this->messages[] = "::cache_id_{$id}/{$group_name}.php not writeable - cannot cache to PHP files";
$good_to_go = 0;
}
else
{
$good_to_go = 1;
}
}
else
{
$good_to_go = 1;
}
}
}
else
{
$this->messages[] = "::cache_id_{$id} not writeable - cannot cache to PHP files";
}
}
//-----------------------------------------
// Write...
//-----------------------------------------
if ( $good_to_go )
{
if ( $FH = @fopen( CACHE_PATH.'skin_cache/cacheid_'.$id.'/'.$group_name.'.php', 'w' ) )
{
fwrite( $FH, $content, strlen($content) );
fclose( $FH );
@chmod( CACHE_PATH.'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)
{
global $ibforums, $DB;
$templates = array();
$parent_in = $parent > 0 ? ','.$parent : '';
$return = 'all';
if ( $type == 'groups' and $group == '' )
{
//-----------------------------------------
// Just return group titles
//-----------------------------------------
$DB->cache_add_query( 'cache_templates_titles', array( 'id' => $id, 'parent_in' => $parent_in, 'root' => $root ) );
$newq = $DB->cache_exec_query();
$return = 'titles';
}
else if ( $type == 'groups' and $group != '' )
{
//-----------------------------------------
// Return group template bits
//-----------------------------------------
$DB->cache_add_query( 'cache_templates_bits', array( 'id' => $id, 'parent_in' => $parent_in, 'root' => $root, 'group' => $group ) );
$newq = $DB->cache_exec_query();
$return = 'group';
}
else
{
//-----------------------------------------
// Return all...
//-----------------------------------------
$DB->cache_add_query( 'cache_templates_all', array( 'id' => $id, 'parent_in' => $parent_in, 'root' => $root ) );
$newq = $DB->cache_exec_query();
}
//-----------------------------------------
// Get all results
//-----------------------------------------
while ( $r = $DB->fetch_row($newq) )
{
if ( $return == 'titles' )
{
$templates[ $r['group_name'] ] = $r;
$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)
{
global $ibforums, $DB, $std;
$insert = 1;
//-----------------------------------------
// Rebuilds the data editable files from the PHP source files
//-----------------------------------------
$this->template->cache_id = $id;
$skin_dir = CACHE_PATH."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 );
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...
if ( preg_match( "/