PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/acp/xbtbb3cker_losttorrents_module.php

https://gitlab.com/protoneutron/xbtbb3cker
PHP | 352 lines | 267 code | 65 blank | 20 comment | 55 complexity | 50814477fe3e548379a40237407b41cc MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package xbtBB3cker
  5. * @copyright (c) 2015 PPK
  6. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
  7. *
  8. *
  9. */
  10. namespace ppk\xbtbb3cker\acp;
  11. /**
  12. * @ignore
  13. */
  14. if (!defined('IN_PHPBB'))
  15. {
  16. exit;
  17. }
  18. /**
  19. * @package acp
  20. */
  21. class xbtbb3cker_losttorrents_module
  22. {
  23. function main($id, $mode)
  24. {
  25. global $config, $request, $template, $user, $db, $table_prefix, $cache, $pagination, $phpbb_container, $auth, $phpEx, $phpbb_root_path;
  26. $user->add_lang_ext('ppk/xbtbb3cker', 'acp_xbtbb3cker_losttorrents');
  27. $this->page_title = $user->lang('ACP_XBTBB3CKER_LOSTTORRENTS');
  28. $this->tpl_name = 'acp_xbtbb3cker_losttorrents';
  29. $submit = (isset($_POST['submit'])) ? true : false;
  30. $form_key = 'info_acp_xbtbb3cker_losttorrents';
  31. add_form_key($form_key);
  32. $error = array();
  33. $pagination = $phpbb_container->get('pagination');
  34. $user->add_lang('mcp');
  35. // Set up general vars
  36. $action = $request->variable('action', '');
  37. $forum_id = $request->variable('f', 0);
  38. $topic_id = $request->variable('t', 0);
  39. $start = $request->variable('start', 0);
  40. $fixmarked = $request->variable('fixmarked', '');
  41. $fixall = $request->variable('fixall', '');
  42. $deletemarked = $request->variable('deletemarked', '');
  43. $deleteall = $request->variable('deleteall', '');
  44. $marked = $request->variable('mark', array(0));
  45. $last_id=$request->variable('last_id', 0);
  46. // Sort keys
  47. $sort_days = $request->variable('st', 0);
  48. $sort_key = $request->variable('sk', 't');
  49. $sort_dir = $request->variable('sd', 'd');
  50. $forum_trackers=array();
  51. $sql='SELECT forum_id FROM '.FORUMS_TABLE." WHERE forum_tracker='1'";
  52. $result=$db->sql_query($sql);
  53. while($row=$db->sql_fetchrow($result))
  54. {
  55. $forum_trackers[]=$row['forum_id'];
  56. }
  57. $db->sql_freeresult($result);
  58. // Fix entries if requested and able
  59. if (($fixmarked || $fixall || $deletemarked || $deleteall) && $auth->acl_get('a_clearlogs'))
  60. {
  61. if (confirm_box(true) || ($fixall && $last_id) || ($deleteall && $last_id))
  62. {
  63. $torrents_id = array();
  64. if (($fixmarked || $deletemarked) && count($marked))
  65. {
  66. foreach ($marked as $mark)
  67. {
  68. $torrents_id[] = intval($mark);
  69. }
  70. }
  71. if($fixall || $deleteall)
  72. {
  73. $sql='SELECT a.attach_id FROM '.POSTS_TABLE.' p, '.USERS_TABLE.' u, '.ATTACHMENTS_TABLE.' a LEFT JOIN '.XBT_FILES.' x ON(a.attach_id=x.fid) WHERE '.$db->sql_in_set('p.forum_id', $forum_trackers)." AND p.post_id=a.post_msg_id AND a.extension='torrent' AND a.in_message='0' AND p.poster_id=u.user_id AND x.fid IS NULL AND a.attach_id > $last_id ORDER BY a.attach_id LIMIT $start, {$config['topics_per_page']}";
  74. $result=$db->sql_query($sql);
  75. while($row=$db->sql_fetchrow($result))
  76. {
  77. $torrents_id[]=$row['attach_id'];
  78. }
  79. $db->sql_freeresult($result);
  80. }
  81. if($deletemarked || $deleteall)
  82. {
  83. if (!function_exists('delete_attachments'))
  84. {
  85. include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  86. }
  87. if ($torrents_id || $deleteall)
  88. {
  89. if(!$torrents_id)
  90. {
  91. trigger_error(sprintf($user->lang['FIX_LOSTTORRENTS_FINISH'], $this->u_action));
  92. }
  93. delete_attachments('attach', $torrents_id);
  94. add_log('admin', 'LOG_DELETE_LOSTTORRENTS_' . strtoupper($mode));
  95. if($deletemarked)
  96. {
  97. trigger_error(sprintf($user->lang['FIX_LOSTTORRENTS_FINISH'], $this->u_action));
  98. }
  99. if($deleteall && $torrents_id)
  100. {
  101. $redirect_url=$this->u_action.'&amp;deleteall=1&amp;last_id=1';
  102. meta_refresh(3, $redirect_url);
  103. }
  104. trigger_error(($deleteall ? $user->lang['FIX_LOSTTORRENTS_WAIT'] : '').sprintf($user->lang['FIX_LOSTTORRENTS_RESULT'], '', $this->u_action));
  105. }
  106. }
  107. else
  108. {
  109. if ($torrents_id || $fixall)
  110. {
  111. if(!$torrents_id)
  112. {
  113. trigger_error(sprintf($user->lang['FIX_LOSTTORRENTS_FINISH'], $this->u_action));
  114. }
  115. $db->sql_query('DELETE FROM '.XBT_FILES.' WHERE '.$db->sql_in_set('fid', $torrents_id));
  116. $db->sql_query('DELETE FROM '.TRACKER_FILES_TABLE.' WHERE '.$db->sql_in_set('torrent', $torrents_id));
  117. $db->sql_query('DELETE FROM '.XBT_FILES_USERS.' WHERE '.$db->sql_in_set('fid', $torrents_id));
  118. $db->sql_query('DELETE FROM '.TRACKER_RANNOUNCES_TABLE.' WHERE '.$db->sql_in_set('torrent', $torrents_id));
  119. $db->sql_query('DELETE FROM '.TRACKER_RTRACK_TABLE.' WHERE '.$db->sql_in_set('user_torrent_zone', $torrents_id))." AND rtracker_type='t'";
  120. include_once("{$phpbb_root_path}ext/ppk/xbtbb3cker/include/recode_torrent.{$phpEx}");
  121. include_once("{$phpbb_root_path}ext/ppk/xbtbb3cker/include/bencoding.{$phpEx}");
  122. $torrent_status=$success_posts=$success_topics=array();
  123. $sql = 'SELECT a.physical_filename, a.real_filename, a.attach_id, a.topic_id, a.post_msg_id post_id, a.poster_id, a.topic_id, p.forum_id FROM ' . ATTACHMENTS_TABLE . ' a, '.POSTS_TABLE.' p WHERE a.post_msg_id=p.post_id AND '.$db->sql_in_set('a.attach_id', $torrents_id);
  124. $result=$db->sql_query($sql);
  125. $message='';
  126. while($row=$db->sql_fetchrow($result))
  127. {
  128. isset($torrent_status[$row['forum_id']]) ? '' : $torrent_status[$row['forum_id']] = ($auth->acl_get('u_canskiptcheck') && $auth->acl_get('f_canskiptcheck', $row['forum_id'])) ? $config['ppkbb_tcdef_statuses'][0] : $config['ppkbb_tcdef_statuses'][1];
  129. $fix_result=recode_torrent_data($row['physical_filename'], $row['attach_id'], $row['forum_id'], $row['topic_id'], $row['post_id'], $torrent_status[$row['forum_id']], array(), $row['poster_id']);
  130. $message.=($fix_result===true ? $user->lang['FIX_LOSTTORRENTS_SUCCESS'] : $user->lang['FIX_LOSTTORRENTS_ERROR'].' ('.$fix_result.')')
  131. .'<a href= "'.append_sid("{$phpbb_root_path}viewtopic.{$phpEx}", "p={$row['post_id']}").'#p'.$row['post_id'].'">'.urldecode($row['real_filename']).'</a><br />';
  132. if($fix_result===true)
  133. {
  134. $success_posts[]=$row['post_id'];
  135. $success_topics[]=$row['topic_id'];
  136. }
  137. $last_id=$row['attach_id'];
  138. }
  139. $db->sql_freeresult($result);
  140. if($success_posts)
  141. {
  142. $db->sql_query('UPDATE '.POSTS_TABLE." SET post_torrent='1' WHERE ".$db->sql_in_set('post_id', $success_posts));
  143. $db->sql_query('UPDATE '.TOPICS_TABLE." SET topic_torrent='1' WHERE ".$db->sql_in_set('topic_id', $success_topics));
  144. }
  145. add_log('admin', 'LOG_FIX_LOSTTORRENTS_' . strtoupper($mode));
  146. if($fixall && $message)
  147. {
  148. $redirect_url=$this->u_action.'&amp;fixall=1&amp;last_id='.$last_id;
  149. meta_refresh(5, $redirect_url);
  150. }
  151. trigger_error(($fixall ? $user->lang['FIX_LOSTTORRENTS_WAIT'] : '').sprintf($user->lang['FIX_LOSTTORRENTS_RESULT'], $message, $this->u_action));
  152. }
  153. }
  154. }
  155. else
  156. {
  157. confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
  158. 'f' => $forum_id,
  159. 'start' => $start,
  160. 'fixmarked' => $fixmarked,
  161. 'fixall' => $fixall,
  162. 'deletemarked' => $deletemarked,
  163. 'deleteall' => $deleteall,
  164. 'mark' => $marked,
  165. 'st' => $sort_days,
  166. 'sk' => $sort_key,
  167. 'sd' => $sort_dir,
  168. 'i' => $id,
  169. 'mode' => $mode,
  170. 'action' => $action))
  171. );
  172. }
  173. }
  174. // Sorting
  175. $limit_days = array(
  176. 0 => $user->lang['ALL_ENTRIES'],
  177. 1 => $user->lang['1_DAY'],
  178. 7 => $user->lang['7_DAYS'],
  179. 14 => $user->lang['2_WEEKS'],
  180. 30 => $user->lang['1_MONTH'],
  181. 90 => $user->lang['3_MONTHS'],
  182. 180 => $user->lang['6_MONTHS'],
  183. 365 => $user->lang['1_YEAR'],
  184. );
  185. $sort_by_text = array(
  186. 'u' => $user->lang['SORT_USERNAME'],
  187. 't' => $user->lang['SORT_DATE'],
  188. 'f' => $user->lang['SORT_FILENAME'],
  189. 'p' => $user->lang['SORT_TTITLE'],
  190. );
  191. $sort_by_sql = array(
  192. 'u' => 'u.username_clean',
  193. 't' => 'a.filetime',
  194. 'f' => 'a.real_filename',
  195. 'p'=>'p.post_subject',
  196. );
  197. $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
  198. gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
  199. // Define where and sort sql for use in displaying logs
  200. $sql_where = ($sort_days) ? (time() - ($sort_days * 86400)) : 0;
  201. $sql_sort = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
  202. // Grab log data
  203. $log_data = array();
  204. $log_count = 0;
  205. if($forum_trackers)
  206. {
  207. $sql='SELECT COUNT(*) log_count FROM '.POSTS_TABLE.' p, '.USERS_TABLE.' u, '.ATTACHMENTS_TABLE.' a LEFT JOIN '.XBT_FILES.' x ON(a.attach_id=x.fid) WHERE '.$db->sql_in_set('p.forum_id', $forum_trackers)." AND p.post_id=a.post_msg_id AND a.extension='torrent' AND a.in_message='0' AND p.poster_id=u.user_id AND x.fid IS NULL".($sql_where ? ' AND a.filetime > '.$sql_where : '')."";
  208. $result=$db->sql_query($sql);
  209. $log_count=$db->sql_fetchrow($result);
  210. $db->sql_freeresult($result);
  211. $log_count=intval($log_count['log_count']);
  212. if($log_count)
  213. {
  214. $sql='SELECT u.username, u.user_colour, a.real_filename, a.filetime, a.post_msg_id, a.attach_id, p.post_subject, p.topic_id, p.forum_id, p.poster_id FROM '.POSTS_TABLE.' p, '.USERS_TABLE.' u, '.ATTACHMENTS_TABLE.' a LEFT JOIN '.XBT_FILES.' x ON(a.attach_id=x.fid) WHERE '.$db->sql_in_set('p.forum_id', $forum_trackers)." AND p.post_id=a.post_msg_id AND a.extension='torrent' AND a.in_message='0' AND p.poster_id=u.user_id AND x.fid IS NULL".($sql_where ? ' AND a.filetime > '.$sql_where : '')." ORDER BY $sql_sort LIMIT $start, {$config['topics_per_page']}";
  215. $result=$db->sql_query($sql);
  216. while($row=$db->sql_fetchrow($result))
  217. {
  218. $log_data[]=$row;
  219. }
  220. $db->sql_freeresult($result);
  221. }
  222. }
  223. $template->assign_vars(array(
  224. 'S_ON_PAGE' => $pagination->get_on_page($config['topics_per_page'], $start),
  225. 'TOTAL_LOGS' => $log_count ? sprintf($user->lang['TOTAL_LOGS'], $log_count) : false,
  226. 'S_LIMIT_DAYS' => $s_limit_days,
  227. 'S_SORT_KEY' => $s_sort_key,
  228. 'S_SORT_DIR' => $s_sort_dir,
  229. 'S_CLEARLOGS' => $auth->acl_get('a_clearlogs'),
  230. )
  231. );
  232. $pagination->generate_template_pagination($this->u_action . "&amp;$u_sort_param", 'pagination', 'start', $log_count, $config['topics_per_page'], $start);
  233. foreach ($log_data as $row)
  234. {
  235. $template->assign_block_vars('log', array(
  236. 'USERNAME' => empty($row['username']) || $row['poster_id']==1 ? $user->lang['TRACKER_ANONYMOUS'] : get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['username']),
  237. 'DATE' => $user->format_date($row['filetime'], 'Y-m-d H:i:s'),
  238. 'FILENAME' => urldecode($row['real_filename']),
  239. 'TTITLE' => !empty($row['post_subject']) ? $row['post_subject'] : $user->lang['TORRENT_DELETED'],
  240. 'URL' => !empty($row['post_subject']) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f={$row['forum_id']}&amp;t={$row['topic_id']}&amp;p={$row['post_msg_id']}")."#p{$row['post_msg_id']}" : '',
  241. 'FILEURL' => !empty($row['real_filename']) ? append_sid("{$phpbb_root_path}download/file.$phpEx", "id={$row['attach_id']}") : '',
  242. 'ID' => $row['attach_id'],
  243. )
  244. );
  245. }
  246. $template->assign_vars(array(
  247. 'L_TITLE' => $user->lang['ACP_XBTBB3CKER_LOSTTORRENTS'],
  248. 'L_TITLE_EXPLAIN' => $user->lang['ACP_XBTBB3CKER_LOSTTORRENTS_EXPLAIN'],
  249. 'S_ERROR' => (count($error)) ? true : false,
  250. 'ERROR_MSG' => implode('<br />', $error),
  251. 'U_ACTION' => $this->u_action,
  252. ));
  253. }
  254. function my_int_val($v=0, $max=0, $drop=false, $negative=false)
  255. {
  256. if(!$v || ($v < 0 && !$negative))
  257. {
  258. return 0;
  259. }
  260. else if($drop && $v>$max)
  261. {
  262. return 0;
  263. }
  264. else if($max && $v>$max)
  265. {
  266. return $max;
  267. }
  268. return @number_format($v+0, 0, '', '');
  269. }
  270. function my_float_val($v=0, $n=3, $max=0, $drop=false, $negative=false)
  271. {
  272. if(!$v || ($v < 0 && !$negative))
  273. {
  274. return "0.".str_repeat('0', $n);
  275. }
  276. else if($drop && $v>$max)
  277. {
  278. return "0.".str_repeat('0', $n);
  279. }
  280. else if($max && $v>$max)
  281. {
  282. return $max;
  283. }
  284. return @number_format($v+0, $n, '.', '');
  285. }
  286. }
  287. ?>