Updated init script.
[dtbartle/bnbt.git] / tracker_login.cpp
1 /***\r
2 *\r
3 * BNBT Beta 8.0 - A C++ BitTorrent Tracker\r
4 * Copyright (C) 2003-2004 Trevor Hogan\r
5 *\r
6 * CBTT variations (C) 2003-2005 Harold Feit\r
7 *\r
8 * This library is free software; you can redistribute it and/or\r
9 * modify it under the terms of the GNU Lesser General Public\r
10 * License as published by the Free Software Foundation; either\r
11 * version 2.1 of the License, or (at your option) any later version.\r
12 *\r
13 * This library is distributed in the hope that it will be useful,\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
16 * Lesser General Public License for more details.\r
17 *\r
18 * You should have received a copy of the GNU Lesser General Public\r
19 * License along with this library; if not, write to the Free Software\r
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
21 *\r
22 ***/\r
23 \r
24 #include "bnbt.h"\r
25 #include "atom.h"\r
26 #include "tracker.h"\r
27 #include "util.h"\r
28 \r
29 void CTracker :: serverResponseLogin( struct request_t *pRequest, struct response_t *pResponse, user_t user )\r
30 {\r
31         struct bnbttv btv = UTIL_CurrentTime( );\r
32 \r
33         if( user.strLogin.empty( ) )\r
34         {\r
35                 pResponse->strCode = "401 Unauthorized";\r
36 \r
37                 pResponse->mapHeaders.insert( pair<string, string>( "Content-Type", "text/plain" ) );\r
38                 pResponse->mapHeaders.insert( pair<string, string>( "WWW-Authenticate", string( "Basic realm=\"" ) + gstrRealm + "\"" ) );\r
39 \r
40                 pResponse->bCompressOK = false;\r
41 \r
42                 pResponse->strContent += "401 Unauthorized";\r
43 \r
44                 return;\r
45         }\r
46 \r
47         pResponse->strCode = "200 OK";\r
48 \r
49         // cookies\r
50 \r
51         time_t tNow = time( NULL );\r
52 \r
53         struct tm tmFuture = *gmtime( &tNow );\r
54         tmFuture.tm_mon++;\r
55         mktime( &tmFuture );\r
56         struct tm tmPast = *gmtime( &tNow );\r
57         tmPast.tm_mon--;\r
58         mktime( &tmPast );\r
59 \r
60         char pTime[256];\r
61         memset( pTime, 0, sizeof( char ) * 256 );\r
62 \r
63         string strLogout = pRequest->mapParams["logout"];\r
64 \r
65         if( strLogout == "1" )\r
66                 strftime( pTime, sizeof( char ) * 256, "%a, %d-%b-%Y %H:%M:%S GMT", &tmPast );\r
67         else\r
68                 strftime( pTime, sizeof( char ) * 256, "%a, %d-%b-%Y %H:%M:%S GMT", &tmFuture );\r
69 \r
70         pResponse->mapHeaders.insert( pair<string, string>( "Content-Type", string( "text/html; charset=" ) + gstrCharSet ) );\r
71         pResponse->mapHeaders.insert( pair<string, string>( "Pragma", "No-Cache" ) );\r
72         pResponse->mapHeaders.insert( pair<string, string>( "Set-Cookie", "login=\"" + user.strLogin + "\"; expires=" + pTime + "; path=/" ) );\r
73         pResponse->mapHeaders.insert( pair<string, string>( "Set-Cookie", "md5=\"" + UTIL_StringToEscaped( user.strMD5 ) + "\"; expires=" + pTime + "; path=/" ) );\r
74 \r
75         pResponse->strContent += "<html>\n";\r
76         pResponse->strContent += "<head>\n";\r
77         if ( !gstrTrackerTitle.empty( ) )\r
78                 pResponse->strContent += "<title>" + gstrTrackerTitle + " - Tracker Login</title>\n";\r
79         else\r
80                 pResponse->strContent += "<title>BNBT Tracker Login</title>\n";\r
81 \r
82         if( !gstrStyle.empty( ) )\r
83                 pResponse->strContent += "<link rel=stylesheet type=\"text/css\" href=\"" + gstrStyle + "\">\n";\r
84 \r
85         pResponse->strContent += "</head>\n";\r
86         pResponse->strContent += "<body>\n";\r
87 \r
88         if ( !gstrTrackerTitle.empty( ) )\r
89                 pResponse->strContent += "<h3>" + gstrTrackerTitle + " - Tracker Login - " + UTIL_RemoveHTML( user.strLogin ) + "</h3>\n";\r
90         else\r
91                 pResponse->strContent += "<h3>BNBT Login - " + UTIL_RemoveHTML( user.strLogin ) + "</h3>\n";\r
92 \r
93         if( strLogout == "1" ){\r
94                 pResponse->strContent += "<p>Logging out... You may need to close your browser window to completely logout.</p>\n";\r
95                 pResponse->strContent += "<p><a href=\"/index.html\">Return to Tracker</a></p>\n";\r
96         }\r
97         else\r
98         {\r
99                 // The Trinity Edition - Modification Begins\r
100                 // The following code reorganizes and creates additional navigation links\r
101                 // when viewing the LOGIN / MY TORRENTS page (login.html)\r
102 \r
103                 // Logout and RTT Link\r
104 \r
105                 pResponse->strContent += "<p>You signed up on " + user.strCreated + ".</p>\n";\r
106                 pResponse->strContent += "<p><a href=\"/login.html?logout=1\">Logout</a> | <a href=\"/index.html\">Return to Tracker</a></p>\n";\r
107 \r
108                 // Upload Link - ONLY DISPLAYED WHEN USER HAS UPLOAD RIGHTS\r
109                 // Sets a CSS class "uploadlink" which can be used to HIDE this link FROM ALL UPLOADERS using the following CSS command:\r
110                 // .uploadlink{display:none}\r
111 \r
112                 if( user.iAccess & ACCESS_UPLOAD ) {\r
113                         pResponse->strContent += "<p class=\"uploadlink\"><a href=\"/upload.html\">Upload a New Torrent</a></p>\n";\r
114                 }\r
115 \r
116                 // Administrative Links - ONLY DISPLAYED WHEN USER HAS ADMIN RIGHTS\r
117                 // Sets a CSS class "adminlinks" which can be used to HIDE this link FROM ALL ADMINS using the following CSS command:\r
118                 // .adminlinks{display:none}\r
119 \r
120                 if( user.iAccess & ACCESS_ADMIN ) {\r
121                         pResponse->strContent += "<p class=\"adminlinks\"><a href=\"/users.html\">User Configuration</a> | \n";\r
122             pResponse->strContent += "<a href=\"/admin.html\">Admin Panel</a></p>\n";\r
123                 }\r
124 \r
125                 /* Original Source Code:\r
126                 pResponse->strContent += "<ul>\n";\r
127                 pResponse->strContent += "<li>You signed up on " + user.strCreated + ".</li>\n";\r
128                 pResponse->strContent += "<li>Click <a href=\"/login.html?logout=1\">here</a> to logout.</li>\n";\r
129                 pResponse->strContent += "<li>Click <a href=\"/index.html\">here</a> to return to the tracker.</li>\n";\r
130                 pResponse->strContent += "</ul>\n";\r
131                 */\r
132 \r
133                 // ------------------------------------------------- End of Modification\r
134                 if( m_bDeleteOwnTorrents )\r
135                 {\r
136                         if( pRequest->mapParams.find( "del" ) != pRequest->mapParams.end( ) )\r
137                         {\r
138                                 string strDelHashString = pRequest->mapParams["del"];\r
139                                 string strDelHash = UTIL_StringToHash( strDelHashString );\r
140                                 string strOK = pRequest->mapParams["ok"];\r
141 \r
142                                 if( strDelHash.empty( ) )\r
143                                 {\r
144                                         pResponse->strContent += "<p>Unable to delete torrent " + strDelHashString + ". The info hash is invalid. Click <a href=\"/login.html\">here</a> to return to the login page.</p>\n";\r
145 \r
146                                         if( m_bGen )\r
147                                                 pResponse->strContent += "<p class=\"gen\">Generated in " + UTIL_ElapsedTimeStr( btv, UTIL_CurrentTime( ) ) + " seconds.</p>\n";\r
148 \r
149                                         pResponse->strContent += "</body>\n";\r
150                                         pResponse->strContent += "</html>\n";\r
151 \r
152                                         return;\r
153                                 }\r
154                                 else\r
155                                 {\r
156                                         if( strOK == "1" )\r
157                                         {\r
158                                                 if( m_pTags )\r
159                                                 {\r
160                                                         CAtom *pTagInfo = m_pTags->getItem( strDelHash );\r
161 \r
162                                                         if( pTagInfo && pTagInfo->isDicti( ) )\r
163                                                         {\r
164                                                                 CAtom *pUploader = ( (CAtomDicti *)pTagInfo )->getItem( "uploader" );\r
165 \r
166                                                                 string strUploader;\r
167 \r
168                                                                 if( pUploader )\r
169                                                                         strUploader = pUploader->toString( );\r
170 \r
171                                                                 if( strUploader != user.strLogin )\r
172                                                                 {\r
173                                                                         pResponse->strContent += "<p>Unable to delete torrent " + strDelHashString + ". You didn't upload that torrent. Click <a href=\"/login.html\">here</a> to return to the login page.</p>\n";\r
174 \r
175                                                                         if( m_bGen )\r
176                                                                                 pResponse->strContent += "<p class=\"gen\">Generated in " + UTIL_ElapsedTimeStr( btv, UTIL_CurrentTime( ) ) + " seconds.</p>\n";\r
177 \r
178                                                                         pResponse->strContent += "</body>\n";\r
179                                                                         pResponse->strContent += "</html>\n";\r
180 \r
181                                                                         return;\r
182                                                                 }\r
183                                                         }\r
184                                                 }\r
185 \r
186                                                 if( m_pAllowed )\r
187                                                 {\r
188                                                         // delete from disk\r
189 \r
190                                                         CAtom *pList = m_pAllowed->getItem( strDelHash );\r
191 \r
192                                                         if( pList && pList->isList( ) )\r
193                                                         {\r
194                                                                 vector<CAtom *> vecTorrent = ( (CAtomList *)pList )->getValue( );\r
195 \r
196                                                                 if( vecTorrent.size( ) == 6 )\r
197                                                                 {\r
198                                                                         CAtom *pFileName = vecTorrent[0];\r
199 \r
200                                                                         if( pFileName )\r
201                                                                         {\r
202                                                                                 if( m_strArchiveDir.empty( ) )\r
203                                                                                         UTIL_DeleteFile( ( m_strAllowedDir + pFileName->toString( ) ).c_str( ) );\r
204                                                                                 else\r
205                                                                                         UTIL_MoveFile( ( m_strAllowedDir + pFileName->toString( ) ).c_str( ), ( m_strArchiveDir + pFileName->toString( ) ).c_str( ) );\r
206                                                                         }\r
207                                                                 }\r
208                                                         }\r
209 \r
210                                                         m_pAllowed->delItem( strDelHash );\r
211                                                         m_pDFile->delItem( strDelHash );\r
212                                                         deleteTag( strDelHash );\r
213 \r
214                                                         pResponse->strContent += "<p>Deleted torrent " + strDelHashString + ". Click <a href=\"/login.html\">here</a> to return to the login page.</p>\n";\r
215 \r
216                                                         if( m_bGen )\r
217                                                                 pResponse->strContent += "<p class=\"gen\">Generated in " + UTIL_ElapsedTimeStr( btv, UTIL_CurrentTime( ) ) + " seconds.</p>\n";\r
218 \r
219                                                         pResponse->strContent += "</body>\n";\r
220                                                         pResponse->strContent += "</html>\n";\r
221 \r
222                                                         return;\r
223                                                 }\r
224                                         }\r
225                                         else\r
226                                         {\r
227                                                 pResponse->strContent += "<p>Are you sure you want to delete the torrent " + strDelHashString + "?</p>\n";\r
228                                                 pResponse->strContent += "<p><a href=\"/login.html?del=" + strDelHashString + "&ok=1\">OK</a></p>\n";\r
229                                                 pResponse->strContent += "</body>\n";\r
230                                                 pResponse->strContent += "</html>\n";\r
231 \r
232                                                 return;\r
233                                         }\r
234                                 }\r
235                         }\r
236                 }\r
237 \r
238                 if( m_pTags )\r
239                 {\r
240                         bool bFound = false;\r
241 \r
242                         map<string, CAtom *> *pmapDicti = m_pTags->getValuePtr( );\r
243 \r
244                         for( map<string, CAtom *> :: iterator i = pmapDicti->begin( ); i != pmapDicti->end( ); i++ )\r
245                         {\r
246                                 if( (*i).second->isDicti( ) )\r
247                                 {\r
248                                         CAtom *pUploader = ( (CAtomDicti *)(*i).second )->getItem( "uploader" );\r
249 \r
250                                         string strUploader;\r
251 \r
252                                         if( pUploader )\r
253                                                 strUploader = pUploader->toString( );\r
254 \r
255                                         if( strUploader != user.strLogin )\r
256                                                 continue;\r
257 \r
258                                         if( !bFound )\r
259                                         {\r
260                                                 pResponse->strContent += "<p>Your Torrents</p>\n";\r
261                                                 pResponse->strContent += "<ul>\n";\r
262 \r
263                                                 bFound = true;\r
264                                         }\r
265 \r
266                                         pResponse->strContent += "<li><a href=\"/stats.html?info_hash=" + UTIL_HashToString( (*i).first ) + "\">";\r
267 \r
268                                         string strName = UTIL_HashToString( (*i).first );\r
269 \r
270                                         if( m_pAllowed )\r
271                                         {\r
272                                                 CAtom *pTorrent = m_pAllowed->getItem( (*i).first );\r
273 \r
274                                                 if( pTorrent && pTorrent->isList( ) )\r
275                                                 {\r
276                                                         vector<CAtom *> vecTorrent = ( (CAtomList *)pTorrent )->getValue( );\r
277 \r
278                                                         if( vecTorrent.size( ) == 6 )\r
279                                                         {\r
280                                                                 if( vecTorrent[1] )\r
281                                                                         strName = vecTorrent[1]->toString( );\r
282                                                         }\r
283                                                 }\r
284                                         }\r
285 \r
286                                         pResponse->strContent += strName + "</a>";\r
287 \r
288                                         if( m_bDeleteOwnTorrents )\r
289                                                 pResponse->strContent += " [<a href=\"/login.html?del=" + UTIL_HashToString( (*i).first ) + "\">DELETE</a>]";\r
290 \r
291                                         pResponse->strContent += "</li>\n";\r
292                                 }\r
293                         }\r
294 \r
295                         if( bFound )\r
296                                 pResponse->strContent += "</ul>\n";\r
297                 }\r
298         }\r
299 \r
300         if( m_bGen )\r
301                 pResponse->strContent += "<p class=\"gen\">Generated in " + UTIL_ElapsedTimeStr( btv, UTIL_CurrentTime( ) ) + " seconds.</p>\n";\r
302 \r
303         pResponse->strContent += "</body>\n";\r
304         pResponse->strContent += "</html>\n";\r
305 }\r