示例#1
0
 /**
  * Sends notifications *now*
  * @param mixed $to string or array...the type of address (email, task ID, user ID) is specified below
  * @param integer $to_type type of $to address
  * @param integer $type type of notification
  * @param array $data additional info needed for notification
  * @access public
  * @return bool
  */
 function send_now($to, $to_type, $type, $data = array())
 {
     global $db, $fs, $proj;
     $emails = array();
     $jids = array();
     $result = true;
     if (defined('FS_NO_MAIL')) {
         return true;
     }
     switch ($to_type) {
         case ADDRESS_DONE:
             // from send_stored()
             list($emails, $jids) = $to;
             $data = unserialize($data['message_data']);
             $subject = $data['subject'];
             $body = $data['body'];
             break;
         case ADDRESS_EMAIL:
             // this happens on email confirmation, when no user exists
             $emails = is_array($to) ? $to : array($to);
             break;
         case ADDRESS_USER:
             // list of user IDs
             list($emails, $jids) = Notifications::user_to_address($to, $type);
             break;
         case ADDRESS_TASK:
             // now we need everyone on the notification list and the assignees
             list($emails, $jids) = Notifications::task_notifications($to, $type, ADDRESS_EMAIL);
             $data['task_id'] = $to;
             break;
     }
     if (isset($data['task_id'])) {
         $data['task'] = Flyspray::getTaskDetails($data['task_id']);
         // we have project specific options
         $pid = $db->x->GetOne('SELECT project_id FROM {tasks} WHERE task_id = ?', null, $data['task_id']);
         $data['project'] = new Project($pid);
     }
     if ($to_type != ADDRESS_DONE) {
         list($subject, $body) = Notifications::generate_message($type, $data);
     }
     if (isset($data['task_id'])) {
         // Now, we add the project contact addresses,
         // but only if the task is public
         $data['task'] = Flyspray::getTaskDetails($data['task_id']);
         if ($data['task']['mark_private'] != '1' && in_array($type, explode(' ', $data['project']->prefs['notify_types']))) {
             $proj_emails = preg_split('/[\\s,;]+/', $proj->prefs['notify_email'], -1, PREG_SPLIT_NO_EMPTY);
             $proj_jids = preg_split('/[\\s,;]+/', $proj->prefs['notify_jabber'], -1, PREG_SPLIT_NO_EMPTY);
             $emails = array_merge($proj_emails, $emails);
             if ($fs->prefs['global_email']) {
                 $emails[] = $fs->prefs['global_email'];
             }
             if ($fs->prefs['global_jabber']) {
                 $jids[] = $fs->prefs['global_jabber'];
             }
             $jids = array_merge($proj_jids, $emails);
         }
     }
     // Now we start sending
     if (count($emails)) {
         Swift_ClassLoader::load('Swift_Connection_Multi');
         Swift_ClassLoader::load('Swift_Connection_SMTP');
         $pool = new Swift_Connection_Multi();
         // first choose method
         if ($fs->prefs['smtp_server']) {
             $split = explode(':', $fs->prefs['smtp_server']);
             $port = null;
             if (count($split) == 2) {
                 $fs->prefs['smtp_server'] = $split[0];
                 $port = $split[1];
             }
             // connection... SSL, TLS or none
             if ($fs->prefs['email_ssl']) {
                 $smtp = new Swift_Connection_SMTP($fs->prefs['smtp_server'], $port ? $port : SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_SSL);
             } else {
                 if ($fs->prefs['email_tls']) {
                     $smtp = new Swift_Connection_SMTP($fs->prefs['smtp_server'], $port ? $port : SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_TLS);
                 } else {
                     $smtp = new Swift_Connection_SMTP($fs->prefs['smtp_server'], $port);
                 }
             }
             if ($fs->prefs['smtp_user']) {
                 $smtp->setUsername($fs->prefs['smtp_user']);
                 $smtp->setPassword($fs->prefs['smtp_pass']);
             }
             if (defined('FS_SMTP_TIMEOUT')) {
                 $smtp->setTimeout(FS_SMTP_TIMEOUT);
             }
             $pool->addConnection($smtp);
         } else {
             Swift_ClassLoader::load('Swift_Connection_NativeMail');
             // a connection to localhost smtp server as fallback, discarded if there is no such thing available.
             $pool->addConnection(new Swift_Connection_SMTP());
             $pool->addConnection(new Swift_Connection_NativeMail());
         }
         $swift = new Swift($pool);
         if (isset($data['task_id'])) {
             $swift->attachPlugin(new NotificationsThread($data['task_id'], $emails, $db), 'MessageThread');
         }
         if (defined('FS_MAIL_DEBUG')) {
             $swift->log->enable();
             Swift_ClassLoader::load('Swift_Plugin_VerboseSending');
             $view = new Swift_Plugin_VerboseSending_DefaultView();
             $swift->attachPlugin(new Swift_Plugin_VerboseSending($view), "verbose");
         }
         $message = new Swift_Message($subject, $body);
         // check for reply-to
         if (isset($data['project']) && $data['project']->prefs['notify_reply']) {
             $message->setReplyTo($data['project']->prefs['notify_reply']);
         }
         if (isset($data['project']) && isset($data['project']->prefs['bounce_address'])) {
             $message->setReturnPath($data['project']->prefs['bounce_address']);
         }
         $message->headers->setCharset('utf-8');
         $message->headers->set('Precedence', 'list');
         $message->headers->set('X-Mailer', 'Flyspray');
         // Add custom headers, possibly
         if (isset($data['headers'])) {
             $headers = array_map('trim', explode("\n", $data['headers']));
             if ($headers = array_filter($headers)) {
                 foreach ($headers as $header) {
                     list($name, $value) = explode(':', $header);
                     $message->headers->set(sprintf('X-Flyspray-%s', $name), $value);
                 }
             }
         }
         $recipients = new Swift_RecipientList();
         $recipients->addTo($emails);
         // && $result purpose: if this has been set to false before, it should never become true again
         // to indicate an error
         $result = $swift->batchSend($message, $recipients, $fs->prefs['admin_email']) === count($emails) && $result;
         if (isset($data['task_id'])) {
             $plugin =& $swift->getPlugin('MessageThread');
             if (count($plugin->thread_info)) {
                 $stmt = $db->x->autoPrepare('{notification_threads}', array('task_id', 'recipient_id', 'message_id'));
                 $db->x->executeMultiple($stmt, $plugin->thread_info);
                 $stmt->free();
             }
         }
         $swift->disconnect();
     }
     if (count($jids)) {
         $jids = array_unique($jids);
         if (!$fs->prefs['jabber_username'] || !$fs->prefs['jabber_password']) {
             return $result;
         }
         // nothing that can't be guessed correctly ^^
         if (!$fs->prefs['jabber_port']) {
             $fs->prefs['jabber_port'] = 5222;
         }
         require_once 'class.jabber2.php';
         $jabber = new Jabber($fs->prefs['jabber_username'], $fs->prefs['jabber_password'], $fs->prefs['jabber_security'], $fs->prefs['jabber_port'], $fs->prefs['jabber_server']);
         $jabber->SetResource('flyspray');
         $jabber->login();
         foreach ($jids as $jid) {
             $result = $jabber->send_message($jid, $body, $subject, 'normal') && $result;
         }
     }
     return $result;
 }
 public function testIsAliveReturnsFalseIfTheConnectionIsClosed()
 {
     $mock1 = new FullMockConnection();
     $mock1->setReturnValue("isAlive", false);
     $mock1->expectOnce("start");
     $mock1->expectNever("stop");
     $mock2 = new FullMockConnection();
     $mock2->setReturnValue("isAlive", true);
     $mock2->expectOnce("start");
     $mock2->expectOnce("stop");
     $multi = new Swift_Connection_Multi();
     $multi->addConnection($mock1, "mock1");
     $multi->addConnection($mock2, "mock2");
     $multi->start();
     $this->assertTrue($multi->isAlive());
     $multi->stop();
     $this->assertFalse($multi->isAlive());
 }
