<?php
/*!
 * Apache 初始化脚本
**/
anmp_log('EXEC FILE   [AnmpReload.apache.php]');


// 注入配置变量
$_ENV['ANMP_HTTPD_DIRS'] = glob(ANMP_ROOT.'/serv/httpd/httpd-*.*.*-win*', GLOB_ONLYDIR);
$_ENV['ANMP_HTTPD_VERS'] = array();

$_ENV['ANMP_PHP_DIRS']   = glob(ANMP_ROOT.'/serv/php/php-*.*.*-win*', GLOB_ONLYDIR);
$_ENV['ANMP_PHP_VERS']   = array();

$_ENV['ANMP_HTTPD_PHPS'] = array();

// print_r($_ENV['ANMP_HTTPD_DIRS']);
// print_r($_ENV['ANMP_PHP_DIRS']);


// 解析HTTPD配置文件
foreach ($_ENV['ANMP_HTTPD_DIRS'] as $dir)
{
    $filename  = "{$dir}/conf/httpd.conf";
    if (is_file($filename)) {
        $content = file_get_contents($filename);
        $_ENV['ANMP_HTTPD_VERS'][basename($dir)] = $dir;
    } else {
        continue;
    }

    $find_str = '#([A-Z][:][\w\.\-_/]+)/(data|serv|www)/([\w\.\-/]+)#u';
    $repl_str = ANMP_ROOT . "/$2/$3";
    $content  = preg_replace($find_str, $repl_str, $content);
	
	// preg_match_all($find_str, $content, $match);
    // print_r($match);
    // exit;
	
	if (!is_file($filename . '.bak')) {
        copy($filename, $filename . '.bak');
    }
    file_put_contents($filename, $content);
}

// 解析PHP配置文件
foreach ($_ENV['ANMP_PHP_DIRS'] as $dir)
{
    $filename1  = "{$dir}/php-apache2handler.ini";
    $filename2  = "{$dir}/php-nginx2handler.ini";
    if (is_file($filename1)) {
        $filename = $filename1;
        $content  = file_get_contents($filename);
        $_ENV['ANMP_PHP_VERS'][basename($dir)] = $dir;
    } elseif (is_file($filename2)) {
        $filename = $filename2;
        $content  = file_get_contents($filename);
    } else {
        continue;
    }

    $find_str = '#([A-Z][:][\w\.\-_/]+)/(data|serv|www)/([\w\.\-/]+)#u';
    $repl_str = ANMP_ROOT . "/$2/$3";
    $content  = preg_replace($find_str, $repl_str, $content);

    if (!is_file($filename . '.bak')) {
        copy($filename, $filename . '.bak');
    }
    file_put_contents($filename, $content);
}

// print_r($_ENV['ANMP_HTTPD_VERS']);
// print_r($_ENV['ANMP_PHP_VERS']);


// 处理兼容性
foreach ($_ENV['ANMP_HTTPD_VERS'] as $httpd_ver => $httpd_dir)
{
    foreach ($_ENV['ANMP_PHP_VERS'] as $php_ver => $php_dir)
    {
        $php_vers = explode('-', $php_ver);
        if (false !== strpos($httpd_ver, 'httpd-2.2.') && is_file($php_dir.'/php5apache2_2.dll')) {
            $key = 'Apache22-php'.$php_vers[1];
        } elseif (false !== strpos($httpd_ver, 'httpd-2.4.') && is_file($php_dir.'/php5apache2_4.dll')){
            $key = 'Apache24-php'.$php_vers[1];
        } else {
            continue;
        }

        $_ENV['ANMP_HTTPD_PHPS'][$key] = array(
            array($httpd_ver, $httpd_dir),
            array($php_ver, $php_dir)
        );
    }
}


