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->_registry, ERRTYPE_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($forumId, explode(',', $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($url, PHP_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.