• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Template does not load after mounting Add-On

#1
Hey folks,

I wrote myself a code to get special Avatars for my users. I wrote it into the Core.php to test it and it worked fine. Now i wanted to make it to a proper Add-On because editing original files ist pretty stupid in my opinion.
My Add-On is listening the "Init_dependencies" event and on call of the callBack is executing the following code:
PHP:
XenForo_Template_Helper_Core::$helperCallbacks['avatar'] = array('McAvatar_Listener', 'helperAvatarUrl');
to redirect the call of the "helperAvatarUrl" method to my own Add-On.
If i now mount my Add-On the following happens: The Template seems to not be loading at all which can be seen in the following screenshot.
http://i.imgur.com/rvUq6x1.png

I hope you know how to fix this problem, in case you need the full source code feel free to message me. Suggestions on how to work around changing those variables or alternative ways are also appreciated.
 

Jake Bunce

XenForo moderator
Staff member
#2
Please post your function here.

The screenshot shows a page that appears to lack CSS styling. But I can't determine anything from a screenshot.
 
#3
Okay, Code incoming


PHP:
<?php
 
class McAvatar_Listener
{
    public static function init(XenForo_Dependencies_Abstract $dependencies, array $data)
    {
        XenForo_Template_Helper_Core::$helperCallbacks['avatar'] = array('McAvatar_Listener', 'helperAvatarUrl');
    }
   
    public static function helperAvatarUrl($user, $size, $forceType = null, $canonical = false)
    {
        if (!is_array($user))
        {
            $user = array();
        }
 
        if ($forceType)
        {
            switch ($forceType)
            {
                case 'default':
                case 'custom':
                    break;
 
                default:
                    $forceType = null;
                    break;
            }
        }
 
        $url = self::getAvatarUrl($user, $size, $forceType);
 
        if ($canonical)
        {
            $url = XenForo_Link::convertUriToAbsoluteUri($url, true);
        }
 
        return htmlspecialchars($url);
    }
 
    public static function getAvatarUrl(array $user, $size, $forceType = '')
    {
        if (!empty($user['user_id']) && $forceType != 'default')
        {
            if ($user['gravatar'] && $forceType != 'custom')
            {
                return self::_getGravatarUrl($user, $size);
            }
            else if (!empty($user['avatar_date']))
            {
                return self::_getCustomAvatarUrl($user, $size);
            }
        }
 
        return self::_getDefaultAvatarUrl($user, $size);
    }
 
    public static function getAvatarUrls(array $user)
    {
        $urls = array();
 
        foreach (XenForo_Model_Avatar::getSizes() AS $sizeCode => $maxDimensions)
        {
            $urls[$sizeCode] = self::getAvatarUrl($user, $sizeCode);
        }
 
        return $urls;
    }
   
    protected static function _getDefaultAvatarUrl(array $user, $size)
    {
 
        if  ($user['is_banned'])
        {
            $baimg = imagecreatefrompng("http://minotar.net/helm/{$user['username']}/".XenForo_Model_Avatar::getSizeFromCode($size).".png");
            $imgsize = getimagesize("http://minotar.net/helm/{$user['username']}/".XenForo_Model_Avatar::getSizeFromCode($size).".png");
            imagesetthickness($baimg, 3);
            imageline($baimg, 0, 0, $imgsize[0], $imgsize[1], $gray);
            imageline($baimg, 0, $imgsize[1], $imgsize[0], 0, $gray);
            imagefilter($baimg, IMG_FILTER_GRAYSCALE);
            imagepng($baimg,"images/banned_{$user['username']}".XenForo_Model_Avatar::getSizeFromCode($size).".png");
            return "images/banned_{$user['username']}".XenForo_Model_Avatar::getSizeFromCode($size).".png";
            imagedestroy($baimg);
        }
        else
        {
            return "http://minotar.net/helm/{$user['username']}/".XenForo_Model_Avatar::getSizeFromCode($size).".png";
        }
    }
 
    protected static function _getCustomAvatarUrl(array $user, $size)
    {
        $group = floor($user['user_id'] / 1000);
        if($user['is_banned'])
        {
            $baimg = imagecreatefromjpeg(XenForo_Application::$externalDataUrl."/avatars/$size/$group/$user[user_id].jpg");
            $imgsize = getimagesize(XenForo_Application::$externalDataUrl."/avatars/$size/$group/$user[user_id].jpg");
            imagesetthickness($baimg, 3);
            imageline($baimg, 0, 0, $imgsize[0], $imgsize[1], $gray);
            imageline($baimg, 0, $imgsize[1], $imgsize[0], 0, $gray);
            imagefilter($baimg, IMG_FILTER_GRAYSCALE);
            imagepng($baimg,"images/banned_{$user['username']}".XenForo_Model_Avatar::getSizeFromCode($size).".png");
            return "images/banned_{$user['username']}".XenForo_Model_Avatar::getSizeFromCode($size).".png";
            imagedestroy($baimg);   
        }
        else
        {
        return XenForo_Application::$externalDataUrl . "/avatars/$size/$group/$user[user_id].jpg?$user[avatar_date]";
        }
    }
 
