forked from SimpleMachines/SimpleDesk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleDesk-MiscActions.php
More file actions
417 lines (359 loc) · 14.3 KB
/
SimpleDesk-MiscActions.php
File metadata and controls
417 lines (359 loc) · 14.3 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/**************************************************************
* Simple Desk Project - www.simpledesk.net *
***************************************************************
* An advanced help desk modification built on SMF *
***************************************************************
* *
* * Copyright 2025 - SimpleDesk.net *
* *
* This file and its contents are subject to the license *
* included with this distribution, license.txt, which *
* states that this software is New BSD Licensed. *
* Any questions, please contact SimpleDesk.net *
* *
***************************************************************
* SimpleDesk Version: 2.1.0 *
* File Info: SimpleDesk-MiscActions.php *
**************************************************************/
/**
* This file handles miscellaneous actions that aren't really tied to anything, that are mostly
* self-contained and aren't big enough to warrant their own file.
*
* @package source
* @since 1.0
*/
if (!defined('SMF'))
die('Hacking attempt...');
/**
* Marks a ticket unread.
*
* There are no permission checks made; other than that the user is who they claim to be. If a ticket is marked unread
* but they can't see it anyway, the consequence is that the database gets lighter.
*
* Invoked through ?action=helpdesk;sa=unreadticket;ticket=x;sessvar=sessid before redirecting back to the main helpdesk page.
*
* @since 1.0
*/
function shd_ticket_unread()
{
global $smcFunc, $user_info, $context;
is_not_guest();
checkSession('get');
if (!empty($context['ticket_id']))
{
call_integration_hook('shd_hook_markunread');
shd_db_query('', '
DELETE FROM {db_prefix}helpdesk_log_read
WHERE id_ticket = {int:current_ticket}
AND id_member = {int:user}',
array(
'current_ticket' => $context['ticket_id'],
'user' => $user_info['id'],
)
);
}
redirectexit($context['shd_home'] . $context['shd_dept_link']);
}
/**
* Marks a ticket resolved or unresolved.
*
* This function identifies whether a given ticket is resolved or not, if not resolved, mark it resolved. If it was resolved
* reopen the ticket back to an appropriate status based on last respondent. In both cases, the action is logged in the action log.
* It is also unassigned from having a user on either closure or reopen.
*
* Accessed through ?action=helpdesk;sa=resolve;ticket=x;sessvar=sessid before redirecting back to the ticket, or add ;home to the
* URL to have it redirect back to the home page.
*
* @since 1.0
*/
function shd_ticket_resolve()
{
global $smcFunc, $user_info, $context, $sourcedir, $modSettings;
checkSession('get');
// First, figure out the state of the ticket - is it resolved or not? Can we even see it?
if (empty($context['ticket_id']))
shd_fatal_lang_error('shd_no_ticket', false);
$context['shd_return_to'] = isset($_REQUEST['home']) ? 'home' : 'ticket';
$query = shd_db_query('', '
SELECT id_member_started, id_member_updated, status, num_replies, subject, id_dept
FROM {db_prefix}helpdesk_tickets AS hdt
WHERE {query_see_ticket}
AND id_ticket = {int:current_ticket}',
array(
'current_ticket' => $context['ticket_id'],
)
);
if ($smcFunc['db_num_rows']($query) == 0)
{
$smcFunc['db_free_result']($query);
shd_fatal_lang_error('shd_no_ticket', false);
}
$row = $smcFunc['db_fetch_assoc']($query);
$smcFunc['db_free_result']($query);
$action = ($row['status'] != TICKET_STATUS_CLOSED) ? 'resolve' : 'unresolve';
call_integration_hook('shd_hook_mark' . $action);
if (!shd_allowed_to('shd_' . $action . '_ticket_any', $row['id_dept']) && (!shd_allowed_to('shd_' . $action . '_ticket_own', $row['id_dept']) || $row['id_member_started'] != $user_info['id']))
shd_fatal_lang_error('shd_cannot_' . $action, false);
// OK, so what about any children related tickets that are still open? Eeek, could be awkward.
if (empty($modSettings['shd_disable_relationships']))
{
$query = shd_db_query('', '
SELECT COUNT(hdt.id_ticket)
FROM {db_prefix}helpdesk_relationships AS rel
INNER JOIN {db_prefix}helpdesk_tickets AS hdt ON (rel.secondary_ticket = hdt.id_ticket)
WHERE rel.primary_ticket = {int:ticket}
AND rel.rel_type = {int:parent}
AND hdt.status NOT IN ({array_int:closed_status})',
array(
'ticket' => $context['ticket_id'],
'parent' => RELATIONSHIP_ISPARENT,
'closed_status' => array(TICKET_STATUS_CLOSED, TICKET_STATUS_DELETED),
)
);
list($count_children) = $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
if (!empty($count_children))
shd_fatal_lang_error('error_shd_cannot_resolve_children', false);
}
$new = shd_determine_status($action, $row['id_member_started'], $row['id_member_updated'], $row['num_replies'], $row['id_dept']);
shd_db_query('', '
UPDATE {db_prefix}helpdesk_tickets
SET status = {int:status}
WHERE id_ticket = {int:ticket}',
array(
'status' => $new,
'ticket' => $context['ticket_id'],
)
);
shd_log_action($action, array(
'ticket' => $context['ticket_id'],
'subject' => $row['subject'],
));
shd_clear_active_tickets($row['id_dept']);
if ($context['shd_return_to'] == 'home')
redirectexit($context['shd_home'] . $context['shd_dept_link']);
redirectexit('action=helpdesk;sa=ticket;ticket=' . $context['ticket_id']);
}
/**
* Marks a ticket private/not private if the user isn't using Javascript.
*
* Assuming the user can see the ticket and has suitable permissions, the privacy flag will be inverted for the ticket and
* updated, as well as updating the action log. If they cannot see the ticket, or do not have privacy-change permission, a fatal
* error will be generated.
*
* Accessed through ?action=helpdesk;sa=privacychange;ticket=x;sessvar=sessid before directing back to the ticket page.
*
* @since 1.0
*/
function shd_privacy_change_noajax()
{
global $smcFunc, $user_info, $context, $sourcedir;
checkSession('get');
// First, figure out the state of the ticket - is it private or not? Can we even see it?
if (empty($context['ticket_id']))
shd_fatal_lang_error('shd_no_ticket', false);
$query = shd_db_query('', '
SELECT id_member_started, subject, private, status, id_dept
FROM {db_prefix}helpdesk_tickets AS hdt
WHERE {query_see_ticket}
AND id_ticket = {int:current_ticket}',
array(
'current_ticket' => $context['ticket_id'],
)
);
if ($smcFunc['db_num_rows']($query) == 0)
{
$smcFunc['db_free_result']($query);
shd_fatal_lang_error('shd_no_ticket', false);
}
$row = $smcFunc['db_fetch_assoc']($query);
$smcFunc['db_free_result']($query);
if (in_array($row['status'], array(TICKET_STATUS_CLOSED, TICKET_STATUS_DELETED)) || !shd_allowed_to('shd_alter_privacy_any', $row['id_dept']) && (!shd_allowed_to('shd_alter_privacy_own', $row['id_dept']) || $row['id_member_started'] != $user_info['id']))
shd_fatal_lang_error('shd_cannot_change_privacy', false);
$smcFunc['db_free_result']($query);
$action = empty($row['private']) ? 'markprivate' : 'marknotprivate';
require_once($sourcedir . '/sd_source/Subs-SimpleDeskPost.php');
$msgOptions = array();
$posterOptions = array();
$ticketOptions = array(
'id' => $context['ticket_id'],
'private' => empty($row['private']),
);
shd_modify_ticket_post($msgOptions, $ticketOptions, $posterOptions);
shd_log_action($action, array(
'ticket' => $context['ticket_id'],
'subject' => $row['subject'],
));
redirectexit('action=helpdesk;sa=ticket;ticket=' . $context['ticket_id']);
}
/**
* Updates a ticket's urgency up or down one level, if the user isn't using Javascript
*
* Assuming the user can see the ticket and has suitable permissions, the urgency of the ticket will be updated and an
* action log entry added. If they cannot see the ticket, or do not have urgency-change permission (including higher
* urgency permission if necessary), a fatal error will be generated.
*
* Accessed through ?action=helpdesk;sa=urgencychange;ticket=x;change=(increase|decrease);sessvar=sessid before directing back to the main helpdesk page.
*
* @see shd_can_alter_urgency()
*
* @since 1.0
*/
function shd_urgency_change_noajax()
{
global $smcFunc, $user_info, $context, $sourcedir;
checkSession('get');
// First, figure out the state of the ticket - is it private or not? Can we even see it? Current urgency?
if (empty($context['ticket_id']))
shd_fatal_lang_error('shd_no_ticket', false);
$query = shd_db_query('', '
SELECT id_member_started, subject, urgency, status, id_dept
FROM {db_prefix}helpdesk_tickets AS hdt
WHERE {query_see_ticket}
AND id_ticket = {int:current_ticket}',
array(
'current_ticket' => $context['ticket_id'],
)
);
if ($smcFunc['db_num_rows']($query) == 0)
{
$smcFunc['db_free_result']($query);
shd_fatal_lang_error('shd_no_ticket', false);
}
$row = $smcFunc['db_fetch_assoc']($query);
$smcFunc['db_free_result']($query);
$can_urgency = shd_can_alter_urgency($row['urgency'], $row['id_member_started'], ($row['status'] == TICKET_STATUS_CLOSED), ($row['status'] == TICKET_STATUS_DELETED), $row['id_dept']);
if (empty($_GET['change']) || empty($can_urgency[$_GET['change']]))
shd_fatal_lang_error('shd_cannot_change_urgency');
$new_urgency = $row['urgency'] + ($_GET['change'] == 'increase' ? 1 : -1);
$action = 'urgency_' . $_GET['change'];
require_once($sourcedir . '/sd_source/Subs-SimpleDeskPost.php');
$msgOptions = array();
$posterOptions = array();
$ticketOptions = array(
'id' => $context['ticket_id'],
'urgency' => $new_urgency,
);
shd_modify_ticket_post($msgOptions, $ticketOptions, $posterOptions);
shd_log_action($action, array(
'ticket' => $context['ticket_id'],
'subject' => $row['subject'],
'urgency' => $new_urgency,
));
redirectexit('action=helpdesk;sa=ticket;ticket=' . $context['ticket_id']);
}
/**
* Updates a ticket's relationships.
*
* This function is responsible for adding, updating and removing relationships.
*
* Accessed through ?action=helpdesk;sa=relation;ticket=x;linkticket=y;relation=[linked|duplicated|parent|child|delete];sessvar=sessid and will redirect back to the ticket once complete.
*
* @since 2.0
*/
function shd_ticket_relation()
{
global $context, $smcFunc, $modSettings;
checkSession('request');
if (!empty($modSettings['shd_disable_relationships']))
shd_fatal_lang_error('shd_relationships_are_disabled', false);
$otherticket = isset($_REQUEST['otherticket']) ? (int) $_REQUEST['otherticket'] : 0;
if (empty($context['ticket_id']) || empty($otherticket))
shd_fatal_lang_error('shd_no_ticket', false);
elseif ($context['ticket_id'] == $otherticket)
shd_fatal_lang_error('shd_cannot_relate_self', false);
$actions = array(
'linked',
'duplicated',
'parent',
'child',
'delete',
);
$rel_action = isset($_REQUEST['relation']) && in_array($_REQUEST['relation'], $actions) ? $_REQUEST['relation'] : '';
if (empty($rel_action))
shd_fatal_lang_error('shd_invalid_relation', false);
// Quick/consistent way to ensure permissions are adhered to and that the ticket exists. Might as well get the subject while here too.
$ticketinfo = shd_load_ticket($context['ticket_id']);
$primary_subject = $ticketinfo['subject'];
$dept = $ticketinfo['dept'];
$ticketinfo = shd_load_ticket($otherticket);
$secondary_subject = $ticketinfo['subject'];
shd_is_allowed_to($rel_action == 'delete' ? 'shd_delete_relationships' : 'shd_create_relationships', $dept);
// See if there's an existing relationship with these parameters
$query = shd_db_query('', '
SELECT rel_type
FROM {db_prefix}helpdesk_relationships
WHERE primary_ticket = {int:ticket}
AND secondary_ticket = {int:otherticket}',
array(
'ticket' => $context['ticket_id'],
'otherticket' => $otherticket,
)
);
$new_relationship = ($smcFunc['db_num_rows']($query) == 0);
list($existing_relation) = $new_relationship ? array(-1) : $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
if ($rel_action == 'delete' && $new_relationship)
shd_fatal_lang_error('shd_no_relation_delete', false);
$log_prefix = 'rel_' . ($new_relationship || $rel_action == 'delete' ? '' : 're_');
$log_map = array(
'delete' => 'delete',
'linked' => 'linked',
'duplicated' => 'duplicated',
'parent' => 'child',
'child' => 'parent',
);
$log_assoc_ids = array(
'delete' => -1, // any value not below, really
'linked' => RELATIONSHIP_LINKED,
'duplicated' => RELATIONSHIP_DUPLICATED,
'parent' => RELATIONSHIP_ISPARENT,
'child' => RELATIONSHIP_ISCHILD,
);
$logaction_ticket = $log_prefix . $rel_action;
$logaction_otherticket = $log_prefix . $log_map[$rel_action];
// The "from" ticket is $context['ticket_id'], $otherticket is the ticket whose id the user gives, and $rel_action sets the relationship type.
call_integration_hook('shd_hook_relations', array(&$rel_action, &$otherticket));
// Delete the link
if ($rel_action == 'delete')
shd_db_query('', '
DELETE FROM {db_prefix}helpdesk_relationships
WHERE (primary_ticket = {int:ticket} AND secondary_ticket = {int:otherticket})
OR (primary_ticket = {int:otherticket} AND secondary_ticket = {int:ticket})',
array(
'ticket' => $context['ticket_id'],
'otherticket' => $otherticket,
)
);
else
$smcFunc['db_insert']('replace',
'{db_prefix}helpdesk_relationships',
array(
'primary_ticket' => 'int', 'secondary_ticket' => 'int', 'rel_type' => 'int',
),
array(
array($context['ticket_id'], $otherticket, $log_assoc_ids[$rel_action]),
array($otherticket, $context['ticket_id'], $log_assoc_ids[$log_map[$rel_action]]),
),
array('primary_ticket', 'secondary_ticket')
);
// Now actually log it, main ticket first -- if it's different to the one we had before
if ($log_assoc_ids[$rel_action] != $existing_relation)
{
shd_log_action($logaction_ticket, array(
'ticket' => $context['ticket_id'],
'otherticket' => $otherticket,
'subject' => $primary_subject,
'othersubject' => $secondary_subject,
));
shd_log_action($logaction_otherticket, array(
'ticket' => $otherticket,
'otherticket' => $context['ticket_id'],
'subject' => $secondary_subject,
'othersubject' => $primary_subject,
));
}
// See yah
redirectexit('action=helpdesk;sa=ticket;ticket=' . $context['ticket_id']);
}