function task_xtaorphan_cleanup(&$task)
{
    global $db, $lang, $plugins, $mybb;
    if (is_object($plugins)) {
        $plugins->run_hooks('xthreads_task_xtacleanup', $task);
    }
    // clean out orphaned xtattachments more than 1 day old
    require_once MYBB_ROOT . 'inc/xthreads/xt_modupdhooks.php';
    $count = xthreads_rm_attach_query('tid=0 AND uploadtime<' . (TIME_NOW - 86400));
    // setting "isdatahandler" to true is destructive!!!
    if (!$lang->task_xtaorphan_run_done) {
        $lang->load('xthreads', true);
    }
    if ($count) {
        add_task_log($task, $lang->sprintf($lang->task_xtaorphan_run_cleaned, $count));
    } else {
        add_task_log($task, $lang->task_xtaorphan_run_done);
    }
    // also perform deferred MD5 hashing
    $query = $db->simple_select('xtattachments', 'aid,indir,attachname,updatetime', 'md5hash IS NULL');
    if (isset($db->db_encoding)) {
        // hack for MyBB >= 1.6.12 to force it to not screw up our binary field
        $old_db_encoding = $db->db_encoding;
        $db->db_encoding = 'binary';
    }
    while ($xta = $db->fetch_array($query)) {
        $file = xthreads_get_attach_path($xta);
        $file_md5 = @md5_file($file, true);
        if (strlen($file_md5) == 32) {
            // perhaps not PHP5
            $file_md5 = pack('H*', $file_md5);
        }
        // we ensure that the attachment hasn't been updated during the hashing process by double-checking the updatetime field
        $db->update_query('xtattachments', array('md5hash' => $db->escape_string($file_md5)), 'aid=' . $xta['aid'] . ' AND updatetime=' . $xta['updatetime']);
    }
    if (isset($old_db_encoding)) {
        $db->db_encoding = $old_db_encoding;
    }
}
Exemple #2
0
function xthreads_admin_rebuildthumbs()
{
    global $mybb, $db;
    if ($mybb->request_method == 'post') {
        if (isset($mybb->input['do_rebuildxtathumbs'])) {
            $page = (int) $mybb->input['page'];
            if ($page < 1) {
                $page = 1;
            }
            $perpage = (int) $mybb->input['xtathumbs'];
            if (!$perpage) {
                $perpage = 500;
            }
            global $lang;
            if (!$lang->xthreads_rebuildxtathumbs_nofields) {
                $lang->load('xthreads');
            }
            $thumbfields = xthreads_admin_getthumbfields();
            if (empty($thumbfields)) {
                flash_message($lang->xthreads_rebuildxtathumbs_nofields, 'error');
                admin_redirect(xthreads_admin_url('tools', 'recount_rebuild'));
                return;
            }
            $where = 'field IN ("' . implode('","', array_keys($thumbfields)) . '")';
            //  AND tid!=0
            $num_xta = $db->fetch_field($db->simple_select('xtattachments', 'count(*) as n', $where), 'n');
            @set_time_limit(1800);
            require_once MYBB_ROOT . 'inc/xthreads/xt_upload.php';
            require_once MYBB_ROOT . 'inc/xthreads/xt_updatehooks.php';
            include_once MYBB_ROOT . 'cache/xthreads_evalcache.php';
            // for thumbnail code
            $xtadir = $mybb->settings['uploadspath'] . '/xthreads_ul/';
            if ($xtadir[0] != '/') {
                $xtadir = '../' . $xtadir;
            }
            // TODO: perhaps check for absolute Windows paths as well?  but then, who uses Windows on a production server? :>
            $query = $db->simple_select('xtattachments', '*', $where, array('order_by' => 'aid', 'limit' => $perpage, 'limit_start' => ($page - 1) * $perpage));
            while ($xta = $db->fetch_array($query)) {
                // remove thumbs, then rebuild
                $name = xthreads_get_attach_path($xta);
                // unfortunately, we still need $xtadir
                if ($thumbs = @glob(substr($name, 0, -6) . '*.thumb')) {
                    foreach ($thumbs as &$thumb) {
                        @unlink($xtadir . $xta['indir'] . basename($thumb));
                    }
                }
                $thumb = xthreads_build_thumbnail($thumbfields[$xta['field']], $xta['aid'], $xta['field'], $name, $xtadir, $xta['indir']);
                // TODO: perhaps check for errors? but then, what to do?
            }
            $db->free_result($query);
            ++$page;
            check_proceed($num_xta, $page * $perpage, $page, $perpage, 'xtathumbs', 'do_rebuildxtathumbs', $lang->xthreads_rebuildxtathumbs_done);
        }
    } else {
        $GLOBALS['plugins']->add_hook('admin_formcontainer_end', 'xthreads_admin_rebuildthumbs_show');
    }
}
Exemple #3
0
function xthreads_rm_attach_fs(&$xta)
{
    $name = xthreads_get_attach_path($xta);
    $path = dirname($name) . '/';
    $success = true;
    // remove thumbnails
    if ($thumbs = @glob(substr($name, 0, -6) . '*.thumb')) {
        foreach ($thumbs as &$thumb) {
            $success = $success && @unlink($path . basename($thumb));
        }
    }
    // else // glob _should_ succeed...
    //	$success = false;
    if (!$success) {
        return false;
    }
    $success = $success && @unlink($name);
    // remove month dir if possible
    if ($xta['indir']) {
        $rmdir = true;
        // check for other files
        if ($od = @opendir($path)) {
            while (($file = readdir($od)) !== false) {
                if ($file != '.' && $file != '..' && $file != 'index.html') {
                    $rmdir = false;
                    break;
                }
            }
            closedir($od);
        }
        if ($rmdir) {
            @unlink($path . 'index.html');
            @rmdir($path);
        }
    }
    return $success;
}
function xthreads_duplicate_threadfield_data($tid_old, $tid_new)
{
    global $db, $mybb;
    @ignore_user_abort(true);
    // not really that good, since cancelling elsewhere will break transaction, but, well, copies might be slow, so...
    $tf = $db->fetch_array($db->simple_select('threadfields_data', '*', 'tid=' . $tid_old));
    if (empty($tf)) {
        // no threadfields set for this thread -> nothing to duplicate
        @ignore_user_abort(false);
        return;
    }
    $tf['tid'] = $tid_new;
    // copy xtattachments over
    $query = $db->simple_select('xtattachments', '*', 'tid=' . $tid_old);
    while ($xta = $db->fetch_array($query)) {
        // we have a file we need to duplicate
        $xta['tid'] = $tid_new;
        $oldname = xthreads_get_attach_path($xta);
        $oldpath = dirname($oldname) . '/';
        $xta['attachname'] = substr(md5(uniqid(mt_rand(), true) . substr($mybb->post_code, 16)), 12, 8) . substr($xta['attachname'], 8);
        unset($xta['aid']);
        $tf[$xta['field']] = $xta['aid'] = xthreads_db_insert('xtattachments', $xta);
        $newname = xthreads_get_attach_path($xta);
        $newpath = dirname($newname) . '/';
        $oldfpref = basename(substr($oldname, 0, -6));
        $newfpref = basename(substr($newname, 0, -6));
        if ($thumbs = @glob($oldpath . $oldfpref . '*.thumb')) {
            foreach ($thumbs as &$thumb) {
                $thumb = basename($thumb);
                xthreads_hardlink_file($oldpath . $thumb, $newpath . str_replace($oldfpref, $newfpref, $thumb));
            }
        }
        xthreads_hardlink_file($oldname, $newname);
    }
    xthreads_db_insert('threadfields_data', $tf);
    @ignore_user_abort(false);
}