<?php
// +----------------------------------------------------------------+
// | step0.php														|
// |																|
// | Function: Check all dependencies								|
// +----------------------------------------------------------------+
// | Source-code (C) CalaCode.com 1998-2008 . All rights reserved	|
// | See http://calacode.com/license.ehtml for license agreement	|
// +----------------------------------------------------------------+
// | Date: May 2008													|
// +----------------------------------------------------------------+


// Make sure we are included from install/index.php, exit if not
if (!defined('ATMAIL_INSTALL_SCRIPT'))
{
	// link to installer
	die("You cannot request this file directly, please use <a href=\"index.php\">the installer</a>");
}

$errors = array();

// If the form from Stage 1 has been submitted
// we need to process it
if (isset($_POST['submit']))
{
	header('Location: index.php?step=1');
}

// Check for required php extensions

   $ext = array('req' => array(), 'opt' => array());

    // Check for session support
    if (!function_exists('session_start'))
        $ext['req'][] = 'session';

    // Check for PCRE
    if (!defined('PREG_PATTERN_ORDER'))
        $ext['req'][] = 'pcre';

    // Check for mysql
    if (!defined('MYSQL_NUM'))
        $ext['opt'][] = 'mysql';

    if (!extension_loaded('sqlite')) {
    	$ext['opt'][] = 'sqlite';
    }
    
    // Check for LDAP
    if (!defined('LDAP_DEREF_NEVER'))
        $ext['opt'][] = 'ldap';

	// Check Mbstring
	if(!extension_loaded('mbstring')) {
    	$ext['opt'][] = 'mbstring';
		$_SESSION['pref']['allow_utf7_folders'] = '0';
	} else	{
		$_SESSION['pref']['allow_utf7_folders'] = '1';
	}
    
    if(!function_exists('iconv')) {
    	$ext['opt'][] = 'iconv';
		$_SESSION['pref']['iconv'] = '0';
	} else	{
		$_SESSION['pref']['iconv'] = '1';
	}

	if(ini_get('safe_mode'))	{
		$ext['req'][] = 'safe_mode';
	}

	if(!ini_get('register_globals'))
		$ext['ini'][] = 'register_globals';

	if(!ini_get('file_uploads'))
		$ext['ini'][] = 'file_uploads';

	if(!ini_get('magic_quotes_gpc'))
		$ext['ini'][] = 'magic_quotes_gpc';

	// Get the filesize
	$size = ini_get('upload_max_filesize');
	$size = str_replace('M', '', $size);

	if(ini_get('upload_max_filesize') < 16)	{
		$ext['ini'][] = 'upload_max_filesize';
		$vars['upload_max_filesize'] = ini_get('upload_max_filesize');
	}

	// Get the post max size
	$size = ini_get('post_max_size');
	$size = str_replace('M', '', $size);

	if(ini_get('post_max_size') < 16)	{
		$ext['ini'][] = 'post_max_size';
		$vars['post_max_size'] = ini_get('post_max_size');
	}
	
	// check for demo version and ionCube loader
	if(preg_match('/demo/i', $_SESSION['pref']['version']) && extension_loaded('ionCube Loader'))
	{
		$vars['ionext'] = '1';
	}
	elseif (preg_match('/demo/i', $_SESSION['pref']['version']) && !extension_loaded('ionCube Loader'))
	{

		if (strlen(ini_get('enable_dl')) == 0) {
		    $ext['req'][] = 'ioncube';
        } else {
        	$ext['opt'][] = 'ioncube';
        }

	    $vars['version'] = 'demo';

	    ob_start();
	    phpinfo(INFO_GENERAL);
	    $php_info = ob_get_contents();
	    ob_end_clean();

	    foreach (split("\n",$php_info) as $line) {
	        if (eregi('command',$line)) {
	            continue;
	        }


	        if (eregi("configuration file.*(</B></td><TD ALIGN=\"left\">| => |v\">)([^ <]*)(.*</td.*)?",$line,$match)) {
	            $php_ini_path = $match[2];

	            if (!@file_exists($php_ini_path)) {
	                $php_ini_path = '';
	            }
	        }


	        if (strlen(ini_get('enable_dl')) == 0) {
	            $ext['ini'][] = 'enable_dl';
	        }
	    }

	    $oc = strtolower(substr(php_uname(),0,3));
		if(preg_match('/x86_64/', php_uname()) && $oc == 'lin')
		$oc = 'lin64';

        $vars['ioncube_lib'] = realpath('../ioncube/'.$oc.'/ioncube_loader_'.$oc.'_'.substr(phpversion(),0,3).(($oc=='win')?'.dll':'.so'));
	    $vars['php_ini_path'] = $php_ini_path;

    }



    if (count($ext['req']) || in_array('mysql', $ext['opt']) && in_array('sqlite', $ext['opt']))
    {
		// Don't let the user proceed
		if ((!$var['version'] == 'demo' && !in_array('ioncube', $ext['req'])) || ($vars['version'] == 'demo')) {
		    $vars['error'] = '1';
		}

        //$var['output'] = parse('html/extensions_required.html', $ext);
        //echo parse('html/english/template.html', $var);
        //exit;
    }

$vars['output'] = parse("$htmlPath/step0.phtml", array_merge($errors, $vars, $ext));

// Preselect the language if already known e.g. we have
// returned from a later step
if ($lang)
	$vars['output'] = str_replace("<option value=\"$lang\">", "<option value=\"$lang\" selected>", $vars['output']);

?>