/**
 * Rebuild thumbnails for attachments
 */
function acp_rebuild_attachment_thumbnails()
{
    global $db, $mybb, $lang;
    $query = $db->simple_select("attachments", "COUNT(aid) as num_attachments");
    $num_attachments = $db->fetch_field($query, 'num_attachments');
    $page = $mybb->get_input('page', MyBB::INPUT_INT);
    $per_page = $mybb->get_input('attachmentthumbs', MyBB::INPUT_INT);
    if ($per_page <= 0) {
        $per_page = 20;
    }
    $start = ($page - 1) * $per_page;
    $end = $start + $per_page;
    require_once MYBB_ROOT . "inc/functions_image.php";
    $query = $db->simple_select("attachments", "*", '', array('order_by' => 'aid', 'order_dir' => 'asc', 'limit_start' => $start, 'limit' => $per_page));
    while ($attachment = $db->fetch_array($query)) {
        $ext = my_strtolower(my_substr(strrchr($attachment['filename'], "."), 1));
        if ($ext == "gif" || $ext == "png" || $ext == "jpg" || $ext == "jpeg" || $ext == "jpe") {
            $thumbname = str_replace(".attach", "_thumb.{$ext}", $attachment['attachname']);
            $thumbnail = generate_thumbnail(MYBB_ROOT . "uploads/" . $attachment['attachname'], MYBB_ROOT . "uploads/", $thumbname, $mybb->settings['attachthumbh'], $mybb->settings['attachthumbw']);
            if ($thumbnail['code'] == 4) {
                $thumbnail['filename'] = "SMALL";
            }
            $db->update_query("attachments", array("thumbnail" => $thumbnail['filename']), "aid='{$attachment['aid']}'");
        }
    }
    check_proceed($num_attachments, $end, ++$page, $per_page, "attachmentthumbs", "do_rebuildattachmentthumbs", $lang->success_rebuilt_attachment_thumbnails);
}
Example #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');
    }
}
function acp_tyl_recount()
{
    global $db, $mybb, $lang, $prefix;
    $page = $mybb->get_input('page', MyBB::INPUT_INT);
    $per_page = $mybb->get_input('tyls', MyBB::INPUT_INT);
    if ($per_page <= 0) {
        $per_page = 500;
    }
    $start = ($page - 1) * $per_page;
    $end = $start + $per_page;
    // On the first run, reset all totals and remove any orphaned tyls
    // Orphaned tyls are those with no linked posts or users
    if ($page == 1) {
        $db->write_query("UPDATE " . TABLE_PREFIX . $prefix . "stats SET value=0 WHERE title='total'");
        $db->write_query("UPDATE " . TABLE_PREFIX . "posts SET tyl_pnumtyls=0");
        $db->write_query("UPDATE " . TABLE_PREFIX . "threads SET tyl_tnumtyls=0");
        $db->write_query("UPDATE " . TABLE_PREFIX . "users SET tyl_unumtyls=0, tyl_unumptyls=0, tyl_unumrcvtyls=0");
        $query = $db->query("\n\t\t\t\tSELECT tyl.tlid\n\t\t\t\tFROM " . TABLE_PREFIX . $prefix . "thankyoulike tyl\n\t\t\t\tLEFT JOIN " . TABLE_PREFIX . "posts p ON ( p.pid = tyl.pid )\n\t\t\t\tLEFT JOIN " . TABLE_PREFIX . "users u ON ( u.uid = tyl.uid )\n\t\t\t\tWHERE p.pid IS NULL OR u.uid IS NULL\n\t\t\t");
        $tlids_remove = array();
        while ($orphan = $db->fetch_array($query)) {
            $tlids_remove[] = $orphan['tlid'];
        }
        if ($tlids_remove) {
            $tlids_remove = implode(',', $tlids_remove);
            // Delete the tyls
            $db->delete_query($prefix . "thankyoulike", "tlid IN ({$tlids_remove})");
        }
        // Lets also update the puid field in the db with uid values from the posts table
        // This is done to sync up the db with the puids of post tyled, since this feature wasn't there in v1.0 so data needs to be generated.
        $db->write_query("UPDATE " . TABLE_PREFIX . $prefix . "thankyoulike tyl \n\t\t\t\t\tLEFT JOIN " . TABLE_PREFIX . "posts p ON ( p.pid=tyl.pid )  \n\t\t\t\t\tSET tyl.puid=p.uid");
        // Update the number of tyled posts for the post owners, we do this here because this needs to be done in one swoop and will break if done in parts
        $db->write_query("UPDATE " . TABLE_PREFIX . "users u \n\t\t\t\t\tJOIN (SELECT puid, COUNT(DISTINCT(pid)) AS pidcount \n\t\t\t\t\tFROM " . TABLE_PREFIX . $prefix . "thankyoulike\n\t\t\t\t\tGROUP BY puid) tyl\n\t\t\t\t\tON ( u.uid=tyl.puid )\n\t\t\t\t\tSET u.tyl_unumptyls=tyl.pidcount");
    }
    $query1 = $db->simple_select($prefix . "thankyoulike", "COUNT(tlid) AS num_tyls");
    $num_tyls = $db->fetch_field($query1, 'num_tyls');
    $query2 = $db->query("\n\t\t\tSELECT tyl.*, p.tid\n\t\t\tFROM " . TABLE_PREFIX . $prefix . "thankyoulike tyl\n\t\t\tLEFT JOIN " . TABLE_PREFIX . "posts p ON (p.pid=tyl.pid)\n\t\t\tORDER BY tyl.dateline ASC\n\t\t\tLIMIT {$start}, {$per_page}\n\t\t");
    $tlids = array();
    $post_tyls = array();
    $thread_tyls = array();
    $user_tyls = array();
    $user_rcvtyls = array();
    while ($tyl = $db->fetch_array($query2)) {
        // Total tyls
        $tlids[] = $tyl['tlid'];
        // Count the tyl for each post, thread and user
        if ($post_tyls[$tyl['pid']]) {
            $post_tyls[$tyl['pid']]++;
        } else {
            $post_tyls[$tyl['pid']] = 1;
        }
        if ($thread_tyls[$tyl['tid']]) {
            $thread_tyls[$tyl['tid']]++;
        } else {
            $thread_tyls[$tyl['tid']] = 1;
        }
        if ($user_tyls[$tyl['uid']]) {
            $user_tyls[$tyl['uid']]++;
        } else {
            $user_tyls[$tyl['uid']] = 1;
        }
        if ($user_rcvtyls[$tyl['puid']]) {
            $user_rcvtyls[$tyl['puid']]++;
        } else {
            $user_rcvtyls[$tyl['puid']] = 1;
        }
    }
    // Update the counts
    if (is_array($post_tyls)) {
        foreach ($post_tyls as $pid => $add) {
            $db->write_query("UPDATE " . TABLE_PREFIX . "posts SET tyl_pnumtyls=tyl_pnumtyls+{$add} WHERE pid='{$pid}'");
        }
    }
    if (is_array($thread_tyls)) {
        foreach ($thread_tyls as $tid => $add) {
            $db->write_query("UPDATE " . TABLE_PREFIX . "threads SET tyl_tnumtyls=tyl_tnumtyls+{$add} WHERE tid='{$tid}'");
        }
    }
    if (is_array($user_tyls)) {
        foreach ($user_tyls as $uid => $add) {
            $db->write_query("UPDATE " . TABLE_PREFIX . "users SET tyl_unumtyls=tyl_unumtyls+{$add} WHERE uid='{$uid}'");
        }
    }
    if (is_array($user_rcvtyls)) {
        foreach ($user_rcvtyls as $puid => $add) {
            $db->write_query("UPDATE " . TABLE_PREFIX . "users SET tyl_unumrcvtyls=tyl_unumrcvtyls+{$add} WHERE uid='{$puid}'");
        }
    }
    if ($tlids) {
        $tlids_count = count($tlids);
        $db->write_query("UPDATE " . TABLE_PREFIX . $prefix . "stats SET value=value+{$tlids_count} WHERE title='total'");
    }
    check_proceed($num_tyls, $end, ++$page, $per_page, "tyls", "do_recounttyls", $lang->tyl_success_thankyoulike_rebuilt);
}