/
mc_file_api.php
250 lines (219 loc) · 8.02 KB
/
mc_file_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
<?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
*/
/**
* Check if the current user can download attachments for the specified bug.
* @param integer $p_bug_id A bug identifier.
* @param integer $p_user_id An user identifier.
* @return boolean
*/
function mci_file_can_download_bug_attachments( $p_bug_id, $p_user_id ) {
$t_can_download = access_has_bug_level( config_get( 'download_attachments_threshold' ), $p_bug_id );
if( $t_can_download ) {
return true;
}
$t_reported_by_me = bug_is_user_reporter( $p_bug_id, $p_user_id );
return( $t_reported_by_me && config_get( 'allow_download_own_attachments' ) );
}
/**
* Read a local file and return its content.
* @param string $p_diskfile Name of file on disk.
* @return string
*/
function mci_file_read_local( $p_diskfile ) {
$t_handle = fopen( $p_diskfile, 'r' );
$t_content = fread( $t_handle, filesize( $p_diskfile ) );
fclose( $t_handle );
return $t_content;
}
/**
* Write a local file.
* @param string $p_diskfile Name of file on disk.
* @param string $p_content File content to write.
* @return void
*/
function mci_file_write_local( $p_diskfile, $p_content ) {
$t_handle = fopen( $p_diskfile, 'w' );
fwrite( $t_handle, $p_content );
fclose( $t_handle );
}
/**
* Add a file
* @param integer $p_id File id.
* @param string $p_name File name.
* @param string $p_content File content to write.
* @param string $p_file_type File type.
* @param string $p_table Database table name.
* @param string $p_title Title.
* @param string $p_desc Description.
* @param string $p_user_id User id.
* @return mixed
*/
function mci_file_add( $p_id, $p_name, $p_content, $p_file_type, $p_table, $p_title = '', $p_desc = '', $p_user_id = null ) {
if( !file_type_check( $p_name ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'File type not allowed.' );
}
if( !file_is_name_unique( $p_name, $p_id ) ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Duplicate filename.' );
}
$t_file_size = strlen( $p_content );
$t_max_file_size = (int)min( ini_get_number( 'upload_max_filesize' ), ini_get_number( 'post_max_size' ), config_get( 'max_file_size' ) );
if( $t_file_size > $t_max_file_size ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'File is too big.' );
}
if( 'bug' == $p_table ) {
$t_project_id = bug_get_field( $p_id, 'project_id' );
$t_id = (int)$p_id;
$t_issue_id = bug_format_id( $p_id );
} else {
$t_project_id = $p_id;
$t_id = $t_project_id;
$t_issue_id = 0;
}
if( $p_user_id === null ) {
$p_user_id = auth_get_current_user_id();
}
if( $t_project_id == ALL_PROJECTS ) {
$t_file_path = config_get( 'absolute_path_default_upload_folder' );
} else {
$t_file_path = project_get_field( $t_project_id, 'file_path' );
if( is_blank( $t_file_path ) ) {
$t_file_path = config_get( 'absolute_path_default_upload_folder' );
}
}
$t_unique_name = file_generate_unique_name( $t_file_path );
$t_disk_file_name = $t_file_path . $t_unique_name;
$t_method = config_get( 'file_upload_method' );
switch( $t_method ) {
case DISK:
if( !file_exists( $t_file_path ) || !is_dir( $t_file_path ) || !is_writable( $t_file_path ) || !is_readable( $t_file_path ) ) {
return SoapObjectsFactory::newSoapFault( 'Server', 'Upload folder \'' . $t_file_path . '\' doesn\'t exist.' );
}
file_ensure_valid_upload_path( $t_file_path );
if( !file_exists( $t_disk_file_name ) ) {
mci_file_write_local( $t_disk_file_name, $p_content );
chmod( $t_disk_file_name, config_get( 'attachments_file_permissions' ) );
$c_content = "''";
}
break;
case DATABASE:
$c_content = db_prepare_binary_string( $p_content );
$t_file_path = '';
break;
}
$t_file_table = db_get_table( $p_table . '_file' );
$t_id_col = $p_table . '_id';
$t_query = 'INSERT INTO ' . $t_file_table . '
( ' . $t_id_col . ', title, description, diskfile, filename, folder, filesize, file_type, date_added, user_id )
VALUES
( ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', '
. db_param() . ', ' . db_param() . ', ' . db_param() . ', '
. db_param() . ', ' . db_param() . ', ' . db_param() . ', '
. db_param() . ' )';
db_query( $t_query, array(
$t_id, $p_title, $p_desc,
$t_unique_name, $p_name, $t_file_path,
$t_file_size, $p_file_type, db_now(),
(int)$p_user_id,
) );
# get attachment id
$t_attachment_id = db_insert_id( $t_file_table );
if( db_is_oracle() ) {
db_update_blob( $t_file_table, 'content', $c_content, "diskfile='$t_unique_name'" );
} else {
$t_query = "UPDATE $t_file_table SET content=" . db_param() . " WHERE id = " . db_param();
db_query( $t_query, array( $c_content, $t_attachment_id ) );
}
if( 'bug' == $p_table ) {
# bump the last_updated date
bug_update_date( $t_issue_id );
# add history entry
history_log_event_special( $t_issue_id, FILE_ADDED, $p_name );
}
return $t_attachment_id;
}
/**
* Returns the attachment contents
*
* @param integer $p_file_id File identifier.
* @param string $p_type The file type, bug or doc.
* @param integer $p_user_id A valid user identifier.
* @return string|soap_fault the string contents, or a soap_fault
*/
function mci_file_get( $p_file_id, $p_type, $p_user_id ) {
# we handle the case where the file is attached to a bug
# or attached to a project as a project doc.
$t_query = '';
switch( $p_type ) {
case 'bug':
$t_query = 'SELECT * FROM {bug_file} WHERE id=' . db_param();
break;
case 'doc':
$t_query = 'SELECT * FROM {project_file} WHERE id=' . db_param();
break;
default:
return SoapObjectsFactory::newSoapFault( 'Server', 'Invalid file type '. $p_type . ' .' );
}
$t_result = db_query( $t_query, array( $p_file_id ) );
if( $t_result->EOF ) {
return SoapObjectsFactory::newSoapFault( 'Client', 'Unable to find an attachment with type ' . $p_type. ' and id ' . $p_file_id . ' .' );
}
$t_row = db_fetch_array( $t_result );
if( $p_type == 'doc' ) {
$t_project_id = $t_row['project_id'];
} else if( $p_type == 'bug' ) {
$t_bug_id = $t_row['bug_id'];
$t_project_id = bug_get_field( $t_bug_id, 'project_id' );
}
$t_diskfile = file_normalize_attachment_path( $t_row['diskfile'], $t_project_id );
$t_content = $t_row['content'];
# Check access rights
switch( $p_type ) {
case 'bug':
if( !mci_file_can_download_bug_attachments( $t_bug_id, $p_user_id ) ) {
return mci_soap_fault_access_denied( $p_user_id );
}
break;
case 'doc':
# Check if project documentation feature is enabled.
if( OFF == config_get( 'enable_project_documentation' ) ) {
return mci_soap_fault_access_denied( $p_user_id );
}
if( !access_has_project_level( config_get( 'view_proj_doc_threshold' ), $t_project_id, $p_user_id ) ) {
return mci_soap_fault_access_denied( $p_user_id );
}
break;
}
# dump file content to the connection.
switch( config_get( 'file_upload_method' ) ) {
case DISK:
if( file_exists( $t_diskfile ) ) {
return mci_file_read_local( $t_diskfile ) ;
} else {
return SoapObjectsFactory::newSoapFault( 'Client', 'Unable to find an attachment with type ' . $p_type. ' and id ' . $p_file_id . ' .' );
}
case DATABASE:
return $t_content;
default:
trigger_error( ERROR_GENERIC, ERROR );
}
}