/
mc_issue_api.php
1749 lines (1439 loc) · 67.1 KB
/
mc_issue_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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* A webservice interface to Mantis Bug Tracker
*
* @package MantisBT
* @copyright Copyright 2004 Victor Boctor - vboctor@users.sourceforge.net
* @copyright Copyright 2005 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/
require_once( dirname( __FILE__ ) . '/mc_core.php' );
/**
* Check if an issue with the given id exists.
*
* @param string $p_username The name of the user trying to access the issue.
* @param string $p_password The password of the user.
* @param integer $p_issue_id The id of the issue to check.
* @return boolean true if there is an issue with the given id, false otherwise.
*/
function mc_issue_exists( $p_username, $p_password, $p_issue_id ) {
$t_user_id = mci_check_login( $p_username, $p_password );
if( $t_user_id === false ) {
return mci_soap_fault_login_failed();
}
if( !bug_exists( $p_issue_id ) ) {
return false;
}
$t_project_id = bug_get_field( $p_issue_id, 'project_id' );
if( !mci_has_readonly_access( $t_user_id, $t_project_id ) ) {
# if we return an error here, then we answered the question!
return false;
}
return true;
}
/**
* Get all details about an issue.
*
* @param string $p_username The name of the user trying to access the issue.
* @param string $p_password The password of the user.
* @param integer $p_issue_id The id of the issue to retrieve.
* @return array that represents an IssueData structure
*/
function mc_issue_get( $p_username, $p_password, $p_issue_id ) {
global $g_project_override;
$t_user_id = mci_check_login( $p_username, $p_password );
if( $t_user_id === false ) {
return mci_soap_fault_login_failed();
}
$t_lang = mci_get_user_lang( $t_user_id );
if( !bug_exists( $p_issue_id ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Issue does not exist' );
}
$t_project_id = bug_get_field( $p_issue_id, 'project_id' );
$g_project_override = $t_project_id;
if( !mci_has_readonly_access( $t_user_id, $t_project_id ) ) {
return mci_soap_fault_access_denied( $t_user_id );
}
if( !access_has_bug_level( config_get( 'view_bug_threshold', null, null, $t_project_id ), $p_issue_id, $t_user_id ) ) {
return mci_soap_fault_access_denied( $t_user_id );
}
log_event( LOG_WEBSERVICE, 'getting details for issue \'' . $p_issue_id . '\'' );
$t_bug = bug_get( $p_issue_id, true );
$t_issue_data = array();
$t_issue_data['id'] = $p_issue_id;
$t_issue_data['view_state'] = mci_enum_get_array_by_id( $t_bug->view_state, 'view_state', $t_lang );
$t_issue_data['last_updated'] = SoapObjectsFactory::newDateTimeVar( $t_bug->last_updated );
$t_issue_data['project'] = mci_project_as_array_by_id( $t_bug->project_id );
$t_issue_data['category'] = mci_get_category( $t_bug->category_id );
$t_issue_data['priority'] = mci_enum_get_array_by_id( $t_bug->priority, 'priority', $t_lang );
$t_issue_data['severity'] = mci_enum_get_array_by_id( $t_bug->severity, 'severity', $t_lang );
$t_issue_data['status'] = mci_enum_get_array_by_id( $t_bug->status, 'status', $t_lang );
$t_issue_data['reporter'] = mci_account_get_array_by_id( $t_bug->reporter_id );
$t_issue_data['summary'] = mci_sanitize_xml_string( $t_bug->summary );
$t_issue_data['version'] = mci_null_if_empty( $t_bug->version );
$t_issue_data['build'] = mci_null_if_empty( $t_bug->build );
$t_issue_data['profile_id'] = mci_null_if_empty( $t_bug->profile_id );
$t_issue_data['platform'] = mci_null_if_empty( $t_bug->platform );
$t_issue_data['os'] = mci_null_if_empty( $t_bug->os );
$t_issue_data['os_build'] = mci_null_if_empty( $t_bug->os_build );
$t_issue_data['reproducibility'] = mci_enum_get_array_by_id( $t_bug->reproducibility, 'reproducibility', $t_lang );
$t_issue_data['date_submitted'] = SoapObjectsFactory::newDateTimeVar( $t_bug->date_submitted );
$t_issue_data['sticky'] = $t_bug->sticky;
$t_issue_data['sponsorship_total'] = $t_bug->sponsorship_total;
if( !empty( $t_bug->handler_id ) ) {
if( access_has_bug_level( config_get( 'view_handler_threshold', null, null, $t_project_id ), $p_issue_id, $t_user_id ) ) {
$t_issue_data['handler'] = mci_account_get_array_by_id( $t_bug->handler_id );
}
}
$t_issue_data['projection'] = mci_enum_get_array_by_id( $t_bug->projection, 'projection', $t_lang );
$t_issue_data['eta'] = mci_enum_get_array_by_id( $t_bug->eta, 'eta', $t_lang );
$t_issue_data['resolution'] = mci_enum_get_array_by_id( $t_bug->resolution, 'resolution', $t_lang );
$t_issue_data['fixed_in_version'] = mci_null_if_empty( $t_bug->fixed_in_version );
$t_issue_data['target_version'] = mci_null_if_empty( $t_bug->target_version );
$t_issue_data['due_date'] = mci_issue_get_due_date( $t_bug );
$t_issue_data['description'] = mci_sanitize_xml_string( $t_bug->description );
$t_issue_data['steps_to_reproduce'] = mci_null_if_empty( mci_sanitize_xml_string( $t_bug->steps_to_reproduce ) );
$t_issue_data['additional_information'] = mci_null_if_empty( mci_sanitize_xml_string( $t_bug->additional_information ) );
$t_issue_data['attachments'] = mci_issue_get_attachments( $p_issue_id );
$t_issue_data['relationships'] = mci_issue_get_relationships( $p_issue_id, $t_user_id );
$t_issue_data['notes'] = mci_issue_get_notes( $p_issue_id );
$t_issue_data['custom_fields'] = mci_issue_get_custom_fields( $p_issue_id );
$t_issue_data['monitors'] = mci_account_get_array_by_ids( bug_get_monitors( $p_issue_id ) );
$t_issue_data['tags'] = mci_issue_get_tags_for_bug_id( $p_issue_id, $t_user_id );
return $t_issue_data;
}
/**
* Get history details about an issue.
*
* @param string $p_username The name of the user trying to access the issue.
* @param string $p_password The password of the user.
* @param integer $p_issue_id The id of the issue to retrieve.
* @return array that represents a HistoryDataArray structure
*/
function mc_issue_get_history( $p_username, $p_password, $p_issue_id ) {
global $g_project_override;
$t_user_id = mci_check_login( $p_username, $p_password );
if( $t_user_id === false ) {
return mci_soap_fault_login_failed();
}
if( !bug_exists( $p_issue_id ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Issue does not exist' );
}
$t_project_id = bug_get_field( $p_issue_id, 'project_id' );
if( !mci_has_readonly_access( $t_user_id, $t_project_id ) ) {
return mci_soap_fault_access_denied( $t_user_id );
}
$g_project_override = $t_project_id;
if( !access_has_bug_level( config_get( 'view_bug_threshold', null, null, $t_project_id ), $p_issue_id, $t_user_id ) ) {
return mci_soap_fault_access_denied( $t_user_id );
}
$t_user_access_level = user_get_access_level( $t_user_id, $t_project_id );
if( !access_compare_level( $t_user_access_level, config_get( 'view_history_threshold' ) ) ) {
return mci_soap_fault_access_denied( $t_user_id );
}
log_event( LOG_WEBSERVICE, 'retrieving history for issue \'' . $p_issue_id . '\'' );
$t_bug_history = history_get_raw_events_array( $p_issue_id, $t_user_id );
return $t_bug_history;
}
/**
* Returns the category name, possibly null if no category is assigned
*
* @param integer $p_category_id A category identifier.
* @return string
*/
function mci_get_category( $p_category_id ) {
if( $p_category_id == 0 ) {
return '';
}
return mci_null_if_empty( category_get_name( $p_category_id ) );
}
/**
* Get due date for a given bug
* @param BugData $p_bug A BugData object.
* @return soapval the value to be encoded as the due date
*/
function mci_issue_get_due_date( BugData $p_bug ) {
$t_value = null;
if( access_has_bug_level( config_get( 'due_date_view_threshold' ), $p_bug->id ) && !date_is_null( $p_bug->due_date ) ) {
$t_value = $p_bug->due_date;
}
return SoapObjectsFactory::newDateTimeVar( $t_value );
}
/**
* Sets the supplied array of custom field values to the specified issue id.
*
* @param integer $p_issue_id Issue id to apply custom field values to.
* @param array &$p_custom_fields The array of custom field values as described in the webservice complex types.
* @param boolean $p_log_insert Create history logs for new values.
* @return mixed
*/
function mci_issue_set_custom_fields( $p_issue_id, array &$p_custom_fields = null, $p_log_insert ) {
# set custom field values on the submitted issue
if( isset( $p_custom_fields ) && is_array( $p_custom_fields ) ) {
foreach( $p_custom_fields as $t_custom_field ) {
$t_custom_field = SoapObjectsFactory::unwrapObject( $t_custom_field );
# Verify validity of custom field specification
$t_msg = 'Invalid Custom field specification';
$t_valid_cf = isset( $t_custom_field['field'] ) && isset( $t_custom_field['value'] );
if( $t_valid_cf ) {
$t_field = get_object_vars( $t_custom_field['field'] );
if( ( !isset( $t_field['id'] ) || $t_field['id'] == 0 ) && !isset( $t_field['name'] ) ) {
$t_valid_cf = false;
$t_msg .= ", either 'name' or 'id' != 0 or must be given.";
}
}
if( !$t_valid_cf ) {
return SoapObjectsFactory::newSoapFault( 'Client', $t_msg );
}
# get custom field id from object ref
$t_custom_field_id = mci_get_custom_field_id_from_objectref( $t_custom_field['field'] );
if( $t_custom_field_id == 0 ) {
return SoapObjectsFactory::newSoapFault( 'Client', "Custom field '" . $t_field['name'] . "' not found." );
}
# skip if current user doesn't have login access.
if( !custom_field_has_write_access( $t_custom_field_id, $p_issue_id ) ) {
continue;
}
$t_value = $t_custom_field['value'];
if( !custom_field_validate( $t_custom_field_id, $t_value ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Invalid custom field value for field id ' . $t_custom_field_id . ' .' );
}
if( !custom_field_set_value( $t_custom_field_id, $p_issue_id, $t_value, $p_log_insert ) ) {
return SoapObjectsFactory::newSoapFault( 'Server', 'Unable to set custom field value for field id ' . $t_custom_field_id . ' to issue ' . $p_issue_id. ' .' );
}
}
}
}
/**
* Get the custom field values associated with the specified issue id.
*
* @param integer $p_issue_id Issue id to get the custom field values for.
*
* @return null if no custom field defined for the project that contains the issue, or if no custom
* fields are accessible to the current user.
*/
function mci_issue_get_custom_fields( $p_issue_id ) {
$t_project_id = bug_get_field( $p_issue_id, 'project_id' );
$t_custom_fields = array();
$t_related_custom_field_ids = custom_field_get_linked_ids( $t_project_id );
foreach( $t_related_custom_field_ids as $t_id ) {
$t_def = custom_field_get_definition( $t_id );
if( custom_field_has_read_access( $t_id, $p_issue_id ) ) {
# user has not access to read this custom field.
$t_value = custom_field_get_value( $t_id, $p_issue_id );
if( $t_value === false ) {
continue;
}
# return a blank string if the custom field value is undefined
if( $t_value === null ) {
$t_value = '';
}
$t_custom_field_value = array();
$t_custom_field_value['field'] = array();
$t_custom_field_value['field']['id'] = $t_id;
$t_custom_field_value['field']['name'] = $t_def['name'];
$t_custom_field_value['value'] = $t_value;
$t_custom_fields[] = $t_custom_field_value;
}
}
return( count( $t_custom_fields ) == 0 ? null : $t_custom_fields );
}
/**
* Get the attachments of an issue.
*
* @param integer $p_issue_id The id of the issue to retrieve the attachments for.
* @return array that represents an AttachmentData structure
*/
function mci_issue_get_attachments( $p_issue_id ) {
$t_attachment_rows = bug_get_attachments( $p_issue_id );
if( $t_attachment_rows == null ) {
return array();
}
$t_result = array();
foreach( $t_attachment_rows as $t_attachment_row ) {
if( !file_can_view_bug_attachments( $p_issue_id, (int)$t_attachment_row['user_id'] ) ) {
continue;
}
$t_attachment = array();
$t_attachment['id'] = $t_attachment_row['id'];
$t_attachment['filename'] = $t_attachment_row['filename'];
$t_attachment['size'] = $t_attachment_row['filesize'];
$t_attachment['content_type'] = $t_attachment_row['file_type'];
$t_attachment['date_submitted'] = SoapObjectsFactory::newDateTimeVar( $t_attachment_row['date_added'] );
$t_attachment['download_url'] = mci_get_mantis_path() . 'file_download.php?file_id=' . $t_attachment_row['id'] . '&type=bug';
$t_attachment['user_id'] = $t_attachment_row['user_id'];
$t_result[] = $t_attachment;
}
return $t_result;
}
/**
* Get the relationships of an issue.
*
* @param integer $p_issue_id The id of the issue to retrieve the relationships for.
* @param integer $p_user_id The user id of the user trying to access the information.
* @return array that represents an RelationShipData structure
*/
function mci_issue_get_relationships( $p_issue_id, $p_user_id ) {
$t_relationships = array();
$t_src_relationships = relationship_get_all_src( $p_issue_id );
foreach( $t_src_relationships as $t_relship_row ) {
if( access_has_bug_level( config_get( 'webservice_readonly_access_level_threshold' ), $t_relship_row->dest_bug_id, $p_user_id ) ) {
$t_relationship = array();
$t_reltype = array();
$t_relationship['id'] = $t_relship_row->id;
$t_reltype['id'] = $t_relship_row->type;
$t_reltype['name'] = relationship_get_description_src_side( $t_relship_row->type );
$t_relationship['type'] = $t_reltype;
$t_relationship['target_id'] = $t_relship_row->dest_bug_id;
$t_relationships[] = $t_relationship;
}
}
$t_dest_relationships = relationship_get_all_dest( $p_issue_id );
foreach( $t_dest_relationships as $t_relship_row ) {
if( access_has_bug_level( config_get( 'webservice_readonly_access_level_threshold' ), $t_relship_row->src_bug_id, $p_user_id ) ) {
$t_relationship = array();
$t_relationship['id'] = $t_relship_row->id;
$t_reltype = array();
$t_reltype['id'] = relationship_get_complementary_type( $t_relship_row->type );
$t_reltype['name'] = relationship_get_description_dest_side( $t_relship_row->type );
$t_relationship['type'] = $t_reltype;
$t_relationship['target_id'] = $t_relship_row->src_bug_id;
$t_relationships[] = $t_relationship;
}
}
return (count( $t_relationships ) == 0 ? null : $t_relationships );
}
/**
* Get all visible notes for a specific issue
*
* @param integer $p_issue_id The id of the issue to retrieve the notes for.
* @return array that represents an SOAP IssueNoteData structure
*/
function mci_issue_get_notes( $p_issue_id ) {
$t_user_id = auth_get_current_user_id();
$t_lang = mci_get_user_lang( $t_user_id );
$t_project_id = bug_get_field( $p_issue_id, 'project_id' );
$t_user_bugnote_order = 'ASC'; # always get the notes in ascending order for consistency to the calling application.
$t_has_time_tracking_access = access_has_bug_level( config_get( 'time_tracking_view_threshold' ), $p_issue_id );
$t_result = array();
foreach( bugnote_get_all_visible_bugnotes( $p_issue_id, $t_user_bugnote_order, 0 ) as $t_value ) {
$t_bugnote = array();
$t_bugnote['id'] = $t_value->id;
$t_bugnote['reporter'] = mci_account_get_array_by_id( $t_value->reporter_id );
$t_bugnote['date_submitted'] = SoapObjectsFactory::newDateTimeString( $t_value->date_submitted );
$t_bugnote['last_modified'] = SoapObjectsFactory::newDateTimeString( $t_value->last_modified );
$t_bugnote['text'] = mci_sanitize_xml_string( $t_value->note );
$t_bugnote['view_state'] = mci_enum_get_array_by_id( $t_value->view_state, 'view_state', $t_lang );
$t_bugnote['time_tracking'] = $t_has_time_tracking_access ? $t_value->time_tracking : 0;
$t_bugnote['note_type'] = $t_value->note_type;
$t_bugnote['note_attr'] = $t_value->note_attr;
$t_result[] = $t_bugnote;
}
return (count( $t_result ) == 0 ? null : $t_result );
}
/**
* Sets the monitors of the specified issue
*
* <p>This functions performs access level checks and only performs operations which would
* modify the existing monitors list.</p>
*
* @param integer $p_issue_id The issue id to set the monitors for.
* @param integer $p_requesting_user_id The user which requests the monitor change.
* @param array $p_monitors An array of arrays with the <em>id</em> field set to the id
* of the users which should monitor this issue.
* @return mixed
*/
function mci_issue_set_monitors( $p_issue_id, $p_requesting_user_id, array $p_monitors ) {
if( bug_is_readonly( $p_issue_id ) ) {
return mci_soap_fault_access_denied( $p_requesting_user_id, 'Issue \'' . $p_issue_id . '\' is readonly' );
}
# 1. get existing monitor ids
$t_existing_monitor_ids = bug_get_monitors( $p_issue_id );
# 2. build new monitors ids
$t_new_monitor_ids = array();
foreach ( $p_monitors as $t_monitor ) {
$t_monitor = SoapObjectsFactory::unwrapObject( $t_monitor );
$t_new_monitor_ids[] = $t_monitor['id'];
}
# 3. for each of the new monitor ids, add it if it does not already exist
foreach( $t_new_monitor_ids as $t_user_id ) {
if( $p_requesting_user_id == $t_user_id ) {
if( ! access_has_bug_level( config_get( 'monitor_bug_threshold' ), $p_issue_id ) ) {
continue;
}
} else {
if( !access_has_bug_level( config_get( 'monitor_add_others_bug_threshold' ), $p_issue_id ) ) {
continue;
}
}
if( in_array( $t_user_id, $t_existing_monitor_ids ) ) {
continue;
}
bug_monitor( $p_issue_id, $t_user_id );
}
# 4. for each of the existing monitor ids, remove it if it is not found in the new monitor ids
foreach ( $t_existing_monitor_ids as $t_user_id ) {
if( $p_requesting_user_id == $t_user_id ) {
if( ! access_has_bug_level( config_get( 'monitor_bug_threshold' ), $p_issue_id ) ) {
continue;
}
} else {
if( !access_has_bug_level( config_get( 'monitor_delete_others_bug_threshold' ), $p_issue_id ) ) {
continue;
}
}
if( in_array( $t_user_id, $t_new_monitor_ids ) ) {
continue;
}
bug_unmonitor( $p_issue_id, $t_user_id );
}
}
/**
* Get the biggest issue id currently used.
*
* @param string $p_username The name of the user trying to retrieve the information.
* @param string $p_password The password of the user.
* @param integer $p_project_id One of -1 default project, 0 for all projects, otherwise project id.
* @return integer The biggest used issue id.
*/
function mc_issue_get_biggest_id( $p_username, $p_password, $p_project_id ) {
global $g_project_override;
$t_user_id = mci_check_login( $p_username, $p_password );
if( $t_user_id === false ) {
return mci_soap_fault_login_failed();
}
$t_any = defined( 'META_FILTER_ANY' ) ? META_FILTER_ANY : 'any';
$t_none = defined( 'META_FILTER_NONE' ) ? META_FILTER_NONE : 'none';
$t_filter = array(
'category_id' => array(
'0' => $t_any,
),
'severity' => array(
'0' => $t_any,
),
'status' => array(
'0' => $t_any,
),
'highlight_changed' => 0,
'reporter_id' => array(
'0' => $t_any,
),
'handler_id' => array(
'0' => $t_any,
),
'resolution' => array(
'0' => $t_any,
),
'build' => array(
'0' => $t_any,
),
'version' => array(
'0' => $t_any,
),
'hide_status' => array(
'0' => $t_none,
),
'monitor_user_id' => array(
'0' => $t_any,
),
'dir' => 'DESC',
'sort' => 'id',
);
$t_page_number = 1;
$t_per_page = 1;
$t_bug_count = 0;
$t_page_count = 0;
# Get project id, if -1, then retrieve the current which will be the default since there is no cookie.
$t_project_id = $p_project_id;
if( $t_project_id == -1 ) {
$t_project_id = helper_get_current_project();
}
$g_project_override = $t_project_id;
if( ( $t_project_id > 0 ) && !project_exists( $t_project_id ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Project \'' . $t_project_id . '\' does not exist.' );
}
if( !mci_has_readonly_access( $t_user_id, $t_project_id ) ) {
return mci_soap_fault_access_denied( $t_user_id );
}
$t_rows = filter_get_bug_rows( $t_page_number, $t_per_page, $t_page_count, $t_bug_count, $t_filter, $t_project_id, $t_user_id );
if( count( $t_rows ) == 0 ) {
return 0;
} else {
return $t_rows[0]->id;
}
}
/**
* Get the id of an issue via the issue's summary.
*
* @param string $p_username The name of the user trying to delete the issue.
* @param string $p_password The password of the user.
* @param string $p_summary The summary of the issue to retrieve.
* @return integer The id of the issue with the given summary, 0 if there is no such issue.
*/
function mc_issue_get_id_from_summary( $p_username, $p_password, $p_summary ) {
global $g_project_override;
$t_user_id = mci_check_login( $p_username, $p_password );
if( $t_user_id === false ) {
return mci_soap_fault_login_failed();
}
$t_query = 'SELECT id FROM {bug} WHERE summary = ' . db_param();
$t_result = db_query( $t_query, array( $p_summary ), 1 );
if( db_num_rows( $t_result ) == 0 ) {
return 0;
} else {
while( ( $t_row = db_fetch_array( $t_result ) ) !== false ) {
$t_issue_id = (int)$t_row['id'];
$t_project_id = bug_get_field( $t_issue_id, 'project_id' );
$g_project_override = $t_project_id;
if( mci_has_readonly_access( $t_user_id, $t_project_id ) &&
access_has_bug_level( config_get( 'view_bug_threshold', null, null, $t_project_id ), $t_issue_id, $t_user_id ) ) {
return $t_issue_id;
}
}
# no issue found that belongs to a project that the user has read access to.
return 0;
}
}
/**
* Does the actual checks when setting the issue handler.
* The user existence check is always done even if handler doesn't change.
* The handler's access level check is done even if handler doesn't change.
* The current user ability to assign issue access check is only done on change.
* This behavior would be consistent with the web UI.
*
* @param integer $p_user_id The id of the logged in user.
* @param integer $p_project_id The id of the project the issue is associated with.
* @param integer $p_old_handler_id The old handler id.
* @param integer $p_new_handler_id The new handler id. 0 for not assigned.
* @return true: access ok, otherwise: soap fault.
*/
function mci_issue_handler_access_check( $p_user_id, $p_project_id, $p_old_handler_id, $p_new_handler_id ) {
if( $p_new_handler_id != 0 ) {
if ( !user_exists( $p_new_handler_id ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'User \'' . $p_new_handler_id . '\' does not exist.' );
}
if( !access_has_project_level( config_get( 'handle_bug_threshold' ), $p_project_id, $p_new_handler_id ) ) {
return mci_soap_fault_access_denied( 'User \'' . $p_new_handler_id . '\' does not have access right to handle issues' );
}
}
if( $p_old_handler_id != $p_new_handler_id ) {
if( !access_has_project_level( config_get( 'update_bug_assign_threshold' ), $p_project_id, $p_user_id ) ) {
return mci_soap_fault_access_denied( 'User \'' . $p_user_id . '\' does not have access right to assign issues' );
}
}
return true;
}
/**
* Add an issue to the database.
*
* @param string $p_username The name of the user trying to add the issue.
* @param string $p_password The password of the user.
* @param stdClass $p_issue A IssueData structure containing information about the new issue.
* @return integer The id of the created issue.
*/
function mc_issue_add( $p_username, $p_password, stdClass $p_issue ) {
global $g_project_override;
$t_user_id = mci_check_login( $p_username, $p_password );
if( $t_user_id === false ) {
return mci_soap_fault_login_failed();
}
$p_issue = SoapObjectsFactory::unwrapObject( $p_issue );
$t_project = $p_issue['project'];
$t_project_id = mci_get_project_id( $t_project );
$g_project_override = $t_project_id; # ensure that helper_get_current_project() calls resolve to this project id
if( !mci_has_readwrite_access( $t_user_id, $t_project_id ) ) {
return mci_soap_fault_access_denied( $t_user_id );
}
$t_handler_id = isset( $p_issue['handler'] ) ? mci_get_user_id( $p_issue['handler'] ) : 0;
$t_priority_id = isset( $p_issue['priority'] ) ? mci_get_priority_id( $p_issue['priority'] ) : config_get( 'default_bug_priority' );
$t_severity_id = isset( $p_issue['severity'] ) ? mci_get_severity_id( $p_issue['severity'] ) : config_get( 'default_bug_severity' );
$t_status_id = isset( $p_issue['status'] ) ? mci_get_status_id( $p_issue['status'] ) : config_get( 'bug_submit_status' );
$t_reproducibility_id = isset( $p_issue['reproducibility'] ) ? mci_get_reproducibility_id( $p_issue['reproducibility'] ) : config_get( 'default_bug_reproducibility' );
$t_resolution_id = isset( $p_issue['resolution'] ) ? mci_get_resolution_id( $p_issue['resolution'] ) : config_get( 'default_bug_resolution' );
$t_projection_id = isset( $p_issue['projection'] ) ? mci_get_projection_id( $p_issue['projection'] ) : config_get( 'default_bug_resolution' );
$t_eta_id = isset( $p_issue['eta'] ) ? mci_get_eta_id( $p_issue['eta'] ) : config_get( 'default_bug_eta' );
$t_view_state_id = isset( $p_issue['view_state'] ) ? mci_get_view_state_id( $p_issue['view_state'] ) : config_get( 'default_bug_view_status' );
$t_summary = $p_issue['summary'];
$t_description = $p_issue['description'];
$t_notes = isset( $p_issue['notes'] ) ? $p_issue['notes'] : array();
# TODO: #17777: Add test case for mc_issue_add() and mc_issue_note_add() reporter override
if( isset( $p_issue['reporter'] ) ) {
$t_reporter_id = mci_get_user_id( $p_issue['reporter'] );
if( $t_reporter_id != $t_user_id ) {
# Make sure that active user has access level required to specify a different reporter.
$t_specify_reporter_access_level = config_get( 'webservice_specify_reporter_on_add_access_level_threshold' );
if( !access_has_project_level( $t_specify_reporter_access_level, $t_project_id, $t_user_id ) ) {
return mci_soap_fault_access_denied( $t_user_id, 'Active user does not have access level required to specify a different issue reporter' );
}
}
} else {
$t_reporter_id = $t_user_id;
}
if( ( $t_project_id == 0 ) || !project_exists( $t_project_id ) ) {
if( $t_project_id == 0 ) {
return SoapObjectsFactory::newSoapFault( 'Client', "Project '" . $t_project->name . "' does not exist." );
} else {
return SoapObjectsFactory::newSoapFault( 'Client', "Project with id '" . $t_project_id . "' does not exist." );
}
}
if( !access_has_project_level( config_get( 'report_bug_threshold' ), $t_project_id, $t_user_id ) ) {
return mci_soap_fault_access_denied( 'User \'' . $t_user_id . '\' does not have access right to report issues' );
}
$t_access_check_result = mci_issue_handler_access_check( $t_user_id, $t_project_id, /* old */ 0, /* new */ $t_handler_id );
if( $t_access_check_result !== true ) {
return $t_access_check_result;
}
$t_category = isset( $p_issue['category'] ) ? $p_issue['category'] : null;
$t_category_id = translate_category_name_to_id( $t_category, $t_project_id );
if( $t_category_id == 0 && !config_get( 'allow_no_category' ) ) {
if( !isset( $p_issue['category'] ) || is_blank( $p_issue['category'] ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Category field must be supplied.' );
} else {
return SoapObjectsFactory::newSoapFault( 'Client', 'Category \'' . $p_issue['category'] . '\' not found for project \'' . $t_project_id . '\'.' );
}
}
if( isset( $p_issue['version'] ) && !is_blank( $p_issue['version'] ) && !version_get_id( $p_issue['version'], $t_project_id ) ) {
$t_version = $p_issue['version'];
$t_error_when_version_not_found = config_get( 'webservice_error_when_version_not_found' );
if( $t_error_when_version_not_found == ON ) {
$t_project_name = project_get_name( $t_project_id );
return SoapObjectsFactory::newSoapFault( 'Client', 'Version \'' . $t_version . '\' does not exist in project \'' . $t_project_name . '\'.' );
} else {
$t_version_when_not_found = config_get( 'webservice_version_when_not_found' );
$t_version = $t_version_when_not_found;
}
}
if( is_blank( $t_summary ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Mandatory field \'summary\' is missing.' );
}
if( is_blank( $t_description ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Mandatory field \'description\' is missing.' );
}
$t_bug_data = new BugData;
$t_bug_data->profile_id = 0;
$t_bug_data->project_id = $t_project_id;
$t_bug_data->reporter_id = $t_reporter_id;
$t_bug_data->handler_id = $t_handler_id;
$t_bug_data->priority = $t_priority_id;
$t_bug_data->severity = $t_severity_id;
$t_bug_data->reproducibility = $t_reproducibility_id;
$t_bug_data->status = $t_status_id;
$t_bug_data->resolution = $t_resolution_id;
$t_bug_data->projection = $t_projection_id;
$t_bug_data->category_id = $t_category_id;
$t_bug_data->date_submitted = isset( $p_issue['date_submitted'] ) ? $p_issue['date_submitted'] : '';
$t_bug_data->last_updated = isset( $p_issue['last_updated'] ) ? $p_issue['last_updated'] : '';
$t_bug_data->eta = $t_eta_id;
$t_bug_data->profile_id = isset( $p_issue['profile_id'] ) ? $p_issue['profile_id'] : 0;
$t_bug_data->os = isset( $p_issue['os'] ) ? $p_issue['os'] : '';
$t_bug_data->os_build = isset( $p_issue['os_build'] ) ? $p_issue['os_build'] : '';
$t_bug_data->platform = isset( $p_issue['platform'] ) ? $p_issue['platform'] : '';
$t_bug_data->version = isset( $p_issue['version'] ) ? $p_issue['version'] : '';
$t_bug_data->fixed_in_version = isset( $p_issue['fixed_in_version'] ) ? $p_issue['fixed_in_version'] : '';
$t_bug_data->build = isset( $p_issue['build'] ) ? $p_issue['build'] : '';
$t_bug_data->view_state = $t_view_state_id;
$t_bug_data->summary = $t_summary;
$t_bug_data->sponsorship_total = isset( $p_issue['sponsorship_total'] ) ? $p_issue['sponsorship_total'] : 0;
if( isset( $p_issue['sticky'] ) &&
access_has_project_level( config_get( 'set_bug_sticky_threshold', null, null, $t_project_id ), $t_project_id ) ) {
$t_bug_data->sticky = $p_issue['sticky'];
}
if( isset( $p_issue['due_date'] ) && access_has_global_level( config_get( 'due_date_update_threshold' ) ) ) {
$t_bug_data->due_date = SoapObjectsFactory::parseDateTimeString( $p_issue['due_date'] );
} else {
$t_bug_data->due_date = date_get_null();
}
if( access_has_project_level( config_get( 'roadmap_update_threshold' ), $t_bug_data->project_id, $t_user_id ) ) {
$t_bug_data->target_version = isset( $p_issue['target_version'] ) ? $p_issue['target_version'] : '';
}
# omitted:
# var $bug_text_id
# $t_bug_data->profile_id;
# extended info
$t_bug_data->description = $t_description;
$t_bug_data->steps_to_reproduce = isset( $p_issue['steps_to_reproduce'] ) ? $p_issue['steps_to_reproduce'] : '';
$t_bug_data->additional_information = isset( $p_issue['additional_information'] ) ? $p_issue['additional_information'] : '';
# submit the issue
$t_issue_id = $t_bug_data->create();
log_event( LOG_WEBSERVICE, 'created new issue id \'' . $t_issue_id . '\'' );
$t_set_custom_field_error = mci_issue_set_custom_fields( $t_issue_id, $p_issue['custom_fields'], false );
if( $t_set_custom_field_error != null ) {
return $t_set_custom_field_error;
}
if( isset( $p_issue['monitors'] ) ) {
mci_issue_set_monitors( $t_issue_id, $t_user_id, $p_issue['monitors'] );
}
if( isset( $t_notes ) && is_array( $t_notes ) ) {
foreach( $t_notes as $t_note ) {
$t_note = SoapObjectsFactory::unwrapObject( $t_note );
if( isset( $t_note['view_state'] ) ) {
$t_view_state = $t_note['view_state'];
} else {
$t_view_state = config_get( 'default_bugnote_view_status' );
}
$t_note_type = isset( $t_note['note_type'] ) ? (int)$t_note['note_type'] : BUGNOTE;
$t_note_attr = isset( $t_note['note_type'] ) ? $t_note['note_attr'] : '';
$t_view_state_id = mci_get_enum_id_from_objectref( 'view_state', $t_view_state );
$t_note_id = bugnote_add(
$t_issue_id,
$t_note['text'],
mci_get_time_tracking_from_note( $t_issue_id, $t_note ),
$t_view_state_id == VS_PRIVATE,
$t_note_type,
$t_note_attr,
$t_user_id,
false ); # don't send mail
log_event( LOG_WEBSERVICE, 'bugnote id \'' . $t_note_id . '\' added to issue \'' . $t_issue_id . '\'' );
}
}
if( isset( $p_issue['tags'] ) && is_array( $p_issue['tags'] ) ) {
mci_tag_set_for_issue( $t_issue_id, $p_issue['tags'], $t_user_id );
}
email_bug_added( $t_issue_id );
if( $t_bug_data->status != config_get( 'bug_submit_status' ) ) {
history_log_event( $t_issue_id, 'status', config_get( 'bug_submit_status' ) );
}
if( $t_bug_data->resolution != config_get( 'default_bug_resolution' ) ) {
history_log_event( $t_issue_id, 'resolution', config_get( 'default_bug_resolution' ) );
}
return $t_issue_id;
}
/**
* Update Issue in database
*
* Created By KGB
* @param string $p_username The name of the user trying to add the issue.
* @param string $p_password The password of the user.
* @param integer $p_issue_id The issue id of the existing issue being updated.
* @param stdClass $p_issue A IssueData structure containing information about the new issue.
* @return integer The id of the created issue.
*/
function mc_issue_update( $p_username, $p_password, $p_issue_id, stdClass $p_issue ) {
global $g_project_override;
$t_user_id = mci_check_login( $p_username, $p_password );
if( $t_user_id === false ) {
return mci_soap_fault_login_failed();
}
if( !bug_exists( $p_issue_id ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Issue \'' . $p_issue_id . '\' does not exist.' );
}
if( bug_is_readonly( $p_issue_id ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Issue \'' . $p_issue_id . '\' is readonly' );
}
$t_project_id = bug_get_field( $p_issue_id, 'project_id' );
if( !mci_has_readwrite_access( $t_user_id, $t_project_id ) ) {
return mci_soap_fault_access_denied( $t_user_id );
}
$g_project_override = $t_project_id; # ensure that helper_get_current_project() calls resolve to this project id
$p_issue = SoapObjectsFactory::unwrapObject( $p_issue );
$t_project_id = mci_get_project_id( $p_issue['project'] );
$t_reporter_id = isset( $p_issue['reporter'] ) ? mci_get_user_id( $p_issue['reporter'] ) : $t_user_id ;
$t_handler_id = isset( $p_issue['handler'] ) ? mci_get_user_id( $p_issue['handler'] ) : 0;
$t_project = $p_issue['project'];
$t_summary = isset( $p_issue['summary'] ) ? $p_issue['summary'] : '';
$t_description = isset( $p_issue['description'] ) ? $p_issue['description'] : '';
if( ( $t_project_id == 0 ) || !project_exists( $t_project_id ) ) {
if( $t_project_id == 0 ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Project \'' . $t_project['name'] . '\' does not exist.' );
}
return SoapObjectsFactory::newSoapFault( 'Client', 'Project \'' . $t_project_id . '\' does not exist.' );
}
if( !access_has_bug_level( config_get( 'update_bug_threshold' ), $p_issue_id, $t_user_id ) ) {
return mci_soap_fault_access_denied( $t_user_id, 'Not enough rights to update issues' );
}
$t_category = isset( $p_issue['category'] ) ? $p_issue['category'] : null;
$t_category_id = translate_category_name_to_id( $t_category, $t_project_id );
if( $t_category_id == 0 && !config_get( 'allow_no_category' ) ) {
if( isset( $p_issue['category'] ) && !is_blank( $p_issue['category'] ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Category field must be supplied.' );
} else {
$t_project_name = project_get_name( $t_project_id );
return SoapObjectsFactory::newSoapFault( 'Client', 'Category \'' . $p_issue['category'] . '\' not found for project \'' . $t_project_name . '\'.' );
}
}
if( isset( $p_issue['version'] ) && !is_blank( $p_issue['version'] ) && !version_get_id( $p_issue['version'], $t_project_id ) ) {
$t_error_when_version_not_found = config_get( 'webservice_error_when_version_not_found' );
if( $t_error_when_version_not_found == ON ) {
$t_project_name = project_get_name( $t_project_id );
return SoapObjectsFactory::newSoapFault( 'Client', 'Version \'' . $p_issue['version'] . '\' does not exist in project \'' . $t_project_name . '\'.' );
} else {
$t_version_when_not_found = config_get( 'webservice_version_when_not_found' );
$p_issue['version'] = $t_version_when_not_found;
}
}
if( is_blank( $t_summary ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Mandatory field \'summary\' is missing.' );
}
if( is_blank( $t_description ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Mandatory field \'description\' is missing.' );
}
# fields which we expect to always be set
$t_bug_data = bug_get( $p_issue_id, true );
$t_bug_data->project_id = $t_project_id;
$t_bug_data->reporter_id = $t_reporter_id;
$t_access_check_result = mci_issue_handler_access_check( $t_user_id, $t_project_id, /* old */ $t_bug_data->handler_id, /* new */ $t_handler_id );
if( $t_access_check_result !== true ) {
return $t_access_check_result;
}
$t_bug_data->handler_id = $t_handler_id;
$t_bug_data->category_id = $t_category_id;
$t_bug_data->summary = $t_summary;
$t_bug_data->description = $t_description;
# fields which might not be set
if( isset( $p_issue['steps_to_reproduce'] ) ) {
$t_bug_data->steps_to_reproduce = $p_issue['steps_to_reproduce'];
}
if( isset( $p_issue['additional_information'] ) ) {
$t_bug_data->additional_information = $p_issue['additional_information'];
}
if( isset( $p_issue['priority'] ) ) {
$t_bug_data->priority = mci_get_priority_id( $p_issue['priority'] );
}
if( isset( $p_issue['severity'] ) ) {
$t_bug_data->severity = mci_get_severity_id( $p_issue['severity'] );
}
if( isset( $p_issue['status'] ) ) {
$t_bug_data->status = mci_get_status_id( $p_issue['status'] );
}
if( isset( $p_issue['reproducibility'] ) ) {
$t_bug_data->reproducibility = mci_get_reproducibility_id( $p_issue['reproducibility'] );
}
if( isset( $p_issue['resolution'] ) ) {
$t_bug_data->resolution = mci_get_resolution_id( $p_issue['resolution'] );
}
if( isset( $p_issue['projection'] ) ) {
$t_bug_data->projection = mci_get_projection_id( $p_issue['projection'] );
}
if( isset( $p_issue['eta'] ) ) {
$t_bug_data->eta = mci_get_eta_id( $p_issue['eta'] );
}
if( isset( $p_issue['view_state'] ) ) {
$t_bug_data->view_state = mci_get_view_state_id( $p_issue['view_state'] );
}
if( isset( $p_issue['date_submitted'] ) ) {
$t_bug_data->date_submitted = $p_issue['date_submitted'];
}
if( isset( $p_issue['date_updated'] ) ) {
$t_bug_data->last_updated = $p_issue['last_updated'];
}
if( isset( $p_issue['profile_id'] ) ) {
$t_bug_data->profile_id = $p_issue['profile_id'];
}
if( isset( $p_issue['os'] ) ) {
$t_bug_data->os = $p_issue['os'];
}
if( isset( $p_issue['os_build'] ) ) {
$t_bug_data->os_build = $p_issue['os_build'];
}
if( isset( $p_issue['build'] ) ) {
$t_bug_data->build = $p_issue['build'];
}
if( isset( $p_issue['platform'] ) ) {
$t_bug_data->platform = $p_issue['platform'];
}
if( isset( $p_issue['version'] ) ) {
$t_bug_data->version = $p_issue['version'];