-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.php
More file actions
174 lines (150 loc) · 5.01 KB
/
index.php
File metadata and controls
174 lines (150 loc) · 5.01 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
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
<?php
/**
* Megabre StokMaster Pro
* Index Page - Main Router
*
* @author Ali Çömez / Slaweally
* @website https://megabre.com
*/
// Include configuration
require_once 'config/config.php';
// Include core files
require_once CORE_PATH . 'Database.php';
require_once CORE_PATH . 'Session.php';
require_once CORE_PATH . 'Authentication.php';
require_once CORE_PATH . 'Language.php';
require_once CORE_PATH . 'Cache.php';
require_once CORE_PATH . 'DynamicFields.php';
require_once CORE_PATH . 'helpers.php';
// Initialize authentication
$auth = new Authentication();
// Check if logged in, redirect to login if not
if (!$auth->isLoggedIn()) {
redirect('login.php');
}
// Handle language change
if (isset($_GET['language'])) {
$lang_code = $_GET['language'];
$language = Language::getInstance();
if ($language->setLanguage($lang_code)) {
// Build clean URL without language parameter
$queryString = $_SERVER['QUERY_STRING'] ?? '';
parse_str($queryString, $params);
unset($params['language']);
$cleanQuery = http_build_query($params);
$scriptName = basename($_SERVER['SCRIPT_NAME']);
// Build relative path
$redirectUrl = $scriptName;
if (!empty($cleanQuery)) {
$redirectUrl .= '?' . $cleanQuery;
}
redirect($redirectUrl);
}
}
// Get current user
$current_user = $auth->getCurrentUser();
// Check if language column exists in users table
$db = Database::getInstance();
try {
$db->query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'users' AND COLUMN_NAME = 'language'");
$langColumnExists = $db->single();
if (!$langColumnExists) {
// Add language column
$db->query("ALTER TABLE users ADD COLUMN language VARCHAR(10) DEFAULT 'tr' AFTER email");
$db->execute();
// Reload user data to include language column
$db->query("SELECT * FROM users WHERE id = :id");
$db->bind(':id', $current_user['id']);
$current_user = $db->single();
unset($current_user['password']);
Session::set('user', $current_user);
}
} catch (Exception $e) {
// If error, try alternative method
try {
$db->query("ALTER TABLE users ADD COLUMN language VARCHAR(10) DEFAULT 'tr' AFTER email");
$db->execute();
// Reload user data to include language column
$db->query("SELECT * FROM users WHERE id = :id");
$db->bind(':id', $current_user['id']);
$current_user = $db->single();
unset($current_user['password']);
Session::set('user', $current_user);
} catch (Exception $e2) {
// Column might already exist, ignore error
error_log('Language column addition: ' . $e2->getMessage());
}
}
// Initialize language - prioritize user preference, then session, then default
$language = Language::getInstance();
// Load user language preference if exists
if (!empty($current_user['language'])) {
$language->setLanguage($current_user['language']);
} elseif (Session::exists('language')) {
// Use session language if user preference not set
$language->setLanguage(Session::get('language'));
}
// Make $L, language and auth available globally
$GLOBALS['L'] = $language->getAll();
$GLOBALS['language'] = $language;
$GLOBALS['auth'] = $auth;
// Get settings
$db = Database::getInstance();
$db->query("SELECT * FROM settings");
$settingsResult = $db->resultSet();
$settings = [];
foreach ($settingsResult as $row) {
$settings[$row['setting_key']] = $row['setting_value'];
}
// Get module and action from URL
$module = isset($_GET['module']) ? $_GET['module'] : 'dashboard';
$action = isset($_GET['action']) ? $_GET['action'] : 'index';
// Handle API requests
if ($module === 'api') {
header('Content-Type: application/json');
$apiFile = API_PATH . $action . '.php';
if (file_exists($apiFile)) {
// Set subaction if provided
if (isset($_GET['subaction'])) {
$_GET['action'] = $_GET['subaction'];
}
require_once $apiFile;
exit;
} else {
http_response_code(404);
echo json_encode(['success' => false, 'message' => 'API endpoint not found']);
exit;
}
}
// Validate module
$allowed_modules = [
'dashboard',
'products',
'categories',
'customers',
'stock',
'orders',
'transactions',
'tools',
'settings',
'profile',
'activity'
];
if (!in_array($module, $allowed_modules)) {
$module = 'dashboard';
}
// Build module file path
$module_file = MODULES_PATH . $module . '/' . $action . '.php';
// Check if module file exists
if (file_exists($module_file)) {
require_once $module_file;
} else {
// If action file doesn't exist, try index.php
$module_index = MODULES_PATH . $module . '/index.php';
if (file_exists($module_index)) {
require_once $module_index;
} else {
// If module doesn't exist, redirect to dashboard
redirect('index.php?module=dashboard');
}
}