=======================================================================================================================================
Modification Title: BB Code: Image Size Control

Modification Version: 1.0

Modification Author: Scan, John Briggs

Modification Description:
This modification will provide controls in the admin panel to set max. width and height ratios for bb code images.

Modification Copyright:  2007-2009 XMBMods.com. All rights reserved.

Modification Compatibility: XMB 1.9.8 SP3

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

Modification License Note: This modification is released under the GPL v3 License. A copy is provided with this software package.

Modification Author Note:
This modification is developed and released for use with XMB 1.9.8 Engage Final SP3 which is provided by XMBGarage.com.

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

===================================
Go To Administration Panel -> Insert Raw SQL
===================================

Upload provided file named "SQL.txt" and click "Submit Changes" button.

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

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

=================================
Add Code At Very Bottom Of File Above ?>
=================================

// BB Code: Image Size Control Mod Begin
$lang['bbcimg_status'] = "BB Code Image Size Control Status:<br /><span class=\"smalltxt\">Enabling this will enforce size controls for BB Code images in posts.<br /><strong>Warning! This feature is server intensive if enabled.</strong></span>";
$lang['bbc_maxht'] = "Maximum height ration for BB Code images in pixels to display in posts.<br /><span class=\"smalltxt\">Maximum height size for bb code images if set to on.</span>";
$lang['bbc_maxwd'] = "Maximum width ration for BB Code images in pixels to display in posts.<br /><span class=\"smalltxt\">Maximum width size for bb code images if set to on.</span>";
// BB Code: Image Size Control Mod End

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

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

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

        settingHTML('onlinetoday_status', $onlinetoday_statuson, $onlinetoday_statusoff);

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

        // BB Code: Image Size Control Mod Begin
        $bbcimgon = $bbcimgoff = '';
        settingHTML('bbcimg_status', $bbcimgon, $bbcimgoff);
        // BB Code: Image Size Control Mod End

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

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

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

        // BB Code: Image Size Control Mod Begin
        printsetting1($lang['bbcimg_status'], 'bbcimg_statusnew', $bbcimgon, $bbcimgoff);
        printsetting2($lang['bbc_maxht'], 'bbc_maxhtnew', ((int)$SETTINGS['bbc_maxht']), 3);
        printsetting2($lang['bbc_maxwd'], 'bbc_maxwdnew', ((int)$SETTINGS['bbc_maxwd']), 3);
        // BB Code: Image Size Control Mod End

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

        $onlinetoday_statusnew = formOnOff('onlinetoday_statusnew');

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

        // BB Code: Image Size Control Mod Begin
        $bbcimg_statusnew = formOnOff('bbcimg_statusnew');
        $bbc_maxwdnew = formInt('bbc_maxwdnew');
        $bbc_maxhtnew = formInt('bbc_maxhtnew');
        // BB Code: Image Size Control Mod End

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

        $db->query("UPDATE ".X_PREFIX."settings SET

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

        $db->query("UPDATE ".X_PREFIX."settings SET
            bbcimg_status='$bbcimg_statusnew',
            bbc_maxht='$bbc_maxhtnew',
            bbc_maxwd='$bbc_maxwdnew',

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

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

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

function postify($message, $smileyoff='no', $bbcodeoff='no', $allowsmilies='yes', $allowhtml='yes', $allowbbcode='yes', $allowimgcode='yes', $ignorespaces=false, $ismood="no", $wrap="yes") {

===============
Add Code Above:
===============

// BB Code: Image Size Control Mod Begin
function check_image_size($matches) {
    global $SETTINGS, $lang;

    if (empty($matches[3])) $matches[3] = '';

    $imgurl = $matches[1].'://'.$matches[2].$matches[3];

    if (list($width, $height) = @getimagesize($imgurl)) {
        $w_ratio = (int)$SETTINGS['bbc_maxwd'] / $width;
        $h_ratio = (int)$SETTINGS['bbc_maxht'] / $height;
        if (($height <= (int)$SETTINGS['bbc_maxht']) && ($width <= (int)$SETTINGS['bbc_maxwd'])) {
            $n_height = $height;
            $n_width = $width;
        } else if (($w_ratio * $height) < (int)$SETTINGS['bbc_maxht']) {
            $n_height = ceil($w_ratio * $height);
            $n_width = (int)$SETTINGS['bbc_maxwd'];
        } else {
            $n_height = (int)$SETTINGS['bbc_maxht'];
            $n_width = ceil($h_ratio * $width);
        }
        $replace = '[img='.$n_width.'x'.$n_height.']'.$imgurl.'[/img]';
    } else {
        $replace = '[bad img]'.$imgurl.'[/bad img]';
    }
    return $replace;
}
// BB Code: Image Size Control Mod End

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

    global $imgdir, $bordercolor, $db, $smdir, $smiliecache, $censorcache, $smiliesnum, $wordsnum, $versionbuild, $fontsize;

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

    global $imgdir, $bordercolor, $db, $smdir, $smiliecache, $censorcache, $smiliesnum, $wordsnum, $versionbuild, $lang, $fontsize, $SETTINGS, $THEME;

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

    if ($allowimgcode != 'no' && $allowimgcode != 'off') {
        if (false == stripos($message, 'javascript:')) {
            $patterns[] = '#\[img\](http[s]?|ftp[s]?){1}://([:a-z\\./_\-0-9%~]+){1}\[/img\]#Smi';
            $replacements[] = '<img src="\1://\2\3" border="0" alt="\1://\2\3"/>';
            $patterns[] = "#\[img=([0-9]*?){1}x([0-9]*?)\](http[s]?|ftp[s]?){1}://([:~a-z\\./0-9_\-%]+){1}(\?[a-z=0-9&_\-;~]*)?\[/img\]#Smi";
            $replacements[] = '<img width="\1" height="\2" src="\3://\4\5" alt="\3://\4\5" border="0" />';
            $patterns[] = "#\[flash=([0-9]*?){1}x([0-9]*?)\]([^\"'<>]*?)\[/flash\]#Ssi";
            $replacements[] = '<object type="application/x-shockwave-flash" data="$3" width="$1" height="$2"><param name="movie" value="$3" /><param name="AllowScriptAccess" value="never" /></object>';
        }
    }

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

    // BB Code: Image Size Control Mod Begin
    if ($allowimgcode != 'no' && $allowimgcode != 'off') {
         if (false == strpos($message, 'javascript:')) {
            if ($SETTINGS['bbcimg_status'] == 'on') {
                $pattern_img = '#\[img\](http[s]?|ftp[s]?){1}://([:a-z\\./_\-0-9%~]+){1}(\?[a-z=_\-0-9&;~]*)?\[/img\]#Smi';
                $message = preg_replace_callback($pattern_img, 'check_image_size', $message);
            } else {
                $patterns[] = '#\[img\](http[s]?|ftp[s]?){1}://([:a-z\\./_\-0-9%~]+){1}(\?[a-z=_\-0-9&;~]*)?\[/img\]#Smi';
                $replacements[] = '<img src="\1://\2\3" alt="\1://\2\3" title="\1://\2\3" border="0" />';
            }
            $patterns[] = "#\[img=([0-9]*?){1}x([0-9]*?)\](http[s]?|ftp[s]?){1}://([:~a-z\\./0-9_\-%]+){1}(\?[a-z=0-9&_\-;~]*)?\[/img\]#Smi";
            $replacements[] = '<img width="\1" height="\2" src="\3://\4\5" alt="\3://\4\5" title="\3://\4\5" border="0" />';
            $patterns[] = "#\[flash=([0-9]*?){1}x([0-9]*?)\](.*?)\[/flash\]#Ssi";
            $replacements[] = '<object type="application/x-shockwave-flash" data="$3" width="$1" height="$2"><param name="movie" value="$3" /><param name="AllowScriptAccess" value="never" /></object>';
        }
    }
    // BB Code: Image Size Control Mod End

=======================================================================================================================================
Enjoy!