-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathframework.php
More file actions
129 lines (120 loc) · 3.12 KB
/
framework.php
File metadata and controls
129 lines (120 loc) · 3.12 KB
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
<?php
namespace NC;
define("NC", 1);
define("BASE", __DIR__);
define("DS", DIRECTORY_SEPARATOR);
class base {
public $db;
public $config;
public $session;
public $cookie;
public $request;
private $command;
private $controller;
public $posts;
public $baseurl;
public $baselink;
function __construct($config = array()) {
$this->config = $config;
$this->session = (object) $_SESSION;
$this->cookie = (object) $_COOKIE;
$this->posts=(object)$_REQUEST;
$route = $_SERVER['PHP_SELF'];
$route = explode("index.php/",$route);
$this->baseurl=str_replace("/index.ph","",substr($route[0],0,strlen($route[0])-1));
if(isset($route[1])){
$this->command = $route[1];
//die($this->command);
} else {
$this->command = "site";
}
$this->request=explode("/",$this->command);
}
public static function getInstance($config = array()) {
static $instance;
if (!$instance) {
$instance = new base($config);
}
return $instance;
}
function run() {
$db = "\NC\system\db\\" . $this->config->db->driver;
$this->db = new $db($this->config->db);
$controllername = strtolower($this->request[0]);
$action = "action";
$action .= isset($this->request[1]) ? ucfirst($this->request[1]) : "Index";
if($controllername=="system"){
$this->controller = new \NC\system\Controller;
}else{
$controller = ucfirst($controllername) . "Controller";
$this->controller = new $controller;
}
if(method_exists($this->controller,$action)){
$this->controller->$action();
}else{
$this->controller->render(strtolower(str_replace("action","",$action)));
}
}
function setCookie($name,$value) {
$expire = time() + 60 * 60 * 24 * 30;
setcookie($name, serialize($value), $expire);
}
function getCookie($name){
if(!isset($_COOKIE[$name])) {
return false;
} else {
return unserialize($_COOKIE[$name]);
}
}
function clearCookie(){
if (isset($_SERVER['HTTP_COOKIE'])) {
$cookies = explode(';', $_SERVER['HTTP_COOKIE']);
foreach($cookies as $cookie) {
$parts = explode('=', $cookie);
$name = trim($parts[0]);
setcookie($name, '', time()-1000);
setcookie($name, '', time()-1000, '/');
}
}
}
}
function load($namespace) {
$splitpath = explode('\\', $namespace);
$path = '';
$name = '';
$firstword = true;
for ($i = 0; $i < count($splitpath); $i++) {
if ($splitpath[$i] && !$firstword) {
if ($i == count($splitpath) - 1) {
$name = $splitpath[$i];
} else {
$path .= DS . $splitpath[$i];
}
}
if ($splitpath[$i] && $firstword) {
if ($splitpath[$i] != __NAMESPACE__) {
break;
}
$firstword = false;
}
}
if (!$firstword) {
$fullpath = __DIR__ . $path . DS . $name . '.php';
if(file_exists($fullpath)){
return include_once ($fullpath);
}
} else {
if (strpos($splitpath[0], "Controller")) {
$fullpath = BASE . DS . 'controllers' . DS . $splitpath[0] . '.php';
if(file_exists($fullpath)){
return include_once ($fullpath);
}
}
}
return false;
}
function loadPath($absPath) {
return include_once ($absPath);
}
spl_autoload_register(__NAMESPACE__ . '\load');
?>