<?php
class Andy_ConvertImage_DataWriter extends XFCP_Andy_ConvertImage_DataWriter
{
public function save()
{
// call parent function
parent::save();
// declare variable
$tempImageDirectoryCheck = '';
// check for imagemagick option
$imPecl = XenForo_Application::get('options')->imageLibrary['class'];
// get options from Admin CP -> Options -> Convert Image -> Temporary Image Directory
$temporaryImageDirectory = XenForo_Application::get('options')->temporaryImageDirectory;
// verify temporary directory exists
if (file_exists($temporaryImageDirectory))
{
$tempImageDirectoryCheck = 'yes';
}
//#####################################
// do basic checks before main loop
// imagemagick must be installed and
// temporary directory created
//#####################################
if ($imPecl == 'imPecl' AND $tempImageDirectoryCheck == 'yes')
{
// seacrh for [IMG] tag
$pos1 = stripos($this->get('message'), '[img]');
// run only if [IMG] tag is found
if (is_numeric($pos1))
{
// get current message
$currentMessage = $this->get('message');
// call mainLoop
$this->mainLoop($currentMessage);
}
}
}
public function mainLoop($currentMessage)
{
// define variables
$done = '';
$newMessage = '';
$updateRequired = '';
// start loop
while ($done != 'yes')
{
//#####################################
// parse the message that was just saved
// look for [IMG] and [/IMG] tags
// each loop will process one image tag
// when the entire message has been parsed
// the $done variable will be set to 'yes'
//#####################################
// clear variables for each loop
$imglink = '';
$posStart = '';
//#####################################
// find [IMG] and [/IMG] tags
//#####################################
// first loop
if ($newMessage == '') {
$original_message = $currentMessage;
}
// looping again
if ($newMessage != '') {
$original_message = $newMessage;
$currentMessage = $newMessage;
}
$pos1 = stripos($currentMessage, '[IMG]');
if ($pos1 === false) {
$done = 'yes';
}
// used later to replace with [ATTACH] code
if ($posStart == '') {
$posStart = $pos1;
}
// start at beginning of url
$pos1 = $pos1 + 5;
// delete $tempMessage up to beginning of image url
$currentMessage = substr($currentMessage, $pos1);
// get end of img tag
$pos1 = stripos($currentMessage, '[/IMG]');
if ($pos1 === false) {
$done = 'yes';
}
// $imglink will contain the link to the image
$imglink = substr($currentMessage, 0 , $pos1);
// only continue if we have an image link
if ($imglink != '' AND $done != 'yes')
{
//#####################################
// download image into temporary directory
//#####################################
// get options from Admin CP -> Options -> Convert Image -> Temporary Image Directory
$temporaryImageDirectory = XenForo_Application::get('options')->temporaryImageDirectory;
// define full path of download
$tempFullPath = $temporaryImageDirectory . 'convert' . '_' . rand();
if (!file_exists($tempFullPath)) {
touch($tempFullPath); // create blank file
chmod($tempFullPath,0777);
}
$ch = curl_init($imglink);
$fp = fopen($tempFullPath, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
// execute curl
curl_exec($ch);
if (curl_errno($ch) > 0)
{
$done = 'yes';
}
// close the file
fclose($fp);
//#####################################
// put image source into memory
// later we will save image to attachments directory
//#####################################
$tempImage = file_get_contents($tempFullPath);
//#####################################
// make sure we have a valid image
//#####################################
// get dimensions - supress error message
@list($width, $height) = getimagesize($tempFullPath);
// if not image abort
if (is_null($width))
{
$done = 'yes';
}
// only contunue if we have a valid image
if ($done != 'yes')
{
//#####################################
// resize image
//#####################################
// get options from Admin CP -> Options -> Attachments -> Maximum Attachment Image Dimensions
$attachmentMaxWidth = XenForo_Application::get('options')->attachmentMaxDimensions['width'];
// get options from Admin CP -> Options -> Attachments -> Maximum Attachment Image Dimensions
$attachmentMaxHeight = XenForo_Application::get('options')->attachmentMaxDimensions['height'];
// check if image width or height exceeds maximum allowed
if ($width > $attachmentMaxWidth OR $height > $attachmentMaxHeight) {
// resize only if width or height exceed maximum - (use the '\>' flag)
exec("/usr/bin/convert $tempFullPath -resize $attachmentMaxWidth x $attachmentMaxHeight\> $tempFullPath");
// get new image size
list($width, $height) = getimagesize($tempFullPath);
}
//#####################################
// begin creating the attachment
// prepare data for SQL query.
//#####################################
$currentUserId = XenForo_Visitor::getUserId();
$uploadDate = time();
$basename = basename($imglink);
// make sure basename fits 100 character limit
if (strlen($basename) > 100)
{
$strlen = strlen($basename);
$pos_start = $strlen - 100;
$basename = substr($basename,$pos_start);
}
$filesize = filesize($tempFullPath);
$filehash = hash_file('md5', $tempFullPath);
$currentPostId = $this->get('post_id');
//#####################################
// run SQL query (attachment_data)
//#####################################
$db = XenForo_Application::get('db');
$db->query("
INSERT INTO xf_attachment_data
(user_id, upload_date, filename, file_size, file_hash, width, height, thumbnail_width, thumbnail_height, attach_count)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
", array($currentUserId, $uploadDate, $basename, $filesize, $filehash, $width, $height, '0', '0', '1'));
// get the new data_id number (auto incremented)
$dataId = $db->lastInsertId();
//#####################################
// run SQL query (attachment)
//#####################################
$db->query("
INSERT INTO xf_attachment
(data_id, content_type, content_id, attach_date, temp_hash, unassociated, view_count)
VALUES
(?, ?, ?, ?, ?, ?, ?)
", array($dataId, 'post', $currentPostId, $uploadDate, '', '0', '0'));
// get the new attachment_id number (auto incremented)
$attachmentId = $db->lastInsertId();