/
news_api.php
326 lines (259 loc) · 8.87 KB
/
news_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
<?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/>.
/**
* News API
* @package CoreAPI
* @subpackage NewsAPI
* @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright (C) 2002 - 2012 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/
/**
* requires current_user_api
*/
require_once( 'current_user_api.php' );
/**
* requires twitter_api
*/
require_once( 'twitter_api.php' );
# --------------------
# Add a news item
function news_create( $p_project_id, $p_poster_id, $p_view_state, $p_announcement, $p_headline, $p_body ) {
$c_project_id = db_prepare_int( $p_project_id );
$c_poster_id = db_prepare_int( $p_poster_id );
$c_view_state = db_prepare_int( $p_view_state );
$c_announcement = db_prepare_bool( $p_announcement );
if( is_blank( $p_headline ) ) {
error_parameters( lang_get( 'headline' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
if( is_blank( $p_body ) ) {
error_parameters( lang_get( 'body' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
$t_news_table = db_get_table( 'mantis_news_table' );
# Add item
$query = "INSERT
INTO $t_news_table
( project_id, poster_id, date_posted, last_modified,
view_state, announcement, headline, body )
VALUES
( " . db_param() . ",
" . db_param() . ",
" . db_param() . ",
" . db_param() . ",
" . db_param() . ",
" . db_param() . ",
" . db_param() . ",
" . db_param() . "
)";
db_query_bound( $query, Array( $c_project_id, $c_poster_id, db_now(), db_now(), $c_view_state, $c_announcement, $p_headline, $p_body ) );
$t_news_id = db_insert_id( $t_news_table );
twitter_news( $t_news_id );
return $t_news_id;
}
# --------------------
# Delete the news entry
function news_delete( $p_news_id ) {
$c_news_id = db_prepare_int( $p_news_id );
$t_news_table = db_get_table( 'mantis_news_table' );
$query = "DELETE FROM $t_news_table
WHERE id=" . db_param();
db_query_bound( $query, Array( $c_news_id ) );
# db_query errors on failure so:
return true;
}
# --------------------
# Delete the news entry
function news_delete_all( $p_project_id ) {
$c_project_id = db_prepare_int( $p_project_id );
$t_news_table = db_get_table( 'mantis_news_table' );
$query = "DELETE FROM $t_news_table
WHERE project_id=" . db_param();
db_query_bound( $query, Array( $c_project_id ) );
# db_query errors on failure so:
return true;
}
# --------------------
# Update news item
function news_update( $p_news_id, $p_project_id, $p_view_state, $p_announcement, $p_headline, $p_body ) {
$c_news_id = db_prepare_int( $p_news_id );
$c_project_id = db_prepare_int( $p_project_id );
$c_view_state = db_prepare_int( $p_view_state );
$c_announcement = db_prepare_bool( $p_announcement );
if( is_blank( $p_headline ) ) {
error_parameters( lang_get( 'headline' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
if( is_blank( $p_body ) ) {
error_parameters( lang_get( 'body' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
$t_news_table = db_get_table( 'mantis_news_table' );
# Update entry
$query = "UPDATE $t_news_table
SET view_state=" . db_param() . ",
announcement=" . db_param() . ",
headline=" . db_param() . ",
body=" . db_param() . ",
project_id=" . db_param() . ",
last_modified= " . db_param() . "
WHERE id=" . db_param();
db_query_bound( $query, Array( $c_view_state, $c_announcement, $p_headline, $p_body, $c_project_id, db_now(), $c_news_id ) );
# db_query errors on failure so:
return true;
}
# --------------------
# Selects the news item associated with the specified id
function news_get_row( $p_news_id ) {
$c_news_id = db_prepare_int( $p_news_id );
$t_news_table = db_get_table( 'mantis_news_table' );
$query = "SELECT *
FROM $t_news_table
WHERE id=" . db_param();
$result = db_query_bound( $query, Array( $c_news_id ) );
if( 0 == db_num_rows( $result ) ) {
trigger_error( ERROR_NEWS_NOT_FOUND, ERROR );
} else {
$row = db_fetch_array( $result );
return $row;
}
}
# --------------------
# get news count (selected project plus sitewide posts)
function news_get_count( $p_project_id, $p_sitewide = true ) {
$c_project_id = db_prepare_int( $p_project_id );
$t_news_table = db_get_table( 'mantis_news_table' );
$t_project_where = helper_project_specific_where( $p_project_id );
$query = "SELECT COUNT(*)
FROM $t_news_table
WHERE $t_project_where";
if( $p_sitewide ) {
$query .= ' OR project_id=' . ALL_PROJECTS;
}
$result = db_query_bound( $query );
return db_result( $result, 0, 0 );
}
# --------------------
# get news items (selected project plus sitewide posts)
function news_get_rows( $p_project_id, $p_sitewide = true ) {
$t_news_table = db_get_table( 'mantis_news_table' );
$t_projects = current_user_get_all_accessible_subprojects( $p_project_id );
$t_projects[] = (int)$p_project_id;
if( $p_sitewide && ALL_PROJECTS != $p_project_id ) {
$t_projects[] = ALL_PROJECTS;
}
$query = "SELECT *
FROM $t_news_table";
if( 1 == count( $t_projects ) ) {
$c_project_id = $t_projects[0];
$query .= " WHERE project_id='$c_project_id'";
} else {
$query .= ' WHERE project_id IN (' . join( $t_projects, ',' ) . ')';
}
$query .= " ORDER BY date_posted DESC";
$result = db_query( $query );
$t_rows = array();
$t_row_count = db_num_rows( $result );
for( $i = 0;$i < $t_row_count;$i++ ) {
$row = db_fetch_array( $result );
array_push( $t_rows, $row );
}
return $t_rows;
}
# --------------------
# Check if the specified news item is private
function news_get_field( $p_news_id, $p_field_name ) {
$row = news_get_row( $p_news_id );
return( $row[$p_field_name] );
}
# --------------------
# Check if the specified news item is private
function news_is_private( $p_news_id ) {
return( news_get_field( $p_news_id, 'view_state' ) == VS_PRIVATE );
}
# --------------------
# Gets a limited set of news rows to be viewed on one page based on the criteria
# defined in the configuration file.
function news_get_limited_rows( $p_offset, $p_project_id = null ) {
if( $p_project_id === null ) {
$p_project_id = helper_get_current_project();
}
$c_offset = db_prepare_int( $p_offset );
$t_projects = current_user_get_all_accessible_subprojects( $p_project_id );
$t_projects[] = (int)$p_project_id;
if( ALL_PROJECTS != $p_project_id ) {
$t_projects[] = ALL_PROJECTS;
}
$t_news_table = db_get_table( 'mantis_news_table' );
$t_news_view_limit = config_get( 'news_view_limit' );
$t_news_view_limit_days = config_get( 'news_view_limit_days' ) * SECONDS_PER_DAY;
switch( config_get( 'news_limit_method' ) ) {
case 0:
# BY_LIMIT - Select the news posts
$query = "SELECT *
FROM $t_news_table";
if( 1 == count( $t_projects ) ) {
$c_project_id = $t_projects[0];
$query .= " WHERE project_id='$c_project_id'";
} else {
$query .= ' WHERE project_id IN (' . join( $t_projects, ',' ) . ')';
}
$query .= ' ORDER BY announcement DESC, id DESC';
$result = db_query( $query, $t_news_view_limit, $c_offset );
break;
case 1:
# BY_DATE - Select the news posts
$query = "SELECT *
FROM $t_news_table WHERE
( " . db_helper_compare_days( 0, 'date_posted', "< $t_news_view_limit_days" ) . "
OR announcement = " . db_param() . " ) ";
$t_params = Array(
db_now(),
1,
);
if( 1 == count( $t_projects ) ) {
$c_project_id = $t_projects[0];
$query .= " AND project_id=" . db_param();
$t_params[] = $c_project_id;
} else {
$query .= ' AND project_id IN (' . join( $t_projects, ',' ) . ')';
}
$query .= " ORDER BY announcement DESC, id DESC";
$result = db_query_bound( $query, $t_params, $t_news_view_limit, $c_offset );
break;
}
# end switch
$t_row_count = db_num_rows( $result );
$t_rows = array();
for( $i = 0;$i < $t_row_count;$i++ ) {
$row = db_fetch_array( $result );
array_push( $t_rows, $row );
}
return $t_rows;
}
# --------------------
# Checks if the news feature is enabled or not.
# true: enabled, otherwise false.
function news_is_enabled() {
return config_get( 'news_enabled' ) == ON;
}
# --------------------
# Ensures that the news feature is enabled, otherwise generates an access denied error.
function news_ensure_enabled() {
if ( !news_is_enabled() ) {
access_denied();
}
}