root/docleanup.php

Revision 316, 10.8 kB (checked in by Nafania, 2 years ago)

Апдейт добавляет возможность получать уведомление на мыло о новых комментариях, и небольшие изменения с уведомлениями в панели управления.
Мелкие фиксы.

Line 
1 <?php</span>
2 <span class="code-lang">$root_path = './';
3 require_once($root_path . 'include/config.php');
4 $userdata = session_pagestart($user_ip);
5 init_userprefs($userdata);
6 loggedinorreturn();</span>
7 <span class="code-lang">
8 @set_time_limit(0);
9
10 $step = request_var('step', 0);</span>
11 <span class="code-lang">
12 if ( $userdata['class'] < UC_SYSOP ) {
13         trigger_error($lang['access_denied']);
14 }
15 else {
16
17     switch($step) {
18         case 0:
19             $db->sql_query('DELETE c.*  FROM ' . COMMENTS_TABLE . ' c       LEFT JOIN ' . TORRENTS_TABLE . ' t ON ( c.comment_for_id = t.fid ) WHERE t.name IS NULL AND c.comment_type = ' . TYPE_TORRENT);
20             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned comments from torrents deleted', LOG_VIEW_SYSOP);
21             $db->sql_query('DELETE f.*  FROM ' . FILES_TABLE . ' f          LEFT JOIN ' . TORRENTS_TABLE . ' t ON f.torrent = t.fid    WHERE t.name IS NULL');
22             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned files from torrents deleted', LOG_VIEW_SYSOP);
23             $db->sql_query('DELETE s.*  FROM ' . SOS_TABLE . ' s            LEFT JOIN ' . TORRENTS_TABLE . ' t ON s.torrentid = t.fid  WHERE t.name IS NULL');
24             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned sos requests deleted', LOG_VIEW_SYSOP);
25             $db->sql_query('DELETE t1.* FROM ' . THANKS_TABLE . ' t1        LEFT JOIN ' . TORRENTS_TABLE . ' t ON t1.torrentid = t.fid WHERE t.name IS NULL');
26             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned thankses deleted', LOG_VIEW_SYSOP);
27             $db->sql_query('DELETE s.*  FROM ' . SNATCHED_TABLE . ' s       LEFT JOIN ' . TORRENTS_TABLE . ' t ON s.torrentid = t.fid  WHERE t.name IS NULL');
28             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned snatched rows deleted', LOG_VIEW_SYSOP);
29             $db->sql_query('DELETE r.*  FROM ' . RATINGS_TABLE . ' r        LEFT JOIN ' . TORRENTS_TABLE . ' t ON r.torrent = t.fid    WHERE t.name IS NULL');
30             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned ratings deleted', LOG_VIEW_SYSOP);
31             $db->sql_query('DELETE i.*  FROM ' . INDEXRELEASES_TABLE . ' i   LEFT JOIN ' . TORRENTS_TABLE . ' t ON i.torrent_id = t.fid WHERE t.name IS NULL');
32             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned indexreleases deleted', LOG_VIEW_SYSOP);
33             $db->sql_query('DELETE c.*  FROM ' . COMMENTS_NOTIFY_TABLE . ' c      LEFT JOIN ' . TORRENTS_TABLE . ' t ON ( c.checkcomm_for_id = t.fid ) WHERE t.name IS NULL AND c.checkcomm_type = ' . TYPE_TORRENT);
34             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned comments notifis deleted', LOG_VIEW_SYSOP);
35             $db->sql_query('DELETE b.*  FROM ' . BOOKMARKS_TABLE . ' b      LEFT JOIN ' . TORRENTS_TABLE . ' t ON b.torrentid = t.fid  WHERE t.name IS NULL');
36             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned bookmarks deleted', LOG_VIEW_SYSOP);
37             $db->sql_query('DELETE s.*  FROM ' . SNATCHED_TABLE . ' s      LEFT JOIN ' . TORRENTS_TABLE . ' t ON s.torrentid = t.fid  WHERE t.name IS NULL');
38             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned snatched deleted', LOG_VIEW_SYSOP);
39             $db->sql_query('DELETE r.*  FROM ' . REPORTS_TABLE . ' r      LEFT JOIN ' . TORRENTS_TABLE . ' t ON r.votedfor = t.fid  WHERE t.name IS NULL AND r.type = 2');
40             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned reports deleted', LOG_VIEW_SYSOP);
41         break;
42
43         case 1:
44             $db->sql_query('DELETE c.*  FROM ' . COMMENTS_TABLE . ' c       LEFT JOIN ' . REQUESTS_TABLE . ' r ON ( c.comment_for_id = r.id ) WHERE r.request IS NULL AND c.comment_type = ' . TYPE_REQUEST);
45             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned comments from requests deleted', LOG_VIEW_SYSOP);
46             $db->sql_query('DELETE c.*  FROM ' . COMMENTS_TABLE . ' c       LEFT JOIN ' . OFFERS_TABLE . ' o   ON ( c.comment_for_id = o.id ) WHERE o.name IS NULL AND c.comment_type = ' . TYPE_OFFER);
47             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned comments from offers deleted', LOG_VIEW_SYSOP);
48             $db->sql_query('DELETE v.*  FROM ' . REQUESTS_VOTES_TABLE . ' v LEFT JOIN ' . REQUESTS_TABLE . ' r ON v.requestid = r.id  WHERE r.request IS NULL');
49             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned requests votes deleted', LOG_VIEW_SYSOP);
50             $db->sql_query('DELETE ov.* FROM ' . OFFERS_VOTES_TABLE . ' ov  LEFT JOIN ' . OFFERS_TABLE . ' o   ON ov.offerid = o.id   WHERE o.name IS NULL');
51             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned offers votes deleted', LOG_VIEW_SYSOP);
52         break;
53
54         case 2:
55             $db->sql_query('DELETE m.* FROM ' . PRIVATE_MESSAGES_TABLE . ' m  LEFT JOIN ' . USERS_TABLE . ' u  ON m.sender = u.uid   WHERE u.uid IS NULL');
56             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned private messages deleted', LOG_VIEW_SYSOP);
57             $db->sql_query('DELETE m.* FROM ' . PRIVATE_MESSAGES_TABLE . ' m  LEFT JOIN ' . USERS_TABLE . ' u  ON m.receiver = u.uid   WHERE u.uid IS NULL');
58             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned private messages deleted', LOG_VIEW_SYSOP);
59             $db->sql_query('DELETE s.* FROM ' . SNATCHED_TABLE . ' s      LEFT JOIN ' . USERS_TABLE . ' u ON s.userid = u.uid  WHERE u.uid IS NULL');
60             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned snatched deleted', LOG_VIEW_SYSOP);
61             $db->sql_query('DELETE f.* FROM ' . FRIENDS_TABLE . ' f      LEFT JOIN ' . USERS_TABLE . ' u ON f.userid = u.uid  WHERE u.uid IS NULL');
62             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned friends deleted', LOG_VIEW_SYSOP);
63             $db->sql_query('DELETE f.* FROM ' . FRIENDS_TABLE . ' f      LEFT JOIN ' . USERS_TABLE . ' u ON f.friendid = u.uid  WHERE u.uid IS NULL');
64             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned friends deleted', LOG_VIEW_SYSOP);
65             $db->sql_query('DELETE f.* FROM ' . FRIENDS_TABLE . ' f      LEFT JOIN ' . USERS_TABLE . ' u ON f.blockid = u.uid  WHERE u.uid IS NULL');
66             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned friends deleted', LOG_VIEW_SYSOP);
67             $db->sql_query('DELETE r.* FROM ' . REPORTS_TABLE . ' r      LEFT JOIN ' . USERS_TABLE . ' u ON r.addedby = u.uid  WHERE u.uid IS NULL');
68             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned reports deleted', LOG_VIEW_SYSOP);
69             $db->sql_query('DELETE r.* FROM ' . REPORTS_TABLE . ' r      LEFT JOIN ' . USERS_TABLE . ' u ON r.votedfor = u.uid  WHERE u.uid IS NULL AND r.type = 1');
70             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned reports deleted', LOG_VIEW_SYSOP);
71             $db->sql_query('DELETE s.* FROM ' . SESSIONS_TABLE . ' s      LEFT JOIN ' . USERS_TABLE . ' u ON s.session_user_id = u.uid  WHERE u.uid IS NULL');
72             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned sessions deleted', LOG_VIEW_SYSOP);
73             $db->sql_query('DELETE s.* FROM ' . SESSIONS_KEYS_TABLE . ' s      LEFT JOIN ' . USERS_TABLE . ' u ON s.user_id = u.uid  WHERE u.uid IS NULL');
74             write_log('docleanup - ' . $db->sql_affectedrows() . ' orphaned session keys deleted', LOG_VIEW_SYSOP);
75         break;
76
77         case 3:
78             //begin delete some old torrent-files and not torrent files from torrents dir
79             $sql = 'SELECT fid FROM ' . TORRENTS_TABLE;
80             $result = $db->sql_query($sql);
81             $torrents = $files = $delete_files = array();
82             while ($row = $db->sql_fetchrow($result)) {
83                 $torrents[$row['fid']] = 1;
84             }
85             $torrent_dir = $root_path . ( !empty($config['torrent_dir']) ? $config['torrent_dir'] : 'torrents');
86
87             if ( $dp = opendir($torrent_dir . DIRECTORY_SEPARATOR) ) {
88                 while ( ( $file = readdir($dp) ) !== false ) {
89                     if ( $file != '.' && $file != '..' && !is_dir($torrent_dir . DIRECTORY_SEPARATOR . $file) ) {
90                         if ( !preg_match('/^(\d+)\.torrent$/', $file, $matches) ) {
91                             $delete_files[] = $file;
92                         }
93                         else {
94                             if ( !isset($torrents[$matches[1]]) ) {
95                                 $delete_files[] = $file;
96                             }
97                         }
98                     }
99                 }
100                 closedir($dp);
101             }
102             if ( sizeof($delete_files) ) {
103                 for ( $i = 0; $i < sizeof($delete_files); ++$i ) {
104                     @unlink($torrent_dir . '/' . $delete_files[$i]);
105                     write_log('docleanup - ' . $delete_files[$i] . ' file deleted - not present in torrents table', LOG_VIEW_SYSOP);
106                 }
107             }
108             //end delete some old torrent-files and not torrent files from torrents dir
109         break;
110
111         case 4:
112             $user_uid = array();
113             $sql = 'SELECT COUNT(m.id) AS unread_pms, m.receiver, u.user_unread_pms
114             FROM ' . PRIVATE_MESSAGES_TABLE . ' m, ' . USERS_TABLE . ' u
115             WHERE m.unread = 1 AND m.location = 1 AND m.receiver = u.uid
116             GROUP BY m.receiver
117             HAVING COUNT(m.id) <> user_unread_pms';
118             $result = $db->sql_query($sql);
119             if ( $row = $db->sql_fetchrow($result) ) {
120                 do {
121                     $user_uid[$row['user_unread_pms']][] = $row['receiver'];
122                 }
123                 while ( $row = $db->sql_fetchrow($result) );
124
125                 foreach ( $user_uid AS $pms_count => $ary ) {
126                     $sql = 'UPDATE ' . USERS_TABLE . ' SET user_unread_pms = ' . $pms_count . ' WHERE ' . $db->sql_in_set('uid'$ary);
127                     $db->sql_query($sql);
128                 }
129
130                 write_log('docleanup - ' . sizeof($user_uid) . ' user PMs count updated', LOG_VIEW_SYSOP);
131             }
132         break;
133
134         case 5:
135             $user_limit_ary_torrents = unserialize($config['user_limit_ary_torrents']);
136             $user_limit_ary_allow_download = unserialize($config['user_limit_ary_allow_download']);
137             $users_class_arr = array();
138             $i = 0;
139             while ( get_user_class_name($i) !== '' ) {
140                 $can_leech = ( isset($user_limit_ary_allow_download[$i]) ? 1 : 0 );
141                 $sql = 'UPDATE ' . USERS_TABLE . ' SET torrents_limit = ' . $user_limit_ary_torrents[$i] . ', can_leech = ' . $can_leech . ' WHERE class = ' . $i;
142                 $db->sql_query($sql);
143                 ++$i;
144                 write_log('docleanup - ' . $db->sql_affectedrows() . ' users torrent limits updated', LOG_VIEW_SYSOP);
145             }
146
147             $sql = 'UPDATE ' . USERS_TABLE . ' SET torrents_limit = ' . $user_limit_ary_torrents['D'] . ' WHERE donor = 1';
148             $db->sql_query($sql);
149             write_log('docleanup - ' . $db->sql_affectedrows() . ' users torrent limits updated', LOG_VIEW_SYSOP);
150
151             $sql = 'UPDATE ' . USERS_TABLE . ' SET torrents_limit = ' . $user_limit_ary_torrents['W'] . ' WHERE warneduntil > 0';
152             $db->sql_query($sql);
153             write_log('docleanup - ' . $db->sql_affectedrows() . ' users torrent limits updated', LOG_VIEW_SYSOP);
154         break;
155
156         case 6:
157             $sql = 'SELECT t.fid, u.uid FROM ' . TORRENTS_TABLE . ' t LEFT JOIN ' . USERS_TABLE . ' u ON ( t.owner = u.uid ) WHERE u.uid IS NULL';
158             $result = $db->sql_query($sql);
159             if ( $row = $db->sql_fetchrow($result) ) {
160                 $i = 0;
161                 do {
162                     $sql = 'UPDATE ' . TORRENTS_TABLE . ' SET owner = ' . ANONYMOUS . ' WHERE fid = ' . $row['fid'];
163                     $db->sql_query($sql);
164
165                     ++$i;
166                 }
167                 while ( $row = $db->sql_fetchrow($result) );
168
169                 write_log('docleanup - ' . $i . ' torrent owner fixed', LOG_VIEW_SYSOP);
170             }
171         break;
172
173         default:
174             trigger_error('all is ok');
175         break;
176
177     }
178
179     meta_refresh(10, append_sid($root_path . 'docleanup.php?step=' . ($step + 1) ));
180     trigger_error('Wait 10 seconds');
181 }
182 ?>
Note: See TracBrowser for help on using the browser.