public function executeBackup(sfWebRequest $request)
 {
     $this->redirectUnless($this->getUser()->isAuthenticated() && $this->getUser()->getApiUserId(), '@sf_guard_signin');
     $auth_key = $this->getUser()->getApiAuthKey();
     $subreddit_data = Api::getInstance()->setUser($auth_key)->get('subreddit/' . $request->getParameter('id'), true);
     $subreddit = ApiDoctrine::createObject('Subreddit', $subreddit_data['body']);
     $quick_subreddit = ApiDoctrine::createQuickObject($subreddit_data['body']);
     $this->forward404Unless($subreddit && $subreddit->getId());
     $this->setLayout(false);
     if ($request->getParameter('which') == 'intro') {
         $file_location = rtrim(ProjectConfiguration::getSubredditAudioFileLocalDirectory(), '/') . '/';
         $filename = $subreddit->getEpisodeIntro();
         if (file_exists($file_location . $filename)) {
             ProjectConfiguration::registerAws();
             $response = $subreddit->saveFileToApplicationBucket($file_location, $filename, 'intro');
             if ($response->isOK()) {
                 unlink($file_location . $filename);
             }
         }
     } elseif ($request->getParameter('which') == 'outro') {
         $file_location = rtrim(ProjectConfiguration::getSubredditAudioFileLocalDirectory(), '/') . '/';
         $filename = $subreddit->getEpisodeOutro();
         if (file_exists($file_location . $filename)) {
             ProjectConfiguration::registerAws();
             $response = $subreddit->saveFileToApplicationBucket($file_location, $filename, 'outro');
             if ($response->isOK()) {
                 unlink($file_location . $filename);
             }
         }
     }
 }
 protected static function sendUsingAmazonSES()
 {
     ProjectConfiguration::registerAws();
     $email = new AmazonSES();
     $message = array('Subject.Data' => self::$_subject, 'Body.Text.Data' => self::$_message);
     $opt = array('ReplyToAddresses' => self::$_from);
     if (self::$_html_message) {
         $message['Body.Html.Data'] = self::$_html_message;
     }
     $response = $email->send_email(self::$_from, array('ToAddresses' => self::$_to), $message, $opt);
     return $response->isOK();
 }
 public function executeBackup(sfWebRequest $request)
 {
     $this->redirectUnless($this->getUser()->isAuthenticated() && $this->getUser()->getApiUserId(), '@sf_guard_signin');
     $auth_key = $this->getUser()->getApiAuthKey();
     $episode_data = Api::getInstance()->setUser($auth_key)->get('episode/' . $request->getParameter('id'), true);
     $episode = ApiDoctrine::createObject('Episode', $episode_data['body']);
     $quick_episode = ApiDoctrine::createQuickObject($episode_data['body']);
     $this->forward404Unless($episode && $episode->getId());
     $this->forward404Unless(strtotime($quick_episode->getReleaseDate()) >= time());
     // If the episode is not released, only the admins and moderators can view it.
     $permission = $this->verifyPermissionsForCurrentUser($quick_episode->getSubredditId(), array('admin'));
     // Unless the owner of the episode is trying to edit it.  That's okay.
     $assignment_data = Api::getInstance()->setUser($auth_key)->get('episodeassignment/' . $quick_episode->getEpisodeAssignmentId(), true);
     $assignment = ApiDoctrine::createQuickObject($assignment_data['body']);
     $this->forward404Unless($permission || $assignment && $assignment->getSfGuardUserId() == $this->getUser()->getApiUserId());
     $this->setLayout(false);
     ProjectConfiguration::registerAws();
     if ($request->getParameter('which') == 'graphic') {
         $file_location = rtrim(ProjectConfiguration::getEpisodeGraphicFileLocalDirectory(), '/') . '/';
         $filename = $episode->getGraphicFile();
         if (file_exists($file_location . $filename)) {
             $result = $episode->saveFileToApplicationBucket($file_location, $filename, 'upload', AmazonS3::ACL_PUBLIC);
             if ($result->isOk()) {
                 unlink($file_location . $filename);
             }
         } else {
             echo $file_location . $filename;
         }
     } elseif ($request->getParameter('which') == 'audio') {
         $file_location = rtrim(ProjectConfiguration::getEpisodeAudioFileLocalDirectory(), '/') . '/';
         $filename = $episode->getAudioFile();
         if (file_exists($file_location . $filename)) {
             $episode->saveFileToApplicationBucket($file_location, $filename, 'audio');
         }
     }
 }
 public function removeFileFromApplicationBucket($filename, $prefix)
 {
     ProjectConfiguration::registerAws();
     $s3 = new AmazonS3();
     $bucket = ProjectConfiguration::getApplicationAmazonBucketName();
     if ($s3->if_bucket_exists($bucket)) {
         $response = $s3->delete_object($bucket, $prefix . '/' . $filename);
         if (!$response->isOK()) {
             throw new Exception("Error deleting file!");
         }
     } else {
         throw new Exception("Amazon bucket '{$bucket}' does not exist!");
     }
     return $response;
 }
 /**
  * We set up the following situation:
  * We have three users, each of different AuthorTypes, who have 
  * EpisodeAssignments with the Episode in question.  The deadline for the 
  * first user has passed, but the deadline for the second has not.  The 
  * third user is in an AuthorType that should prevent creating an 
  * AuthorType for the Episode before the second user's deadline passes, even
  * though that User is an admin.
  * 
  * The second user (the one who is able to submit their episode) is a
  * moderator, but she should not be able to approve her own episode.
  */
 public function setUp()
 {
     ProjectConfiguration::registerAws();
     $original_filename = '1234567890abcde.mp3';
     $this->episode_filename = 'abcde0123456789.mp3';
     $this->unapproved_file_location = sfConfig::get('sf_data_dir') . '/temp/';
     if (!copy($this->unapproved_file_location . $original_filename, $this->unapproved_file_location . $this->episode_filename)) {
         echo "failed to copy {$this->unapproved_file_location}{$original_filename}...\n";
     }
     $this->aws = new AmazonS3();
     $this->subreddit = new Subreddit();
     $this->subreddit->setName(rand(0, 1000));
     $this->subreddit->setDomain(rand(0, 1000));
     $this->subreddit->save();
     $first = AuthorTypeTable::getInstance()->findOneByType('squid');
     $understudy = AuthorTypeTable::getInstance()->findOneByType('shark');
     $dark_horse = AuthorTypeTable::getInstance()->findOneByType('blue_whale');
     $this->first_deadline = new Deadline();
     $this->first_deadline->setAuthorType($first);
     $this->first_deadline->setSeconds(1000);
     $this->first_deadline->setSubreddit($this->subreddit);
     $this->first_deadline->save();
     $this->second_deadline = new Deadline();
     $this->second_deadline->setAuthorType($understudy);
     $this->second_deadline->setSeconds(500);
     $this->second_deadline->setSubreddit($this->subreddit);
     $this->second_deadline->save();
     $this->third_deadline = new Deadline();
     $this->third_deadline->setAuthorType($dark_horse);
     $this->third_deadline->setSeconds(100);
     $this->third_deadline->setSubreddit($this->subreddit);
     $this->third_deadline->save();
     $this->user = new sfGuardUser();
     $this->user->setEmailAddress(rand(0, 1000));
     $this->user->setUsername(rand(0, 1000));
     $this->user->setIsValidated(1);
     $this->user->save();
     $this->after_deadline_user = new sfGuardUser();
     $this->after_deadline_user->setEmailAddress(rand(0, 1000));
     $this->after_deadline_user->setUsername(rand(0, 1000));
     $this->after_deadline_user->setIsValidated(1);
     $this->after_deadline_user->save();
     $this->dark_horse_user = new sfGuardUser();
     $this->dark_horse_user->setEmailAddress(rand(0, 1000));
     $this->dark_horse_user->setUsername(rand(0, 1000));
     $this->dark_horse_user->setIsValidated(1);
     $this->dark_horse_user->save();
     $this->approver = new sfGuardUser();
     $this->approver->setEmailAddress(rand(0, 1000));
     $this->approver->setUsername(rand(0, 1000));
     $this->approver->setIsValidated(1);
     $this->approver->save();
     $moderator = MembershipTable::getInstance()->findOnebyType('moderator');
     $admin = MembershipTable::getInstance()->findOnebyType('admin');
     $member = MembershipTable::getInstance()->findOnebyType('user');
     $this->first_membership = new sfGuardUserSubredditMembership();
     $this->first_membership->setMembership($member);
     $this->first_membership->setSubreddit($this->subreddit);
     $this->first_membership->setSfGuardUser($this->after_deadline_user);
     $this->first_membership->save();
     $this->second_membership = new sfGuardUserSubredditMembership();
     $this->second_membership->setMembership($moderator);
     $this->second_membership->setSubreddit($this->subreddit);
     $this->second_membership->setSfGuardUser($this->user);
     $this->second_membership->save();
     $this->third_membership = new sfGuardUserSubredditMembership();
     $this->third_membership->setMembership($admin);
     $this->third_membership->setSubreddit($this->subreddit);
     $this->third_membership->setSfGuardUser($this->dark_horse_user);
     $this->third_membership->save();
     $this->fourth_membership = new sfGuardUserSubredditMembership();
     $this->fourth_membership->setMembership($moderator);
     $this->fourth_membership->setSubreddit($this->subreddit);
     $this->fourth_membership->setSfGuardUser($this->approver);
     $this->fourth_membership->save();
     $this->episode = new Episode();
     $this->episode->setReleaseDate(date('Y-m-d H:i:s', time() + 20000));
     $this->episode->setAudioFile($this->episode_filename);
     $this->episode->setNiceFilename('14. We Will Rock You.mp3');
     //$this->episode->setSfGuardUserId($this->user);
     $this->episode->setDescription('This is a test.');
     $this->episode->setTitle('Test Episode');
     $this->episode->setIsNsfw(false);
     $this->episode->setSubreddit($this->subreddit);
     $this->episode->save();
     $this->first_ep_assignment = new EpisodeAssignment();
     $this->first_ep_assignment->setEpisodeId($this->episode->getIncremented());
     $this->first_ep_assignment->setAuthorType($first);
     $this->first_ep_assignment->setSfGuardUser($this->after_deadline_user);
     $this->first_ep_assignment->save();
     $this->second_ep_assignment = new EpisodeAssignment();
     $this->second_ep_assignment->setEpisodeId($this->episode->getIncremented());
     $this->second_ep_assignment->setAuthorType($understudy);
     $this->second_ep_assignment->setSfGuardUser($this->user);
     $this->second_ep_assignment->save();
     $this->third_ep_assignment = new EpisodeAssignment();
     $this->third_ep_assignment->setEpisodeId($this->episode->getIncremented());
     $this->third_ep_assignment->setAuthorType($dark_horse);
     $this->third_ep_assignment->setSfGuardUser($this->dark_horse_user);
     try {
         $this->third_ep_assignment->save();
     } catch (sfException $exception) {
         unset($exception);
     }
     $this->episode->setReleaseDate(date('Y-m-d H:i:s', time() + 750));
     $this->episode->save();
 }
 public function save(Doctrine_Connection $conn = null)
 {
     if (!$this->isNew() && !$this->getSkipBackup() && in_array('graphic_file', $this->_modified) && $this->_get('graphic_file')) {
         $file_location = rtrim(ProjectConfiguration::getEpisodeGraphicFileLocalDirectory(), '/') . '/';
         $filename = $this->_get('graphic_file');
         if (file_exists($file_location . $filename)) {
             ProjectConfiguration::registerAws();
             $response = $this->saveFileToApplicationBucket($file_location, $filename, 'upload', AmazonS3::ACL_PUBLIC);
             if ($response->isOK()) {
                 unlink($file_location . $filename);
             }
         }
     }
     if (!$this->isNew() && !$this->getSkipBackup() && in_array('audio_file', $this->_modified) && $this->_get('audio_file')) {
         $file_location = rtrim(ProjectConfiguration::getEpisodeAudioFileLocalDirectory(), '/') . '/';
         $filename = $this->_get('audio_file');
         if (file_exists($file_location . $filename)) {
             ProjectConfiguration::registerAws();
             $response = $this->saveFileToApplicationBucket($file_location, $filename, 'audio');
         }
     }
     if (!$this->isNew() && in_array('is_submitted', $this->_modified) && $this->_get('is_submitted')) {
         /* The episode has been submitted.  We need to send an email about
          * it to the subreddit moderators.
          */
         $types = array('moderator');
         $memberships = sfGuardUserSubredditMembershipTable::getInstance()->getAllBySubredditAndMemberships($this->getSubredditId(), $types);
         $initial_is_submitted = $this->_get('is_submitted');
         $initial_submitted_at = $this->_get('submitted_at');
         foreach ($memberships as $membership) {
             $user = $membership->getSfGuardUser();
             $parameters = array('user_id' => $membership->getSfGuardUserId(), 'episode_id' => $this->getIncremented());
             $prefer_html = $user->getPreferHtml();
             $address = $user->getEmailAddress();
             $name = $user->getPreferredName() ? $user->getPreferredName() : $user->getFullName();
             $email = EmailTable::getInstance()->getFirstByEmailTypeAndLanguage('EpisodeApprovalPending', $user->getPreferredLanguage());
             $subject = $email->generateSubject($parameters);
             $body = $email->generateBodyText($parameters, $prefer_html);
             $from = sfConfig::get('app_email_address', ProjectConfiguration::getApplicationName() . ' <' . ProjectConfiguration::getApplicationEmailAddress() . '>');
             AppMail::sendMail($address, $from, $subject, $body, $prefer_html ? $body : null);
             $user->addLoginMessage('You have Episodes awaiting your approval.');
         }
         // @todo: The previous foreach loop sets the 'is_submitted' and 'submitted_at' columns to null.  I don't know why.
         $this->_set('is_submitted', $initial_is_submitted);
         $this->_set('submitted_at', $initial_submitted_at);
     }
     return parent::save($conn);
 }