%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /dom405686/wp-content/plugins/pws-core/admin/handlers/
Upload File :
Create Path :
Current File : /dom405686/wp-content/plugins/pws-core/admin/handlers/class-theme-handler.php

<?php
namespace GD_cORe\handlers;

if ( ! defined( 'ABSPATH' ) ) {

	exit;

}
/**
 * The admin-specific functionality of the plugin.
 *
 *
 * @package    GD_cORe
 * @subpackage GD_cORe/admin
 */
class Themes extends Main {
  private $function_actions;
  public function __construct(){
    parent::__construct();
    $this->function_actions = new Base_Function();
  }

  /**
   * calls api to see what themes a user has access to
   *
   * @return array contains theme list with details in alpha order (ASC)
   * @since 1.0.0
   **/
    public function lookup_theme_access() {
        $response = $this->gdcore_http_request('themes?accountId=' . GD_ACCOUNT_ID_IN_CORE, 'GET');

        if (is_wp_error($response)) {
            return [];
        }

        $decoded = json_decode($response, true);
        
        if (!is_array($decoded)) {
            return [];
        }

        return $decoded;
    }

  /**
   * checks for beaver builder theme
   *
   * @return 1-installed and activated|2-installed not activated|3-neither installed or activated|4 installed but out of date
   * @since 1.0.0
   **/
  public function check_theme_installs($theme_key){
    $active_theme   = wp_get_theme();
    $active_name    = $active_theme->get('Name');
    $provided_theme= json_decode($this->gdcore_http_request("themes/$theme_key?accountId=" . GD_ACCOUNT_ID_IN_CORE, 'GET'), true);
    $required_theme = wp_get_theme($provided_theme['key']);
    if ($required_theme->exists()) {
      $required_name = $required_theme->get('Name');
      $current_version = $required_theme->get('Version');
      $version_check = $this->check_theme_versions($theme_key, $current_version);
      if($version_check){
        return 4;
      }else{
        if($active_name === $required_name){
          return 1;
        } else {
          return 2;
        }
      }
    } else {
      return 3;
    }
  }

  /**
   * activates the theme
   *
   * @return empty
   * @since 1.0.0
   **/
  public function switch_the_theme($themeName){
    switch_theme($themeName, $themeName);
  }

  /**
   * checks if installed theme is out of date with the version the API contains
   *
   * @return alert
   * @since 1.0.0
   **/
  public function check_theme_versions($theme_key, $installed_verison){
    $result = json_decode($this->gdcore_http_request("themes/$theme_key?accountId=" . GD_ACCOUNT_ID_IN_CORE, 'GET'), true);
    if(isset($result['version'])){
      $current_version = $result['version'];
      $versionCheckComp = version_compare($installed_verison, $current_version);
      if ($versionCheckComp < 0) {
        //needs update
        return true;
      } else {
        //up to date
        return false;
      }
    }
  }

  /**
   * Calls the API and installs a theme by key
   *
   * @since 1.0.0
  */
  public function install_the_theme($theme_key) {
    // Get theme details from API using the key
    $theme = json_decode($this->gdcore_http_request("themes/$theme_key?accountId=" . GD_ACCOUNT_ID_IN_CORE, 'GET'), true);

    if (!$theme || !isset($theme['key'])) {
        echo "Failed to retrieve theme data.";
        return;
    }

    $themeSlug = $theme['key'];

    // Sanitize the API response too (don't trust external data)
    $themeSlug = sanitize_file_name($themeSlug);

    // Validate path construction
    $filePath = WP_CONTENT_DIR . '/themes/' . $themeSlug . '.zip';

    // Ensure path stays within expected directory
    $realDir = realpath(dirname($filePath));
    $expectedDir = realpath(WP_CONTENT_DIR . '/themes');

    if ($realDir !== $expectedDir || !$expectedDir) {
        error_log("Path traversal attempt blocked: $filePath");
        echo "Invalid file path.";
        return;
    }

    $result = $this->gdcore_http_request("themes/$theme_key/download?accountId=" . GD_ACCOUNT_ID_IN_CORE, 'GET');

    // Reject the download unless it is a real ZIP within a sane size cap.
    // Guards against a compromised upstream serving HTML/error bodies.
    if (!is_string($result) || strlen($result) < 4 || substr($result, 0, 4) !== "PK\x03\x04") {
        error_log("Rejected non-ZIP payload for theme $themeSlug");
        echo "Failed to install theme.";
        return;
    }
    if (strlen($result) > 100 * 1024 * 1024) {
        error_log("Rejected oversized theme payload for $themeSlug");
        echo "Failed to install theme.";
        return;
    }

    // Save ZIP to file
    $folderPath = WP_CONTENT_DIR . '/themes/';
    file_put_contents($filePath, $result);

    $this->config_theme($themeSlug);

    // unzip_file() enforces safe extraction (rejects entries with ../
    // or absolute paths), unlike ZipArchive::extractTo which is
    // vulnerable to Zip Slip.
    require_once ABSPATH . 'wp-admin/includes/file.php';
    \WP_Filesystem();
    $unzip_result = unzip_file($filePath, $folderPath);

    if (!is_wp_error($unzip_result)) {
        // Safe file deletion with existence check
        if (file_exists($filePath) && is_file($filePath)) {
            unlink($filePath);
        } else {
            error_log("Attempted to delete non-existent file: $filePath");
        }

        wp_clean_themes_cache();
        $this->switch_the_theme($themeSlug);

    } else {
        if (file_exists($filePath) && is_file($filePath)) {
            unlink($filePath);
        }
        echo "Failed to unzip.";
    }
  }

    /**
     * sets theme settings to our preferred settings
     *
     * @since 4.0.0
     * @return none
     **/
    public function config_theme($theme_slug){
        $themeClass = str_replace('-', '_', $theme_slug);
        $themeClass = "GD_cORe\handlers\default_configs\\". $themeClass;
        if(class_exists($themeClass)){
            $themeConfig = new $themeClass;
            $themeConfig->runConfig();
        }
    }


  /**
   * used to link functions together in order to update a theme upon finding out its out of date.
   *
   * @return error
   * @since 1.0.0
   **/
  public function update_required_theme($theme_slug){
    if (!empty($theme_slug)) {
      $this->remove_required_theme($theme_slug);
      $this->install_the_theme($theme_slug);
    } else {
      return 'upgrade failed';
    }
  }

  /**
   * deletes a themes files and folders
   *
   * @return empty
   * @since 1.0.0
   **/
  public function remove_required_theme($theme_slug){
    if (!empty($theme_slug)) {
      $file_path_removal = WP_CONTENT_DIR . '/themes/' . $theme_slug;
      $this->function_actions->rrmdir($file_path_removal);
    }
  }

}

Zerion Mini Shell 1.0