Beispiel #1
0
 public function execute()
 {
     // get a logger
     $logger = Zend_Registry::get("logger");
     echo "Memory usage on startup: " . memory_get_usage() . "\r\n";
     // Access the email
     $mail = new Zend_Mail_Storage_Pop3(array('host' => 'xxxxxx', 'user' => 'xxxxxx', 'password' => 'xxxxxx', 'ssl' => 'SSL'));
     // If no new email, goodbye
     if ($mail->countMessages() == 0) {
         $log = "No new emails to process.";
         echo "{$log}\r\n";
         $logger->log($log, Zend_Log::INFO);
         exit;
     }
     $email_count = 0;
     foreach ($mail as $messageNum => $message) {
         // A bit of feedback
         $log = "Message to {$message->to}.. (mem: " . memory_get_usage() . ")...";
         echo "{$log}";
         $logger->log($log, Zend_Log::INFO);
         // Get the user, if not we continue
         if (!($user = $this->getUser($message->to))) {
             $log = "skipped.";
             echo "{$log}\r\n";
             $logger->log($log, Zend_Log::INFO);
             $mail->removeMessage($messageNum);
             continue;
         }
         // Assign the shard value
         Zend_Registry::set("shard", $user['id']);
         $this->_user = $user;
         $this->_properties = new Properties(array(Stuffpress_Db_Properties::KEY => $user['id']));
         // Get the subject
         try {
             $subject = $this->mimeWordDecode(trim($message->subject));
         } catch (Exception $e) {
             $subject = "";
         }
         // Get the content
         $foundPart = null;
         $plain_text = "";
         $html_text = "";
         $latitude = false;
         $longitude = false;
         $image = false;
         $audio = false;
         $files = new Files();
         $timestamp = time();
         foreach (new RecursiveIteratorIterator($message) as $part) {
             try {
                 $part_type = strtok($part->contentType, ';');
                 if ($part_type == 'text/plain') {
                     $charset = $this->getCharset($part->contentType);
                     $plain_text = $this->recode(trim($part->getContent()), $charset);
                 } else {
                     if ($part_type == 'text/html') {
                         $charset = $this->getCharset($part->contentType);
                         $html_text = $this->recode(trim($part->getContent()), $charset);
                     } else {
                         if (substr_compare($part_type, 'image', 0, 5) == 0) {
                             if ($details = $this->getFileDetails($part->contentType)) {
                                 if ($content = base64_decode($part->getContent())) {
                                     $file_id = $files->saveFile($content, $details['name'], $details['mime'], "Email upload");
                                     $file = $files->getFile($file_id);
                                     $key = $file->key;
                                     $files->fitWidth($file_id, 240, 'small');
                                     $files->fitWidth($file_id, 500, 'medium');
                                     $files->fitWidth($file_id, 1024, 'large');
                                     $files->fitSquare($file_id, 75, 'thumbnails');
                                     $exif = $files->readExif($file_id);
                                     // Retrieve the picture date/time
                                     if (isset($exif['DateTimeOriginal'])) {
                                         $timestamp = Stuffpress_Date::strToTimezone($exif['DateTimeOriginal'], $this->_properties->getProperty('timezone'));
                                     }
                                     // Get longitude if provided
                                     if (!empty($exif['GPSLongitude']) && count($exif['GPSLongitude']) == 3 && !empty($exif['GPSLongitudeRef'])) {
                                         $longitude = ($exif['GPSLongitudeRef'] == 'W' ? '-' : '') . Stuffpress_Exif::exif_gpsconvert($exif['GPSLongitude']);
                                     }
                                     // Get latitude
                                     if (!empty($exif['GPSLatitude']) && count($exif['GPSLatitude']) == 3 && !empty($exif['GPSLatitudeRef'])) {
                                         $latitude = ($exif['GPSLatitudeRef'] == 'S' ? '-' : '') . Stuffpress_Exif::exif_gpsconvert($exif['GPSLatitude']);
                                     }
                                     $image = $key;
                                 }
                             }
                         } else {
                             if (substr_compare($part_type, 'audio', 0, 5) == 0) {
                                 if ($details = $this->getFileDetails($part->contentType)) {
                                     if ($content = base64_decode($part->getContent())) {
                                         $file_id = $files->saveFile($content, $details['name'], $details['mime'], "Email upload");
                                         $file = $files->getFile($file_id);
                                         $audio = $file->key;
                                     }
                                 }
                             }
                         }
                     }
                 }
             } catch (Zend_Mail_Exception $e) {
                 // ignore
             }
         }
         $body = strlen($html_text) > 0 ? $html_text : $plain_text;
         // Post the content
         // 1 - a status message
         if (!$image && !$audio && strlen($subject) == 0) {
             $item_id = $this->postStatus($user['id'], $timestamp, $body);
         } else {
             if (!$image && !$audio) {
                 $item_id = $this->postBlog($user['id'], $timestamp, $subject, $body);
             } else {
                 if ($image) {
                     $item_id = $this->postImage($user['id'], $timestamp, $subject, $body, $image);
                 } else {
                     if ($audio) {
                         $item_id = $this->postAudio($user['id'], $timestamp, $subject, $body, $audio);
                     } else {
                         echo "Unsupported fotmat\r\n";
                     }
                 }
             }
         }
         // Set the location of the item if provided
         if ($latitude && $longitude) {
             $source = StuffpressModel::forUser($this->_user->id);
             $source_id = $source->getID();
             $data = new Data();
             $data->setLocation($source_id, $item_id, $latitude, $longitude, 0);
         }
         $email_count++;
         // Delete the email
         $mail->removeMessage($messageNum);
         echo "processed.\r\n";
         $logger->log("Message delivered to {$user['username']}.", Zend_Log::INFO);
         // Clean up before the loop
         unset($content);
     }
     $logger->log("Processed {$email_count} emails. (max mem: " . memory_get_peak_usage() . ").", Zend_Log::INFO);
 }
Beispiel #2
0
 public function testRemove()
 {
     $mail = new Zend_Mail_Storage_Pop3($this->_params);
     $count = $mail->countMessages();
     $mail->removeMessage(1);
     $this->assertEquals($mail->countMessages(), --$count);
     unset($mail[2]);
     $this->assertEquals($mail->countMessages(), --$count);
 }