// 生成菜单项
$apacheVerTextA = $apacheVerTextB = '';
foreach ($_ENV['ANMP_HTTPD_PHPS'] as $apache_ver => $apache_arr)
{

    $versionA = explode('-', $apache_arr[0][0]);
    $versionB = explode('-', $apache_arr[1][0]);
    $apache_bin   = "%WRoot%/serv/httpd/{$apache_arr[0][0]}/bin/httpd.exe";
    $apache_desc  = "AnmpApache/{$versionA[1]} (". ucfirst($versionA[2]) .") PHP/$versionB[1]";
    $apache_glyph = ($apache_ver === $_ENV['ANMP_CONF']['apache_vers']) ? " Glyph: 13;\n" : "\n";

    if ($apache_ver === $_ENV['ANMP_CONF']['apache_vers']) {
        $_ANMP_MENU_TMPL_STR = str_replace('-Apache-Server-Description-', $apache_desc, $_ANMP_MENU_TMPL_STR);
    }

    $apacheVerTextA .= "    Type: item; Caption: \"{$apache_ver}\"; Action: multi; Actions: MultiAction.Switch_{$apache_ver}; {$apache_glyph}\n";
    $apacheVerTextB .= "[MultiAction.Switch_{$apache_ver}]\n"
        // 卸载旧服务
        .'    Action: service; Service: AnmpApacheServer; ServiceAction: stop; Flags: waituntilterminated ignoreerrors;'."\n"
        .'    Action: closeservices; Flags: ignoreerrors;'."\n"
        .'    Action: run; FileName: "SC"; Parameters: "DELETE AnmpApacheServer"; ShowCmd: hidden; Flags: waituntilterminated ignoreerrors;'."\n"
        .'    Action: resetservices;'."\n"

        // 安装新服务
        .'    Action: run; FileName: "'. $apache_bin .'"; Parameters: "-k install -n AnmpApacheServer"; ShowCmd: hidden; Flags: waituntilterminated;'."\n"
        .'    Action: run; FileName: "SC"; Parameters: "CONFIG AnmpApacheServer start= '. $_ENV['ANMP_CONF']['apache_type'] .'"; ShowCmd: hidden; Flags: waituntilterminated;'."\n"
        .'    Action: run; FileName: "SC"; Parameters: "DESCRIPTION AnmpApacheServer ""'. $apache_desc .'"""; ShowCmd: hidden; Flags: waituntilterminated;'."\n"
        .'    Action: resetservices;'."\n"

        // 切换新服务
        .'    Action: run; FileName: "%WBPHPExe%"; Parameters: "AnmpSwitch.php Apache '. $_ENV['ANMP_CONF']['apache_vers'] .' '. $apache_ver .' '. $apache_arr[0][0] .' '. $apache_arr[1][0] .'"; WorkingDir: "%WRoot%/bin/AeTrayMenu/"; Flags: waituntilterminated;'."\n"
        .'    Action: run; FileName: "%WBPHPExe%"; Parameters: "AnmpReload.php"; WorkingDir: "%WRoot%/bin/AeTrayMenu/"; Flags: waituntilterminated;'."\n"
        .'    Action: service; Service: AnmpApacheServer; ServiceAction: startresume; Flags: ignoreerrors;'."\n"
        .'    Action: readconfig;'."\n";
}

// 替换菜单项
$apacheVerTextA .= "    Type: separator;\n".'    Type: item; Caption: "Get more..."; Action: run; FileName: "explorer.exe"; Parameters: "http://dev.osdu.net/p/anmpserver/?anmp_addons_apache";' . "\n";
$_ANMP_MENU_TMPL_STR = str_replace(';ANMP_APACHE_VERSION_MENU',   $apacheVerTextA, $_ANMP_MENU_TMPL_STR);
$_ANMP_MENU_TMPL_STR = str_replace(';ANMP_APACHE_VERSION_SWITCH', $apacheVerTextB, $_ANMP_MENU_TMPL_STR);


// print_r($_ENV['ANMP_HTTPD_PHPS']);
// print_r($_ENV);


//END