/
rss_api.php
195 lines (164 loc) · 5.33 KB
/
rss_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
<?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/>.
/**
* RSS API
*
* @package CoreAPI
* @subpackage RSSAPI
* @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright (C) 2002 - 2013 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @uses authentication_api.php
* @uses config_api.php
* @uses constant_inc.php
* @uses crypto_api.php
* @uses current_user_api.php
* @uses helper_api.php
* @uses user_api.php
*/
require_api( 'authentication_api.php' );
require_api( 'config_api.php' );
require_api( 'constant_inc.php' );
require_api( 'crypto_api.php' );
require_api( 'current_user_api.php' );
require_api( 'helper_api.php' );
require_api( 'user_api.php' );
/**
* Calculates a key to be used for RSS authentication based on user name,
* cookie and password. If the user changes their user name or password, this
* RSS authentication key will become invalidated.
* @param int $p_user_id User ID for the user which the key is being calculated for
* @return string RSS authentication key (384bit) encoded according to the base64 with URI safe alphabet approach described in RFC4648
*/
function rss_calculate_key( $p_user_id = null ) {
if( $p_user_id === null ) {
$t_user_id = auth_get_current_user_id();
} else {
$t_user_id = $p_user_id;
}
$t_username = user_get_field( $t_user_id, 'username' );
$t_password = user_get_field( $t_user_id, 'password' );
$t_cookie = user_get_field( $t_user_id, 'cookie_string' );
$t_key_raw = hash( 'whirlpool', 'rss_key' . config_get_global( 'crypto_master_salt' ) . $t_username . $t_password . $t_cookie, true );
# Note: We truncate the last 8 bits from the hash output so that base64
# encoding can be performed without any trailing padding.
$t_key_base64_encoded = base64_encode( substr( $t_key_raw, 0, 63 ) );
$t_key = strtr( $t_key_base64_encoded, '+/', '-_' );
return $t_key;
}
/**
* Given the user name and the rss key, this method attempts to login the user. If successful, it
* return true, otherwise, returns false.
* @param string $p_username
* @param string $p_key
* @return bool
*/
function rss_login( $p_username, $p_key ) {
if(( $p_username === null ) || ( $p_key === null ) ) {
return false;
}
$t_user_id = user_get_id_by_name( $p_username );
if( false === $t_user_id ) {
return false;
}
$t_correct_key = rss_calculate_key( $t_user_id );
if( $p_key != $t_correct_key ) {
return false;
}
if( !auth_attempt_script_login( $p_username ) ) {
return false;
}
return true;
}
/**
* return rss issues feed url
* @param int $p_project_id
* @param string $p_username
* @param int $p_filter_id
* @param bool $p_relative
* @return string
*/
function rss_get_issues_feed_url( $p_project_id = null, $p_username = null, $p_filter_id = null, $p_relative = true ) {
if( $p_username === null ) {
$t_username = current_user_get_field( 'username' );
} else {
$t_username = $p_username;
}
if( $p_project_id === null ) {
$t_project_id = helper_get_current_project();
} else {
$t_project_id = (integer) $p_project_id;
}
$t_user_id = user_get_id_by_name( $t_username );
if( $p_relative ) {
$t_url = config_get( 'path' );
} else {
$t_url = '';
}
if( user_is_anonymous( $t_user_id ) ) {
$t_url .= 'issues_rss.php?';
if( $t_project_id == ALL_PROJECTS ) {
$t_url .= 'project_id=' . $t_project_id;
}
} else {
$t_url .= 'issues_rss.php?username=' . $t_username . '&key=' . rss_calculate_key( $t_user_id );
if( $t_project_id != ALL_PROJECTS ) {
$t_url .= '&project_id=' . $t_project_id;
}
}
if( $p_filter_id !== null ) {
$t_url .= '&filter_id=' . $p_filter_id;
}
return $t_url;
}
/**
* return rss news feed url
* @param int $p_project_id
* @param string $p_username
* @param bool $p_relative
* @return string
*/
function rss_get_news_feed_url( $p_project_id = null, $p_username = null, $p_relative = true ) {
if( $p_username === null ) {
$t_username = current_user_get_field( 'username' );
} else {
$t_username = $p_username;
}
if( $p_project_id === null ) {
$t_project_id = helper_get_current_project();
} else {
$t_project_id = (integer) $p_project_id;
}
if( $p_relative ) {
$t_rss_link = '';
} else {
$t_rss_link = config_get( 'path' );
}
$t_user_id = user_get_id_by_name( $t_username );
// If we have a logged in user then they can be given a 'proper' feed, complete with auth string.
if( user_is_anonymous( $t_user_id ) ) {
$t_rss_link .= "news_rss.php";
if( $t_project_id != ALL_PROJECTS ) {
$t_rss_link .= "?project_id=$t_project_id";
}
} else {
$t_rss_link .= "news_rss.php?username=$t_username&key=" . rss_calculate_key( $t_user_id );
if( $t_project_id != ALL_PROJECTS ) {
$t_rss_link .= "&project_id=$t_project_id";
}
}
return $t_rss_link;
}