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 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.º 10
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');
    }
Ejemplo n.º 11
0
 public function importMedia($oldId, $tempFile, $contentType = '', array $xengalleryMedia = array(), array $xfAttachment = array(), array $xfAttachmentData = array())
 {
     $db = $this->_getDb();
     XenForo_Db::beginTransaction($db);
     $attachmentId = 0;
     if ($xfAttachment) {
         /** @var $attachmentDw XenForo_DataWriter_Attachment */
         $attachmentDw = XenForo_DataWriter::create('XenForo_DataWriter_Attachment');
         $attachmentDw->setImportMode(true);
         $attachmentDw->bulkSet($xfAttachment);
         $attachmentDw->save();
         $attachmentId = $attachmentDw->get('attachment_id');
     }
     $newId = false;
     if ($xengalleryMedia) {
         /** @var $mediaDw XenGallery_DataWriter_Media */
         $mediaDw = XenForo_DataWriter::create('XenGallery_DataWriter_Media');
         $mediaDw->setImportMode(true);
         $mediaDw->set('imported', XenForo_Application::$time);
         if ($this->_retainKeys) {
             $mediaDw->set('media_id', $oldId);
         }
         $xengalleryMedia['attachment_id'] = $attachmentId;
         $mediaDw->bulkSet($xengalleryMedia);
         if ($mediaDw->save()) {
             $newId = $mediaDw->get('media_id');
             $this->_getImportModel()->logImportData('xengallery_media', $oldId, $newId);
         }
         $media = $mediaDw->getMergedData();
         if ($media['likes'] && $media['like_users'] && $contentType) {
             $this->_convertLikesToNewContentType($oldId, $newId, $contentType, 'xengallery_media');
         }
     }
     if ($xfAttachmentData) {
         $fileIsVideo = false;
         $db->update('xf_attachment', array('content_id' => $media['media_id']), 'attachment_id = ' . $db->quote($attachmentId));
         $options = XenForo_Application::getOptions();
         $upload = new XenForo_Upload($xfAttachmentData['filename'], $tempFile);
         if ($upload->isImage()) {
             $image = new XenGallery_Helper_Image($tempFile);
             $image->importMode = true;
             $dimensions = array('width' => $image->getWidth(), 'height' => $image->getHeight());
             $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfmg');
             if ($tempThumbFile && $image) {
                 $resized = $image->resize($dimensions['thumbnail_width'] = $options->xengalleryThumbnailDimension['width'], $dimensions['thumbnail_height'] = $options->xengalleryThumbnailDimension['height'], 'crop');
                 if (!$resized) {
                     return false;
                 }
                 $image->saveToPath($tempThumbFile);
                 unset($image);
             } else {
                 return false;
             }
         } else {
             $dimensions = array();
             $fileIsVideo = true;
             $tempThumbFile = false;
             if ($options->get('xengalleryVideoTranscoding', 'thumbnail')) {
                 try {
                     $video = new XenGallery_Helper_Video($upload->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);
                 }
             }
             $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->getModelFromCache('XenGallery_Model_Media');
         try {
             $dataDw = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData');
             $filename = $upload->getFileName();
             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->bulkSet($xfAttachmentData);
             $dataDw->setExtraData(XenForo_DataWriter_AttachmentData::DATA_TEMP_FILE, $tempFile);
             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();
             $attachmentData = $dataDw->getMergedData();
             $db->update('xf_attachment', array('data_id' => $attachmentData['data_id']), 'attachment_id = ' . $db->quote($attachmentId));
         } catch (Exception $e) {
             if ($tempThumbFile) {
                 @unlink($tempThumbFile);
             }
             throw $e;
         }
         if ($tempThumbFile) {
             @unlink($tempThumbFile);
         }
     }
     if ($newId) {
         XenForo_Db::commit($db);
     } else {
         XenForo_Db::rollback($db);
     }
     return $newId;
 }