Location: PHPKode > projects > Web Application Gateway > wagateway/admin/apps.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) 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: 41 $
    $LastChangedDate: 2008-01-14 14:07:02 -0700 (Mon, 14 Jan 2008) $
    $LastChangedBy: gpeangel $
 
*************************************************************************************/

require("../common/access.php");
require("../common/dbconnect.php");
require_once("../common/functions.php");

// Cycle through the available applications and match them against those in the
// Applications table.  If any are found in the database that don't exist in
// the apps directory, delete orphaned applications from the database.  This would
// include removing references to Users and Groups.  If they exist in the apps
// directory, but not in the Applicaitions table, insert an application record.

$ary_apps = readFolder("../apps");

$lng_counter = 0;
$ary_apps_info = array();

foreach ($ary_apps as $ary_app)
    { 
        
    include($ary_app[0]);
    
    $ary_apps_info[$lng_counter][] = $app_name;
    $ary_apps_info[$lng_counter][] = $app_guid;
    $ary_apps_info[$lng_counter][] = $ary_app[1];
    $ary_apps_info[$lng_counter][] = $app_description;
    
    $lng_counter = $lng_counter + 1;
    } 

sort($ary_apps_info);

$query = "SELECT app_id, app_guid, app_location FROM ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."Applications";
$result = mysql_query($query, $db_conn) or die (DBError($_SERVER["PHP_SELF"]."\n\n".$query."\n\n".mysql_errno().": ".mysql_error()));

$row_count = mysql_num_rows($result);

for ($i = 0; $i < $row_count; $i++)
    {
    $row = mysql_fetch_array($result);
    
    $bln_found = 0;
    
    for ($j = 0; $j < count($ary_apps_info); $j++)
        {
        
        // Collect only those appllications that are not orphaned, i.e. exist 
        // on the server in the apps subdirectory
        if (($row["app_guid"] == $ary_apps_info[$j][1]) && ($row["app_location"] == $ary_apps_info[$j][2]))
            {
            $bln_found = 1;
            }
        }
    
    if ($bln_found == 0)
        {
        // Remove orphaned application from database, i.e. have an apps record 
        // in the database but don't exist on the server in the apps subdirectory
        $query = "DELETE FROM ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."AppGroups WHERE ag_app_id = ".$row[0];
        $result_del = mysql_query($query, $db_conn) or die (DBError($_SERVER["PHP_SELF"]."\n\n".$query."\n\n".mysql_errno().": ".mysql_error()));
        
        $query = "DELETE FROM ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."UserApps WHERE ua_app_id = ".$row[0];
        $result_del = mysql_query($query, $db_conn) or die (DBError($_SERVER["PHP_SELF"]."\n\n".$query."\n\n".mysql_errno().": ".mysql_error()));
        
        $query = "DELETE FROM ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."Applications WHERE app_id = ".$row[0];
        $result_del = mysql_query($query, $db_conn) or die (DBError($_SERVER["PHP_SELF"]."\n\n".$query."\n\n".mysql_errno().": ".mysql_error()));
        }
    }

for ($i = 0; $i < count($ary_apps_info); $i++)
    {

    $query = "SELECT count(app_id) AS rec_count FROM ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."Applications WHERE app_guid = '".$ary_apps_info[$i][1]."'";
    $result = mysql_query($query, $db_conn) or die (DBError($_SERVER["PHP_SELF"]."\n\n".$query."\n\n".mysql_errno().": ".mysql_error()));
    
    $row = mysql_fetch_array($result);
    
    if ($row["rec_count"] == 0)
        {
    
        $query_app = "INSERT INTO ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."Applications (app_menu_title, app_description, app_guid, app_location, app_status, app_date_added) VALUES ('".addslashes($ary_apps_info[$i][0])."', '".addslashes($ary_apps_info[$i][3])."', '".$ary_apps_info[$i][1]."', '".$ary_apps_info[$i][2]."', 0, NOW())";
        $result_app = mysql_query($query_app, $db_conn) or die (DBError($_SERVER["PHP_SELF"]."\n\n".$query_app."\n\n".mysql_errno().": ".mysql_error()));
        
        $app_id = mysql_insert_id();
        
        // Automatically add this application to the System Administrators Group and give the admin access rights
        $query_app = "INSERT INTO ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."AppGroups (ag_app_id, ag_grp_id) VALUES (".$app_id.", 1)";
        $result_app = mysql_query($query_app, $db_conn) or die (DBError($_SERVER["PHP_SELF"]."\n\n".$query_app."\n\n".mysql_errno().": ".mysql_error()));
        
        $query_app = "INSERT INTO ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."UserApps (ua_usr_id, ua_app_id) VALUES (1, ".$app_id.")";
        $result_app = mysql_query($query_app, $db_conn) or die (DBError($_SERVER["PHP_SELF"]."\n\n".$query_app."\n\n".mysql_errno().": ".mysql_error()));
        }
    }

// Refresh the current admin user's application permissions

