Location: PHPKode > projects > Web Application Gateway > wagateway/init.php
<?

/*************************************************************************************

    COPYRIGHT NOTICE

    This copyright notice must appear at the top of all scripts which are part of
    the Web Application Gateway package.

    Copyright (C) 2001-2008 Gregory Engel
    
    All rights reserved
    
    8547 E Arapahoe Rd, #J-504
    Greenwood Village, CO  80112 USA
    hide@address.com

    This program is free software; you can redistribute it and/or modify it under
    the terms of the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

    The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
    A copy is included with the WAG package and is found in the text file gpl.txt

    You should have received a copy of the GNU General Public License (gpl.txt, gpl.html)
    along with the WAG distribution package; if not, the GNU General Public License can
    be found at http://www.gnu.org/copyleft/gpl.html, or by writing to:
    
    Free Software Foundation, Inc.
    59 Temple Place - Suite 330
    Boston, MA  02111-1307, USA.

    This script is part of the Web Application Gateway (WAG) Accounting Application 
    Project. The WAG software is free, subject to the terms of the GNU General Public 
    License as published by the Free Software Foundation, either version 2 of the License, 
    or (at your option) any later version.
    
    $Revision: 56 $
    $LastChangedDate: 2008-01-16 09:52:29 -0700 (Wed, 16 Jan 2008) $
    $LastChangedBy: gpeangel $
 
*************************************************************************************/

// Verify directory path to the WAG installation directory.  WAG may be installed 
// in the web server document root or in a subdirectory to the document root.

session_start();

$document_root = $_SERVER["DOCUMENT_ROOT"];
$server_name = $_SERVER["SERVER_NAME"];
$install_dir = "/";

if (!is_file($document_root."/main.php") && !is_file($document_root."/js/main.js") && !is_file($document_root."/admin/index.php"))
    {
    
    // Looks like WAG was installed in a subdirectory to the web server document root
    
    $ary_doc_root = explode("/", $document_root);
    $ary_script_path = explode("/", dirname($_SERVER['SCRIPT_FILENAME']));
    
    for ($i = count($ary_doc_root); $i <= count($ary_script_path); $i++)
        {
        
        $install_dir .= $ary_script_path[$i];
        
        if (is_file($document_root.$install_dir."/main.php") && is_file($document_root.$install_dir."/js/main.js") && is_file($document_root.$install_dir."/admin/index.php"))
            {
            
            // This appears to be the root directory for WAG
            
            $install_dir .= "/";
            
            break;

            }
        } 
    }

$wag_install_dir = $document_root.$install_dir;
$wag_web_root = $server_name.$install_dir;

$_SESSION["WAGATEWAY"]["INSTALL_DIR"] = $wag_install_dir;
$_SESSION["WAGATEWAY"]["WEB_ROOT"] = $wag_web_root;

