Esempio n. 1
0
 public function receive_email_crond()
 {
     $now = time();
     $lock_time = AWS_APP::cache()->get('receive_email_locker');
     if ($lock_time and $now - $lock_time <= 600) {
         return false;
     }
     $receiving_email_accounts = $this->get_receiving_email_config();
     if (!$receiving_email_accounts) {
         return false;
     }
     AWS_APP::cache()->set('receive_email_locker', $now, 600);
     foreach ($receiving_email_accounts as $receiving_email_config) {
         if (!$receiving_email_config['server'] or !$receiving_email_config['protocol'] or !$receiving_email_config['username'] or !$receiving_email_config['password'] or !$receiving_email_config['uid']) {
             continue;
         }
         $mail_config = array('host' => $receiving_email_config['server'], 'user' => $receiving_email_config['username'], 'password' => $receiving_email_config['password']);
         if ($receiving_email_config['ssl'] == 1) {
             $mail_config['ssl'] = 'SSL';
         }
         if ($receiving_email_config['port']) {
             $mail_config['port'] = $receiving_email_config['port'];
         }
         try {
             switch ($receiving_email_config['protocol']) {
                 case 'pop3':
                     $mail = new Zend_Mail_Storage_Pop3($mail_config);
                     break;
                 case 'imap':
                     $mail = new Zend_Mail_Storage_Imap($mail_config);
                     break;
                 default:
                     continue 2;
             }
         } catch (Exception $e) {
             $this->notification_of_receive_email_error($receiving_email_config['id'], $e->getMessage());
             continue;
         }
         $this->notification_of_receive_email_error($receiving_email_config['id'], null);
         $received_email['config_id'] = $receiving_email_config['id'];
         $received_email['uid'] = $receiving_email_config['uid'];
         foreach ($mail as $num => $message) {
             try {
                 if ($receiving_email_config['protocol'] == 'imap' and $message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)) {
                     continue;
                 }
                 $received_email['message_id'] = substr($message->messageID, 1, -1);
                 $received_email['date'] = intval(strtotime($message->Date));
                 if ($now - $received_email['date'] > 604800 or $this->fetch_row('received_email', 'message_id = "' . $this->quote($received_email['message_id']) . '" AND date = ' . $received_email['date'])) {
                     continue;
                 }
                 if ($message->isMultipart()) {
                     for ($i = 1; $i <= $message->countParts(); $i++) {
                         $part = $message->getPart($i);
                         if (substr($part->contentType, 0, 5) == 'text/') {
                             $encoding = $part->contentTransferEncoding;
                             $type = $part->contentType;
                             $received_email['content'] = $part->getContent();
                             break;
                         } else {
                             continue;
                         }
                     }
                 } else {
                     $encoding = $message->contentTransferEncoding;
                     $type = $message->contentType;
                     $received_email['content'] = $message->getContent();
                 }
                 if (!$encoding or !$type) {
                     continue;
                 }
                 preg_match('/charset\\s?=\\s?"?([a-zA-Z0-9-]+)"?$/i', $type, $matches);
                 $charset = strtolower($matches[1]);
                 $received_email['subject'] = decode_eml($message->Subject);
                 preg_match('/<?([^<]+@.+(\\.[^>]+)+)>?$/i', $message->From, $matches);
                 $received_email['from'] = strtolower($matches[1]);
                 switch ($encoding) {
                     case 'base64':
                         $received_email['content'] = base64_decode($received_email['content']);
                         break;
                     case 'quoted-printable':
                         $received_email['content'] = quoted_printable_decode($received_email['content']);
                         break;
                 }
                 if ($charset and $charset != 'utf-8') {
                     $received_email['subject'] = mb_convert_encoding($received_email['subject'], 'utf-8', $charset);
                     $received_email['content'] = mb_convert_encoding($received_email['content'], 'utf-8', $charset);
                 }
                 $received_email['subject'] = strip_tags($received_email['subject']);
                 $received_email['content'] = strip_tags(preg_replace(array('/<p(\\s+[^>]*)?>/i', '/<\\/p>/i', '/<br\\s*\\/?>/i'), "\n", $received_email['content']));
                 $now++;
                 $received_email['access_key'] = md5($received_email['uid'] . $now);
                 $this->insert('received_email', $received_email);
                 if ($receiving_email_config['protocol'] == 'pop3') {
                     $mail->removeMessage($num);
                 }
                 if ($receiving_email_config['protocol'] == 'imap') {
                     $mail->setFlags($num, array(Zend_Mail_Storage::FLAG_SEEN));
                 }
             } catch (Exception $e) {
                 continue;
             }
         }
     }
     AWS_APP::cache()->delete('receive_email_locker');
     return true;
 }
