============================================================================================================================
Modification Title: BBCode: Image Size Control

Version: 1.0

Author: Scan, John Briggs

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

Copyright:  2010 John Briggs. All Rights Reserved.

Compatability: XMB 1.9.5 SP1

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

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

Author Note:
You downloaded this modification from XMBGarage.com, the #1 source for XMB related downloads.
Please visit http://www.xmbgarage.com/ for support.
============================================================================================================================
=======
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 To End Of File Above  ?>
============================

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

============================================================================================================================
=======
Step 3:
=======
==============
Edit File: cp.php
==============
==========
Find Code:
==========

        if (!empty($avatarlist)) {
            $avchecked[1] = true;
        } elseif (!empty($avataroff)) {
            $avchecked[2] = true;
        } else {
            $avchecked[0] = true;
        }

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

        // BB Code: Image Size Control Mod Begin
        $bbcimgon = $bbcimgoff = '';
        switch ($SETTINGS['bbcimg_status']) {
            case 'on':
                $bbcimgon = $selHTML;
                break;
            default:
                $bbcimgoff = $selHTML;
                break;
        }
        // BB Code: Image Size Control Mod End

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

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

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

        // BB Code: Image Size Control Mod Begin
        $SETTINGS['bbc_maxwd'] = (int) $SETTINGS['bbc_maxwd'];
        $SETTINGS['bbc_maxht'] = (int) $SETTINGS['bbc_maxht'];
        // BB Code: Image Size Control Mod End

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

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

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

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

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

        $resetSigNew = ($resetSigNew == 'on') ? 'on' : 'off';

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

        // BB Code: Image Size Control Mod Begin
        $bbcimg_statusnew = ($bbcimg_statusnew == 'on') ? 'on' : 'off';
        $bbc_maxwdnew = (int) $bbc_maxwdnew;
        $bbc_maxhtnew = (int) $bbc_maxhtnew;
        // BB Code: Image Size Control Mod End

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

        $db->query("UPDATE $table_settings SET

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

");

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

, bbcimg_status='$bbcimg_statusnew', bbc_maxht='$bbc_maxhtnew', bbc_maxwd='$bbc_maxwdnew'");

============================================================================================================================
=======
Step 4:
=======
==================
Edit File: functions.php
==================
==========
Find Code:
==========

function createAbsFSizeFromRel($rel) {
    global $fontsize;
    static $cachedFs;

    if (!is_array($cachedFs) || count($cachedFs) != 2) {
        preg_match('#([0-9]+)([a-z]+)?#i', $fontsize, $res);
        $cachedFs[0] = $res[1];
        $cachedFs[1] = $res[2];

        if (empty($cachedFs[1])) {
            $cachedFs[1] = 'px';
        }
    }
    $o = ($rel+$cachedFs[0]).$cachedFs[1];
    return $o;
}

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

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

    $SETTINGS['bbc_maxwd'] = (int) $SETTINGS['bbc_maxwd'];
    $SETTINGS['bbc_maxht'] = (int) $SETTINGS['bbc_maxht'];

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

    $imgurl = $matches[1].'://'.$matches[2].$matches[3];
    if (list($width, $height) = @getimagesize($imgurl)) {
        $w_ratio = $SETTINGS['bbc_maxwd'] / $width;
        $h_ratio = $SETTINGS['bbc_maxht'] / $height;

        if (($height <= $SETTINGS['bbc_maxht']) && ($width <= $SETTINGS['bbc_maxwd'])) {
            $n_height = $height;
            $n_width = $width;
        } elseif (($w_ratio * $height) < $SETTINGS['bbc_maxht']) {
            $n_height = ceil($w_ratio * $height);
            $n_width = $SETTINGS['bbc_maxwd'];
        } else {
            $n_height = $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, $table_words, $table_forums, $table_smilies, $db, $smdir, $smiliecache, $censorcache, $smiliesnum, $wordsnum, $versionbuild, $lang, $fontsize;

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

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

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

        if ($allowimgcode != 'no' && $allowimgcode != 'off') {
            if (false == strpos($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 ((stristr($message, 'jpg[/img]') || stristr($message, 'jpeg[/img]') || stristr($message, 'gif[/img]') || stristr($message, 'png[/img]') || stristr($message, 'bmp[/img]') || stristr($message, 'php[/img]'))) {
                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!