Esempio n. 1
0
 public function indexAction()
 {
     $cache = $this->di->get('cache');
     $rating_query = '(true = true)';
     $rating_cache = '';
     if ($this->fa->canSeeArt('adult')) {
         $rating_cache = 'gma';
     } elseif ($this->fa->canSeeArt('mature')) {
         $rating_query = '(up.rating = ' . Upload::RATING_GENERAL . ' OR up.rating = ' . Upload::RATING_MATURE . ')';
         $rating_cache = 'gm-';
     } else {
         $rating_query = '(up.rating = ' . Upload::RATING_GENERAL . ')';
         $rating_cache = 'g--';
     }
     $frontpage_cache_key = 'fa.frontpage.recent' . $rating_cache;
     $frontpage_cache_lifetime = 30;
     // 30 seconds
     $frontpage_data = $cache->load($frontpage_cache_key);
     if (!$frontpage_data) {
         $frontpage_data = array();
         $record_types = array('images' => Upload::TYPE_IMAGE, 'audio' => Upload::TYPE_AUDIO, 'text' => Upload::TYPE_TEXT);
         $type_records_query = $this->em->createQuery('SELECT up.id, up.title, up.description, up.rating, up.thumbnail, us.username, us.lower, us.avatar_mtime FROM Entity\\Upload up JOIN up.user us WHERE up.upload_type = :type AND up.is_scrap = 0 AND ' . $rating_query . ' ORDER BY up.id DESC')->setMaxResults(21);
         foreach ($record_types as $type_key => $type_code) {
             $type_records_raw = $type_records_query->setParameter('type', $type_code)->getArrayResult();
             $type_records = array();
             foreach ($type_records_raw as $record) {
                 if ($record['rating'] != Upload::RATING_GENERAL) {
                     $this->fa->setPageHasMatureContent(true);
                 }
                 $record['rating_text'] = Upload::getRatingText($record['rating']);
                 $record['thumbnail_url'] = Upload::getFileUrl($record['thumbnail']);
                 $record['avatar'] = User::getUserAvatar($record['lower'], $record['avatar_mtime']);
                 $type_records[$record['id']] = $record;
             }
             $frontpage_data[$type_key] = $type_records;
         }
         $cache->set($frontpage_data, $frontpage_cache_key, $frontpage_cache_lifetime);
     }
     $this->view->records = $frontpage_data;
 }
 public function favoriteAction()
 {
     $csrf_key = $this->getParam('key');
     // Verify this isn't a cross-domain attack before proceeding
     if ($this->csrf->verify($csrf_key, '_upload_content')) {
         // TODO: Check comment hiding rate limit here!
         $upload_id = (int) $this->getParam('id');
         $upload = Upload::find($upload_id);
         // Verifying if the Upload exists
         if (!$upload instanceof Upload) {
             throw new \FA\Exception('Upload not found!');
         }
         $favorite = $this->em->createQuery('SELECT f FROM \\Entity\\Favorite f WHERE f.upload_id = :upload_id')->setParameter('upload_id', $upload->id)->getArrayResult();
         // Verify the user can even progress further
         //self::_userCheck($upload);
         // If the favorite exists, delete it. If not, create it!
         // TODO: Look into soft deleting
         if (count($favorite) > 0) {
             \FA\Utilities::print_r($favorite);
         } else {
             $favorite = new Favorite();
             $favorite->upload = $upload;
             $favorite->user = $this->user;
             $favorite->save();
         }
         // Redirect to the Upload page to our comment!
         // TODO: Add a way to auto-lock on to the comment. Maybe a perma-link style approach?
         return $this->redirectToName('upload_view', array('id' => $upload->id));
     }
 }
Esempio n. 3
0
 public function indexAction()
 {
     // Compile all social info.
     $owner_social = array();
     $social_types = $this->config->fa->social->toArray();
     $escaper = new \Phalcon\Escaper();
     foreach ($social_types as $social_category => $social_items) {
         foreach ($social_items as $social_type => $social_info) {
             $owner_social_item = $this->owner->contact->{$social_type};
             if (empty($owner_social_item)) {
                 continue;
             }
             $social_image = $this->url->getStatic('img/contact/' . $social_type . '.gif');
             $social_title = $social_info['name'] . ': ' . $escaper->escapeHtmlAttr($owner_social_item);
             if ($social_info['format']) {
                 $social_url = sprintf($social_info['format'], $escaper->escapeUrl($owner_social_item));
                 $owner_social[] = '<a href="' . $social_url . '" target="_blank"><img class="contacticon" src="' . $social_image . '" title="' . $social_title . '"></a>';
             } else {
                 $owner_social[] = '<img class="contacticon" src="' . $social_image . '" title="' . $social_title . '">';
             }
         }
     }
     $this->view->owner_social = $owner_social;
     // Commission information
     $has_commissions = $this->owner->commission_types->count() == 0;
     $this->view->has_commissions = $has_commissions;
     $this->view->accept_trades = $this->owner->getVariable('accept_trades');
     $this->view->accept_commissions = $this->owner->getVariable('accept_commissions');
     // Maturity Rating Filter
     if ($this->fa->canSeeArt('adult')) {
         $maturity_filter = array(Upload::RATING_GENERAL, Upload::RATING_ADULT, Upload::RATING_MATURE);
     } elseif ($this->fa->canSeeArt('mature')) {
         $maturity_filter = array(Upload::RATING_GENERAL, Upload::RATING_MATURE);
     } else {
         $maturity_filter = array(Upload::RATING_GENERAL);
     }
     // Profile picture.
     if ($this->owner->profile_pic) {
         $profile_pic = Upload::find($this->owner->profile_pic);
         if ($profile_pic instanceof Upload && in_array($profile_pic->rating, $maturity_filter)) {
             if ($profile_pic->rating == Upload::RATING_ADULT) {
                 $this->fa->setPageHasMatureContent();
             }
             $this->view->profile_pic = $profile_pic;
         }
     }
     // Featured picture
     if ($this->owner->featured) {
         $featured_pic = Upload::find($this->owner->featured);
         if ($featured_pic instanceof Upload && in_array($featured_pic->rating, $maturity_filter)) {
             if ($featured_pic->rating == Upload::RATING_ADULT) {
                 $this->fa->setPageHasMatureContent();
             }
             $this->view->featured_pic = $featured_pic;
         }
     }
     // Upload data
     $uploads = $this->em->createQuery('SELECT up FROM Entity\\Upload up WHERE up.is_scrap = 0 AND up.rating IN (:ratings) AND up.user_id = :user_id ORDER BY up.id DESC')->setParameter('ratings', $maturity_filter)->setParameter('user_id', $this->owner->id)->setMaxResults(14)->execute();
     if ($uploads) {
         foreach ($uploads as $row) {
             if ($row->rating == Upload::RATING_ADULT) {
                 $this->fa->setPageHasMatureContent();
             }
         }
         $this->view->latest_uploads = $uploads;
     }
     // Favorite filters
     $fav_maturity_filter = $maturity_filter;
     if ($this->acl->isAllowed('administer all') || $this->user->id == $this->owner->id) {
         $fav_filter = 'n';
     } else {
         $fav_filter = $this->owner->getVariable('hide_favorites');
     }
     switch ($fav_filter) {
         case 'e':
             // hide everything
             $fav_maturity_filter = null;
             break;
         case 'ma':
             // hide adult+mature
             unset($fav_maturity_filter[Upload::RATING_MATURE], $fav_maturity_filter[Upload::RATING_ADULT]);
             break;
         case 'a':
             // hide adult
             unset($fav_maturity_filter[Upload::RATING_ADULT]);
             break;
         case 'n':
             // hide nothing
         // hide nothing
         default:
             // No changes.
             break;
     }
     // Favorites
     if (!empty($fav_maturity_filter)) {
         $latest_faves = $this->em->createQuery('SELECT f, up FROM Entity\\Favorite f JOIN f.upload up WHERE f.user_id = :user_id AND up.rating IN (:ratings) ORDER BY f.id DESC')->setParameter('user_id', $this->owner->id)->setParameter('ratings', $fav_maturity_filter)->setMaxResults(14)->execute();
         if ($latest_faves) {
             foreach ($latest_faves as $row) {
                 if ($row->rating == Upload::RATING_ADULT) {
                     $this->fa->setPageHasMatureContent();
                 }
             }
             $this->view->latest_faves = $latest_faves;
         }
     }
     // Watched by / Is watching counts
     $watched_by_count = $this->em->createQuery('SELECT COUNT(w.id) FROM Entity\\Watch w WHERE w.target_id = :user_id')->setParameter('user_id', $this->owner->id)->getSingleScalarResult();
     $this->view->num_watched_by = $watched_by_count;
     $watching_count = $this->em->createQuery('SELECT COUNT(w.id) FROM Entity\\Watch w WHERE w.user_id = :user_id')->setParameter('user_id', $this->owner->id)->getSingleScalarResult();
     $this->view->num_watching = $watching_count;
     // Most recent journal
     $journal = $this->em->createQuery('SELECT j FROM Entity\\Journal j WHERE j.user_id = :user_id ORDER BY j.id DESC')->setParameter('user_id', $this->owner->id)->setMaxResults(1)->getOneOrNullResult();
     $this->view->journal = $journal;
     // Shouts
     $shouts = $this->em->createQuery('SELECT s, us FROM Entity\\Shout s JOIN s.sender us WHERE s.recipient_id = :user_id ORDER BY s.id DESC')->setParameter('user_id', $this->owner->id)->setMaxResults(12)->execute();
     $this->view->shouts = $shouts;
     // New shout form.
     $shout_form_config = $this->current_module_config->forms->shout->toArray();
     $shout_form_config['action'] = $this->url->routeFromHere(array('action' => 'shout'));
     $shout_form = new \FA\Form($shout_form_config);
     $this->view->shout_form = $shout_form;
 }
