/** * Adds default settings when plugin is activated */ function bp_rbe_activate() { // Load the bp-rbe functions file require BP_RBE_DIR . '/includes/bp-rbe-functions.php'; if (!($settings = bp_get_option('bp-rbe'))) { $settings = array(); } // Set default mode to Inbound if no mode exists if (!isset($settings['mode'])) { $settings['mode'] = 'inbound'; } // generate a unique key if one doesn't exist if (!isset($settings['key'])) { $settings['key'] = uniqid(''); } // set a default value for the keepalive value if (!isset($settings['keepalive'])) { $settings['keepalive'] = bp_rbe_get_execution_time('minutes'); } bp_update_option('bp-rbe', $settings); // remove remnants from any previous failed attempts to stop the inbox bp_rbe_cleanup(); }
/** * Adds webhost warnings to the admin page. * * If certain conditions for the webhost are not met, these warnings will be displayed on the admin page. */ protected function webhost_warnings() { if (bp_rbe_is_inbound()) { return; } $warnings = array(); if (!function_exists('imap_open')) { $warnings[] = __('IMAP extension for PHP is <strong>disabled</strong>. This plugin will not run without it. Please contact your webhost to enable this.', 'bp-rbe'); } if (ini_get('safe_mode')) { $warnings[] = sprintf(__('PHP Safe Mode is <strong>on</strong>. This is not a dealbreaker, but this means you cannot set the Keep Alive Connection value larger than %d minutes.', 'bp-rbe'), bp_rbe_get_execution_time('minutes')); } if (!empty($warnings)) { echo '<h3>' . __('Webhost Warnings', 'bp-rbe') . '</h3>'; } foreach ($warnings as $warning) { echo '<div class="error"><p>' . $warning . '</p></div>'; } }
/** * Check if we're connected to the IMAP inbox. * * This is updated in {@link BP_Reply_By_Email_IMAP::connect()} and in * {@link BP_Reply_By_Email_IMAP::close()}. * * @since 1.0-RC4 * * @return bool */ function bp_rbe_is_connected($args = array()) { $r = wp_parse_args($args, array('clearcache' => true)); if (true == $r['clearcache']) { clearstatcache(); } $file = bp_core_avatar_upload_path() . '/bp-rbe-connected.txt'; if (file_exists($file)) { // check if we're connected if (time() <= filemtime($file) + bp_rbe_get_execution_time() + 15) { return true; } } return false; }
/** * The main method we use to parse an IMAP inbox. */ public function run() { // $instance must be initialized before we go on! if (self::$instance === false) { return false; } // If safe mode isn't on, then let's set the execution time to unlimited if (!ini_get('safe_mode')) { set_time_limit(0); } // Try to connect $connect = $this->connect(); if (!$connect) { return false; } // Total duration we should keep the IMAP stream alive for in seconds $duration = bp_rbe_get_execution_time(); bp_rbe_log('--- Keep alive for ' . $duration / 60 . ' minutes ---'); bp_rbe_remove_imap_lock(); // Mark the current timestamp, mark the future time when we should close the IMAP connection; // Do our parsing until $future > $now; re-mark the timestamp at end of loop... rinse and repeat! for ($now = time(), $future = time() + $duration; $future > $now; $now = time()) { // Get number of messages $message_count = imap_num_msg($this->connection); // If there are messages in the inbox, let's start parsing! if ($message_count != 0) { // According to this: // http://www.php.net/manual/pl/function.imap-headerinfo.php#95012 // This speeds up rendering the email headers... could be wrong imap_headers($this->connection); bp_rbe_log('- Checking inbox -'); // Loop through each email message for ($i = 1; $i <= $message_count; ++$i) { // flush object cache if necessary // // we only flush the cache if the default object cache is in use // why? b/c the default object cache is only meant for single page loads and // since RBE runs in the background, we need to flush the object cache so WP // will do a direct DB query for the next data fetch if (!wp_using_ext_object_cache()) { wp_cache_flush(); } self::$html = false; $content = self::body_parser($this->connection, $i); $headers = $this->get_mail_headers($this->connection, $i); $data = array('headers' => $headers, 'to_email' => BP_Reply_By_Email_Parser::get_header($headers, 'To'), 'from_email' => BP_Reply_By_Email_Parser::get_header($headers, 'From'), 'content' => $content, 'is_html' => self::$html, 'subject' => imap_utf8(BP_Reply_By_Email_Parser::get_header($headers, 'Subject'))); $parser = BP_Reply_By_Email_Parser::init($data, $i); if (is_wp_error($parser)) { //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, $parser->get_error_code() ); do_action('bp_rbe_no_match', $parser, $data, $i, $this->connection); } // do something during the loop // we mark the message for deletion here via this hook do_action('bp_rbe_imap_loop', $this->connection, $i); // unset some variables at the end of the loop unset($content, $headers, $data, $parser); } // do something after the loop do_action('bp_rbe_imap_after_loop', $this->connection); } // stop the loop if necessary if (bp_rbe_should_stop()) { if ($this->close()) { bp_rbe_log('--- Manual termination of connection confirmed! Kaching! ---'); } else { bp_rbe_log('--- Error - invalid connection during manual termination ---'); } remove_action('shutdown', 'bp_rbe_spawn_inbox_check'); exit; } // Give IMAP server a break sleep(10); // If the IMAP connection is down, reconnect if (!imap_ping($this->connection)) { bp_rbe_log('-- IMAP connection is down, attempting to reconnect... --'); if (bp_rbe_is_connecting(array('clearcache' => true))) { bp_rbe_log('--- RBE is already attempting to connect - stopping connection attempt ---'); continue; } // add lock marker before connecting bp_rbe_add_imap_lock(); // attempt to reconnect $reopen = BP_Reply_By_Email_Connect::init(array('reconnect' => true), $this->connection); if ($reopen) { bp_rbe_log('-- Reconnection successful! --'); } else { bp_rbe_log('-- Reconnection failed! :( --'); bp_rbe_log('Cannot connect: ' . imap_last_error()); // cleanup RBE after failure bp_rbe_cleanup(); remove_action('shutdown', 'bp_rbe_spawn_inbox_check'); exit; } } // Unset some variables to clear some memory unset($message_count); } if ($this->close()) { bp_rbe_log('--- Closing current connection automatically ---'); } else { bp_rbe_log('--- Invalid connection during close time ---'); } // autoconnect is off if (1 !== (int) bp_rbe_get_setting('keepaliveauto', array('refetch' => true))) { remove_action('shutdown', 'bp_rbe_spawn_inbox_check'); // sleep a bit before autoconnecting again } else { sleep(5); } exit; }