/
project_api.php
445 lines (359 loc) · 13.1 KB
/
project_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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?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: project_api.php,v 1.19 2002-11-27 03:07:25 jfitzell Exp $
# --------------------------------------------------------
###########################################################################
# Project API
###########################################################################
#===================================
# Caching
#===================================
#########################################
# SECURITY NOTE: cache globals are initialized here to prevent them
# being spoofed if register_globals is turned on
#
$g_cache_project = array();
# --------------------
# Cache a project row if necessary and return the cached copy
# If the second parameter is true (default), trigger an error
# if the project can't be found. If the second parameter is
# false, return false if the project can't be found.
function project_cache_row( $p_project_id, $p_trigger_errors=true) {
global $g_cache_project;
$c_project_id = db_prepare_int( $p_project_id );
if ( isset ( $g_cache_project[$c_project_id] ) ) {
return $g_cache_project[$c_project_id];
}
$t_project_table = config_get( 'mantis_project_table' );
$query = "SELECT *
FROM $t_project_table
WHERE id='$c_project_id'";
$result = db_query( $query );
if ( 0 == db_num_rows( $result ) ) {
if ( $p_trigger_errors ) {
trigger_error( ERROR_PROJECT_NOT_FOUND, ERROR );
} else {
return false;
}
}
$row = db_fetch_array( $result );
$g_cache_project[$c_project_id] = $row;
return $row;
}
# --------------------
# Clear the project cache (or just the given id if specified)
function project_clear_cache( $p_project_id = null ) {
global $g_cache_project;
if ( null === $p_project_id ) {
$g_cache_project = array();
} else {
$c_project_id = db_prepare_int( $p_project_id );
unset( $g_cache_project[$c_project_id] );
}
return true;
}
#===================================
# Boolean queries and ensures
#===================================
# --------------------
# check to see if project exists by id
# return true if it does, false otherwise
function project_exists( $p_project_id ) {
# we're making use of the caching function here. If we
# succeed in caching the project then it exists and is
# now cached for use by later function calls. If we can't
# cache it we return false.
if ( false == project_cache_row( $p_project_id, false ) ) {
return false;
} else {
return true;
}
}
# --------------------
# check to see if project exists by id
# if it doesn't exist then error
# otherwise let execution continue undisturbed
function project_ensure_exists( $p_project_id ) {
if ( ! project_exists( $p_project_id ) ) {
trigger_error( ERROR_PROJECT_NOT_FOUND, ERROR );
}
}
# --------------------
# check to see if project exists by name
function project_is_name_unique( $p_name ) {
$c_name = db_prepare_string( $p_name );
$t_project_table = config_get( 'mantis_project_table' );
$query ="SELECT COUNT(*)
FROM $t_project_table
WHERE name='$c_name'";
$result = db_query( $query );
if ( 0 == db_result( $result ) ) {
return true;
} else {
return false;
}
}
# --------------------
# check to see if project exists by id
# if it doesn't exist then error
# otherwise let execution continue undisturbed
function project_ensure_name_unique( $p_name ) {
if ( ! project_is_name_unique( $p_name ) ) {
trigger_error( ERROR_PROJECT_NAME_NOT_UNIQUE, ERROR );
}
}
# --------------------
# check to see if the user/project combo already exists
# returns true is duplicate is found, otherwise false
function project_includes_user( $p_project_id, $p_user_id ) {
$t_project_user_list_table = config_get( 'mantis_project_user_list_table' );
$c_project_id = db_prepare_int( $p_project_id );
$c_user_id = db_prepare_int( $p_user_id );
$query = "SELECT COUNT(*)
FROM $t_project_user_list_table
WHERE project_id='$c_project_id' AND
user_id='$c_user_id'";
$result = db_query( $query );
if ( 0 == db_result( $result ) ) {
return false;
} else {
return true;
}
}
#===================================
# Creation / Deletion / Updating
#===================================
# --------------------
# Create a new project
function project_create( $p_name, $p_description, $p_status, $p_view_state=PUBLIC, $p_file_path='', $p_enabled=true ) {
# Make sure file path has trailing slash
$p_file_path = terminate_directory_path( $p_file_path );
$c_name = db_prepare_string( $p_name );
$c_description = db_prepare_string( $p_description );
$c_status = db_prepare_int( $p_status );
$c_view_state = db_prepare_int( $p_view_state );
$c_file_path = db_prepare_string( $p_file_path );
$c_enabled = db_prepare_bool( $p_enabled );
if ( is_blank( $p_name ) ) {
trigger_error( ERROR_PROJECT_NAME_INVALID, ERROR );
}
project_ensure_name_unique( $p_name );
$t_project_table = config_get( 'mantis_project_table' );
$query = "INSERT
INTO $t_project_table
( id, name, status, enabled, view_state, file_path, description )
VALUES
( null, '$c_name', '$c_status', '$c_enabled', '$c_view_state', '$c_file_path', '$c_description' )";
db_query( $query );
# return the id of the new project
return db_insert_id();
}
# --------------------
# Delete a project
function project_delete( $p_project_id ) {
$c_project_id = db_prepare_int( $p_project_id );
$t_project_table = config_get( 'mantis_project_table' );
# Delete the bugs
bug_delete_all( $p_project_id );
# Delete the project categories
category_delete_all( $p_project_id );
# Delete the project versions
version_delete_all( $p_project_id );
# Delete the project custom attributes
attribute_delete_all( $p_project_id );
# Delete the project files
project_delete_all_files( $p_project_id );
# Delete the records assigning users to this project
project_remove_all_users( $p_project_id );
# Delete the project entry
$query = "DELETE
FROM $t_project_table
WHERE id='$c_project_id'";
db_query( $query );
project_clear_cache( $p_project_id );
# db_query() errors on failure so:
return true;
}
# --------------------
# Update a project
function project_update( $p_project_id, $p_name, $p_description, $p_status, $p_view_state, $p_file_path, $p_enabled ) {
# Make sure file path has trailing slash
$p_file_path = terminate_directory_path( $p_file_path );
$c_project_id = db_prepare_int( $p_project_id );
$c_name = db_prepare_string( $p_name );
$c_description = db_prepare_string( $p_description );
$c_status = db_prepare_int( $p_status );
$c_view_state = db_prepare_int( $p_view_state );
$c_file_path = db_prepare_string( $p_file_path );
$c_enabled = db_prepare_bool( $p_enabled );
if ( is_blank( $p_name ) ) {
trigger_error( ERROR_PROJECT_NAME_INVALID, ERROR );
}
$t_old_name = project_get_field( $p_project_id, 'name' );
if ( $p_name != $t_old_name ) {
project_ensure_name_unique( $p_name );
}
$t_project_table = config_get( 'mantis_project_table' );
$query = "UPDATE $t_project_table
SET name='$c_name',
status='$c_status',
enabled='$c_enabled',
view_state='$c_view_state',
file_path='$c_file_path',
description='$c_description'
WHERE id='$c_project_id'";
db_query( $query );
project_clear_cache( $p_project_id );
# db_query errors on failure so:
return true;
}
#===================================
# Data Access
#===================================
# --------------------
# Return the specified field of the specified project
function project_get_field( $p_project_id, $p_field_name ) {
$row = project_cache_row( $p_project_id );
if ( isset( $row[$p_field_name] ) ) {
return $row[$p_field_name];
} else {
trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
return '';
}
}
# --------------------
# Return the user's access level on the project or false
# if the user is not listed on the project
function project_get_user_access_level( $p_project_id, $p_user_id ) {
$c_project_id = db_prepare_int( $p_project_id );
$c_user_id = db_prepare_int( $p_user_id );
if ( 0 == $c_project_id ) {
return false;
}
$t_project_user_list_table = config_get( 'mantis_project_user_list_table' );
$query = "SELECT access_level
FROM $t_project_user_list_table
WHERE user_id='$c_user_id' AND project_id='$c_project_id'";
$result = db_query( $query );
if ( db_num_rows( $result ) > 0 ) {
return db_result( $result );
} else {
return false;
}
}
# --------------------
# return the descriptor holding all the info from the project user list
# for the specified project
function project_get_all_user_rows( $p_project_id ) {
$t_project_user_list_table = config_get( 'mantis_project_user_list_table' );
$c_project_id = db_prepare_int( $p_project_id );
$query = "SELECT *
FROM $t_project_user_list_table
WHERE project_id='$c_project_id'";
$result = db_query( $query );
$t_user_rows = array();
$t_row_count = db_num_rows( $result );
for ( $i=0 ; $i < $t_row_count ; $i++ ) {
array_push( $t_user_rows, db_fetch_array( $result ) );
}
return $t_user_rows;
}
#===================================
# Data Modification
#===================================
# --------------------
# add user with the specified access level to a project
function project_add_user( $p_project_id, $p_user_id, $p_access_level ) {
$t_project_user_list_table = config_get( 'mantis_project_user_list_table' );
$c_project_id = db_prepare_int( $p_project_id );
$c_user_id = db_prepare_int( $p_user_id );
$c_access_level = db_prepare_int( $p_access_level );
$query = "INSERT
INTO $t_project_user_list_table
( project_id, user_id, access_level )
VALUES
( '$c_project_id', '$c_user_id', '$c_access_level')";
db_query( $query );
# db_query errors on failure so:
return true;
}
# --------------------
# update entry
# must make sure entry exists beforehand
function project_update_user_access( $p_project_id, $p_user_id, $p_access_level ) {
$t_project_user_list_table = config_get( 'mantis_project_user_list_table' );
$c_project_id = db_prepare_int( $p_project_id );
$c_user_id = db_prepare_int( $p_user_id );
$c_access_level = db_prepare_int( $p_access_level );
$query = "UPDATE $t_project_user_list_table
SET access_level='$c_access_level'
WHERE project_id='$c_project_id' AND
user_id='$c_user_id'";
db_query( $query );
# db_query errors on failure so:
return true;
}
# --------------------
# update or add the entry as appropriate
# This function involves one more db query than project_update_user_acces()
# or project_add_user()
function project_set_user_access( $p_project_id, $p_user_id, $p_access_level ) {
if ( project_includes_user( $p_project_id, $p_user_id ) ) {
return project_update_user_access( $p_project_id, $p_user_id, $p_access_level );
} else {
return project_add_user( $p_project_id, $p_user_id, $p_access_level );
}
}
# --------------------
# remove user from project
function project_remove_user( $p_project_id, $p_user_id ) {
$t_project_user_list_table = config_get( 'mantis_project_user_list_table' );
$c_project_id = db_prepare_int( $p_project_id );
$c_user_id = db_prepare_int( $p_user_id );
$query = "DELETE FROM $t_project_user_list_table
WHERE project_id='$c_project_id' AND
user_id='$c_user_id'";
db_query( $query );
# db_query errors on failure so:
return true;
}
# --------------------
# delete all users from the project user list for a given project
# this is useful when deleting or closing a project
function project_remove_all_users( $p_project_id ) {
$t_project_user_list_table = config_get( 'mantis_project_user_list_table' );
$c_project_id = db_prepare_int( $p_project_id );
$query = "DELETE FROM $t_project_user_list_table
WHERE project_id='$c_project_id'";
db_query( $query );
# db_query errors on failure so:
return true;
}
# --------------------
# Copy all users and their permissions from the source project to the
# destination project
function project_copy_users( $p_destination_id, $p_source_id ) {
# Copy all users from current project over to another project
$rows = project_get_all_user_rows( $p_source_id );
for ( $i = 0 ; $i < sizeof( $rows ) ; $i++ ) {
extract( $rows[$i], EXTR_PREFIX_ALL, 'v' );
# if there is no duplicate then add a new entry
# otherwise just update the access level for the existing entry
if ( project_includes_user( $p_destination_id, $v_user_id ) ) {
project_update_user_access( $p_destination_id, $v_user_id, $v_access_level );
} else {
project_add_user( $p_destination_id, $v_user_id, $v_access_level );
}
}
}
# --------------------
# Delete all files associated with a project
function project_delete_all_files( $p_project_id ) {
# @@@ to be written
}
?>