예제 #1
0
/**
 * MySupport 0.4 - Task File

 * Copyright 2010 Matthew Rogowski

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 ** http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
**/
function task_mysupport($task)
{
    global $mybb, $db, $lang;
    $lang->load("mysupport");
    $task_log = $lang->task_mysupport_ran;
    // if this is empty or 0 it'll effect all threads
    if ($mybb->settings['mysupporttaskautosolvetime'] > 0) {
        $cut = TIME_NOW - intval($mybb->settings['mysupporttaskautosolvetime']);
        $mysupport_forums = implode(",", array_map("intval", mysupport_forums()));
        // are there any MySupport forums??
        if (!empty($mysupport_forums)) {
            // select all the unsolved threads in MySupport forums where the last post was before the cut-off time, and either the status time is before the cut-off time, or the status of the thread has never been changed
            // this means it's not been posted in, and no MySupport actions have taken place on it, within the cut-off time
            $query = $db->simple_select("threads", "tid", "status != '1' AND fid IN (" . $db->escape_string($mysupport_forums) . ") AND lastpost < '" . intval($cut) . "' AND (statustime < '" . intval($cut) . "' OR statustime = '0')");
            $tids = array();
            while ($thread = $db->fetch_array($query)) {
                $tids[] = $thread['tid'];
            }
            $threads_solved = false;
            // if there are any threads to mark as solved
            if (!empty($tids)) {
                mysupport_change_status($tids, 1, true);
                $threads_solved = true;
            }
        }
        if ($threads_solved) {
            $task_log .= $lang->sprintf($lang->task_mysupport_autosolve_count, count($tids));
        }
    }
    if ($mybb->settings['mysupporttaskbackup'] > 0) {
        $timecut = TIME_NOW - $mybb->settings['mysupporttaskbackup'];
        $query = $db->simple_select("mysupport", "*", "type = 'backup' AND extra > '" . intval($timecut) . "'");
        // no backups have been made within the cut off time
        if ($db->num_rows($query) == 0) {
            if (!defined('MYBB_ADMIN_DIR')) {
                if (!isset($config['admin_dir'])) {
                    $config['admin_dir'] = "admin";
                }
                define('MYBB_ADMIN_DIR', MYBB_ROOT . $config['admin_dir'] . "/");
            }
            if (is_writable(MYBB_ADMIN_DIR . "backups")) {
                $name = substr(md5($mybb->user['uid'] . TIME_NOW), 0, 10) . random_str(54);
                $file = MYBB_ADMIN_DIR . "backups/mysupport_backup_" . $name . ".sql";
                $f = @fopen($file, "w");
                @fwrite($f, "<?php\n");
                @fwrite($f, "/**\n * Backup of MySupport data\n * Generated: " . date("dS F Y \\a\\t H:i", TIME_NOW) . "\n * Only to be imported via the MySupport backup importer.\n**/\n\n");
                require_once MYBB_ROOT . "inc/plugins/mysupport/mysupport.php";
                $mysupport_columns = mysupport_table_columns(2);
                foreach ($mysupport_columns as $table => $columns) {
                    switch ($table) {
                        case "forums":
                            $id_field = "fid";
                            break;
                        case "threads":
                            $id_field = "tid";
                            break;
                        case "users":
                            $id_field = "uid";
                            break;
                        case "usergroups":
                            $id_field = "gid";
                            break;
                    }
                    $columns = implode(", ", array_map($db->escape_string, array_keys($columns)));
                    $query = $db->simple_select($table, $id_field . "," . $columns);
                    $columns = explode(", ", $columns);
                    while ($r = $db->fetch_array($query)) {
                        $set = "";
                        foreach ($columns as $column) {
                            if (!empty($set)) {
                                $set .= ", ";
                            }
                            $set .= "`" . $column . "` = '" . $r[$column] . "'";
                        }
                        $q = "\$queries[] = \"UPDATE " . TABLE_PREFIX . $table . " SET " . $set . " WHERE `" . $id_field . "` = '" . $r[$id_field] . "'\";\n";
                        @fwrite($f, $q);
                    }
                }
                $query = $db->simple_select("mysupport");
                while ($r = $db->fetch_array($query)) {
                    $keys = array();
                    $vals = array();
                    foreach ($r as $key => $val) {
                        $keys[] = "`" . $key . "`";
                        $vals[] = "'" . $val . "'";
                    }
                    $q = "\$queries[] = \"INSERT INTO " . TABLE_PREFIX . "mysupport (" . implode(",", $keys) . ") VALUES (" . implode(",", $vals) . ")\";\n";
                    @fwrite($f, $q);
                }
                @fwrite($f, "?>");
                @fclose($f);
                $insert = array("type" => "backup", "name" => $db->escape_string($name), "extra" => TIME_NOW);
                $db->insert_query("mysupport", $insert);
                // get the latest 3 backups
                $query = $db->simple_select("mysupport", "mid", "type = 'backup'", array("order_by" => "extra", "order_dir" => "DESC", "limit" => 3));
                $backups = array(0);
                while ($backup = $db->fetch_field($query, "mid")) {
                    $backups[] = $backup;
                }
                $backups = implode(",", array_map("intval", $backups));
                // select all the backups that aren't the last 3
                $query = $db->simple_select("mysupport", "mid, name", "type = 'backup' AND mid NOT IN (" . $db->escape_string($backups) . ")");
                while ($backup = $db->fetch_array($query)) {
                    if (file_exists(MYBB_ADMIN_DIR . "backups/mysupport_backup_" . $backup['name'] . ".sql")) {
                        @unlink(MYBB_ADMIN_DIR . "backups/mysupport_backup_" . $backup['name'] . ".sql");
                    }
                    $db->delete_query("mysupport", "mid = '" . intval($backup['mid']) . "'");
                }
                $task_log .= " " . $lang->task_mysupport_backup_ran;
            }
        }
    }
    if (!empty($task_log)) {
        add_task_log($task, $task_log);
    }
    /*
    SELECT `t`.`tid`, `t`.`subject`, `t`.`fid`, `f`.`name`, `t`.`status`, `t`.`statusuid`, `u1`.`username` AS `statusuid_username`, `t`.`statustime`, `t`.`bestanswer`, `t`.`assign`, `u2`.`username` AS `assign_username`, `t`.`assignuid`, `u3`.`username` AS `assignuid_username`, `t`.`priority`, `m`.`name` AS `priority_name`, `t`.`prefix`, `tp`.`prefix` AS `prefix_name`
    FROM `mybb_threads` `t`
    LEFT JOIN `mybb_forums` `f` ON `t`.`fid` = `f`.`fid`
    LEFT JOIN `mybb_threadprefixes` `tp` ON `t`.`prefix` = `tp`.`pid`
    LEFT JOIN `mybb_users` `u1` ON `t`.`statusuid` = `u1`.`uid`
    LEFT JOIN `mybb_users` `u2` ON `t`.`assign` = `u2`.`uid`
    LEFT JOIN `mybb_users` `u3` ON `t`.`assignuid` = `u3`.`uid`
    LEFT JOIN `mybb_mysupport` `m` ON `t`.`priority` = `m`.`mid`
    WHERE CONCAT(',', f.parentlist, ',') LIKE '%,1,%'
    AND `t`.`closed` NOT LIKE 'moved|%'
    ORDER BY `t`.`tid` ASC;
    */
}
예제 #2
0
function mysupport_do_inline_thread_moderation()
{
    global $mybb;
    // we're hooking into the start of moderation.php, so if we're not submitting a MySupport action, exit now
    if (strpos($mybb->input['action'], "mysupport") === false) {
        return false;
    }
    verify_post_check($mybb->input['my_post_key']);
    global $db, $cache, $lang, $mod_log_action, $redirect;
    $lang->load("mysupport");
    $fid = intval($mybb->input['fid']);
    if (!is_moderator($fid, 'canmanagethreads')) {
        error_no_permission();
    }
    if ($mybb->input['inlinetype'] == "search") {
        $type = "search";
        $id = $mybb->input['searchid'];
        $redirect_url = "search.php?action=results&sid=" . rawurlencode($id);
    } else {
        $type = "forum";
        $id = $fid;
        $redirect_url = get_forum_link($fid);
    }
    $threads = getids($id, $type);
    if (count($threads) < 1) {
        mysupport_error($lang->error_inline_nothreadsselected);
        exit;
    }
    clearinline($id, $type);
    $tids = implode(",", array_map("intval", $threads));
    $mysupport_threads = array();
    // in a list of search results, you could see threads that aren't from a MySupport forum, but the MySupport options will always show in the inline moderation options regardless of this
    // this is a way of determining which of the selected threads from a list of search results are in a MySupport forum
    // this isn't necessary for inline moderation via the forum display, as the options only show in MySupport forums to begin with
    if ($type == "search") {
        // list of MySupport forums
        $mysupport_forums = implode(",", array_map("intval", mysupport_forums()));
        // query all the threads that are in the list of TIDs and where the FID is also in the list of MySupport forums and where the thread is set to be a support thread
        // this will knock out the non-MySupport threads
        $query = $db->simple_select("threads", "tid", "fid IN (" . $db->escape_string($mysupport_forums) . ") AND tid IN (" . $db->escape_string($tids) . ") AND issupportthread = '1'");
        while ($tid = $db->fetch_field($query, "tid")) {
            $mysupport_threads[] = intval($tid);
        }
        $threads = $mysupport_threads;
        // if the new list of threads is empty, no MySupport threads have been selected
        if (count($threads) < 1) {
            mysupport_error($lang->no_mysupport_threads_selected);
            exit;
        }
    } elseif ($type == "forum") {
        $query = $db->simple_select("threads", "tid", "tid IN (" . $db->escape_string($tids) . ") AND issupportthread = '1'");
        while ($tid = $db->fetch_field($query, "tid")) {
            $mysupport_threads[] = intval($tid);
        }
        $threads = $mysupport_threads;
        // if the new list of threads is empty, no MySupport threads have been selected
        if (count($threads) < 1) {
            mysupport_error($lang->no_mysupport_threads_selected);
            exit;
        }
    }
    $mod_log_action = "";
    $redirect = "";
    if (strpos($mybb->input['action'], "status") !== false) {
        $status = str_replace("mysupport_status_", "", $mybb->input['action']);
        if ($status == 2 || $status == 4) {
            $perm = "canmarktechnical";
        } else {
            $perm = "canmarksolved";
        }
        // they don't have permission to perform this action, so go through the different statuses and show an error for the right one
        if (!mysupport_usergroup($perm)) {
            switch ($status) {
                case 1:
                    mysupport_error($lang->no_permission_mark_solved_multi);
                    break;
                case 2:
                    mysupport_error($lang->no_permission_mark_technical_multi);
                    break;
                case 3:
                    mysupport_error($lang->no_permission_mark_solved_close_multi);
                    break;
                case 4:
                    mysupport_error($lang->no_permission_mark_nottechnical_multi);
                    break;
                default:
                    mysupport_error($lang->no_permission_mark_notsolved_multi);
            }
        }
        mysupport_change_status($threads, $status, true);
    }
    if (strpos($mybb->input['action'], "onhold") !== false) {
        $hold = str_replace("mysupport_onhold_", "", $mybb->input['action']);
        if (!mysupport_usergroup("canmarksolved")) {
            mysupport_error($lang->no_permission_thread_hold_multi);
            exit;
        }
        mysupport_change_hold($threads, $hold, true);
    } elseif (strpos($mybb->input['action'], "assign") !== false) {
        if (!mysupport_usergroup("canassign")) {
            mysupport_error($lang->assign_no_perms);
            exit;
        }
        $assign = str_replace("mysupport_assign_", "", $mybb->input['action']);
        if ($assign == 0) {
            // in the function to change the assigned user, -1 means removing; 0 is easier to put into the form than -1, so change it back here
            $assign = -1;
        } else {
            $assign_users = mysupport_get_assign_users();
            // -1 is what's used to unassign a thread so we need to exclude that
            if (!array_key_exists($assign, $assign_users)) {
                mysupport_error($lang->assign_invalid);
                exit;
            }
        }
        mysupport_change_assign($threads, $assign, true);
    } elseif (strpos($mybb->input['action'], "priority") !== false) {
        if (!mysupport_usergroup("cansetpriorities")) {
            mysupport_error($lang->priority_no_perms);
            exit;
        }
        $priority = str_replace("mysupport_priority_", "", $mybb->input['action']);
        if ($priority == 0) {
            // in the function to change the priority, -1 means removing; 0 is easier to put into the form than -1, so change it back here
            $priority = -1;
        } else {
            $mysupport_cache = $cache->read("mysupport");
            $mids = array();
            if (!empty($mysupport_cache['priorities'])) {
                foreach ($mysupport_cache['priorities'] as $priority_info) {
                    $mids[] = intval($priority_info['mid']);
                }
            }
            if (!in_array($priority, $mids)) {
                mysupport_error($lang->priority_invalid);
                exit;
            }
        }
        mysupport_change_priority($threads, $priority, true);
    } elseif (strpos($mybb->input['action'], "category") !== false) {
        $category = str_replace("mysupport_category_", "", $mybb->input['action']);
        if ($category == 0) {
            // in the function to change the category, -1 means removing; 0 is easier to put into the form than -1, so change it back here
            $category = -1;
        } else {
            $categories = mysupport_get_categories($forum);
            if (!array_key_exists($category, $categories) && $category != "-1") {
                mysupport_error($lang->category_invalid);
                exit;
            }
        }
        mysupport_change_category($threads, $category, true);
    }
    $mod_log_data = array("fid" => intval($fid));
    log_moderator_action($mod_log_data, $mod_log_action);
    redirect($redirect_url, $redirect);
}