vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Issue auto converting image links to attachments (https://vborg.vbsupport.ru/showthread.php?t=288507)

Twilkey 09-30-2012 06:51 PM

Issue auto converting image links to attachments
 
Just as the title says, I am having issues converting remote image links to attachments. I am trying to port a vB 3 product to vB 4. The below code pulls the URLs just fine, but during process_upload an error is thrown.

The error is "Fatal error: Field thumbnail_dateline is not defined in $validfields in class vB_DataManager_Attachment in [path]/includes/class_dm.php on line 515".

I have gone through many of the core files to see if I could figure out what is wrong, but nothing I have done has fixed the issue. SO I am going to try to see if any of you can tell what is wrong. Below is the code from the class file used.

PHP Code:

<?php

class vB_AutoAttach {
    
/**
     * @var        vB_Registry
     */
    
protected $_registry;
    
    
/**
     * Brings vBulletin registry into scope
     * @param    vB_Registry
     */
    
public function __construct(vB_Registry $registry) {
        
$this->_registry $registry;
    }
    
    
/**
     * Automatically attaches all IMG tags in a new post, when applicable
     * @param    vB_DataManager_ThreadPost        DM for new post
     */
    
public function process(vB_DataManager_ThreadPost $dm) {
        if (
$dm instanceof vB_DataManager_Thread_FirstPost) {
            
$forumId $dm->fetch_field('forumid');
        } else {
            
$forumId $dm->info['forum']['forumid'];
        }
        
        if (!
$this->_activeInForum($forumId)) {
            return;
        }
        
        if (
$dm->info['posthash'] == 'invalid posthash') {
            
$dm->info['posthash'] = md5(
                
$this->_registry->userinfo['userid'] .
                
uniqid() .
                
$this->_registry->userinfo['salt']
            );
        }
        
        
        
$errors   = array();
        
$postinfo = array('posthash' => $dm->info['posthash']);
        
$userinfo $this->_registry->userinfo;
        
        foreach (
$this->_getImages($dm->fetch_field('pagetext''post')) as $i => $match) {
            list(
$matchedText$imageUrl) = $match;
            if (!
$this->_isAttachable($imageUrl)) {
                continue;
            }
            
            
$this->_registry->GPC['attachment']['url'][$i]   = true;  
            
$this->_registry->GPC['attachment']['name'][$i]  = $imageUrl;
            
$this->_registry->GPC['attachment']['match'][$i] = $matchedText;
        }
        
        
$replacements = array();
        
$uploadsum    count($this->_registry->GPC['attachment']['name']);
        
        for (
$i 0$i $uploadsum$i++) {
            require_once(
DIR '/includes/class_upload.php');
            require_once(
DIR '/includes/class_image.php');
            
            
$attachdata datamanager_init('Attachment'$this->_registryERRTYPE_ARRAY);
            
$upload     = new vB_Upload_Attachment($this->_registry);
            
$image      vB_Image::fetch_library($this->_registry);
            
            
$upload->data  $attachdata;
            
$upload->image $image;

            if (
$uploadsum 1) {
                
$upload->emptyfile false;
            }
            
            
$upload->foruminfo fetch_foruminfo($forumId);
            
$upload->postinfo  $postinfo;
            
            
$attachment $this->_registry->GPC['attachment']['name'][$i];
            
            if (
$attachmentId $upload->process_upload($attachment)) {
                if (
$attachmentId === true) {
                    
$attachmentId $upload->data->fetch_field('attachmentid');
                }

                
$replacements[$this->_registry->GPC['attachment']['match'][$i]] = sprintf(
                    
'[IMG]%s/attachment.php?attachmentid=%s&dateline=%d[/IMG]',
                    
$this->_registry->options['bburl'],
                    
$attachmentId,
                    
$upload->data->fetch_field('dateline')
                );
            }
        }
        
        if (
$replacements) {
            
$dm->set('pagetext'str_replace(
                
array_keys($replacements), 
                
array_values($replacements),
                
$dm->fetch_field('pagetext''post')
            ));
        }
        
        if (
$dm->info['posthash'])
        {
            
$dm->info['newattach'] = $dm->fetch_attachment_count($dm->info['posthash'], $dm->fetch_field('userid''post'));
            
$dm->set('attach',
                
intval($dm->fetch_field('attach')) +
                
$dm->info['newattach']
            );
        }
    }
    
    
/**
     * Fetches all of the images from a given message
     * @param    string        Message containing BBCode
     * @return    array        Matched images (see _rotateMatches)
     */
    
protected function _getImages($message) {
        
$matches null;
        
preg_match_all('#\[img\]\s*(https?://([^*\r\n]+|[a-z0-9/\\._\- !]+))\[/img\]#iU'$message$matches);
        return 
$this->_rotateMatches($matches);
    }
    
    
/**
     * Rotates the preg_match_all results array
     * @param    array        Matches
     * @return    array        Rotated matches
     */
    
protected function _rotateMatches(array $matches) {
        
$rotated = array();
        
        for (
$i 0$i count($matches); $i++) {
            for (
$j 0$j count($matches[$i]); $j++) {
                
$rotated[$j][$i] = $matches[$i][$j];
            }
        }
        
        return 
$rotated;
    }
    
    
/**
     * Checks to see if auto attaching is active for this forum
     * @param    integer        Forum ID to check
     * @return    boolean        True if active in this forum
     */
    
protected function _activeInForum($forumId) {
        if (!
in_array($forumIdexplode(','$this->_registry->options['autoattach_forums']))) {
            return 
false;
        }
        
        return 
true;
    }
    
    
/**
     * Checks to see if an image URL is attachable 
     * Checks file extension and hostname
     * @param    string        Image URL
     * @return    boolean        True if image is attachable
     */
    
protected function _isAttachable($url) {
        if (!
in_array(strtolower(str_replace('.'''file_extension($url))), explode(','$this->_registry->options['autoattach_extensions']))) {
            return 
false;
        }
        
        foreach (
explode("\n"$this->_registry->options['autoattach_blacklist']) as $blacklisted) {
            if (
stripos(parse_url($urlPHP_URL_HOST), $blacklisted) !== false) {
                return 
false;
            }
        }
        
        return 
true;
    }
}

Also, just in case, here are the 2 plugins file used to include the class.

Hookname - postdata_presave
PHP Code:

require_once(DIR '/includes/class_autoattach.php');
if (!
$autoAttach $this->registry->autoattach) {
    
$autoAttach $this->registry->autoattach = new vB_AutoAttach($this->registry);
}

$autoAttach->process($this); 

Hookname - threadfpdata_presave
PHP Code:

require_once(DIR '/includes/class_autoattach.php');
if (!
$autoAttach $this->registry->autoattach) {
    
$autoAttach $this->registry->autoattach = new vB_AutoAttach($this->registry);
}

$autoAttach->process($this); 

--------------- Added [DATE]1349072286[/DATE] at [TIME]1349072286[/TIME] ---------------

I have a feeling that this code may be the wrong way to handle attachments in vB 4, so if anyone has an example of the new method vB uses to handle attachments, that would be good.


All times are GMT. The time now is 02:08 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01045 seconds
  • Memory Usage 1,796KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (1)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete