forked from dszulist/mkphp-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMK.php
More file actions
156 lines (142 loc) · 5.1 KB
/
MK.php
File metadata and controls
156 lines (142 loc) · 5.1 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
<?php
/**
* MK
*
* @category MK
* @package MK
* @author bskrzypkowiak
*/
class MK
{
/**
* Autoloader dla MKPhp
*
* @param String $className
*
* @return Boolean
*/
public static function _autoload($className)
{
if(substr($className, 0, 3) == 'MK_') { //todo tu jest błąd bo nie załaduje MK::someMethod(); :)
include (substr(MK_PATH, 0, -3) . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php');
return true;
}
return false;
}
/**
* Funkcja wywoływana na zakończenie skryptu PHP
* w przypadku gdy skrypt kończy się błędem uruchamia funkcje powiadamiajaca o błędzie
*/
public static function shutdownFunction()
{
$error = error_get_last();
if(!empty($error)) {
MK_Error::handler(isset($error['type']) ? $error['type'] : null, isset($error['message']) ? $error['message'] : null, isset($error['file']) ? $error['file'] : null, isset($error['line']) ? $error['line'] : null);
}
}
/**
* Uruchamia kontroler konsoli i wywołuje z niego metode z parametrami
*
* @param Array $argv - (default:array())Tablica z parametrami przekazanymi w lini polecen
*
* @return Boolean
*/
public static function executeCLICommand(array $argv)
{
if(empty($argv)) {
return;
}
if(version_compare(PHP_VERSION, '5.3.0', '>=') && APP_NAME !== 'spirb' && APP_NAME !== 'docflow') { // Tymczasowy "myk" dla SPiRB-a z APP_NAME
$consoleController = strtolower(APP_NAME) . '\controller\Console';
} else {
$consoleController = ucfirst(APP_NAME) . '_Controller_Console';
}
if(class_exists($consoleController)) {
$consoleController = new $consoleController();
} elseif(class_exists('ConsoleController')) {
/** @noinspection PhpUndefinedClassInspection */
$consoleController = new ConsoleController();
} else {
$consoleController = new MK_Controller_Console();
}
if(count($argv) >= 2) {
$optArgv = array_slice($argv, 2);
} else {
$optArgv = array ();
}
$optArray = getopt('m::');
if(empty($optArray)) {
return;
}
foreach ($optArray as $optKey => $optValue) {
switch ($optKey) {
case 'm':
if(is_array($optValue)) {
foreach ($optValue as $value) {
if(!method_exists($consoleController, $value)) {
exit("Brak funkcji {$value}\n");
}
$consoleController->{$value}($optArgv);
}
} else {
if(!method_exists($consoleController, $optValue)) {
exit("Brak funkcji {$optValue}\n");
}
$consoleController->{$optValue}($optArgv);
}
break;
}
}
exit;
}
/**
* Sprawdza czy rządanie jest wysłane za pomocą XMLHttpRequest (AJAX)
* w przypadku ajax'a zwraca true
* w pozostałych przypadkach false
*
* @param Boolean $sendHeaders (default:false) - jezeli true to wyśle nagłówki dla typu JSON i z wyłączonym caschowaniem
*
* @return Boolean
*/
public static function isAjaxExecution($sendHeaders = false)
{
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
if($sendHeaders) {
MK::sendJSONHeaders();
}
return true;
}
return false;
}
/**
* Sprawdza czy istnieje plik blokujacy uzycie aplikacji
*/
public static function checkApplicationState()
{
if(file_exists(APP_FILE_LOCK)) {
if(strpos(file_get_contents(APP_FILE_LOCK), 'upgrade') !== false) {
$msg = '<h1>Przerwa techniczna</h1><br/>Proszę spróbować za 10 minut.<br/>Proszę nie wyłączać i nie restartować serwera.';
if(self::isAjaxExecution(true)) {
echo json_decode(array ("success" => false, "msg" => $msg));
} else {
if(file_exists(MK_DIR_VIEW_ERRORS . DIRECTORY_SEPARATOR . 'upgrade.html')) {
include(MK_DIR_VIEW_ERRORS . DIRECTORY_SEPARATOR . 'upgrade.html');
} else {
header("Content-type: text/html; charset=utf-8");
echo $msg;
}
}
die;
}
}
}
/**
* Tworzy nagłówki JSON, i ustawia brak cashowania (do obsługi zapytan typu XHR)
*/
public static function sendJSONHeaders()
{
header('Cache-Control: no-cache, must-revalidate');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() - 10) . ' GMT');
header('Content-type: application/json');
}
}