/**
 * MyBB 1.8
 * Copyright 2014 MyBB Group, All Rights Reserved
 *
 * Website: http://www.mybb.com
 * License: http://www.mybb.com/about/license
 *
 */
function task_delayedmoderation($task)
{
    global $db, $lang, $plugins;
    require_once MYBB_ROOT . "inc/class_moderation.php";
    $moderation = new Moderation();
    require_once MYBB_ROOT . "inc/class_custommoderation.php";
    $custommod = new CustomModeration();
    // Iterate through all our delayed moderation actions
    $query = $db->simple_select("delayedmoderation", "*", "delaydateline <= '" . TIME_NOW . "'");
    while ($delayedmoderation = $db->fetch_array($query)) {
        if (is_object($plugins)) {
            $args = array('task' => &$task, 'delayedmoderation' => &$delayedmoderation);
            $plugins->run_hooks('task_delayedmoderation', $args);
        }
        $tids = explode(',', $delayedmoderation['tids']);
        $input = my_unserialize($delayedmoderation['inputs']);
        if (my_strpos($delayedmoderation['type'], "modtool") !== false) {
            list(, $custom_id) = explode('_', $delayedmoderation['type'], 2);
            $custommod->execute($custom_id, $tids);
        } else {
            switch ($delayedmoderation['type']) {
                case "openclosethread":
                    $closed_tids = $open_tids = array();
                    $query2 = $db->simple_select("threads", "tid,closed", "tid IN({$delayedmoderation['tids']})");
                    while ($thread = $db->fetch_array($query2)) {
                        if ($thread['closed'] == 1) {
                            $closed_tids[] = $thread['tid'];
                        } else {
                            $open_tids[] = $thread['tid'];
                        }
                    }
                    if (!empty($closed_tids)) {
                        $moderation->open_threads($closed_tids);
                    }
                    if (!empty($open_tids)) {
                        $moderation->close_threads($open_tids);
                    }
                    break;
                case "deletethread":
                    foreach ($tids as $tid) {
                        $moderation->delete_thread($tid);
                    }
                    break;
                case "move":
                    foreach ($tids as $tid) {
                        $moderation->move_thread($tid, $input['new_forum']);
                    }
                    break;
                case "stick":
                    $unstuck_tids = $stuck_tids = array();
                    $query2 = $db->simple_select("threads", "tid,sticky", "tid IN({$delayedmoderation['tids']})");
                    while ($thread = $db->fetch_array($query2)) {
                        if ($thread['sticky'] == 1) {
                            $stuck_tids[] = $thread['tid'];
                        } else {
                            $unstuck_tids[] = $thread['tid'];
                        }
                    }
                    if (!empty($stuck_tids)) {
                        $moderation->unstick_threads($stuck_tids);
                    }
                    if (!empty($unstuck_tids)) {
                        $moderation->stick_threads($unstuck_tids);
                    }
                    break;
                case "merge":
                    // $delayedmoderation['tids'] should be a single tid
                    if (count($tids) != 1) {
                        continue;
                    }
                    // explode at # sign in a url (indicates a name reference) and reassign to the url
                    $realurl = explode("#", $input['threadurl']);
                    $input['threadurl'] = $realurl[0];
                    // Are we using an SEO URL?
                    if (substr($input['threadurl'], -4) == "html") {
                        // Get thread to merge's tid the SEO way
                        preg_match("#thread-([0-9]+)?#i", $input['threadurl'], $threadmatch);
                        preg_match("#post-([0-9]+)?#i", $input['threadurl'], $postmatch);
                        if ($threadmatch[1]) {
                            $parameters['tid'] = $threadmatch[1];
                        }
                        if ($postmatch[1]) {
                            $parameters['pid'] = $postmatch[1];
                        }
                    } else {
                        // Get thread to merge's tid the normal way
                        $splitloc = explode(".php", $input['threadurl']);
                        $temp = explode("&", my_substr($splitloc[1], 1));
                        if (!empty($temp)) {
                            for ($i = 0; $i < count($temp); $i++) {
                                $temp2 = explode("=", $temp[$i], 2);
                                $parameters[$temp2[0]] = $temp2[1];
                            }
                        } else {
                            $temp2 = explode("=", $splitloc[1], 2);
                            $parameters[$temp2[0]] = $temp2[1];
                        }
                    }
                    if ($parameters['pid'] && !$parameters['tid']) {
                        $post = get_post($parameters['pid']);
                        $mergetid = $post['tid'];
                    } else {
                        if ($parameters['tid']) {
                            $mergetid = $parameters['tid'];
                        }
                    }
                    $mergetid = (int) $mergetid;
                    $mergethread = get_thread($mergetid);
                    if (!$mergethread['tid']) {
                        continue;
                    }
                    if ($mergetid == $delayedmoderation['tids']) {
                        // sanity check
                        continue;
                    }
                    if ($input['subject']) {
                        $subject = $input['subject'];
                    } else {
                        $query = $db->simple_select("threads", "subject", "tid='{$delayedmoderation['tids']}'");
                        $subject = $db->fetch_field($query, "subject");
                    }
                    $moderation->merge_threads($mergetid, $delayedmoderation['tids'], $subject);
                    break;
                case "removeredirects":
                    foreach ($tids as $tid) {
                        $moderation->remove_redirects($tid);
                    }
                    break;
                case "removesubscriptions":
                    $moderation->remove_thread_subscriptions($tids, true);
                    break;
                case "approveunapprovethread":
                    $approved_tids = $unapproved_tids = array();
                    $query2 = $db->simple_select("threads", "tid,visible", "tid IN({$delayedmoderation['tids']})");
                    while ($thread = $db->fetch_array($query2)) {
                        if ($thread['visible'] == 1) {
                            $approved_tids[] = $thread['tid'];
                        } else {
                            $unapproved_tids[] = $thread['tid'];
                        }
                    }
                    if (!empty($approved_tids)) {
                        $moderation->unapprove_threads($approved_tids);
                    }
                    if (!empty($unapproved_tids)) {
                        $moderation->approve_threads($unapproved_tids);
                    }
                    break;
                case "softdeleterestorethread":
                    $delete_tids = $restore_tids = array();
                    $query2 = $db->simple_select("threads", "tid,visible", "tid IN({$delayedmoderation['tids']})");
                    while ($thread = $db->fetch_array($query2)) {
                        if ($thread['visible'] == -1) {
                            $restore_tids[] = $thread['tid'];
                        } else {
                            $delete_tids[] = $thread['tid'];
                        }
                    }
                    if (!empty($restore_tids)) {
                        $moderation->restore_threads($restore_tids);
                    }
                    if (!empty($delete_tids)) {
                        $moderation->soft_delete_threads($delete_tids);
                    }
                    break;
            }
        }
        $db->delete_query("delayedmoderation", "did='{$delayedmoderation['did']}'");
    }
    add_task_log($task, $lang->task_delayedmoderation_ran);
}
 // Stick or unstick that post to the top bab!
 case "stick":
     // Verify incoming POST request
     verify_post_check($mybb->input['my_post_key']);
     if (!is_moderator($fid, "canmanagethreads")) {
         error_no_permission();
     }
     $plugins->run_hooks("moderation_stick");
     if ($thread['sticky'] == 1) {
         $stuckunstuck = $lang->unstuck;
         $redirect = $lang->redirect_unstickthread;
         $moderation->unstick_threads($tid);
     } else {
         $stuckunstuck = $lang->stuck;
         $redirect = $lang->redirect_stickthread;
         $moderation->stick_threads($tid);
     }
     $lang->mod_process = $lang->sprintf($lang->mod_process, $stuckunstuck);
     log_moderator_action($modlogdata, $lang->mod_process);
     moderation_redirect(get_thread_link($thread['tid']), $redirect);
     break;
     // Remove redirects to a specific thread
 // Remove redirects to a specific thread
 case "removeredirects":
     // Verify incoming POST request
     verify_post_check($mybb->input['my_post_key']);
     if (!is_moderator($fid, "canmanagethreads")) {
         error_no_permission();
     }
     $plugins->run_hooks("moderation_removeredirects");
     $moderation->remove_redirects($tid);
 function hook_newpoints_do_shop_start()
 {
     global $mybb, $db, $lang, $cache, $theme, $header, $templates, $plugins, $headerinclude, $footer, $options, $inline_errors;
     if ($mybb->get_input('shop_action') == 'buy_sticky') {
         $do = false;
     } elseif ($mybb->get_input('shop_action') == 'do_buy_sticky') {
         $do = true;
     } else {
         return false;
     }
     if ($do) {
         $plugins->run_hooks('newpoints_shop_do_buy_sticky_start');
     } else {
         $plugins->run_hooks('newpoints_shop_buy_sticky_start');
     }
     if (!($item = newpoints_shop_get_item($mybb->get_input('iid', 1)))) {
         error($lang->newpoints_shop_invalid_item);
     }
     if (!($cat = newpoints_shop_get_category($item['cid']))) {
         error($lang->newpoints_shop_invalid_cat);
     }
     if (!newpoints_shop_check_permissions($cat['usergroups'])) {
         error_no_permission();
     }
     if (!$item['visible'] || !$cat['visible']) {
         error_no_permission();
     }
     if (!$item['buy_sticky'] || $item['buy_sticky_time'] < 1) {
         error_no_permission();
     }
     $myitems = @unserialize($mybb->user['newpoints_items']);
     if (!$myitems) {
         error($lang->newpoints_shop_inventory_empty);
     }
     $key = array_search($item['iid'], $myitems);
     if ($key === false) {
         error($lang->newpoints_shop_selected_item_not_owned);
     }
     $this->load_language();
     if ($do) {
         // ~~~ @ https://github.com/PaulBender/Move-Posts/blob/master/inc/plugins/moveposts.php#L217 //
         if ($db->table_exists('google_seo')) {
             $regexp = "{$mybb->settings['bburl']}/{$mybb->settings['google_seo_url_threads']}";
             if ($regexp) {
                 $regexp = preg_quote($regexp, '#');
                 $regexp = str_replace('\\{\\$url\\}', '([^./]+)', $regexp);
                 $regexp = str_replace('\\{url\\}', '([^./]+)', $regexp);
                 $regexp = "#^{$regexp}\$#u";
             }
             $url = $mybb->get_input('threadurl');
             $url = preg_replace('/^([^#?]*)[#?].*$/u', '\\1', $url);
             $url = preg_replace($regexp, '\\1', $url);
             $url = urldecode($url);
             $query = $db->simple_select('google_seo', 'id', "idtype='4' AND url='{$db->escape_string($url)}'");
             $redeemtid = $db->fetch_field($query, 'id');
         }
         $realurl = explode('#', $mybb->get_input('threadurl'));
         $mybb->input['threadurl'] = $realurl[0];
         if (substr($mybb->get_input('threadurl'), -4) == 'html') {
             preg_match('#thread-([0-9]+)?#i', $mybb->get_input('threadurl'), $threadmatch);
             preg_match('#post-([0-9]+)?#i', $mybb->get_input('threadurl'), $postmatch);
             if ($threadmatch[1]) {
                 $parameters['tid'] = $threadmatch[1];
             }
             if ($postmatch[1]) {
                 $parameters['pid'] = $postmatch[1];
             }
         } else {
             $splitloc = explode('.php', $mybb->get_input('threadurl'));
             $temp = explode('&', my_substr($splitloc[1], 1));
             if (!empty($temp)) {
                 for ($i = 0; $i < count($temp); $i++) {
                     $temp2 = explode('=', $temp[$i], 2);
                     $parameters[$temp2[0]] = $temp2[1];
                 }
             } else {
                 $temp2 = explode('=', $splitloc[1], 2);
                 $parameters[$temp2[0]] = $temp2[1];
             }
         }
         if ($parameters['pid'] && !$parameters['tid']) {
             $query = $db->simple_select('posts', '*', "pid='" . (int) $parameters['pid'] . "'");
             $post = $db->fetch_array($query);
             $redeemtid = $post['tid'];
         } elseif ($parameters['tid']) {
             $redeemtid = $parameters['tid'];
         }
         $thread = get_thread($redeemtid);
         // ~~~ //
         if (!$thread['tid'] || !$thread['visible'] || $thread['deletetime']) {
             error($lang->newpoints_buy_sticky_redeem_error_invalid);
         }
         if ($thread['sticky']) {
             error($lang->newpoints_buy_sticky_redeem_error_alreadystickied);
         }
         if ($thread['closed']) {
             error($lang->newpoints_buy_sticky_redeem_error_closedthread);
         }
         if ($thread['uid'] != $mybb->user['uid']) {
             error($lang->newpoints_buy_sticky_redeem_error_wronguser);
         }
         // We need more extensive permission checkings here late on..
         require_once MYBB_ROOT . 'inc/class_moderation.php';
         $moderation = new Moderation();
         $lang->load('moderation');
         $moderation->stick_threads($thread['tid']);
         log_moderator_action(array('fid' => $thread['fid'], 'tid' => $thread['tid']), $lang->sprintf($lang->mod_process, $lang->stuck));
         newpoints_log('buy_sticky', $mybb->settings['bburl'] . '/' . get_thread_link($thread['tid']), $mybb->user['username'], $mybb->user['uid']);
         $rundate = TIME_NOW + $item['buy_sticky_time'] * 86400;
         $did = $db->insert_query("delayedmoderation", array('type' => $db->escape_string('stick'), 'delaydateline' => (int) $rundate, 'uid' => (int) $mybb->user['uid'], 'tids' => (int) $thread['tid'], 'fid' => (int) $thread['fid'], 'dateline' => TIME_NOW, 'inputs' => $db->escape_string(my_serialize(array('new_forum' => (int) $thread['fid'], 'method' => 'move', 'redirect_expire' => '')))));
         $plugins->run_hooks('moderation_do_delayedmoderation');
         // remove item from our inventory
         unset($myitems[$key]);
         sort($myitems);
         $db->update_query('users', array('newpoints_items' => serialize($myitems)), "uid='" . (int) $mybb->user['uid'] . "'");
         $plugins->run_hooks('newpoints_shop_do_buy_sticky_end');
         $message = $lang->sprintf($lang->newpoints_buy_sticky_redeem_done, my_date('relative', $rundate, '', 2));
         redirect($mybb->settings['bburl'] . '/newpoints.php?action=shop&amp;shop_action=myitems', $message, $lang->newpoints_buy_sticky_redeem_done_title);
     } else {
         $lang->newpoints_shop_action = $lang->newpoints_buy_sticky_redeem_title;
         $item['name'] = htmlspecialchars_uni($item['name']);
         global $shop_action, $data, $colspan;
         $colspan = 2;
         $shop_action = 'do_buy_sticky';
         $fields = '<input type="hidden" name="iid" value="' . $item['iid'] . '">';
         $data = "<td class=\"trow1\" width=\"50%\"><strong>" . $lang->newpoints_buy_sticky_redeem_thread . ":</strong><br /><small>" . $lang->newpoints_buy_sticky_redeem_message . "</small></td><td class=\"trow1\" width=\"50%\"><input type=\"text\" class=\"textbox\" name=\"threadurl\" value=\"\"></td>";
         $plugins->run_hooks('newpoints_shop_buy_sticky_end');
         $page = eval($templates->render('newpoints_shop_do_action'));
         output_page($page);
     }
     exit;
 }