/** * @return sonnb_XenGallery_Model_PhotoFilter */ public static function getInstance() { if (self::$_instance === null) { self::$_instance = new self(); self::$_instance->imageFilter(); } return self::$_instance; }
public function insertPhotos($photoDataInsert, $hash, array $deletes = array(), array $privacy = array()) { $conditions = array('temp_hash' => $hash, 'content_type' => sonnb_XenGallery_Model_Photo::$contentType, 'unassociated' => 1); $fetchOptions = array('order' => 'recent'); $photoModel = $this->_getPhotoModel(); $photoDataModel = $this->_getPhotoDataModel(); $xenOptions = XenForo_Application::getOptions(); $photoData = $photoDataModel->getData($conditions, $fetchOptions); $coverContentDataId = $this->getExtraData(self::COVER_CONTENT_DATA_ID); if ($photoData) { foreach ($photoData as $photoDataId => $_photoData) { if (isset($deletes[$photoDataId])) { unset($photoData[$photoDataId]); unset($photoDataInsert['content_title'][$photoDataId]); unset($photoDataInsert['content_description'][$photoDataId]); unset($photoDataInsert['content_location'][$photoDataId]); unset($photoDataInsert['location_lat'][$photoDataId]); unset($photoDataInsert['location_lng'][$photoDataId]); } } $visitor = XenForo_Visitor::getInstance(); $photoData = array_reverse($photoData, true); $conditions = array('content_type' => sonnb_XenGallery_Model_Photo::$contentType); $maximumPhotoCount = $photoModel->getPhotoCountLimit(); $existingPhotoCount = $photoModel->countContentsByAlbumId($this->get('album_id'), $conditions); if ($maximumPhotoCount && $existingPhotoCount + count($photoData) > $maximumPhotoCount) { $this->error(new XenForo_Phrase('sonnb_xengallery_this_album_reached_photo_limit', array('limit' => $maximumPhotoCount))); return false; } $currentPosition = $this->get('content_count'); $addedPhotoIds = array(); foreach ($photoData as $photoDataId => $_photoData) { if (isset($deletes[$photoDataId])) { continue; } $photoDw = XenForo_DataWriter::create('sonnb_XenGallery_DataWriter_Photo', XenForo_DataWriter::ERROR_ARRAY); $photoDw->bulkSet(array('user_id' => $visitor['user_id'], 'username' => $visitor['username'], 'album_id' => $this->get('album_id'), 'content_data_id' => $_photoData['content_data_id'], 'title' => isset($photoDataInsert['content_title'][$photoDataId]) ? $photoDataInsert['content_title'][$photoDataId] : '', 'description' => isset($photoDataInsert['content_description'][$photoDataId]) ? $photoDataInsert['content_description'][$photoDataId] : '', 'content_location' => isset($photoDataInsert['content_location'][$photoDataId]) ? $photoDataInsert['content_location'][$photoDataId] : '', 'position' => $currentPosition)); $currentPosition++; if ($xenOptions->sonnbXG_nudityFilter) { $file = $photoDataModel->getContentDataFile($_photoData); if ($xenOptions->sonnbXG_disableOriginal) { $file = $photoDataModel->getContentDataLargeThumbnailFile($_photoData); } $score = sonnb_XenGallery_Model_PhotoFilter::getInstance()->getScore($file); if ($score > $xenOptions->sonnbXG_nudityScore) { switch ($xenOptions->sonnbXG_nudityAction) { case 'refuse': $this->error(new XenForo_Phrase('sonnb_xengallery_your_photo_might_contain_nudity_content')); continue; break; case 'moderated': $photoDw->set('content_state', 'moderated'); break; default: break; } } } $photoDw->set('photo_exif', $photoModel->getPhotoExif($_photoData)); if (isset($privacy['photo_allow_view'])) { $photoDw->set('content_privacy', array('allow_view' => $privacy['photo_allow_view'], 'allow_comment' => $privacy['photo_allow_comment'])); $photoDw->insertCustomPrivacy(array('allow_view_username' => $privacy['photo_allow_view_username'], 'allow_comment_username' => $privacy['photo_allow_comment_username'])); } $photoDw->preSave(); if ($errors = $photoDw->getErrors()) { $this->mergeErrors($errors); } else { $photoDw->save(); $photo = $photoDw->getMergedData(); if ($photo['content_state'] === 'visible') { $addedPhotoIds[] = $photo['content_id']; } if (!empty($coverContentDataId) && $coverContentDataId == $photoDataId) { $this->set('cover_content_id', $photo['content_id'], '', array('setAfterPreSave' => true)); $this->_db->update('sonnb_xengallery_album', array('cover_content_id' => $photo['content_id']), array('album_id = ?' => $this->get('album_id'))); } $locationLatlng = array('location_name' => isset($photoDataInsert['content_location'][$photoDataId]) ? $photoDataInsert['content_location'][$photoDataId] : '', 'location_lat' => isset($photoDataInsert['location_lat'][$photoDataId]) ? $photoDataInsert['location_lat'][$photoDataId] : 0, 'location_lng' => isset($photoDataInsert['location_lng'][$photoDataId]) ? $photoDataInsert['location_lng'][$photoDataId] : 0); $this->_getLocationModel()->insertLocation(sonnb_XenGallery_Model_Photo::$contentType, $photo['content_id'], $locationLatlng); unset($photoDataInsert['content_title'][$photoDataId]); unset($photoDataInsert['content_description'][$photoDataId]); unset($photoDataInsert['content_location'][$photoDataId]); unset($photoDataInsert['location_lat'][$photoDataId]); unset($photoDataInsert['location_lng'][$photoDataId]); if (!empty($photoDataInsert['content_people'][$photoDataId])) { /* @var $tagModel sonnb_XenGallery_Model_Tag */ $tagModel = $this->getModelFromCache('sonnb_XenGallery_Model_Tag'); $tagModel->addTagUsers($photoDataInsert['content_people'][$photoDataId], sonnb_XenGallery_Model_Photo::$contentType, $photo['content_id'], true); } if (!$this->get('cover_content_id')) { $this->set('cover_content_id', $photo['content_id'], '', array('setAfterPreSave' => true)); $this->set('cover_content_type', $photo['content_type'], '', array('setAfterPreSave' => true)); } $this->_db->update('sonnb_xengallery_content_data', array('temp_hash' => '', 'unassociated' => 0), array('content_data_id = ?' => $photoDataId)); } } $this->set('photo_count', $this->get('photo_count') + count($addedPhotoIds), '', array('setAfterPreSave' => true)); $this->set('content_count', $this->get('content_count') + count($addedPhotoIds), '', array('setAfterPreSave' => true)); if ($this->isUpdate() && count($addedPhotoIds) > 0) { $this->_triggerNewPhotosAdded(count($addedPhotoIds), $addedPhotoIds); } } foreach ($photoDataInsert['content_description'] as $photoDataId => $description) { $photoDw = XenForo_DataWriter::create('sonnb_XenGallery_DataWriter_Photo', XenForo_DataWriter::ERROR_ARRAY); $photoDw->setExistingData(array('content_data_id' => $photoDataId)); if ($photoDw->hasErrors()) { continue; } $_photo = $photoDw->getMergedData(); if (!$photoModel->canEditContent($_photo)) { continue; } if (isset($deletes[$photoDataId])) { if (!$photoModel->canDeleteContent($_photo, 'soft')) { continue; } if ($photoModel->canDeleteContent($_photo, 'hard')) { $photoDw->delete(); continue; } else { $photoDw->set('content_state', 'deleted'); } } $photoDw->bulkSet(array('title' => isset($photoDataInsert['content_title'][$photoDataId]) ? $photoDataInsert['content_title'][$photoDataId] : '', 'description' => isset($photoDataInsert['content_description'][$photoDataId]) ? $photoDataInsert['content_description'][$photoDataId] : '', 'content_location' => isset($photoDataInsert['content_location'][$photoDataId]) ? $photoDataInsert['content_location'][$photoDataId] : '')); $photoDw->preSave(); if ($errors = $photoDw->getErrors()) { $this->mergeErrors($errors); } else { $photoDw->save(); $this->_getTagModel()->addTagUsers($photoDataInsert['content_people'][$photoDataId], sonnb_XenGallery_Model_Photo::$contentType, $photoDw->get('content_id'), true); if ($photoDw->isChanged('content_location')) { $locationLatlng = array('location_name' => isset($photoDataInsert['content_location'][$photoDataId]) ? $photoDataInsert['content_location'][$photoDataId] : '', 'location_lat' => isset($photoDataInsert['location_lat'][$photoDataId]) ? $photoDataInsert['location_lat'][$photoDataId] : '', 'location_lng' => isset($photoDataInsert['location_lng'][$photoDataId]) ? $photoDataInsert['location_lng'][$photoDataId] : ''); $this->_getLocationModel()->insertLocation(sonnb_XenGallery_Model_Photo::$contentType, $photoDw->get('content_id'), $locationLatlng); } } } }
public function actionDoUpload() { $this->_assertPostOnly(); $input = $this->_input->filter(array('hash' => XenForo_Input::STRING, 'album_id' => XenForo_Input::UINT)); if (!$input['hash']) { $input['hash'] = $this->_input->filterSingle('temp_hash', XenForo_Input::STRING); } $this->_assertPhpUploadError(); $this->_assertCanUploadContents(); $contentDataModel = $this->_getPhotoDataModel(); $contentModel = $this->_getPhotoModel(); $conditions = array('content_type' => sonnb_XenGallery_Model_Photo::$contentType); $existingPhotoData = $input['album_id'] ? $this->_getPhotoModel()->countContentsByAlbumId($input['album_id'], $conditions) : 0; $newPhotoData = $contentDataModel->countDataByHash($input['hash'], $conditions); $maxPhotoCounts = $contentModel->getPhotoCountLimit(); if ($maxPhotoCounts > 0 && $existingPhotoData + $newPhotoData >= $maxPhotoCounts) { return $this->responseError(new XenForo_Phrase('sonnb_xengallery_you_may_upload_maximum_x_photos_to_this_album_only', array('total' => $maxPhotoCounts))); } $contentDataConstraints = $contentModel->getPhotoDataConstraints(); if ($contentDataConstraints['count'] > 0) { $remainingUploads = $contentDataConstraints['count'] - (count($existingPhotoData) + count($newPhotoData)); if ($remainingUploads <= 0) { return $this->responseError(new XenForo_Phrase('you_may_not_upload_more_files_with_message_allowed_x', array('total' => $contentDataConstraints['count']))); } } $file = sonnb_XenGallery_Model_PhotoUpload::getUploadedFile('upload'); if (!$file) { return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS, $this->_buildLink('gallery/photos/upload', false, array('hash' => $input['hash'], 'album_id' => $input['album_id']))); } $file->setConstraints($contentDataConstraints); if (!$file->isImage()) { return $this->responseError(new XenForo_Phrase('sonnb_xengallery_our_gallery_currently_accepts_images_only')); } if (!$file->isValid()) { return $this->responseError($file->getErrors()); } if (!$contentModel->canResizeImage($file->getImageInfoField('width'), $file->getImageInfoField('height'))) { return $this->responseError(new XenForo_Phrase('sonnb_xengallery_your_photo_is_too_big')); } $xenOptions = XenForo_Application::getOptions(); if ($xenOptions->sonnbXG_nudityFilter && $xenOptions->sonnbXG_nudityAction === 'refuse') { //TODO: Prevent timeout on slow server $score = sonnb_XenGallery_Model_PhotoFilter::getInstance()->getScore($file->getTempFile()); if ($score > $xenOptions->sonnbXG_nudityScore) { return $this->responseError(new XenForo_Phrase('sonnb_xengallery_your_photo_might_contain_nudity_content')); } } if (!$xenOptions->sonnbXG_enableResize) { if ($contentDataConstraints['width'] && $file->getImageInfoField('width') > $contentDataConstraints['width'] || $contentDataConstraints['height'] && $file->getImageInfoField('height') > $contentDataConstraints['height']) { return $this->responseError(new XenForo_Phrase('sonnb_xengallery_your_photo_is_too_big')); } } $extras = array('temp_hash' => $input['hash'], 'content_type' => sonnb_XenGallery_Model_Photo::$contentType); $contentData = $contentDataModel->insertUploadedPhotoData($file, $extras); if (empty($contentData['content_data_id'])) { return $this->responseError(new XenForo_Phrase('sonnb_xengallery_error_occurred_while_uploading')); } $message = new XenForo_Phrase('upload_completed_successfully'); if ($this->_noRedirect()) { $viewParams = array('album' => $this->_getAlbumModel()->getAlbumById($input['album_id']), 'content' => $contentDataModel->prepareDataSingle($contentData), 'message' => $message); return $this->responseView('sonnb_XenGallery_ViewPublic_Photo_DoUpload', '', $viewParams); } else { return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS, $this->_buildLink('gallery/photos/upload', false, array('hash' => $input['hash'], 'album_id' => $input['album_id'])), $message); } }