/
mc_api.php
1043 lines (904 loc) · 30.8 KB
/
mc_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
*/
# set up error_handler() as the new default error handling function
set_error_handler( 'mc_error_handler' );
/**
* Webservice APIs
*
* @uses api_token_api.php
*/
require_api( 'api_token_api.php' );
/**
* A class to capture a RestFault
*/
class RestFault {
/**
* @var integer The http status code
*/
public $status_code;
/**
* @var string The http status string
*/
public $fault_string;
/**
* RestFault constructor.
*
* @param integer $p_status_code The http status code
* @param string $p_fault_string The error description
*/
function __construct( $p_status_code, $p_fault_string = '' ) {
$this->status_code = $p_status_code;
$this->fault_string = $p_fault_string === null ? '' : $p_fault_string;
}
}
/**
* A factory class that can abstract away operations that can behave differently based
* on the API being accessed (SOAP vs. REST).
*/
class ApiObjectFactory {
/**
* @var bool true: SOAP API, false: REST API
*/
static public $soap = true;
/**
* Generate a new fault - this method should only be called from within this factory class. Use methods for
* specific error cases.
*
* @param string $p_fault_code SOAP fault code (Server or Client).
* @param string $p_fault_string Fault description.
* @param integer $p_status_code The http status code.
* @return RestFault|SoapFault The fault object.
* @access private
*/
static function fault( $p_fault_code, $p_fault_string, $p_status_code = null ) {
# Default status code based on fault code, if not specified.
if( $p_status_code === null ) {
$p_status_code = ( $p_fault_code == 'Server' ) ? 500 : 400;
}
if( ApiObjectFactory::$soap ) {
return new SoapFault( $p_fault_code, $p_fault_string );
}
return new RestFault( $p_status_code, $p_fault_string );
}
/**
* Fault generated when a resource doesn't exist.
*
* @param string $p_fault_string The fault details.
* @return RestFault|SoapFault The fault object.
*/
static function faultNotFound( $p_fault_string ) {
return ApiObjectFactory::fault( 'Client', $p_fault_string, HTTP_STATUS_NOT_FOUND );
}
/**
* Fault generated when an operation is not allowed.
*
* @param string $p_fault_string The fault details.
* @return RestFault|SoapFault The fault object.
*/
static function faultForbidden( $p_fault_string ) {
return ApiObjectFactory::fault( 'Client', $p_fault_string, HTTP_STATUS_FORBIDDEN );
}
/**
* Fault generated when a request is invalid.
*
* @param string $p_fault_string The fault details.
* @return RestFault|SoapFault The fault object.
*/
static function faultBadRequest( $p_fault_string ) {
return ApiObjectFactory::fault( 'Client', $p_fault_string, HTTP_STATUS_BAD_REQUEST );
}
/**
* Fault generated when the request is failed due to conflict with current state of the data.
* This can happen either due to a race condition or lack of checking on client side before
* issuing the request.
*
* @param string $p_fault_string The fault details.
* @return RestFault|SoapFault The fault object.
*/
static function faultConflict( $p_fault_string ) {
return ApiObjectFactory::fault( 'Client', $p_fault_string, HTTP_STATUS_CONFLICT );
}
/**
* Fault generated when a request fails due to server error.
*
* @param string $p_fault_string The fault details.
* @return RestFault|SoapFault The fault object.
*/
static function faultServerError( $p_fault_string ) {
return ApiObjectFactory::fault( 'Server', $p_fault_string, HTTP_STATUS_INTERNAL_SERVER_ERROR );
}
/**
* Convert a soap object to an array
* @param stdClass|array $p_object Object.
* @return array
*/
static function objectToArray($p_object ) {
if( is_object( $p_object ) ) {
return get_object_vars( $p_object );
}
return $p_object;
}
/**
* Convert a timestamp to a soap DateTime variable
* @param integer $p_value Integer value to return as date time string.
* @return datetime in expected API format.
*/
static function datetime($p_value ) {
$t_string_value = self::datetimeString( $p_value );
if( ApiObjectFactory::$soap ) {
return new SoapVar($t_string_value, XSD_DATETIME, 'xsd:dateTime');
}
return $t_string_value;
}
/**
* Convert a timestamp to a DateTime string
* @param integer $p_timestamp Integer value to format as date time string.
* @return string for provided timestamp
*/
static function datetimeString($p_timestamp ) {
if( $p_timestamp == null || date_is_null( $p_timestamp ) ) {
return null;
}
return date( 'c', (int)$p_timestamp );
}
/**
* Checks if an object is a SoapFault
* @param mixed $p_maybe_fault Object to check whether a SOAP fault.
* @return boolean
*/
static function isFault( $p_maybe_fault ) {
if( !is_object( $p_maybe_fault ) ) {
return false;
}
if( ApiObjectFactory::$soap && get_class( $p_maybe_fault ) == 'SoapFault') {
return true;
}
if( !ApiObjectFactory::$soap && get_class( $p_maybe_fault ) == 'RestFault') {
return true;
}
return false;
}
}
/**
* Get the MantisConnect webservice version.
* @return string
*/
function mc_version() {
return MANTIS_VERSION;
}
/**
* Attempts to login the user.
* If logged in successfully, return user information.
* If failed to login in, then throw a fault.
* @param string $p_username Login username.
* @param string $p_password Login password.
* @return array Array of user data for the current API user
*/
function mc_login( $p_username, $p_password ) {
$t_user_id = mci_check_login( $p_username, $p_password );
if( $t_user_id === false ) {
return mci_fault_login_failed();
}
return mci_user_get( $p_username, $p_password, $t_user_id );
}
/**
* Given an id, this method returns the user.
* When calling this method make sure that the caller has the right to retrieve
* information about the target user.
* @param string $p_username Login username.
* @param string $p_password Login password.
* @param integer $p_user_id A valid user identifier.
* @return array array of user data for the supplied user id
*/
function mci_user_get( $p_username, $p_password, $p_user_id ) {
$t_user_data = array();
# if user doesn't exist, then mci_account_get_array_by_id() will throw.
if( ApiObjectFactory::$soap ) {
$t_user_data['account_data'] = mci_account_get_array_by_id( $p_user_id );
$t_user_data['access_level'] = access_get_global_level( $p_user_id );
} else {
$t_account_data = mci_account_get_array_by_id( $p_user_id );
foreach( $t_account_data as $t_key => $t_value ) {
$t_user_data[$t_key] = $t_value;
}
$t_user_data['language'] = mci_get_user_lang( $p_user_id );
$t_access_level = access_get_global_level( $p_user_id );
$t_user_data['access_level'] = array(
'id' => $t_access_level,
'name' => MantisEnum::getLabel( config_get( 'access_levels_enum_string' ), $t_access_level ),
);
$t_project_ids = user_get_accessible_projects( $p_user_id, /* disabled */ true );
$t_projects = array();
foreach( $t_project_ids as $t_project_id ) {
$t_projects[] = mci_project_get( $t_project_id );
}
$t_user_data['projects'] = $t_projects;
}
$t_user_data['timezone'] = user_pref_get_pref( $p_user_id, 'timezone' );
return $t_user_data;
}
/**
* Get project info for the specified id.
*
* @param int $p_project_id The project id to get info for.
* @return array project info.
*/
function mci_project_get( $p_project_id ) {
$t_row = project_get_row( $p_project_id );
$t_user_id = auth_get_current_user_id();
$t_user_access_level = access_get_project_level( $p_project_id, $t_user_id );
$t_access_levels = config_get( 'access_levels_enum_string', /* default */ null, $t_user_id, $p_project_id );
# Get project info that makes sense to publish via API. For example, skip file_path.
$t_project = array(
'id' => $p_project_id,
'name' => $t_row['name'],
'description' => $t_row['description'],
'enabled' => (int)$t_row['enabled'] != 0,
'status' => array(
'id' => (int)$t_row['status'],
'name' => MantisEnum::getLabel( config_get( 'project_status_enum_string' ), (int)$t_row['status'] ) ),
'view_state' => array(
'id' => (int)$t_row['view_state'],
'name' => MantisEnum::getLabel( config_get( 'project_view_state_enum_string' ), (int)$t_row['view_state'] ) ),
'access_min' => array(
'id' => (int)$t_row['access_min'],
'name' => MantisEnum::getLabel( $t_access_levels, (int)$t_row['access_min'] ) ),
'access_level' => array(
'id' => $t_user_access_level,
'name' => MantisEnum::getLabel( $t_access_levels, $t_user_access_level ) ),
);
return $t_project;
}
/**
* access_ if MantisBT installation is marked as offline by the administrator.
* @return true: offline, false: online
*/
function mci_is_mantis_offline() {
$t_offline_file = dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'mantis_offline.php';
return file_exists( $t_offline_file );
}
/**
* handle a soap API login
* @param string $p_username Login username.
* @param string $p_password Login password.
* @return integer|false return user_id if successful, otherwise false.
*/
function mci_check_login( $p_username, $p_password ) {
static $s_already_called = false;
if( $s_already_called === true ) {
return auth_get_current_user_id();
}
$s_already_called = true;
if( mci_is_mantis_offline() ) {
return false;
}
# Must not pass in null password, otherwise, authentication will be by-passed
# by auth_attempt_script_login().
$t_password = ( $p_password === null ) ? '' : $p_password;
if( api_token_validate( $p_username, $t_password ) ) {
# Token is valid, then login the user without worrying about a password.
if( auth_attempt_script_login( $p_username, null ) === false ) {
return false;
}
} else {
# User cookie
$t_user_id = auth_user_id_from_cookie( $p_password );
if( $t_user_id !== false ) {
# Cookie is valid
if( auth_attempt_script_login( $p_username, null ) === false ) {
return false;
}
} else {
# Use regular passwords
if( auth_attempt_script_login( $p_username, $t_password ) === false ) {
return false;
}
}
}
# Set language to user's language
lang_push( lang_get_default() );
return auth_get_current_user_id();
}
/**
* Check with a user has readonly access to the webservice for a given project
* @param integer|null $p_user_id A user id or null for logged in user.
* @param integer $p_project_id A project identifier ( Default All Projects ).
* @return boolean indicating whether user has readonly access
*/
function mci_has_readonly_access( $p_user_id = null, $p_project_id = ALL_PROJECTS ) {
$t_user_id = is_null( $p_user_id ) ? auth_get_current_user_id() : $p_user_id;
$t_access_level = user_get_access_level( $t_user_id, $p_project_id );
return( $t_access_level >= config_get( 'webservice_readonly_access_level_threshold' ) );
}
/**
* Check with a user has readwrite access to the webservice for a given project
* @param integer|null $p_user_id User id or null for logged in user.
* @param integer $p_project_id Project Id ( Default All Projects ).
* @return boolean indicating whether user has readwrite access
*/
function mci_has_readwrite_access( $p_user_id = null, $p_project_id = ALL_PROJECTS ) {
$t_user_id = is_null( $p_user_id ) ? auth_get_current_user_id() : $p_user_id;
$t_access_level = user_get_access_level( $t_user_id, $p_project_id );
return( $t_access_level >= config_get( 'webservice_readwrite_access_level_threshold' ) );
}
/**
* Check with a user has the required access level for a given project
* @param integer $p_access_level Access level.
* @param integer $p_user_id User id.
* @param integer $p_project_id Project Id ( Default All Projects ).
* @return boolean indicating whether user has the required access
*/
function mci_has_access( $p_access_level, $p_user_id, $p_project_id = ALL_PROJECTS ) {
$t_access_level = user_get_access_level( $p_user_id, $p_project_id );
return( $t_access_level >= (int)$p_access_level );
}
/**
* Check with a user has administrative access to the webservice
* @param integer $p_user_id User id.
* @param integer $p_project_id Project Id ( Default All Projects ).
* @return boolean indicating whether user has the required access
*/
function mci_has_administrator_access( $p_user_id, $p_project_id = ALL_PROJECTS ) {
$t_access_level = user_get_access_level( $p_user_id, $p_project_id );
return( $t_access_level >= config_get( 'webservice_admin_access_level_threshold' ) );
}
/**
* Given an object, return the project id
* @param object $p_project Project Object.
* @return integer project id
*/
function mci_get_project_id( $p_project ) {
if( is_object( $p_project ) ) {
$p_project = get_object_vars( $p_project );
}
if( isset( $p_project['id'] ) && (int)$p_project['id'] != 0 ) {
$t_project_id = (int)$p_project['id'];
} else if( isset( $p_project['name'] ) && !is_blank( $p_project['name'] ) ) {
$t_project_id = project_get_id_by_name( $p_project['name'] );
} else {
$t_project_id = ALL_PROJECTS;
}
return $t_project_id;
}
/**
* Return project Status
* @param object $p_status Status.
* @return integer Status
*/
function mci_get_project_status_id( $p_status ) {
return mci_get_enum_id_from_objectref( 'project_status', $p_status );
}
/**
* Return project view state
* @param object $p_view_state View state.
* @return integer View state
*/
function mci_get_project_view_state_id( $p_view_state ) {
return mci_get_enum_id_from_objectref( 'project_view_state', $p_view_state );
}
/**
* Return user id
* @param stdClass|array $p_user User.
* @return integer user id
*/
function mci_get_user_id( $p_user ) {
if( is_object( $p_user ) ) {
$p_user = ApiObjectFactory::objectToArray( $p_user );
}
$t_user_id = 0;
if( isset( $p_user['id'] ) && (int)$p_user['id'] != 0 ) {
$t_user_id = (int)$p_user['id'];
} elseif( isset( $p_user['name'] ) ) {
$t_user_id = user_get_id_by_name( $p_user['name'] );
} elseif( isset( $p_user['email'] ) ) {
$t_user_id = user_get_id_by_email( $p_user['email'] );
}
return $t_user_id;
}
/**
* Given a profile id, return its information as an array or null
* if profile id is 0 or not found.
*
* @param integer $p_profile_id The profile id, can be 0.
* @return array|null The profile or null if not found.
*/
function mci_profile_as_array_by_id( $p_profile_id ) {
$t_profile_id = (int)$p_profile_id;
if( $t_profile_id == 0 ) {
return null;
}
$t_profile = profile_get_row_direct( $t_profile_id );
if( $t_profile === false ) {
return null;
}
return array(
'id' => $t_profile_id,
'user' => mci_account_get_array_by_id( $t_profile['user_id'] ),
'platform' => $t_profile['platform'],
'os' => $t_profile['os'],
'os_build' => $t_profile['os_build'],
'description' => $t_profile['description']
);
}
/**
* Get basic issue info for related issues.
*
* @param integer $p_issue_id The issue id.
* @return array|null The issue id or null if not found.
*/
function mci_related_issue_as_array_by_id( $p_issue_id ) {
$t_issue_id = (int)$p_issue_id;
if( !bug_exists( $t_issue_id ) ) {
return null;
}
$t_user_id = auth_get_current_user_id();
$t_lang = mci_get_user_lang( $t_user_id );
$t_bug = bug_get( $t_issue_id );
$t_related_issue = array(
'id' => $t_bug->id,
'status' => mci_enum_get_array_by_id( $t_bug->status, 'status', $t_lang ),
'resolution' => mci_enum_get_array_by_id( $t_bug->resolution, 'resolution', $t_lang ),
'summary' => $t_bug->summary
);
if( !empty( $t_bug->handler_id ) ) {
if( access_has_bug_level(
config_get( 'view_handler_threshold', null, null, $t_bug->project_id ),
$t_issue_id, $t_user_id ) ) {
$t_related_issue['handler'] = mci_account_get_array_by_id( $t_bug->handler_id );
}
}
return $t_related_issue;
}
/**
* Return user's default language given a user id
* @param integer $p_user_id User id.
* @return string language string
*/
function mci_get_user_lang( $p_user_id ) {
$t_lang = user_pref_get_pref( $p_user_id, 'language' );
if( $t_lang == 'auto' ) {
$t_lang = config_get( 'fallback_language' );
}
return $t_lang;
}
/**
* Return Status
* @param object $p_status Status.
* @return integer status id
*/
function mci_get_status_id( $p_status ) {
return mci_get_enum_id_from_objectref( 'status', $p_status );
}
/**
* Return Severity
* @param object $p_severity Severity.
* @return integer severity id
*/
function mci_get_severity_id( $p_severity ) {
return mci_get_enum_id_from_objectref( 'severity', $p_severity );
}
/**
* Return Priority
* @param object $p_priority Priority.
* @return integer priority id
*/
function mci_get_priority_id( $p_priority ) {
return mci_get_enum_id_from_objectref( 'priority', $p_priority );
}
/**
* Return Reproducibility
* @param object $p_reproducibility Reproducibility.
* @return integer reproducibility id
*/
function mci_get_reproducibility_id( $p_reproducibility ) {
return mci_get_enum_id_from_objectref( 'reproducibility', $p_reproducibility );
}
/**
* Return Resolution
* @param object $p_resolution Resolution object.
* @return integer Resolution id
*/
function mci_get_resolution_id( $p_resolution ) {
return mci_get_enum_id_from_objectref( 'resolution', $p_resolution );
}
/**
* Return projection
* @param object $p_projection Projection object.
* @return integer projection id
*/
function mci_get_projection_id( $p_projection ) {
return mci_get_enum_id_from_objectref( 'projection', $p_projection );
}
/**
* Return ETA id
* @param object $p_eta ETA object.
* @return integer eta id
*/
function mci_get_eta_id( $p_eta ) {
return mci_get_enum_id_from_objectref( 'eta', $p_eta );
}
/**
* Return view state id
* @param object $p_view_state View state object.
* @return integer view state
*/
function mci_get_view_state_id( $p_view_state ) {
return mci_get_enum_id_from_objectref( 'view_state', $p_view_state );
}
/**
* Get null on empty value.
*
* @param string $p_value The value.
* @return string|null The value if not empty; null otherwise.
*/
function mci_null_if_empty( $p_value ) {
if( !is_blank( $p_value ) ) {
return $p_value;
}
return null;
}
/**
* Removes any invalid character from the string per XML 1.0 specification
*
* @param string $p_input XML string.
* @return string the sanitized XML
*/
function mci_sanitize_xml_string ( $p_input ) {
if( ApiObjectFactory::$soap ) {
return preg_replace( '/[^\x9\xA\xD\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/u', '', $p_input );
}
return $p_input;
}
/**
* Gets the url for MantisBT.
*
* @return string MantisBT URL terminated by a /.
*/
function mci_get_mantis_path() {
return config_get( 'path' );
}
/**
* Gets the sub-projects that are accessible to the specified user / project.
* @param integer $p_user_id User id.
* @param integer $p_parent_project_id Parent Project id.
* @param string $p_lang Language string.
* @return array
*/
function mci_user_get_accessible_subprojects( $p_user_id, $p_parent_project_id, $p_lang = null ) {
if( $p_lang === null ) {
$t_lang = mci_get_user_lang( $p_user_id );
} else {
$t_lang = $p_lang;
}
$t_result = array();
foreach( user_get_accessible_subprojects( $p_user_id, $p_parent_project_id ) as $t_subproject_id ) {
$t_subproject_row = project_cache_row( $t_subproject_id );
$t_subproject = array();
$t_subproject['id'] = $t_subproject_id;
$t_subproject['name'] = $t_subproject_row['name'];
$t_subproject['status'] = mci_enum_get_array_by_id( $t_subproject_row['status'], 'project_status', $t_lang );
$t_subproject['enabled'] = $t_subproject_row['enabled'];
$t_subproject['view_state'] = mci_enum_get_array_by_id( $t_subproject_row['view_state'], 'project_view_state', $t_lang );
$t_subproject['access_min'] = mci_enum_get_array_by_id( $t_subproject_row['access_min'], 'access_levels', $t_lang );
$t_subproject['file_path'] = array_key_exists( 'file_path', $t_subproject_row ) ? $t_subproject_row['file_path'] : '';
$t_subproject['description'] = array_key_exists( 'description', $t_subproject_row ) ? $t_subproject_row['description'] : '';
$t_subproject['subprojects'] = mci_user_get_accessible_subprojects( $p_user_id, $t_subproject_id, $t_lang );
$t_result[] = $t_subproject;
}
return $t_result;
}
/**
* Convert a category name to a category id for a given project
* @param string|array $p_category Category name or array with id and/or name.
* @param integer $p_project_id Project id.
* @return integer category id or 0 if not found
*/
function mci_get_category_id( $p_category, $p_project_id ) {
if( !isset( $p_category ) ) {
return 0;
}
if( is_array( $p_category ) ) {
if( isset( $p_category['id'] ) ) {
if( category_exists( $p_category['id'] ) ) {
return $p_category['id'];
}
} else if( isset( $p_category['name'] ) ) {
$t_category_name = $p_category['name'];
} else {
return 0;
}
} else {
$t_category_name = $p_category;
}
$t_cat_array = category_get_all_rows( $p_project_id );
foreach( $t_cat_array as $t_category_row ) {
if( $t_category_row['name'] == $t_category_name ) {
return $t_category_row['id'];
}
}
return 0;
}
/**
* Basically this is a copy of core/filter_api.php#filter_db_get_available_queries().
* The only difference is that the result of this function is not an array of filter
* names but an array of filter structures.
* @param integer $p_project_id Project id.
* @param integer $p_user_id User id.
* @return array
*/
function mci_filter_db_get_available_queries( $p_project_id = null, $p_user_id = null ) {
$t_overall_query_arr = array();
if( null === $p_project_id ) {
$t_project_id = helper_get_current_project();
} else {
$t_project_id = (int)$p_project_id;
}
if( null === $p_user_id ) {
$t_user_id = auth_get_current_user_id();
} else {
$t_user_id = (int)$p_user_id;
}
# If the user doesn't have access rights to stored queries, just return
if( !access_has_project_level( config_get( 'stored_query_use_threshold' ) ) ) {
return $t_overall_query_arr;
}
# Get the list of available queries. By sorting such that public queries are
# first, we can override any query that has the same name as a private query
# with that private one
$t_query = 'SELECT * FROM {filters}
WHERE (project_id=' . db_param() . '
OR project_id=0)
AND name!=\'\'
AND (is_public = ' . db_param() . '
OR user_id = ' . db_param() . ')
ORDER BY is_public DESC, name ASC';
$t_result = db_query( $t_query, array( $t_project_id, true, $t_user_id ) );
$t_query_count = db_num_rows( $t_result );
for( $i = 0;$i < $t_query_count;$i++ ) {
$t_row = db_fetch_array( $t_result );
$t_filter_detail = explode( '#', $t_row['filter_string'], 2 );
if( !isset($t_filter_detail[1]) ) {
continue;
}
$t_filter = json_decode( $t_filter_detail[1], true );
$t_filter = filter_ensure_valid_filter( $t_filter );
$t_row['url'] = filter_get_url( $t_filter );
$t_overall_query_arr[$t_row['name']] = $t_row;
}
return array_values( $t_overall_query_arr );
}
/**
* Get a category definition.
*
* @param integer $p_category_id The id of the category to retrieve.
* @return array an array containing the id and the name of the category.
*/
function mci_category_as_array_by_id( $p_category_id ) {
$t_result = array();
$t_result['id'] = $p_category_id;
$t_result['name'] = category_get_name( $p_category_id );
return $t_result;
}
/**
* Transforms a version array into an array suitable for marshalling into ProjectVersionData
*
* @param array $p_version Version array.
* @return array
*/
function mci_project_version_as_array( array $p_version ) {
return array(
'id' => $p_version['id'],
'name' => $p_version['version'],
'project_id' => $p_version['project_id'],
'date_order' => ApiObjectFactory::datetime( $p_version['date_order'] ),
'description' => mci_null_if_empty( $p_version['description'] ),
'released' => $p_version['released'],
'obsolete' => $p_version['obsolete']
);
}
/**
* Returns time tracking information from a bug note.
*
* @param integer $p_issue_id The id of the issue.
* @param array $p_note A note as passed to the soap api methods.
*
* @return String the string time entry to be added to the bugnote, in 'HH:mm' format
*/
function mci_get_time_tracking_from_note( $p_issue_id, array $p_note ) {
if( !access_has_bug_level( config_get( 'time_tracking_view_threshold' ), $p_issue_id ) ) {
return '00:00';
}
if( !isset( $p_note['time_tracking'] ) ) {
return '00:00';
}
return db_minutes_to_hhmm( $p_note['time_tracking'] );
}
/**
* Default error handler
*
* This handler will not receive E_ERROR, E_PARSE, E_CORE_*, or E_COMPILE_* errors.
*
* E_USER_* are triggered by us and will contain an error constant in $p_error
* The others, being system errors, will come with a string in $p_error
* @param integer $p_type Contains the level of the error raised, as an integer.
* @param string $p_error Contains the error message, as a string.
* @param string $p_file Contains the filename that the error was raised in, as a string.
* @param integer $p_line Contains the line number the error was raised at, as an integer.
* @param array $p_context To the active symbol table at the point the error occurred (optional).
* @return void
*/
function mc_error_handler( $p_type, $p_error, $p_file, $p_line, array $p_context ) {
# check if errors were disabled with @ somewhere in this call chain
# also suppress php 5 strict warnings
if( 0 == error_reporting() || 2048 == $p_type ) {
return;
}
# flush any language overrides to return to user's natural default
if( function_exists( 'db_is_connected' ) ) {
if( db_is_connected() ) {
lang_push( lang_get_default() );
}
}
# build an appropriate error string
switch( $p_type ) {
case E_WARNING:
$t_error_type = 'SYSTEM WARNING';
$t_error_description = $p_error;
break;
case E_NOTICE:
$t_error_type = 'SYSTEM NOTICE';
$t_error_description = $p_error;
break;
case E_USER_ERROR:
$t_error_type = 'APPLICATION ERROR #' . $p_error;
$t_error_description = error_string( $p_error );
break;
case E_USER_WARNING:
$t_error_type = 'APPLICATION WARNING #' . $p_error;
$t_error_description = error_string( $p_error );
break;
case E_USER_NOTICE:
# used for debugging
$t_error_type = 'DEBUG';
$t_error_description = $p_error;
break;
default:
#shouldn't happen, just display the error just in case
$t_error_type = '';
$t_error_description = $p_error;
}
$t_error_stack = error_get_stack_trace();
error_log( '[mantisconnect.php] Error Type: ' . $t_error_type . ',' . "\n" . 'Error Description: ' . $t_error_description . "\n" . 'Stack Trace:' . "\n" . $t_error_stack );
throw new SoapFault( 'Server', 'Error Type: ' . $t_error_type . ',' . "\n" . 'Error Description: ' . $t_error_description );
}
/**
* Get a stack trace from either PHP or xdebug if present
* @return string
*/
function error_get_stack_trace() {
$t_trace = '';
if( extension_loaded( 'xdebug' ) ) {
#check for xdebug presence
$t_stack = xdebug_get_function_stack();
# reverse the array in a separate line of code so the
# array_reverse() call doesn't appear in the stack
$t_stack = array_reverse( $t_stack );
array_shift( $t_stack );
#remove the call to this function from the stack trace
foreach( $t_stack as $t_frame ) {
$t_trace .= ( isset( $t_frame['file'] ) ? basename( $t_frame['file'] ) : 'UnknownFile' ) . ' L' . ( isset( $t_frame['line'] ) ? $t_frame['line'] : '?' ) . ' ' . ( isset( $t_frame['function'] ) ? $t_frame['function'] : 'UnknownFunction' );
$t_args = array();
if( isset( $t_frame['params'] ) && ( count( $t_frame['params'] ) > 0 ) ) {
$t_trace .= ' Params: ';
foreach( $t_frame['params'] as $t_value ) {
$t_args[] = error_build_parameter_string( $t_value );
}
$t_trace .= '(' . implode( $t_args, ', ' ) . ')';
} else {
$t_trace .= '()';
}
$t_trace .= "\n";
}
} else {
$t_stack = debug_backtrace();
array_shift( $t_stack ); #remove the call to this function from the stack trace
array_shift( $t_stack ); #remove the call to the error handler from the stack trace
foreach( $t_stack as $t_frame ) {
$t_trace .= ( isset( $t_frame['file'] ) ? basename( $t_frame['file'] ) : 'UnknownFile' ) . ' L' . ( isset( $t_frame['line'] ) ? $t_frame['line'] : '?' ) . ' ' . ( isset( $t_frame['function'] ) ? $t_frame['function'] : 'UnknownFunction' );
$t_args = array();
if( isset( $t_frame['args'] ) ) {
foreach( $t_frame['args'] as $t_value ) {
$t_args[] = error_build_parameter_string( $t_value );
}
$t_trace .= '(' . implode( $t_args, ', ' ) . ')';
} else {
$t_trace .= '()';
}
$t_trace .= "\n";
}
}
return $t_trace;
}
/**
* Returns a fault signalling corresponding to a failed login
* situation
*
* @return RestFault|SoapFault
*/
function mci_fault_login_failed() {
return ApiObjectFactory::faultForbidden( 'Access denied' );
}
/**
* Returns a soap_fault signalling that the user does not have
* access rights for the specific action.
*
* @param integer $p_user_id A user id, optional.
* @param string $p_detail The optional details to append to the error message.
* @return RestFault|SoapFault
*/
function mci_fault_access_denied($p_user_id = 0, $p_detail = '' ) {
if( $p_user_id ) {
$t_user_name = user_get_name( $p_user_id );
$t_reason = 'Access denied for user '. $t_user_name . '.';
} else {
$t_reason = 'Access denied';
}
if( !is_blank( $p_detail ) ) {
$t_reason .= ' Reason: ' . $p_detail . '.';
}