Esempio n. 4
0
 public function editAction()
 {
     $types = $this->config->fa->upload_types->toArray();
     $id = (int) $this->getParam('id');
     if ($id !== 0) {
         // Edit existing record.
         // TODO: Reimplement administrator access.
         $record = Upload::getRepository()->findOneBy(array('id' => $id, 'user_id' => $this->user->id));
         if (!$record instanceof Upload) {
             throw new \FA\Exception('Submission ID not found!');
         }
         $edit_mode = true;
         $type = $record->submission_type;
     } else {
         // Create new submission.
         $type = $this->getParam('type');
         // Show type selector if no type is specified.
         if (empty($type) || !isset($types[$type])) {
             $this->view->types = $types;
             return $this->view->pick('uploads/select');
         }
         $edit_mode = false;
         $record = NULL;
     }
     $type_info = $types[$type];
     $form_config = $this->current_module_config->forms->uploads_edit->toArray();
     // Create mode changes
     if (!$edit_mode) {
         $form_config['groups']['files']['elements']['submission'][1]['required'] = true;
         unset($form_config['groups']['files']['elements']['submission'][1]['description']);
         unset($form_config['groups']['files']['elements']['rebuild_thumbnail']);
     }
     // Changes to the form based on submission type.
     if (isset($type_info['category'])) {
         $form_config['groups']['metadata']['elements']['category'][1]['default'] = $type_info['category'];
     }
     if ($type !== Upload::TYPE_IMAGE) {
         unset($form_config['groups']['files']['elements']['rebuild_thumbnail']);
     }
     $form_config['groups']['files']['elements']['submission'][1]['allowedTypes'] = $type_info['types'];
     // Create the form class.
     $form = new \FA\Form($form_config);
     // Populate the form (if available).
     if ($record instanceof Upload) {
         $form->setDefaults($record->toArray(TRUE, TRUE));
     }
     // Handle form submission.
     if ($_POST && $this->request->hasFiles() && $form->isValid(array_merge($_POST, $_FILES))) {
         $data = $form->getValues();
         if (!$record instanceof Upload) {
             $record = new Upload();
             $record->upload_type = $type;
             $record->user = $this->user;
             $record->is_hidden = true;
             // Hide until properly populated.
             $record->save();
             // Immediately save to generate IDs used in next steps.
         }
         $record->fromArray($data);
         // Begin file handling.
         \IMagick::setResourceLimit(\Imagick::RESOURCETYPE_MEMORY, 32);
         \IMagick::setResourceLimit(\Imagick::RESOURCETYPE_MAP, 32);
         $files = $form->getFiles($this->request);
         $imagine = new Imagine();
         $submission_file = $files['submission'][0];
         $thumbnail_file = null;
         $thumbnail_paths = array();
         $preview_file = null;
         $preview_paths = array();
         if ($submission_file) {
             $submission_paths = $record->generatePaths($submission_file->getName());
             // Create the proper artwork directory if it doesn't exist.
             $submission_dir = dirname($submission_paths['full']['path']);
             @mkdir($submission_dir);
             if ($type == Upload::TYPE_IMAGE) {
                 // Handle image uploads.
                 $submission_image = $imagine->open($submission_file->getTempName());
                 $is_animated = count($submission_image->layers()) > 1;
                 // So, it seems Imagine really loves to screw up GIFs, so lets avoid that
                 if ($is_animated) {
                     $dest_path = $submission_paths['full']['path'];
                     // Copying this instead of moving due to the file being reused by preview/thumbnail
                     copy($submission_file->getTempName(), $dest_path);
                 } else {
                     $submission_image->save($submission_paths['full']['path'], array('animated' => $is_animated));
                 }
                 // Make this file the thumbnail if no other is specified.
                 if (empty($files['thumbnail']) && (!$edit_mode || $data['rebuild_thumbnail'])) {
                     $thumbnail_file = $submission_file;
                     $thumbnail_paths = $submission_paths;
                 }
                 // Set up the preview parameters
                 $preview_file = $submission_file;
                 $preview_paths = $submission_paths;
             } else {
                 // Handle non-images. Way simpler, right?
                 $dest_path = $submission_paths['full']['path'];
                 $submission_file->moveTo($dest_path);
                 // Prevent the file from being deleted below.
                 $submission_file = null;
             }
             $record->setFull($submission_paths['full']['base']);
         }
         // Use the thumbnail field if supplied.
         if (!empty($files['thumbnail'])) {
             $thumbnail_file = $files['thumbnail'][0];
             $thumbnail_paths = $record->generatePaths($thumbnail_file->getName());
         }
         // If we haven't set a preview image/path, then use the thumbnail if possible
         if (is_null($preview_file)) {
             $preview_file = $thumbnail_file;
             $preview_paths = $thumbnail_paths;
         }
         // Process either the uploaded thumbnail, or resize the original file to be our preview.
         if ($preview_file) {
             // Generate "small" size thumbnail.
             $preview_size = new Box(self::MAX_PREVIEW_SIZE, self::MAX_PREVIEW_SIZE);
             self::_saveAsJPG($imagine, $preview_file, $preview_paths['small']['path'], 90, $preview_size);
             $record->setSmall(self::_changeExtension($preview_paths['small']['base'], 'jpg'));
         }
         // Process either the uploaded thumbnail, or thumbnailize the original file.
         if ($thumbnail_file) {
             // Generate "thumb" size thumbnail.
             $thumbnail_size = new Box(self::MAX_THUMB_SIZE, self::MAX_THUMB_SIZE);
             self::_saveAsJPG($imagine, $thumbnail_file, $thumbnail_paths['thumbnail']['path'], 90, $thumbnail_size);
             $record->setThumbnail(self::_changeExtension($thumbnail_paths['thumbnail']['base'], 'jpg'));
         }
         // Delete the temp files (if not already moved).
         if ($submission_file) {
             @unlink($submission_file->getTempName());
         }
         if ($thumbnail_file) {
             @unlink($thumbnail_file->getTempName());
         }
         // Unhide the record that was hidden earlier.
         if (!$edit_mode) {
             $record->is_hidden = false;
         }
         $record->save();
         $view_url = $this->url->get('view/' . $record->id);
         $view_link = 'You can <a href="' . $view_url . '" target="_blank_">view your submission\'s public page here</a>.';
         if ($edit_mode) {
             $this->alert('<b>Submission Updated!</b><br>' . $view_link, 'green');
         } else {
             $this->alert('<b>New Submission Uploaded!</b><br>' . $view_link, 'green');
         }
         return $this->redirectFromHere(array('action' => 'index', 'id' => NULL, 'type' => NULL));
     }
     // Render the main form.
     $this->view->type_info = $type_info;
     $this->view->form = $form;
 }
