Title: Attachment Image Size Control v1.0

Author: John Briggs, Scan

Description: This mod will provide controls in admin panel to set max width and height ratio for attachment images.

Copyright:  2006 John Briggs. All rights reserved.

Compatability: XMB 1.9.5 Final

Install Note: Before adding this mod to your forum, you should back up all files related to this mod.

License Note: This mod is released under the GPL License. A copy is provided with this software.

Author Note:
You downloaded this hack from XMBMods.com, the #1 source for XMB related downloads.
Please visit http://www.xmbmods.com/ for support.

=======================================================================================================================================
=======
Step 1:
=======

============================================
Go to administration panel -> Insert raw SQL
============================================

============================
Paste Code And Click Submit:
============================

ALTER TABLE `$table_attachments` ADD `fileheight` varchar(5) NOT NULL default '';
ALTER TABLE `$table_attachments` ADD `filewidth` varchar(5) NOT NULL default '';
ALTER TABLE `$table_settings` ADD `max_attheight` smallint(5) NOT NULL default '350';
ALTER TABLE `$table_settings` ADD `max_attwidth` smallint(5) NOT NULL default '350';
UPDATE `$table_attachments` SET fileheight='350' WHERE fileheight='';
UPDATE `$table_attachments` SET filewidth='350' WHERE filewidth='';

=======================================================================================================================================
=======
Step 2:
=======

===========================
Edit File: English.lang.php
===========================

================================
Add Code At Very Bottom of File:
================================

$lang['max_attheight'] = "Maximum height ration in pixels for attachments to display in posts.";
$lang['max_attwidth'] = "Maximum width ration in pixels for attachments to display in posts.";
$lang['invalidFilename'] = "Invalid Filename";
$lang['thumbEnlarge'] = "Click Image To Enlarge";

=======================================================================================================================================
=======
Step 3:
=======

=================
Edit File: cp.php
=================

==========
Find Code:
==========

        $lang['spell_checker'] .= $spell_off_reason;

===============
Add Code Below:
===============

        $SETTINGS['max_attheight'] = (int) $SETTINGS['max_attheight'];
        $SETTINGS['max_attwidth'] = (int) $SETTINGS['max_attwidth'];

==========
Find Code:
==========

        printsetting1($lang['attachimginpost'], "attachimgpostnew", $attachimgposton, $attachimgpostoff);

===============
Add Code Below:
===============

        printsetting2($lang['max_attheight'], 'max_attheightnew', $SETTINGS['max_attheight'], 3);
        printsetting2($lang['max_attwidth'], 'max_attwidthnew', $SETTINGS['max_attwidth'], 3);

==========
Find Code:
==========

        $max_avatar_size_w_new = (int) $max_avatar_size_w_new;
        $max_avatar_size_h_new = (int) $max_avatar_size_h_new;

===============
Add Code Below:
===============

        $max_attheightnew = (int) $max_attheightnew;
        $max_attwidthnew = (int) $max_attwidthnew;

==========
Find Code:
==========