$query = "SELECT ua_app_id FROM ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."UserApps WHERE ua_usr_id = ".$_SESSION["WAGATEWAY"]["USR_ID"];
$result = mysql_query($query, $db_conn) or die (DBError($_SERVER["PHP_SELF"]."\n\n".$query."\n\n".mysql_errno().": ".mysql_error()));

while ($row = mysql_fetch_array($result, MYSQL_NUM))
    {
    $_SESSION["WAGATEWAY"]["APPS"][] = $row[0];  
    }
                                
?>
<html>
<head>
<title><? echo $_SERVER["PHP_SELF"]; ?></title>
<script type="text/javascript" src="../js/ajax.js"></script>
<script type="text/javascript" src="../js/main.js"></script>
<script type="text/javascript" src="../js/admin.js"></script>
<script type="text/javascript" src="../js/wagsession.js"></script>
<link rel="stylesheet" type="text/css" href="../common/gateway.css" />
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
</head>

<body bgcolor="#ffffff">

<table bgcolor="#f5f5f5" border="0" cellpadding="4" cellspacing="0" style="margin-bottom: 15px;" width="100%">
    <tr>
        <td><b><a href="index.php" onmouseout="javascript:window.status=''; return true;" onmouseover="javascript:window.status=''; return true;" onmousemove="javascript:window.status='Administration';" target="_self">Administration</a></b></td>
        <td align="right"><a href="javascript:openHelpWindow('Help', '../help/apps_help.php', '0');" onmouseout="javascript:window.status=''; return true;" onmouseover="javascript:window.status=''; return true;" onmousemove="javascript:window.status='Help';">Help</a></td>
    </tr>
</table>

<table bgcolor="#dcdcdc" border="0" cellpadding="4" cellspacing="0" style="border: solid black 1px; margin-bottom: 5px;" width="100%">
    <tr>
        <td align="center" width="1px"><img src="../common/images/apps.png" /></td>
        <td valign="middle"><b>Available Applications</td>
    </tr>
</table>

<table bgcolor="#f5f5f5" border="0" cellpadding="4" cellspacing="0" style="border: solid black 1px;" width="100%">
    <tr bgcolor="#f5f5f5">
        <td align="center" style="border-right: solid black 1px;" width="1px"><b>Enabled</b></td>
        <td style="border-right: solid black 1px; width: 1px;" width="175px"><b>Title</b></td>
        <td colspan="2" width="*"><b>Description</b></td>
    </tr>
    <?
    
    // Fetch list of currently installed applicaitons
        
    $query = "SELECT * FROM ".$_SESSION["WAGATEWAY"]["DB_TABLE_PREFIX"]."Applications ORDER BY app_menu_title";
    $result = mysql_query($query, $db_conn) or die (DBError($_SERVER["PHP_SELF"]."\n\n".$query."\n\n".mysql_errno().": ".mysql_error()));
    
    $row_count = mysql_num_rows($result);
    
    if ($row_count > 0)
        {
        
        for ($i = 0; $i < $row_count; $i++)
            {
            $row = mysql_fetch_array($result);
            ?>
            <tr>
                <!-- <td align="center" style="border-top: solid black 1px; border-right: solid black 1px; width: 1px;" width="1px"><input onclick="javascript:requestAction('app_state', '<? echo $row["app_guid"]; ?>');" style="color: red;" type="button" value="<? if ($row["app_status"] == 1) { echo "Disable"; } else { echo "Enable"; } ?>" /></td> -->
                <td align="center" style="border-top: solid black 1px; border-right: solid black 1px; width: 1px;" width="1px"><input id="chk_<? echo $row["app_guid"]; ?>" name="chk_<? echo $row["app_guid"]; ?>" <? if ($row["app_status"] == 1) { echo " checked"; } ?> onclick="javascript:changeAppState('app_state', '<? echo $row["app_guid"]; ?>');" type="checkbox" /></td>
                <td style="border-top: solid black 1px; border-right: solid black 1px;" width="175px"><? echo $row["app_menu_title"]; ?></td>
                <td style="border-top: solid black 1px; border-right: solid black 1px;" width="*"><? echo $row["app_description"]; ?>&nbsp;</td>
                <td align="center" style="border-top: solid black 1px; width: 1px;" width="1px"><input onclick="javascript:changeAppState('uninstall', '<? echo $row["app_guid"]; ?>');" type="button" value="Uninstall" /></td>
            </tr>
            <?
            }
        }
    
    ?>
</table>

<table bgcolor="#dcdcdc" border="0" cellpadding="4" cellspacing="0" style="border: solid black 1px; margin-top: 10px;" width="100%">
    <tr>
        <td>
        <table bgcolor="#dcdcdc" border="0" cellpadding="4" cellspacing="0">
            <tr>
                <td align="center"><button id="btn_install" name="btn_install" onclick="javascript:requestInstall('install', 0);" style="width: 150px;">Install / Upgrade</button></td>
            </tr>
        </table>
        </td>
    </tr>
</table>

<div id="result_message" style="background-color: #f8f8ff;  left: 0px; position: absolute; text-align: center; top: 0px; visibility: hidden;"></div>

</body>
</html>
Return current item: Web Application Gateway