Ejemplo n.º 1
0
 /**
  * Check the plugins directory and retrieve all plugin files with plugin data.
  * This function will also retrieve the URL and name of the repository/page
  * where it is being published at the WordPress plugins market.
  *
  * @return array Key is the plugin file path and the value is an array of the plugin data.
  */
 public static function getPlugins()
 {
     // Check if the cache library was loaded.
     $can_cache = class_exists('SucuriScanCache');
     if ($can_cache) {
         $cache = new SucuriScanCache('plugindata');
         $cached_data = $cache->get('plugins', SUCURISCAN_GET_PLUGINS_LIFETIME, 'array');
         // Return the previously cached results of this function.
         if ($cached_data !== false) {
             return $cached_data;
         }
     }
     // Get the plugin's basic information from WordPress transient data.
     $plugins = get_plugins();
     $pattern = '/^http(s)?:\\/\\/wordpress\\.org\\/plugins\\/(.*)\\/$/';
     $wp_market = 'sucuri://wordpress.org/plugins/%s/';
     // Loop through each plugin data and complement its information with more attributes.
     foreach ($plugins as $plugin_path => $plugin_data) {
         // Default values for the plugin extra attributes.
         $repository = '';
         $repository_name = '';
         $is_free_plugin = false;
         /**
          * Extract the information of the plugin which includes the repository name,
          * repository URL, and if the source code of the plugin is publicly released or
          * not, in this last case if the source code of the plugin is not hosted in the
          * official WordPress server it means that it is premium and is being
          * distributed by an independent developer.
          */
         if (isset($plugin_data['PluginURI']) && preg_match($pattern, $plugin_data['PluginURI'], $match)) {
             $repository = $match[0];
             $repository_name = $match[2];
             $is_free_plugin = true;
         } else {
             if (strpos($plugin_path, '/') !== false) {
                 $plugin_path_parts = explode('/', $plugin_path, 2);
             } else {
                 $plugin_path_parts = explode('.', $plugin_path, 2);
             }
             if (isset($plugin_path_parts[0])) {
                 $possible_repository = sprintf($wp_market, $plugin_path_parts[0]);
                 $possible_repository = SucuriScanAPI::apiUrlProtocol($possible_repository);
                 $resp = wp_remote_head($possible_repository);
                 if (!is_wp_error($resp) && $resp['response']['code'] == 200) {
                     $repository = $possible_repository;
                     $repository_name = $plugin_path_parts[0];
                     $is_free_plugin = true;
                 }
             }
         }
         // Complement the plugin's information with these attributes.
         $plugins[$plugin_path]['Repository'] = $repository;
         $plugins[$plugin_path]['RepositoryName'] = $repository_name;
         $plugins[$plugin_path]['InstallationPath'] = sprintf('%s/%s', WP_PLUGIN_DIR, $repository_name);
         $plugins[$plugin_path]['IsFreePlugin'] = $is_free_plugin;
         $plugins[$plugin_path]['PluginType'] = $is_free_plugin ? 'free' : 'premium';
         $plugins[$plugin_path]['IsPluginActive'] = false;
         $plugins[$plugin_path]['IsPluginInstalled'] = false;
         if (is_plugin_active($plugin_path)) {
             $plugins[$plugin_path]['IsPluginActive'] = true;
         }
         if (is_dir($plugins[$plugin_path]['InstallationPath'])) {
             $plugins[$plugin_path]['IsPluginInstalled'] = true;
         }
     }
     if ($can_cache) {
         // Add the information of the plugins to the file-based cache.
         $cache->add('plugins', $plugins);
     }
     return $plugins;
 }