Conditions | 77 |
Paths | > 20000 |
Total Lines | 304 |
Code Lines | 183 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
265 | function determineActions($urls, $preferred_prefix = false) |
||
266 | { |
||
267 | global $txt, $user_info, $modSettings, $smcFunc, $scripturl, $context; |
||
268 | |||
269 | if (!allowedTo('who_view')) |
||
270 | return array(); |
||
271 | loadLanguage('Who'); |
||
272 | |||
273 | // Actions that require a specific permission level. |
||
274 | $allowedActions = array( |
||
275 | 'admin' => array('moderate_forum', 'manage_membergroups', 'manage_bans', 'admin_forum', 'manage_permissions', 'send_mail', 'manage_attachments', 'manage_smileys', 'manage_boards', 'edit_news'), |
||
276 | 'ban' => array('manage_bans'), |
||
277 | 'boardrecount' => array('admin_forum'), |
||
278 | 'calendar' => array('calendar_view'), |
||
279 | 'corefeatures' => array('admin_forum'), |
||
280 | 'editnews' => array('edit_news'), |
||
281 | 'featuresettings' => array('admin_forum'), |
||
282 | 'languages' => array('admin_forum'), |
||
283 | 'logs' => array('admin_forum'), |
||
284 | 'mailing' => array('send_mail'), |
||
285 | 'mailqueue' => array('admin_forum'), |
||
286 | 'maintain' => array('admin_forum'), |
||
287 | 'manageattachments' => array('manage_attachments'), |
||
288 | 'manageboards' => array('manage_boards'), |
||
289 | 'managecalendar' => array('admin_forum'), |
||
290 | 'managesearch' => array('admin_forum'), |
||
291 | 'managesmileys' => array('manage_smileys'), |
||
292 | 'membergroups' => array('manage_membergroups'), |
||
293 | 'mlist' => array('view_mlist'), |
||
294 | 'moderate' => array('access_mod_center', 'moderate_forum', 'manage_membergroups'), |
||
295 | 'modsettings' => array('admin_forum'), |
||
296 | 'news' => array('edit_news', 'send_mail', 'admin_forum'), |
||
297 | 'optimizetables' => array('admin_forum'), |
||
298 | 'packages' => array('admin_forum'), |
||
299 | 'paidsubscribe' => array('admin_forum'), |
||
300 | 'permissions' => array('manage_permissions'), |
||
301 | 'postsettings' => array('admin_forum'), |
||
302 | 'regcenter' => array('admin_forum', 'moderate_forum'), |
||
303 | 'repairboards' => array('admin_forum'), |
||
304 | 'reports' => array('admin_forum'), |
||
305 | 'scheduledtasks' => array('admin_forum'), |
||
306 | 'search' => array('search_posts'), |
||
307 | 'search2' => array('search_posts'), |
||
308 | 'securitysettings' => array('admin_forum'), |
||
309 | 'sengines' => array('admin_forum'), |
||
310 | 'serversettings' => array('admin_forum'), |
||
311 | 'setcensor' => array('moderate_forum'), |
||
312 | 'setreserve' => array('moderate_forum'), |
||
313 | 'stats' => array('view_stats'), |
||
314 | 'theme' => array('admin_forum'), |
||
315 | 'viewerrorlog' => array('admin_forum'), |
||
316 | 'viewmembers' => array('moderate_forum'), |
||
317 | ); |
||
318 | call_integration_hook('who_allowed', array(&$allowedActions)); |
||
319 | |||
320 | if (!is_array($urls)) |
||
321 | $url_list = array(array($urls, $user_info['id'])); |
||
322 | else |
||
323 | $url_list = $urls; |
||
324 | |||
325 | // These are done to later query these in large chunks. (instead of one by one.) |
||
326 | $topic_ids = array(); |
||
327 | $profile_ids = array(); |
||
328 | $board_ids = array(); |
||
329 | |||
330 | $data = array(); |
||
331 | foreach ($url_list as $k => $url) |
||
332 | { |
||
333 | // Get the request parameters.. |
||
334 | $actions = $smcFunc['json_decode']($url[0], true); |
||
335 | if ($actions === array()) |
||
336 | continue; |
||
337 | |||
338 | // If it's the admin or moderation center, and there is an area set, use that instead. |
||
339 | if (isset($actions['action']) && ($actions['action'] == 'admin' || $actions['action'] == 'moderate') && isset($actions['area'])) |
||
340 | $actions['action'] = $actions['area']; |
||
341 | |||
342 | // Check if there was no action or the action is display. |
||
343 | if (!isset($actions['action']) || $actions['action'] == 'display') |
||
344 | { |
||
345 | // It's a topic! Must be! |
||
346 | if (isset($actions['topic'])) |
||
347 | { |
||
348 | // Assume they can't view it, and queue it up for later. |
||
349 | $data[$k] = array('label' => 'who_hidden', 'class' => 'em'); |
||
350 | $topic_ids[(int) $actions['topic']][$k] = $txt['who_topic']; |
||
351 | } |
||
352 | // It's a board! |
||
353 | elseif (isset($actions['board'])) |
||
354 | { |
||
355 | // Hide first, show later. |
||
356 | $data[$k] = array('label' => 'who_hidden', 'class' => 'em'); |
||
357 | $board_ids[$actions['board']][$k] = $txt['who_board']; |
||
358 | } |
||
359 | // It's the board index!! It must be! |
||
360 | else |
||
361 | $data[$k] = sprintf($txt['who_index'], $scripturl, $context['forum_name_html_safe']); |
||
362 | } |
||
363 | // Probably an error or some goon? |
||
364 | elseif ($actions['action'] == '') |
||
365 | $data[$k] = sprintf($txt['who_index'], $scripturl, $context['forum_name_html_safe']); |
||
366 | // Some other normal action...? |
||
367 | else |
||
368 | { |
||
369 | // Viewing/editing a profile. |
||
370 | if ($actions['action'] == 'profile') |
||
371 | { |
||
372 | // Whose? Their own? |
||
373 | if (empty($actions['u'])) |
||
374 | $actions['u'] = $url[1]; |
||
375 | |||
376 | $data[$k] = array('label' => 'who_hidden', 'class' => 'em'); |
||
377 | $profile_ids[(int) $actions['u']][$k] = $actions['u'] == $url[1] ? $txt['who_viewownprofile'] : $txt['who_viewprofile']; |
||
378 | } |
||
379 | elseif (($actions['action'] == 'post' || $actions['action'] == 'post2') && empty($actions['topic']) && isset($actions['board'])) |
||
380 | { |
||
381 | $data[$k] = array('label' => 'who_hidden', 'class' => 'em'); |
||
382 | $board_ids[(int) $actions['board']][$k] = isset($actions['poll']) ? $txt['who_poll'] : $txt['who_post']; |
||
383 | } |
||
384 | // A subaction anyone can view... if the language string is there, show it. |
||
385 | elseif (isset($actions['sa']) && isset($txt['whoall_' . $actions['action'] . '_' . $actions['sa']])) |
||
386 | $data[$k] = $preferred_prefix && isset($txt[$preferred_prefix . $actions['action'] . '_' . $actions['sa']]) ? $txt[$preferred_prefix . $actions['action'] . '_' . $actions['sa']] : sprintf($txt['whoall_' . $actions['action'] . '_' . $actions['sa']], $scripturl); |
||
|
|||
387 | // An action any old fellow can look at. (if ['whoall_' . $action] exists, we know everyone can see it.) |
||
388 | elseif (isset($txt['whoall_' . $actions['action']])) |
||
389 | $data[$k] = $preferred_prefix && isset($txt[$preferred_prefix . $actions['action']]) ? $txt[$preferred_prefix . $actions['action']] : sprintf($txt['whoall_' . $actions['action']], $scripturl); |
||
390 | // Viewable if and only if they can see the board... |
||
391 | elseif (isset($txt['whotopic_' . $actions['action']])) |
||
392 | { |
||
393 | // Find out what topic they are accessing. |
||
394 | $topic = (int) (isset($actions['topic']) ? $actions['topic'] : (isset($actions['from']) ? $actions['from'] : 0)); |
||
395 | |||
396 | $data[$k] = array('label' => 'who_hidden', 'class' => 'em'); |
||
397 | $topic_ids[$topic][$k] = $txt['whotopic_' . $actions['action']]; |
||
398 | } |
||
399 | elseif (isset($txt['whopost_' . $actions['action']])) |
||
400 | { |
||
401 | // Find out what message they are accessing. |
||
402 | $msgid = (int) (isset($actions['msg']) ? $actions['msg'] : (isset($actions['quote']) ? $actions['quote'] : 0)); |
||
403 | |||
404 | $result = $smcFunc['db_query']('', ' |
||
405 | SELECT m.id_topic, m.subject |
||
406 | FROM {db_prefix}messages AS m |
||
407 | ' . ($modSettings['postmod_active'] ? 'INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic AND t.approved = {int:is_approved})' : '') . ' |
||
408 | WHERE m.id_msg = {int:id_msg} |
||
409 | AND {query_see_message_board}' . ($modSettings['postmod_active'] ? ' |
||
410 | AND m.approved = {int:is_approved}' : '') . ' |
||
411 | LIMIT 1', |
||
412 | array( |
||
413 | 'is_approved' => 1, |
||
414 | 'id_msg' => $msgid, |
||
415 | ) |
||
416 | ); |
||
417 | list ($id_topic, $subject) = $smcFunc['db_fetch_row']($result); |
||
418 | $data[$k] = sprintf($txt['whopost_' . $actions['action']], $id_topic, $subject, $scripturl); |
||
419 | $smcFunc['db_free_result']($result); |
||
420 | |||
421 | if (empty($id_topic)) |
||
422 | $data[$k] = array('label' => 'who_hidden', 'class' => 'em'); |
||
423 | } |
||
424 | // Viewable only by administrators.. (if it starts with whoadmin, it's admin only!) |
||
425 | elseif (allowedTo('moderate_forum') && isset($txt['whoadmin_' . $actions['action']])) |
||
426 | $data[$k] = sprintf($txt['whoadmin_' . $actions['action']], $scripturl); |
||
427 | // Viewable by permission level. |
||
428 | elseif (isset($allowedActions[$actions['action']])) |
||
429 | { |
||
430 | if (allowedTo($allowedActions[$actions['action']]) && !empty($txt['whoallow_' . $actions['action']])) |
||
431 | $data[$k] = sprintf($txt['whoallow_' . $actions['action']], $scripturl); |
||
432 | elseif (in_array('moderate_forum', $allowedActions[$actions['action']])) |
||
433 | $data[$k] = $txt['who_moderate']; |
||
434 | elseif (in_array('admin_forum', $allowedActions[$actions['action']])) |
||
435 | $data[$k] = $txt['who_admin']; |
||
436 | else |
||
437 | $data[$k] = array('label' => 'who_hidden', 'class' => 'em'); |
||
438 | } |
||
439 | elseif (!empty($actions['action'])) |
||
440 | $data[$k] = $txt['who_generic'] . ' ' . $actions['action']; |
||
441 | else |
||
442 | $data[$k] = array('label' => 'who_unknown', 'class' => 'em'); |
||
443 | } |
||
444 | |||
445 | if (isset($actions['error'])) |
||
446 | { |
||
447 | loadLanguage('Errors'); |
||
448 | |||
449 | if (isset($txt[$actions['error']])) |
||
450 | $error_message = str_replace('"', '"', empty($actions['error_params']) ? $txt[$actions['error']] : vsprintf($txt[$actions['error']], (array) $actions['error_params'])); |
||
451 | elseif ($actions['error'] == 'guest_login') |
||
452 | $error_message = str_replace('"', '"', $txt['who_guest_login']); |
||
453 | else |
||
454 | $error_message = str_replace('"', '"', $actions['error']); |
||
455 | |||
456 | if (!empty($error_message)) |
||
457 | { |
||
458 | $error_message = ' <span class="main_icons error" title="' . $error_message . '"></span>'; |
||
459 | |||
460 | if (is_array($data[$k])) |
||
461 | $data[$k]['error_message'] = $error_message; |
||
462 | else |
||
463 | $data[$k] .= $error_message; |
||
464 | } |
||
465 | } |
||
466 | |||
467 | // Maybe the action is integrated into another system? |
||
468 | if (count($integrate_actions = call_integration_hook('integrate_whos_online', array($actions))) > 0) |
||
469 | { |
||
470 | foreach ($integrate_actions as $integrate_action) |
||
471 | { |
||
472 | if (!empty($integrate_action)) |
||
473 | { |
||
474 | $data[$k] = $integrate_action; |
||
475 | if (isset($actions['topic']) && isset($topic_ids[(int) $actions['topic']][$k])) |
||
476 | $topic_ids[(int) $actions['topic']][$k] = $integrate_action; |
||
477 | if (isset($actions['board']) && isset($board_ids[(int) $actions['board']][$k])) |
||
478 | $board_ids[(int) $actions['board']][$k] = $integrate_action; |
||
479 | if (isset($actions['u']) && isset($profile_ids[(int) $actions['u']][$k])) |
||
480 | $profile_ids[(int) $actions['u']][$k] = $integrate_action; |
||
481 | break; |
||
482 | } |
||
483 | } |
||
484 | } |
||
485 | } |
||
486 | |||
487 | // Load topic names. |
||
488 | if (!empty($topic_ids)) |
||
489 | { |
||
490 | $result = $smcFunc['db_query']('', ' |
||
491 | SELECT t.id_topic, m.subject |
||
492 | FROM {db_prefix}topics AS t |
||
493 | INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) |
||
494 | WHERE {query_see_topic_board} |
||
495 | AND t.id_topic IN ({array_int:topic_list})' . ($modSettings['postmod_active'] ? ' |
||
496 | AND t.approved = {int:is_approved}' : '') . ' |
||
497 | LIMIT {int:limit}', |
||
498 | array( |
||
499 | 'topic_list' => array_keys($topic_ids), |
||
500 | 'is_approved' => 1, |
||
501 | 'limit' => count($topic_ids), |
||
502 | ) |
||
503 | ); |
||
504 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
505 | { |
||
506 | // Show the topic's subject for each of the actions. |
||
507 | foreach ($topic_ids[$row['id_topic']] as $k => $session_text) |
||
508 | $data[$k] = sprintf($session_text, $row['id_topic'], censorText($row['subject']), $scripturl); |
||
509 | } |
||
510 | $smcFunc['db_free_result']($result); |
||
511 | } |
||
512 | |||
513 | // Load board names. |
||
514 | if (!empty($board_ids)) |
||
515 | { |
||
516 | $result = $smcFunc['db_query']('', ' |
||
517 | SELECT b.id_board, b.name |
||
518 | FROM {db_prefix}boards AS b |
||
519 | WHERE {query_see_board} |
||
520 | AND b.id_board IN ({array_int:board_list}) |
||
521 | LIMIT {int:limit}', |
||
522 | array( |
||
523 | 'board_list' => array_keys($board_ids), |
||
524 | 'limit' => count($board_ids), |
||
525 | ) |
||
526 | ); |
||
527 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
528 | { |
||
529 | // Put the board name into the string for each member... |
||
530 | foreach ($board_ids[$row['id_board']] as $k => $session_text) |
||
531 | $data[$k] = sprintf($session_text, $row['id_board'], $row['name'], $scripturl); |
||
532 | } |
||
533 | $smcFunc['db_free_result']($result); |
||
534 | } |
||
535 | |||
536 | // Load member names for the profile. (is_not_guest permission for viewing their own profile) |
||
537 | $allow_view_own = allowedTo('is_not_guest'); |
||
538 | $allow_view_any = allowedTo('profile_view'); |
||
539 | if (!empty($profile_ids) && ($allow_view_any || $allow_view_own)) |
||
540 | { |
||
541 | $result = $smcFunc['db_query']('', ' |
||
542 | SELECT id_member, real_name |
||
543 | FROM {db_prefix}members |
||
544 | WHERE id_member IN ({array_int:member_list}) |
||
545 | LIMIT ' . count($profile_ids), |
||
546 | array( |
||
547 | 'member_list' => array_keys($profile_ids), |
||
548 | ) |
||
549 | ); |
||
550 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
551 | { |
||
552 | // If they aren't allowed to view this person's profile, skip it. |
||
553 | if (!$allow_view_any && ($user_info['id'] != $row['id_member'])) |
||
554 | continue; |
||
555 | |||
556 | // Set their action on each - session/text to sprintf. |
||
557 | foreach ($profile_ids[$row['id_member']] as $k => $session_text) |
||
558 | $data[$k] = sprintf($session_text, $row['id_member'], $row['real_name'], $scripturl); |
||
559 | } |
||
560 | $smcFunc['db_free_result']($result); |
||
561 | } |
||
562 | |||
563 | call_integration_hook('whos_online_after', array(&$urls, &$data)); |
||
564 | |||
565 | if (!is_array($urls)) |
||
566 | return isset($data[0]) ? $data[0] : false; |
||
567 | else |
||
568 | return $data; |
||
569 | } |
||
924 | ?> |