示例#3
0
 /**
  * Builds a Swift_Connection depending on the type (native, smtp, sendmail, multi, rotator)
  * Params depend on the connection type
  * 
  * - native:
  *    additional_params
  * 
  * - smtp:
  *     server (*)
  *     port
  *     encryption (SSL, TLS, or OFF)
  *     authentication:
  *       username (*)
  *       password
  *     timeout
  *     requires_ehlo
  *   
  * - sendmail:
  *     command
  *     flags
  *     timeout
  *     requires_ehlo
  *   
  * - multi:
  *     connections:
  *       connection_name1:
  *         type
  *         params
  *       connection_name2:
  *         type
  *         params
  *       etc...
  *     requires_ehlo
  *   
  * - rotator:
  *     connections:
  *       connection_name1:
  *         type
  *         params
  *       connection_name2:
  *         type
  *         params
  *       etc...
  *     requires_ehlo
  *  
  * (*) Mandatory !
  *
  * @param string $type
  * @param array $params
  * @return Swift_Connection
  */
 protected static function getConnection($type, $params = array())
 {
     switch ($type) {
         case 'native':
             $connection = new Swift_Connection_NativeMail();
             if (@$params['additional_params']) {
                 $connection->setAdditionalMailParams($params['additional_params']);
             }
             break;
         case 'smtp':
             if (!@$params['encryption']) {
                 $params['encryption'] = 'OFF';
             }
             $encryption = constant('Swift_Connection_SMTP::ENC_' . $params['encryption']);
             $connection = new Swift_Connection_SMTP($params['server'], @$params['port'], $encryption);
             if (@$params['authentication']) {
                 $connection->setUsername(@$params['authentication']['username']);
                 $connection->setPassword(@$params['authentication']['password']);
             }
             if (@$params['timeout']) {
                 $connection->setTimeout($params['timeout']);
             }
             if (@$params['requires_ehlo']) {
                 $connection->setRequiresEHLO(true);
             }
             break;
         case 'sendmail':
             $connection = new Swift_Connection_Sendmail();
             if (@$params['command']) {
                 $connection->setCommand($params['command']);
             }
             if (@$params['flags']) {
                 $connection->setFlags($params['flags']);
             }
             if (@$params['timeout']) {
                 $connection->setTimeout($params['timeout']);
             }
             if (@$params['requires_ehlo']) {
                 $connection->setRequiresEHLO(true);
             }
             break;
         case 'multi':
             $connection = new Swift_Connection_Multi();
             foreach ($params['connections'] as $id => $conn_info) {
                 $connection->addConnection(self::getConnection($conn_info['type'], $conn_info['params']));
             }
             if (@$params['requires_ehlo']) {
                 $connection->setRequiresEHLO(true);
             }
             break;
         case 'rotator':
             $connection = new Swift_Connection_Multi();
             foreach ($params['connections'] as $id => $conn_info) {
                 $connection->addConnection(self::getConnection($conn_info['type'], $conn_info['params']));
             }
             if (@$params['requires_ehlo']) {
                 $connection->setRequiresEHLO(true);
             }
             break;
     }
     return $connection;
 }