-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathevents.php
More file actions
279 lines (251 loc) · 7.71 KB
/
events.php
File metadata and controls
279 lines (251 loc) · 7.71 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
namespace KVSun\Events;
use \shgysk8zer0\Core\{
Console,
PDO,
Listener,
FormData,
Gravatar,
Timer,
JSON_Response as Resp
};
use \shgysk8zer0\Login\{User};
use \shgysk8zer0\DOM\{
HTML,
HTMLElement,
RSS
};
use \shgysk8zer0\Authorize\{
ShippingAddress
};
use const \KVSun\Consts\{
ICONS,
DOMAIN,
PASSWD,
LOGGED_IN_ONLY,
LOGGED_OUT_ONLY,
DEBUG,
ERROR_LOG
};
use function \KVSun\Functions\{
user_can,
email,
html_email,
build_rss,
add_e_edition
};
/**
* Responds to login events
* @param User $user The user that just logged in
* @param boolean $remember Whether or not to set the login cookie
* @return Resp JSON_Response instance with notification, etc.
*/
function login_handler(User $user, Bool $remember = true): Resp
{
try {
if ($remember) {
$user->setCookie('user', PASSWD);
}
$resp = Resp::getInstance();
$grav = new Gravatar($user->email, 64);
$user->setSession('user');
$resp->notify('Login Successful', "Welcome back, {$user->name}", "{$grav}");
$resp->close('#login-dialog');
$resp->clear('login');
$resp->enable(join(', ', LOGGED_IN_ONLY));
$resp->disable(join(', ', LOGGED_OUT_ONLY));
$resp->attributes('#user-avatar', 'src', "$grav");
if (user_can('eEdition')) {
$resp->before('#user-avatar', add_e_edition(null, [
'class' => 'cat-link',
'role' => 'button',
]));
}
if (user_can('createPosts', 'editPosts')) {
$resp->attributes('main', 'contextmenu', 'admin_menu');
}
//$avatar->data_load_form = 'update-user';
$resp->attributes('#user-avatar', 'data-load-form', 'update-user');
$resp->attributes('#user-avatar', 'data-show-modal', false);
} catch (\Throwable $e) {
trigger_error($e);
} finally {
return $resp;
}
}
/**
* Handles logout events
* @param User $user The user logging out
* @return Resp JSON_Response instance with notification, etc.
*/
function logout_handler(User $user): Resp
{
try {
$user->logout();
$resp = Resp::getInstance();
$resp->notify('Success', 'You have been logged out.', DOMAIN . ICONS['sign-out']);
$resp->close('dialog[open]');
$resp->remove('#update-user-dialog, #admin_menu, #E-Edition-link');
$resp->attributes('#user-avatar', 'src', DOMAIN . ICONS['sign-in']);
$resp->attributes('#user-avatar', 'data-load-form', false);
$resp->attributes('#user-avatar', 'data-show-modal', '#login-dialog');
$resp->attributes('[contextmenu="admin_menu"]', 'contextmenu', false);
$resp->enable(join(', ', LOGGED_OUT_ONLY));
$resp->disable(join(', ', LOGGED_IN_ONLY));
} catch (\Throwable $e) {
trigger_error($e);
} finally {
return $resp;
}
}
/**
* Handles error events
* @param Int $severity E_* error level
* @param String $message Error message
* @param String $file File the error occured in
* @param Int $line Line the error occured on
* @return Bool True to prevent default error handling
*/
function error_handler(Int $severity, String $message, String $file, Int $line): Bool
{
$err = new \ErrorException($message, 0, $severity, $file, $line);
error_log($err . PHP_EOL, 3,ERROR_LOG);
return true;
}
/**
* Handles exception events
* @param Throwable $e Exception or Error
* @return void
*/
function exception_handler(\Throwable $e)
{
error_log($e . PHP_EOL, 3, ERROR_LOG);
}
/**
* Handles error events with debugging info for developers
* @param Int $severity E_* error level
* @param String $message Error message
* @param String $file File the error occured in
* @param Int $line Line the error occured on
* @return Bool True to prevent default error handling
*/
function dev_error_handler(Int $severity, String $message, String $file, Int $line): Bool
{
Console::error([
'message' => $message,
'file' => $file,
'line' => $line,
'trace' => debug_backtrace(),
]);
return true;
}
/**
* Handles exception events with debugging for developers
* @param Throwable $e Exception or Error
* @return void
*/
function dev_exception_handler(\Throwable $e)
{
Console::error([
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace(),
]);
}
/**
* Build RSS feed on new/updated posts
* @param FormData $post Data submitted by form
* @return void
*/
function content_posted(FormData $post)
{
try {
$cat = str_replace(' ', '-', strtolower($post->category));
$rss = build_rss($cat);
$rss->save("rss/{$cat}.rss");
} catch (\Throwable $e) {
trigger_error($e->getMessage());
}
}
/**
* Send email notification on new comments
* @param User $user User posting comment
* @param String $url URL comment posted on
* @param FormData $comment Comment data from submitted form
* @return Bool Whether or not the email was sent
*/
function comment_posted(User $user, String $url, FormData $comment): Bool
{
$to = ['editor@kvsun.com'];
$subject = "New comment on {$_SERVER['HTTP_HOST']}";
$headers = ['From' => 'notifications@kvsun.com'];
$message = new HTML;
$body = $message->body;
$body->append('h3', "New comment by {$user->name}");
$body->append('a', $url, ['href' => $url]);
$body->append('blockquote', $comment->text);
return html_email($to, $subject, $message, $headers);
}
/**
* Event triggered to notify circulation of a new print subscription by email
* @param User $user User that made the transaction
* @param stdClass $sub Subscription info from database
* @param ShippingAddress $address Address entered in form
* @return Bool Whether or not the email was sent
*/
function print_subscription(User $user, \stdClass $sub, ShippingAddress $address): Bool
{
$to = ['circulation@kvsun.com'];
$subject = 'New print subscription made online';
$headers = ['From' => 'notifications@kvsun.com'];
$message = new HTML();
$expires = new \DateTime($sub->term);
$body = $message->body;
$body->append('h3', "New {$sub->name} subscription for {$user->name}");
$info = $body->append('ul');
$info->append('li', "Subscriber: {$address->first_name} {$address->last_name}");
$info->append('li', "Expires: {$expires->format('D. M j, Y')}");
$info->append('li', "Address: {$address->address}, {$address->city}, {$address->state} {$address->zip}");
return html_email($to, $subject, $message, $headers);
}
new Listener('error', __NAMESPACE__ . '\error_handler');
new Listener('exception', __NAMESPACE__ . '\exception_handler');
new Listener('login', __NAMESPACE__ . '\login_handler');
new Listener('logout', __NAMESPACE__ . '\logout_handler');
new Listener('printSubscription', __NAMESPACE__ . '\print_subscription');
new Listener('contentPosted', __NAMESPACE__ . '\content_posted');
new Listener('contentUpdated', __NAMESPACE__ . '\content_posted');
new Listener('commentPosted', __NAMESPACE__ . '\comment_posted');
if (user_can('debug') or DEBUG) {
$timer = new Timer();
new Listener('error', __NAMESPACE__ . '\dev_error_handler');
new Console('exception', __NAMESPACE__ . '\dev_exception_handler');
new Listener('load', function() use ($timer)
{
Console::info([
'system' => [
'time' => "{$timer} s",
'memory' => (memory_get_peak_usage(true) / 1024) . ' kb',
],
'resources' => [
'files' => get_included_files(),
'path' => explode(PATH_SEPARATOR, get_include_path()),
'functions' => get_defined_functions(true)['user'],
'constansts' => get_defined_constants(true)['user'],
'classes' => [
'classes' => get_declared_classes(),
'interfaces' => get_declared_interfaces(),
'traits' => get_declared_traits(),
],
],
'globals' => [
'_SERVER' => $_SERVER,
'_REQUEST' => $_REQUEST,
'_SESSION' => $_SESSION ?? [],
'_COOKIE' => $_COOKIE,
],
])->sendLogHeader();
});
unset($timer);
}