1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if (!defined('BASEPATH')) { |
4
|
|
|
exit('No direct script access allowed'); |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
use Monolog\Handler\StreamHandler; |
8
|
|
|
use Propel\Runtime\Propel; |
9
|
|
|
use Propel\Runtime\Connection\ConnectionManagerSingle; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Executing custom CMS initialication code |
13
|
|
|
*/ |
14
|
|
|
class Lib_init |
15
|
|
|
{ |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* @var MY_Controller |
20
|
|
|
*/ |
21
|
|
|
private $CI; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Lib_init constructor. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() { |
27
|
|
|
|
28
|
|
|
try { |
29
|
|
|
|
30
|
|
|
$this->CI = get_instance(); |
31
|
|
|
|
32
|
|
|
if (function_exists('date_default_timezone_set')) { |
33
|
|
|
date_default_timezone_set(config_item('default_time_zone')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// Sessions engine should run on cookies to minimize opportunities |
37
|
|
|
// of session fixation attack |
38
|
|
|
ini_set('session.use_only_cookies', 1); |
39
|
|
|
|
40
|
|
|
$this->CI->load->library('native_session', '', 'session'); |
41
|
|
|
|
42
|
|
|
$this->CI->load->library('cache'); |
43
|
|
|
|
44
|
|
|
if (!$this->checkIsInstalled()) { // if not installed then not all bootstrap needed |
|
|
|
|
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->initDatabase(); |
49
|
|
|
|
50
|
|
|
$this->initPropel(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @todo |
54
|
|
|
* 1. Delete this line |
55
|
|
|
* 2. Kill all eval && hooks before delete this line |
56
|
|
|
*/ |
57
|
|
|
$this->CI->load->library('cms_hooks'); |
58
|
|
|
|
59
|
|
|
$this->CI->load->library('lib_category'); // needs database |
60
|
|
|
|
61
|
|
|
// Redirect to url with out ending slash |
62
|
|
|
$uri = $this->_detect_uri(); |
63
|
|
|
|
64
|
|
|
$first_segment = $this->CI->uri->segment(1); |
65
|
|
|
if (substr($uri, -1, 1) === '/' && $first_segment !== 'admin' && $uri !== '/') { |
66
|
|
|
$get_params = ''; |
67
|
|
|
if ($this->CI->input->get()) { |
68
|
|
|
$get_params = '?' . http_build_query($this->CI->input->get()); |
69
|
|
|
} |
70
|
|
|
redirect(substr($uri, 0, -1) . $get_params, 'location', 301); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->initShop(); |
74
|
|
|
|
75
|
|
|
} catch (Exception $ex) { |
76
|
|
|
log_message('error', $ex->getMessage()); |
77
|
|
|
show_error('Init error', 500); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function checkIsInstalled() { |
82
|
|
|
$isNotInstalledInConfig = !config_item('is_installed'); |
83
|
|
|
$installControllerExists = file_exists(getModulePath('install') . '/install.php'); |
84
|
|
|
|
85
|
|
|
if (!$isNotInstalledInConfig && !$installControllerExists) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Not installed |
90
|
|
|
if ($isNotInstalledInConfig && $installControllerExists) { |
91
|
|
|
if ($this->CI->uri->segment(1) != 'install') { |
92
|
|
|
redirect("/install"); |
93
|
|
|
} |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Something went bad during installation in past. Both of this variables |
98
|
|
|
// should be either true (not installed) or false (install was complete) |
99
|
|
|
if ($isNotInstalledInConfig && !$installControllerExists) { |
100
|
|
|
show_error('Something went wrong during installation... It could be repaired only manually.'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Load CI_DB_mysqli_driver |
106
|
|
|
* |
107
|
|
|
* @throws Exception |
|
|
|
|
108
|
|
|
*/ |
109
|
|
|
private function initDatabase() { |
110
|
|
|
|
111
|
|
|
// Load DB |
112
|
|
|
$this->CI->load->database(); |
113
|
|
|
|
114
|
|
|
$db = $this->CI->db; |
115
|
|
|
|
116
|
|
|
if ($db == null) { |
117
|
|
|
throw new \Exception('Database object not initialized'); |
118
|
|
|
} |
119
|
|
|
$result = $this->CI->db->query("SHOW TABLES FROM `{$db->database}`"); |
120
|
|
|
|
121
|
|
|
if (!$result) { |
122
|
|
|
throw new \Exception('Error on checking database'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!$result->num_rows > 0) { |
126
|
|
|
throw new \Exception('No tables in database'); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function initPropel() { |
131
|
|
|
$serviceContainer = Propel::getServiceContainer(); |
132
|
|
|
$serviceContainer->setAdapterClass('Shop', 'mysql'); |
133
|
|
|
$manager = new ConnectionManagerSingle(); |
134
|
|
|
|
135
|
|
|
$manager->setConfiguration( |
136
|
|
|
[ |
137
|
|
|
'dsn' => 'mysql:host=' . $this->CI->db->hostname . ';dbname=' . $this->CI->db->database, |
138
|
|
|
'user' => $this->CI->db->username, |
139
|
|
|
'password' => $this->CI->db->password, |
140
|
|
|
'settings' => [ |
141
|
|
|
'charset' => 'utf8' |
142
|
|
|
], |
143
|
|
|
] |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$serviceContainer->setConnectionManager('Shop', $manager); |
147
|
|
|
|
148
|
|
|
Propel::getConnection('Shop')->query('SET NAMES utf8 COLLATE utf8_unicode_ci'); |
149
|
|
|
|
150
|
|
|
// log propel queries |
151
|
|
|
if (ENVIRONMENT == 'development') { |
152
|
|
|
$con = Propel::getWriteConnection('Shop'); |
153
|
|
|
$con->useDebug(true); |
154
|
|
|
$logger = new Monolog\Logger('defaultLogger'); |
155
|
|
|
$logger->pushHandler(new StreamHandler(APPPATH . 'logs/propel.log')); |
156
|
|
|
$serviceContainer->setLogger('defaultLogger', $logger); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
private function initShop() { |
161
|
|
|
|
162
|
|
|
if ($shopPath = getModulePath('shop')) { |
163
|
|
|
|
164
|
|
|
define('SHOP_DIR', $shopPath); |
165
|
|
|
|
166
|
|
|
ShopCore::init(); |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function _detect_uri() { |
172
|
|
|
if (!$this->CI->input->server('REQUEST_URI')) { |
173
|
|
|
return ''; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$uri = $this->CI->input->server('REQUEST_URI'); |
177
|
|
|
if (strpos($uri, $this->CI->input->server('SCRIPT_NAME')) === 0) { |
178
|
|
|
$uri = substr($uri, strlen($this->CI->input->server('SCRIPT_NAME'))); |
179
|
|
|
} elseif (strpos($uri, dirname($this->CI->input->server('SCRIPT_NAME'))) === 0) { |
180
|
|
|
$uri = substr($uri, strlen(dirname($this->CI->input->server('SCRIPT_NAME')))); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct |
184
|
|
|
// URI is found, and also fixes the QUERY_STRING server var and $_GET array. |
185
|
|
|
if (strncmp($uri, '?/', 2) === 0) { |
186
|
|
|
$uri = substr($uri, 2); |
187
|
|
|
} |
188
|
|
|
$parts = preg_split('#\?#i', $uri, 2); |
189
|
|
|
$uri = $parts[0]; |
190
|
|
|
if (isset($parts[1])) { |
191
|
|
|
$_SERVER['QUERY_STRING'] = $parts[1]; |
192
|
|
|
parse_str($this->CI->input->server('QUERY_STRING'), $this->CI->input->get()); |
|
|
|
|
193
|
|
|
} else { |
194
|
|
|
$_SERVER['QUERY_STRING'] = ''; |
195
|
|
|
$_GET = []; |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if ($uri == '/' || empty($uri)) { |
199
|
|
|
return '/'; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$uri = parse_url($uri, PHP_URL_PATH); |
203
|
|
|
return $uri; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
} |