예제 #1
0
 function add_do()
 {
     $rcmail = $this->rcmail;
     $pop3fetcher_email = get_input_value('_pop3fetcher_email', RCUBE_INPUT_POST);
     $pop3fetcher_username = get_input_value('_pop3fetcher_username', RCUBE_INPUT_POST);
     $pop3fetcher_password = get_input_value('_pop3fetcher_password', RCUBE_INPUT_POST);
     $pop3fetcher_serveraddress = get_input_value('_pop3fetcher_serveraddress', RCUBE_INPUT_POST);
     $pop3fetcher_serverport = get_input_value('_pop3fetcher_serverport', RCUBE_INPUT_POST);
     $pop3fetcher_ssl = get_input_value('_pop3fetcher_ssl', RCUBE_INPUT_POST);
     $pop3fetcher_leaveacopy = get_input_value('_pop3fetcher_leaveacopy', RCUBE_INPUT_POST);
     $pop3fetcher_provider = get_input_value('_pop3fetcher_provider', RCUBE_INPUT_POST);
     $pop3fetcher_testconnection = get_input_value('_pop3fetcher_testconnection', RCUBE_INPUT_POST);
     $pop3fetcher_defaultfolder = get_input_value('_pop3fetcher_defaultfolder', RCUBE_INPUT_POST);
     $pop3fetcher_import_old_messages = get_input_value('_pop3fetcher_import_old_messages', RCUBE_INPUT_POST);
     //MUST CREATE THE TARGET FOLDER IF IT DOESN'T EXIST
     $rcmail->imap_connect();
     $delimiter = $rcmail->imap->get_hierarchy_delimiter();
     if ($this->config["debug"]) {
         write_log("pop3fetcher.txt", "SET DEFAULT FOLDER ON SAVE: {$pop3fetcher_defaultfolder}");
     }
     if ($pop3fetcher_defaultfolder == "#AUTO_FOLDER#") {
         $pop3fetcher_defaultfolder = $this->config["root_folder_path"] . $delimiter . str_replace($delimiter, "_", $pop3fetcher_email);
     }
     // check if the folder exists
     if ($rcmail->imap->folder_exists($pop3fetcher_defaultfolder)) {
         if ($this->config["debug"]) {
             write_log("pop3fetcher.txt", "INTERCEPT: settings, FOLDER {$pop3fetcher_defaultfolder} EXISTS");
         }
     } else {
         $rcmail->imap->create_folder($pop3fetcher_defaultfolder, true);
         if ($this->config["debug"]) {
             write_log("pop3fetcher.txt", "INTERCEPT: settings, FOLDER {$pop3fetcher_defaultfolder} DOESN'T EXIST, MUST CREATE IT!");
         }
     }
     if ($pop3fetcher_leaveacopy == "true") {
         $pop3fetcher_leaveacopy = true;
     } else {
         $pop3fetcher_leaveacopy = false;
     }
     if ($pop3fetcher_import_old_messages == "true") {
         $pop3fetcher_import_old_messages = true;
     } else {
         $pop3fetcher_import_old_messages = false;
     }
     $user_id = $rcmail->user->data['user_id'];
     /* QUA VERIFICO LA CONNESSIONE */
     if ($pop3fetcher_testconnection == "true" && !$this->test_connection($pop3fetcher_username, $pop3fetcher_password, $pop3fetcher_serveraddress, $pop3fetcher_serverport, $pop3fetcher_ssl)) {
         $rcmail->output->add_label('pop3fetcher.account_unableconnect');
         $this->rcmail->output->command('plugin.add_do_error_connecting', array());
         $this->rcmail->output->send('plugin');
     } else {
         require_once './plugins/pop3fetcher/XPM4/POP35.php';
         require_once './plugins/pop3fetcher/XPM4/FUNC5.php';
         // SALVO L'UID DELL'ULTIMO MESSAGGIO, IN MODO DA SCARICARE SOLO I NUOVI
         $last_uidl = 0;
         if (!$pop3fetcher_import_old_messages) {
             $c = POP35::connect($pop3fetcher_serveraddress, $pop3fetcher_username, $pop3fetcher_password, intval($pop3fetcher_serverport), $pop3fetcher_ssl, 100);
             if ($c) {
                 $msglist = POP35::puidl($c);
                 $last_uidl = $msglist[count($msglist) - 1];
                 if ($this->config["debug"]) {
                     write_log("pop3fetcher.txt", "INTERCEPT: TASK ADDACCOUNT, fetching last UID {$last_uidl}");
                 }
             }
         }
         $query = "SELECT * FROM " . get_table_name('pop3fetcher_accounts') . " WHERE user_id=? AND pop3fetcher_email=?";
         $ret = $rcmail->db->query($query, $user_id, $pop3fetcher_email);
         $arr = $rcmail->db->fetch_assoc($ret);
         if (!is_array($arr)) {
             $query = "INSERT INTO " . get_table_name('pop3fetcher_accounts') . "(pop3fetcher_email, pop3fetcher_username, pop3fetcher_password, pop3fetcher_serveraddress, pop3fetcher_serverport, pop3fetcher_ssl, pop3fetcher_leaveacopyonserver, user_id, last_uidl, pop3fetcher_provider, default_folder) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
             $ret = $rcmail->db->query($query, $pop3fetcher_email, $pop3fetcher_username, $pop3fetcher_password, $pop3fetcher_serveraddress, $pop3fetcher_serverport, $pop3fetcher_ssl, $pop3fetcher_leaveacopy, $user_id, $last_uidl, $pop3fetcher_provider, $pop3fetcher_defaultfolder);
             if ($ret) {
                 $this->rcmail->output->command('plugin.add_do_ok', array());
                 $this->rcmail->output->send('plugin');
             } else {
                 $this->rcmail->output->command('plugin.add_do_ok', array());
                 $this->rcmail->output->send('plugin');
             }
         } else {
             $this->rcmail->output->command('plugin.add_do_ok', array());
             $this->rcmail->output->send('plugin');
         }
     }
 }