Esempio n. 2
0
 public function testRemove()
 {
     $mail = new Zend_Mail_Storage_Imap($this->_params);
     $count = $mail->countMessages();
     $mail->removeMessage(1);
     $this->assertEquals($mail->countMessages(), $count - 1);
 }
Esempio n. 3
0
 /**
  * Perform periodic tasks
  *
  * @return boolean true because otherwise it disables subsequent plugins
  */
 public function hookPeriodicTask(&$pa_params)
 {
     global $AUTH_CURRENT_USER_ID;
     $t_log = new Eventlog();
     $o_db = new Db();
     //$t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Could not authenticate to remote system %1', $vs_base_url), 'SOURCE' => 'traveloguePlugin->hookPeriodicTask'));
     // Get new email
     $pn_locale_id = 1;
     // US
     $vs_server = $this->opo_config->get('imap_server');
     $vs_username = $this->opo_config->get('username');
     $vs_password = $this->opo_config->get('password');
     $vs_ssl = $this->opo_config->get('ssl');
     if (!$vs_server) {
         return true;
     }
     if (!$vs_username) {
         return true;
     }
     try {
         $o_mail = new Zend_Mail_Storage_Imap(array('host' => $vs_server, 'user' => $vs_username, 'password' => $vs_password, 'ssl' => $vs_ssl));
     } catch (Exception $e) {
         return true;
     }
     $va_mimetypes = $this->opo_config->getList('mimetypes');
     $va_mail_to_delete = array();
     foreach ($o_mail as $vn_message_num => $o_message) {
         $va_mail_to_delete[$vn_message_num] = true;
         // Extract title from subject line of email
         $vs_subject = $o_message->subject;
         $vs_from = $o_message->headerExists('from') ? $o_message->from : "";
         print "PROCESSING {$vs_subject} FROM {$vs_from}\n";
         // Extract media from email attachments
         // Extract caption from email body
         $va_images = array();
         $va_texts = array();
         foreach (new RecursiveIteratorIterator($o_message) as $o_part) {
             try {
                 if (in_array(strtok($o_part->contentType, ';'), $va_mimetypes)) {
                     $va_images[] = $o_part;
                 } else {
                     if (in_array(strtok($o_part->contentType, ';'), array("text/plain", "text/html"))) {
                         $va_texts[] = (string) $o_part;
                     }
                 }
             } catch (Zend_Mail_Exception $e) {
                 // ignore
             }
         }
         if (!sizeof($va_images)) {
             continue;
         }
         // Get user by email address
         if (preg_match('!<([^>]+)>!', $vs_from, $va_matches)) {
             // extract raw address from "from" header
             $vs_from = $va_matches[1];
         }
         $t_user = new ca_users();
         if ($t_user->load(array('email' => $vs_from))) {
             $AUTH_CURRENT_USER_ID = $vn_user_id = $t_user->getPrimaryKey();
             // force libs to consider matched user as logged in; change log will reflect this name
         } else {
             $vn_user_id = null;
         }
         // Create object
         $t_object = new ca_objects();
         $t_object->setMode(ACCESS_WRITE);
         $t_object->set('type_id', $this->opo_config->get('object_type'));
         // TODO: set idno to autogenerated # and/or allow for configurable policy
         $t_object->set('idno', '');
         $t_object->set('access', $this->opo_config->get('default_access'));
         $t_object->set('status', $this->opo_config->get('default_status'));
         // TODO: make this a configurable mapping ala how media metadata is done
         $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'generalNotes' => join("\n\n", $va_texts)), 'generalNotes');
         $t_object->insert();
         DataMigrationUtils::postError($t_object, "While adding object", "traveloguePlugin");
         // TODO: log this
         $t_object->addLabel(array('name' => $vs_subject), $pn_locale_id, null, true);
         DataMigrationUtils::postError($t_object, "While adding label", "traveloguePlugin");
         // TODO: log this
         // Add representation
         $vs_tmp_file_path = tempnam(caGetTempDirPath(), 'travelogue_');
         foreach ($va_images as $vn_i => $vs_file_content) {
             if (file_put_contents($vs_tmp_file_path, base64_decode((string) $vs_file_content))) {
                 $t_object->addRepresentation($vs_tmp_file_path, $this->opo_config->get('representation_type'), 1, $this->opo_config->get('default_status'), $this->opo_config->get('default_access'), true);
                 DataMigrationUtils::postError($t_object, "While adding media", "traveloguePlugin");
                 // TODO: log this
             }
         }
         // TODO: add option to link user-as-entity to image (probably as creator)
     }
     foreach (array_reverse(array_keys($va_mail_to_delete)) as $vn_message_num) {
         $o_mail->removeMessage($vn_message_num);
     }
     return true;
 }