index.php 8.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
/* Skynet - Automated "Cloud" Security Scanner                                *#
#* Copyright (C) 2014-present  Jason Frisvold <friz@godshell.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.                               *#
#*                                                                            *#
#* You should have received a copy of the GNU General Public License          *#
#* along with this program; if not, write to the Free Software                *#
#* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

   // Define skynet (to allow access to files)
   define('skynet', 1);

22
23
   // Load framework
   require 'vendor/autoload.php';
24
25
26
27

   // Load the configuration file
   require_once("config.php");

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
   // Globals   
      $data = array(
          'skynet_favicon' => $skynet_favicon,
          'skynet_stylesheet' => $skynet_stylesheet,
          'skynet_homepage' => $skynet_homepage,
          'skynet_version' => $skynet_version,
      );
   
   // Connect to the database server
   $sqlhdlr = new mysqli($skynet_dbHost,  $skynet_dbUser,  $skynet_dbPass,
                         $skynet_dbName);
   
   // Make sure we have a connection
   if (mysqli_connect_errno()) {
      die( '<p>Unable to connect to the database server at this time: ' .
            mysqli_connect_error() . ' </p>' );      
   }

   function prep_smarty($app) {
      $smarty = $app->view()->getInstance();
      $smarty->loadFilter("pre", 'whitespace_control');
      $smarty->debugging = true;
   }

   $app = new \Slim\Slim(array(
       'view' => new \Slim\Views\Smarty(),
       'debug' => true,
       'log.enable' => true,
       'log.path' => 'logs/',
       'log.level' => 4,
       'mode' => 'development'
       ));
   
   $view = $app->view();
   $view->parserDirectory = $skynet_smarty_dir;
   $view->parserCompileDirectory = "$skynet_work_dir/templates_c";
   $view->parserCacheDirectory = "$skynet_work_dir/cache";
   $view->parserExtensions = array(
      dirname(__FILE__) . '/libs',
   );

   $authenticateForRole = function ( $role = 'member' ) {
    return function () use ( $role ) {
        $user = User::fetchFromDatabaseSomehow();
        if ( $user->belongsToRole($role) === false ) {
            $app = \Slim\Slim::getInstance();
            $app->flash('error', 'Login required');
            $app->redirect('/login');
        }
    };
   };

   $isauthenticated = function() {
      return function() {
         // Globalize the phptodo variables needed
         global $skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $skynet_dbName,
                $skynet_sessTime, $sqlhdlr, $data;

         // Globalize the user variables
         //global $smarty;

         // Try and get the id, last time, and user if from the sessions database
         $query = sprintf('SELECT id, last, user_id FROM sessions WHERE ' .
                          'phpsessid = "%s"', session_id());

         if ($result = $sqlhdlr->query($query)) {
            list($id, $last, $user_id) = $result->fetch_array();
            $result->close();
         } else {
            die( 'Error: ' . $sqlhdlr->error );
         }

         // Check to see if an id was set, and if the time is good
         if ((isset($id)) && (($last + $skynet_sessTime) >= time())) {
            // Good session, update the timestamp
            $query = sprintf("UPDATE sessions SET last = %d WHERE id = %d", time(),
                             $id);
      
            $sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
      
            // Create the user object
            $user_obj = new skynetUser($skynet_dbHost, $skynet_dbUser,
                                       $skynet_dbPass, $skynet_dbName, $user_id);
      
            $data['username'] = $user_obj->username();
            $data['adminflag'] = $user_obj->adminflag();
            // Assign the username to the smarty template
            //$smarty->assign('username', $user_obj->username());
            //$smarty->assign('adminflag', $user_obj->adminflag());
            if (get_magic_quotes_gpc()) {
               $data['fullname'] = stripslashes(htmlentities($user_obj->fullname(),
                                                         ENT_QUOTES));
            //   $smarty->assign('fullname',
            //                   stripslashes(htmlentities($user_obj->fullname(),
            //                                             ENT_QUOTES)));
            } else {
               $data['fullname'] = htmlentities($user_obj->fullname(),
                                                         ENT_QUOTES);
            //   $smarty->assign('fullname', htmlentities($user_obj->fullname(),
            //                                            ENT_QUOTES));
            }
      
            return($user_obj);
         } else {
            // Return a 0 to indicate that authentication was not successful
            $app = \Slim\Slim::getInstance();
            $app->flash('error', 'Login required');
            //$app->redirect('/login');
         }
      
         // Return a 0 to indicate that authentication was not successful
         $app = \Slim\Slim::getInstance();
         $app->flash('error', 'Login required');
         //$app->redirect('/login');
      };
   };

   // GET route
   $app->get('/', $isauthenticated(), function () use ($app) {
      global $data;
   
      prep_smarty($app);
print_r($data);

      $app->render('index.tpl', $data);
   
   });

   // GET route
   $app->post('/', function () use ($app) {
      global $data;
   
      prep_smarty($app);   
      $app->render('index.tpl', $data);
   
   });

   $app->run();



   
   // Define usesmarty (to indicate that config needs to load smarty)
   define('usesmarty', 1);

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
   // Connect to the database server
   $sqlhdlr = new mysqli($skynet_dbHost,  $skynet_dbUser,  $skynet_dbPass,
                         $skynet_dbName);
   
   // Make sure we have a connection
   if (mysqli_connect_errno()) {
      die( '<p>Unable to connect to the database server at this time: ' .
            mysqli_connect_error() . ' </p>' );      
   }

   // Some global variables
   $skynet_nameRegex = '/^[a-zA-Z0-9_\-]{1,15}\z/';
   $skynet_pwdRegex = '/^[a-zA-Z0-9@#$%\^&\*\/]{4,15}\z/';

   // Check to see if this is a login attempt
   if (isset($_REQUEST['username']) && isset($_REQUEST['password'])) {
      if (preg_match($skynet_nameRegex, $_REQUEST['username']) &&
          preg_match($skynet_pwdRegex, $_REQUEST['password'])) {
         $authenticated = login($_REQUEST['username'], $_REQUEST['password']);
      } else {
         $authenticated = 0;
      }
   }

   // If the user is authenticated, jump them to the main page
   if ((isset($authenticated) && ($authenticated == 1)) ||
       ($user_obj = authenticate())) {
      if (isset($_SESSION['redirect'])) {
         header('Location: ' . $_SESSION['redirect']);
      } else {
         //header('Location: ' . $skynet_serveruri .
         //       dirname($_SERVER['PHP_SELF']) . '/main.php');
         header('Location: ' . $skynet_serveruri .
                join_paths(dirname($_SERVER['PHP_SELF']), '/main.php'));
      }
      unset($_SESSION['redirect']);

   // Otherwise show the login page
   } else {
      // If the previous attempt was a failure, show it properly
      if (isset($authenticated) && ($authenticated == 0)) {
         $smarty->assign('failed', 1);
      }

      // Display the template
218
      //$smarty->display('index.tpl');
219
220
221
222
223
   }

   $sqlhdlr->close();

?>