Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 09-30-2012, 06:51 PM
Twilkey's Avatar
Twilkey Twilkey is offline
 
Join Date: Oct 2006
Posts: 314
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07: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.03367 seconds
  • Memory Usage 2,235KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete