problem with overriding icons

Hi all,

Spent all day trying to debug a problem and no luck. I hope you can help.

I have an object of subtype hjfile, and have written hooks (both through pagehandler like profile icon and without like file plugin). Neither seems to work. Moreover, whenever the plugin is enabled, all icons that were previously uploaded disappearing.

I can't locate the source of the problem. It almost seems as if I reset the filestore path or something... Maybe you can help...

 

HOOK

$file = $params['entity'];
    $size = $params['size'];
   
    if ($file->getSubtype() == 'hjfile') {
        if ($file->tinythumb) {
            return "mod/hypeFramework/fileicon.php?guid={$file->guid}&size={$size}";
        }

        $mapping = hj_framework_mime_mapping();

        $mime = $file->mimetype;
        if ($mime) {
            $base_type = substr($mime, 0, strpos($mime, '/'));
        } else {
            $mime = 'none';
            $base_type = 'none';
        }

        if (isset($mapping[$mime])) {
            $type = $mapping[$mime];
        } elseif (isset($mapping[$base_type])) {
            $type = $mapping[$base_type];
        } else {
            $type = 'general';
        }

        $url = "mod/hypeFramework/graphics/mime/{$size}/{$type}.png";
        return $url;
    }

return $return

 

FILEICON.PHP

<?php

// Get engine
require_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");

$file_guid = (int) get_input('guid', 0);
$size = get_input('size', 'small');
$file = get_entity($file_guid);
if (!$file || $file->getSubtype() != "hjfile") {
    exit;
}
$simpletype = $file->simpletype;
if ($simpletype == "image") {

    // Get file thumbnail
    switch ($size) {
        case "tiny" :
            $thumbfile = $file->tinythumb;
            break;
        case "small" :
            $thumbfile = $file->smallthumb;
            break;
        case "medium":
        default :
            $thumbfile = $file->mediumthumb;
            break;
        case "large":
            $thumbfile = $file->largethumb;
            break;
    }

    if ($thumbfile && !empty($thumbfile)) {
        $readfile = new ElggFile();
        $readfile->owner_guid = $file->owner_guid;
        $readfile->setFilename($thumbfile);
        $contents = $readfile->grabFile();


        // caching images for 10 days
        header("Content-type: image/jpeg");
        header('Expires: ' . date('r', time() + 864000));
        header("Pragma: public", true);
        header("Cache-Control: public", true);
        header("Content-Length: " . strlen($contents));

        echo $contents;
        exit;
    }
}

THUMB GEN

// Just in case we want to upload a newer version of the file in the future
                            if ($file_guid = get_input("{$field_name}_guid")) {
                                $existing_file = true;
                                $file_guid = (int) $file_guid;
                            } else {
                                $existing_file = false;
                                $file_guid = null;
                            }

                            if (!$file_title = get_input("{$field_name}_title")) {
                                $file_title = get_input('title');
                            }
                            if (!$file_description = get_input("{$field_name}_description")) {
                                $file_description = get_input('description');
                            }
                            if (!$file_tags = get_input("{$field_name}_tags")) {
                                $file_tags = get_input('tags');
                            }
                            $filehandler = new hjFile($file_guid);
                            $filehandler->owner_guid = elgg_get_logged_in_user_guid();
                            $filehandler->container_guid = $filefolder->guid;
                            $filehandler->access_id = $filefolder->access_id;
                            $filehandler->title = $file_title;
                            $filehandler->description = $file_description;
                            $filehandler->tags = $file_tags;

                            $prefix = "hjfile/";

                            if ($existing_file) {
                                $filename = $filehandler->getFilenameOnFilestore();
                                if (file_exists($filename)) {
                                    unlink($filename);
                                }
                                $filestorename = $filehandler->getFilename();
                                $filestorename = elgg_substr($filestorename, elgg_strlen($prefix));
                            } else {
                                $filestorename = elgg_strtolower($file['name']);
                            }

                            $filehandler->setFilename($prefix . $filestorename);
                            $filehandler->setMimeType($file['type']);
                            $filehandler->originalfilename = $file['name'];
                            $filehandler->simpletype = file_get_simple_type($file['type']);

                            $filehandler->open("write");
                            $filehandler->close();
                            move_uploaded_file($file['tmp_name'], $filehandler->getFilenameOnFilestore());

                            $file_guid = $filehandler->save();

                            hj_framework_set_entity_priority($filehandler);

                            if ($file_guid) {
                                $formSubmission->$field_name = $filehandler->getGUID();
                            } else {
                                $formSubmission->$field_name = $filehandler->getFilenameOnFilestore();
                            }

                            if ($file_guid && $filehandler->simpletype == "image") {

                                $thumb_sizes = array(
                                    'tiny' => 25,
                                    'small' => 40,
                                    'medium' => 100,
                                    'large' => 250,
                                    'preview' => 600,
                                );

                                foreach ($thumb_sizes as $thumb_type => $thumb_size) {
                                    $thumbnail = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $thumb_size, $thumb_size, true);
                                    if ($thumbnail) {
                                        $thumb = new ElggFile();
                                        $thumb->setMimeType($file['type']);

                                        $thumb->setFilename("{$prefix}thumb{$thumb_type}_{$filestorename}");
                                        $thumb->open("write");
                                        $thumb->write($thumbnail);
                                        $thumb->close();

                                        $thumb_meta = "{$thumb_type}thumb";
                                        $filehandler->$thumb_meta = "{$prefix}thumb{$thumb_type}_{$filestorename}";
                                        unset($thumbnail);
                                    }
                                }
                            }

 

All files appear in the uploads folder as expected. Src looks fine as well. The problems seems to be with headers? Or am I missing something?

Any help is appreciated

  • Btw, by 'all icons uploaded' I mean that profile icons, and file icons of objects that were created previously, and are created afterwards, are not showing...

  • I am giving up. Can't seem to find the source of the problem.

    Trying a million different variations. Tried local WAMP and shared hosting. Tried outputing using content-type and location. Tried removing container_guids etc etc etc. Nothing seems to work...

  • Hi Ismayil, I had a problem with random icons and pictures not showing up after a server move and fixed it with the Time Zone mod described here http://docs.elgg.org/wiki/No_images I might be off base but maybe it is related to your concern. 

    Steve

  • After 3 days of painful debugging, I have finally located the source of the problem. No matter how stupid this sounds, this cost me days of headache. So I hope this helps others in the future.

    DO NOT LEAVE RETURNS BETWEEN ?> <?PHP TAGS AND DO NOT ADD EXTRA RETURNS AT THE BEGINNING OF YOUR PHP FILES.

    I found out the hard way. While all other functionalities work fine, rendering of icons is disrupted as these returns are added above the php headers. How annoying.

    To the elgg team - perhaps you could add this to documentation. And if there is a possibility to clear those line breaks before rendering the icons, that would perahps help.

  • ihayredinov, it seems you were a victim of a famous "headers already sent" bug. It causes icons to not to show and causes redirects to stop working. I spent a long time on this bug some time in the past as well ( and someone from my team just had it today... ). More annoying version is when you save file with BOM encoding - then the char added is invisible.

     

    It's also good to not to put ?> at the end of PHP files.

  • I wish you were here 3 days ago ;) 

    I didn't even think this would be the cause of the problem, but alas. Anyhow, I will be smarter in the future ;)