kritbit

kritbit Commit Details


Date:2015-11-14 16:18:09 (9 years 1 month ago)
Author:Natalie Adams
Branch:master
Commit:55f82dba5425ad695cf27b7810e603931c0d2832
Parents: 6c17c7d25a40758ed2fcfb72d49033c50aaa6b1e
Message:Adding oauth Upgrade HF to use autoloading

Changes:

File differences

web/application/controllers/base.php
11
22
3
3
44
5
6
7
8
9
10
11
12
13
14
15
16
517
618
719
820
9
21
1022
11
23
1224
1325
1426
......
2032
2133
2234
23
35
2436
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
2568
2669
<?php
abstract class base extends HF_Controller {
use \application\models\Sessions;
abstract class base extends \system\engine\HF_Controller {
protected $session = null;
protected $sessionData = null;
public function isLoggedIn() {
if (!$this->sessionData && !isset($this->sessionData->userId)) {
header("Location: /login");
return false;
} else {
return true;
}
}
public function __construct($config, $core, $tpl)
{
parent::__construct($config, $core, $tpl);
if ($this->config["DATABASETYPE"] == "SQLITE") {
if ($this->config["DATABASE_TYPE"] == "SQLITE") {
$this->pdo = new PDO("sqlite:kritbot.sqlite3");
DB::$c = $this->pdo;
\vendor\DB\DB::$c = $this->pdo;
} else {
$this->pdo = new PDO(
"mysql:dbname={$this->config['MYSQL_DBNAME']};host={$this->config['MYSQL_HOST']}",
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
DB::$c = $this->pdo;
\vendor\DB\DB::$c = $this->pdo;
}
if (isset($_COOKIE["session"])) {
$validSession = Sessions::getByField("sessionid", $_COOKIE["session"]);
if ($validSession) {
try {
$this->session = $validSession[0];
$this->sessionData = json_decode($this->session->data);
if ($this->sessionData == null) {
return;
}
$this->user = \application\models\Users::getByField("id", $this->sessionData->userId)[0];
} catch (\Exception $e) {
setcookie("session", "", time() - 3600);
header("Location: /login");
}
} else {
setcookie("session", "", time() - 3600);
header("Location: /login");
}
} else {
$bool = true;
$bytes = openssl_random_pseudo_bytes (10, $bool);
$sessionId = bin2hex($bytes);
$this->session = new Sessions();
$this->session->ip = $_SERVER["REMOTE_ADDR"];
$this->session->userAgent = $_SERVER["HTTP_USER_AGENT"];
$this->session->sessionid = $sessionId;
$this->session->save();
setcookie("session", $sessionId, 2147483647);
}
}
}
web/application/controllers/login.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
<?php
use system\vendor\phpoauthlib2\providers\GoogleAuthProvider;
use system\vendor\phpoauthlib2\OAuth;
use \vendor\DB\DB;
use application\models\Users;
class login extends base {
private function accessDenied() {
return "ACCESS DENIED";
}
public function index() {
$authProvider = new GoogleAuthProvider($_GET, [
"client_id" => $this->config["GOOGLE_OAUTH_ID"],
"client_secret" => $this->config["GOOGLE_OAUTH_SECRET"],
"redirect_uri" => "http://localhost/login"
]);
$oauth = new OAuth($authProvider, $_GET);
$check = $oauth->check();
if ($check === true) {
$email = $authProvider->getEmail();
/** @var Users $user */
//$user = DB::fetchObject("SELECT * FROM users WHERE email = ?", "users", [$email]);
$users = Users::getByField("email", $email);
if (count($users) == 0) {
echo $this->accessDenied();
return;
}
$user = $users[0];
$this->session->data = json_encode(["userId" => $user->id]);
$this->session->save();
$this->sessionData = $this->session->data;
header("Location: /");
} else {
header("Location: " . $check);
}
}
}
web/application/controllers/main.php
11
22
3
3
44
55
66
77
8
8
9
10
11
12
913
1014
1115
<?php
class main extends HF_Controller
class main extends base
{
public function index()
{
echo $this->loadRender("login.html");
if ($this->isLoggedIn()) {
echo "Hello - " . $this->sessionData->userId;
echo "email = " . $this->user->email;
}
//echo "hello";
}
}
web/application/migrations/1.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
echo "Creating session table..." . PHP_EOL;
DB::query("CREATE TABLE sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sessionid VARCHAR(255),
ip VARCHAR(255),
userAgent VARCHAR(255),
data TEXT
)");
echo "Creating users table..." . PHP_EOL;
DB::query("CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email VARCHAR(255)
)");
echo "Adding user " . PHP_EOL;
DB::insert("users", ["email" => "adamsna@datanethost.net"]);
web/application/models/Sessions.php
1
2
3
4
5
6
7
8
9
10
11
<?php
namespace application\models;
class Sessions extends \system\engine\HF_Model {
public $id;
public $sessionid;
public $ip;
public $userAgent;
public $data;
}
web/application/models/Users.php
1
2
3
4
5
6
7
8
9
10
11
<?php
namespace application\models;
use system\engine\HF_Model;
class Users extends HF_Model
{
public $id;
public $email;
}
web/index.php
11
22
3
4
5
36
47
58
69
710
8
9
10
11
1112
<?php
spl_autoload_extensions(".php"); // comma-separated list
spl_autoload_register();
foreach (glob("system/vendor/*.php") as $filename)
{
include $filename;
}
require('system/engine/core.php');
$core = new HF_Core();
$core = new \system\engine\HF_Core();
$core->run();
web/migrations.php
1
2
3
4
5
6
7
8
9
10
11
<?php
foreach (glob("system/vendor/*.php") as $filename)
{
include $filename;
}
require('system/engine/HF_Core.php');
$core = new HF_Core();
$core->runMigrations();
web/system/engine/HF_Controller.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
<?php
namespace system\engine;
class HF_Controller
{
protected $config;
protected $tpl;
protected $core;
public function __construct($config, $core, $tpl = null)
{
$this->config = $config;
$this->tpl = $tpl;
$this->core = $core;
}
public function loadRender($template, $parameters=array())
{
$this->tpl->loadTemplate($template);
return $this->tpl->render($parameters);
}
}
web/system/engine/HF_Core.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
<?php
/*include "system/engine/HF_Controller.php";
include "system/engine/SMTP.php";
include "system/engine/exceptions.php";
include "system/engine/HF_Model.php";*/
namespace system\engine;
use vendor\DB\DB;
include "exceptions.php";
class HF_Core
{
private $class;
private $method;
private $classname;
private $args = array();
private $config = array();
private $tpl;
public function __construct()
{
$config = include("system/engine/config-default.php");
if (is_file("application/config.php"))
{
$newconfig = include("application/config.php");
}
$this->config = array_merge($config, $newconfig);
\vendor\DB\DB::$type = $config["DATABASE_TYPE"];
if ($this->config["USE_H20_TPL"])
$this->tpl = new \H2o(null, array(
"searchpath" => getcwd() . "/application/views/",
"cache_dir" => "application/tmp/",
'cache' => 'file'
));
set_error_handler("\\system\\engine\\HF_Core::error_handler");
//set_exception_handler("\\system\\engine\\HF_Core::exception_handler");
$this->findController();
}
public static function exception_handler($e) {
echo "Hello";
}
public function siteURL()
{
if (isvarset($this->config["SITE_URL"]))
{
return $this->config["SITE_URL"];
}
$path = explode("/", $_SERVER["REQUEST_URI"]);
$path = array_filter($path, 'strlen');
if (count($path) == 0)
{
return $_SERVER["HTTP_HOST"] . "/";
} else {
if (in_array($this->classname, $path))
{
$newpath = implode("/", array_splice($path, 0, -2));
return $_SERVER["HTTP_HOST"] . "/" . $newpath . "/";
} else {
$newpath = implode("/", $path);
return $_SERVER["HTTP_HOST"] . "/" . $newpath . "/";
}
}
}
private function findController()
{
try
{
if (isvarset($_SERVER["PATH_INFO"]))
{
$request = $_SERVER["PATH_INFO"];
//$request = $_SERVER["PHP_SELF"];
$splitreq = explode("/", $request);
/*$request = "";
for($i = 0; $i < count($splitreq); $i++)
{
if ($splitreq[$i] == "index.php")
{
$request = implode("/", array_splice($splitreq, $i+1));
}
}*/
//print $request;
//$request = substr($request, 1);
//$request = substr($request, 0, -1);
} else {
$request = "";
}
if ($request == "" || $request == "/")
{
require_once("application/controllers/" . $this->config["DEFAULT_ROUTE"] . ".php");
$this->loadController(new $this->config["DEFAULT_ROUTE"]($this->config, $this, $this->tpl), $this->config["DEFAULT_ROUTE"], "index");
return;
}
if ($request[strlen($request)-1] == "/")
$request = substr($request, 0, -1);
$arr = explode("/", $request);
$path = "application/controllers/";
for($i = 0; $i < count($arr); $i++)
{
if (is_file($path . $arr[$i] . ".php")) // found the controller
{
include_once($path . $arr[$i] . ".php");
if ($i + 1 < count($arr)) // if there is a define after the controller name - this would be the method name
{
$this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], $arr[$i+1], array_slice ($arr, 2));
} else { // call index
$this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], "index");
}
return;
}
if (is_dir($path . $arr[$i])) // controller is hidden deeper
{
$path = $path . $arr[$i] . "/";
continue;
}
include_once($path . $this->config["DEFAULT_ROUTE"] . ".php");
$this->loadController(new $this->config["DEFAULT_ROUTE"]($this->config, $this, $this->tpl), $this->config["DEFAULT_ROUTE"], "index");
//$this->load404Controller();
break;
// throw exception controller not found
}
} catch (\Exception $e) {
if ($this->config["DEBUG"])
echo vdump($e, $this);
else
$this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
}
}
private function load404Controller()
{
if (is_file(getcwd() . "/application/status.php"))
{
include_once (getcwd() . "/application/status.php");
$this->loadController(new HF_Status($this->config, $this, $this->tpl), "HF_Status", "Status404");
} else {
include_once(getcwd() . "/system/engine/status.php");
$this->loadController(new HF_Status($this->config, $this, $this->tpl), "HF_Status", "Status404");
}
}
private function load500Controller()
{
if (is_file(getcwd() . "/application/status.php"))
{
include_once (getcwd() . "/application/status.php");
$this->loadController(new HF_Status($this->config, $this, $this->tpl), "\\system\\engine\\HF_Status", "Status500");
} else {
include_once (getcwd() . "/system/engine/status.php");
$this->loadController(new HF_Status($this->config, $this, $this->tpl), "\\system\\engine\\HF_Status", "Status500");
}
}
private function loadController($class, $classname, $method, $args = array())
{
$this->class = $class;
$this->classname = $classname;
$this->method = $method;
$this->args = $args;
}
public function run($err=false)
{
try
{
$call = new \ReflectionMethod($this->classname, $this->method);
if ($err)
{
$call->invokeArgs($this->class, $this->args);
return;
}
$numOfReqPara = $call->getNumberOfRequiredParameters();
$numOfOptPara = $call->getNumberOfParameters() - $numOfReqPara;
$remainparas = count($this->args) - $numOfReqPara;
if ($numOfReqPara == 0 || ($remainparas >= 0 && $remainparas <= $numOfOptPara))
{
$call->invokeArgs($this->class, $this->args);
}
else
{
$this->load404Controller();
$this->run(true);
}
}
catch (\ReflectionException $e)
{
if (strstr($e->getMessage(), "does not exist") !== false)
{
$this->load404Controller();
} else {
$this->load500Controller();
}
$this->run(true);
if ($this->config["DEBUG"])
echo vdump($e, $this);
else
$this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
} catch (\Exception $e) {
$this->load500Controller();
$this->run(true);
if ($this->config["DEBUG"])
echo vdump($e, $this);
else
$this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
}
}
public function mail_admins($subject, $msg, $html = false)
{
if (array_key_exists("ADMINS", $this->config))
{
foreach($this->config["ADMINS"] as $email)
{
$this->mail_user($email, $subject, $msg, $html);
}
}
}
public function mail_user($to, $subject, $msg, $html = false)
{
if ($this->config["USE_HF_SMTP"])
{
$smtp = new HF_SMTP($this->config["SMTP_FROM"], $to, $subject, $msg, $this->config["SMTP_SERVER"], $this->config["SMTP_USER"], $this->config["SMTP_PASS"], $this->config["SMTP_PORT"]);
$smtp->send($html);
} else {
require_once "Mail.php";
$smtp = null;
if ($this->$this->config["SMTP_USER"] && $this->config["SMTP_PASS"])
$smtp = Mail::factory('smtp', array(
"host" => $this->config["SMTP_SERVER"],
"port" => $this->config["SMTP_PORT"],
"auth" => true,
'username' => $this->config["SMTP_USER"],
'password' => $this->config["SMTP_PASS"]
));
else
$smtp = Mail::factory('smtp', array(
"host" => $this->config["SMTP_SERVER"],
"port" => $this->config["SMTP_PORT"]
));
$headers = array ('From' => $this->config["SMTP_FROM"],
'To' => $to,
'Subject' => $subject);
$smtp->send($to, $headers, $msg);
}
}
public static function error_handler($err_severity, $err_msg, $err_file, $err_line, array $err_context)
{
if (0 === error_reporting()) { return false;}
switch($err_severity)
{
case E_ERROR: throw new \ErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_WARNING: throw new WarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_PARSE: throw new ParseException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_NOTICE: throw new NoticeException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_CORE_ERROR: throw new CoreErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_CORE_WARNING: throw new CoreWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_COMPILE_ERROR: throw new CompileErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_COMPILE_WARNING: throw new CoreWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_ERROR: throw new UserErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_WARNING: throw new UserWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_NOTICE: throw new UserNoticeException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_STRICT: throw new StrictException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_RECOVERABLE_ERROR: throw new RecoverableErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_DEPRECATED: throw new DeprecatedException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_DEPRECATED: throw new UserDeprecatedException ($err_msg, 0, $err_severity, $err_file, $err_line);
}
}
public function runMigrations() {
global $argv;
switch($this->config["DATABASE_TYPE"]) {
case "SQLITE":
DB::$c = new \PDO("sqlite:" . $this->config["DATABASE_FILE"]);
break;
case "MySQL":
DB::$c = new \PDO(
"mysql:dbname={$this->config['MYSQL_DBNAME']};host={$this->config['MYSQL_HOST']}",
$this->config['MYSQL_USER'],
$this->config['MYSQL_PASS'],
array(
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_OBJ,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
)
);
break;
}
DB::$c->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
DB::query("CREATE TABLE IF NOT EXISTS migrations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
migration INTEGER,
ran_at DATETIME
)");
switch ($argv[1]) {
case "show":
foreach(DB::fetch("SELECT migration, ran_at FROM migrations") as $migration) {
echo $migration["migration"] . " => " . $migration["ran_at"] . PHP_EOL;
}
break;
case "count":
echo DB::column("SELECT COUNT(id) FROM migrations");
break;
case "run":
$migrations = DB::fetch("SELECT migration FROM migrations");
$migrationArray = [];
foreach($migrations as $migration) {
$migrationArray[] = $migration["migration"];
}
foreach (glob("application/migrations/*.php") as $filename)
{
if (!in_array($filename, $migrationArray)) {
include $filename;
DB::insert("migrations", ["migration" => $filename, "ran_at" => (new \DateTime())->format("Y-m-d")]);
}
}
break;
case "clear":
DB::query("DELETE FROM migrations");
break;
case "reset":
switch($this->config["DATABASE_TYPE"]) {
case "SQLITE":
DB::$c = null;
unlink($this->config["DATABASE_FILE"]);
break;
case "MYSQL":
DB::query("DROP DATABASE " . $this->config['MYSQL_DBNAME']);
DB::query("CREATE DATABASE " . $this->config['MYSQL_DBNAME']);
break;
}
break;
}
}
}
web/system/engine/HF_Model.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
<?php
namespace system\engine;
use \vendor\DB\DB;
abstract class HF_Model {
protected $id = null;
public static function saveFromArray($data) {
$fieldMap = [];
$table = strtolower(get_class());
foreach(DB::getColumns($table) as $column) {
$fieldMap[$column] = $data[$column];
}
if ($fieldMap["id"] == null) {
DB::insert($table, $fieldMap);
} else {
$updateFields = $fieldMap;
unset($updateFields["id"]);
DB::update($table, $updateFields, $fieldMap["id"]);
}
}
public function save() {
$fieldMap = [];
$function = new \ReflectionClass(get_called_class());
$table = strtolower($function->getShortName());
foreach(DB::getColumns($table) as $column) {
$fieldMap[$column] = $this->$column;
}
if ($fieldMap["id"] == null) {
DB::insert($table, $fieldMap);
} else {
$updateFields = $fieldMap;
unset($updateFields["id"]);
DB::update($table, $updateFields, $fieldMap["id"]);
}
}
public static function getByField($field, $value) {
$function = new \ReflectionClass(get_called_class());
$table = strtolower($function->getShortName());
$fields = implode(", ", DB::getColumns($table));
return DB::fetchObject("SELECT $fields FROM $table WHERE $field = ?", get_called_class(), [$value]);
}
}
web/system/engine/SMTP.php
11
22
3
4
35
46
57
<?php
namespace system\engine;
// Based on tutorial from here: https://portal.cyberhostpro.com/knowledgebase/170/PHP-Mail-Script-with-SMTP-Authentication.html
class HF_SMTP
{
web/system/engine/controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
class HF_Controller
{
protected $config;
protected $tpl;
protected $core;
public function __construct($config, $core, $tpl = null)
{
$this->config = $config;
$this->tpl = $tpl;
$this->core = $core;
}
public function loadRender($template, $parameters=array())
{
$this->tpl->loadTemplate($template);
return $this->tpl->render($parameters);
}
}
web/system/engine/core.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
<?php
include "system/engine/controller.php";
include "system/engine/SMTP.php";
include "system/engine/exceptions.php";
class HF_Core
{
private $class;
private $method;
private $classname;
private $args = array();
private $config = array();
private $tpl;
public function __construct()
{
$config = include("system/engine/config-default.php");
if (is_file("application/config.php"))
{
$newconfig = include("application/config.php");
}
$this->config = array_merge($config, $newconfig);
if ($this->config["USE_H20_TPL"])
$this->tpl = new H2o(null, array(
"searchpath" => getcwd() . "/application/views/",
"cache_dir" => "application/tmp/",
'cache' => 'file'
));
set_error_handler("HF_Core::error_handler");
$this->findController();
}
public function siteURL()
{
if (isvarset($this->config["SITE_URL"]))
{
return $this->config["SITE_URL"];
}
$path = explode("/", $_SERVER["REQUEST_URI"]);
$path = array_filter($path, 'strlen');
if (count($path) == 0)
{
return $_SERVER["HTTP_HOST"] . "/";
} else {
if (in_array($this->classname, $path))
{
$newpath = implode("/", array_splice($path, 0, -2));
return $_SERVER["HTTP_HOST"] . "/" . $newpath . "/";
} else {
$newpath = implode("/", $path);
return $_SERVER["HTTP_HOST"] . "/" . $newpath . "/";
}
}
}
private function findController()
{
try
{
if (isvarset($_SERVER["PATH_INFO"]))
{
$request = $_SERVER["PATH_INFO"];
//$request = $_SERVER["PHP_SELF"];
$splitreq = explode("/", $request);
/*$request = "";
for($i = 0; $i < count($splitreq); $i++)
{
if ($splitreq[$i] == "index.php")
{
$request = implode("/", array_splice($splitreq, $i+1));
}
}*/
//print $request;
//$request = substr($request, 1);
//$request = substr($request, 0, -1);
} else {
$request = "";
}
if ($request == "" || $request == "/")
{
require("application/controllers/" . $this->config["DEFAULT_ROUTE"] . ".php");
$this->loadController(new $this->config["DEFAULT_ROUTE"]($this->config, $this, $this->tpl), $this->config["DEFAULT_ROUTE"], "index");
return;
}
if ($request[strlen($request)-1] == "/")
$request = substr($request, 0, -1);
$arr = explode("/", $request);
$path = "application/controllers/";
for($i = 0; $i < count($arr); $i++)
{
if (is_file($path . $arr[$i] . ".php")) // found the controller
{
include($path . $arr[$i] . ".php");
if ($i + 1 < count($arr)) // if there is a define after the controller name - this would be the method name
{
$this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], $arr[$i+1], array_slice ($arr, 2));
} else { // call index
$this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], "index");
}
return;
}
if (is_dir($path . $arr[$i])) // controller is hidden deeper
{
$path = $path . $arr[$i] . "/";
continue;
}
include($path . $this->config["DEFAULT_ROUTE"] . ".php");
$this->loadController(new $this->config["DEFAULT_ROUTE"]($this->config, $this, $this->tpl), $this->config["DEFAULT_ROUTE"], "index");
//$this->load404Controller();
break;
// throw exception controller not found
}
} catch (Exception $e) {
if ($this->config["DEBUG"])
echo vdump($e, $this);
else
$this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
}
}
private function load404Controller()
{
if (is_file(getcwd() . "/application/status.php"))
{
include_once (getcwd() . "/application/status.php");
$this->loadController(new status($this->config, $this, $this->tpl), "status", "Status404");
} else {
include_once(getcwd() . "/system/engine/status.php");
$this->loadController(new HF_Status($this->config, $this, $this->tpl), "HF_Status", "Status404");
}
}
private function load500Controller()
{
if (is_file(getcwd() . "/application/status.php"))
{
include_once (getcwd() . "/application/status.php");
$this->loadController(new status($this->config, $this, $this->tpl), "status", "Status500");
} else {
include_once (getcwd() . "/system/engine/status.php");
$this->loadController(new HF_Status($this->config, $this, $this->tpl), "HF_Status", "Status500");
}
}
private function loadController($class, $classname, $method, $args = array())
{
$this->class = $class;
$this->classname = $classname;
$this->method = $method;
$this->args = $args;
}
public function run($err=false)
{
try
{
$call = new ReflectionMethod($this->classname, $this->method);
if ($err)
{
$call->invokeArgs($this->class, $this->args);
return;
}
$numOfReqPara = $call->getNumberOfRequiredParameters();
$numOfOptPara = $call->getNumberOfParameters() - $numOfReqPara;
$remainparas = count($this->args) - $numOfReqPara;
if ($numOfReqPara == 0 || ($remainparas >= 0 && $remainparas <= $numOfOptPara))
{
$call->invokeArgs($this->class, $this->args);
}
else
{
$this->load404Controller();
$this->run(true);
}
} catch (ReflectionException $e)
{
if (strstr($e->getMessage(), "does not exist") !== false)
{
$this->load404Controller();
} else {
$this->load500Controller();
}
$this->run(true);
if ($this->config["DEBUG"])
echo vdump($e, $this);
else
$this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
} catch (Exception $e) {
$this->load500Controller();
$this->run(true);
if ($this->config["DEBUG"])
echo vdump($e, $this);
else
$this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
}
}
public function mail_admins($subject, $msg, $html = false)
{
if (array_key_exists("ADMINS", $this->config))
{
foreach($this->config["ADMINS"] as $email)
{
$this->mail_user($email, $subject, $msg, $html);
}
}
}
public function mail_user($to, $subject, $msg, $html = false)
{
if ($this->config["USE_HF_SMTP"])
{
$smtp = new HF_SMTP($this->config["SMTP_FROM"], $to, $subject, $msg, $this->config["SMTP_SERVER"], $this->config["SMTP_USER"], $this->config["SMTP_PASS"], $this->config["SMTP_PORT"]);
$smtp->send($html);
} else {
require_once "Mail.php";
$smtp = null;
if ($this->$this->config["SMTP_USER"] && $this->config["SMTP_PASS"])
$smtp = Mail::factory('smtp', array(
"host" => $this->config["SMTP_SERVER"],
"port" => $this->config["SMTP_PORT"],
"auth" => true,
'username' => $this->config["SMTP_USER"],
'password' => $this->config["SMTP_PASS"]
));
else
$smtp = Mail::factory('smtp', array(
"host" => $this->config["SMTP_SERVER"],
"port" => $this->config["SMTP_PORT"]
));
$headers = array ('From' => $this->config["SMTP_FROM"],
'To' => $to,
'Subject' => $subject);
$smtp->send($to, $headers, $msg);
}
}
public static function error_handler($err_severity, $err_msg, $err_file, $err_line, array $err_context)
{
if (0 === error_reporting()) { return false;}
switch($err_severity)
{
case E_ERROR: throw new ErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_WARNING: throw new WarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_PARSE: throw new ParseException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_NOTICE: throw new NoticeException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_CORE_ERROR: throw new CoreErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_CORE_WARNING: throw new CoreWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_COMPILE_ERROR: throw new CompileErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_COMPILE_WARNING: throw new CoreWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_ERROR: throw new UserErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_WARNING: throw new UserWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_NOTICE: throw new UserNoticeException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_STRICT: throw new StrictException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_RECOVERABLE_ERROR: throw new RecoverableErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_DEPRECATED: throw new DeprecatedException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_DEPRECATED: throw new UserDeprecatedException ($err_msg, 0, $err_severity, $err_file, $err_line);
}
}
}
web/system/engine/exceptions.php
11
22
3
4
35
46
5
6
7
8
9
10
11
12
13
14
15
16
17
18
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
namespace system\engine;
//Original idea by http://www.php.net/manual/en/function.set-error-handler.php#112881
class WarningException extends ErrorException {}
class ParseException extends ErrorException {}
class NoticeException extends ErrorException {}
class CoreErrorException extends ErrorException {}
class CoreWarningException extends ErrorException {}
class CompileErrorException extends ErrorException {}
class CompileWarningException extends ErrorException {}
class UserErrorException extends ErrorException {}
class UserWarningException extends ErrorException {}
class UserNoticeException extends ErrorException {}
class StrictException extends ErrorException {}
class RecoverableErrorException extends ErrorException {}
class DeprecatedException extends ErrorException {}
class UserDeprecatedException extends ErrorException {}
class WarningException extends \ErrorException {}
class ParseException extends \ErrorException {}
class NoticeException extends \ErrorException {}
class CoreErrorException extends \ErrorException {}
class CoreWarningException extends \ErrorException {}
class CompileErrorException extends \ErrorException {}
class CompileWarningException extends \ErrorException {}
class UserErrorException extends \ErrorException {}
class UserWarningException extends \ErrorException {}
class UserNoticeException extends \ErrorException {}
class StrictException extends \ErrorException {}
class RecoverableErrorException extends \ErrorException {}
class DeprecatedException extends \ErrorException {}
class UserDeprecatedException extends \ErrorException {}
web/system/engine/status.php
11
22
3
4
35
46
57
<?php
namespace system\engine;
class HF_Status extends HF_Controller
{
web/system/vendor/DB.php
1010
1111
1212
13
14
15
1316
1417
1518
1619
20
21
1722
1823
1924
......
8085
8186
8287
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
83103
84104
85105
......
126146
127147
128148
149
150
151
152
153
154
155
156
157
158
159
160
161
129162
* @licenseMIT License <http://www.opensource.org/licenses/mit-license.php>
********************************** 80 Columns *********************************
*/
namespace vendor\DB;
class DB
{
static $q,$c,$p,$i = '`';
static $type = "";
/**
* Fetch a column offset from the result set (COUNT() queries)
*
}
/**
* Fetch all query result rows as object
*
* @param string $query query string
* @param array $params query parameters
* @return array
*/
static function fetchObject($query, $className, $params = NULL)
{
if( ! $statement = DB::query($query, $params)) return null;
$statement->setFetchMode(\PDO::FETCH_INTO, new $className);
return $statement->fetchAll();
}
/**
* Prepare and send a query returning the PDOStatement
*
* @param string $query query string
))
return $statement->rowCount();
}
/**
* Returns array containing all field names
* @param $table
* @return array
*/
static function getColumns($table) {
switch (self::$type) {
case "SQLITE":
return self::fetch("PRAGMA table_info($table)", null, 1);
break;
}
}
}
web/system/vendor/StackTracePrint.php
1010
1111
1212
13
13
1414
1515
1616
$ret = "<pre style='background: #eee; border: 1px solid #aaa; clear: both; overflow: auto; padding: 10px; text-align: left; margin-bottom: 5px'>";
$ret .- "<b>".htmlspecialchars(trim($code[$backtrace[0]['line']-1]))."</b>\n";
$ret .= "<b>".htmlspecialchars(trim($code[$backtrace[0]['line']-1]))."</b>\n";
$ret .= "\n";
web/system/vendor/h2o.php
11
2
23
34
45
<?php
define('H2O_VERSION', '0.3');
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
defined('H2O_ROOT') or define('H2O_ROOT', dirname(__FILE__) . DS);

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.13610s using 14 queries.