Esempio n. 5
0
 public function indexAction()
 {
     $upload_id = (int) $this->getParam('id');
     // Grab the submission information (Which includes the uploader's info and comments)
     $upload = Upload::find($upload_id);
     if (!$upload instanceof Upload) {
         throw new \FA\Exception('Upload not found!');
     }
     $view = $this->view;
     $view->upload = $upload;
     $view->is_favorited = false ? '+Favorite' : '-Favorite';
     $view->comment_csrf_str = $this->csrf->generate('_upload_comments');
     $view->upload_csrf_str = $this->csrf->generate('_upload_content');
     $view->file_mime = $upload->getMIME();
     $view->keyword_arr = $upload->getKeywords();
     $view->created_at = \FA\Utilities::fa_date_format($upload->created_at, $upload->user->getTimezoneDiff());
     if ($this->user != NULL) {
         // Determine if the user is the owner of the upload
         $view->is_owner = $upload->user->id == $this->user->id;
         // Get if the user prefer fullview first
         $view->fullview = $this->user->fullview ? 'true' : 'false';
         // Apparently, Volt doesn't seem to want convert straight to string
     }
     // Comments!
     // Create the comment forms
     $form_config = $this->current_module_config->forms->upload_comment->toArray();
     $form_config['action'] = $this->url->named('upload_view', array('id' => $upload->id)) . '/comment/new';
     // Add the action so they can actually comment!
     $view->comment_form = new \FA\Form($form_config);
     // Reply form. Uses the same config, but different id.
     $form_config['action'] = '';
     // No need for this.
     $form_config['id'] = 'reply_form';
     $view->reply_form = new \FA\Form($form_config);
     // Edit form. Same story.
     $form_config['id'] = 'edit_form';
     $view->edit_form = new \FA\Form($form_config);
     // Construct the comments
     $comment_ents = \Entity\UploadComment::getRepository()->findBy(array('upload_id' => $upload->id), array('id' => 'DESC'));
     // TODO: Move to CommentTrait for a more global use
     // Initialize our upload comment array
     $up_comments = array();
     foreach ($comment_ents as $comment) {
         // Get the comment's parents
         $parent_path = array_reverse($comment->getParentPath());
         // Map the array, creating new arrays along the way
         $results = self::_mapArray($parent_path, array($comment), 'a');
         // Merge our new array with our overall one!
         $up_comments = array_merge_recursive($results, $up_comments);
     }
     // Flatten the array to allow Volt to run through it without issue
     $view->upload_comments = \Nette\Utils\Arrays::flatten($up_comments);
     // Only need to do these when users with access need to see these stats.
     if ($this->acl->isAllowed('administer all')) {
         $view->total_deleted_comments = 0;
         $view->total_deleted_comments_by_admin = 0;
         $view->total_deleted_comments_by_uploader = 0;
         $view->total_deleted_comments_by_poster = 0;
         // Get the total comments deleted
         foreach ($comment_ents as $comment) {
             $deleting_user = $comment->deleting_user;
             if ($deleting_user != NULL) {
                 // Post has been deleted
                 $view->total_deleted_comments++;
                 // Determine who deleted it!
                 if ($deleting_user->id == $comment->user_id) {
                     // Poster deleted it!
                     $view->total_deleted_comments_by_poster++;
                 } elseif ($deleting_user->id == $upload->user . id) {
                     // Uploader deleted it!
                     $view->total_deleted_comments_by_uploader++;
                 } elseif ($this->acl->userAllowed('administer all', $deleting_user)) {
                     // Admin deleted it!
                     $view->total_deleted_comments_by_admin++;
                 }
             }
         }
     }
     // Grab the EXIF info (If any) and pass it to the view
     // TODO: Will need to determine if we need to include more or less information
     //$exif = exif_read_data($upload->getFullPath(), 'EXIF');
     //$view->exif_info = ($exif ? $exif : '');
     // Legacy stuff
     // TODO: Move this off to either ACL or some other config. Most if not all is controller specific
     $view->edit_duration_sec = \Entity\UploadComment::getEditDuration();
     $view->STATIC_ASSET_MODIFICATION_DATE = self::STATIC_ASSET_MODIFICATION_DATE;
     // Assuming this is for versioning.
 }