public static function verifyOption(array &$values, XenForo_DataWriter $dw, $fieldName)
 {
     if ($dw->isInsert()) {
         return true;
     }
     if (empty($values['enabled'])) {
         return true;
     }
     try {
         $helper = new XenGallery_Helper_Video(null, $values['ffmpegPath'], !empty($values['transcode']));
         $errors = $helper->getFfmpegErrors();
         if ($errors) {
             $dw->error(reset($errors), $fieldName);
             return false;
         }
     } catch (Exception $e) {
         $dw->error($e->getMessage(), $fieldName);
         return false;
     }
     if (!empty($values['transcode'])) {
         try {
             if (!is_file($values['phpPath']) && !is_executable($values['phpPath'])) {
                 $dw->error(new XenForo_Phrase('xengallery_php_binary_path_could_not_be_verified'));
                 return false;
             }
         } catch (Exception $e) {
             $dw->error($e->getMessage(), $fieldName);
             return false;
         }
     }
     return true;
 }
 public function runTranscodeQueue($limit)
 {
     $pending = $this->countQueue('pending');
     $queue = $this->getRunnableTranscodeQueue($limit);
     if (!$queue) {
         return false;
     }
     $video = new XenGallery_Helper_Video();
     $db = $this->_getDb();
     foreach ($queue as $id => $record) {
         $pending--;
         $data = @unserialize($record['queue_data']);
         if ($video->setFilename($data['filename'])) {
             $video->beginTranscode($record);
         } else {
             $this->sendTranscodeAlert($data['media'], false);
             $db->delete('xengallery_transcode_queue', 'transcode_queue_id = ' . $db->quote($id));
         }
     }
     return $pending > 0;
 }
// CLI only
if (PHP_SAPI != 'cli') {
    die('This script may only be run at the command line.');
}
$fileDir = realpath(dirname(__FILE__) . '/../../../');
chdir($fileDir);
require_once $fileDir . '/library/XenForo/Autoloader.php';
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
XenForo_Application::initialize($fileDir . '/library', $fileDir);
$dependencies = new XenForo_Dependencies_Public();
$dependencies->preLoadData();
set_time_limit(0);
if ($argc < 2) {
    if (empty($argv[1])) {
        die('No queue ID specified.');
    }
}
/** @var XenGallery_Model_Transcode $model */
$model = XenForo_Model::create('XenGallery_Model_Transcode');
$queueRecord = $model->getTranscodeQueueItem($argv[1]);
if (!$queueRecord) {
    die('Queue record no longer exists.');
}
if (!$model->setQueueItemProcessing($queueRecord['transcode_queue_id'])) {
    die('Queue record already set to processing by another process');
}
$queueRecord['queue_data'] = @unserialize($queueRecord['queue_data']);
$video = new XenGallery_Helper_Video($queueRecord['queue_data']['filename']);
$outputFile = $video->transcodeProcess();
$video->finalizeTranscode($queueRecord, $outputFile);
Example #4
0
 public function rebuildThumbnail(array $media, array $imageInfo, $deleteExisting = true, $skipThumbnail = 0)
 {
     $originalThumbFile = $this->getMediaThumbnailFilePath($media);
     $media['file_hash'] = $imageInfo['file_hash'];
     $options = XenForo_Application::getOptions();
     $thumbFile = $this->getMediaThumbnailFilePath($media);
     $thumbImage = false;
     if ($skipThumbnail) {
         XenForo_Helper_File::safeRename($originalThumbFile, $thumbFile);
     } else {
         if ($media['media_type'] == 'image_upload') {
             $originalFile = $this->getOriginalDataFilePath($media, true);
             $thumbImage = new XenGallery_Helper_Image($originalFile);
         } else {
             if ($media['media_type'] == 'video_upload') {
                 $originalFile = $this->getAttachmentDataFilePath($media);
                 $tempThumbFile = false;
                 if ($options->get('xengalleryVideoTranscoding', 'thumbnail')) {
                     try {
                         $video = new XenGallery_Helper_Video($originalFile);
                         $tempThumbFile = $video->getKeyFrame();
                     } catch (XenForo_Exception $e) {
                     }
                 }
                 if (!$tempThumbFile) {
                     $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfmg');
                     if ($tempThumbFile) {
                         @copy($options->xengalleryDefaultNoThumb, $tempThumbFile);
                     }
                 }
                 $thumbImage = new XenGallery_Helper_Image($tempThumbFile);
             }
         }
         if ($thumbImage) {
             $thumbImage->resize($options->xengalleryThumbnailDimension['width'], $options->xengalleryThumbnailDimension['height'], 'crop');
             $thumbnailed = $thumbImage->saveToPath($thumbFile);
             if ($thumbnailed && $deleteExisting) {
                 @unlink($originalThumbFile);
             }
         }
     }
     $mediaWriter = XenForo_DataWriter::create('XenGallery_DataWriter_Media');
     $mediaWriter->setExistingData($media);
     $mediaWriter->bulkSet(array('last_edit_date' => XenForo_Application::$time, 'thumbnail_date' => $media['thumbnail_date']));
     $mediaWriter->save();
     if ($media['album_id']) {
         $albumWriter = XenForo_DataWriter::create('XenGallery_DataWriter_Album');
         $albumWriter->setExistingData($media['album_id']);
         if (!$albumWriter->get('manual_media_cache') && !$albumWriter->get('album_thumbnail_date')) {
             $mediaItems = $this->getMediaForAlbumCache($media['album_id']);
             $albumWriter->bulkSet(array('last_update_date' => XenForo_Application::$time, 'media_cache' => serialize($mediaItems)));
             $albumWriter->save();
         }
     }
 }
