BP_GOPP_Image_Editor_GS::gs_cmd_win()

Tries to determine the Windows path of the Ghostscript executable.

Description

Return

(false|string) Returns false if can't determine path, else path.

Source

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

	protected static function gs_cmd_win() {
		$win_path = false;

		// Try using REG QUERY to access the registry.
		// Do one test query first to see if it works.
		$cmd        = 'REG QUERY HKEY_LOCAL_MACHINE\\SOFTWARE 2>&1';
		$return_var = -1;
		$output     = array();
		exec( $cmd, $output, $return_var );
		if ( 0 === $return_var && is_array( $output ) ) {
			// Might work.
			$products = array(
				'GPL Ghostscript',
				'GNU Ghostscript',
				'AFPL Ghostscript',
				'Aladdin Ghostscript',
			);
			foreach ( $products as $product ) {
				$cmd    = sprintf( 'REG QUERY "HKEY_LOCAL_MACHINE\\SOFTWARE\\%s" /S 2>&1', $product );
				$output = array();
				exec( $cmd, $output, $return_var );
				if ( 0 === $return_var && is_array( $output ) ) {
					// Find latest version.
					$best_match  = '';
					$highest_ver = 0;
					foreach ( $output as $out ) {
						$out = trim( $out );
						if ( preg_match( '/^GS_DLL[\t ]+REG_SZ[\t ]+(.+)\\\\gs([0-9.]+)\\\\bin\\\\gsdll(64|32)\.dll$/', $out, $matches ) ) {
							$ver = (float) $matches[2];
							if ( $highest_ver < $ver ) {
								$possible_path = $matches[1] . '\\gs' . $matches[2] . '\\bin\\gswin' . $matches[3] . 'c.exe';
								if ( self::test_gs_cmd( $possible_path ) ) {
									$best_match  = $possible_path;
									$highest_ver = $ver;
								}
							}
						}
					}
					if ( $best_match ) {
						$win_path = $best_match;
						break;
					}
				}
			}
		}

		if ( ! $win_path ) {
			// Try GSC environment variable. TODO: Is this still used?
			if ( ! empty( $_SERVER['GSC'] ) && is_string( $_SERVER['GSC'] ) && self::test_gs_cmd( $_SERVER['GSC'] ) ) {
				$win_path = $_SERVER['GSC'];
			}
		}

		if ( ! $win_path ) {
			// Try default install location.
			$program_dirs = array();
			if ( ! empty( $_SERVER['ProgramW6432'] ) && is_string( $_SERVER['ProgramW6432'] ) ) {
				$program_dirs[] = stripslashes( $_SERVER['ProgramW6432'] );
			}
			if ( ! empty( $_SERVER['ProgramFiles'] ) && is_string( $_SERVER['ProgramFiles'] ) ) {
				$program_dirs[] = stripslashes( $_SERVER['ProgramFiles'] );
			}
			if ( ! empty( $_SERVER['ProgramFiles(x86)'] ) && is_string( $_SERVER['ProgramFiles(x86)'] ) ) {
				$program_dirs[] = stripslashes( $_SERVER['ProgramFiles(x86)'] );
			}
			if ( $program_dirs ) {
				$program_dirs = array_unique( $program_dirs );
			} else {
				$program_dirs[] = 'C:\\Program Files';
			}
			foreach ( $program_dirs as $program_dir ) {
				$gs_dir = glob( $program_dir . '\\gs\\gs*', GLOB_NOESCAPE );
				if ( $gs_dir ) {
					// Find latest version.
					$best_match  = '';
					$highest_ver = 0;
					foreach ( $gs_dir as $gs_entry ) {
						if ( preg_match( '/[0-9]+\.[0-9]+$/', $gs_entry, $matches ) ) {
							$ver = (float) $matches[0];
							if ( $highest_ver < $ver ) {
								if ( @ is_executable( $gs_entry . '\\bin\\gswin64c.exe' ) && self::test_gs_cmd( $gs_entry . '\\bin\\gswin64c.exe' ) ) {
									$best_match  = $gs_entry . '\\bin\\gswin64c.exe';
									$highest_ver = $ver;
								} elseif ( @ is_executable( $gs_entry . '\\bin\\gswin32c.exe' ) && self::test_gs_cmd( $gs_entry . '\\bin\\gswin32c.exe' ) ) {
									$best_match  = $gs_entry . '\\bin\\gswin32c.exe';
									$highest_ver = $ver;
								}
							}
						}
					}
					if ( $best_match ) {
						$win_path = $best_match;
						break;
					}
				}
			}
		}

		// Resort to PATH.
		if ( ! $win_path && self::test_gs_cmd( 'gswin64c.exe' ) ) {
			$win_path = 'gswin64c.exe';
		}
		if ( ! $win_path && self::test_gs_cmd( 'gswin32c.exe' ) ) {
			$win_path = 'gswin32c.exe';
		}

		return $win_path;
	}

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.