<?php

# PLUGIN PREVIEW BY TEXTPATTERN.INFO

if(@txpinterface == 'admin') {
    
add_privs('aks_cache_tab''1,2');
    
register_tab("extensions""aks_cache_tab""aks_cache");
    
register_callback("aks_cache_tab""aks_cache_tab");
    
add_privs('plugin_prefs.aks_cache''1,2');
    
register_callback('aks_cache_tab''plugin_prefs.aks_cache');
}
if (
class_exists('\Textpattern\Tag\Registry')) {
    
Txp::get('\Textpattern\Tag\Registry')
        ->
register('aks_cache')
        ->
register('aks_cache_disable')
    ;
}

function 
aks_cache_disable(){
    global 
$aks_cache_disable;
    
$aks_cache_disable 1;
}

function 
aks_cache($atts,$thing='') {
    global 
$prefs$aks_cache_disable;
    
extract(lAtts(array(
        
'block'        => '',
        
'id'        => serverSet("REQUEST_URI"),
        
'day'        => 0,
        
'hour'        => 0,
        
'min'        => 0,
        
'noreset'    => '',
        
'disable'    => 0,
        
'form'        => '',
        
'debug'        => ((strpos($prefs['aks_cache_opt'],'debug')===false)? 0:1)
    ),
$atts));

    if( !(
strpos($prefs['aks_cache_opt'],'disable')===false) || $disable){
        return 
aks_cache_parse($thing$form);
    }
    
$prr=is_logged_in();
    if( !(
strpos($prefs['aks_cache_opt'],'dis_adm')===false)&& $prr['privs']==){
        return 
aks_cache_parse($thing$form);
    }
    if( !(
strpos($prefs['aks_cache_opt'],'dis_users')===false)&& $prr ){
        return 
aks_cache_parse($thing$form);
    }

    if( !(
strpos($prefs['aks_cache_opt'],'reset')===false)&& ($prefs['aks_cache_site_lastmod']!=$prefs['lastmod']) ){
        
safe_update("txp_prefs""val='".$prefs['lastmod']."'""name='aks_cache_site_lastmod'");
        
safe_delete("aks_cache2""infos not like 'noreset-%'");
        
$prefs['aks_cache_site_lastmod'] = $prefs['lastmod'];
    }
    if( 
$noreset ){
        
$noreset 'noreset-';
    }
    
$diff $day*1440+$hour*60+$min;
    if(!
$diff){
        
$diff $prefs['aks_cache_time'];
    }
    
$ttl2 time()+$diff*60;
    
$hash doSlash(md5($noreset.$block.$idtrue));
    
$id2 doSlash($noreset.$block.$id);
    
$act "from cache";
    
$aks_cache_disable 0;
    
$rs safe_row("ttl, IF(spack='1', uncompress(data), data ) data""aks_cache2""hid='$hash'");
    if(
$rs){
        
extract($rs);
        if( 
$ttl<time() ){
            
$data aks_cache_parse($thing$form);
            if( !
$aks_cache_disable ){
                
$act "update cache";
                
$data2 doSlash($data);
                
$sql "ttl='$ttl2', infos='$id2|$diff|".strlen($data)."'";
                if( 
strpos(@$prefs['aks_cache_opt'], "sql_pack")!==false ){
                    
$sql .= ", data=compress('$data2'), spack='1'";
                }else{
                    
$sql .= ", data='$data2', spack='0'";
                }
                
safe_update("aks_cache2"$sql"hid='{$hash}'");
            }else{
                
$act "disable cache";
            }
        }
    }else{
        
$data aks_cache_parse($thing$form);
        if(!
$aks_cache_disable){
            
$act "insert into cache";
            
$data2 doSlash($data);
            
$sql "ttl='$ttl2', infos='$id2|$diff|".strlen($data)."'";
            if( 
strpos(@$prefs['aks_cache_opt'], "sql_pack")!==false ){
                
$sql .= ", data=compress('$data2'), spack='1'";
            }else{
                
$sql .= ", data='$data2', spack='0'";
            }
            
safe_insert("aks_cache2""hid='{$hash}', $sql");
        }else{
            
$act "disable cache";
        }
    }
    if(
$debug){
        return 
"<!--txp:aks_cache block='{$block}' id='{$id}' action='{$act}' ttl='{$diff} minutes' -->{$data}<!--/txp:aks_cache-->";
    }
    return 
$data;
}

function 
aks_cache_parse($thing=''$form='') {
    if( 
$form ){
        return 
parse_form($form);
    }else{
        return 
parse($thing);
    }
}