$db->query("UPDATE $table_settings SET

===========================================
Find Code In-Line In Above Query Statement:
===========================================

");

==================
Replace Code With:
==================

, max_attheight='$max_attheightnew', max_attwidth='$max_attwidthnew'");

=======================================================================================================================================
=======
Step 4:
=======

========================
Edit File: functions.php
========================

==========
Find Code:
==========

/**
* function() - short description of function
*
* Long description of function
*
* @param    $varname    type, what it does
* @return   type, what the return does
*/
function get_attached_file($file, $attachstatus, $max_size=1000000) {
    global $lang, $filename, $filetype, $filesize;

    $filename = '';
    $filetype = '';
    $filesize = 0;

    if ( $file['name'] != 'none' && !empty($file['name']) && $attachstatus != 'off' && is_uploaded_file($file['tmp_name'])) {
        $attachment = addslashes(fread( fopen($file['tmp_name'], 'rb'), filesize($file['tmp_name'])));
        $file['size'] = filesize($file['tmp_name']);    // fix bad filesizes

        if ( $file['size'] > $max_size) {
            error($lang['attachtoobig'], false, '', '', false, false, false, false);
            return false;
        }else{
            $filename = $file['name'];
            $filetype = $file['type'];
            $filesize = $file['size'];

            if ( $filesize == 0) {
                return false;
            } else {
                return $attachment;
            }
        }
    }else{
        return false;
    }
}

==================
Replace Code With:
==================

/**
* function() - short description of function
*
* Long description of function
*
* @param    $varname    type, what it does
* @return   type, what the return does
*/
function isValidFilename($filename) {
    return preg_match('#^[^:\\/?*<>|]+$#', trim($filename));
}

/**
* function() - short description of function
*
* Long description of function
*
* @param    $varname    type, what it does
* @return   type, what the return does
*/
function get_attached_file($file, $attachstatus, $max_size=1000000) {
    global $db, $forum, $lang, $filename, $filetype, $filesize, $fileheight, $filewidth;

    $filename = $filetype = $fileheight = $filewidth = '';
    $filesize = 0;

    if (is_array($file) && $file['name'] != 'none' && !empty($file['name']) && $forum['attachstatus'] != 'off' && is_uploaded_file($file['tmp_name'])) {
        if (!isValidFilename($file['name'])) {
            error($lang['invalidFilename'], false, '', '', false, false, false, false);
            return false;
        }

        $filesize = intval(filesize($file['tmp_name']));
        if ($filesize > $max_size) {
            error($lang['attachtoobig'], false, '', '', false, false, false, false);
            return false;
        }

        $attachment = addslashes(fread(fopen($file['tmp_name'], 'rb'), filesize($file['tmp_name'])));
        $filename = checkInput($file['name']);
        $filetype = checkInput($file['type']);

        $extention = strtolower(substr(strrchr($file['name'],'.'),1));
        if ($extention == 'jpg' || $extention == 'jpeg' || $extention == 'gif' || $extention == 'png' || $extention == 'bmp') {
            $exsize = getimagesize($file['tmp_name']);
            $fileheight = $exsize[1];
            $filewidth = $exsize[0];
        }

        if ($filesize !== 0) {
            return $attachment;
        }
    }
    return false;
}

=======================================================================================================================================
=======
Step 5:
=======

=====================
Edit File: header.php
=====================

==========
Find Code:
==========

    $threadSubject  = '';

===============
Add Code Below:
===============

    $filename       = '';
    $filetype       = '';
    $fileheight     = '';
    $filewidth      = '';
    $filesize       = 0;

=======================================================================================================================================
=======
Step 6:
=======

===================
Edit File: post.php
===================

==========
Find Code:
==========

        // Insert Attachment if there is one
        // Do this last so if it errors out it doesn't break anything important
        if (isset($_FILES['attach']) && ($attachedfile = get_attached_file($_FILES['attach'], $forums['attachstatus'], $max_attach_size)) !== false) {
            $db->query("INSERT INTO $table_attachments ( tid, pid, filename, filetype, filesize, attachment, downloads ) VALUES ('$tid', '$pid', '$filename', '$filetype', '$filesize', '$attachedfile', '0')");
        }

==================
Replace Code With:
==================

        // Insert Attachment if there is one
        // Do this last so if it errors out it doesn't break anything important
        if (isset($_FILES['attach']) && ($attachedfile = get_attached_file($_FILES['attach'], $forums['attachstatus'], $max_attach_size)) !== false) {
            $db->query("INSERT INTO $table_attachments (tid, pid, filename, filetype, filesize, attachment, downloads, fileheight, filewidth) VALUES ('$tid', '$pid', '$filename', '$filetype', '$filesize', '$attachedfile', '0', '$fileheight', '$filewidth')");
        }

==========
Find Code:
==========

            // Insert Attachment if there is one
            // Insert this last so if it errors out it doesn't break something
            if (isset($_FILES['attach']) && ($attachedfile = get_attached_file($_FILES['attach'], $forums['attachstatus'], $max_attach_size)) !== false) {
                $db->query("INSERT INTO $table_attachments ( tid, pid, filename, filetype, filesize, attachment, downloads ) VALUES ('$tid', '$pid', '$filename', '$filetype', '$filesize', '$attachedfile', '0')");
            }

==================
Replace Code With:
==================

            // Insert Attachment if there is one
            // Insert this last so if it errors out it doesn't break something
            if (isset($_FILES['attach']) && ($attachedfile = get_attached_file($_FILES['attach'], $forums['attachstatus'], $max_attach_size)) !== false) {
                $db->query("INSERT INTO $table_attachments (tid, pid, filename, filetype, filesize, attachment, downloads, fileheight, filewidth ) VALUES ('$tid', '$pid', '$filename', '$filetype', '$filesize', '$attachedfile', '0', '$fileheight', '$filewidth')");
            }

==========
Find Code:
==========

            $query = $db->query("SELECT filename, filesize, downloads FROM $table_attachments WHERE pid='$pid' AND tid='$tid'");

==================
Replace Code With:
==================

            $query = $db->query("SELECT filename, filesize, downloads, fileheight, filewidth FROM $table_attachments WHERE pid='$pid' AND tid='$tid'");

==========
Find Code:
==========

            $query = $db->query("SELECT a.filename, a.filesize, a.downloads, p.* FROM $table_posts p LEFT JOIN $table_attachments a  ON (a.pid = p.pid) WHERE p.pid='$pid' AND p.tid='$tid' AND p.fid='$forum[fid]'");

==================
Replace Code With:
==================

            $query = $db->query("SELECT a.filename, a.filesize, a.downloads, a.fileheight, a.filewidth, p.* FROM $table_posts p LEFT JOIN $table_attachments a ON (a.pid=p.pid) WHERE p.pid='$pid' AND p.tid='$tid' AND p.fid='$forum[fid]'");

==========
Find Code:
==========

            if (isset($_FILES['attach']) && ($file = get_attached_file($_FILES['attach'], $forums['attachstatus'], $max_attach_size)) !== false) {
                $db->query("INSERT INTO $table_attachments ( tid, pid, filename, filetype, filesize, attachment, downloads ) VALUES ('$tid', '$pid', '$filename', '$attach[type]', '$filesize', '$file', '0')");
            }

==================
Replace Code With:
==================

            if (isset($_FILES['attach']) && ($file = get_attached_file($_FILES['attach'], $forums['attachstatus'], $max_attach_size)) !== false) {
                $db->query("INSERT INTO $table_attachments (tid, pid, filename, filetype, filesize, attachment, downloads, fileheight, filewidth) VALUES ('$tid', '$pid', '$filename', '$filetype', '$filesize', '$file', '0', '$fileheight', '$filewidth')");
            }

==========
Find Code:
==========

                    case 'replace':
                        if (isset($_FILES['attachment_replace']) && ($file = get_attached_file($_FILES['attachment_replace'], $forums['attachstatus'], $max_attach_size)) !== false) {
                            $db->query("DELETE FROM $table_attachments WHERE pid='$pid'");
                            $db->query("INSERT INTO $table_attachments ( aid, tid, pid, filename, filetype, filesize, attachment, downloads ) VALUES ('', '$tid', '$pid', '$filename', '$attachment_replace[type]', '$filesize', '$file', '0')");
                        }
                        break;

==================
Replace Code With:
==================

                    case 'replace':
                        if (($file = get_attached_file($_FILES['attachment_replace'], $forums['attachstatus'], $max_attach_size)) !== false) {
                            $db->query("DELETE FROM $table_attachments WHERE pid='$pid'");
                            $db->query("INSERT INTO $table_attachments (tid, pid, filename, filetype, filesize, attachment, downloads, fileheight, filewidth ) VALUES ('$tid', '$pid', '$filename', '$attachment_replace[type]', '$filesize', '$file', '0', '$fileheight', '$filewidth')");
                        }
                        break;

=======================================================================================================================================
=======
Step 7:
=======

=========================
Edit File: viewthread.php
=========================

==========
Find Code:
==========

    $querypost = $db->query("SELECT a.aid, a.filename, a.filetype, a.filesize, a.downloads, p.*, m.*,w.time FROM $table_posts p LEFT JOIN $table_members m ON m.username=p.author LEFT JOIN $table_attachments a ON a.pid=p.pid LEFT JOIN $table_whosonline w ON p.author=w.username WHERE p.fid='$fid' AND p.tid='$tid' ORDER BY p.pid ASC LIMIT $start_limit, $ppp");

==================
Replace Code With:
==================

    $querypost = $db->query("SELECT a.aid, a.filename, a.filetype, a.filesize, a.downloads, a.fileheight, a.filewidth, p.*, m.*,w.time FROM $table_posts p LEFT JOIN $table_members m ON m.username=p.author LEFT JOIN $table_attachments a ON a.pid=p.pid LEFT JOIN $table_whosonline w ON p.author=w.username WHERE p.fid='$fid' AND p.tid='$tid' ORDER BY p.pid ASC LIMIT $start_limit, $ppp");

==========
Find Code:
==========

            $extention = strtolower(substr(strrchr($post['filename'],"."),1));
            if ($attachimgpost == 'on' && ($extention == 'jpg' || $extention == 'jpeg' || $extention == 'jpe' || $extention == 'gif' || $extention == 'png' || $extention == 'bmp')) {

==================
Replace Code With:
==================

            $extention = strtolower(substr(strrchr($post['filename'],"."),1));
            if ($attachimgpost == 'on' && ($extention == 'jpg' || $extention == 'jpeg' || $extention == 'gif' || $extention == 'png' || $extention == 'bmp')) {
                // resize attachments
                if ($post['fileheight'] != '' && $post['filewidth'] != '') {
                    $SETTINGS['max_attheight'] = (int) $SETTINGS['max_attheight'];
                    $SETTINGS['max_attwidth'] = (int) $SETTINGS['max_attwidth'];
                    $h_ratio = $SETTINGS['max_attheight'] / $post['fileheight'];
                    $w_ratio = $SETTINGS['max_attwidth'] / $post['filewidth'];
                    if (($post['fileheight'] <= $SETTINGS['max_attheight']) && ($post['filewidth'] <= $SETTINGS['max_attwidth'])) {
                        $n_height = $post['fileheight'];
                        $n_width = $post['filewidth'];
                    } elseif (($w_ratio * $post['fileheight']) < $SETTINGS['max_attheight']) {
                        $n_height = ceil($w_ratio * $post['fileheight']);
                        $n_width = $SETTINGS['max_attwidth'];
                    } else {
                        $n_height = $SETTINGS['max_attheight'];
                        $n_width = ceil($h_ratio * $post['filewidth']);
                    }
                }

=======================================================================================================================================
=======
Step 8:
=======

==========================================================================
Go to administration panel -> Templates -> viewthread_post_attachmentimage
==========================================================================

=================
Replace All With:
=================

<br />
<br />
$post[author] $lang[textattachedimg]<br />
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-style: dashed; border-width: 1px; padding: 10px" bordercolor="$bordercolor" width="$n_width">
<tr>
<td>
<img src="viewthread.php?action=attachment&amp;tid=$tid&amp;pid=$post[pid]" title="$lang[thumbEnlarge]" alt="$lang[thumbEnlarge]" border="0px" width="$n_width" height="$n_height" onclick="window.open('viewthread.php?action=attachment&amp;tid=$tid&amp;pid=$post[pid]','ne','width=$post[filewidth],height=$post[fileheight],resizable=yes,scrollbars=yes')" />
</td>
</tr>
</table>
<br />

=======================================================================================================================================