%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-plugin-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 Plugins extends Main {

  private $theme_actions;
  private $function_actions;
  public function __construct(){
    parent::__construct();
    $this->theme_actions = new Themes();
    $this->function_actions = new Base_Function();
  }

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

        if (is_wp_error($response)) {
            error_log('Plugin access fetch failed: ' . $response->get_error_message());
            return [];
        }

        $decoded = json_decode($response, true);
        
        // Ensure we always return an array
        if (!is_array($decoded)) {
            error_log('Plugin access response invalid JSON or null: ' . substr($response, 0, 200));
            return [];
        }

        return $decoded;
    }


  /**
   * checks if a plugin is installed by name
   *
   * @return array contains 1(installed and activate) or 2(installed not active) | Plugin Path | Plugin version or null if not installed
   * @since 1.0.0
   **/
   public function check_plugin_status($plugin_name) {
    $installed_plugins = get_plugins();

    foreach ($installed_plugins as $path => $details) {
        if ($plugin_name === $details['Name']) {
            if (is_plugin_active($path)) {
                return [1, $path, $details['Version']]; // active
            } else {
                return [2, $path, $details['Version']]; // installed, not active
            }
        }
    }

    // Not installed at all
    return [0, null, null];
   }


    /**
   * checks if installed plugin is out of date with the version the API contains returns true if update needed
   *
   * @return boolean
   * @since 1.0.0
   **/
  public function check_plugin_versions($plugin_key, $installed_version){
    $result = json_decode($this->gdcore_http_request("plugins/$plugin_key?accountId=" . GD_ACCOUNT_ID_IN_CORE, 'GET'), true);
    if($result['hosted']){
      if(isset($result['version'])){
        $current_version = $result['version'];
        $versionCheckComp = version_compare($installed_version, $current_version);
        if ($versionCheckComp < 0) {
          return true;
        }
      }
    }
  }

  /**
   * gets plugin details based on KEY
   * @return $pluginData array
   * @since 1.0.0
  **/
  public function gatherPluginData($plugin_key){
    //gets plugin details from API
    $pluginData = $this->gdcore_http_request("plugins/$plugin_key?accountId=" . GD_ACCOUNT_ID_IN_CORE, 'GET');
    $pluginData = json_decode($pluginData, true);

    return $pluginData;
  }

    /**
     * Installs plugin(s) from API or WordPress and optionally installs a theme
     *
     * @since 1.0.0
     */
    public function install_required_plugin($plugin_keys, $installTheme = null, $redirect = null) {
        $plugin_keys = (array)$plugin_keys;
        $activateList = [];

        foreach ($plugin_keys as $pluginInstall) {
            // Get plugin details from API
            $pluginData = $this->gatherPluginData($pluginInstall);
            if (!$pluginData || !isset($pluginData['key'])) {
                echo "Failed to retrieve plugin data for $pluginInstall";
                continue;
            }

            $pluginKey = $pluginData['key'];
            $pluginName = $pluginData['name'];
            $pluginSlug = $pluginKey;

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

            $pluginVersion = $pluginData['version'];
            $pluginHost = $pluginData['hosted'];

            $this->config_plugin($pluginSlug);

            // Fetch plugin from correct source
            if ($pluginHost) {
                $result = $this->gdcore_http_request("plugins/$pluginKey/download?accountId=" . GD_ACCOUNT_ID_IN_CORE, 'GET');
            } else {
                if ($pluginVersion == 0) {
                    $result = $this->gdcore_http_request(null, 'GET', null, true, $pluginSlug, $pluginVersion);
                } else {
                    $realVersion = json_decode($this->gdcore_http_request(null, 'GET', null, true, $pluginSlug, null, self::WORDPRESS_API . "$pluginSlug.json"));
                    $realVersion = $realVersion->version ?? $pluginVersion;
                    $result = $this->gdcore_http_request(null, 'GET', null, true, $pluginSlug, $realVersion);
                }
            }

            // Install ZIP
            $filePath = WP_CONTENT_DIR . '/plugins/' . $pluginSlug . '.zip';

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

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

            $folderPath = WP_CONTENT_DIR . '/plugins/';

            // 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 plugin $pluginSlug");
                echo "Failed to install $pluginName";
                continue;
            }
            if (strlen($result) > 100 * 1024 * 1024) {
                error_log("Rejected oversized plugin payload for $pluginSlug");
                echo "Failed to install $pluginName";
                continue;
            }

            file_put_contents($filePath, $result);

            // 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 plugin file: $filePath");
                }

                wp_clean_plugins_cache();
                $installed_plugins = get_plugins();

                foreach ($installed_plugins as $path => $details) {
                    if ($pluginName === $details['Name']) {
                        $pluginPath = $path;
                        break;
                    }
                }

                if (isset($pluginPath)) {
                    $activateList[] = $pluginPath;
                }
            } else {
                if (file_exists($filePath) && is_file($filePath)) {
                    unlink($filePath);
                }
                echo "Failed to install $pluginName";
            }
        }

        foreach ((array)$activateList as $pluginActivate) {
            $this->activate_required_plugins($pluginActivate);
        }

        // Install V3 theme if required
        if ($installTheme) {
            $available_themes = $this->theme_actions->lookup_theme_access();
            $themeKey = '';

            foreach ($available_themes as $key => $theme) {
                if (($theme['effort'] ?? '') === 'V3') {
                    $themeKey = $key;
                    break;
                }
            }

            if ($themeKey) {
                $this->theme_actions->install_the_theme($themeKey);
            }
        }

        if ($redirect) {
            $this->function_actions->redirect(self::PLUGIN_MAIN);
        }
    }


  /**
   * activates a plugin by path
   *
   * @return empty
   * @since 1.0.0
   **/
  public function activate_required_plugins($path){
    activate_plugin($path, null, false, false);
  }

  /**
  * deactivates a plugin by path
  *
  * @return empty
  * @since 1.0.0
  **/
  public function deactivate_required_plugins($path){
    deactivate_plugins($path);
  }

  /**
   * used to link functions together in order to update a plugin upon finding out its out of date.
   *
   * @return error
   * @since 1.0.0
   **/
  public function update_required_plugin($plugin_key){
    //gets plugin details from API
    $pluginData = $this->gatherPluginData($plugin_key);

    //assign plugin vars
    $pluginName = $pluginData['name'];
    $pluginSlug= $pluginData['key'];

    //do we host the file?
    $pluginHost = $pluginData['hosted'];

    if($pluginHost){
      if (!empty($pluginSlug)) {
        $this->remove_required_plugin($pluginSlug);
        $this->install_required_plugin($plugin_key);
      } else {
        return 'upgrade failed';
      }
    }else{
      return 'upgrade through the main WordPres plugin screen';
    }
  }

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

  /**
   * sets plugin settings to our preferred settings
   *
   * @since 2.2.2
   * @Overhauled 2.8
   * @return none
   **/
  public function config_plugin($plugin_slug){
    $pluginClass = str_replace('-', '_', $plugin_slug);
    $pluginClass = "GD_cORe\handlers\plugin_configs\\". $pluginClass;
    if(class_exists($pluginClass)){
      $pluginConfig = new $pluginClass;
      $pluginConfig->runConfig();
    }
  }

 /**
   * gets a list of installed plugins not on our approved list
   *
   * @since 3.0.0
   * @return array
  **/
  public function plugin_review() {
    $get_api_plugins = $this->lookup_plugin_access();
    $api_plugins = [];

    foreach ($get_api_plugins as $plugin) {
        $api_plugins[] = $plugin['name'];
    }

    // Add any manually approved plugins
    $api_plugins[] = 'GD cORe';
    $api_plugins[] = 'PWS Core';

    $installed_plugins = [];

    foreach (get_plugins() as $plugin) {
        $installed_plugins[] = $plugin['Name'];
    }

    return array_diff($installed_plugins, $api_plugins);
  }

  /**
   * checks if bulk action requested can be done considering the provided plugins status
   *
   * @since 3.0.0
   * @return boolean
   **/
  public function bulk_action_performable($action, $plugin_status){
    switch($action){
      case 'update':
        if($plugin_status === 3){
          return true;
        }else{
          return false;
        }
        break;
      case 'deactivate':
        if($plugin_status === 1){
          return true;
        }else{
          return false;
        }
        break;
      case 'activate':
        if($plugin_status === 2){
          return true;
        }else{
          return false;
        }
        break;
      default:
      if(!in_array($plugin_status, [1, 2, 3], true)){
        return true;
      }else{
        return false;
      }
        break;
    }
  }

  /**
   * executes bulk action requested to the provided plugin
   *
   * @since 3.0.0
   * @return boolean
   **/
  public function perform_bulk_action($action, $plugin_key, $plugin_path = NULL){
    switch($action){
      case 'update':
        $this->update_required_plugin($plugin_key);
        break;
      case 'deactivate':
        $this->deactivate_required_plugins($plugin_path);
        break;
      case 'activate':
        $this->activate_required_plugins($plugin_path);
        break;
      case 'install':
        $this->install_required_plugin($plugin_key);
      break;
      default:
        return;
        break;
    }
  }
}

Zerion Mini Shell 1.0