forked from mantisbt/mantisbt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
profile_api.php
308 lines (264 loc) · 9.18 KB
/
profile_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
<?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/>.
/**
* Profile API
*
* @package CoreAPI
* @subpackage ProfileAPI
* @copyright Copyright 2000 - 2002 Kenzaburo Ito - [email protected]
* @copyright Copyright 2002 MantisBT Team - [email protected]
* @link http://www.mantisbt.org
*
* @uses authentication_api.php
* @uses constant_inc.php
* @uses database_api.php
* @uses error_api.php
* @uses helper_api.php
* @uses lang_api.php
* @uses user_api.php
* @uses utility_api.php
*/
require_api( 'authentication_api.php' );
require_api( 'constant_inc.php' );
require_api( 'database_api.php' );
require_api( 'error_api.php' );
require_api( 'helper_api.php' );
require_api( 'lang_api.php' );
require_api( 'user_api.php' );
require_api( 'utility_api.php' );
/**
* Create a new profile for the user, return the ID of the new profile
* @param integer $p_user_id A valid user identifier.
* @param string $p_platform Value for profile platform.
* @param string $p_os Value for profile operating system.
* @param string $p_os_build Value for profile operation system build.
* @param string $p_description Description of profile.
* @return integer
*/
function profile_create( $p_user_id, $p_platform, $p_os, $p_os_build, $p_description ) {
$p_user_id = (int)$p_user_id;
if( ALL_USERS != $p_user_id ) {
user_ensure_unprotected( $p_user_id );
}
# platform cannot be blank
if( is_blank( $p_platform ) ) {
error_parameters( lang_get( 'platform' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
# os cannot be blank
if( is_blank( $p_os ) ) {
error_parameters( lang_get( 'os' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
# os_build cannot be blank
if( is_blank( $p_os_build ) ) {
error_parameters( lang_get( 'version' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
# Add profile
db_param_push();
$t_query = 'INSERT INTO {user_profile}
( user_id, platform, os, os_build, description )
VALUES
( ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ' )';
db_query( $t_query, array( $p_user_id, $p_platform, $p_os, $p_os_build, $p_description ) );
return db_insert_id( db_get_table( 'user_profile' ) );
}
/**
* Delete a profile for the user
*
* Note that although profile IDs are currently globally unique, the existing
* code included the user_id in the query and I have chosen to keep that for
* this API as it hides the details of id implementation from users of the API
* @param integer $p_user_id A valid user identifier.
* @param integer $p_profile_id A profile identifier.
* @return void
*/
function profile_delete( $p_user_id, $p_profile_id ) {
if( ALL_USERS != $p_user_id ) {
user_ensure_unprotected( $p_user_id );
}
# Delete the profile
db_param_push();
$t_query = 'DELETE FROM {user_profile} WHERE id=' . db_param() . ' AND user_id=' . db_param();
db_query( $t_query, array( $p_profile_id, $p_user_id ) );
}
/**
* Update a profile for the user
* @param integer $p_user_id A valid user identifier.
* @param integer $p_profile_id A profile identifier.
* @param string $p_platform Value for profile platform.
* @param string $p_os Value for profile operating system.
* @param string $p_os_build Value for profile operation system build.
* @param string $p_description Description of profile.
* @return void
*/
function profile_update( $p_user_id, $p_profile_id, $p_platform, $p_os, $p_os_build, $p_description ) {
if( ALL_USERS != $p_user_id ) {
user_ensure_unprotected( $p_user_id );
}
# platform cannot be blank
if( is_blank( $p_platform ) ) {
error_parameters( lang_get( 'platform' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
# os cannot be blank
if( is_blank( $p_os ) ) {
error_parameters( lang_get( 'os' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
# os_build cannot be blank
if( is_blank( $p_os_build ) ) {
error_parameters( lang_get( 'version' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
# Add item
db_param_push();
$t_query = 'UPDATE {user_profile}
SET platform=' . db_param() . ',
os=' . db_param() . ',
os_build=' . db_param() . ',
description=' . db_param() . '
WHERE id=' . db_param() . ' AND user_id=' . db_param();
db_query( $t_query, array( $p_platform, $p_os, $p_os_build, $p_description, $p_profile_id, $p_user_id ) );
}
/**
* Return a profile row from the database
* @param integer $p_user_id A valid user identifier.
* @param integer $p_profile_id A profile identifier.
* @return array
*/
function profile_get_row( $p_user_id, $p_profile_id ) {
db_param_push();
$t_query = 'SELECT * FROM {user_profile} WHERE id=' . db_param() . ' AND user_id=' . db_param();
$t_result = db_query( $t_query, array( $p_profile_id, $p_user_id ) );
return db_fetch_array( $t_result );
}
/**
* Return a profile row from the database
* @param integer $p_profile_id A profile identifier.
* @return array
* @todo relationship of this function to profile_get_row?
*/
function profile_get_row_direct( $p_profile_id ) {
db_param_push();
$t_query = 'SELECT * FROM {user_profile} WHERE id=' . db_param();
$t_result = db_query( $t_query, array( $p_profile_id ) );
return db_fetch_array( $t_result );
}
/**
* Return an array containing all rows for a given user
* @param integer $p_user_id A valid user identifier.
* @param boolean $p_all_users Include profiles for all users.
* @return array
*/
function profile_get_all_rows( $p_user_id, $p_all_users = false ) {
db_param_push();
$t_query_where = 'user_id = ' . db_param();
$t_param[] = (int)$p_user_id;
if( $p_all_users && ALL_USERS != $p_user_id ) {
$t_query_where .= ' OR user_id = ' . db_param();
$t_param[] = ALL_USERS;
}
$t_query = 'SELECT * FROM {user_profile} WHERE ' . $t_query_where . ' ORDER BY platform, os, os_build';
$t_result = db_query( $t_query, $t_param );
$t_rows = array();
while( $t_row = db_fetch_array( $t_result ) ) {
array_push( $t_rows, $t_row );
}
return $t_rows;
}
/**
* Return an array containing all profiles for a given user,
* including global profiles
* @param integer $p_user_id A valid user identifier.
* @return array
*/
function profile_get_all_for_user( $p_user_id ) {
return profile_get_all_rows( $p_user_id, $p_user_id != ALL_USERS );
}
/**
* Return an array of strings containing unique values for the specified field based
* on private and public profiles accessible to the specified user.
* @param string $p_field Field name of the profile to retrieve.
* @param integer $p_user_id A valid user identifier.
* @return array
*/
function profile_get_field_all_for_user( $p_field, $p_user_id = null ) {
$c_user_id = ( $p_user_id === null ) ? auth_get_current_user_id() : $p_user_id;
switch( $p_field ) {
case 'id':
case 'user_id':
case 'platform':
case 'os':
case 'os_build':
case 'description':
$c_field = $p_field;
break;
default:
trigger_error( ERROR_GENERIC, ERROR );
}
db_param_push();
$t_query = 'SELECT DISTINCT ' . $c_field . '
FROM {user_profile}
WHERE ( user_id=' . db_param() . ' ) OR ( user_id = 0 )
ORDER BY ' . $c_field;
$t_result = db_query( $t_query, array( $c_user_id ) );
$t_rows = array();
while( $t_row = db_fetch_array( $t_result ) ) {
array_push( $t_rows, $t_row[$c_field] );
}
return $t_rows;
}
/**
* Return an array containing all profiles used in a given project
* @param integer $p_project_id A valid project identifier.
* @return array
*/
function profile_get_all_for_project( $p_project_id ) {
$t_project_where = helper_project_specific_where( $p_project_id );
$t_query = 'SELECT DISTINCT(up.id), up.user_id, up.platform, up.os, up.os_build
FROM {user_profile} up, {bug} b
WHERE ' . $t_project_where . '
AND up.id = b.profile_id
ORDER BY up.platform, up.os, up.os_build';
$t_result = db_query( $t_query );
$t_rows = array();
while( $t_row = db_fetch_array( $t_result ) ) {
array_push( $t_rows, $t_row );
}
return $t_rows;
}
/**
* Returns the default profile
* @param integer $p_user_id A valid user identifier.
* @return string
*/
function profile_get_default( $p_user_id ) {
db_param_push();
$t_query = 'SELECT default_profile FROM {user_pref} WHERE user_id=' . db_param();
$t_result = db_query( $t_query, array( $p_user_id ) );
$t_default_profile = (int)db_result( $t_result, 0, 0 );
return $t_default_profile;
}
/**
* Returns whether the specified profile is global
* @param integer $p_profile_id A valid profile identifier.
* @return boolean
*/
function profile_is_global( $p_profile_id ) {
$t_row = profile_get_row( ALL_USERS, $p_profile_id );
return( $t_row !== false );
}