public function save(Doctrine_Connection $conn = null)
 {
     $send_message = false;
     if ($this->isNew()) {
         if (!$this->hasVerifiedSender()) {
             $this->deleteWithException("Cannot create Message " . "because sfGuardUser " . $this->getSenderId() . " has not been validated yet.", 406);
         }
         if (!$this->hasVerifiedRecipient()) {
             $this->deleteWithException("Cannot create Message " . "because sfGuardUser " . $this->getSenderId() . " has not been validated yet.", 406);
         }
         $send_message = true;
     }
     /* If the obejct is not new or has passed all rules for saving, we pass
      * it on to the parent save function.
      */
     parent::save($conn);
     if ($send_message) {
         /* The following is for sending an email to the recipient to notify them that they've received a message.
          */
         $recipient = sfGuardUserTable::getInstance()->find($this->getRecipientId());
         if (!$recipient || !$recipient->getReceiveNotificationOfPrivateMessages()) {
             return parent::save($conn);
         }
         $parameters = array('user_id' => $this->getRecipientId(), 'message_id' => $this->getIncremented());
         $prefer_html = $recipient->getPreferHtml();
         $address = $recipient->getEmailAddress();
         $name = $recipient->getPreferredName() ? $recipient->getPreferredName() : $recipient->getFullName();
         $email = EmailTable::getInstance()->getFirstByEmailTypeAndLanguage('NewPrivateMessage', $recipient->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);
     }
 }
 public function save(Doctrine_Connection $conn = null)
 {
     if (is_null($this->_get('username')) && is_null($this->_get('email_address'))) {
         return;
         //throw new sfException('Cannot save User with null username and email!');
     }
     if ($this->isNew() && sfGuardUserTable::getIfValidatedUserHasUsername($this->_get('username'))) {
         throw new sfException('Cannot save user.  This username has already been validated with another user.');
     }
     if (!$this->isNew() && in_array('is_validated', $this->_modified) && !$this->_get('is_validated')) {
         /* The user has been un-validated, probably due to changing their
          * Reddit validation key by username or password.  We need to send
          * them an email about it.
          */
         $parameters = array('user_id' => $this->getIncremented());
         $prefer_html = $this->getPreferHtml();
         $address = $this->getEmailAddress();
         $name = $this->getPreferredName() ? $this->getPreferredName() : $this->getFullName();
         $email = EmailTable::getInstance()->getFirstByEmailTypeAndLanguage('ChangeRedditKey', $this->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);
         $this->addLoginMessage('You have changed information relating to your Reddit user and will need to validate your Reddit username again.  Please see your email for more information.');
     }
     parent::save($conn);
 }
 public function executeSend(sfWebRequest $request)
 {
     $this->forward404Unless($request->isMethod('post'));
     if ($this->getUser()->getApiUserId()) {
         sfConfig::set('app_recaptcha_active', false);
     }
     $this->form = new FeedbackForm();
     if ($this->getUser()->getApiUserId()) {
         unset($this->form['name']);
         unset($this->form['email']);
     }
     $requestData = $request->getParameter($this->form->getName());
     if (sfConfig::get('app_recaptcha_active', false)) {
         $requestData['challenge'] = $this->getRequestParameter('recaptcha_challenge_field');
         $requestData['response'] = $this->getRequestParameter('recaptcha_response_field');
     }
     $this->form->bind($requestData);
     if ($this->form->isValid()) {
         if ($this->getUser()->getApiUserId()) {
             $user_data = Api::getInstance()->get('user/' . $this->getUser()->getApiUserId(), true);
             $user = ApiDoctrine::createQuickObject($user_data['body']);
         } else {
             $user = null;
         }
         $values = $this->form->getValues();
         $name = $this->getUser()->getApiUserId() ? $user->getPreferredName() ? $user->getPreferredName() : $user->getFullName() : $this->form->getValue('name');
         $email = $this->getUser()->getApiUserId() ? $user->getEmailAddress() : $this->form->getValue('email');
         $signinUrl = $this->getUser()->getReferer($request->getReferer());
         $message = $name . ' ' . $email . "\n" . $values['message'] . "\nReferer:" . $signinUrl;
         $to = ProjectConfiguration::getApplicationFeedbackAddress();
         $subjects = sfConfig::get('app_feedback_subjects', array());
         $subject = ProjectConfiguration::getApplicationName() . ': ' . (array_key_exists($values['subject'], $subjects) ? $subjects[$values['subject']] : $values['subject']);
         $from_address = $this->getUser()->getApiUserId() ? "{$name} <{$email}>" : ProjectConfiguration::getApplicationEmailAddress();
         AppMail::sendMail($to, $from_address, $subject, $message);
         $this->getUser()->setFlash('notice', 'Your message has been sent to ' . ProjectConfiguration::getApplicationName() . '.');
         return $this->redirect('' != $signinUrl ? $signinUrl : '@homepage');
     }
     $this->getUser()->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());
     $this->setTemplate('feedback');
 }
 public function sendMail($body_function, $additional_params = array())
 {
     $user = $this->getGuardUser();
     if (!$user) {
         return;
     }
     if (!$user->getReceiveNotificationOfEpisodeApprovalPending() && $body_function == "EpisodeApprovalPending") {
         return;
     }
     if (!$user->getReceiveNotificationOfNewlyOpenedEpisodes() && $body_function == "NewlyOpenedEpisode") {
         return;
     }
     if (!$user->getReceiveNotificationOfPrivateMessages() && $body_function == "NewPrivateMessage") {
         return;
     }
     $prefer_html = $user->getPreferHtml();
     $address = $user->getEmailAddress();
     $name = $user->getPreferredName() ? $user->getPreferredName() : $user->getFullName();
     $user_id = $this->getApiUserId();
     if (!array_key_exists('user_id', $additional_params)) {
         $additional_params['user_id'] = $user_id;
     }
     if (array_key_exists('language', $additional_params)) {
         $email = EmailTable::getInstance()->getFirstByEmailTypeAndLanguage($body_function, $additional_params['language']);
     } else {
         $email = EmailTable::getInstance()->getFirstByEmailTypeAndLanguage($body_function);
     }
     if (!$email) {
         throw new sfException("Cannot find email '{$body_function}' in language '{$language}'.");
     }
     $subject = $email->generateSubject($additional_params);
     $body = $email->generateBodyText($additional_params, $prefer_html);
     $from = sfConfig::get('app_email_address', ProjectConfiguration::getApplicationName() . ' <' . ProjectConfiguration::getApplicationEmailAddress() . '>');
     return AppMail::sendMail($address, $from, $subject, $body, $prefer_html ? $body : null);
 }
 public function sendEmailAboutPassedDeadline($user_id, $episode_id)
 {
     // Send an email to that user telling them their EpisodeAssignment is now valid
     $parameters = array('user_id' => $user_id, 'episode_id' => $episode_id);
     $user = sfGuardUserTable::getInstance()->find($user_id);
     $prefer_html = $user->getPreferHtml();
     $address = $user->getEmailAddress();
     $name = $user->getPreferredName() ? $user->getPreferredName() : $user->getFullName();
     $email = EmailTable::getInstance()->getFirstByEmailTypeAndLanguage('PassedDeadlineOnEpisode', $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('Your episode passed its release deadline and has been re-assigned.');
 }
 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);
 }
 protected function produceAtom($feedArray)
 {
     // Atom 1.0
     $doc = new DomDocument('1.0', 'utf-8');
     $feed = $doc->createElement('feed');
     $feed->setAttribute('xmlns:itunes', 'http://www.itunes.com/dtds/podcast-1.0.dtd');
     $feed->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
     $feed->setAttribute('xml:lang', $feedArray['language']);
     $doc->appendChild($feed);
     $title = $doc->createElement('title', $feedArray['title']);
     $title->setAttribute('type', 'text');
     $subtitle = $doc->createElement('subtitle', $feedArray['description']);
     $subtitle->setAttribute('type', 'text');
     $updated = $doc->createElement('updated', date('Y-m-d\\TH:i:sP'));
     $generator = $doc->createElement('generator', ProjectConfiguration::getApplicationName() . ' Feed Module');
     $link_alternate = $doc->createElement('link');
     $link_alternate->setAttribute('rel', 'alternate');
     $link_alternate->setAttribute('type', 'text/html');
     $link_alternate->setAttribute('href', $feedArray['link']);
     $link_self = $doc->createElement('link');
     $link_self->setAttribute('rel', 'self');
     $link_self->setAttribute('type', 'application/atom+xml');
     $link_self->setAttribute('href', $feedArray['atom_link']);
     $id = $doc->createElement('id', $feedArray['link']);
     $author = $doc->createElement('author');
     $a_name = $doc->createElement('name', ProjectConfiguration::getApplicationName());
     $a_email = $doc->createElement('email', ProjectConfiguration::getApplicationEmailAddress());
     $a_uri = $doc->createElement('uri', $this->getController()->genUrl('@homepage', true));
     $author->appendChild($a_name);
     $author->appendChild($a_email);
     $author->appendChild($a_uri);
     $feed->appendChild($title);
     $feed->appendChild($subtitle);
     $feed->appendChild($updated);
     $feed->appendChild($generator);
     $feed->appendChild($link_alternate);
     $feed->appendChild($link_self);
     $feed->appendChild($id);
     $feed->appendChild($author);
     $itunes_explicit = $doc->createElement('itunes:explicit', $feedArray["is_nsfw"]);
     $itunes_summary = $doc->createElement('itunes:summary', substr($feedArray['description'], 0, 3999));
     $itunes_category = $doc->createElement('itunes:category');
     $itunes_category->setAttribute('text', 'Technology');
     $itunes_subcategory = $doc->createElement('itunes:category');
     $itunes_subcategory->setAttribute('text', 'Podcasting');
     $itunes_category->appendChild($itunes_subcategory);
     // Including the itunes:owner messes with the feed by overriding the title in iTunes.
     $feed->appendChild($itunes_explicit);
     $feed->appendChild($itunes_summary);
     $feed->appendChild($itunes_category);
     foreach ($feedArray['entries'] as $entry) {
         $fentry = $doc->createElement('entry');
         //$fentry->setAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
         $e_title = $doc->createElement('title');
         $e_title->setAttribute('type', 'html');
         $cdata_title = $doc->createCDATASection($entry['title']);
         $e_title->appendChild($cdata_title);
         $thumbnail_tag = $entry['thumbnail'] ? '<p><img src="' . $entry['thumbnail'] . '"/></p>' : '';
         $e_summary = $doc->createElement('summary');
         $e_summary->setAttribute('type', 'html');
         $cdata_summary = $doc->createCDATASection($thumbnail_tag . substr($entry['description'], 0, 500));
         $e_summary->appendChild($cdata_summary);
         $e_published = $doc->createElement('published', date('Y-m-d\\TH:i:sP', $entry['released']));
         $e_updated = $doc->createElement('updated', date('Y-m-d\\TH:i:sP', $entry['modified'] > $entry['released'] ? $entry['modified'] : $entry['released']));
         $e_link = $doc->createElement('link');
         $e_link->setAttribute('rel', 'alternate');
         $e_link->setAttribute('type', 'text/html');
         $e_link->setAttribute('href', $entry['link']);
         $e_id = $doc->createElement('id', $entry['link']);
         $e_author = $doc->createElement('author');
         $ea_name = $doc->createElement('name', $entry['author']['name']);
         $e_author->appendChild($ea_name);
         $e_enclosure = $doc->createElement('link');
         $e_enclosure->setAttribute('rel', 'enclosure');
         $audio_info = $this->getRemoteInfo($entry['audio_location']);
         $e_enclosure->setAttribute('type', $audio_info['type']);
         $e_enclosure->setAttribute('href', $entry['audio_location']);
         $e_enclosure->setAttribute('length', $audio_info['length']);
         $e_enclosure->setAttribute('title', 'Audio');
         $e_content = $doc->createElement('content');
         //$e_content->setAttribute('xmlns:xhtml',
         //                         'http://www.w3.org/1999/xhtml');
         $e_content->setAttribute('type', 'xhtml');
         $fragment = $doc->createDocumentFragment();
         $fragment->appendXML($thumbnail_tag . $entry['content']);
         $e_div = $doc->createElement('div');
         $e_div->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
         $e_div->appendChild($fragment);
         $e_content->appendChild($e_div);
         if ($entry['reddit_post_url']) {
             $e_comments = $doc->createElement('link');
             $e_comments->setAttribute('rel', 'replies');
             $e_comments->setAttribute('type', 'text/html');
             $e_comments->setAttribute('href', $entry['reddit_post_url']);
         }
         $fentry->appendChild($e_title);
         $fentry->appendChild($e_summary);
         $fentry->appendChild($e_published);
         $fentry->appendChild($e_updated);
         $fentry->appendChild($e_link);
         $fentry->appendChild($e_id);
         $fentry->appendChild($e_author);
         $fentry->appendChild($e_enclosure);
         $fentry->appendChild($e_content);
         $i_itunes_author = $doc->createElement('itunes:author', $entry['author']['name']);
         $i_itunes_summary = $doc->createElement('itunes:summary', substr(strip_tags($entry['description']), 0, 3999));
         $i_itunes_explicit = $doc->createElement('itunes:explicit', $entry['is_nsfw']);
         $fentry->appendChild($i_itunes_author);
         $fentry->appendChild($i_itunes_summary);
         $fentry->appendChild($i_itunes_explicit);
         $feed->appendChild($fentry);
     }
     $doc->formatOutput = true;
     $doc->preserveWhitespace = false;
     return $doc->saveXML();
 }