root/rss.php

Revision 336, 10.4 kB (checked in by Nafania, 1 year ago)

Мелкие баг-фиксы. Поддержка сфинкса (корявая) в поиске торрентов. sphinxapi.php надо использовать свой - тот что лежит, только для примера.
Список файлов показывается напрямую из торрент файла, а не из базы, но таблицы пока оставлены - на всякий случай.

Line 
1 <?php</span>
2 <span class="code-lang">define('IN_RSS', true);
3 $root_path = './';</span>
4 <span class="code-lang">require ($root_path . 'include/config.php');
5 require ($root_path . 'include/bbcode/bbcode.lib.php');
6
7 function ExitWithHeader( $output, $message = '' ) {
8
9     if( function_exists('getallheaders') ) {
10         header("HTTP/1.1 $output");
11      }
12     else {
13          header('Status: '. $output);
14      }
15
16     $code = intval(substr($output,0,3));
17     if ( ( $code == 200 ) || ( $code == 304 ) ) {
18         if( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ) {
19                 header('Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE']);
20             }
21             if( isset($_SERVER['HTTP_IF_NONE_MATCH']) ) {
22                 header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']);
23             }
24      }
25      if( $message ) {
26          header ('Content-Type: text/plain');
27         echo $message;
28      }
29      gc();
30 }
31
32 function RSSTimeFormat($utime, $uoffset=0) {
33         $result = gmdate('D, d M Y H:i:s', $utime + ($uoffset * 3600) );
34         $uoffset = intval($uoffset*100);
35         $result.= ' ' . ( ($uoffset > 0) ? '+' : '' ) . ( ( !$uoffset ) ? 'GMT' : sprintf( ( ( $uoffset < 0 ) ? '%05d' : '%04d' ) , $uoffset) );
36         return $result;
37 }
38
39 $rss_type = request_var('rss_type', '');
40
41 $db->sql_return_on_error(true);</span>
42 <span class="code-lang">
43 if ( isset($_GET['passkey']) ) {
44     $passkey = request_var('passkey', '');
45     $sql = 'SELECT uid, tzoffset, language, user_style, user_dst FROM ' . USERS_TABLE . ' WHERE torrent_pass = ' . "'" . $db->sql_escape($passkey) . "'";
46     $result = $db->sql_query($sql);
47     $tzoffset = ($row = $db->sql_fetchrow($result)) ? $row['tzoffset'] : '';
48     if ( $tzoffset == '') {
49         ExitWithHeader('500 Internal Server Error', 'Invalid user');
50      }
51
52      $userdata = array('uid' => $row['uid'], 'tzoffset' => $row['tzoffset'], 'language' => $row['language'], 'user_style' => $row['user_style'], 'user_dst' => $row['user_dst']);
53
54      init_userprefs($userdata);
55 }
56 else {
57      $userdata = session_pagestart($user_ip);
58      init_userprefs($userdata);
59      //loggedinorreturn();
60      $tzoffset = $userdata['tzoffset'];
61 }
62
63 $where = '';</span>
64 <span class="code-lang">if ( isset($_GET['cat']) ) {
65     $cat = explode(',', $_GET['cat']);
66     if ( $cat ) {
67         $cats = '';
68         foreach ( $cat AS $cat_val ) {
69             $cat_val = intval($cat_val);
70             $cats .= ( $cats ? ', '. $cat_val : $cat_val);
71         }
72         $where = 'category IN (' . $cats . ') AND';
73     }
74 }
75
76 $where .= ( $userdata['uid'] == ANONYMOUS ) ? ' c.anonymous_view = 1 AND' : '' ;
77
78 $cats_arr = $cache->obtain_cats();
79
80 $base_url = generate_base_url();
81
82 $template->assign_vars(array(</span>
83 <span class="code-lang">        'XML_HEADER' => '<?xml version="1.0" encoding="utf-8" ?>' . "\n",
84         'URL' => $base_url,
85         'IMAGE_URL' => $base_url . '/logo88x31.gif',
86         'TITLE' => htmlspecialchars($config['sitename']),
87         'DESCRIPTION' => htmlspecialchars($config['site_desc']),
88         'MANAGING_EDITOR' => $config['sitemail'],
89         'WEBMASTER' => $config['sitemail'],
90         'BUILD_DATE' => gmdate('D, d M Y H:i:s').' GMT',
91         'LANGUAGE' => format_language($config['default_lang']),
92         'COPYRIGHT' => 'Copyright ' . date('Y', time()) . ' ' . $config['sitename'] )
93 );
94
95 $sql = 'SELECT MAX(ctime) AS max_added FROM ' . TORRENTS_TABLE . ' t, ' . CATEGORIES_TABLE . ' c WHERE ' . $where . ' t.visible = 1 AND hidden = 0 AND t.category = c.id';
96 $result = $db->sql_query($sql);
97 $max_added = ($row = $db->sql_fetchrow($result)) ? $row['max_added'] : 0;
98 $db->sql_freeresult($result);
99
100 $deadline = gmdate('D, d M Y H:i:s', $max_added) . ' GMT';
101 $ETag = 'RSS' . gmdate('YmdHis', $max_added);
102
103 $s_modified = ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : '' );
104 $s_etag = ( isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : '' );
105
106 //here we get all announce urls
107 $base_url = generate_base_url();
108
109 $announce_urls = array();</span>
110 <span class="code-lang">if ( defined('USE_XBTT') ) {
111     $announce = $config['xbt_listen_url'] . ':' . $config['listen_port'] . '/' . ( $userdata['session_logged_in'] ? $userdata['torrent_pass'] . '/' : '' ) . 'announce/';
112 }
113 else {
114     $announce = $base_url . '/announce.php' . ( $userdata['session_logged_in'] ? '?passkey=' . $userdata['torrent_pass'] : '' );
115 }
116
117 $announce_urls['base'] = $announce;
118 $announce_urls[] = 'http://retracker.local/announce';
119 //here we get all announce urls
120
121
122 if( $s_modified == $deadline || $s_etag == $ETag ) {</span>
123 <span class="code-keyword">     ExitWithHeader('304 Not Modified', 'Not Modifed');
124 }
125 else {
126     require($root_path . 'include/class.bencode.php');
127
128     if ( $rss_cache = $cache->get('_rss_cache') === false ) {
129         $rss_cache = array();
130     }
131
132     $sql = "SELECT t.fid, t.name AS torrent_name, t.descr, t.filename, t.size, t.category, t.ctime, u.name, t.owner, u.privacy, t.info_hash, t.seeders, t.leechers
133         FROM " . TORRENTS_TABLE . " t, " . USERS_TABLE . " u, " . CATEGORIES_TABLE . " c
134         WHERE $where t.visible = 1
135         AND t.hidden = 0
136         AND t.banned = 0
137         AND t.moderated = 1
138         AND c.id = t.category
139         AND t.owner = u.uid
140         " . ( $s_modified ? ' AND t.ctime > ' . $s_modified : '' ) . ' ORDER BY t.ctime DESC';
141     $result = $db->sql_query_limit($sql, 100);
142
143     $bb_code = new bbcode();
144
145     while ( $row = $db->sql_fetchrow($result) ) {
146
147         $cat_id = $row['category'];
148         $cat_parent_id = ( isset($cats_arr[$cats_arr[$cat_id]['cat_parent']]['cat_id']) ? $cats_arr[$cats_arr[$cat_id]['cat_parent']]['cat_id'] : 0 );
149         $id = $row['fid'];
150
151         // www.phpBB-SEO.com SEO TOOLKIT BEGIN
152         $seo->set_torrent_url($id, $row['torrent_name'], $cat_id);
153         // www.phpBB-SEO.com SEO TOOLKIT END
154
155         $description = $row['descr'];
156
157         $bb_code->parse($description);
158          $description = $bb_code->get_html();
159          $description = censor_text($description);
160
161         $link = ( isset($_GET['feed']) ? 'details.php?id=' . $id . '&amp;action=download' : 'details.php?id=' . $id );
162         $size = mksize($row['size']);
163         $category = $cats_arr[$cat_id]['cat_name'];
164         $date = create_date($row['ctime']);
165         $owner = ( $row['owner'] ? ( $row['privacy'] == PRIVACY_LEVEL_HIGH && $row['owner'] <> $userdata['uid'] ? $lang['anonymous'] : $row['name'] ) : $lang['unknown'] );
166
167         if ( $seo->seo_opt['url_rewrite'] ) {
168             $u_link = append_sid($root_path . $link);
169             $u_link = $seo->drop_sid($u_link);
170             $u_reply = append_sid($root_path . 'comment.php?type=main&amp;action=add&amp;tid=' . $id);
171             $u_reply = $seo->drop_sid($u_reply);
172             $u_category = append_sid($root_path . 'browse.php?cat=' . $cat_id);
173             $u_category = $seo->drop_sid($u_category);
174             $u_download = $seo->drop_sid(append_sid($root_path . 'details.php?id=' . $id . '&amp;action=download'));
175             $u_view = $seo->drop_sid(append_sid($root_path . 'details.php?id=' . $id));
176         }
177         else {
178             $u_link = $base_url . '/' . $link;
179             $u_reply = $base_url . '/comment.php?type=main&amp;action=add&amp;tid=' . $id;
180             $u_category = $base_url . '/browse.php?cat=' . $cat_id;
181             $u_download = $base_url . '/details.php?id=' . $id . '&amp;action=download';
182             $u_view = $base_url . '/details.php?id=' . $id;
183         }
184         $description = preg_replace('/ onclick="(.*?)"/si', '', $description);
185
186         if ( !isset($rss_cache['filesize'][$row['fid']]) ) {
187             $torrent_file = $root_path . ( !empty($config['torrent_dir']) ? $config['torrent_dir'] : 'torrents') . '/' . $row['fid'] . '.torrent';
188             $rss_cache['filesize'][$row['fid']] = @filesize($torrent_file);
189         }
190         $filesize = ( $rss_cache['filesize'][$row['fid']] ? (int) $rss_cache['filesize'][$row['fid']] : 0 );
191
192         $info_hash = preg_replace_callback('/./s', create_function('$matches','return sprintf(\'%02x\', ord($matches[0]));'), str_pad($row['info_hash'], 20));
193
194         $template->assign_block_vars('torrent_item', array(
195                 'NAME' => $row['torrent_name'],
196                 'AUTHOR' => htmlspecialchars($row['name']),
197                 'CATEGORY' => htmlspecialchars($category),
198                 'CAT_ID' => $cat_id,
199                 'SIZE' => $size,
200                 'ADDED' => $date,
201                 'PUB_DATE' => RSSTimeFormat($row['ctime'], $tzoffset),
202                 'DESCRIPTION' => htmlspecialchars($description),
203                 'LINK' => $u_link,
204                 'U_REPLY' => $u_reply,
205                 'U_CATEGORY' => $u_category,
206
207                 'U_DOWNLOAD' => $u_download,
208                 'U_VIEW' => $u_view,
209                 'TORRENT_FILENAME' => $row['filename'],
210                 'TORRENT_FILE_LENGTH' => $filesize,
211                 'CONTENT_LEGTH' => $row['size'],
212
213                 'U_MAGNET' => 'magnet:?xt=urn:btih:' . $info_hash . '&amp;dn=' . rawurlencode($row['filename']) . '&amp;tr=' . implode('&amp;tr=', $announce_urls),
214                 'INFO_HASH' => $info_hash,
215         ));
216
217         foreach ( $announce_urls AS $ann_type => $announce_url ) {
218
219             $key = base64_encode($row['info_hash']);
220
221             if ( !isset($rss_cache['scrape_info'][$announce_url][$key]) ) {
222                 if ( $ann_type === 'base' ) {
223                     $seeders = $row['seeders'];
224                     $leechers = $row['leechers'];
225                 }
226                 else {
227                     if ( strpos($announce_url, $base_url) !== false ) {
228                     $scrape_info = array(
229                         'complete' => $row['seeders'],
230                         'incomplete' => $row['leechers'],
231                     );
232                     }
233                     else {
234                     $scrape_info = Torrent::scrape($announce_url, $row['info_hash']);
235                     }
236                     $seeders = (int) $scrape_info['complete'];
237                     $leechers = (int) $scrape_info['incomplete'];
238                 }
239
240                 $rss_cache['scrape_info'][$announce_url][$key]['seeders'] = $seeders;
241                 $rss_cache['scrape_info'][$announce_url][$key]['leechers'] = $leechers;
242             }
243
244             $seeders = $rss_cache['scrape_info'][$announce_url][$key]['seeders'];
245             $leechers = $rss_cache['scrape_info'][$announce_url][$key]['leechers'];
246
247             $template->assign_block_vars('torrent_item.trackers_list', array(
248                 'URL' => $announce_url,
249                 'SEEDS' => $seeders,
250                 'LEECHERS' => $leechers
251             ));
252         }
253     }
254     $db->sql_freeresult($result);
255
256     $cache->put('_rss_cache', $rss_cache, 60 * 60);
257
258     if (!empty($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache/2')) {
259         header ('Cache-Control: no-cache, pre-check=0, post-check=0, max-age=0');
260     }
261     else {
262         header ('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
263     }
264     $GMTtime = gmdate('D, d M Y H:i:s', $max_added) . ' GMT';
265
266     header('Content-Type: text/xml; charset=utf-8');
267     header('Last-Modified: ' . $GMTtime);
268     header('Expires: ' . gmdate("D, d M Y H:i:s", time()) . ' GMT');
269     header('Etag: ' . $ETag);
270
271     switch( $rss_type ){
272         case 'torrent':
273             $tpl = 'rss_ezrss.html';
274         break;
275         default:
276             $tpl = 'rss.html';
277         break;
278     }
279
280     $template->set_filenames(array(
281             'rss_tpl' => $tpl
282     ));
283
284     $template->display('rss_tpl');
285
286     gc();
287 }
288 ?>
289
Note: See TracBrowser for help on using the browser.