· 8 years ago · Dec 21, 2017, 09:18 PM
1<?php
2
3/* Hooks */
4
5$plugins->add_hook("index_end", "hax_rooms_list_rooms");
6$plugins->add_hook("global_start", "hax_rooms_get_templates");
7
8$plugins->add_hook('xmlhttp', 'hax_rooms_xmlhttp');
9
10function hax_rooms_xmlhttp()
11{
12 global $mybb, $db;
13
14 if($mybb->input['action'] == "hax_get_room_info")
15 {
16 $rooms = array();
17
18 $rid_from_sb = array();
19 if(count($_GET['rid']))
20 foreach ($_GET['rid'] as $room_id)
21 if(strlen($room_id) == 65)
22 {
23 $rid_from_sb[] = $room_id;
24 $rooms[$room_id] = array('rid' => $room_id);
25 }
26
27 $query = $db->query("
28 SELECT rc.id, rc.rid, rc.name, rc.players, rc.players_max, rc.online, rc.borndate, rc.updatedate
29 FROM ".TABLE_PREFIX . "hax_rooms_cache rc
30 WHERE rc.online=1".(count($rid_from_sb) ? ' OR rc.rid IN("'.implode('","', $rid_from_sb).'")' : '')."
31 ORDER BY borndate DESC
32 LIMIT 10
33 ");
34 while($room = $db->fetch_array($query))
35 {
36 $rooms[$room['rid']] = array(
37 'id' => $room['id'],
38 'rid' => $room['rid'],
39 'name' => $room['name'],
40 'players' => $room['players'],
41 'players_max' => $room['players_max'],
42 'online' => $room['online'],
43 'borndate' => $room['borndate'],
44 'updatedate' => $room['updatedate'],
45 'relatedate' => my_date("relative", $room['borndate'])
46 );
47 }
48
49 foreach ($rooms as $rid => $room_info)
50 if(isset($room_info['online']) && !$room_info['online'])
51 unset($rooms[$rid]);
52
53 $should_update = 0;
54 foreach ($rooms as $rid => $room_info)
55 {
56 if(!isset($room_info['updatedate']) || time() - $room_info['updatedate'] > 15)
57 {
58 $should_update = 1;
59 break;
60 }
61 }
62
63 $lock_filename = 'hax_rooms_cache.lock';
64 if($should_update && (!file_exists($lock_filename) || time() - filemtime($lock_filename) > 10))
65 {
66 touch($lock_filename);
67
68 $list = file_get_contents('http://www.haxball.com/list3');
69 $list = gzuncompress($list);
70 $len = implode('', unpack('n*', $list));
71 $bytes = unpack('c*', $list);
72
73 foreach ($rooms as $rid => $room_info)
74 {
75 $room_id = $room_info['rid'];
76 $room_id_proper_length = 65;
77 if(strlen($room_id) == $room_id_proper_length)
78 foreach($bytes as $key=>$byte)
79 {
80 $pin = 0;
81 for($i = 0; $i < $room_id_proper_length; $i++)
82 if(chr($bytes[$key + $i]) != $room_id[$i])
83 {
84 $pin = 1;
85 break;
86 }
87 if(!$pin)
88 {
89 $room_name = '';
90 for($i = 2; $i < 50; $i++)
91 {
92 if(
93 $bytes[$key + $room_id_proper_length + $i + 3] == 0 &&
94 $bytes[$key + $room_id_proper_length + $i + 4] == 2
95 )
96 {
97 $players = $bytes[$key + $room_id_proper_length + $i];
98 $players_max = $bytes[$key + $room_id_proper_length + $i + 1];
99 $pass = $bytes[$key + $room_id_proper_length + $i + 2];
100 break;
101 }
102 $room_name .= chr($bytes[$key + $room_id_proper_length + $i]);
103 }
104 $rooms[$rid]['name'] = $room_name;
105 $rooms[$rid]['players'] = $players;
106 $rooms[$rid]['players_max'] = $players_max;
107 $rooms[$rid]['online'] = 1;
108 break;
109 }
110 $rooms[$rid]['online'] = 0;
111 }
112 }
113
114 foreach ($rooms as $rid => $room_info)
115 {
116 if($rooms[$rid]['id'])
117 $db->update_query('hax_rooms_cache', array(
118 'rid' => $db->escape_string($rooms[$rid]['rid']),
119 'name' => $db->escape_string($rooms[$rid]['name']),
120 'players' => $db->escape_string($rooms[$rid]['players']),
121 'players_max' => $db->escape_string($rooms[$rid]['players_max']),
122 'online' => $db->escape_string($rooms[$rid]['online']),
123 'updatedate' => TIME_NOW,
124 ), 'id=' . (int)$rooms[$rid]['id']);
125 else
126 $db->insert_query('hax_rooms_cache', array(
127 'rid' => $db->escape_string($rooms[$rid]['rid']),
128 'name' => $db->escape_string($rooms[$rid]['name']),
129 'players' => $db->escape_string($rooms[$rid]['players']),
130 'players_max' => $db->escape_string($rooms[$rid]['players_max']),
131 'online' => $db->escape_string($rooms[$rid]['online']),
132 'borndate' => TIME_NOW,
133 'updatedate' => TIME_NOW,
134 ));
135 }
136 unlink($lock_filename);
137 }
138
139 echo json_encode($rooms);
140 }
141}
142
143function hax_rooms_info()
144{
145 return array(
146 "name" => "Hax Rooms",
147 "description" => "A plug-in that shows online rooms.",
148 "author" => "okon",
149 "version" => "1.0",
150 "codename" => "hax_rooms",
151 "compatibility" => "18*"
152 );
153}
154
155function hax_rooms_install()
156{
157 global $db, $mybb;
158
159 if($mybb->version_code < 1801)
160 {
161 flash_message("Sorry, but this plugin requires you to update to 1.8.1 or higher.", "error");
162 admin_redirect("index.php?module=config-plugins");
163 }
164
165 global $db;
166
167 $db->write_query("
168 CREATE TABLE IF NOT EXISTS " . TABLE_PREFIX . "hax_rooms_cache (
169 id int(11) NOT NULL auto_increment,
170 rid varchar(200) NOT NULL,
171 name varchar(200) NOT NULL,
172 players int(3) NOT NULL,
173 players_max int(3) NOT NULL,
174 online int(3) NOT NULL,
175 borndate int(11) NOT NULL,
176 updatedate int(11) NOT NULL,
177 PRIMARY KEY (id),
178 UNIQUE KEY rid_2 (rid),
179 KEY rid_3 (borndate)
180 ) ENGINE=InnoDB " . $db->build_create_table_collation() . "
181 ");
182
183}
184
185function hax_rooms_is_installed()
186{
187 global $db;
188 if($db->table_exists("hax_rooms_cache"))
189 return true;
190 return false;
191}
192
193function hax_rooms_activate()
194{
195 global $db;
196 $new_template['hax_rooms'] = '<div class="panel_head">Teraz gramy</div>
197 <div class="panel_body">
198 {$hax_rooms}
199 </div>';
200
201 $new_template['hax_rooms_room'] = '<div class="room_in_panel">
202 <div class="time_ago"><i class="fa fa-clock-o"></i>
203 {$hax_room_date}</div>
204 <a class="room_name" target="_blank" href="http://www.haxball.com/?roomid=~{$hax_room_id}">{$hax_room_name}</a>
205 <p>{$hax_room_players}/{$hax_room_players_max}</p>
206 </div>';
207
208 foreach($new_template as $title => $template)
209 {
210 $new_template = array('title' => $db->escape_string($title), 'template' => $db->escape_string($template), 'sid' => '-1', 'version' => '1800', 'dateline' => TIME_NOW);
211 $db->insert_query('templates', $new_template);
212 }
213
214 require_once MYBB_ROOT . "/inc/adminfunctions_templates.php";
215
216 find_replace_templatesets('index', "#" . preg_quote('{$forums}') . "#i", '{$forums}<div id="hax_rooms">{$hax_rooms}</div>');
217}
218
219function hax_rooms_deactivate()
220{
221 global $db;
222 $db->delete_query("templates", "title IN('hax_rooms','hax_rooms_room')");
223
224 require_once MYBB_ROOT . "/inc/adminfunctions_templates.php";
225
226 find_replace_templatesets('index', "#" . preg_quote('<div id="hax_rooms">{$hax_rooms}</div>') . "#i", '');
227}
228
229function hax_rooms_uninstall()
230{
231 global $db;
232 $db->drop_table("hax_rooms_cache");
233}
234
235function hax_rooms_list_rooms($return=false)
236{
237 global $mybb, $db, $templates, $hax_rooms, $hax_rooms;
238
239 $query = $db->query("
240 SELECT rc.id, rc.rid, rc.name, rc.players, rc.players_max, rc.online, rc.borndate, rc.updatedate
241 FROM ".TABLE_PREFIX . "hax_rooms_cache rc
242 WHERE online=1
243 ORDER BY rc.borndate DESC
244 LIMIT 10
245 ");
246 while($room = $db->fetch_array($query))
247 {
248 $hax_room_id = $room['rid'];
249 $hax_room_name = $room['name'];
250 $hax_room_date = my_date("relative", $room['borndate']);
251 $hax_room_players = $room['players'];
252 $hax_room_players_max = $room['players_max'];
253 eval("\$hax_rooms .= \"".$templates->get("hax_rooms_room")."\";");
254 }
255 if($db->num_rows($query))
256 eval("\$hax_rooms = \"".$templates->get("hax_rooms")."\";");
257}
258
259function hax_rooms_get_templates()
260{
261 global $templatelist;
262 if(THIS_SCRIPT == "index.php")
263 {
264 $templatelist .= ",hax_rooms,hax_rooms_room";
265 }
266}
267
268function hax_rooms_refresh_rooms()
269{
2701;
271}
272
273function hax_rooms_can_view()
274{
275 return TRUE;
276}
277
278?>