Resource icon

MetaMirror 1.6.1

No permission to download
If you have some code sample for how to add an attachment (either from a local file or a string) to a post, then I could roll it in pretty quick.

I haven't had time to look into the code but sniffing on the importer files (i love to reading importers because you can learn so much!) I took the code that imports attachments and came up with this method (not my code, I just took it and removed unnecessary lines):

Watch out for:
  • $this->_getDb(): I don't know where you'll use this code (probably a model) so I just left it unmodified. This code is from a model so the class where I took it extends XenForo_Model. If you use it somewhere else, make sure to use a proper database object.
PHP:
    public function importPostAttachment($fileName, $tempFile, $userId, $postId, $date, array $attach = array())
    {
        $upload = new XenForo_Upload($fileName, $tempFile);
   
        try
        {
            $dataExtra = array('upload_date' => $date, 'attach_count' => 1);
            $dataId = $this->getModelFromCache('XenForo_Model_Attachment')->insertUploadedAttachmentData($upload, $userId, $dataExtra);
        }
        catch (XenForo_Exception $e)
        {
            return false;
        }
   
        $dw = XenForo_DataWriter::create('XenForo_DataWriter_Attachment');
        //$dw->setImportMode(true);
   
        $dw->bulkSet(array(
                'data_id' => $dataId,
                'content_type' => 'post',
                'content_id' => $postId,
                'attach_date' => $date,
                'unassociated' => 0
        ));
        $dw->bulkSet($attach);
   
        $dw->save();
   
        $newAttachmentId = $dw->get('attachment_id');
   
        $this->_getDb()->query('
                UPDATE xf_post SET
                attach_count = IF(attach_count < 65535, attach_count + 1, 65535)
                WHERE post_id = ?
                ', $postId);
   
        return $newAttachmentId;
    }
 
Last edited:
Mick West updated MetaMirror with a new update entry:

Handles absolute External Data URL

If you have a CDN set up and reference via something like:

$config['externalDataUrl'] = 'http://example.com/data';

Then MetaMirror will now detect this, and handle renaming the URL correctly.

It will also automatically add the externalDataUrl string to the list of ignore strings if it is absolute. So you won't get in a loop of re-hosting your CDN files

Read the rest of this update entry...
 
One problem with using attachments is that a lot of the time people will cut and paste in a section of a web page that has a lot of tiny images, like arrows and buttons, smilies, facebook profile images, and even blank spacers. So if they are all rehosted as attachments you are going to get a lot of duplicates of tiny meaningless images in your attachment manager.

Example 645 byte image.
3_14_up.png


A possible solution would be to only use attachments for files over a certain size, so only useful/interesting images get made into attachments.
 
One problem with using attachments is that a lot of the time people will cut and paste in a section of a web page that has a lot of tiny images, like arrows and buttons, smilies, facebook profile images, and even blank spacers. So if they are all rehosted as attachments you are going to get a lot of duplicates of tiny meaningless images in your attachment manager.

Example 645 byte image.
3_14_up.png


A possible solution would be to only use attachments for files over a certain size, so only useful/interesting images get made into attachments.
Humm... Another option could be to attach images with larger dimensions than the default thumbnail dimensions. Something like images larger than 100x100.
 
One problem with using attachments is that a lot of the time people will cut and paste in a section of a web page that has a lot of tiny images, like arrows and buttons, smilies, facebook profile images, and even blank spacers. So if they are all rehosted as attachments you are going to get a lot of duplicates of tiny meaningless images in your attachment manager.

Hi Mike,

There's really no such thing as a meaningless image. For example one small smilie can change the entire tone of a post.

Curious, how you set up the path or folder the images are stored in? With Xenforo, attachments folders contains a maximum of 1000 attachments, this is an important feature because you don't want a folder with tens of thousands of files in it.
 
Hi Mike,

There's really no such thing as a meaningless image. For example one small smilie can change the entire tone of a post.
I mean they would be meaningless in the attachments manager. Just a lot of clutter - so it's not that useful to have them as attachments. I'd suggest maybe (optionally) using the direct file caching for small images and converting to attachments for the large images. Although you'd still get the issue of the small files being left behind if a post is deleted.

Do you know if there is some kind of reference counting for attachments, to determine if they can be deleted?

Curious, how you set up the path or folder the images are stored in? With Xenforo, attachments folders contains a maximum of 1000 attachments, this is an important feature because you don't want a folder with tens of thousands of files in it.

It's just a single folder under data/ right now. I guess I could add an option to split it up, maybe based on sanitized remote host names. Personally I like it all in one large folder, but that's probably because I'm just a small forum guy (there were under 2000 images that needed rehosting). What are the problems with lots of images in a single folder?
 
I mean they would be meaningless in the attachments manager. Just a lot of clutter - so it's not that useful to have them as attachments. I'd suggest maybe (optionally) using the direct file caching for small images and converting to attachments for the large images. Although you'd still get the issue of the small files being left behind if a post is deleted.

Do you know if there is some kind of reference counting for attachments, to determine if they can be deleted?

I'm sorry, I don't know what you mean by attachment manager. Is this something your add-on provides?

It's just a single folder under data/ right now. I guess I could add an option to split it up, maybe based on sanitized remote host names. Personally I like it all in one large folder, but that's probably because I'm just a small forum guy (there were under 2000 images that needed rehosting). What are the problems with lots of images in a single folder?

The biggest downside to having tens of thousands of files in a single folder is that if you click a folder that contains the files using a program like Dreamweaver or Webmin, it takes a very long time before you can see all the files.
 
Last edited:
@Mick West

Currently you can add all the clutter manually with attachments anyway :rolleyes: , and when you amend the post , it'll still show as image. If you show full image or thumbnial , you don't get the image list at the bottom.

When cron runs all you need to return is
Code:
[ATTACH]52512[/ATTACH]

View attachment 52512

See http://xenforo.com/community/threads/posting-an-attachment.55941/#post-596042

So won't look any different in thread/post, but easier to manage.

like I've said before, if it is an attachment like standard xenforo, you needn't have to develop/fix things for cdn/image management etc.

Still think you've done an awesome job by releasing this for free though(y)
 
Just a note - this is still open to exploits even with the exif_imagetype fix. It's quite easy to embed php code inside an image that would pass that check.

Either .php (and any other php-enabled extensions, such as .html) needs to be in the ignore list by default or users need to disable php execution in /data
 
I'm sorry, I don't know what you mean by attachment manager. Is this something your add-on provides?
I meant the Attachment Browser, in the ACP under Applications

The biggest downside to having tens of thousands of files in a single folder is that if you click a folder that contains the files using a program like Dreamweaver or Webmin, it takes a very long time before you can see all the files.

I'll put it on the list :)
 
Just a note - this is still open to exploits even with the exif_imagetype fix. It's quite easy to embed php code inside an image that would pass that check.

Either .php (and any other php-enabled extensions, such as .html) needs to be in the ignore list by default or users need to disable php execution in /data

That would prevent caching of dynamically generated images (which you may or may not want, depending on your site),

What if I were to just remove any non-image file extension from the cached filename? So "nastyphp.php becomes nastyphp_php"? That way if it's a valid image it will still display. But if it's got php in it it will not execute.
 
I've been using simple_html_dom and it uses getimagesize which i believe is a good way to ensure it's an image (but you guys would know better)

Code:
if ($curlErrorCode === 0) {

            $info = curl_getinfo ( $curl_array [$i] );

            $ext = getExtention ( $info ['content_type'] );

            if ($info ['content_type'] !== null) {

                $temp = "temp/img" . md5 ( mt_rand () ) . $ext;

                touch ( $temp );

                $imageContent = curl_multi_getcontent ( $curl_array [$i] );

                file_put_contents ( $temp, $imageContent );

                if ($maxHeight == 0 || $maxWidth == 0) {

                    $res [] = $temp;

                } else {

                    $size = getimagesize ( $temp );

                    if ($size [0] >= $maxHeight && $size [0] >= $maxWidth) {

                        $res [] = $temp;

                    } else {

                        unlink ( $temp );

                    }

                }

            }

        }
 
Top Bottom