summaryrefslogtreecommitdiffstats
path: root/www/admin.php
blob: cdeafdbb2ff5a9c828d80aabd5961fec4395efa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 Jonas Häggqvist
 *
 * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

require_once('preconfig.inc.php');
session_start();

function changestatuses(&$site) {
    foreach($_REQUEST['prevstatus'] as $id => $prevstatus) {
        $newstatus = $_REQUEST['status'][$id];
        $oldstatus = $_REQUEST['prevstatus'][$id];
        $reason    = $_REQUEST['reason'][$id];
        if ($oldstatus != $newstatus) {
            $site->changestatus($id, $newstatus, $oldstatus, $reason);
        }
    }
}

/* First, check if the user is logged in and handle logins */
if (isset($_REQUEST['logout'])) {
    unset($_SESSION['user']);
    $t->assign('msg', 'Logged out.');
}
if (isset($_REQUEST['user'])) {
    if ($site->adminlogin($_REQUEST['user'], $_REQUEST['pass'])) {
        $_SESSION['user'] = $_REQUEST['user'];
    }
    else {
        $t->assign('msg', 'Login failed. Please try again');
    }
}
/* If not logged in, show the login form */
if (!isset($_SESSION['user'])) {
    $template = 'login.tpl';
}
else {
    /* Else, we need to figure out what to do then */
    /* show list of themes for a target */
    if (isset($_REQUEST['target'])) {
        if (isset($_REQUEST['changestatuses'])) {
            changestatuses($site);
        }
        $approved = isset($_REQUEST['approved']) ? $_REQUEST['approved'] : 'any';
        $template = 'adminlist.tpl';
        $themes = $site->listthemes($_REQUEST['target'], 'timestamp DESC',$approved, $onlyverified = false);
        $t->assign('themes', $themes);
        $t->assign('approved', $approved);
        $t->assign('target', $site->target2fullname($_REQUEST['target']));
    }
    /* search for themes */
      if (isset($_REQUEST['searchtheme'])) {
        if (isset($_REQUEST['changestatuses'])) {
            changestatuses($site);
        }
        $approved = isset($_REQUEST['approved']) ? $_REQUEST['approved'] : 'any';
        $template = 'adminlist.tpl';
        $themes = $site->searchthemes($_REQUEST['searchtype'],$_REQUEST['searchword'],true);
        $t->assign('themes', $themes);
        $t->assign('approved', $approved);
    }
    /* show all themes */
    if (isset($_REQUEST['allthemes'])) {
        if (isset($_REQUEST['changestatuses'])) {
            changestatuses($site);
        }
        $approved = isset($_REQUEST['approved']) ? $_REQUEST['approved'] : 'any';
        $template = 'adminlist.tpl';
        $themes = $site->listthemes(false, 'timestamp DESC',$approved, $onlyverified = false);
        $t->assign('themes', $themes);
        $t->assign('approved', $approved);
    }
    /* Show a theme's details, possibly updating it as result of an admin
     * submitting changes */
    elseif (isset($_REQUEST['edittheme'])) {
        /* Update the theme */
        if (isset($_REQUEST['themename'])) {
            $site->updatetheme(
                $_REQUEST['edittheme'],
                $_REQUEST['themename'],
                $_REQUEST['mainlcd'],
                $_REQUEST['author'],
                $_REQUEST['email'],
                $_REQUEST['description']
            );
        }
        $theme = $site->themedetails($_REQUEST['edittheme']);
        $targets = array();
        foreach($site->listtargets() as $target) {
            $targets[$target['shortname']] = $target['fullname'];
        }
        $t->assign('targets', $targets);
        $t->assign('theme', $theme);
        $template = 'edittheme.tpl';
    }
    /* Adding a target */
    elseif (isset($_REQUEST['addtarget'])) {
        $site->addtarget(
            $_REQUEST['shortname'],
            $_REQUEST['fullname'],
            $_REQUEST['mainlcd'],
            $_REQUEST['pic'],
            $_REQUEST['depth'],
            empty($_REQUEST['remotelcd']) ? false : $_REQUEST['remotelcd']
        );
        $t->assign('adminmsg', 'Target added');
    }
    /* Edit a target */
    elseif (isset($_REQUEST['edittarget'])) {
        $site->edittarget(
            $_REQUEST['edittarget'],
            $_REQUEST['shortname'],
            $_REQUEST['fullname'],
            $_REQUEST['mainlcd'],
            $_REQUEST['pic'],
            $_REQUEST['depth'],
            empty($_REQUEST['remotelcd']) ? false : $_REQUEST['remotelcd']
        );
        $t->assign('adminmsg', 'Target edited');
    }
    /* Show target */
    elseif (isset($_REQUEST['showtarget'])) {
        foreach($site->listtargets() as $target) {
            if($target['shortname'] == $_REQUEST['curtarget']) {
                $t->assign("target",$target);
            }
        }
        $template = 'target.tpl';
    }
    /* Show Settings page*/
    elseif (isset($_REQUEST['showsetting'])) {
        $template = 'setting.tpl';
    }
    /* add a setting */
    elseif (isset($_REQUEST['addsetting'])) {
        $site->addsetting(
            $_REQUEST['name'],
            $_REQUEST['type']);
        $t->assign('adminmsg', 'Setting added');
    }
    /* Run checkwps on all themes */
    elseif (isset($_REQUEST['runcheckwps'])) {
	system("/bin/touch " . preconfig::privpath . "../need_update");
        $t->assign('adminmsg', 'WPS run scheduled');
    }
    /* Show log */
    elseif (isset($_REQUEST['showlog'])) {
        $log = $site->getlog();
        $template = 'showlog.tpl';
        $t->assign('log', $log);
    }
    /* Or just show the front page */
    if (!isset($template)) {
        $t->assign('title', 'Admin');
        $t->assign('targets', $site->listtargets());
        $t->assign('settings', $site->allowedsettings());
        $t->assign('adminworkneeded',$site->adminworkneeded());
        $t->assign('admin', true);
        $template = 'frontpage.tpl';
    }
}
$t->render($template);
?>