예제 #2
0
 /**
  * Synchronize all available pop3fetcher accounts
  *
  * @return	void
  */
 public function synchronize()
 {
     define('DISPLAY_XPM4_ERRORS', false);
     // display XPM4 errors
     require_once $this->docRoot . '/plugins/pop3fetcher/XPM4/POP35.php';
     require_once $this->docRoot . '/plugins/pop3fetcher/XPM4/FUNC5.php';
     $this->rcmail = rcmail::get_instance();
     $query = "\n\t\t\tSELECT\n\t\t\t\t`t1`.*, `t2`.`username`, `t2`.`mail_host`,\n\t\t\t\t`t3`.`mail_pass`\n\t\t\tFROM\n\t\t\t\t`{IMSCP_DATABASE}_roundcube`.`pop3fetcher_accounts` AS `t1`\n\t\t\tLEFT JOIN\n\t\t\t\t`{IMSCP_DATABASE}_roundcube`.`users` AS `t2` ON(`t2`.`user_id` = `t1`.`user_id`)\n\t\t\tLEFT JOIN\n\t\t\t\t`{IMSCP_DATABASE}`.`mail_users` AS `t3` ON(`t3`.`mail_addr` = `t2`.`username`)\n\t\t\tWHERE\n\t\t\t\t(`t3`.`mail_type` LIKE '%normal_mail%' OR `t3`.`mail_type` LIKE '%alias_mail%' OR `t3`.`mail_type` LIKE '%subdom_mail%')\n\t\t\t\t\n\t\t\tORDER BY\n\t\t\t\t`t1`.`user_id` ASC\n\t\t";
     $ret = $this->rcmail->db->query($query);
     $accounts = array();
     while ($account = $this->rcmail->db->fetch_assoc($ret)) {
         $accounts[] = $account;
     }
     $temparr = array();
     foreach ($accounts as $key => $val) {
         $temparr[$key] = $val['pop3fetcher_email'];
         if ($this->debug) {
             write_log("pop3fetcher_cron.txt", "INTERCEPT: task mail, checkunread for " . $val['pop3fetcher_email']);
         }
         $last_uidl = $val['last_uidl'];
         $c = POP35::connect($val['pop3fetcher_serveraddress'], $val['pop3fetcher_username'], $val['pop3fetcher_password'], intval($val['pop3fetcher_serverport']), $val['pop3fetcher_ssl'], 100);
         if ($c) {
             $s = POP35::pStat($c, false);
             if (is_array($s)) {
                 list($Count, $b) = each($s);
                 if ($this->debug) {
                     write_log("pop3fetcher_cron.txt", "INTERCEPT: task mail, found new messages: {$Count}, {$b}");
                 }
                 $msglist = POP35::puidl($c);
                 if (is_array($msglist)) {
                     $Count = count($msglist);
                 } else {
                     $Count = 0;
                 }
                 if (sizeof($msglist) > 0) {
                     $i = 0;
                     for ($j = 1; $j <= sizeof($msglist); $j++) {
                         if ($this->debug) {
                             write_log("pop3fetcher_cron.txt", "INTERCEPT: task mail, MSG UIDL: " . $msglist["{$j}"]);
                         }
                         if ($this->debug) {
                             write_log("pop3fetcher_cron.txt", "INTERCEPT: task mail, LAST UIDL: " . $last_uidl);
                         }
                         if ($msglist["{$j}"] == $last_uidl) {
                             $i = $j;
                             break;
                         }
                     }
                     if ($Count < $i) {
                         POP35::disconnect($c);
                     }
                     if ($Count == 0) {
                         POP35::disconnect($c);
                     } else {
                         $newmsgcount = $Count - $i;
                         if ($this->debug && $this->debug_console) {
                             echo "Login OK: Inbox contains [" . $newmsgcount . "] messages\n";
                         }
                     }
                     if ($newmsgcount > 0) {
                         // Start to login to the imap server
                         $this->rcmail->kill_session();
                         if ($this->debug && $this->debug_console) {
                             echo "Start Login IMAP Server (" . $val['mail_host'] . ") with account: " . $val['username'] . "\n";
                         }
                         if ($this->debug) {
                             write_log("pop3fetcher_cron.txt", "Start Login IMAP Server (" . $val['mail_host'] . ") with account: " . $val['username']);
                         }
                         if ($this->rcmail->login($val['username'], $val['mail_pass'], $auth['mail_host'], false)) {
                             $this->rcmail->get_storage();
                             $this->rcmail->storage_connect();
                             $max_messages_downloaded_x_session = 10;
                             $max_bytes_downloaded_x_session = 1000000;
                             $cur_bytes_downloaded = 0;
                             $k = 1;
                             for (; $i < $Count; $i++) {
                                 if ($k <= $max_messages_downloaded_x_session) {
                                     $cur_msg_index = $i + 1;
                                     $l = POP35::pList($c, $cur_msg_index);
                                     $last_uidl = $msglist["{$cur_msg_index}"];
                                     if ($l["{$cur_msg_index}"] <= $this->max_forwarded_message_size || $this->max_forwarded_message_size == 0) {
                                         $cur_bytes_downloaded = $cur_bytes_downloaded + $l["{$i}"];
                                         if ($this->debug && $this->debug_console) {
                                             echo "INTERCEPT: task mail, k=" . $k . " Downloading a message with UIDL " . $msglist["{$cur_msg_index}"] . ", SIZE: " . $l["{$cur_msg_index}"] . "\n";
                                         }
                                         if ($this->debug) {
                                             write_log("pop3fetcher_cron.txt", "INTERCEPT: task mail, k=" . $k . " Downloading a message with UIDL " . $msglist["{$cur_msg_index}"] . ", SIZE: " . $l["{$cur_msg_index}"]);
                                         }
                                         $Message = POP35::pRetr($c, $cur_msg_index) or die(print_r($_RESULT));
                                         if ($this->debug && $this->debug_console) {
                                             echo "INTERCEPT: task mail, k=" . $k . "  Downloaded a message with UIDL " . $msglist["{$cur_msg_index}"] . "\n";
                                         }
                                         if ($this->debug) {
                                             write_log("pop3fetcher_cron.txt", "INTERCEPT: task mail, k=" . $k . "  Downloaded a message with UIDL " . $msglist["{$cur_msg_index}"]);
                                         }
                                         $message_id = $this->rcmail->storage->save_message($val['default_folder'], $Message);
                                         if ($this->debug && $this->debug_console) {
                                             echo "INTERCEPT: task mail, k=" . $k . " Stored a message with UIDL " . $msglist["{$cur_msg_index}"] . "\n";
                                         }
                                         if ($this->debug) {
                                             write_log("pop3fetcher_cron.txt", "INTERCEPT: task mail, k=" . $k . " Stored a message with UIDL " . $msglist["{$cur_msg_index}"]);
                                         }
                                         $this->rcmail->storage->unset_flag($message_id, "SEEN", $val['default_folder']);
                                         if (!$val['pop3fetcher_leaveacopyonserver']) {
                                             if (POP35::pDele($c, $cur_msg_index)) {
                                                 if ($this->debug && $this->debug_console) {
                                                     echo "INTERCEPT: DELETING MESSAGE " . $cur_msg_index . " " . $last_uidl . "\n";
                                                 }
                                                 if ($this->debug) {
                                                     write_log("pop3fetcher_cron.txt", "INTERCEPT: DELETING MESSAGE " . $cur_msg_index . " " . $last_uidl);
                                                 }
                                             } else {
                                                 if ($this->debug && $this->debug_console) {
                                                     echo "INTERCEPT: ERROR: CANNOT DELETE " . $last_uidl . "\n";
                                                 }
                                                 if ($this->debug) {
                                                     write_log("pop3fetcher_cron.txt", "INTERCEPT: ERROR: CANNOT DELETE " . $last_uidl);
                                                 }
                                             }
                                         } else {
                                             if ($this->debug && $this->debug_console) {
                                                 echo "INTERCEPT: leaveacopyonserver IS SET\n";
                                             }
                                             if ($this->debug) {
                                                 write_log("pop3fetcher_cron.txt", "INTERCEPT: leaveacopyonserver IS SET");
                                             }
                                         }
                                         if ($cur_bytes_downloaded > $max_bytes_downloaded_x_session) {
                                             $i = $Count + 1;
                                         }
                                     } else {
                                         if ($this->debug && $this->debug_console) {
                                             echo "INTERCEPT: Skipped message " . $last_uidl . "\n";
                                         }
                                         if ($this->debug) {
                                             write_log("pop3fetcher_cron.txt", "INTERCEPT: Skipped message " . $last_uidl);
                                         }
                                     }
                                     if ($this->debug) {
                                         write_log("pop3fetcher_cron.txt", "INTERCEPT: trying to update DB: " . $last_uidl . " " . $val['pop3fetcher_id']);
                                     }
                                     $query = "UPDATE {IMSCP_DATABASE}_roundcube.pop3fetcher_accounts SET last_uidl = ? WHERE pop3fetcher_id = ?";
                                     $ret = $this->rcmail->db->query($query, $last_uidl, $val['pop3fetcher_id']);
                                     if ($this->debug) {
                                         write_log("pop3fetcher_cron.txt", "INTERCEPT: updated DB: " . $last_uidl . " " . $val['pop3fetcher_id']);
                                     }
                                     $k = $k + 1;
                                 } else {
                                     $i = $Count + 1;
                                 }
                             }
                             $this->rcmail->logout_actions();
                             $this->rcmail->kill_session();
                         } else {
                             if ($this->debug && $this->debug_console) {
                                 echo "ERROR: Can't login to IMAP Server (" . $val['mail_host'] . ") with account: " . $val['username'] . "\n";
                             }
                             if ($this->debug) {
                                 write_log("pop3fetcher_cron.txt", "ERROR: Can't login to IMAP Server (" . $val['mail_host'] . ") with account: " . $val['username']);
                             }
                         }
                     } else {
                         if ($this->debug && $this->debug_console) {
                             echo "Nothing to do. Inbox contains [" . $newmsgcount . "] messages\n";
                         }
                         if ($this->debug) {
                             write_log("pop3fetcher_cron.txt", "Nothing to do. Inbox contains [" . $newmsgcount . "] messages");
                         }
                     }
                 }
                 POP35::disconnect($c);
             } else {
                 if ($this->debug) {
                     write_log("pop3fetcher_cron.txt", "INTERCEPT: task mail, ERROR ON is_array FOR " . $val['pop3fetcher_email']);
                 }
             }
         } else {
             if ($this->debug) {
                 write_log("pop3fetcher_cron.txt", "INTERCEPT: task mail, POP35::connectfailed for " . $val['pop3fetcher_email']);
             }
         }
     }
 }