Example #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 $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');
    }
 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;
 }
 public function execute(array $deferred, array $data, $targetRunTime, &$status)
 {
     $data = array_merge(array('position' => 0, 'batch' => 10), $data);
     $data['batch'] = max(1, $data['batch']);
     /* @var $mediaModel XenGallery_Model_Media */
     $mediaModel = XenForo_Model::create('XenGallery_Model_Media');
     $mediaIds = $mediaModel->getMediaIdsInRange($data['position'], $data['batch'], 'all');
     if (sizeof($mediaIds) == 0) {
         return true;
     }
     $options = XenForo_Application::getOptions();
     $fetchOptions = array('join' => XenGallery_Model_Media::FETCH_ATTACHMENT);
     $media = $mediaModel->getMediaByIds($mediaIds, $fetchOptions);
     $media = $mediaModel->prepareMediaItems($media);
     foreach ($media as $item) {
         $data['position'] = $item['media_id'];
         if ($item['thumbnail_date']) {
             continue;
         }
         $thumbnailPath = $mediaModel->getMediaThumbnailFilePath($item);
         $dataPath = $mediaModel->getOriginalDataFilePath($item, true);
         if ($item['media_type'] == 'image_upload') {
             XenForo_Helper_File::createDirectory(dirname($thumbnailPath), true);
             if (!file_exists($dataPath) || !is_readable($dataPath)) {
                 continue;
             }
             $image = new XenGallery_Helper_Image($dataPath);
             if ($image) {
                 $image->resize($dimensions['thumbnail_width'] = $options->xengalleryThumbnailDimension['width'], $dimensions['thumbnail_height'] = $options->xengalleryThumbnailDimension['height'], 'crop');
                 $image->saveToPath($thumbnailPath);
                 unset($image);
                 $attachmentDataWriter = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData');
                 $attachmentDataWriter->setExistingData($item['data_id']);
                 $attachmentDataWriter->bulkSet($dimensions);
                 $attachmentDataWriter->save();
             }
         } else {
             if ($item['media_type'] == 'video_upload') {
                 XenForo_Helper_File::createDirectory(dirname($thumbnailPath), true);
                 if (!file_exists($dataPath) || !is_readable($dataPath)) {
                     continue;
                 }
                 $tempThumbFile = false;
                 if ($options->get('xengalleryVideoTranscoding', 'thumbnail')) {
                     try {
                         $video = new XenGallery_Helper_Video($dataPath);
                         $tempThumbFile = $video->getKeyFrame();
                     } 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($thumbnailPath);
                     unset($image);
                     $attachmentDataWriter = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData');
                     $attachmentDataWriter->setExistingData($item['data_id']);
                     $attachmentDataWriter->bulkSet($dimensions);
                     $attachmentDataWriter->save();
                 }
             } else {
                 if ($item['media_type'] == 'video_embed') {
                     preg_match('/\\[media=(.*?)\\](.*?)\\[\\/media\\]/is', $item['media_tag'], $parts);
                     $mediaModel->getVideoThumbnailUrlFromParts($parts, true);
                 }
             }
         }
         /** @var $mediaWriter XenGallery_DataWriter_Media */
         $mediaWriter = XenForo_DataWriter::create('XenGallery_DataWriter_Media');
         $mediaWriter->setImportMode(true);
         $mediaWriter->setExistingData($item['media_id']);
         $mediaWriter->set('last_edit_date', XenForo_Application::$time);
         $mediaWriter->save();
     }
     $actionPhrase = new XenForo_Phrase('rebuilding');
     $typePhrase = new XenForo_Phrase('xengallery_rebuild_thumbnails');
     $status = sprintf('%s... %s (%s)', $actionPhrase, $typePhrase, XenForo_Locale::numberFormat($data['position']));
     return $data;
 }
