forked from i4ki/secplus-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecplus.lib.php
executable file
·364 lines (312 loc) · 8.93 KB
/
secplus.lib.php
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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/**
* secplus.lib.php
*
* SEC+ WebFramework
* License: GNU GPL v2.0
* Light MVC PHP Framework designed for security.
*
* @author i4k - Tiago Natel de Moura <[email protected]>
* @author m0nad - Victor Ramos Mello <[email protected]>
*
* @version 1.0
* @package secplus-php
*/
namespace SecPlus;
/**
* Configuration class
* Extend this class to configure your project.
*
* *NOT* change any default configuration in this class...
* Configure your project in the extended class.
*/
abstract class Config {
/* Overload this constant and set the project name */
const PROJECT_NAME = "Sec+ WebFramework";
/* Overload this constant and set the complete url of the project */
const PROJECT_URL = "http://www.secplus.com.br/";
/**
* Singleton configuration instance
* @var Config
*/
private static $instance;
/**
* Database configuration
* *Not* change the properties here, extend this abstract class and use the getters and setters
* to update the database configurations.
*/
/**
* Database host
* @var string
*/
protected $dbHost = "127.0.0.1";
/**
* Database user
* @var string
*/
protected $dbUser = "";
/**
* Database password
* @var string
*/
protected $dbPass = "";
/**
* Database driver
* @var string
*/
protected $dbms = "mysql";
/**
* Database name
* @var string
*/
protected $dbDatabase = "";
/**
* Salt for hash algorithms
* @var string
*/
protected $salt = "Welcome, to the desert of the real";
/**
* Directory configuration
*/
protected $rootProjectDir;
protected $libDir;
protected $controllerDir;
protected $modelDir;
protected $daoDir;
protected $voDir;
protected $viewDir;
protected $staticDir;
/**
* Safe PHP files to include to prevent LFI/LFD
* Array with every php file that is safe to include/require into project
& @var array
*/
protected $safeFiles = array();
protected $safeProperties = array(
'rootProjectDir','libDir','controllerDir',
'modelDir','daoDir','voDir','viewDir','staticDir',
'dbHost','dbUser','dbPass','dbDatabase','dbms',
'salt','controllerName','actionName','safeFiles','defaultController'
);
protected $defaultController = "home";
/**
* MVC Configuration
*/
/**
* Name of the controllers.
* This is the name of the uri parameter that invoke the controller.
* ex.: http://site]/?$controllerName=home
* @var string
*/
protected $controllerName = "controller";
/**
* Name of the action.
* @var string
*/
protected $actionName = "action";
/**
* Get a instance of the configuration class
* @return Config
*/
public static function getInstance() {
if (isset(self::$instance))
return self::$instance;
else {
$c = get_called_class();
self::$instance = new $c();
return self::$instance;
}
}
/**
* Constructor sets up the following properties:
* {@link $root_project_dir}
* {@link $lib_dir}
* {@link $controller_dir}
* {@link $model_dir}
* {@link $dao_dir}
* {@link $vo_dir}
* {@link $view_dir}
* {@link $static_dir}
*/
protected function __construct() {
$this->rootProjectDir = dirname($_SERVER['SCRIPT_FILENAME']);
$this->libDir = $this->rootProjectDir . '/lib';
$this->controllerDir = $this->rootProjectDir . '/controller';
$this->modelDir = $this->rootProjectDir . '/model';
$this->daoDir = $this->modelDir . '/dao';
$this->voDir = $this->modelDir . '/vo';
$this->viewDir = $this->rootProjectDir . '/view';
$this->staticDir = Config::PROJECT_URL . '/view';
}
/**
* This is a PHP magic method.
* Implementing this method we avoid have to declare boilerplate getters and setters.
* @param string $func
* @param array $args
* @return mixed
*/
public function __call($func, $args) {
if (preg_match('/^get/', $func) && count($args) == 0) {
$prop = lcfirst(substr($func, 3));
if (!empty($prop) && in_array($prop, $this->safeProperties)) {
return $this->{$prop};
}
} else if (preg_match('/^set/', $func) && count($args) == 1) {
$prop = lcfirst(substr($func, 3));
if (in_array($prop, $this->safeProperties)) {
$this->{$prop} = $args[0];
return;
}
}
throw new \Exception("Fatal error: Method '" . htmlentities($func) . "' not found or permission denied to be called.");
}
}
/**
* Main class
*/
class WebFramework {
protected $config;
protected $controller;
public function __construct($conf) {
$this->config = $conf;
spl_autoload_register(array($this, 'autoload'));
$this->handleController();
}
/**
* Loader for the SecPlus classes.
*/
private function autoload($classname) {
$filename = "";
if (preg_match('/Controller$/', $classname)) {
$filename = $this->config->getControllerDir();
} else if (preg_match('/DAO$/', $classname)) {
$filename = $this->config->getDaoDir();
} else if (preg_match('/View$/', $classname)) {
$filename = $this->config->getViewDir();
}
$filename .= DIRECTORY_SEPARATOR . $classname . '.php';
/**
* Security against LFI/LFD
* Each file that needs to be dynamically included, *MUST* be defined in the configuration class.
*/
if (file_exists($filename) && in_array($filename, $this->config->getSafeFiles()))
require $filename;
else {
print '<span style="color: white; background-color: red;">File ' . htmlentities($filename) . ' not found or permission denied to include.</span><br><br>';
die();
}
}
/**
* Controller manager.
* Identify the controller and execute.
*/
protected function handleController() {
$c = Config::getInstance();
$controllerName = $c->getControllerName();
$action_name = $c->getActionName();
if (!empty($_GET[$controllerName])) {
$controller = $_GET[$controllerName];
} else {
$controller = $c->getDefaultController(); // If any controller, this is the default.
}
$class = ucfirst($controller) . 'Controller';
$c = new $class();
$c->setup();
}
}
/**
* Interface for controllers
*/
interface IController {
/**
* SecPlus\WebFramework automatically call this method for set up the controller
*/
public function setup();
}
/**
* Every Controller need extend this abstract class
*/
abstract class AbstractController implements IController {
/**
* Singleton instance of the Config class.
*/
protected $config;
/**
* Renderize the view
*
* $view is the name of the view to render.
* $arr_vars is an array of variables to export to be visible in the view file context.
*/
public function render($view, $arr_vars) {
$view_file = $this->config->getViewDir() . DIRECTORY_SEPARATOR . $view . 'View.php';
$safe_files = $this->config->getSafeFiles();
if (in_array($view_file, $safe_files)) {
extract($arr_vars);
include $view_file;
} else {
print "<span style=\"color: red;\">permission denied to include the file '" . htmlentities($view_file) . "'</span>";
die();
}
}
}
/**
* Every Model need extend this abstract class
*/
abstract class AbstractModel implements IModel {
/**
* Singleton instance of the database connection
*/
private static $conn = null;
public function connect() {
self::$conn = Database::getConnection();
}
}
/**
* Interface to Models
*/
interface IModel {
}
/**
* Manages the database
*/
class Database {
private static $conn = null;
private function __construct() {
/**
* *Never* try to instantiate this class
*/
}
/**
* Connect to database
*/
public static function getConnection() {
try {
$config = Config::getInstance();
$dbms = $config->getDbms();
$host = $config->getDbHost();
$user = $config->getDbUser();
$pass = $config->getDbPass();
$dbname = $config->getDbDatabase();
$datasource = $dbms . ":" . "host=" . $host . ";dbname=" . $dbname;
self::$conn = new \PDO($datasource, $user, $pass);
self::$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); // Set Errorhandling to Exception
return self::$conn;
} catch (PDOException $e) {
print "database connection error.";
die();
} catch (Exception $e) {
print "error: " . $e->getMessage();
}
}
public static function closeConnection() {
self::$conn = null;
}
}
/**
* Helpers to aid in develop.
*/
final class Helper {
public static function http_redirect($url) { header("Location: $url"); }
public static function html_redirect($url, $time) { print '<meta http-equiv="refresh" content="' . htmlentities($time) . '; url=' . $url . '">'; }
public static function alert($msg) { print '<script type="text/javascript">alert("' . $msg . '");</script>'; }
}