<?  // PHPmyGallery - backend core

/*  (C) Copyright 2002-2007 Peter Krebs (PeKa)

The GNU/GPL license can be applied to every file that came with the
PHPmyGallery installation package as well as every file
this code produces when writing a configuration file.
- - - - -   

This file is part of PHPmyGallery.

PHPmyGallery 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.

PHPmyGallery 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.

You should have received a copy of the GNU General Public License
along with PHPmyGallery; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

---------------------------------
File description:

This file is the main entrypoint for the adminmenu.
Everything you do in the adminmenu has it's root somewhere here. So
have a look, the rabbithole always goes depper ;)
All modules are in the _conf/_php-core/adminmenu/ directory.

### What this script does:
### 
### Script Starts
### |
### | (Uses templates from index_view and plug-ins)
### +-+- Adminmenu Index View (Load available Groups & Descriptions, etc., Display loaded Groups)
### | +- Display Galleries    (Load available Galleries in Group & Descriptions, Display Galleries)
### +- PlugIns                (Load PlugIns, Select one, Display selected PlugIn)
### +- Groups Setup           (Load Groups Settings, Modify Groups, Save to DB, display Result)
### +- Main Setup             (Load overall Settings and default Settings, Modify Settings, Save to DB, display Result)
### +- Gallery Setup          (Load Gallery Settings, Modify Settings, Save to DB, display Result)
### +- Picture Descriptions   (Load Descriptions if any, Modify Descriptions, Save to DB, display Result)
### +- Statistics View        (Load Statistics, Crop unpopular Entries, display Statistics) 
### +- FAQ/Help               (Load selected Help Page, display Page)
### |
### Display DebugBox          (Make DebugBox Template, if DebugMode activated)
### |
### Script Ends
### 
*/

/* Script Starts */


/* Initialisation */
$dbsystem = 'filesystem';
$admindir = './';

require_once($admindir.'_php-core/globals.php');
require_once('_php-core/functions.php');
require_once('db_libs/'.$dbsystem.'.php');
$conf = getData('mainsetup');
$conf['AdminPWD'] = getData('mainsetup','adminpassword');

$galleryconf = getData('gallerysettings','all');
foreach($galleryconf as $k=>$confItem)
    $conf[$k] = $confItem;

// needed POST / GET vars
$saveit = $_POST['saveit'];
$action = $_GET['action'];
$picdir = urldecode($_GET['picdir']);
$group  = urldecode($_GET['group']);

if ($action=='') $action = 'default'; // 'default' shows adminmenu home

// Empty template vars, then processes language vars etc.
$pktpl_vars = pktpl_start();
require('_php-core/common-tpl-vars.php');

// Load page metatags & set onLoad string
$pktpl_vars['template_admin_metatags'] = pktpl('adminmenu/all_metatags.html');
$pktpl_vars['onLoad'] = '';

/* Login */
if ($conf['login_system']=='sessions') {
    // Login via PHP sessions
    
    session_start();
    if ($saveit=='login') {
        // Init session vars
        $_SESSION['sess_password'] = $_POST['sess_password'];
    } // if
    
    $password_correct = ($_SESSION['sess_password']==$conf['AdminPWD']&&$conf['AdminPWD']!='');
    if ($action!='logout'&&$password_correct) {
        $logged_in = true;
        $pktpl_vars['GET_params'] = '&'.session_name().'='.session_id();
        $pktpl_vars['template_admin_indexNavigation'] = pktpl('adminmenu/index_view/indexNavigationLoggedIn.html');
    } // if
    else {
        $logged_in = false;
        $pktpl_vars['GET_params'] = '';
        $pktpl_vars['text_pleasesetPassword'] = ($conf['AdminPWD']=='') ? $l['please_set_adminpassword']: '';
        $pktpl_vars['template_admin_indexNavigation'] = pktpl('adminmenu/index_view/indexNavigation.html');
        
        if ($action=='logout') {
            session_destroy();
        } // if
        
        // If admin password is an empty string, user is redirected to
        // the admin-password creating screen.
        if($conf['AdminPWD']=='' && $action != 'help')
            $action = 'sessionpwdcreate';
        else {
            if($action != 'help')
                $action = 'login';
        }
        
        if (!$password_correct&&$saveit=='login') {
            $text = $l['wrong_password'];
        } // if
        else {
            $text = '';
        } // else
    } // else
} // if
else {
    // default login method (via htaccess)
    // If the .htaccess file exists and has content, we
    // suggest the user logged in through it.
    // If this suggestion results to false, the user is
    // redirected to the htaccess-file creating screen.
    $logged_in = (file_exists('.htaccess')&&filesize('.htaccess') > 2);
    if(!$logged_in && $action != 'help')
        $action = 'htaccesscreate';
    else
        $pktpl_vars['template_admin_indexNavigation'] = pktpl('adminmenu/index_view/indexNavigation.html');
    
} // else

// If password is correct, display backend. $action tells
// which file to include within _phpcore/adminmenu/
// If not logged in, user can only log in, set the
// admin password or take a look at the FAQ.
if ($logged_in || $action=='help' || $action=='login' ||
    $action=='sessionpwdcreate' || $action=='htaccesscreate') {
    
    require_once('_php-core/adminmenu/do_'.$action.'.php');
} // if

/* Fire up Debugging System if $debugText holds a message */
if ($debugText!='') {
    $pktpl_vars['debug_box'] = debugBox();
} // if

// Set execution time and flush HTML to browser
$pktpl_vars['text_timeExectime'] = pk_exectime($time_start,$l['decimal_point']);
echo pktpl('adminmenu/adminmenu.html');

?>