Changeset 6022
- Timestamp:
- 10/13/2017 08:56:56 AM (7 years ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/rebuild-zip.php
r5175 r6022 1 1 <?php 2 2 namespace WordPressdotorg\Plugin_Directory; 3 use WordPressdotorg\Plugin_Directory\Plugin_Directory; 3 4 use WordPressdotorg\Plugin_Directory\Tools\SVN; 4 5 … … 86 87 $zip_builder = new ZIP\Builder(); 87 88 89 $plugin_post = Plugin_Directory::get_plugin_post( $plugin_slug ); 90 if ( ! $plugin_post ) { 91 throw new Exception( "Could not locate plugin post" ); 92 } 93 $stable_tag = get_post_meta( $plugin_post->ID, 'stable_tag', true ) ?? 'trunk'; 94 88 95 // (re)Build & Commit 5 Zips at a time to avoid limitations. 89 96 foreach ( array_chunk( $versions, 5 ) as $versions_to_build ) { … … 91 98 $plugin_slug, 92 99 $versions_to_build, 93 "{$plugin_slug}: Rebuild triggered by " . php_uname('n' ) 100 "{$plugin_slug}: Rebuild triggered by " . php_uname('n' ), 101 $stable_tag 94 102 ); 95 103 } -
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php
r5841 r6022 196 196 $svn_revision_triggered ? 197 197 "{$plugin_slug}: ZIP build triggered by https://plugins.trac.wordpress.org/changeset/{$svn_revision_triggered}" : 198 "{$plugin_slug}: ZIP build triggered by " . php_uname('n') 198 "{$plugin_slug}: ZIP build triggered by " . php_uname('n'), 199 $stable_tag 199 200 ); 200 201 } catch( Exception $e ) { -
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-builder.php
r5246 r6022 15 15 const ZIP_SVN_URL = PLUGIN_ZIP_SVN_URL; 16 16 17 protected $zip_file = ''; 18 protected $tmp_build_dir = ''; 19 protected $tmp_dir = ''; 20 21 protected $slug = ''; 22 protected $version = ''; 23 protected $context = ''; 24 25 /** 26 * Generate a ZIP for a provided Plugin versions. 17 protected $zip_file = ''; 18 protected $checksum_file = ''; 19 protected $tmp_build_dir = ''; 20 protected $tmp_dir = ''; 21 22 protected $slug = ''; 23 protected $version = ''; 24 protected $context = ''; 25 protected $stable_tag = ''; 26 27 /** 28 * Generate a ZIP for a provided Plugin tags. 27 29 * 28 30 * @param string $slug The plugin slug. … … 30 32 * @param string $context The context of this Builder instance (commit #, etc) 31 33 */ 32 public function build( $slug, $versions, $context = '' ) {34 public function build( $slug, $versions, $context = '', $stable_tag = '' ) { 33 35 // Bail when in an unconfigured environment. 34 36 if ( ! defined( 'PLUGIN_ZIP_SVN_URL' ) ) { … … 36 38 } 37 39 38 $this->slug = $slug; 39 $this->versions = $versions; 40 $this->context = $context; 40 $this->slug = $slug; 41 $this->versions = $versions; 42 $this->context = $context; 43 $this->stable_tag = $stable_tag; 41 44 42 45 // General TMP directory … … 97 100 // Pull the ZIP file down we're going to modify, which may not already exist. 98 101 SVN::up( $this->zip_file ); 102 // This is done within the checksum generation function due to us not knowing the checksum filename until export_plugin(). 103 // SVN::up( $this->checksum_file ); 99 104 100 105 try { … … 104 109 105 110 $this->export_plugin(); 106 $this->fix_directory_dates(); 111 $this->fix_directory_dates(); 112 107 113 $this->generate_zip(); 114 115 $this->generate_checksums(); 116 108 117 $this->cleanup_plugin_tmp(); 109 118 … … 114 123 // Perform an SVN up to revert any changes made. 115 124 SVN::up( $this->zip_file ); 125 if ( $this->checksum_file ) { 126 SVN::up( $this->checksum_file ); 127 } 116 128 continue; 117 129 } 118 130 119 131 // Add the ZIP file to SVN - This is only really needed for new files which don't exist in SVN. 120 SVN::add( $this->zip_file ); 132 SVN::add( $this->zip_file ); 133 if ( $this->checksum_file ) { 134 SVN::add( $this->checksum_file ); 135 } 121 136 } 122 137 … … 146 161 147 162 /** 163 * Generates a JSON file containing the checksums of the files within the ZIP. 164 * 165 * In the event that a previous ZIP for this version exists, checksums for all versions of the file will be included. 166 */ 167 function generate_checksums() { 168 // Only enable this for the `exploit-scanner` plugin for the time being. 169 if ( 'exploit-scanner' != $this->slug ) { 170 return; 171 } 172 173 // Don't create checksums for trunk. 174 if ( ! $this->stable_tag || ( 'trunk' == $this->version && 'trunk' != $this->stable_tag && '' != $this->stable_tag ) ) { 175 return; 176 } 177 178 // Fetch the plugin headers 179 $plugin_data = false; 180 foreach ( glob( $this->tmp_build_dir . '/' . $this->slug . '/*.php' ) as $filename ) { 181 $plugin_data = get_plugin_data( $filename, false, false ); 182 183 if ( $plugin_data['Name'] && '' !== $plugin_data['Version'] ) { 184 break; 185 } 186 } 187 188 if ( ! $plugin_data || '' === $plugin_data['Version'] ) { 189 return; 190 } 191 192 $plugin_version = $plugin_data['Version']; 193 // Catch malformed version strings. 194 if ( basename( $plugin_version ) != $plugin_version ) { 195 return; 196 } 197 198 $this->checksum_file = "{$this->tmp_dir}/{$this->slug}/{$this->slug}.{$plugin_version}.checksums.json"; 199 200 // Checkout the Checksum file for this plugin version 201 SVN::up( $this->checksum_file ); 202 203 // Existing checksums? 204 $existing_json_checksum_file = file_exists( $this->checksum_file ); 205 206 $this->exec( sprintf( 207 'cd %s && find . -type f -print0 | sort -z | xargs -0 md5sum 2>&1', 208 escapeshellarg( $this->tmp_build_dir . '/' . $this->slug ) 209 ), $checksum_output, $return_value ); 210 211 if ( $return_value ) { 212 // throw new Exception( __METHOD__ . ': Checksum generation failed, return code: ' . $return_value, 503 ); 213 // For now, just silently bail. 214 return; 215 } 216 217 $checksums = array(); 218 foreach ( $checksum_output as $line ) { 219 list( $md5, $filename ) = preg_split( '!\s+!', $line ); 220 $filename = preg_replace( '!^./!', '', $filename ); 221 $checksums[ trim( $filename ) ] = trim( $md5 ); 222 } 223 224 $json_checksum_file = (object) array( 225 'plugin' => $this->slug, 226 'version' => $plugin_version, 227 'source_tag' => $this->version, 228 'zip' => basename( $this->zip_file ), 229 'checksums' => $checksums 230 ); 231 232 // If the checksum file exists already, merge it into this one. 233 if ( $existing_json_checksum_file ) { 234 $existing_json_checksum_file = json_decode( file_get_contents( $this->checksum_file ) ); 235 236 if ( $existing_json_checksum_file && ! empty( $existing_json_checksum_file->checksums ) ) { 237 foreach ( $existing_json_checksum_file->checksums as $file => $checksum_details ) { 238 239 if ( ! isset( $json_checksum_file->checksums[ $file ] ) ) { 240 // Deleted file, include it in checksums. 241 $json_checksum_file->checksums[ $file ] = $checksum_details; 242 243 } elseif ( $json_checksum_file->checksums[ $file ] != $checksum_details ) { 244 // Checksum has changed, include both in the resulting json file. 245 if ( is_array( $checksum_details ) ) { 246 $checksum_details[] = $json_checksum_file->checksums[ $file ]; 247 $json_checksum_file->checksums[ $file ] = $checksum_details; 248 } else { 249 $json_checksum_file->checksums[ $file ] = array( 250 $json_checksum_file->checksums[ $file ], 251 $checksum_details 252 ); 253 } 254 } 255 } 256 } 257 } 258 259 file_put_contents( $this->checksum_file, wp_json_encode( $json_checksum_file ) ); 260 } 261 262 /** 148 263 * Generates a temporary unique directory in a given directory 149 264 * … … 279 394 */ 280 395 public function invalidate_zip_caches( $versions ) { 281 // TODO: Implement PURGE 396 // TODO: Implement PURGE 282 397 return true; 283 398 if ( ! defined( 'PLUGIN_ZIP_X_ACCEL_REDIRECT_LOCATION' ) ) { -
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-serve.php
r5305 r6022 37 37 $zip = basename( parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) ); 38 38 39 $slug = false; 39 if ( ! preg_match( "!^(?P<slug>[a-z0-9-_]+)(\.(?P<version>.+?))?\.(?P<request_type>zip|checksums\.json)$!i", $zip, $m ) ) { 40 throw new Exception( __METHOD__ . ": Invalid URL." ); 41 } 42 43 $slug = strtolower( $m['slug'] ); 44 40 45 $version = 'trunk'; 41 42 if ( ! preg_match( "!^(?P<slug>[a-z0-9-_]+)(.(?P<version>.+))?.zip$!i", $zip, $m ) ) { 43 throw new Exception( __METHOD__ . ": Invalid URL." ); 44 } 45 46 $slug = strtolower( $m['slug'] ); 47 if ( isset( $m['version'] ) ) { 46 if ( isset( $m['version'] ) && '' !== $m['version'] ) { 48 47 $version = $m['version']; 49 48 } 50 51 49 if ( 'latest-stable' == $version ) { 52 50 $version = $this->get_stable_tag( $slug ); 51 } 52 53 if ( 'zip' == strtolower( $m['request_type'] ) ) { 54 $checksum_request = false; 55 } else { 56 $checksum_request = true; 57 58 // Checksum requests for 'trunk' are not possible. 59 if ( 'trunk' == $version ) { 60 throw new Exception( __METHOD__ . ": Checksum requests must include a version." ); 61 } 62 53 63 } 54 64 … … 56 66 'stats' => true, 57 67 ); 58 if ( isset( $_GET['stats'] ) ) { 68 69 if ( $checksum_request ) { 70 $args['stats'] = false; 71 72 } elseif ( isset( $_GET['stats'] ) ) { 59 73 $args['stats'] = (bool) $_GET['stats']; 74 60 75 } elseif ( isset( $_GET['nostats'] ) ) { 61 76 $args['stats'] = !empty( $_GET['nostats'] ); 62 77 } 63 78 64 return compact( 'zip', 'slug', 'version', 'args' );79 return compact( 'zip', 'slug', 'version', 'args', 'checksum_request' ); 65 80 } 66 81 … … 118 133 119 134 /** 120 * Returns the file s to usefor the request.135 * Returns the file to be served for the request. 121 136 * 122 137 * @param array $request The request object for the request. 123 * @return array An array containing the files to use for the request, 'zip' and 'md5'.138 * @return array The file to serve. 124 139 */ 125 140 protected function get_file( $request ) { 126 if ( empty( $request['version'] ) || 'trunk' == $request['version'] ) { 141 // Checksum requests must include a version 142 if ( $request['checksum_request'] ) { 143 return "{$request['slug']}/{$request['slug']}.{$request['version']}.checksums.json"; 144 145 } elseif ( empty( $request['version'] ) || 'trunk' == $request['version'] ) { 127 146 return "{$request['slug']}/{$request['slug']}.zip"; 147 128 148 } else { 129 149 return "{$request['slug']}/{$request['slug']}.{$request['version']}.zip"; 130 150 } 151 131 152 } 132 153 … … 137 158 */ 138 159 protected function serve_zip( $request ) { 139 $ zip= $this->get_file( $request );160 $file = $this->get_file( $request ); 140 161 141 162 if ( defined( 'PLUGIN_ZIP_X_ACCEL_REDIRECT_LOCATION' ) ) { 142 $zip_url = PLUGIN_ZIP_X_ACCEL_REDIRECT_LOCATION . $zip; 143 144 header( 'Content-Type: application/zip' ); 145 header( 'Content-Disposition: attachment; filename=' . basename( $zip ) ); 146 header( "X-Accel-Redirect: $zip_url" ); 163 $file_url = PLUGIN_ZIP_X_ACCEL_REDIRECT_LOCATION . $file; 164 165 if ( $request['checksum_request'] ) { 166 header( 'Content-Type: application/json' ); 167 } else { 168 header( 'Content-Type: application/zip' ); 169 header( 'Content-Disposition: attachment; filename=' . basename( $file ) ); 170 } 171 header( "X-Accel-Redirect: $file_url" ); 147 172 } else { 148 173 header( 'Content-Type: text/plain' ); 149 echo "This is a request for $ zip, this server isn't currently configured to serve zipfiles.\n";174 echo "This is a request for $file, this server isn't currently configured to serve files.\n"; 150 175 } 151 176
Note: See TracChangeset
for help on using the changeset viewer.