function init() { $this->rcmail = rcmail::get_instance(); $skin = $this->rcmail->config->get('skin'); $this->add_texts('localization/'); $this->include_script('pop3fetcher.js'); // abort if there are no css adjustments if (!file_exists('plugins/pop3fetcher/skins/' . $skin . '/pop3fetcher.css')) { if (!file_exists('plugins/pop3fetcher/skins/default/pop3fetcher.css')) { $this->skin = "default"; } else { $this->skin = "default"; } } $this->add_hook('render_page', array($this, 'render_page')); $this->register_action('plugin.pop3fetcher', array($this, 'navigation')); $this->add_hook('preferences_sections_list', array($this, 'settings_link')); $this->add_hook('preferences_list', array($this, 'settings')); $this->add_hook('template_object_pop3fetcher_form_edit', array($this, 'accounts_edit')); $this->add_hook('template_object_pop3fetcher_form_add', array($this, 'accounts_add')); // ADDED A CHECK ON ACTION "refresh", used since Roundcube 9.X if ($_GET['_action'] == 'check-recent' || $_GET['_action'] == 'plugin.checkunread' || $_GET['_action'] == 'refresh') { define('DISPLAY_XPM4_ERRORS', false); // display XPM4 errors // path to 'POP3.php' file from XPM4 package require_once './plugins/pop3fetcher/XPM4/POP35.php'; require_once './plugins/pop3fetcher/XPM4/FUNC5.php'; $user_id = $this->rcmail->user->data['user_id']; $query = "SELECT * FROM " . get_table_name('pop3fetcher_accounts') . " WHERE user_id=?"; $ret = $this->rcmail->db->query($query, $user_id); $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->config["debug"]) { write_log("pop3fetcher.txt", "INTERCEPT: task mail, checkunread for " . $val['pop3fetcher_email']); } // SCARICO I MESSAGGI DAL POP3 //print_r($val); $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); //print_r($_RESULT); if ($c) { $s = POP35::pStat($c, false); // or die(print_r($_RESULT)); if (is_array($s)) { //$Count - total number of messages, $b - total bytes list($Count, $b) = each($s); if ($this->config["debug"]) { write_log("pop3fetcher.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++) { //write_log("pop3fetcher.txt", "INTERCEPT: task mail, MSG UIDL: ".$msglist["$j"]); //write_log("pop3fetcher.txt", "INTERCEPT: task mail, LAST UIDL: ".$last_uidl); if ($msglist["{$j}"] == $last_uidl) { $i = $j + 1; break; } } if ($Count < $i) { POP35::disconnect($c); } if ($Count == 0) { //echo "Login OK: Inbox EMPTY<br />"; POP35::disconnect($c); } else { $newmsgcount = $Count - $i + 1; //echo "Login OK: Inbox contains [" . $newmsgcount . "] messages<br />"; } // These two calls create errors in Roundcube 0.7.2, maybe they are useless also in later versions.... testing... $this->rcmail->get_storage(); $this->rcmail->imap_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->config["max_forwarded_message_size"] || $this->config["max_forwarded_message_size"] == 0) { $cur_bytes_downloaded = $cur_bytes_downloaded + $l["{$i}"]; if ($this->config["debug"]) { write_log("pop3fetcher.txt", "INTERCEPT: task mail, k={$k} Downloading a message with UIDL " . $msglist["{$cur_msg_index}"] . ", SIZE: " . $l["{$cur_msg_index}"]); } //set_time_limit(20); // 20 seconds per message max $Message = POP35::pRetr($c, $cur_msg_index) or die(print_r($_RESULT)); // <- get the last mail (newest) if ($this->config["debug"]) { write_log("pop3fetcher.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->config["debug"]) { write_log("pop3fetcher.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->config["debug"]) { write_log("pop3fetcher.txt", "INTERCEPT: DELETING MESSAGE {$cur_msg_index} {$last_uidl}"); } } else { if ($this->config["debug"]) { write_log("pop3fetcher.txt", "INTERCEPT: ERROR: CANNOT DELETE {$last_uidl}"); } } } else { if ($this->config["debug"]) { write_log("pop3fetcher.txt", "INTERCEPT: leaveacopyonserver IS SET"); } } if ($cur_bytes_downloaded > $max_bytes_downloaded_x_session) { $i = $Count + 1; } } else { if ($this->config["debug"]) { write_log("pop3fetcher.txt", "INTERCEPT: Skipped message {$last_uidl} "); } } if ($this->config["debug"]) { write_log("pop3fetcher.txt", "INTERCEPT: trying to update DB: {$last_uidl} " . $val['pop3fetcher_id']); } $query = "UPDATE " . get_table_name('pop3fetcher_accounts') . " SET last_uidl=? WHERE pop3fetcher_id=?"; $ret = $this->rcmail->db->query($query, $last_uidl, $val['pop3fetcher_id']); if ($this->config["debug"]) { write_log("pop3fetcher.txt", "INTERCEPT: updated DB: {$last_uidl} " . $val['pop3fetcher_id']); } $k = $k + 1; } else { $i = $Count + 1; } } } POP35::disconnect($c); } else { if ($this->config["debug"]) { write_log("pop3fetcher.txt", "INTERCEPT: task mail, ERROR ON is_array FOR " . $val['pop3fetcher_email']); } } } else { if ($this->config["debug"]) { write_log("pop3fetcher.txt", "INTERCEPT: task mail, POP35::connectfailed for " . $val['pop3fetcher_email']); } } } } }
/** * 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']); } } } }