string, 'files' => array( rel => md5 ), 'built' => int ) */ public static function manifest( $kind, $slug, $version, $allow_download = true ) { self::ensure_dirs(); $file = self::manifest_file( $kind, $slug, $version ); $m = null; // 0. Themes bundled with this WordPress release are covered by the core checksum manifest // (the theme-directory package of the same version can differ slightly), but only while the // installed copy IS the bundled release: bundled themes update separately, and a newer // release must be compared with its own package, never "repaired" back to the core version. $core = null; if ( 'theme' === $kind ) { $core = self::manifest_from_core( $slug ); if ( $core ) { $style = trailingslashit( get_theme_root() ) . $slug . '/style.css'; $same_release = empty( $core['files']['style.css'] ) || ! file_exists( $style ) || md5_file( $style ) === $core['files']['style.css']; if ( $same_release ) { self::write_manifest( $file, $core ); return $core; } if ( file_exists( $file ) ) { $c = self::read_manifest( $file ); if ( is_array( $c ) && isset( $c['source'] ) && 'WordPress core checksums' === $c['source'] ) { wp_delete_file( $file ); // stale: built when the theme was still the bundled release } } } } if ( file_exists( $file ) ) { $c = self::read_manifest( $file ); // Manifests of releases that kept packages on disk carry a 'root' or 'zip' key; those are rebuilt. if ( is_array( $c ) && ! empty( $c['files'] ) && ! isset( $c['root'] ) && empty( $c['zip'] ) ) { return $c; } } // 1. Checksum API (plugins only). if ( ! $m && 'plugin' === $kind ) { $sums = ITSM_Scanner::plugin_checksums( $slug, $version ); if ( $sums ) { $files = array(); foreach ( $sums as $rel => $h ) { // A file can have several valid hashes (the API then sends a list); '' = no usable hash, never verified. $files[ $rel ] = self::clean_hashes( is_array( $h ) ? ( isset( $h['md5'] ) ? $h['md5'] : '' ) : $h ); } $m = array( 'source' => 'wordpress.org checksums', 'files' => $files, 'built' => time() ); } } // 2. Checksums from a vendor zip the administrator uploaded are already a cached manifest (see store_upload()). // 3. Exact version zip from wordpress.org: hashed from WordPress's temporary download and deleted at once. if ( ! $m && $allow_download && self::is_wporg( $kind, $slug ) ) { $url = 'plugin' === $kind ? 'https://downloads.wordpress.org/plugin/' . rawurlencode( $slug ) . '.' . rawurlencode( $version ) . '.zip' : 'https://downloads.wordpress.org/theme/' . rawurlencode( $slug ) . '.' . rawurlencode( $version ) . '.zip'; $files = self::hash_remote_package( $url, $slug ); if ( $files ) { $m = array( 'source' => 'wordpress.org package', 'files' => $files, 'built' => time() ); } } // 4. A bundled theme that is not the release shipped with core and whose own package could not be fetched has // no reference: the core checksums describe another version, so nothing is returned or cached and the // next scan tries again. if ( $m ) { self::write_manifest( $file, $m ); } return $m; } /** Normalises a checksum entry: one md5 string, a list of md5 strings, or '' when there is no usable hash. */ private static function clean_hashes( $h ) { $list = array(); foreach ( (array) $h as $one ) { if ( is_string( $one ) && preg_match( '/^[a-f0-9]{32}$/i', $one ) ) { $list[] = strtolower( $one ); } } if ( ! $list ) { return ''; } return 1 === count( $list ) ? $list[0] : array_values( array_unique( $list ) ); } private static function manifest_from_core( $slug ) { global $wp_version; $sums = get_transient( 'itsm_core_checksums' ); if ( ! is_array( $sums ) || ! $sums ) { $sums = array(); if ( ! get_transient( 'itsm_core_checksums_failed' ) ) { $res = wp_remote_get( 'https://api.wordpress.org/core/checksums/1.0/?version=' . rawurlencode( $wp_version ) . '&locale=en_US', array( 'timeout' => 20 ) ); $json = is_wp_error( $res ) ? null : json_decode( wp_remote_retrieve_body( $res ), true ); $sums = isset( $json['checksums'] ) && is_array( $json['checksums'] ) ? $json['checksums'] : array(); if ( $sums ) { set_transient( 'itsm_core_checksums', $sums, HOUR_IN_SECONDS ); } else { // A failed request is not a list: only the failure is remembered (briefly), under its own key. set_transient( 'itsm_core_checksums_failed', 1, 10 * MINUTE_IN_SECONDS ); } } } $prefix = 'wp-content/themes/' . $slug . '/'; $files = array(); foreach ( $sums as $rel => $md5 ) { if ( 0 === strpos( $rel, $prefix ) ) { $files[ substr( $rel, strlen( $prefix ) ) ] = $md5; } } return $files ? array( 'source' => 'WordPress core checksums', 'files' => $files, 'built' => time() ) : null; } /** * Checksums of every file in a package on downloads.wordpress.org. The archive only exists as the temporary * file download_url() creates in WordPress's temp folder and is deleted before this returns. */ private static function hash_remote_package( $url, $slug ) { if ( ! function_exists( 'download_url' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $tmp = download_url( $url, 60 ); if ( is_wp_error( $tmp ) ) { return null; } $b = self::build_manifest( $tmp, $slug ); wp_delete_file( $tmp ); return $b ? $b['files'] : null; } /* ----------------------------- Archive access ----------------------------- */ /** An entry name that could escape the package folder: rejected, the whole archive is refused. */ private static function unsafe_name( $name ) { return 0 === strpos( $name, '/' ) || false !== strpos( $name, '\\' ) || preg_match( '#(?:^|/)\.\.(?:/|$)#', $name ) || false !== strpos( $name, "\0" ); } /** A member that is skipped: directories and the macOS resource fork folder. */ private static function skip_name( $name ) { return '' === $name || '/' === substr( $name, -1 ) || 0 === strpos( $name, '__MACOSX/' ); } /** * The folder prefix inside the archive that holds the package: "slug/" when every entry sits under * it, the single shared top-level folder otherwise, or '' for a flat archive. */ private static function base_prefix( array $names, $slug ) { if ( '' !== (string) $slug ) { $all = (bool) $names; foreach ( $names as $n ) { if ( 0 !== strpos( $n, $slug . '/' ) ) { $all = false; break; } } if ( $all ) { return $slug . '/'; } } $top = null; foreach ( $names as $n ) { $pos = strpos( $n, '/' ); if ( false === $pos ) { return ''; } $t = substr( $n, 0, $pos + 1 ); if ( null === $top ) { $top = $t; } elseif ( $t !== $top ) { return ''; } } return null === $top ? '' : $top; } private static function pclzip( $zip ) { if ( ! class_exists( 'PclZip' ) ) { require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; } return new PclZip( $zip ); } /** * Hash every member of a package archive without extracting it. * * @return array|null array( 'base' => prefix, 'files' => array( rel => md5 ) ), or null when the archive * cannot be read, is empty or contains an entry that could escape its folder. */ public static function build_manifest( $zip, $slug = '' ) { if ( ! is_file( $zip ) ) { return null; } $slug = (string) $slug; if ( class_exists( 'ZipArchive' ) ) { $za = new ZipArchive(); if ( true !== $za->open( $zip ) ) { return null; } $names = array(); for ( $i = 0; $i < $za->numFiles; $i++ ) { $n = (string) $za->getNameIndex( $i ); if ( self::unsafe_name( $n ) ) { $za->close(); return null; } if ( ! self::skip_name( $n ) ) { $names[ $n ] = $i; } } $base = self::base_prefix( array_keys( $names ), $slug ); $files = array(); foreach ( $names as $n => $i ) { if ( '' !== $base && 0 !== strpos( $n, $base ) ) { continue; } $rel = substr( $n, strlen( $base ) ); $c = $za->getFromIndex( $i ); if ( '' === $rel || false === $c ) { continue; } $files[ $rel ] = md5( $c ); } $za->close(); return $files ? array( 'base' => $base, 'files' => $files ) : null; } $p = self::pclzip( $zip ); $list = $p->listContent(); if ( ! is_array( $list ) ) { return null; } $names = array(); foreach ( $list as $e ) { $n = isset( $e['filename'] ) ? (string) $e['filename'] : ''; if ( self::unsafe_name( $n ) ) { return null; } if ( ! empty( $e['folder'] ) || self::skip_name( $n ) ) { continue; } $names[ $n ] = $e['index']; } $base = self::base_prefix( array_keys( $names ), $slug ); $files = array(); foreach ( $names as $n => $i ) { if ( '' !== $base && 0 !== strpos( $n, $base ) ) { continue; } $rel = substr( $n, strlen( $base ) ); $r = $p->extractByIndex( (string) $i, PCLZIP_OPT_EXTRACT_AS_STRING ); if ( '' === $rel || ! is_array( $r ) || empty( $r[0] ) || ! isset( $r[0]['content'] ) ) { continue; } $files[ $rel ] = md5( $r[0]['content'] ); } return $files ? array( 'base' => $base, 'files' => $files ) : null; } /** Content of one member of the archive that is being hashed (a temporary download or PHP's upload temp file), or null. */ private static function member( array $m, $rel ) { if ( empty( $m['zip'] ) || ! is_file( $m['zip'] ) ) { return null; } $rel = ltrim( wp_normalize_path( $rel ), '/' ); if ( '' === $rel || self::unsafe_name( $rel ) ) { return null; } $name = ( isset( $m['base'] ) ? $m['base'] : '' ) . $rel; if ( class_exists( 'ZipArchive' ) ) { $za = new ZipArchive(); if ( true !== $za->open( $m['zip'] ) ) { return null; } $c = $za->getFromName( $name ); $za->close(); return false === $c ? null : $c; } $r = self::pclzip( $m['zip'] )->extract( PCLZIP_OPT_BY_NAME, $name, PCLZIP_OPT_EXTRACT_AS_STRING ); if ( ! is_array( $r ) || empty( $r[0] ) || ! isset( $r[0]['content'] ) || ( isset( $r[0]['status'] ) && 'ok' !== $r[0]['status'] ) ) { return null; } return $r[0]['content']; } /** One file-header field ("Version", "Plugin Name") parsed from file content, the way get_file_data() does. */ private static function header_value( $content, $field ) { $head = str_replace( "\r", "\n", substr( (string) $content, 0, 8192 ) ); if ( preg_match( '/^(?:[ \t]*<\?php)?[ \t\/*#@]*' . preg_quote( $field, '/' ) . ':(.*)$/mi', $head, $mm ) && $mm[1] ) { return trim( preg_replace( '/\s*(?:\*\/|\?>).*/', '', $mm[1] ) ); } return ''; } /** The version declared by a package: style.css for themes, the main plugin file header for plugins. */ private static function package_version( $kind, array $m ) { if ( 'theme' === $kind ) { $css = self::member( $m, 'style.css' ); return null === $css ? '' : self::header_value( $css, 'Version' ); } foreach ( array_keys( $m['files'] ) as $rel ) { if ( false !== strpos( $rel, '/' ) || ! preg_match( '/\.php$/i', $rel ) ) { continue; } $c = self::member( $m, $rel ); if ( null !== $c && '' !== self::header_value( $c, 'Plugin Name' ) ) { return self::header_value( $c, 'Version' ); } } return ''; } /* ----------------------------- Lookups ----------------------------- */ /** Is this slug hosted on wordpress.org? True only when the directory confirmed it (see wporg_state()). */ public static function is_wporg( $kind, $slug ) { return true === self::wporg_state( $kind, $slug ); } /** * Is this slug hosted on wordpress.org? true = yes, false = the directory answered that it is not (or lists * a different product under that name), null = the directory could not be asked (timeout, outage). Definite * answers are cached one day, an unknown one ten minutes. */ public static function wporg_state( $kind, $slug ) { $key = 'itsm_wporg_' . md5( $kind . $slug ); $c = get_transient( $key ); if ( false !== $c ) { return 'unknown' === $c ? null : 'yes' === $c; } if ( 'plugin' === $kind ) { if ( ! function_exists( 'plugins_api' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; } $api = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) ); } else { if ( ! function_exists( 'themes_api' ) ) { require_once ABSPATH . 'wp-admin/includes/theme.php'; } $api = themes_api( 'theme_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) ); } if ( is_wp_error( $api ) || empty( $api ) ) { // Only the directory's own 'not found' answer is definite; anything else (timeout, 5xx, unreadable body) is unknown. if ( is_wp_error( $api ) && false !== stripos( $api->get_error_message(), 'not found' ) ) { set_transient( $key, 'no', DAY_IN_SECONDS ); return false; } set_transient( $key, 'unknown', 10 * MINUTE_IN_SECONDS ); return null; } $yes = self::same_product( $kind, $slug, $api ); set_transient( $key, $yes ? 'yes' : 'no', DAY_IN_SECONDS ); return $yes; } /** * A wordpress.org package is only a valid reference when it is the same product as the installed one. * Commercial plugins and themes sometimes share a folder name with an unrelated free listing; comparing the * installed header (name, author, URI) with the directory entry prevents "repairing" them with foreign files. */ private static function same_product( $kind, $slug, $api ) { $api_name = is_object( $api ) ? (string) ( $api->name ?? '' ) : (string) ( $api['name'] ?? '' ); $api_author = is_object( $api ) ? ( $api->author ?? '' ) : ( $api['author'] ?? '' ); if ( is_array( $api_author ) || is_object( $api_author ) ) { // The theme directory describes the author as a record (display_name, user_nicename, ...). $rec = (array) $api_author; $api_author = isset( $rec['display_name'] ) ? $rec['display_name'] : ( isset( $rec['author'] ) ? $rec['author'] : ( isset( $rec['user_nicename'] ) ? $rec['user_nicename'] : '' ) ); } $api_author = (string) $api_author; $api_home = is_object( $api ) ? (string) ( $api->homepage ?? '' ) : (string) ( $api['homepage'] ?? '' ); $name = $author = $uri = ''; if ( 'plugin' === $kind ) { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } foreach ( get_plugins() as $file => $d ) { if ( dirname( $file ) === $slug ) { $name = (string) $d['Name']; $author = (string) $d['AuthorName']; $uri = (string) ( $d['PluginURI'] ?: $d['AuthorURI'] ); break; } } } else { $theme = wp_get_theme( $slug ); if ( $theme->exists() ) { $name = (string) $theme->get( 'Name' ); $author = (string) $theme->get( 'Author' ); $uri = (string) ( $theme->get( 'ThemeURI' ) ?: $theme->get( 'AuthorURI' ) ); } } if ( '' === $name ) { return true; // nothing installed to compare with (lookup for an unknown slug): keep the old behaviour } $norm = function ( $v ) { $v = strtolower( wp_strip_all_tags( html_entity_decode( (string) $v, ENT_QUOTES, 'UTF-8' ) ) ); $v = preg_replace( '/\b(the|wordpress|wp|plugin|theme|for|by|lite|free|pro|premium)\b/', ' ', $v ); return trim( preg_replace( '/[^a-z0-9]+/', '', $v ) ); }; $n1 = $norm( $name ); $n2 = $norm( $api_name ); if ( '' !== $n1 && '' !== $n2 && ( $n1 === $n2 || false !== strpos( $n1, $n2 ) || false !== strpos( $n2, $n1 ) ) ) { return true; } $a1 = $norm( $author ); $a2 = $norm( $api_author ); if ( '' !== $a1 && '' !== $a2 && ( $a1 === $a2 || false !== strpos( $a1, $a2 ) || false !== strpos( $a2, $a1 ) ) ) { return true; } $h1 = strtolower( (string) wp_parse_url( $uri, PHP_URL_HOST ) ); $h2 = strtolower( (string) wp_parse_url( $api_home, PHP_URL_HOST ) ); if ( '' !== $h1 && $h1 === $h2 && 'wordpress.org' !== $h1 ) { return true; } ITSM_Utils::log( 'reference_mismatch', $kind . ':' . $slug . ' installed "' . $name . '" vs wordpress.org "' . $api_name . '"' ); return false; } /** Pristine content of one file, fetched from the WordPress.org repositories into memory (never stored), or null. */ public static function original_file( $kind, $slug, $version, $rel ) { $rel = ltrim( wp_normalize_path( $rel ), '/' ); if ( 'core' === $kind ) { global $wp_version; foreach ( array( 'https://core.svn.wordpress.org/tags/' . $wp_version . '/' . $rel ) as $u ) { $c = self::http_get( $u ); if ( null !== $c ) { return $c; } } return null; } $m = self::manifest( $kind, $slug, $version, false ); if ( 'plugin' === $kind ) { foreach ( array( 'https://plugins.svn.wordpress.org/' . $slug . '/tags/' . $version . '/' . $rel, 'https://plugins.svn.wordpress.org/' . $slug . '/trunk/' . $rel ) as $u ) { $c = self::http_get( $u ); if ( null !== $c ) { return $c; } } } if ( 'theme' === $kind ) { global $wp_version; if ( $m && 'WordPress core checksums' === $m['source'] ) { foreach ( array( 'https://core.svn.wordpress.org/tags/' . $wp_version . '/wp-content/themes/' . $slug . '/' . $rel ) as $u ) { $c = self::http_get( $u ); if ( null !== $c ) { return $c; } } } $c = self::http_get( 'https://themes.svn.wordpress.org/' . $slug . '/' . $version . '/' . $rel ); if ( null !== $c ) { return $c; } } return null; } private static function http_get( $url ) { $res = wp_remote_get( $url, array( 'timeout' => 30, 'user-agent' => 'itsMalicious/' . ITSM_VERSION ) ); if ( is_wp_error( $res ) || 200 !== wp_remote_retrieve_response_code( $res ) ) { return null; } return wp_remote_retrieve_body( $res ); } /** * Compare an installed package with its reference. * * @return array|null { modified: rel[], missing: rel[], unknown: rel[], matched: rel[], unchecked: rel[], verified: int, source: string } * matched = files whose checksum equals the reference; unchecked = files the reference has no * usable checksum for (neither modified nor verified). */ public static function verify( $kind, $slug, $version, $base_dir ) { $m = self::manifest( $kind, $slug, $version ); if ( ! $m ) { return null; } $base = rtrim( wp_normalize_path( $base_dir ), '/' ) . '/'; $out = array( 'modified' => array(), 'missing' => array(), 'unknown' => array(), 'matched' => array(), 'unchecked' => array(), 'verified' => 0, 'source' => $m['source'] ); foreach ( $m['files'] as $rel => $md5 ) { $p = $base . $rel; if ( ! file_exists( $p ) ) { $out['missing'][] = $rel; continue; } $want = self::clean_hashes( $md5 ); $have = '' === $want ? false : md5_file( $p ); if ( '' === $want || false === $have ) { $out['unchecked'][] = $rel; // no usable reference hash, or the file cannot be read: not compared } elseif ( ! in_array( $have, (array) $want, true ) ) { if ( self::harmless_difference( $rel, $p, $m, $kind, $slug, $version ) ) { $out['verified']++; // documentation, or only the "Tested up to"-style header lines differ (re-packaged release) } else { $out['modified'][] = $rel; } } else { $out['verified']++; $out['matched'][] = $rel; } } $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base, FilesystemIterator::SKIP_DOTS ) ); foreach ( $it as $f ) { $rel = substr( wp_normalize_path( $f->getPathname() ), strlen( $base ) ); if ( ! isset( $m['files'][ $rel ] ) ) { $out['unknown'][] = $rel; } } return $out; } /** * A difference that cannot affect security: documentation/licence/screenshot files, or a text file * (style.css, readme.txt) whose only changes are header lines such as "Tested up to:" that the * directory rewrites when it re-packages a release. */ private static function harmless_difference( $rel, $path, array $m, $kind = '', $slug = '', $version = '' ) { static $lookups = 0; if ( preg_match( '/(?:^|\/)(?:readme\.(?:txt|md)|changelog\.(?:txt|md)|licen[cs]e(?:\.txt|\.md)?|screenshot(?:-\d+)?\.(?:png|jpe?g|gif))$/i', $rel ) ) { return true; } // Text files only, a few per request: the original is read from the WordPress.org repository into memory. if ( '' === $slug || 'uploaded vendor copy' === $m['source'] || ! preg_match( '/\.(?:css|txt|md)$/i', $rel ) || filesize( $path ) > 300000 || $lookups >= 6 ) { return false; } $lookups++; $orig = self::original_file( $kind, $slug, $version, $rel ); if ( null === $orig ) { return false; } $norm = function ( $c ) { $c = preg_replace( '/^[ \t\*]*(?:Tested up to|Requires at least|Requires PHP|Stable tag|Version|Text Domain|Domain Path|Tags|Contributors|Donate link|License|License URI|Update URI)\s*:[^\n]*$/mi', '', (string) $c ); return trim( preg_replace( '/\s+/', ' ', $c ) ); }; return $norm( file_get_contents( $path ) ) === $norm( $orig ); } /* ----------------------------- Vendor uploads ----------------------------- */ /** * Takes the checksums of a vendor zip the administrator uploads (commercial plugin or theme) and keeps only * those. The archive is read from PHP's upload temp file and never copied anywhere. Returns info|WP_Error. */ public static function store_upload( $kind, $tmp_zip ) { self::ensure_dirs(); $b = self::build_manifest( $tmp_zip, '' ); if ( ! $b ) { return new WP_Error( 'itsm_zip', __( 'The file could not be read as a zip archive. Upload the original zip from the vendor.', 'itsmalicious' ) ); } $slug = sanitize_file_name( rtrim( $b['base'], '/' ) ); $ver = $slug ? self::package_version( $kind, array( 'zip' => wp_normalize_path( $tmp_zip ), 'base' => $b['base'], 'files' => $b['files'] ) ) : ''; if ( ! $slug || ! $ver ) { return new WP_Error( 'itsm_zip', __( 'This does not look like a plugin or theme package (no header found).', 'itsmalicious' ) ); } foreach ( (array) glob( self::dir() . '/manifests/' . sanitize_key( $kind ) . '-' . sanitize_file_name( $slug ) . '-*.json.gz' ) as $old ) { wp_delete_file( $old ); // checksums of other versions of this package } self::write_manifest( self::manifest_file( $kind, $slug, $ver ), array( 'source' => 'uploaded vendor copy', 'files' => $b['files'], 'built' => time() ) ); ITSM_Utils::log( 'reference_uploaded', "$kind $slug $ver" ); return array( 'slug' => $slug, 'version' => $ver, 'files' => count( $b['files'] ) ); } public static function list_cached() { self::ensure_dirs(); $out = array(); foreach ( (array) glob( self::dir() . '/manifests/*.json.gz' ) as $f ) { $m = self::read_manifest( $f ); if ( ! is_array( $m ) ) { continue; } if ( preg_match( '/^(plugin|theme)-(.+)-([^-]+)-[a-f0-9]{20}\.json\.gz$/', basename( $f ), $mm ) ) { $size = 0; // checksums only: no package is kept $out[] = array( 'file' => basename( $f ), 'kind' => $mm[1], 'slug' => $mm[2], 'version' => $mm[3], 'source' => isset( $m['source'] ) ? $m['source'] : '', 'files' => isset( $m['files'] ) ? count( (array) $m['files'] ) : 0, 'built' => isset( $m['built'] ) ? $m['built'] : 0, 'size' => $size ); } } return $out; } /** Remove a cached manifest (and whatever an older release stored next to it). */ public static function delete_cached( $file ) { $name = basename( (string) $file ); if ( ! preg_match( '/^(plugin|theme)-[A-Za-z0-9._-]+-[a-f0-9]{20}\.json\.gz$/', $name ) ) { return false; // only a checksum list of this plugin, never the folder's protection files } $f = self::dir() . '/manifests/' . $name; if ( ! file_exists( $f ) ) { return false; } wp_delete_file( $f ); return true; } /** Remove cached packages for versions that are no longer installed (daily). */ public static function prune() { $installed = array(); if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } foreach ( get_plugins() as $file => $d ) { $installed[ 'plugin-' . dirname( $file ) . '-' . $d['Version'] ] = true; } foreach ( wp_get_themes() as $slug => $t ) { $installed[ 'theme-' . $slug . '-' . $t->get( 'Version' ) ] = true; } foreach ( self::list_cached() as $c ) { if ( 'uploaded vendor copy' !== $c['source'] && ! isset( $installed[ $c['kind'] . '-' . $c['slug'] . '-' . $c['version'] ] ) ) { self::delete_cached( $c['file'] ); } } } /** * Upgrade step: earlier releases kept packages on disk (extracted folders, later the zip archives) under * reference/packages and reference/uploads. Everything in those two folders is removed; the checksums of * an uploaded vendor package are kept, everything else is rebuilt on demand. Returns the number of items removed. */ public static function migrate_storage() { $n = 0; foreach ( array( self::dir() . '/packages', self::dir() . '/uploads' ) as $parent ) { if ( ! is_dir( $parent ) ) { continue; } foreach ( (array) glob( $parent . '/*' ) as $p ) { if ( is_dir( $p ) ) { self::rrmdir( $p ); } else { wp_delete_file( $p ); } $n++; } ITSM_Utils::delete_private_tree( $parent ); // folder of an earlier release, now empty } // Plain-text manifests of earlier releases: only a cache, rebuilt (compressed) when next needed. foreach ( (array) glob( self::dir() . '/manifests/*.json' ) as $f ) { wp_delete_file( $f ); $n++; } foreach ( (array) glob( self::dir() . '/manifests/*.json.gz' ) as $f ) { if ( ! preg_match( '/-[a-f0-9]{20}\.json\.gz$/', $f ) ) { // name of an earlier release $old = self::read_manifest( $f ); if ( is_array( $old ) && isset( $old['source'], $old['files'] ) && 'uploaded vendor copy' === $old['source'] && preg_match( '/^(plugin|theme)-(.+)-([^-]+)\.json\.gz$/', basename( $f ), $mm ) ) { self::write_manifest( self::manifest_file( $mm[1], $mm[2], $mm[3] ), array( 'source' => $old['source'], 'files' => $old['files'], 'built' => isset( $old['built'] ) ? (int) $old['built'] : time() ) ); } wp_delete_file( $f ); // a cached checksum list is fetched again when needed $n++; continue; } $m = self::read_manifest( $f ); if ( ! is_array( $m ) || ( ! isset( $m['zip'] ) && ! isset( $m['root'] ) ) ) { continue; } if ( isset( $m['source'], $m['files'] ) && 'uploaded vendor copy' === $m['source'] && ! isset( $m['root'] ) ) { self::write_manifest( $f, array( 'source' => $m['source'], 'files' => $m['files'], 'built' => isset( $m['built'] ) ? (int) $m['built'] : time() ) ); } else { wp_delete_file( $f ); } $n++; } if ( $n ) { ITSM_Utils::log( 'reference_storage', 'stored reference packages removed; only checksums are kept from now on' ); } return $n; } public static function rrmdir( $dir ) { ITSM_Utils::delete_private_tree( $dir ); // only inside the plugin's uploads folder } }