$Date: 2005-10-14 18:52:45 +0100 (Fri, 14 Oct 2005) $
| > $Revision: 50 $
| > $Author: matt $
+---------------------------------------------------------------------------
|
| > 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')
{
//-----------------------------------------
// 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 $i => $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' );
$this->messages[] = "Обновление оформления выполнено... (id: ".implode(',',$theids).")";
}
//============================================================================================================
// TEMPLATE FUNCTIONS
//============================================================================================================
//-----------------------------------------
// TEMPLATE: Recache and splash
//-----------------------------------------
function _recache_templates( $id, $parent="", $group_only="", $root=1 )
{
$ids = array( 0 => array( 'id' => $id, 'parent' => $parent ) );
//-----------------------------------------
// 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";
//$out .= 'function '. $group['group_name'] .'( $ipsclass )'."\n{\n".'$this->ipsclass = &$ipsclass;'."\n}\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...
//-----------------------------------------
$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[] = "HTML шаблоны успешно обновлены... (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_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} не записан - невозможно создавать там php файлы";
}
}
//-----------------------------------------
// 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[] = "/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'] ][ $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[] = "Этот стиль установлен в безопасном режиме (safe mode) и php файлы кеша отсутствуют, соответствено нет необходимости запускать эту утилиту для этого стиля.";
return 0;
}
if ( ! is_readable($skin_dir) )
{
$this->messages[] = "ОШИБКА: Невозможна запись в папку /$skin_dir/, пожалуйста, проверьте права доступа (CHMOD) они должны быть 0777!";
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[] = "Пропущен skin_bugtracker.php...";
continue;
}
if ($FH = fopen($skin_dir."/".$filename, 'r') )
{
$fdata = fread( $FH, filesize($skin_dir."/".$filename) );
fclose($FH);
}
else
{
$this->messages[] = "Невозможно открыть для чтения файл $filename, этот файл пропущен...";
continue;
}
$fdata = str_replace( "\r", "\n", $fdata );
$fdata = str_replace( "\n\n", "\n", $fdata );
if ( ! preg_match( "/\n/", $fdata ) )
{
$this->messages[] = "Файл $filename пустой, этот файл пропущен...";
continue;
}
$farray = explode( "\n", $fdata );
//-----------------------------------------
$functions = array();
foreach($farray as $f)
{
//-----------------------------------------
// Skip javascript functions...
//-----------------------------------------
if ( preg_match( "/