BP_GOPP_Image_Editor_GS::gs_valid( string $file, bool $no_read_check = false )

Checks that file is local, doesn’t have a funny name and is a PDF.

Description

Parameters

$file

(Required) File path.

$no_read_check

(Optional) If true then doesn't open & read file to check existence and magic bytes.

Default value: false

Return

(bool|String) Returns true if valid; returns error message string if invalid.

Source

File: bp-document/classes/class-bp-gopp-image-editor-gs.php

	protected static function gs_valid( $file, $no_read_check = false ) {
		// Loading from URL not currently supported.
		if ( preg_match( '|^https?://|', $file ) ) {
			return __( 'Loading from URL not supported.', 'buddyboss' );
		}

		// Check filename can't be interpreted by Ghostscript as special - see https://ghostscript.com/doc/9.20/Use.htm#Options
		if ( preg_match( '/^[@|%-]/', $file ) ) {
			return __( 'Unsupported file name.', 'buddyboss' );
		}

		// Check for suspect chars in base filename - same as $special_chars in sanitize_file_name() with ctrls, space and del added
		// but (for BC with common older uploads) with "+" removed - see #16226 for its addition (along with "%") Oct 2015.
		if ( preg_match( '/[?\[\]\/\\\\=<>:;,\'"&$#*()|~`!{}%\x00-\x20\x7f]/', wp_basename( $file ) ) ) {
			return __( 'Unsupported file name.', 'buddyboss' );
		}

		if ( $no_read_check ) {
			return true;
		}

		// Check existence & magic bytes.
		$fp = @ fopen( $file, 'rb' );
		if ( false === $fp ) {
			return __( 'File doesn&#8217;t exist?', 'buddyboss' );
		}
		$magic_bytes = fread( $fp, 10 ); // Max 10 chars: "%PDF-N.NN" plus optional initial linefeed.
		fclose( $fp );
		// This is a similar test to that done by libmagic, but more strict on version format by insisting it's "0." or "1." followed by 1 or 2 numbers.
		if ( ! preg_match( '/^\n?%PDF-[01]\.[0-9]{1,2}/', $magic_bytes ) ) {
			do_action( 'gopp_error', __CLASS__, __FUNCTION__, __LINE__, compact( 'file', 'magic_bytes' ) );
			return __( 'File is not a PDF.', 'buddyboss' );
		}

		return true;
	}

Changelog

Changelog
Version Description
BuddyBoss 1.4.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.