/
bug_api.php
423 lines (335 loc) · 11.3 KB
/
bug_api.php
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
418
419
420
421
422
423
<?php
# Mantis - a php based bugtracking system
# Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
# Copyright (C) 2002 Mantis Team - mantisbt-dev@lists.sourceforge.net
# This program is distributed under the terms and conditions of the GPL
# See the files README and LICENSE for details
# --------------------------------------------------------
# $Id: bug_api.php,v 1.10 2002-09-16 04:16:59 jfitzell Exp $
# --------------------------------------------------------
###########################################################################
# Bug API
###########################################################################
#===================================
# Caching
#===================================
#########################################
# SECURITY NOTE: cache globals are initialized here to prevent them
# being spoofed if register_globals is turned on
#
$g_cache_bug = array();
$g_cache_bug_text = array();
# Cache a bug row if necessary and return the cached copy
# If the second parameter is true (default), trigger an error
# if the bug can't be found. If the second parameter is
# false, return false if the bug can't be found.
function bug_cache_row( $p_bug_id, $p_trigger_errors=true) {
global $g_cache_bug;
$c_bug_id = db_prepare_int( $p_bug_id );
$t_bug_table = config_get( 'mantis_bug_table' );
if ( isset ( $g_cache_bug[$c_bug_id] ) ) {
return $g_cache_bug[$c_bug_id];
}
$query = "SELECT *
FROM $t_bug_table
WHERE id='$c_bug_id'";
$result = db_query( $query );
if ( 0 == db_num_rows( $result ) ) {
if ( $p_trigger_errors ) {
trigger_error( ERROR_BUG_NOT_FOUND, ERROR );
} else {
return false;
}
}
$row = db_fetch_array( $result );
$g_cache_bug[$c_bug_id] = $row;
return $row;
}
# --------------------
# Clear the bug cache (or just the given id if specified)
function bug_clear_cache( $p_bug_id = null ) {
global $g_cache_bug;
if ( null === $p_bug_id ) {
$g_cache_bug = array();
} else {
$c_bug_id = db_prepare_int( $p_bug_id );
unset( $g_cache_bug[$c_bug_id] );
}
return true;
}
# Cache a bug text row if necessary and return the cached copy
# If the second parameter is true (default), trigger an error
# if the bug text can't be found. If the second parameter is
# false, return false if the bug text can't be found.
function bug_text_cache_row( $p_bug_id, $p_trigger_errors=true) {
global $g_cache_bug_text;
$c_bug_id = db_prepare_int( $p_bug_id );
$t_bug_table = config_get( 'mantis_bug_table' );
$t_bug_text_table = config_get( 'mantis_bug_text_table' );
if ( isset ( $g_cache_bug_text[$c_bug_id] ) ) {
return $g_cache_bug_text[$c_bug_id];
}
$query = "SELECT bt.*
FROM $t_bug_text_table bt, $t_bug_table b
WHERE b.id='$c_bug_id'
AND b.bug_text_id = bt.id";
$result = db_query( $query );
if ( 0 == db_num_rows( $result ) ) {
if ( $p_trigger_errors ) {
trigger_error( ERROR_BUG_NOT_FOUND, ERROR );
} else {
return false;
}
}
$row = db_fetch_array( $result );
$g_cache_bug_text[$c_bug_id] = $row;
return $row;
}
# --------------------
# Clear the bug text cache (or just the given id if specified)
function bug_text_clear_cache( $p_bug_id = null ) {
global $g_cache_bug_text;
if ( null === $p_bug_id ) {
$g_cache_bug_text = array();
} else {
$c_bug_id = db_prepare_int( $p_bug_id );
unset( $g_cache_bug_text[$c_bug_id] );
}
return true;
}
#===================================
# Boolean queries and ensures
#===================================
# --------------------
# check to see if bug exists by id
# return true if it does, false otherwise
function bug_exists( $p_bug_id ) {
$c_bug_id = db_prepare_int( $p_bug_id );
$t_bug_table = config_get( 'mantis_bug_table' );
$query = "SELECT COUNT(*)
FROM $t_bug_table
WHERE id='$c_bug_id'";
$result = db_query( $query );
if ( db_result( $result ) > 0 ) {
return true;
} else {
return false;
}
}
# --------------------
# check to see if bug exists by id
# if it doesn't exist then error
# otherwise let execution continue undisturbed
function bug_ensure_exists( $p_bug_id ) {
if ( ! bug_exists( $p_bug_id ) ) {
trigger_error( ERROR_BUG_NOT_FOUND, ERROR );
}
}
#===================================
# Creation / Deletion / Updating
#===================================
# --------------------
function bug_create() {
}
# --------------------
# allows bug deletion :
# delete the bug, bugtext, bugnote, and bugtexts selected
# used in bug_delete.php & mass treatments
function bug_delete( $p_bug_id ) {
$c_bug_id = db_prepare_int( $p_bug_id );
$t_bug_table = config_get( 'mantis_bug_table' );
$t_bug_text_table = config_get( 'mantis_bug_text_table' );
email_bug_deleted( $p_bug_id );
# Delete bugnotes
bugnote_delete_all( $p_bug_id );
# Delete files
file_delete_attachments( $p_bug_id );
# Delete the bug history
history_delete( $p_bug_id );
# Delete the bugnote text
$t_bug_text_id = bug_get_field( $p_bug_id, 'bug_text_id' );
$query = "DELETE
FROM $t_bug_text_table
WHERE id='$t_bug_text_id'";
db_query( $query );
# Delete the bug entry
$query = "DELETE
FROM $t_bug_table
WHERE id='$c_bug_id'";
db_query( $query );
bug_clear_cache( $p_bug_id );
bug_text_clear_cache( $p_bug_id );
# db_query() errors on failure so:
return true;
}
# --------------------
# Delete all bugs associated with a project
function bug_delete_all( $p_project_id ) {
$c_project_id = db_prepare_int( $p_project_id );
$t_bug_table = config_get( 'mantis_bug_table' );
$query = "SELECT id
FROM $t_bug_table
WHERE project_id='$c_project_id'";
$result = db_query( $query );
$bug_count = db_num_rows( $result );
for ( $i=0 ; $i < $bug_count ; $i++ ) {
$row = db_fetch_array( $result );
bug_delete( $row['id'] );
}
# @@@ should we check the return value of each bug_delete() and
# return false if any of them return false? Presumable bug_delete()
# will eventually trigger an error on failure so it won't matter...
return true;
}
# --------------------
function bug_update() {
}
#===================================
# Data Access
#===================================
# Returns the extended record of the specified bug, this includes
# the bug text fields
# @@@ include reporter name and handler name, the problem is that
# handler can be 0, in this case no corresponding name will be
# found. Use equivalent of (+) in Oracle.
function bug_get_extended_row( $p_bug_id ) {
$t_base = bug_cache_row( $p_user_id );
$t_text = bug_text_cache_row( $p_user_id );
# merge $t_text first so that the 'id' key has the bug id not the bug text id
return array_merge( $t_text, $t_base );
}
# --------------------
# Returns the record of the specified bug
function bug_get_row( $p_bug_id ) {
return bug_cache_row( $p_bug_id );
}
# --------------------
# return the specified field of the given bug
# if the field does not exist, display a warning and return ''
function bug_get_field( $p_bug_id, $p_field_name ) {
$row = bug_get_row( $p_bug_id );
if ( isset( $row[$p_field_name] ) ) {
return $row[$p_field_name];
} else {
trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
return '';
}
}
# --------------------
# return the specified text field of the given bug
# if the field does not exist, display a warning and return ''
function bug_get_text_field( $p_bug_id, $p_field_name ) {
$row = bug_text_cache_row( $p_bug_id );
if ( isset( $row[$p_field_name] ) ) {
return $row[$p_field_name];
} else {
trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
return '';
}
}
# --------------------
# Returns the number of bugnotes for the given bug_id
function bug_get_bugnote_count( $p_bug_id ) {
$c_bug_id = db_prepare_int( $p_bug_id );
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
if ( !access_level_check_greater_or_equal( config_get( 'private_bugnote_threshold' ), $t_project_id ) ) {
$t_restriction = 'AND view_state=' . PUBLIC;
} else {
$t_restriction = '';
}
$t_bugnote_table = config_get( 'mantis_bugnote_table' );
$query = "SELECT COUNT(*)
FROM $t_bugnote_table
WHERE bug_id ='$c_bug_id' $t_restriction";
$result = db_query( $query );
return db_result( $result );
}
# --------------------
# return the timestamp for the most recent time at which a bugnote
# associated wiht the bug was modified
function bug_get_newest_bugnote_timestamp( $p_bug_id ) {
$c_bug_id = db_prepare_int( $p_bug_id );
$t_bugnote_table = config_get( 'mantis_bugnote_table' );
$query = "SELECT UNIX_TIMESTAMP(last_modified) as last_modified
FROM $t_bugnote_table
WHERE bug_id='$c_bug_id'
ORDER BY last_modified DESC
LIMIT 1";
$result = db_query( $query );
return db_result( $result );
}
#===================================
# Data Modification
#===================================
# --------------------
# assign the bug to the given user
function bug_assign( $p_bug_id, $p_user_id ) {
$c_bug_id = db_prepare_int( $p_bug_id );
$c_user_id = db_prepare_int( $p_user_id );
# extract current information into history variables
$h_status = bug_get_field ( $p_bug_id, 'status' );
$h_handler_id = bug_get_field ( $p_bug_id, 'handler_id' );
if ( ON == config_get( 'auto_set_status_to_assigned' ) ) {
$t_ass_val = ASSIGNED;
} else {
$t_ass_val = $h_status;
}
$t_bug_table = config_get( 'mantis_bug_table' );
if ( ( $t_ass_val != $h_status ) || ( $t_handler_id != $h_handler_id ) ) {
# get user id
$query = "UPDATE $t_bug_table
SET handler_id='$c_user_id', status='$t_ass_val'
WHERE id='$c_bug_id'";
db_query( $query );
# updated the last_updated date
bug_update_date( $p_bug_id );
# log changes
history_log_event_direct( $c_bug_id, 'status', $h_status, $t_ass_val );
history_log_event_direct( $c_bug_id, 'handler_id', $h_handler_id, $t_handler_id );
# send assigned to email
email_assign( $p_bug_id );
}
return true;
}
# --------------------
# close the given bug
function bug_close( $p_bug_id, $p_bugnote_text ) {
$p_bugnote_text = trim( $p_bugnote_text );
$c_bug_id = db_prepare_int( $p_bug_id );
$c_bugnote_text = db_prepare_string( $p_bugnote_text );
$h_status = bug_get_field( $p_bug_id, 'status' );
# bug is already closed, return error
if ( CLOSED == $h_status ) {
return false;
}
# Add bugnote if supplied
if ( $p_bugnote_text != '' ) {
bugnote_add( $p_bug_id, $p_bugnote_text );
}
# Clean variables
$t_bug_table = config_get ( 'mantis_bug_table' );
$t_status_val = CLOSED;
# Update fields
$query = "UPDATE $t_bug_table
SET status='$t_status_val'
WHERE id='$c_bug_id'";
db_query( $query );
# updated the last_updated date
bug_update_date( $p_bug_id );
# log changes
history_log_event_direct( $p_bug_id, 'status', $h_status, CLOSED );
email_close( $p_bug_id );
return true;
}
# --------------------
# updates the last_updated field
function bug_update_date( $p_bug_id ) {
$c_bug_id = db_prepare_int( $p_bug_id );
$t_bug_table = config_get( 'mantis_bug_table' );
$query = "UPDATE $t_bug_table
SET last_updated=NOW()
WHERE id='$c_bug_id'";
db_query( $query );
return true;
}
?>