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

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
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.