public function executeUpload_audio(sfWebRequest $request)
 {
     $id = $request->getParameter('id');
     $filename = $request->getParameter('name');
     $this->forward404Unless($id && $filename);
     $valid_episode = $this->validateEpisodeForAudioUpload($id, $filename);
     $this->forward404Unless($valid_episode);
     // Settings
     $targetDir = rtrim(ProjectConfiguration::getEpisodeAudioFileLocalDirectory(), '/');
     //$targetDir = 'uploads/';
     //$cleanupTargetDir = false; // Remove old files
     //$maxFileAge = 60 * 60; // Temp file age in seconds
     // 5 minutes execution time
     @set_time_limit(5 * 60);
     $fileName = $this->getUser()->getAttribute('valid_episode_audio_file_hash', '');
     return $this->handlePlupload($request, $targetDir, $fileName);
 }
 public function executeAudio(sfWebRequest $request)
 {
     $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']);
     $this->forward404Unless($episode && $episode->getId());
     /* @var $episode Episode */
     // If the episode's file is remote, everyone is allowed to download the file from Amazon
     if ($episode->getFileIsRemote()) {
         $this->redirect($episode->getRemoteUrl());
     }
     // If the episode is not released, only the admins and moderators can view it.
     $permission = $this->verifyPermissionsForCurrentUser($episode->getSubredditId(), array('admin', 'moderator'));
     // Unless the owner of the episode is trying to download it.  That's okay.
     $assignment_data = Api::getInstance()->setUser($auth_key)->get('episodeassignment/' . $episode->getEpisodeAssignmentId(), true);
     $assignment = ApiDoctrine::createQuickObject($assignment_data['body']);
     $this->forward404Unless($permission || $assignment->getSfGuardUserId() == $this->getUser()->getApiUserId());
     // Check to make sure that the local file is there; if not try to get it from the Application bucket.
     $file_location = rtrim(ProjectConfiguration::getEpisodeAudioFileLocalDirectory(), '/') . '/';
     if (!file_exists($file_location . $episode->getAudioFile())) {
         $episode->pullAudioFileFromApplicationBucket();
         $this->forward404If(!file_exists($file_location . $episode->getAudioFile()));
     }
     // Now that we're serving the local file, let's set up the server to serve it.
     header('Content-Disposition: attachment;filename=' . $episode->getNiceFilename());
     switch ($request->getParameter('format')) {
         case "wma":
             header("Content-Type: audio/x-ms-wma");
             break;
         case "m4a":
             header("Content-Type: audio/mp4a-latm");
             break;
         case "ogg":
             header("Content-Type: application/ogg");
             break;
         default:
         case 'mp3':
             header("Content-Type: audio/mpeg");
             break;
     }
     // Check if we're using nginx and if nxginx and XSendfile are installed and use that
     if (array_key_exists('SERVER_SOFTWARE', $_SERVER) && preg_match('/nginx/i', $_SERVER['SERVER_SOFTWARE'])) {
         header("X-Accel-Redirect: /audio/temp/" . $episode->getAudioFile());
         die;
     }
     // If not, check if Apache has mod_xsendfile and use that
     if (array_key_exists('SERVER_SOFTWARE', $_SERVER) && preg_match('/apache/i', $_SERVER['SERVER_SOFTWARE']) && in_array('mod_xsendfile', apache_get_modules())) {
         header('X-Sendfile: ' . sfConfig::get('sf_data_dir') . '/temp/' . $episode->getAudioFile());
         die;
     }
     // If not that, then we'll try and see if we have lighttpd
     if (array_key_exists('SERVER_SOFTWARE', $_SERVER) && preg_match('/lighttpd/i', $_SERVER['SERVER_SOFTWARE'])) {
         header('X-LIGHTTPD-send-file: ' . sfConfig::get('sf_data_dir') . '/temp/' . $episode->getAudioFile());
         die;
     }
     // Otherwise, let's waste time by loading the file into memory and serving it through PHP (horror!  Not a joke!)
     if (sfConfig::get('app_enable_slow_audio_download', false)) {
         $filename = sfConfig::get('sf_data_dir') . '/temp/' . $episode->getAudioFile();
         header("Content-type: application/octet-stream");
         header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
         header("Content-Length: " . filesize($filename));
         readfile($filename);
         die;
     }
 }
 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);
 }
 /**
  * Retrieves a  collection of Episode objects
  * @param   sfWebRequest   $request a request object
  * @return  string
  */
 public function executeUpload(sfWebRequest $request)
 {
     // PUT makes more sense, but I am limited currently by my API to POST.
     $this->forward404Unless($request->isMethod(sfRequest::POST));
     $content = $request->getContent();
     // Restores backward compatibility. Content can be the HTTP request full body, or a form encoded "content" var.
     if (strpos($content, 'content=') === 0 || $request->hasParameter('content')) {
         $content = $request->getParameter('content');
     }
     $request->setRequestFormat('html');
     try {
         $parameters = $request->getParameterHolder()->getAll();
         $params = $this->getApiAuthFieldValues($parameters, $content);
         $this->validateUpload($content, $request);
     } catch (Exception $e) {
         $this->getResponse()->setStatusCode($e->getCode() ? $e->getCode() : 406);
         $serializer = $this->getSerializer();
         $this->getResponse()->setContentType($serializer->getContentType());
         $error = $e->getMessage();
         // event filter to enable customisation of the error message.
         $result = $this->dispatcher->filter(new sfEvent($this, 'sfDoctrineRestGenerator.filter_error_output'), $error)->getReturnValue();
         if ($error === $result) {
             $error = array(array('message' => $error));
             $this->output = $serializer->serialize($error, 'error');
         } else {
             $this->output = $serializer->serialize($result);
         }
         $this->setTemplate('index');
         return sfView::SUCCESS;
     }
     // We move the file from its temporary location to the Episode in question.
     if ($this->_nice_filename && $this->_temporary_file_location) {
         $targetDir = rtrim(ProjectConfiguration::getEpisodeAudioFileLocalDirectory(), '/');
         $pattern = '/\\.([^\\.]+)$/';
         preg_match($pattern, $filename, $matches);
         $extension = array_key_exists(1, $matches) ? $matches[1] : 'mp3';
         // We don't need the upload hash because we're not uploading AJAX-like in real time.
         $hash = sha1(microtime() . $this->object->getIncremented());
         $fileName = $hash . '.' . $extension;
         //Move the file.
         rename($this->_temporary_file_location, $targetDir . '/' . $fileName);
         // update and save it
         $this->object->setAudioFile($fileName);
         $this->object->setNiceFilename($this->_nice_filename);
     }
     return $this->doSave($params);
 }