Example #8
0
 protected function _associateAttachmentsAndMedia(array $attachments, array $input, array $container)
 {
     $mediaModel = $this->_getMediaModel();
     $visitor = XenForo_Visitor::getInstance();
     $requiresTranscode = false;
     foreach ($attachments as $attachmentId => $attachment) {
         if (!isset($input[$attachmentId])) {
             continue;
         }
         $attachInput = array_merge(array('media_title' => '', 'media_description' => '', 'tags' => '', 'custom_fields' => array()), $input[$attachmentId]);
         $mediaData = array('media_title' => $attachInput['media_title'], 'media_description' => $attachInput['media_description'], 'media_type' => $input['media_type'], 'media_state' => $input['media_state'], 'attachment_id' => $attachmentId, 'user_id' => $visitor->user_id, 'username' => $visitor->username, 'album_id' => !empty($container['album_id']) ? $container['album_id'] : 0, 'category_id' => !empty($container['category_id']) ? $container['category_id'] : 0);
         if ($input['media_type'] == 'video_upload' && $mediaModel->requiresTranscoding($attachment)) {
             $customFields = array();
             $customFieldsShown = array();
             $customFieldsData = $this->_updateCustomFields($attachInput, array(), true);
             if ($customFieldsData) {
                 list($customFields, $customFieldsShown) = $customFieldsData;
             }
             $transcodeData = array('attachmentId' => $attachmentId, 'media' => $mediaData, 'tags' => $attachInput['tags'], 'customFields' => $customFieldsData ? $customFields : array(), 'customFieldsShown' => $customFieldsData ? $customFieldsShown : array());
             $attachmentFile = $mediaModel->getAttachmentDataFilePath($attachment);
             // We check for tag errors and permissions here, now, so nothing will prevent the transcoding finishing later.
             if ($mediaModel->canEditTags()) {
                 $tagModel = XenForo_Model::create('XenForo_Model_Tag');
                 /** @var XenForo_TagHandler_Tagger $tagger */
                 $tagger = $tagModel->getTagger('xengallery_media');
                 $tagger->setPermissionsFromContext($container)->setTags($tagModel->splitTags($attachInput['tags']));
                 if ($tagger->getErrors()) {
                     throw $this->responseException($this->responseError($tagger->getErrors()));
                 }
                 $transcodeData['tagger_permissions'] = $tagger->getPermissions();
             } else {
                 unset($transcodeData['tags']);
             }
             $video = new XenGallery_Helper_Video($attachmentFile);
             $video->queueTranscode($transcodeData);
             $requiresTranscode = true;
         } else {
             $mediaWriter = XenForo_DataWriter::create('XenGallery_DataWriter_Media');
             $tagger = null;
             if ($mediaModel->canEditTags()) {
                 $tagModel = $this->getModelFromCache('XenForo_Model_Tag');
                 /** @var XenForo_TagHandler_Tagger $tagger */
                 $tagger = $tagModel->getTagger('xengallery_media');
                 $tagger->setPermissionsFromContext($container)->setTags($tagModel->splitTags($attachInput['tags']));
                 $mediaWriter->mergeErrors($tagger->getErrors());
             }
             $mediaWriter->bulkSet($mediaData);
             $mediaWriter->save();
             $media = $mediaWriter->getMergedData();
             if ($tagger) {
                 $tagger->setContent($media['media_id'], true)->save();
             }
             $this->_updateCustomFields($attachInput, $media);
             $mediaModel->updateAttachmentData($attachmentId, $media['media_id']);
             $mediaModel->updateExifData($attachment, $media);
             $mediaWriter->updateUserMediaQuota();
             if ($media['media_type'] == 'image_upload') {
                 $watermarkModel = $this->_getWatermarkModel();
                 $options = XenForo_Application::getOptions();
                 if ($options->get('xengalleryEnableWatermarking') == 'enabled' && !$watermarkModel->canBypassWatermark()) {
                     $media = array_merge($media, $attachment);
                     $imageInfo = $watermarkModel->addWatermarkToImage($media);
                     if ($imageInfo) {
                         $mediaModel->rebuildThumbnail($media, $imageInfo);
                     }
                 }
             }
         }
     }
     return $requiresTranscode;
     // Does not require transcoding
 }