Ejemplo n.º 1
0
 /**
  * Processes an image upload for a content.
  *
  * @param XenForo_Upload $upload The uploaded image.
  * @param integer $contentId Content ID image belongs to
  * @param array|false $permissions User's permissions. False to skip permission checks
  *
  * @return array Changed image fields
  */
 public function uploadImage(XenForo_Upload $upload, $contentId, $permissions)
 {
     $largestDimension = $this->getSizeFromCode('l');
     if (!$upload->isValid()) {
         throw new XenForo_Exception($upload->getErrors(), true);
     }
     if (!$upload->isImage()) {
         throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $imageType = $upload->getImageInfoField('type');
     //prd($imageType);
     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);
     }
     $baseTempFile = $upload->getTempFile();
     $width = $upload->getImageInfoField('width');
     $height = $upload->getImageInfoField('height');
     return $this->applyImage($contentId, $baseTempFile, $imageType, $width, $height, $permissions);
 }
Ejemplo n.º 2
0
 public function uploadAvatar(XenForo_Upload $upload, $teamId)
 {
     if (!$teamId) {
         throw new Nobita_Teams_Exception_Abstract('Missing team ID.');
     }
     if (!$upload->isValid()) {
         throw new Nobita_Teams_Exception_Abstract($upload->getErrors(), true);
     }
     if (!$upload->isImage()) {
         throw new Nobita_Teams_Exception_Abstract(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $imageType = $upload->getImageInfoField('type');
     if (!in_array($imageType, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
         throw new Nobita_Teams_Exception_Abstract(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $baseTempFile = $upload->getTempFile();
     $width = $upload->getImageInfoField('width');
     $height = $upload->getImageInfoField('height');
     if ($width < 280 || $height < 280) {
         throw new Nobita_Teams_Exception_Abstract(new XenForo_Phrase('Teams_avatar_required_least_280x280_pixels'), true);
     }
     return $this->applyAvatar($teamId, $baseTempFile, $imageType, $width, $height);
 }
Ejemplo n.º 3
0
 /**
  * Processes an cover upload for a user.
  *
  * @param XenForo_Upload $upload The uploaded cover.
  * @param integer $teamId User ID cover belongs to
  *
  * @return array Changed cover fields
  */
 public function uploadCoverPhoto(XenForo_Upload $upload, $teamId, $existingCoverDate)
 {
     if (!$teamId) {
         throw new Nobita_Teams_Exception_Abstract("Missing team ID");
     }
     if (!$upload->isValid()) {
         throw new Nobita_Teams_Exception_Abstract($upload->getErrors(), true);
     }
     if (!$upload->isImage()) {
         throw new Nobita_Teams_Exception_Abstract(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $imageType = $upload->getImageInfoField('type');
     if (!in_array($imageType, array(IMAGETYPE_PNG, IMAGETYPE_JPEG))) {
         throw new Nobita_Teams_Exception_Abstract(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $baseTempFile = $upload->getTempFile();
     $width = $upload->getImageInfoField('width');
     $height = $upload->getImageInfoField('height');
     if ($height < self::$minHeight) {
         throw new Nobita_Teams_Exception_Abstract(new XenForo_Phrase('Teams_upload_image_greater_x', array('min' => self::$minHeight)), true);
     }
     return $this->applyCover($teamId, $baseTempFile, $imageType, $width, $height, $existingCoverDate);
 }
Ejemplo n.º 4
0
 /**
  * Processes an avatar upload for a user.
  *
  * @param XenForo_Upload $upload The uploaded avatar.
  * @param integer $userId User ID avatar belongs to
  * @param array|false $permissions User's permissions. False to skip permission checks
  *
  * @return array Changed avatar fields
  */
 public function uploadAvatar(XenForo_Upload $upload, $userId, $permissions)
 {
     if (!$userId) {
         throw new XenForo_Exception('Missing user ID.');
     }
     if ($permissions !== false && !is_array($permissions)) {
         throw new XenForo_Exception('Invalid permission set.');
     }
     $largestDimension = $this->getSizeFromCode('l');
     if (!$upload->isValid()) {
         throw new XenForo_Exception($upload->getErrors(), true);
     }
     if (!$upload->isImage()) {
         throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $imageType = $upload->getImageInfoField('type');
     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);
     }
     $baseTempFile = $upload->getTempFile();
     $width = $upload->getImageInfoField('width');
     $height = $upload->getImageInfoField('height');
     return $this->applyAvatar($userId, $baseTempFile, $imageType, $width, $height, $permissions);
 }
Ejemplo n.º 5
0
 /**
  * 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');
 }
Ejemplo n.º 6
0
 public function uploadAuthorCover(XenForo_Upload $cover, $userId)
 {
     if (!$userId) {
         return false;
     }
     if (!$cover->isValid()) {
         throw new XenForo_Exception($cover->getErrors(), true);
     }
     if (!$cover->isImage()) {
         throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $imageType = $cover->getImageInfoField('type');
     if (!isset(sonnb_XenGallery_Model_ContentData::$extensionMap[$imageType])) {
         throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $baseTempFile = $cover->getTempFile();
     $width = $cover->getImageInfoField('width');
     $height = $cover->getImageInfoField('height');
     return $this->applyAuthorCover($baseTempFile, $userId, $imageType, $width, $height);
 }
Ejemplo n.º 7
0
 public function uploadVideoThumbnail(XenForo_Upload $upload, array $video)
 {
     if (!$video) {
         return false;
     }
     if (!$upload->isValid()) {
         throw new XenForo_Exception($upload->getErrors(), true);
     }
     if (!$upload->isImage()) {
         throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $baseTempFile = $upload->getTempFile();
     $imageType = $upload->getImageInfoField('type');
     $width = $upload->getImageInfoField('width');
     $height = $upload->getImageInfoField('height');
     return $this->applyVideoThumbnail($video, $baseTempFile, $imageType, $width, $height);
 }
Ejemplo n.º 8
0
 public function uploadCategoryIcon(XenForo_Upload $upload, $categoryId)
 {
     if (!$categoryId) {
         throw new XenForo_Exception("Missing category ID.");
     }
     if (!$upload->isValid()) {
         throw new XenForo_Exception($upload->getErrors(), true);
     }
     if (!$upload->isImage()) {
         throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $baseTempFile = $upload->getTempFile();
     $imageType = $upload->getImageInfoField('type');
     $width = $upload->getImageInfoField('width');
     $height = $upload->getImageInfoField('height');
     return $this->applyCategoryIcon($categoryId, $baseTempFile, $imageType, $width, $height);
 }
Ejemplo n.º 9
0
 public function insertUploadedPhotoData(XenForo_Upload $file, array $extra = array(), &$errorPhraseKey = '')
 {
     $return = false;
     if ($this->_getPhotoModel()->canResizeImage($file->getImageInfoField('width'), $file->getImageInfoField('height'))) {
         $xenOptions = XenForo_Application::getOptions();
         $tempFile = $file->getTempFile();
         $tempType = $file->getImageInfoField('type');
         $dimensions = array('width' => $file->getImageInfoField('width'), 'height' => $file->getImageInfoField('height'), 'is_animated' => $this->_getPhotoModel()->isAnimatedGif($tempFile) ? 1 : 0);
         $smallThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf');
         $mediumThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf');
         $largeThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf');
         $originalFile = false;
         $successCount = 0;
         if ($xenOptions->sonnbXG_enableResize) {
             $photoConstraints = $this->_getPhotoModel()->getPhotoDataConstraints();
             //Resize original image if it excess user's limit.
             if ($dimensions['width'] > $photoConstraints['width'] || $dimensions['height'] > $photoConstraints['height']) {
                 $image = XenForo_Image_Abstract::createFromFile($tempFile, $tempType);
                 if ($image) {
                     if ($image->thumbnail($photoConstraints['width'], $photoConstraints['height'])) {
                         $image->output($tempType, $tempFile, 100);
                         $dimensions['width'] = $image->getWidth();
                         $dimensions['height'] = $image->getHeight();
                     }
                     unset($image);
                 }
             }
         }
         if (!$xenOptions->sonnbXG_disableOriginal) {
             $originalFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf');
             if ($this->_getContentDataModel()->copyFile($tempFile, $originalFile)) {
                 $successCount++;
             }
         }
         $this->exifRotate($tempFile, $tempType, $tempFile);
         if ($largeThumbFile) {
             if ($this->createContentDataThumbnailFile($tempFile, $largeThumbFile, $tempType, sonnb_XenGallery_Model_ContentData::CONTENT_FILE_TYPE_LARGE, $fallback, $dimensions)) {
                 $successCount++;
             }
             if ($fallback === true) {
                 $dimensions['large_width'] = $file->getImageInfoField('width');
                 $dimensions['large_height'] = $file->getImageInfoField('height');
             }
         }
         if ($mediumThumbFile) {
             if ($this->createContentDataThumbnailFile($largeThumbFile, $mediumThumbFile, $tempType, sonnb_XenGallery_Model_ContentData::CONTENT_FILE_TYPE_MEDIUM, $fallback)) {
                 $successCount++;
             }
             if ($fallback === true) {
                 $dimensions['medium_width'] = $file->getImageInfoField('width');
                 $dimensions['medium_height'] = $file->getImageInfoField('height');
             }
         }
         if ($smallThumbFile) {
             if ($this->createContentDataThumbnailFile($largeThumbFile, $smallThumbFile, $tempType, sonnb_XenGallery_Model_ContentData::CONTENT_FILE_TYPE_SMALL, $fallback)) {
                 $successCount++;
             }
             if ($fallback === true) {
                 $dimensions['small_width'] = $file->getImageInfoField('width');
                 $dimensions['small_height'] = $file->getImageInfoField('height');
             }
         }
         if ($xenOptions->sonnbXG_disableOriginal) {
             if ($successCount !== 3) {
                 return $return;
             }
             $dimensions['width'] = $dimensions['large_width'];
             $dimensions['height'] = $dimensions['large_height'];
             $dimensions['is_animated'] = $this->_getPhotoModel()->isAnimatedGif($largeThumbFile) ? 1 : 0;
         } else {
             if ($successCount !== 4) {
                 return $return;
             }
         }
         try {
             /** @var sonnb_XenGallery_DataWriter_ContentData $dataDw */
             $dataDw = XenForo_DataWriter::create('sonnb_XenGallery_DataWriter_ContentData');
             $dataDw->bulkSet($extra);
             $dataDw->bulkSet($dimensions);
             $dataDw->set('extension', sonnb_XenGallery_Model_ContentData::$extensionMap[$tempType]);
             if (!$xenOptions->sonnbXG_disableOriginal && $originalFile) {
                 $dataDw->setExtraData(sonnb_XenGallery_DataWriter_ContentData::DATA_TEMP_FILE, $originalFile);
             }
             if ($smallThumbFile) {
                 $dataDw->setExtraData(sonnb_XenGallery_DataWriter_ContentData::DATA_TEMP_SMALL_THUMB_FILE, $smallThumbFile);
             }
             if ($mediumThumbFile) {
                 $dataDw->setExtraData(sonnb_XenGallery_DataWriter_ContentData::DATA_TEMP_MEDIUM_THUMB_FILE, $mediumThumbFile);
             }
             if ($largeThumbFile) {
                 $dataDw->setExtraData(sonnb_XenGallery_DataWriter_ContentData::DATA_TEMP_LARGE_THUMB_FILE, $largeThumbFile);
             }
             $dataDw->save();
             $return = $dataDw->getMergedData();
             $exif = $this->_getPhotoModel()->getPhotoExif($return, $tempFile);
             $return['title'] = isset($exif['title']) ? trim($exif['title']) : ($xenOptions->sonnbXG_useFileName ? trim(pathinfo($file->getFileName(), PATHINFO_FILENAME)) : '');
             $return['description'] = isset($exif['description']) ? trim($exif['description']) : '';
             $return['location_lat'] = isset($exif['latitude']) ? $exif['latitude'] : '';
             $return['location_lng'] = isset($exif['longitude']) ? $exif['longitude'] : '';
             $return['content_location'] = isset($exif['address']) ? $exif['address'] : '';
         } catch (Exception $e) {
             if ($smallThumbFile) {
                 @unlink($smallThumbFile);
             }
             if ($mediumThumbFile) {
                 @unlink($mediumThumbFile);
             }
             if ($largeThumbFile) {
                 @unlink($largeThumbFile);
             }
             if (!$xenOptions->sonnbXG_disableOriginal && $originalFile) {
                 @unlink($originalFile);
             }
             throw $e;
         }
         if ($smallThumbFile) {
             @unlink($smallThumbFile);
         }
         if ($mediumThumbFile) {
             @unlink($mediumThumbFile);
         }
         if ($largeThumbFile) {
             @unlink($largeThumbFile);
         }
         if (!$xenOptions->sonnbXG_disableOriginal && $originalFile) {
             @unlink($originalFile);
         }
     }
     unset($tempFile);
     return $return;
 }
Ejemplo n.º 10
0
 public function uploadMediaThumbnail(XenForo_Upload $upload, array $media)
 {
     if (!$media) {
         throw new XenForo_Exception('Missing media record.');
     }
     if (!$upload->isValid()) {
         throw new XenForo_Exception($upload->getErrors(), true);
     }
     if (!$upload->isImage()) {
         throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
     }
     $baseTempFile = $upload->getTempFile();
     $imageType = $upload->getImageInfoField('type');
     $width = $upload->getImageInfoField('width');
     $height = $upload->getImageInfoField('height');
     return $this->processMediaThumbnail($media, $baseTempFile, $imageType, $width, $height);
 }
Ejemplo n.º 11
0
    /**
     * 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');
    }