<?php

define("CLI", (bool)(strcasecmp(php_sapi_name(), "cli") == 0));

ob_start();
ob_clean();
phpinfo();
define("PHPINFO", ob_get_contents());
ob_end_clean();

function pretty_output($info)
{
    $os = $info['server-config']['os'];
    $osFriendlyName = ( ($os=="lin") ? "Linux" : "Windows" );

    if (CLI)
    {
        echo "
Dinkey Pro/FD PHP Extension Helper
==================================

System Configuration
--------------------
Operating System: $osFriendlyName
PHP Version: {$info['server-config']['php-ver']}
Compiler: ". ( ($os == "win") ? $info['server-config']['compiler'] : "Not Applicable" ) ."
Thread Safety: ".(($info['server-config']['thread-safety'] == "ts") ? "Enabled" : "Disabled")."
Architecture: {$info['server-config']['arch']}-bit
PHP Extensions Directory: {$info['server-config']['ext-dir']}
PHP Configuration File: {$info['server-config']['php-ini-filename']}

Use the following PHP extension on this system:
  {$info['dp-ext-filename']}

Installation
------------
1. Lock {$info['dp-ext-filename']} with DinkeyAdd and copy the locked file to {$info['server-config']['ext-dir']}
2. Edit the {$info['server-config']['php-ini-filename']} file to include the following line:\n    extension={$info['dp-ext-filename']}
". (($info['server-config']['webserver'] != "") ? "3. Restart {$info['server-config']['webserver']}" : "") ."\n";

        return;
    }

    // Web-server output
    echo "
<!DOCTYPE html>
<html lang=\"en\">
<head>
    <title>Dinkey Pro/FD PHP Extension Helper</title>
</head>
<body>
    <h1>System Configuration</h1>
    <table>
        <tr><td>Operating System:</td><td>$osFriendlyName</td></tr>
        <tr><td>PHP Version:</td><td>{$info['server-config']['php-ver']}</td></tr>
        ". ( ($os == "win") ? "<tr><td>Compiler:</td><td>{$info['server-config']['compiler']}</td></tr>" : "" ) ."
        <tr><td>Thread Safety:</td><td>".(($info['server-config']['thread-safety'] == "ts") ? "Enabled" : "Disabled")."</td></tr>
        <tr><td>Architecture:</td><td>{$info['server-config']['arch']}-bit</td></tr>
        <tr><td>PHP Extensions Directory:</td><td>{$info['server-config']['ext-dir']}</td></tr>
        <tr><td>PHP Configuration File:</td><td>{$info['server-config']['php-ini-filename']}</td></tr>
    </table>
    <p>Use the following PHP extension on this system: <b>{$info['dp-ext-filename']}</b>.</p>
    <h1>Installation</h1>
    <ol>
        <li><p>Lock <b>{$info['dp-ext-filename']}</b> with DinkeyAdd and copy the locked file to <b>{$info['server-config']['ext-dir']}</b>.</p></li>
        <li><p>Edit the <b>{$info['server-config']['php-ini-filename']}</b> file to include the following line:<br>&nbsp;&nbsp;<b>extension={$info['dp-ext-filename']}</b></p></li>
        ". (($info['server-config']['webserver'] != "") ? "<li><p>Restart {$info['server-config']['webserver']}.</p></li>" : "") ."
    </ol>
</body>
</html>
    ";
}


$os         = "";
$version    = "";
$threadsafe = "";
$compiler   = "";
$arch       = "";

if (stristr(php_uname("s"), "linux") !== false)
    $os = "lin";
else if (stristr(php_uname("s"), "win") !== false)  // Windows can be given as "Windows NT", or "WINNT"... just search for "win"
    $os = "win";

$v = explode(".", PHP_VERSION);
$version = $v[0] . "." . $v[1];

$matches = array();
$match = (bool) preg_match("/php extension build[^,]+,([NTS]{2,3}),?(VC[0-9]+)?/i", PHPINFO, $matches);
if ($match)
{
    $threadsafe = strtolower($matches[1]);

    if ($os == "win")
        $compiler = strtolower($matches[2]);
}

$i = intval("9223372036854775807");
$s = strval($i);
if ($s == "9223372036854775807")
    $arch = "64";
else
    $arch = "32";

if ($os == "")
    die("Error, could not detect operating system.");
if ($threadsafe == "")
    die("Error, could not detect thread-safety setting.");
if (($os == "win") && ($version >= 5.3) && ($compiler == ""))
    die("Error, could not detect compiler version.");

$filename = (($os=="lin") ? "lib" : "") . "dpphp-$os$arch-$version-$threadsafe" . (($os=="win" && $version>=5.3) ? "-$compiler" : "") . "." . (($os=="win") ? "dll" : "so");

$phpIniFilename = get_cfg_var('cfg_file_path');
$extDir = ini_get("extension_dir");

$webserver = "";
if (isset($_SERVER['SERVER_SOFTWARE']))
{
    if (stripos($_SERVER['SERVER_SOFTWARE'], "iis") !== FALSE)
        $webserver = "Microsoft IIS";
    else if (stripos($_SERVER['SERVER_SOFTWARE'], "apache") !== FALSE)
        $webserver = "Apache";
    else
        $webserver = "your HTTP server software";
}

$info = array(
    'server-config' => array(
        'os' => $os,
        'php-ver' => $version,
        'webserver' => $webserver,
        'compiler' => $compiler,
        'thread-safety' => (bool)($threadsafe == "ts"),
        'arch' => $arch,
        'ext-dir' => $extDir,
        'php-ini-filename' => $phpIniFilename
    ),

    'dp-ext-filename' => $filename,
);

pretty_output($info);

?>
