WordPress.org

Making WordPress.org

Changeset 3321


Ignore:
Timestamp:
06/09/2016 04:37:58 AM (3 years ago)
Author:
dd32
Message:

Plugin Directory: When building ZIP files, use a custom implementation of tempnam() to accomodate plugins which have longer filenames.
Previously a plugin with a slug and version combination exceeding 60 characters would cause a build failure.
See #1578

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-builder.php

    r3291 r3321  
    5858    public function build() {
    5959        try {
    60             // The name must have at least 1 `.` in it for CLI `zip` not to complain.
    61             $this->tmp_build_file = tempnam( dirname( $this->zip_file ), "tmp-{$this->slug}.{$this->version}" );
     60            $this->tmp_build_file = $this->generate_temporary_filename( dirname( $this->zip_file ), "tmp-{$this->slug}.{$this->version}", '.zip' );
    6261            $this->tmp_build_dir  = $this->tmp_build_file . '-files';
    6362            mkdir( $this->tmp_build_dir, 0777, true );
     
    7877            $this->cleanup();
    7978        }*/
     79    }
     80
     81    /**
     82     * Generates a temporary unique file in a given directory
     83     *
     84     * Performs a similar job to `tempnam()` with an added suffix and doesn't
     85     * cut off the $prefix at 60 characters.
     86     * As with `tempnam()` the caller is responsible for removing the temorarily file.
     87     *
     88     * Note: `strlen( $prefix . $suffix )` shouldn't exceed 238 characters.
     89     *
     90     * @param string $dir The directory to create the file in.
     91     * @param string $prefix The file prefix.
     92     * @param string $suffix The file suffix, optional.
     93     *
     94     * @return string Filename of unique temporary file.
     95     */
     96    protected function generate_temporary_filename( $dir, $prefix, $suffix = '' ) {
     97        $i = 0;
     98        do {
     99            $rand = uniqid();
     100            $filename = "{$dir}/{$prefix}-{$rand}{$i}{$suffix}";
     101        } while ( false === ($fp = @fopen( $filename, 'x' ) ) && $i++ < 50 );
     102
     103        if ( $i >= 50 ) {
     104            throw new Exception( __METHOD__ . ': Could not find unique filename.' );
     105        }
     106
     107        fclose( $fp );
     108
     109        return $filename;
    80110    }
    81111
Note: See TracChangeset for help on using the changeset viewer.