1. 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

Discussion in 'XenForo Development Discussions' started by MPhil, Mar 22, 2013.

  1. MPhil

    MPhil New Member

    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.
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
    MPhil likes this.
  3. MPhil

    MPhil New Member

    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($urltrue);
            }
     
            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($baimg3);
                
    imageline($baimg00$imgsize[0], $imgsize[1], $gray);
                
    imageline($baimg0$imgsize[1], $imgsize[0], 0$gray);
                
    imagefilter($baimgIMG_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($baimg3);
                
    imageline($baimg00$imgsize[0], $imgsize[1], $gray);
                
    imageline($baimg0$imgsize[1], $imgsize[0], 0$gray);
                
    imagefilter($baimgIMG_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}";
        }
     
    }
    ?>
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Any related errors in the log?

    Admin CP -> Tools -> Server Error Log
     
  5. MPhil

    MPhil New Member

    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) {
    }
    }
     
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Looks like extra white space before <?php at the top of the file.
     
  7. MPhil

    MPhil New Member

    Seriously?! I'm going to check that ASAP. Thanks.

    Edit: Apparantly, there is no extra white space. :/
     
  8. Jon W

    Jon W Well-Known Member

    Maybe the white space at the end? There is no need to put a ?> at the end.
     
  9. MPhil

    MPhil New Member

    Removed the ?> but the error still occurs.
     
  10. cclaerhout

    cclaerhout Well-Known Member

    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).
     
  11. Jon W

    Jon W Well-Known Member

    cclaerhout likes this.
  12. cclaerhout

    cclaerhout Well-Known Member

    Jon W likes this.
  13. Jon W

    Jon W Well-Known Member

    I've always assumed the same as you until I spotted this recently. I always use single quotes anyway though to avoid making mistakes.
     
    cclaerhout likes this.
  14. MPhil

    MPhil New Member

    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.
     
  15. Jon W

    Jon W Well-Known Member

    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.
     
  16. MPhil

    MPhil New Member

    Well it seems it is because of the XML file, could you please post the one you created here so i can check the differences?
     
  17. Jon W

    Jon W Well-Known Member

    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. MPhil

    MPhil New Member

    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
     
  19. Jon W

    Jon W Well-Known Member

    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.
     
  20. Jon W

    Jon W Well-Known Member

    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
     

Share This Page