1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Všeobecně sdílený objekt frameworku. |
4
|
|
|
* Tento objekt je automaticky přez svůj singleton instancován do každého Ease* |
5
|
|
|
* objektu. |
6
|
|
|
* Poskytuje kdykoliv přístup k často volaným objektům framworku jako například |
7
|
|
|
* uživatel, databáze, webstránka nebo logy. |
8
|
|
|
* Také obsahuje pole obecnych nastavení a funkce pro jeho obluhu. |
9
|
|
|
* |
10
|
|
|
* @author Vitex <[email protected]> |
11
|
|
|
* @copyright 2009-2018 [email protected] (G) |
12
|
|
|
* |
13
|
|
|
* PHP 7 |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Ease; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Všeobecně sdílený objekt frameworku. |
20
|
|
|
* Tento objekt je automaticky přez svůj singleton instancován do každého Ease* |
21
|
|
|
* objektu. |
22
|
|
|
* Poskytuje kdykoliv přístup k často volaným objektům framworku jako například |
23
|
|
|
* uživatel, databáze, webstránka nebo logy. |
24
|
|
|
* Také obsahuje pole obecnych nastavení a funkce pro jeho obluhu. |
25
|
|
|
* |
26
|
|
|
* @copyright 2009-2016 [email protected] (G) |
27
|
|
|
* @author Vitex <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Shared extends Atom |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Pole konfigurací. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
public $configuration = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Saves obejct instace (singleton...). |
40
|
|
|
* |
41
|
|
|
* @var Shared |
42
|
|
|
*/ |
43
|
|
|
private static $instance = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Array of Status Messages |
47
|
|
|
* |
48
|
|
|
* @var array of Logger\Message |
49
|
|
|
*/ |
50
|
|
|
public $messages = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* session item name with user object |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
public static $userSessionName = 'User'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Inicializace sdílené třídy. |
61
|
|
|
*/ |
62
|
|
|
public function __construct() |
63
|
|
|
{ |
64
|
|
|
$cgiMessages = []; |
65
|
|
|
$webMessages = []; |
66
|
|
|
$prefix = defined('EASE_APPNAME') ? constant('EASE_APPNAME') : ''; |
67
|
|
|
$msgFile = sys_get_temp_dir().'/'.$prefix.'EaseStatusMessages'.posix_getuid().'.ser'; |
68
|
|
|
if (file_exists($msgFile) && is_readable($msgFile) && filesize($msgFile) |
69
|
|
|
&& is_writable($msgFile) |
70
|
|
|
) { |
71
|
|
|
$cgiMessages = unserialize(file_get_contents($msgFile)); |
72
|
|
|
file_put_contents($msgFile, ''); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (defined('EASE_APPNAME')) { |
76
|
|
|
if (isset($_SESSION[constant('EASE_APPNAME')]['EaseMessages'])) { |
77
|
|
|
$webMessages = $_SESSION[constant('EASE_APPNAME')]['EaseMessages']; |
78
|
|
|
unset($_SESSION[constant('EASE_APPNAME')]['EaseMessages']); |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
|
|
if (isset($_SESSION['EaseMessages'])) { |
82
|
|
|
$webMessages = $_SESSION['EaseMessages']; |
83
|
|
|
unset($_SESSION['EaseMessages']); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
$this->statusMessages = is_array($cgiMessages) ? array_merge( |
87
|
|
|
$cgiMessages, $webMessages |
88
|
|
|
) : $webMessages; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako konstruktor) |
93
|
|
|
* se bude v ramci behu programu pouzivat pouze jedna jeho Instance (ta prvni). |
94
|
|
|
* |
95
|
|
|
* @param string $class název třídy jenž má být zinstancována |
96
|
|
|
* |
97
|
|
|
* @link http://docs.php.net/en/language.oop5.patterns.html Dokumentace a priklad |
98
|
|
|
* |
99
|
|
|
* @return \Ease\Shared |
100
|
|
|
*/ |
101
|
|
|
public static function singleton() |
102
|
|
|
{ |
103
|
|
|
if (!isset(self::$instance)) { |
104
|
|
|
self::$instance = new self(); |
105
|
|
|
} |
106
|
|
|
return self::$instance; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Vrací se. |
111
|
|
|
* |
112
|
|
|
* @return Shared |
113
|
|
|
*/ |
114
|
|
|
public static function &instanced() |
115
|
|
|
{ |
116
|
|
|
$easeShared = self::singleton(); |
117
|
|
|
|
118
|
|
|
return $easeShared; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Nastavuje hodnotu konfiguračního klíče. |
123
|
|
|
* |
124
|
|
|
* @param string $configName klíč |
125
|
|
|
* @param mixed $configValue hodnota klíče |
126
|
|
|
*/ |
127
|
|
|
public function setConfigValue($configName, $configValue) |
128
|
|
|
{ |
129
|
|
|
$this->configuration[$configName] = $configValue; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Vrací konfigurační hodnotu pod klíčem. |
134
|
|
|
* |
135
|
|
|
* @param string $configName klíč |
136
|
|
|
* |
137
|
|
|
* @return mixed |
138
|
|
|
*/ |
139
|
|
|
public function getConfigValue($configName) |
140
|
|
|
{ |
141
|
|
|
return array_key_exists($configName, $this->configuration) ? $this->configuration[$configName] |
142
|
|
|
: null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Vrací instanci objektu logování. |
147
|
|
|
* |
148
|
|
|
* @return Logger |
149
|
|
|
*/ |
150
|
|
|
public static function logger() |
151
|
|
|
{ |
152
|
|
|
return Logger\Regent::singleton(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Write remaining messages to temporary file. |
157
|
|
|
*/ |
158
|
|
|
public function __destruct() |
159
|
|
|
{ |
160
|
|
|
if (php_sapi_name() == 'cli') { |
161
|
|
|
$prefix = defined('EASE_APPNAME') ? constant('EASE_APPNAME') : ''; |
162
|
|
|
$messagesFile = sys_get_temp_dir().'/'.$prefix.'EaseStatusMessages'.posix_getuid().'.ser'; |
163
|
|
|
file_put_contents($messagesFile, serialize($this->statusMessages)); |
164
|
|
|
} else { |
165
|
|
|
if (defined('EASE_APPNAME')) { |
166
|
|
|
$_SESSION[constant('EASE_APPNAME')]['EaseMessages'] = $this->statusMessages; |
167
|
|
|
} else { |
168
|
|
|
$_SESSION['EaseMessages'] = $this->statusMessages; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Vrací, případně i založí objekt uživatele. |
175
|
|
|
* |
176
|
|
|
* @param User|Anonym|string $user objekt nového uživatele nebo |
177
|
|
|
* název třídy |
178
|
|
|
* |
179
|
|
|
* @return User |
180
|
|
|
*/ |
181
|
|
|
public static function &user($user = null, $userSessionName = 'User') |
182
|
|
|
{ |
183
|
|
|
$efprefix = defined('EASE_APPNAME') ? constant('EASE_APPNAME') : 'EaseFramework'; |
184
|
|
|
if (is_null($user) && isset($_SESSION[$efprefix][self::$userSessionName]) |
185
|
|
|
&& is_object($_SESSION[$efprefix][self::$userSessionName])) { |
186
|
|
|
return $_SESSION[$efprefix][self::$userSessionName]; |
187
|
|
|
} |
188
|
|
|
if (!is_null($userSessionName)) { |
189
|
|
|
self::$userSessionName = $userSessionName; |
190
|
|
|
} |
191
|
|
|
if (is_object($user)) { |
192
|
|
|
$_SESSION[$efprefix][self::$userSessionName] = clone $user; |
193
|
|
|
} else { |
194
|
|
|
if (class_exists($user)) { |
195
|
|
|
$_SESSION[$efprefix][self::$userSessionName] = new $user(); |
196
|
|
|
} elseif (!isset($_SESSION[$efprefix][self::$userSessionName]) || !is_object($_SESSION[$efprefix][self::$userSessionName])) { |
197
|
|
|
$_SESSION[$efprefix][self::$userSessionName] = new Anonym(); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
return $_SESSION[$efprefix][self::$userSessionName]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Load Configuration values from json file $this->configFile and define UPPERCASE keys |
205
|
|
|
* |
206
|
|
|
* @param string $configFile Path to file with configuration |
207
|
|
|
* @param boolean $defineConstants false to do not define constants |
208
|
|
|
* |
209
|
|
|
* @return array full configuration array |
210
|
|
|
*/ |
211
|
|
|
public function loadConfig($configFile, $defineConstants = false) |
212
|
|
|
{ |
213
|
|
|
if (!file_exists($configFile)) { |
214
|
|
|
throw new Exception( |
215
|
|
|
'Config file '.(realpath($configFile) ? realpath($configFile) : $configFile).' does not exist' |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
$configuration = json_decode(file_get_contents($configFile), true); |
219
|
|
|
foreach ($configuration as $configKey => $configValue) { |
220
|
|
|
if ($defineConstants && (strtoupper($configKey) == $configKey) && (!defined($configKey))) { |
221
|
|
|
define($configKey, $configValue); |
222
|
|
|
} else { |
223
|
|
|
$this->setConfigValue($configKey, $configValue); |
224
|
|
|
} |
225
|
|
|
$this->configuration[$configKey] = $configValue; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if (array_key_exists('debug', $this->configuration)) { |
229
|
|
|
$this->debug = boolval($this->configuration['debug']); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $this->configuration; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|