Exemplo n.º 1
0
 /**
  * Generate and store a Plancake Email address
  *
  */
 public function generateAndStorePlancakeEmailAddress()
 {
     do {
         $truncatedUserEmail = preg_replace('/@.*$/', '', $this->getEmail());
         $randomPart = mt_rand(10, 99) . chr(mt_rand(97, 122)) . mt_rand(100, 999);
         $plancakeEmailAddressWithoutDomain = 'inbox_' . $truncatedUserEmail . '_' . $randomPart;
         // I double check the email address doesn't exist already
         $c = new Criteria();
         $c->add(PcPlancakeEmailAddressPeer::EMAIL, $plancakeEmailAddressWithoutDomain, Criteria::EQUAL);
         $entry = PcPlancakeEmailAddressPeer::doSelectOne($c);
     } while (is_object($entry));
     $plancakeEmailDbEntry = new PcPlancakeEmailAddress();
     $plancakeEmailDbEntry->setUserId($this->getId())->setEmail($plancakeEmailAddressWithoutDomain)->save();
 }
 /**
  * Populates the object using an array.
  *
  * This is particularly useful when populating an object from one of the
  * request arrays (e.g. $_POST).  This method goes through the column
  * names, checking to see whether a matching key exists in populated
  * array. If so the setByName() method is called for that column.
  *
  * You can specify the key type of the array by additionally passing one
  * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
  * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
  * The default key type is the column's phpname (e.g. 'AuthorId')
  *
  * @param      array  $arr     An array to populate the object from.
  * @param      string $keyType The type of keys the array uses.
  * @return     void
  */
 public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
 {
     $keys = PcPlancakeEmailAddressPeer::getFieldNames($keyType);
     if (array_key_exists($keys[0], $arr)) {
         $this->setUserId($arr[$keys[0]]);
     }
     if (array_key_exists($keys[1], $arr)) {
         $this->setEmail($arr[$keys[1]]);
     }
 }
 /**
  * Retrieve multiple objects by pkey.
  *
  * @param      array $pks List of primary keys
  * @param      PropelPDO $con the connection to use
  * @throws     PropelException Any exceptions caught during processing will be
  *		 rethrown wrapped into a PropelException.
  */
 public static function retrieveByPKs($pks, PropelPDO $con = null)
 {
     if ($con === null) {
         $con = Propel::getConnection(PcPlancakeEmailAddressPeer::DATABASE_NAME, Propel::CONNECTION_READ);
     }
     $objs = null;
     if (empty($pks)) {
         $objs = array();
     } else {
         $criteria = new Criteria(PcPlancakeEmailAddressPeer::DATABASE_NAME);
         $criteria->add(PcPlancakeEmailAddressPeer::USER_ID, $pks, Criteria::IN);
         $objs = PcPlancakeEmailAddressPeer::doSelect($criteria, $con);
     }
     return $objs;
 }
 /**
  * @see sfTask
  */
 protected function executeTask($env, $arguments = array(), $options = array())
 {
     /**
      * Getting the directory where the emails are stored
      */
     $inboxUser = sfConfig::get('app_emailToInbox_inboxUser');
     $emailDomain = sfConfig::get('app_emailToInbox_mailServerDomain');
     $newEmailPath = $arguments['emailFileAbsolutePath'];
     // there are some regular Plancake inbox email address that are
     // use just for spam
     $spamAccounts = array();
     $spamAccounts[] = 'niki_5436';
     // this will be interpreted as inbox_niki_5436@plancakebox.com
     $spamAccounts[] = 'niki.jones_15c522';
     $this->log('');
     $this->log('');
     $this->log("parsing the email at " . $newEmailPath);
     $mailParser = new PlancakeEmailParser(file_get_contents($newEmailPath));
     $plancakeSubjectOK = false;
     $plancakeRecipientOK = false;
     $emailTo = array();
     $emailSubject = '';
     $emailCc = $mailParser->getCc();
     try {
         $emailTo = $mailParser->getTo();
     } catch (Exception $e) {
         $this->handleFault("couldn't retrieve the 'to' header of the email", $newEmailPath);
         return;
     }
     try {
         $emailSubject = $mailParser->getSubject();
         $plancakeSubjectOK = true;
         $this->log("got the subject of the email: " . $emailSubject);
     } catch (Exception $e) {
         $this->handleFault("couldn't retrieve the subject of the email", $newEmailPath);
         return;
     }
     $emailRecipients = array_merge($emailTo, $emailCc);
     $emailRecipients = implode(', ', $emailRecipients);
     $deliveredToHeader = $mailParser->getHeader('Delivered-To');
     $emailRecipients = $deliveredToHeader . ', ' . $emailRecipients;
     $this->log("all recipients of the email: " . $emailRecipients);
     $internalEmail = false;
     // to flag an email sent to the catchall address
     $spamEmail = false;
     if (preg_match('/' . $inboxUser . "@{$emailDomain}/", $emailRecipients, $matches)) {
         $internalEmail = true;
         $this->log("discarding the email as it is an internal one");
         if (is_file($newEmailPath)) {
             unlink($newEmailPath);
         }
         return;
     }
     if (preg_match("/inbox_([^@]+)@{$emailDomain}/i", $emailRecipients, $matches)) {
         // found Plancake Inbox address!
         $plancakeInbox = $matches[1];
         if (in_array($plancakeInbox, $spamAccounts)) {
             $spamEmail = true;
             $this->handleFault("discarding the email because it is from a spammer", $newEmailPath);
             return;
         } else {
             $emailRecipient = 'inbox_' . $plancakeInbox . "@{$emailDomain}";
             $plancakeRecipientOK = true;
             $this->log("got the Plancake recipient of the email: " . $emailRecipient);
         }
     } else {
         $this->handleFault("couldn't find a Plancake recipient for the email", $newEmailPath);
         return;
     }
     /**
      * Sorting the email into the database
      */
     if ($plancakeRecipientOK && $plancakeSubjectOK) {
         $this->log('well done. For this email we got both the recipient and the subject. I can now create the task for the user.');
         $emailRecipientWithoutDomain = str_replace("@{$emailDomain}", '', $emailRecipient);
         $c = new Criteria();
         $c->add(PcPlancakeEmailAddressPeer::EMAIL, $emailRecipientWithoutDomain, Criteria::EQUAL);
         $plancakeEmail = PcPlancakeEmailAddressPeer::doSelectOne($c);
         if (is_object($plancakeEmail)) {
             // everything's OK
             $userId = $plancakeEmail->getUserId();
             $user = PcUserPeer::retrieveByPk($userId);
             PcUserPeer::setLoggedInUser($user);
             // check whether there is a note for the task
             $note = $this->extractNote($mailParser->getPlainBody());
             if (strlen($note)) {
                 $this->log("note: {$note}");
             }
             if (!strlen($emailSubject)) {
                 $emailSubject = 'Something went wrong with a task you sent via email. Please contact us.';
             }
             PcTaskPeer::createOrEdit($emailSubject, $user->getInbox()->getId(), 0, '', false, $note);
             $this->log('the email has successfully become a task for the user.');
         } else {
             // something wrong
             $this->handleFault('no email user', $newEmailPath);
             $this->log('couldn\'t create a task from the email - the Plancake address is not in the system :-(.');
         }
     } else {
         if ((!$plancakeRecipientOK || !$plancakeSubjectOK) && !$internalEmail && !$spamEmail) {
             // something wrong
             $this->handleFault('email parsing', $newEmailPath);
             $this->log("counldn't find both the recipient and the subject of the email. Nothing to do.");
         }
     }
     $this->log("deleting the email from the hard disk.");
     if (is_file($newEmailPath)) {
         unlink($newEmailPath);
     }
     $this->log('');
     $this->log('');
 }
Exemplo n.º 5
0
 /**
  * Gets a single PcPlancakeEmailAddress object, which is related to this object by a one-to-one relationship.
  *
  * @param      PropelPDO $con
  * @return     PcPlancakeEmailAddress
  * @throws     PropelException
  */
 public function getPcPlancakeEmailAddress(PropelPDO $con = null)
 {
     if ($this->singlePcPlancakeEmailAddress === null && !$this->isNew()) {
         $this->singlePcPlancakeEmailAddress = PcPlancakeEmailAddressPeer::retrieveByPK($this->id, $con);
     }
     return $this->singlePcPlancakeEmailAddress;
 }