    protected static function _getGravatarUrl(array $user, $size, $default = '')
    {
        $md5 = md5($user['gravatar']);
 
        if ($default === '')
        {
            $default = '&d=' . urlencode(XenForo_Application::get('options')->boardUrl . '/' . self::_getDefaultAvatarUrl($user, $size));
        }
        else if (!empty($default))
        {
            $default = '&d=' . urlencode($default);
        }
 
        if (is_string($size))
        {
            $size = XenForo_Model_Avatar::getSizeFromCode($size);
        }
 
        return (XenForo_Application::$secure ? 'https://secure' : 'http://www')
            . ".gravatar.com/avatar/{$md5}.jpg?s={$size}{$default}";
    }
 
}
?>
 
#5
Positive:

#1
Error Info
ErrorException: Cannot modify header information - headers already sent by (output started at /var/customers/webs/Marc/library/McAvatar/Listener.php:1) -library/XenForo/CssOutput.php:111
Generated By: Unknown Account, 2 minutes ago
Stack Trace
#0 [internal function]: XenForo_Application::handlePhpError(2, 'Cannot modify h...', '/var/customers/...', 111, Array)
#1 /var/customers/webs/Marc/library/XenForo/CssOutput.php(111): header('HTTP/1.1 304 No...', true, 304)
#2 /var/customers/webs/Marc/library/XenForo/CssOutput.php(580): XenForo_CssOutput->handleIfModifiedSinceHeader(Array)
#3 /var/customers/webs/Marc/css.php(12): XenForo_CssOutput::run()
#4 {main}
Request State
array(3) {
["url"] => string(147) "http://www.alonian.de/css.php?css=b...de_list,user_bar&style=3&dir=LTR&d=1364061063"
["_GET"] => array(4) {
["css"] => string(84) "bb_code,bbcm_js,dark_taigachat,editor_ui,node_category,node_forum,node_list,user_bar"
["style"] => string(1) "3"
["dir"] => string(3) "LTR"
["d"] => string(10) "1364061063"
}
["_POST"] => array(0) {
}
}




#2
ErrorException: Cannot modify header information - headers already sent by (output started at /var/customers/webs/Marc/library/McAvatar/Listener.php:1) - library/XenForo/Cron.php:46
Generated By: Unknown Account, 4 minutes ago
Stack Trace
#0 [internal function]: XenForo_Application::handlePhpError(2, 'Cannot modify h...', '/var/customers/...', 46, Array)
#1 /var/customers/webs/Marc/library/XenForo/Cron.php(46): header('Content-Type: i...')
#2 /var/customers/webs/Marc/library/XenForo/Cron.php(63): XenForo_Cron->outputImage()
#3 /var/customers/webs/Marc/cron.php(12): XenForo_Cron::runAndOutput()
#4 {main}
Request State
array(3) {
["url"] => string(41) "http://www.alonian.de/cron.php?1364166163"
["_GET"] => array(1) {
[1364166163] => string(0) ""
}
["_POST"] => array(0) {
}
}
 

cclaerhout

Well-known member
#10
May be you should debug your img functions line by line
PHP:
"/avatars/$size/$group/$user[user_id].jpg");
$user[user_id] => will not work, only "simple" variables (without array key) can be used in double quotes strings.

This cmd ' imagedestroy($baimg);' will not be executed since it is below the return.

The $gray variable is not defined.

About the imagepng cmd, you should check if the image is saved in the directory (does the directory exist?) ; otherwise it seems this function will directly try to output the image (I've never used it, so I'm not sure).
 

Jon W

Well-known member
#13
Each time I've tried to use it, it wasn't working... :confused:
Thanks for the link, I will try again

EDIT:.... i see... each time I've tried I was wrapping the key with single quotes.
Thanks again !!!
I've always assumed the same as you until I spotted this recently. I always use single quotes anyway though to avoid making mistakes.
 
#14
I tried all the things you guys suggested but is still is not working! Could someone please try it out for your self?
Sorry for the late response.
 

Jon W

Well-known member
#15
Not sure if this is what is supposed to happen, but if I enable this add-on (it would've been nice if you'd sent us the XML, but was easy enough to replicate), then I see this when I go to my members page:
scr.png

Seems to work with no errors.
 

Jon W

Well-known member
#17
I've deleted it now, but I don't think the XML is the problem anyway. You can recreate it simply by deleting your add-on, creating it again and then adding the one listener.

Do you have problems with any other add-ons?
 
#18
I haven't encountered similar problems with any other add-on.
Im going to try it again and give some feedback afterwards

Edit: I tried it and it seems to work now, at least ist loading the template correctly but it is not replacing the avatars with the pictures it is supposed to
 

Jon W

Well-known member
#19
The problem seems like it is not with the actual code, but with the editor you are using or how you are uploading the file that is adding characters to the first line of the code. You might want to try different ways of editing it/uploading it or check your settings.
 

Jon W

Well-known member
#20
I haven't encountered similar problems with any other add-on.
Im going to try it again and give some feedback afterwards

Edit: I tried it and it seems to work now, at least ist loading the template correctly but it is not replacing the avatars with the pictures it is supposed to
I noticed that it didn't like usernames with spaces in on my test site. You might want to check this out:
http://php.net/manual/en/function.urlencode.php