public function renderRaw() { $url = $this->_params['url']; $width = $this->_params['width']; $height = $this->_params['height']; $crop = $this->_params['crop']; $extension = XenForo_Helper_File::getFileExtension($url); $imageInfo = @getimagesize($url); if (!$imageInfo || !in_array($imageInfo[2], array_values(sonnb_XenGallery_Model_PhotoData::$typeMap)) || !in_array(strtolower($extension), array_keys(sonnb_XenGallery_Model_PhotoData::$imageMimes))) { $url = XenForo_Template_Helper_Core::getAvatarUrl(array(), 'l'); $extension = XenForo_Helper_File::getFileExtension($url); $imageInfo = @getimagesize($url); } $this->_response->setHeader('Content-type', sonnb_XenGallery_Model_PhotoData::$imageMimes[$extension], true); $this->_response->setHeader('ETag', XenForo_Application::$time, true); $this->_response->setHeader('X-Content-Type-Options', 'nosniff'); $this->setDownloadFileName($url, true); $image = XenForo_Image_Abstract::createFromFile($url, sonnb_XenGallery_Model_PhotoData::$typeMap[$extension]); if ($image) { if (XenForo_Image_Abstract::canResize($width, $height)) { if ($crop) { $image->thumbnail($width * 2, $height * 2); $image->crop(0, 0, $width, $height); } else { $image->thumbnail($width, $height); } } else { $image->output(sonnb_XenGallery_Model_PhotoData::$typeMap[$extension]); } } }
public function applyCover($teamId, $fileName, $imageType = false, $width = false, $height = false, $existingCoverDate = 0) { if (!$imageType || !$width || !$height) { $imageInfo = @getimagesize($fileName); if (!$imageInfo) { throw new Nobita_Teams_Exception_Abstract('Non-image passed in to applyCover'); } $width = $imageInfo[0]; $height = $imageInfo[1]; $imageType = $imageInfo[2]; } if (!in_array($imageType, array(IMAGETYPE_JPEG, IMAGETYPE_PNG))) { throw new Nobita_Teams_Exception_Abstract('Invalid image type passed in to applyCover'); } if (!XenForo_Image_Abstract::canResize($width, $height)) { throw new Nobita_Teams_Exception_Abstract(new XenForo_Phrase('uploaded_image_is_too_big'), true); } $maxFileSize = XenForo_Application::getOptions()->Teams_coverFileSize; if ($maxFileSize && filesize($fileName) > $maxFileSize) { @unlink($fileName); throw new XenForo_Exception(new XenForo_Phrase('Teams_your_cover_file_size_large_smaller_x', array('size' => XenForo_Locale::numberFormat($maxFileSize, 'size'))), true); } if ($existingCoverDate) { $existedFile = $this->getCoverCropFilePath($teamId, $existingCoverDate); @unlink($existedFile); } $this->_witerCoverPhoto($teamId, $fileName); @unlink($fileName); $dwData = array('cover_date' => XenForo_Application::$time, 'cover_crop_details' => array('height' => $height, 'width' => $width)); $dw = XenForo_DataWriter::create('Nobita_Teams_DataWriter_Team'); $dw->setExistingData($teamId); $dw->bulkSet($dwData); $dw->save(); return $dwData; }
public function execute(array $deferred, array $data, $targetRunTime, &$status) { $data = array_merge(array('batch' => 100, 'position' => 0), $data); /* @var $attachmentModel XenForo_Model_Attachment */ $attachmentModel = XenForo_Model::create('XenForo_Model_Attachment'); $s = microtime(true); $dataIds = $attachmentModel->getAttachmentDataIdsInRange($data['position'], $data['batch']); if (sizeof($dataIds) == 0) { return false; } foreach ($dataIds as $dataId) { $data['position'] = $dataId; $dw = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData', XenForo_DataWriter::ERROR_SILENT); if ($dw->setExistingData($dataId) && $dw->get('width') && XenForo_Image_Abstract::canResize($dw->get('width'), $dw->get('height'))) { $attach = $dw->getMergedData(); $attachFile = $attachmentModel->getAttachmentDataFilePath($attach); $imageInfo = @getimagesize($attachFile); if ($imageInfo) { try { $image = XenForo_Image_Abstract::createFromFile($attachFile, $imageInfo[2]); } catch (Exception $e) { XenForo_Error::logException($e, false, "Thumbnail rebuild error {$attachFile}: "); continue; } if ($image) { if ($image->thumbnail(XenForo_Application::get('options')->attachmentThumbnailDimensions)) { ob_start(); $image->output($imageInfo[2]); $thumbData = ob_get_contents(); ob_end_clean(); } else { // no resize necessary, use the original $thumbData = file_get_contents($attachFile); } $dw->set('thumbnail_width', $image->getWidth()); $dw->set('thumbnail_height', $image->getHeight()); $dw->setExtraData(XenForo_DataWriter_AttachmentData::DATA_THUMB_DATA, $thumbData); try { $dw->save(); } catch (Exception $e) { XenForo_Error::logException($e, false, "Thumb rebuild for #{$dataId}: "); } unset($image); } } } if ($targetRunTime && microtime(true) - $s > $targetRunTime) { break; } } $actionPhrase = new XenForo_Phrase('rebuilding'); $typePhrase = new XenForo_Phrase('attachment_thumbnails'); $status = sprintf('%s... %s (%s)', $actionPhrase, $typePhrase, XenForo_Locale::numberFormat($data['position'])); return $data; }
/** * * @see XenResource_Model_Resource::applyResourceIcon() * */ public function applyResourceIcon($resourceId, $fileName, $imageType = false, $width = false, $height = false) { if (!$imageType || !$width || !$height) { $imageInfo = getimagesize($fileName); if (!$imageInfo) { return parent::applyResourceIcon($resourceId, $fileName, $imageType, $width, $height); } $width = $imageInfo[0]; $height = $imageInfo[1]; $imageType = $imageInfo[2]; } if (!in_array($imageType, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { return parent::applyResourceIcon($resourceId, $fileName, $imageType, $width, $height); } if (!XenForo_Image_Abstract::canResize($width, $height)) { return parent::applyResourceIcon($resourceId, $fileName, $imageType, $width, $height); } $imageQuality = self::$iconQuality; $outputType = $imageType; $image = XenForo_Image_Abstract::createFromFile($fileName, $imageType); if (!$image) { return false; } $maxDimensions = max(array($image->getWidth(), $image->getHeight())); if ($image->getOrientation() != XenForo_Image_Abstract::ORIENTATION_SQUARE) { $x = floor(($maxDimensions - $image->getWidth()) / 2); $y = floor(($maxDimensions - $image->getHeight()) / 2); $image->resizeCanvas($x, $y, $maxDimensions, $maxDimensions); } $newTempFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf'); if (!$newTempFile) { return false; } $image->output($outputType, $newTempFile, $imageQuality); unset($image); $returnValue = parent::applyResourceIcon($resourceId, $newTempFile, $imageType, $width, $height); if ($returnValue) { $filePath = $this->getResourceIconFilePath($resourceId); $image = XenForo_Image_Abstract::createFromFile($filePath, $imageType); if (!$image) { return false; } $width = XenForo_Application::get('options')->th_resourceIcons_width; $height = XenForo_Application::get('options')->th_resourceIcons_height; $x = floor(($width - $image->getWidth()) / 2); $y = floor(($height - $image->getHeight()) / 2); $image->resizeCanvas($x, $y, $width, $height); $image->output($outputType, $filePath, $imageQuality); unset($image); } }
/** * Inserts uploaded attachment data. * * @param XenForo_Upload $file Uploaded attachment info. Assumed to be valid * @param integer $userId User ID uploading * @param array $extra Extra params to set * * @return integer Attachment data ID */ public function insertUploadedAttachmentData(XenForo_Upload $file, $userId, array $extra = array()) { if ($file->isImage() && XenForo_Image_Abstract::canResize($file->getImageInfoField('width'), $file->getImageInfoField('height'))) { $dimensions = array('width' => $file->getImageInfoField('width'), 'height' => $file->getImageInfoField('height')); $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf'); if ($tempThumbFile) { $image = XenForo_Image_Abstract::createFromFile($file->getTempFile(), $file->getImageInfoField('type')); if ($image) { if ($image->thumbnail(XenForo_Application::get('options')->attachmentThumbnailDimensions)) { $image->output($file->getImageInfoField('type'), $tempThumbFile); } else { copy($file->getTempFile(), $tempThumbFile); // no resize necessary, use the original } $dimensions['thumbnail_width'] = $image->getWidth(); $dimensions['thumbnail_height'] = $image->getHeight(); unset($image); } } } else { $tempThumbFile = ''; $dimensions = array(); } try { $dataDw = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData'); $dataDw->bulkSet($extra); $dataDw->set('user_id', $userId); $dataDw->set('filename', $file->getFileName()); $dataDw->bulkSet($dimensions); $dataDw->setExtraData(XenForo_DataWriter_AttachmentData::DATA_TEMP_FILE, $file->getTempFile()); if ($tempThumbFile) { $dataDw->setExtraData(XenForo_DataWriter_AttachmentData::DATA_TEMP_THUMB_FILE, $tempThumbFile); } $dataDw->save(); } catch (Exception $e) { if ($tempThumbFile) { @unlink($tempThumbFile); } throw $e; } if ($tempThumbFile) { @unlink($tempThumbFile); } // TODO: add support for "on rollback" behavior return $dataDw->get('data_id'); }
/** * Applies the avatar file to the specified user. * * @param integer $teamId * @param string $fileName * @param constant|false $imageType Type of image (IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG) * @param integer|false $width * @param integer|false $height * * @return array */ public function applyAvatar($teamId, $fileName, $imageType = false, $width = false, $height = false) { if (!$imageType || !$width || !$height) { $imageInfo = getimagesize($fileName); if (!$imageInfo) { throw new Nobita_Teams_Exception_Abstract('Non-image passed in to applyAvatar'); } $width = $imageInfo[0]; $height = $imageInfo[1]; $imageType = $imageInfo[2]; } if (!in_array($imageType, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { throw new Nobita_Teams_Exception_Abstract('Invalid image type passed in to applyAvatar'); } if (!XenForo_Image_Abstract::canResize($width, $height)) { throw new Nobita_Teams_Exception_Abstract(new XenForo_Phrase('uploaded_image_is_too_big'), true); } $maxFileSize = XenForo_Application::getOptions()->Teams_avatarFileSize; if ($maxFileSize && filesize($fileName) > $maxFileSize) { @unlink($fileName); throw new Nobita_Teams_Exception_Abstract(new XenForo_Phrase('your_avatar_file_size_large_smaller_x', array('size' => XenForo_Locale::numberFormat($maxFileSize, 'size'))), true); } // should be use 280x280px because of grid style $maxDimensions = 280; $imageQuality = intval(Nobita_Teams_Setup::getInstance()->getOption('logoQuality')); $outputType = $imageType; $image = XenForo_Image_Abstract::createFromFile($fileName, $imageType); if (!$image) { return false; } $image->thumbnailFixedShorterSide($maxDimensions); if ($image->getOrientation() != XenForo_Image_Abstract::ORIENTATION_SQUARE) { $cropX = floor(($image->getWidth() - $maxDimensions) / 2); $cropY = floor(($image->getHeight() - $maxDimensions) / 2); $image->crop($cropX, $cropY, $maxDimensions, $maxDimensions); } $newTempFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf'); if (!$newTempFile) { return false; } $image->output($outputType, $newTempFile, $imageQuality); unset($image); $filePath = $this->getAvatarFilePath($teamId); $directory = dirname($filePath); if (XenForo_Helper_File::createDirectory($directory, true) && is_writable($directory)) { if (file_exists($filePath)) { @unlink($filePath); } $writeSuccess = XenForo_Helper_File::safeRename($newTempFile, $filePath); if ($writeSuccess && file_exists($newTempFile)) { @unlink($newTempFile); } } else { $writeSuccess = false; } $date = XenForo_Application::$time; if ($writeSuccess) { $dw = XenForo_DataWriter::create('Nobita_Teams_DataWriter_Team'); $dw->setExistingData($teamId); $dw->set('team_avatar_date', $date); $dw->save(); } return $writeSuccess ? $date : 0; }
public function applyCategoryIcon($categoryId, $fileName, $imageType = false, $width = false, $height = false) { if (!$imageType || !$width || !$height) { $imageInfo = getimagesize($fileName); if (!$imageInfo) { throw new XenForo_Exception('Non-image passed in to applyCategoryIcon'); } $width = $imageInfo[0]; $height = $imageInfo[1]; $imageType = $imageInfo[2]; } if (!in_array($imageType, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true); } if (!XenForo_Image_Abstract::canResize($width, $height)) { throw new XenForo_Exception(new XenForo_Phrase('uploaded_image_is_too_big'), true); } $maxDimensions = self::$iconSize; $imageQuality = self::$iconQuality; $outputType = $imageType; $image = XenForo_Image_Abstract::createFromFile($fileName, $imageType); if (!$image) { return false; } $image->thumbnailFixedShorterSide($maxDimensions); if ($image->getOrientation() != XenForo_Image_Abstract::ORIENTATION_SQUARE) { $cropX = floor(($image->getWidth() - $maxDimensions) / 2); $cropY = floor(($image->getHeight() - $maxDimensions) / 2); $image->crop($cropX, $cropY, $maxDimensions, $maxDimensions); } $newTempFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf'); if (!$newTempFile) { return false; } $image->output($outputType, $newTempFile, $imageQuality); unset($image); $filePath = $this->getCategoryIconFilePath($categoryId); $directory = dirname($filePath); if (XenForo_Helper_File::createDirectory($directory, true) && is_writable($directory)) { if (file_exists($filePath)) { @unlink($filePath); } $writeSuccess = XenForo_Helper_File::safeRename($newTempFile, $filePath); if ($writeSuccess && file_exists($newTempFile)) { @unlink($newTempFile); } } else { $writeSuccess = false; } if ($writeSuccess) { $dw = XenForo_DataWriter::create('Nobita_Teams_DataWriter_Category'); $dw->setExistingData($categoryId); $dw->set('icon_date', XenForo_Application::$time); $dw->save(); } return $writeSuccess; }
/** * Applies the avatar file to the specified user. * * @param integer $userId * @param string $fileName * @param constant|false $imageType Type of image (IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG) * @param integer|false $width * @param integer|false $height * @param array|false $permissions * * @return array */ public function applyAvatar($userId, $fileName, $imageType = false, $width = false, $height = false, $permissions = false) { if (!$imageType || !$width || !$height) { $imageInfo = getimagesize($fileName); if (!$imageInfo) { throw new XenForo_Exception('Non-image passed in to applyAvatar'); } $width = $imageInfo[0]; $height = $imageInfo[1]; $imageType = $imageInfo[2]; } if (!in_array($imageType, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { throw new XenForo_Exception('Invalid image type passed in to applyAvatar'); } if (!XenForo_Image_Abstract::canResize($width, $height)) { throw new XenForo_Exception(new XenForo_Phrase('uploaded_image_is_too_big'), true); } // require 2:1 aspect ratio or squarer if ($width > 2 * $height || $height > 2 * $width) { throw new XenForo_Exception(new XenForo_Phrase('please_provide_an_image_whose_longer_side_is_no_more_than_twice_length'), true); } $outputFiles = array(); $outputType = $imageType; reset(self::$_sizes); list($sizeCode, $maxDimensions) = each(self::$_sizes); $shortSide = $width > $height ? $height : $width; if ($shortSide > $maxDimensions) { $newTempFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf'); $image = XenForo_Image_Abstract::createFromFile($fileName, $imageType); if (!$image) { throw new XenForo_Exception(new XenForo_Phrase('image_could_be_processed_try_another_contact_owner'), true); } $image->thumbnailFixedShorterSide($maxDimensions); $image->output($outputType, $newTempFile, self::$imageQuality); $width = $image->getWidth(); $height = $image->getHeight(); $outputFiles[$sizeCode] = $newTempFile; } else { $outputFiles[$sizeCode] = $fileName; } if (is_array($permissions)) { $maxFileSize = XenForo_Permission::hasPermission($permissions, 'avatar', 'maxFileSize'); if ($maxFileSize != -1 && filesize($outputFiles[$sizeCode]) > $maxFileSize) { foreach ($outputFiles as $tempFile) { if ($tempFile != $fileName) { @unlink($tempFile); } } throw new XenForo_Exception(new XenForo_Phrase('your_avatar_file_size_large_smaller_x', array('size' => XenForo_Locale::numberFormat($maxFileSize, 'size'))), true); } } $crop = array('x' => array('m' => 0), 'y' => array('m' => 0)); while (list($sizeCode, $maxDimensions) = each(self::$_sizes)) { $newTempFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf'); $image = XenForo_Image_Abstract::createFromFile($fileName, $imageType); if (!$image) { continue; } $image->thumbnailFixedShorterSide($maxDimensions); if ($image->getOrientation() != XenForo_Image_Abstract::ORIENTATION_SQUARE) { $crop['x'][$sizeCode] = floor(($image->getWidth() - $maxDimensions) / 2); $crop['y'][$sizeCode] = floor(($image->getHeight() - $maxDimensions) / 2); $image->crop($crop['x'][$sizeCode], $crop['y'][$sizeCode], $maxDimensions, $maxDimensions); } $image->output($outputType, $newTempFile, self::$imageQuality); unset($image); $outputFiles[$sizeCode] = $newTempFile; } if (count($outputFiles) != count(self::$_sizes)) { foreach ($outputFiles as $tempFile) { if ($tempFile != $fileName) { @unlink($tempFile); } } throw new XenForo_Exception(new XenForo_Phrase('image_could_be_processed_try_another_contact_owner'), true); } // done in 2 loops as multiple items may point to same file foreach ($outputFiles as $sizeCode => $tempFile) { $this->_writeAvatar($userId, $sizeCode, $tempFile); } foreach ($outputFiles as $tempFile) { if ($tempFile != $fileName) { @unlink($tempFile); } } $dwData = array('avatar_date' => XenForo_Application::$time, 'avatar_width' => $width, 'avatar_height' => $height, 'avatar_crop_x' => $crop['x']['m'], 'avatar_crop_y' => $crop['y']['m'], 'gravatar' => ''); $dw = XenForo_DataWriter::create('XenForo_DataWriter_User'); $dw->setExistingData($userId); $dw->bulkSet($dwData); $dw->save(); return $dwData; }
/** * Applies the image file to the specified user. * * @param integer $contentId * @param string $fileName * @param constant|false $imageType Type of image (IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG) * @param integer|false $width * @param integer|false $height * @param array|false $permissions * * @return array */ public function applyImage($contentId, $fileName, $imageType = false, $width = false, $height = false, $permissions = false) { if (!$imageType || !$width || !$height) { $imageInfo = getimagesize($fileName); if (!$imageInfo) { throw new XenForo_Exception('Non-image passed in to applyImage'); } $width = $imageInfo[0]; $height = $imageInfo[1]; $imageType = $imageInfo[2]; } if (!in_array($imageType, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { throw new XenForo_Exception('Invalid image type passed in to applyImage'); } if (!XenForo_Image_Abstract::canResize($width, $height)) { throw new XenForo_Exception(new XenForo_Phrase('uploaded_image_is_too_big'), true); } $outputFiles = array(); $outputType = $imageType; reset(self::$_sizes); list($sizeCode, $maxDimensions) = each(self::$_sizes); $newTempFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf'); //print_r($newTempFile);die; $image = XenForo_Image_Abstract::createFromFile($fileName, $imageType); if (!$image) { throw new XenForo_Exception(new XenForo_Phrase('image_could_be_processed_try_another_contact_owner'), true); } $image->thumbnailFixedShorterSide(280); if ($image->getOrientation() != XenForo_Image_Abstract::ORIENTATION_SQUARE) { $cropX = floor(($image->getWidth() - 280) / 2); $cropY = floor(($image->getHeight() - 250) / 2); $image->crop($cropX, $cropY, 280, 250); } $image->output($outputType, $newTempFile, self::$imageQuality); $outputFiles[$sizeCode] = $newTempFile; list($sizeCode, $maxDimensions) = each(self::$_sizes); $shortSide = $width > $height ? $height : $width; if ($shortSide > $maxDimensions) { $newTempFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf'); //print_r($newTempFile);die; $image = XenForo_Image_Abstract::createFromFile($fileName, $imageType); if (!$image) { throw new XenForo_Exception(new XenForo_Phrase('image_could_be_processed_try_another_contact_owner'), true); } $image->thumbnailFixedShorterSide($maxDimensions); $image->output($outputType, $newTempFile, self::$imageQuality); $width = $image->getWidth(); $height = $image->getHeight(); $outputFiles[$sizeCode] = $newTempFile; } else { $outputFiles[$sizeCode] = $fileName; } $crop = array('x' => array('m' => 0), 'y' => array('m' => 0)); while (list($sizeCode, $maxDimensions) = each(self::$_sizes)) { $newTempFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf'); $image = XenForo_Image_Abstract::createFromFile($fileName, $imageType); if (!$image) { continue; } if ($maxDimensions) { $image->thumbnailFixedShorterSide($maxDimensions); } if ($image->getOrientation() != XenForo_Image_Abstract::ORIENTATION_SQUARE) { $crop['x'][$sizeCode] = floor(($image->getWidth() - $maxDimensions) / 2); $crop['y'][$sizeCode] = floor(($image->getHeight() - $maxDimensions) / 2); $image->crop($crop['x'][$sizeCode], $crop['y'][$sizeCode], $maxDimensions, $maxDimensions); } $image->output($outputType, $newTempFile, self::$imageQuality); unset($image); $outputFiles[$sizeCode] = $newTempFile; } if (count($outputFiles) != count(self::$_sizes)) { foreach ($outputFiles as $tempFile) { if ($tempFile != $fileName) { @unlink($tempFile); } } throw new XenForo_Exception(new XenForo_Phrase('image_could_be_processed_try_another_contact_owner'), true); } // done in 2 loops as multiple items may point to same file foreach ($outputFiles as $sizeCode => $tempFile) { $this->_writeImage($contentId, $sizeCode, $tempFile); } foreach ($outputFiles as $tempFile) { if ($tempFile != $fileName) { @unlink($tempFile); } } $dwData = array('image_date' => XenForo_Application::$time); $this->getModelFromCache('XenForo_Model_Thread')->upgradeThreadImageDate($contentId, XenForo_Application::$time); return $dwData; }
/** * Checks the state of the upload to determine if it's * a valid image. */ protected function _checkImageState() { if ($this->_imageInfo !== null) { return; } $this->_imageInfo = false; // default to not an image if (!$this->_tempFile) { return; } $imageInfo = @getimagesize($this->_tempFile); if (!$imageInfo) { if (in_array($this->_extension, array('gif', 'jpg', 'jpe', 'jpeg', 'png'))) { $this->_errors['extension'] = new XenForo_Phrase('the_uploaded_file_was_not_an_image_as_expected'); } return; } $imageInfo['width'] = $imageInfo[0]; $imageInfo['height'] = $imageInfo[1]; $imageInfo['type'] = $imageInfo[2]; $type = $imageInfo['type']; $extensionMap = array(IMAGETYPE_GIF => array('gif'), IMAGETYPE_JPEG => array('jpg', 'jpeg', 'jpe'), IMAGETYPE_PNG => array('png')); if (!isset($extensionMap[$type])) { return; // only consider gif, jpeg, png to be images in this system } if (!in_array($this->_extension, $extensionMap[$type])) { $this->_errors['extension'] = new XenForo_Phrase('contents_of_uploaded_image_do_not_match_files_extension'); return; } $fp = @fopen($this->_tempFile, 'rb'); if ($fp) { $previous = ''; while (!@feof($fp)) { $content = fread($fp, 256000); $test = $previous . $content; $exists = strpos($test, '<?php') !== false || preg_match('/<script\\s+language\\s*=\\s*(php|"php"|\'php\')\\s*>/i', $test); if ($exists) { @fclose($fp); $this->_errors['content'] = new XenForo_Phrase('uploaded_image_contains_invalid_content'); return; } $previous = $content; } @fclose($fp); } $orientation = 0; if ($imageInfo['type'] == IMAGETYPE_JPEG && function_exists('exif_read_data')) { $exif = @exif_read_data($this->_tempFile, 'EXIF'); if ($exif && !empty($exif['Orientation']) && $exif['Orientation'] > 1) { $orientation = $exif['Orientation']; } } $transformRequired = $orientation > 1; $maxWidth = $this->_maxWidth; $maxHeight = $this->_maxHeight; if ($orientation >= 5 && $orientation <= 8) { // after rotation the X and Y coords will be reversed, // so flip the limits to reflect the "after" value $maxHeight = $this->_maxWidth; $maxWidth = $this->_maxHeight; } $resizeRequired = $maxWidth && $imageInfo['width'] > $maxWidth || $maxHeight && $imageInfo['height'] > $maxHeight; if (XenForo_Image_Abstract::canResize($imageInfo['width'], $imageInfo['height']) && ($resizeRequired || $transformRequired)) { $image = XenForo_Image_Abstract::createFromFile($this->_tempFile, $type); if ($image) { if ($resizeRequired) { $image->thumbnail($maxWidth ? $maxWidth : $maxHeight, $maxHeight); } if ($transformRequired) { $image->transformByExif($orientation); } $success = $image->output($type, $this->_tempFile); if ($success) { $imageInfo['width'] = $imageInfo[0] = $image->getWidth(); $imageInfo['height'] = $imageInfo[1] = $image->getHeight(); } else { if ($resizeRequired) { $this->_errors['dimensions'] = new XenForo_Phrase('uploaded_image_is_too_big'); } // ok to ignore the EXIF transform failing } } else { // treat as non-image $imageInfo = false; } } else { if ($resizeRequired) { $this->_errors['dimensions'] = new XenForo_Phrase('uploaded_image_is_too_big'); } } $this->_imageInfo = $imageInfo; }
/** * Inserts uploaded attachment data. * * @param XenForo_Upload $file Uploaded attachment info. Assumed to be valid * @param integer $userId User ID uploading * @param array $exif Exif data to cache * * @return integer Attachment data ID */ public function insertUploadedAttachmentData(XenForo_Upload $file, $userId, array $exif = array()) { $dimensions = array(); $fileIsVideo = false; $tempThumbFile = false; $options = XenForo_Application::getOptions(); if ($file->isImage()) { $dimensions = array('width' => $file->getImageInfoField('width'), 'height' => $file->getImageInfoField('height')); if (XenForo_Image_Abstract::canResize($dimensions['width'], $dimensions['height'])) { $imageFile = $file->getTempFile(); } else { $imageFile = $options->xengalleryDefaultNoThumb; } $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfmg'); if ($tempThumbFile) { @copy($imageFile, $tempThumbFile); } } else { $fileIsVideo = true; if ($options->get('xengalleryVideoTranscoding', 'thumbnail')) { try { $video = new XenGallery_Helper_Video($file->getTempFile()); $tempThumbFile = $video->getKeyFrame(); list($width, $height) = $video->getVideoDimensions(); $dimensions['width'] = $width; $dimensions['height'] = $height; } catch (XenForo_Exception $e) { } } if (!$tempThumbFile) { $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfmg'); if ($tempThumbFile) { @copy($options->xengalleryDefaultNoThumb, $tempThumbFile); } } } if ($tempThumbFile) { $image = new XenGallery_Helper_Image($tempThumbFile); if ($image) { $image->resize($dimensions['thumbnail_width'] = $options->xengalleryThumbnailDimension['width'], $dimensions['thumbnail_height'] = $options->xengalleryThumbnailDimension['height'], 'crop'); $image->saveToPath($tempThumbFile); unset($image); } } $mediaModel = $this->_getMediaModel(); try { $dataDw = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData'); $filename = $file->getFileName(); $dataDw->set('user_id', $userId); if ($fileIsVideo) { $filename = strtr($filename, strtolower(substr(strrchr($filename, '.'), 1)), 'mp4'); $dataDw->set('file_path', $mediaModel->getVideoFilePath()); } $dataDw->set('filename', $filename); $dataDw->bulkSet($dimensions); $dataDw->setExtraData(XenForo_DataWriter_AttachmentData::DATA_TEMP_FILE, $file->getTempFile()); if ($tempThumbFile) { $dataDw->setExtraData(XenForo_DataWriter_AttachmentData::DATA_TEMP_THUMB_FILE, $tempThumbFile); } $dataDw->setExtraData(XenGallery_DataWriter_AttachmentData::DATA_XMG_FILE_IS_VIDEO, $fileIsVideo); $dataDw->setExtraData(XenGallery_DataWriter_AttachmentData::DATA_XMG_DATA, true); $dataDw->save(); } catch (Exception $e) { if ($tempThumbFile) { @unlink($tempThumbFile); } throw $e; } if ($tempThumbFile) { @unlink($tempThumbFile); } $exif = $this->_getMediaModel()->sanitizeExifData($exif); $db = $this->_getDb(); $db->query(' INSERT IGNORE INTO xengallery_exif_cache (data_id, media_exif_data_cache_full, cache_date) VALUES (?, ?, ?) ', array($dataDw->get('data_id'), @json_encode($exif), XenForo_Application::$time)); return $dataDw->get('data_id'); }