function 
aks_cache_tab($event$step) {
    global 
$prefs;

    
$tcheck getRows("SHOW TABLES LIKE '".PFX."aks_cache2'");
    if(!
$tcheck) {
        
$qc "CREATE TABLE `".PFX."aks_cache2` (
            `hid` binary(16) NOT NULL,
            `ttl` int(4) unsigned NOT NULL default '0',
            `spack` tinyint(1) NOT NULL default '0',
            `data` mediumblob NOT NULL,
            `infos` varchar(255) NOT NULL,
            UNIQUE KEY  (`hid`),
            KEY `ind_ttl` (`ttl`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
;
        
safe_query($qc);
    }

    if(!isset(
$prefs['aks_cache_time'])){
        
set_pref('aks_cache_time'60'aks_cache'1);
    }
    if(!isset(
$prefs['aks_cache_opt'])){
        
set_pref('aks_cache_opt''''aks_cache'1);
        
$qq '';
    }else{
        
$qq $prefs['aks_cache_opt'];
    }
    if(!isset(
$prefs['aks_cache_site_lastmod'])){
        
set_pref('aks_cache_site_lastmod''''aks_cache'2);
    }

    
safe_delete("aks_cache2""ttl<UNIX_TIMESTAMP()");

    if (
ps("save")) {
        
pagetop("aks_cache Prefs""Preferences Saved");
        
set_pref('aks_cache_time', (int)ps('aks_cache_time'), 'aks_cache'1);
        if(isset(
$_POST['aks_cache_opt'])){
            
$opt implode(','$_POST['aks_cache_opt']);
        }else{
            
$opt="";
        }
        
set_pref('aks_cache_opt'$opt'aks_cache'1);
        
header("Location: index.php?event=aks_cache_tab");
    } else {
        
pagetop("aks_cache Prefs");
    }
    if(
ps("reset_cache")){
        
safe_delete("aks_cache2""infos not like 'noreset-%'");
    }
    if(
ps("clear_cache")){
        
safe_query("truncate table `".PFX."aks_cache2`");
    }
    
$GLOBALS['prefs'] = get_prefs();
    
$qcount safe_count('aks_cache2''1=1');

    
$aks_list 'disable:Disable aks_cache|sql_pack:Compress data|reset:<b>Reset cache</b> if site was updated|dis_adm:Disable cache for admin|dis_users:Disable cache for users|debug:Debug|show_all:Display more then 100 records';
    
$out "";
    foreach(
do_list($aks_list'|') as $aa){
        
$a explode(':'$aa2);
        
$out.=trtda($a[1]).tda(checkbox('aks_cache_opt[]',$a[0],((strpos($qq,$a[0])===false)? 0:1) ), ' ') .tda(" ") );
    }

    echo 
'<table width="100%" border="0"><tr valign="top"><td width="36%">'.form(startTable('aks-list').
    
tr(tdcs("<div style='text-align:right;'>[<a href='?event=plugin&step=plugin_help&name=aks_cache'>Plugin help</a>]</div>".hed("<a href='?event=aks_cache_tab'>aks_cache</a>",1),3)).
    
$out.
    
tr(
    
tda(gTxt('Default cache time in minutes:')).tda(fInput("text","aks_cache_time",$prefs['aks_cache_time'],'edit','','','10'), ' ') .tda("")
    ).
    
tr(
    
tda(fInput("submit","save",gTxt("save_button"),"publish").eInput("aks_cache_tab").sInput('saveprefs'), " class=\"noline\"").tda(" ")
    ).
    
tr(tda('<br /><br />').tda(" ")).

    
tr(
    
tda('Items in cache:').tda("<b>$qcount</b>")
    ).
    
tr(
    
tda(fInput("submit","clear_cache","Clear all cache","publish")).tda(fInput("submit","reset_cache","Reset cache","publish"))
    ). 
endTable());

    
$limit = (strpos($qq"show_all")===false)? "limit 100" "";
    
$qinfos safe_column("infos""aks_cache2""1=1 order by infos asc $limit");
    echo 
'</td><td><table width="98%"><tr><th>cache block_id</th><th>ttl (min)</th><th>size (bytes)</th></tr>';
    foreach(
$qinfos as $qs){
        
$qs preg_replace('/\|/''</td><td style="text-align:right;">'$qs);
        echo 
"<tr><td>{$qs}</td></tr>";
    }
    echo 
"</table></td></tr></table>";
echo <<<EOF
<style type="text/css">
#aks-list td{
    padding: 5px;
    border-bottom: 1px dotted #ccc;
}
#aks-list .edit{
    width: 100%;
}

#aks-list tr td:first-child {
    min-width: 108px;
    text-align:right;
    vertical-align:middle;
}
</style>
EOF;

}