Changeset 5147 for sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-serve.php
- Timestamp:
- 03/13/2017 05:56:27 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-serve.php
r4728 r5147 13 13 class Serve { 14 14 15 const ZIP_DIR = '/tmp/plugin-zipfiles';16 17 15 public function __construct() { 18 16 try { 19 17 $request = $this->determine_request(); 20 18 21 // Serve & perhaps build if need be 22 $files = $this->get_files( $request ); 23 if ( ! file_exists( $files['zip'] ) ) { 24 $builder = new Builder( $request['slug'], $request['version'] ); 25 $builder->build(); 26 clearstatcache(); 27 } 28 29 $this->serve_zip( $files, $request ); 19 $this->serve_zip( $request ); 30 20 31 21 if ( $request['args']['stats'] ) { … … 34 24 35 25 } catch ( Exception $e ) { 36 $this->error( $e->getCode());26 $this->error(); 37 27 } 38 28 … … 51 41 52 42 if ( ! preg_match( "!^(?P<slug>[a-z0-9-]+)(.(?P<version>.+))?.zip$!i", $zip, $m ) ) { 53 throw new Exception( __METHOD__ . ": Invalid URL " );43 throw new Exception( __METHOD__ . ": Invalid URL." ); 54 44 } 55 45 … … 96 86 } 97 87 if ( ! $version ) { 98 throw new Exception( __METHOD__ . ": A version for $plugin_slug cannot be determined." , 404);88 throw new Exception( __METHOD__ . ": A version for $plugin_slug cannot be determined." ); 99 89 } 100 90 … … 121 111 122 112 if ( ! $post_id ) { 123 throw new Exception( __METHOD__ . ": A post_id for $plugin_slug cannot be determined." , 404);113 throw new Exception( __METHOD__ . ": A post_id for $plugin_slug cannot be determined." ); 124 114 } 125 115 … … 133 123 * @return array An array containing the files to use for the request, 'zip' and 'md5'. 134 124 */ 135 protected function get_file s( $request ) {125 protected function get_file( $request ) { 136 126 if ( empty( $request['version'] ) || 'trunk' == $request['version'] ) { 137 $zip = self::ZIP_DIR . "/{$request['slug']}/{$request['slug']}.zip";127 return "{$request['slug']}/{$request['slug']}.zip"; 138 128 } else { 139 $zip = self::ZIP_DIR . "/{$request['slug']}/{$request['slug']}.{$request['version']}.zip"; 140 } 141 $md5 = $zip . '.md5'; 142 143 return compact( 'zip', 'md5' ); 129 return "{$request['slug']}/{$request['slug']}.{$request['version']}.zip"; 130 } 144 131 } 145 132 … … 147 134 * Output a ZIP file with all headers. 148 135 * 149 * @param array $files {150 * Array of files for the request.151 *152 * @type string $zip The Zip file to serve.153 * @type string $md5 The MD5 file to use for the Content-MD5 header. Optional.154 * }155 136 * @param array $request The request array for the request. 156 137 */ 157 protected function serve_zip( $files, $request ) { 158 header( 'Content-Type: application/zip' ); 159 header( 'Content-Disposition: attachment; filename=' . basename( $files['zip'] ) ); 160 if ( !empty( $files['md5'] ) && ( $md5 = file_get_contents( $files['md5'] ) ) ) { 161 header( 'Content-MD5: ' . $md5 ); 162 } 163 164 // TODO: Accel Redirect allows for ZIP files to be cached on the LB's 165 // header('X-Accel-Redirect: ' . $accel_redirect ); 166 167 header( 'Content-Length: ' . filesize( $files['zip'] ) ); 168 readfile( $files['zip'] ); 138 protected function serve_zip( $request ) { 139 $zip = $this->get_file( $request ); 140 141 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" ); 147 } else { 148 header( 'Content-Type: text/plain' ); 149 echo "This is a request for $zip, this server isn't currently configured to serve zip files.\n"; 150 } 151 152 if ( function_exists( 'fastcgi_finish_request' ) ) { 153 fastcgi_finish_request(); 154 } 155 169 156 } 170 157 … … 225 212 226 213 /** 227 * Quit with an Error code. 228 * 229 * @param int $code The HTTP Error code, 404 or 503. 230 */ 231 protected function error( $code = 404 ) { 214 * Bail with a 404. 215 */ 216 protected function error() { 232 217 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; 233 218 $protocol .= ' '; 234 switch ( $code ) { 235 case 503: 236 header( $protocol . '503 Service Unavailable' ); 237 die( '503 Service Unavailable' ); 238 239 default: 240 case 404: 241 header( $protocol . '404 File not found' ); 242 die( '404 File not found' ); 243 } 219 220 header( $protocol . '404 File not found' ); 221 die( '404 file not found' ); 244 222 } 245 223
Note: See TracChangeset
for help on using the changeset viewer.