if (file_exists($_SESSION["WAGATEWAY"]["INSTALL_DIR"]."config.php"))
    {

    require_once($_SESSION["WAGATEWAY"]["INSTALL_DIR"]."config.php");
    
    if ($config_integrity_check != 1)
        {
        
        // There is a problem with the config.php file, perhaps the install configuration was interupted.
        
        header("Location: http://".$_SESSION["WAGATEWAY"]["WEB_ROOT"]."install/install.php?inst_step=1");
        }
    else
        {
        $_SESSION["WAGATEWAY"]["DB_SERVER"] = $wag_db_server;
        $_SESSION["WAGATEWAY"]["DB_DATABASE"] = $wag_db_database;
        $_SESSION["WAGATEWAY"]["DB_USERNAME"] = $wag_db_username;
        $_SESSION["WAGATEWAY"]["DB_PASSWORD"] = $wag_db_password;
        $_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"] = $wag_db_table_prefix;
        $_SESSION["WAGATEWAY"]["DB_CONNECTION"] = mysql_connect($_SESSION["WAGATEWAY"]["DB_SERVER"], $_SESSION["WAGATEWAY"]["DB_USERNAME"], $_SESSION["WAGATEWAY"]["DB_PASSWORD"]) or die ($query."<br><br>".mysql_errno().": ".mysql_error()."<br>");
        $_SESSION["WAGATEWAY"]["DB_SELECTED_DATABASE"] = mysql_select_db($_SESSION["WAGATEWAY"]["DB_DATABASE"], $_SESSION["WAGATEWAY"]["DB_CONNECTION"]) or die ($query."<br><br>".mysql_errno().": ".mysql_error()."<br>");
        
        $query = "SELECT * FROM ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."WAGConfig";
        $result = mysql_query($query, $_SESSION["WAGATEWAY"]["DB_CONNECTION"]);
        
        if ($result)
            {
            
            // Load gateway settings
            
            $row = mysql_fetch_array($result);
            
            $_SESSION["WAGATEWAY"]["WAG_VERSION"] = $row["conf_wag_version"];
            $_SESSION["WAGATEWAY"]["SITE_NAME"] = $row["conf_site_name"];
            $_SESSION["WAGATEWAY"]["SERVER_ROOT_DIR"] = $row["conf_server_root_dir"];
            $_SESSION["WAGATEWAY"]["REQUIRE_SSL"] = $row["conf_require_ssl"];
            $_SESSION["WAGATEWAY"]["DEMO_MODE"] = $row["conf_demo_mode"];
            $_SESSION["WAGATEWAY"]["DEBUG_ON"] = $row["conf_debug_on"];
            $_SESSION["WAGATEWAY"]["SESSION_LIFESPAN"] = ($row["conf_session_lifespan"]);
            $_SESSION["WAGATEWAY"]["PASSWORD_LIFESPAN"] = $row["conf_password_exp"];
            $_SESSION["WAGATEWAY"]["PASSWORD_LENGTH1"] = $row["conf_password_length1"];
            $_SESSION["WAGATEWAY"]["PASSWORD_LENGTH2"] = $row["conf_password_length2"];
            $_SESSION["WAGATEWAY"]["PASSWORD_ALPHANUMERIC"] = $row["conf_password_alphanumeric"];
            $_SESSION["WAGATEWAY"]["SYNC_EMAIL"] = $row["conf_sync_email"];
            $_SESSION["WAGATEWAY"]["EMAIL_DB_SERVER"] = $row["conf_email_db_server"];
            $_SESSION["WAGATEWAY"]["EMAIL_DB_NAME"] = $row["conf_email_db_name"];
            $_SESSION["WAGATEWAY"]["EMAIL_TABLE_NAME"] = $row["conf_email_table_name"];
            $_SESSION["WAGATEWAY"]["EMAIL_USERID_FIELD_NAME"] = $row["conf_email_userid_field_name"];
            $_SESSION["WAGATEWAY"]["EMAIL_USERID_WITH_DOMAIN"] = $row["conf_email_userid_with_domain"];
            $_SESSION["WAGATEWAY"]["EMAIL_PASSWORD_FIELD_NAME"] = $row["conf_email_password_field_name"];
            $_SESSION["WAGATEWAY"]["EMAIL_FIELD_ENCRYPT"] = $row["conf_email_field_encrypt"];
            $_SESSION["WAGATEWAY"]["EMAIL_DB_USER_NAME"] = $row["conf_email_db_user_name"];
            $_SESSION["WAGATEWAY"]["EMAIL_DB_USER_PASSWORD"] = $row["conf_email_db_user_password"];
            
            $_SESSION["WAGATEWAY"]["SESSION_START"] = time();
            
            // If session lifespan is zero, set value to 365 days
            
            if ($_SESSION["WAGATEWAY"]["SESSION_LIFESPAN"] == 0)
                {
                $_SESSION["WAGATEWAY"]["SESSION_LIFESPAN"] = 31536000; 
                }
            
            if ($_SESSION["WAGATEWAY"]["REQUIRE_SSL"] == 1)
                {
                $_SESSION["WAGATEWAY"]["PROTOCOL"] = "https://";
                }
            else
                {
                $_SESSION["WAGATEWAY"]["PROTOCOL"] = "http://";
                }
            
            }
        
        $query = "SELECT * FROM ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."Users WHERE usr_id = 1";
        $result = mysql_query($query, $_SESSION["WAGATEWAY"]["DB_CONNECTION"]);
        
        if ($result)
            {
            
            $row = mysql_fetch_array($result);
            
            $_SESSION["WAGATEWAY"]["ADMIN_EMAIL"] = $row["usr_email"];
            
            }
            
        if ($_SESSION["WAGATEWAY"]["REQUIRE_SSL"] == 1 && $_SERVER["SERVER_PORT"] != 443)
            {
            
            // Force access via https
            
            if (isset($_SESSION["WAGATEWAY"]["VALID_USER"]))    
                {
                header("Location: ".$_SESSION["WAGATEWAY"]["PROTOCOL"].$_SESSION["WAGATEWAY"]["WEB_ROOT"]."/main.php");
                }
            else
                {
                header("Location: ".$_SESSION["WAGATEWAY"]["PROTOCOL"].$_SESSION["WAGATEWAY"]["WEB_ROOT"]."/index.php");
                }
            }
        }
    }
else
    {
    
    // No config.php file means WAG needs to be configured.
        
    header("Location: http://".$_SESSION["WAGATEWAY"]["WEB_ROOT"]."install/install.php?inst_step=1");
    }

// Set token for cross-site request forgery check

$_SESSION["WAGATEWAY"]["CSRF_TOKEN"] = md5(uniqid(rand(), true));

?>
Return current item: Web Application Gateway