예제 #1
0
 function SendJabber()
 {
     global $db, $fs;
     include_once BASEDIR . '/includes/class.jabber2.php';
     if (empty($fs->prefs['jabber_server']) || empty($fs->prefs['jabber_port']) || empty($fs->prefs['jabber_username']) || empty($fs->prefs['jabber_password'])) {
         return false;
     }
     // get listing of all pending jabber notifications
     $result = $db->Query("SELECT DISTINCT message_id\n                            FROM {notification_recipients}\n                            WHERE notify_method='j'");
     if (!$db->CountRows($result)) {
         return false;
     }
     $JABBER = new Jabber($fs->prefs['jabber_username'] . '@' . $fs->prefs['jabber_server'], $fs->prefs['jabber_password'], $fs->prefs['jabber_ssl'], $fs->prefs['jabber_port']);
     $JABBER->login();
     // we have notifications to process - connect
     $JABBER->log("We have notifications to process...");
     $JABBER->log("Starting Jabber session:");
     $ids = array();
     while ($row = $db->FetchRow($result)) {
         $ids[] = $row['message_id'];
     }
     $desired = join(",", array_map('intval', $ids));
     $JABBER->log("message ids to send = {" . $desired . "}");
     // removed array usage as it's messing up the select
     // I suspect this is due to the variable being comma separated
     // Jamin W. Collins 20050328
     $notifications = $db->Query("\n\t\t\t\tSELECT * FROM {notification_messages}\n\t\t\t\tWHERE message_id IN ({$desired})\n\t\t\t\tORDER BY time_created ASC");
     $JABBER->log("number of notifications {" . $db->CountRows($notifications) . "}");
     // loop through notifications
     while ($notification = $db->FetchRow($notifications)) {
         $subject = $notification['message_subject'];
         $body = $notification['message_body'];
         $JABBER->log("Processing notification {" . $notification['message_id'] . "}");
         $recipients = $db->Query("\n\t\t\t\tSELECT * FROM {notification_recipients}\n\t\t\t\tWHERE message_id = ?\n\t\t\t\tAND notify_method = 'j'", array($notification['message_id']));
         // loop through recipients
         while ($recipient = $db->FetchRow($recipients)) {
             $jid = $recipient['notify_address'];
             $JABBER->log("- attempting send to {" . $jid . "}");
             // send notification
             if ($JABBER->send_message($jid, $body, $subject, 'normal')) {
                 // delete entry from notification_recipients
                 $result = $db->Query("DELETE FROM {notification_recipients}\n                                         WHERE message_id = ?\n                                         AND notify_method = 'j'\n                                         AND notify_address = ?", array($notification['message_id'], $jid));
                 $JABBER->log("- notification sent");
             } else {
                 $JABBER->log("- notification not sent");
             }
         }
         // check to see if there are still recipients for this notification
         $result = $db->Query("SELECT * FROM {notification_recipients}\n                                  WHERE message_id = ?", array($notification['message_id']));
         if ($db->CountRows($result) == 0) {
             $JABBER->log("No further recipients for message id {" . $notification['message_id'] . "}");
             // remove notification no more recipients
             $result = $db->Query("DELETE FROM {notification_messages}\n                                     WHERE message_id = ?", array($notification['message_id']));
             $JABBER->log("- Notification deleted");
         }
     }
     // disconnect from server
     $JABBER->disconnect();
     $JABBER->log("Disconnected from Jabber server");
     return true;
 }