/
flpavatar.php
474 lines (388 loc) · 12.9 KB
/
flpavatar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
/*
+---------------------------------------------------------------------------
| First and Last Post Avatar
| =============================================
| by Tom Moore (www.xekko.co.uk)
| Copyright 2012 Mooseypx Design / Xekko
| =============================================
+---------------------------------------------------------------------------
*/
if(!defined('IN_MYBB'))
{
die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}
// Set this to the dimensions of your default avatar
define('DEF_FP_SIZE', '44|44');
// Set this if you want to override the maximum post size, not really needed
//define('MAX_FP_SIZE', '100x100');
global $mybb;
function flpavatar_info()
{
return array(
'name' => 'Last/First Post Avatar',
'description' => 'A plugin that allows the first and last post avatar around the forum.',
'website' => 'http://resources.xekko.co.uk',
'author' => 'Tomm',
'authorsite' => 'http://xekko.co.uk',
'version' => '1.0.2',
'guid' => '9fbd88aaa25448e1bd26ca5b523a0dcd',
'compatibility' => '*'
);
}
if(defined('IN_ADMINCP'))
{
require_once MYBB_ROOT.'inc/plugins/flpavatar_acp.php';
}
else
{
$exp = explode(',', $mybb->settings['flp_permissions']);
$mybb->settings['flp_permissions'] = array('forumdisplay' => $exp[0], 'index' => $exp[1], 'pm' => $exp[2], 'search' => $exp[3], 'showthread' => $exp[4]);
$plugins->add_hook('build_forumbits_forum', 'flpavatar_forumlist');
$plugins->add_hook('forumdisplay_thread', 'flpavatar_threadlist');
$plugins->add_hook('postbit_announcement', 'flpavatar_thread');
$plugins->add_hook('announcements_end', 'flpavatar_end');
$plugins->add_hook('postbit', 'flpavatar_thread');
$plugins->add_hook('showthread_end', 'flpavatar_end');
// Search
if(THIS_SCRIPT == 'search.php' && $mybb->settings['flp_permissions']['search'])
{
define('IN_SEARCH', 1);
$plugins->add_hook('search_results_thread', 'flpavatar_threadlist');
$plugins->add_hook('search_results_post', 'flpavatar_threadlist');
}
// Maintenance
$plugins->add_hook('forumdisplay_announcement', 'flpavatar_anno');
$plugins->add_hook('usercp_do_avatar_end', 'flpavatar_avatar_update');
if(THIS_SCRIPT == 'modcp.php' && in_array($mybb->input['action'], array('do_new_announcement', 'do_edit_announcement')) && $mybb->settings['flp_permissions']['forumdisplay'])
{
$plugins->add_hook('redirect', 'flpavatar_anno_update');
}
if(THIS_SCRIPT == 'private.php')
{
$plugins->add_hook('private_end', 'flpavatar_private_end');
$plugins->add_hook("private_results_end", "flpavatar_private_end");
$plugins->add_hook("private_tracking_end", "flpavatar_private_end");
}
}
// For forum list
function flpavatar_forumlist(&$_f)
{
global $cache, $db, $fcache, $mybb;
if(!$mybb->settings['flp_permissions']['index'])
{
return;
}
if(!isset($cache->cache['flp_cache']))
{
$cache->cache['flp_cache'] = array();
$flp_cache = $cache->read('flp_cache');
$forums = new RecursiveIteratorIterator(new RecursiveArrayIterator($fcache));
// This loop goes through each forum and finds the right lastposter
foreach($forums as $_forum)
{
$forum = $forums->getSubIterator();
if($forum['fid'])
{
$forum = iterator_to_array($forum);
$flp_cache[$forum['fid']] = $forum;
if($forum['parentlist'])
{
$flp_cache[$forum['fid']] = $forum;
$flp_cache[$forum['fid']]['avataruid'] = $forum['lastposteruid'];
$exp = array_reverse(explode(',', $forum['parentlist']));
foreach($exp as $parent)
{
if($parent == $forum['fid']) continue;
if(isset($flp_cache[$parent]) && $forum['lastpost'] > $flp_cache[$parent]['lastpost'])
{
$flp_cache[$parent]['lastpost'] = $forum['lastpost'];
$flp_cache[$parent]['avataruid'] = $forum['lastposteruid']; // Bubble up to replace parent lastpost
}
}
}
}
}
// This loop gathers lastpost users and sorts by user/forums
$users = array();
foreach($flp_cache as $forum)
{
if(isset($forum['avataruid']))
{
$users[$forum['avataruid']][] = $forum['fid'];
}
}
// Third loop; this retrieves above users' avatar info
if(!empty($users))
{
$sql = implode(',', array_keys($users));
$query = $db->simple_select('users', 'uid, username, username AS userusername, avatar, avatardimensions', "uid IN ({$sql})");
while($user = $db->fetch_array($query))
{
// Finally, assign avatars
$avatar = flp_format_avatar($user);
foreach($users[$user['uid']] as $fid)
{
$flp_cache[$fid]['flp_avatar'] = $avatar;
}
}
}
// Encore! Replace our inline cache
$cache->cache['flp_cache'] = $flp_cache;
}
$_f['flp_lastpost'] = $cache->cache['flp_cache'][$_f['fid']]['flp_avatar'];
}
// For thread list (in search and forumdisplay)
function flpavatar_threadlist()
{
global $db, $flp_avatar, $flp_firstpost, $flp_lastpost, $mybb, $post, $search, $thread, $threadcache, $thread_cache;
static $flp_cache, $flp_type;
if(!isset($flp_cache))
{
$users = $flp_cache = array();
$flp_type = (defined('IN_SEARCH')) ? 2 : 1;
$cache = ($thread_cache) ? $thread_cache : $threadcache;
if($flp_type == 1 && !$mybb->settings['flp_permissions']['forumdisplay'] || $flp_type == 2 && !$mybb->settings['flp_permissions']['search'])
{
$flp_cache = array();
return;
}
if(isset($cache))
{
// Handling threadlist or search results in threads
foreach($cache as $t)
{
if(!in_array($t['uid'], $users))
{
$users[] = "'".intval($t['uid'])."'"; // The original author of the thread
}
if(!in_array($t['lastposteruid'], $users))
{
$users[] = "'".intval($t['lastposteruid'])."'"; // The lastposter (if they aren't the original author)
}
}
if(!empty($users))
{
$sql = implode(',', $users);
$query = $db->simple_select('users', 'uid, username, username AS userusername, avatar, avatardimensions', "uid IN ({$sql})");
while($user = $db->fetch_array($query))
{
$flp_cache[$user['uid']] = flp_format_avatar($user);
}
}
}
elseif(isset($post) && isset($search))
{
// Handling search results in posts
$flp_type = 3;
$query = $db->query("
SELECT u.uid, u.username, u.username as userusername, u.avatar, u.avatardimensions
FROM ".TABLE_PREFIX."posts p
LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid = p.uid)
WHERE p.pid IN ({$search['posts']})
");
while($user = $db->fetch_array($query))
{
if(!isset($flp_cache[$user['uid']]))
{
$flp_cache[$user['uid']] = flp_format_avatar($user);
}
}
}
}
if(empty($flp_cache))
{
return; // There are no users with avatars
}
$uid = ($post['uid']) ? $post['uid'] : $thread['uid']; // Always have an author
if(isset($flp_cache[$uid]))
{
$flp_avatar = $flp_cache[$uid];
}
if(isset($flp_cache[$thread['lastposteruid']]))
{
$flp_lastpost = $flp_cache[$thread['lastposteruid']]; // Specific for lastposters
}
}
// For threads
function flpavatar_thread(&$post)
{
global $flp_avatar, $mybb, $thread;
if(isset($flp_avatar) || $thread['firstpost'] != $post['pid'] || !$mybb->settings['flp_permissions']['showthread'])
{
return; // This is not the post you are looking for...
}
$flp_avatar = flp_format_avatar($post);
}
function flpavatar_end()
{
global $db, $flp_avatar, $mybb, $thread;
if(!$mybb->settings['flp_permissions']['showthread'])
{
return;
}
// Fallback if the firstposter isn't on the thread page
if(!isset($flp_avatar) || !is_array($flp_avatar))
{
$uid = intval($thread['uid']);
$query = $db->simple_select('users', 'uid, username, username AS userusername, avatar, avatardimensions', "uid = '{$uid}'");
$user = $db->fetch_array($query);
$flp_avatar = flp_format_avatar($user);
}
}
// Upkeep of user's avatar
function flpavatar_avatar_update()
{
global $cache, $db, $extra_user_updates, $mybb, $updated_avatar, $user;
$user = ($user) ? $user : $mybb->user;
$inline_avatars = $cache->read('inline_avatars');
if(!$inline_avatars[$user['uid']])
{
return; // No need to keep this inline as we'll never use it
}
$update = ($extra_user_updates) ? $extra_user_updates : $updated_avatar;
if(is_array($update))
{
$user = array_merge($user, $update);
$inline_avatars[$user['uid']] = flp_format_avatar($user);
$cache->update('inline_avatars', $inline_avatars);
}
}
// Detection for announcements
function flpavatar_anno()
{
global $announcement, $cache, $flp_avatar, $mybb;
if(!$mybb->settings['flp_permissions']['forumdisplay'])
{
return;
}
$inline_avatars = $cache->read('inline_avatars');
if($inline_avatars[$announcement['uid']])
{
$flp_avatar = array(
'avatar' => $inline_avatars[$announcement['uid']]['avatar'],
'dimensions' => $inline_avatars[$announcement['uid']]['dimensions'],
'username' => $announcement['username'],
'profile' => $inline_avatars[$announcement['uid']]['profile']
);
}
}
function flpavatar_anno_update($args)
{
global $cache, $db, $insert_announcement, $mybb, $update_announcement;
$inline_avatars = $cache->read('inline_avatars');
$anno = ($update_announcement) ? $update_announcement : $insert_announcement;
if(is_array($inline_avatars) && $inline_avatars[$anno['uid']])
{
return; // No need to re-cache
}
if($anno['uid'] == $mybb->user['uid'])
{
$inline_avatars[$anno['uid']] = flp_format_avatar($mybb->user);
}
else
{
$query = $db->simple_select('users', 'uid, username, username AS userusername, avatar, avatardimensions', "uid = '{$anno['uid']}'");
$user = $db->fetch_array($query);
$inline_avatars[$user['uid']] = flp_format_avatar($user);
}
$cache->update('inline_avatars', $inline_avatars);
}
// For private messages and tracking
function flpavatar_private_end()
{
global $db, $messagelist, $mybb, $unreadmessages, $readmessages;
if(!$mybb->settings['flp_permissions']['pm'])
{
return;
}
$users = array();
foreach(array($messagelist, $unreadmessages, $readmessages) as $content)
{
if(!$content) continue;
preg_match_all('#<flp_avatar\[([0-9]+)\]#', $content, $matches);
if(is_array($matches[1]) && !empty($matches[1]))
{
foreach($matches[1] as $user)
{
if(!intval($user)) continue;
$users[] = intval($user);
}
}
}
if(!empty($users))
{
$sql = implode(',', $users);
$query = $db->simple_select('users', 'uid, username, username AS userusername, avatar, avatardimensions', "uid IN ({$sql})");
$find = $replace = array();
while($user = $db->fetch_array($query))
{
$parameters = flp_format_avatar($user);
foreach($parameters as $piece => $cake)
{
$find[] = "<flp_avatar[{$user['uid']}]['{$piece}']>";
$replace[] = $cake;
}
}
if(isset($messagelist)) $messagelist = str_replace($find, $replace, $messagelist);
if(isset($readmessages)) $readmessages = str_replace($find, $replace, $readmessages);
if(isset($unreadmessages)) $unreadmessages = str_replace($find, $replace, $unreadmessages);
}
}
function flp_format_avatar($user)
{
global $mybb;
if($mybb->version_code >= 1700)
{
// 1.8 has a slightly different syntax
$dimensions = ($user['avatar']) ? $user['avatardimensions'] : (defined('DEF_FP_SIZE')) ? DEF_FP_SIZE : '44|44';
$size = (defined('MAX_FP_SIZE')) ? MAX_FP_SIZE : $mybb->settings['postmaxavatarsize'];
$avatar = format_avatar($user['avatar'], $dimensions, $size);
return array(
'avatar' => $avatar['image'],
'dimensions' => $avatar['width_height'],
'username' => $user['username'],
'profile' => get_profile_link($user['uid'])
);
}
return format_avatar($user);
}
// format_avatar is a 1.8 function; create it if our party doesn't have >= 1.7 (LOSERS, hah).
if(!function_exists('format_avatar'))
{
function format_avatar($user)
{
global $mybb;
static $users;
if(!isset($users))
{
$users = array();
}
if(isset($users[$user['uid']]))
{
return $users[$user['uid']];
}
$size = (defined('MAX_FP_SIZE')) ? MAX_FP_SIZE : $mybb->settings['postmaxavatarsize'];
$avatar = ($user['avatar']) ? htmlspecialchars_uni($user['avatar']) : $mybb->settings['bburl'].'/images/default_avatar.gif';
$avatar_width_height = '';
$dimensions = explode('|', ($user['avatar']) ? $user['avatardimensions'] : (defined('DEF_FP_SIZE')) ? DEF_FP_SIZE : '44|44');
if(is_array($dimensions) && $dimensions[0] && $dimensions[1])
{
$avatar_width_height = " width='{$dimensions[0]}' height='{$dimensions[1]}'";
list($max_width, $max_height) = explode('x', $size);
if($dimensions[0] > $max_width || $dimensions[1] > $max_height)
{
require_once MYBB_ROOT.'inc/functions_image.php';
$scaled_dimensions = scale_image($dimensions[0], $dimensions[1], $max_width, $max_height);
$avatar_width_height = " width='{$scaled_dimensions['width']}' height='{$scaled_dimensions['height']}'";
}
}
$users[$user['uid']] = array(
'avatar' => $avatar,
'dimensions' => $avatar_width_height,
'username' => htmlspecialchars_uni($user['userusername']),
'profile' => get_profile_link($user['uid']),
);
return $users[$user['uid']];
}
}