Exemple #1
0
/**
 * Remove our scheduled function from WP and stop the IMAP loop.
 */
function bp_rbe_deactivate()
{
    // stop IMAP connection if active
    if (bp_rbe_is_connected()) {
        bp_rbe_stop_imap();
        // give plugin a chance to stop IMAP connection as it could be sleeping
        sleep(10);
        bp_rbe_log('Daisy, Daisy, give me your answer, do...');
    }
    // remove remnants from any previous failed attempts to stop the inbox
    bp_rbe_cleanup();
    bp_rbe_log('Plugin deactivated!');
}
 /**
  * Validate and sanitize our options before saving to the DB.
  *
  * Callback from {@link register_setting()}.
  *
  * @param array $input The submitted values from the form
  * @return array $output The validated values to be inserted into the DB
  */
 public function validate($input)
 {
     $messages = false;
     $output = array();
     $output['mode'] = wp_filter_nohtml_kses($input['mode']);
     // switching from IMAP to inbound email mode
     if ('inbound' == $output['mode'] && 'imap' == bp_rbe_get_setting('mode')) {
         // stop RBE if still connected via IMAP
         if (bp_rbe_is_connected()) {
             bp_rbe_stop_imap();
         }
         bp_rbe_log('- Operating mode switched to inbound -');
     }
     // check if key is alphanumeric
     if (ctype_alnum($input['key'])) {
         $output['key'] = $input['key'];
     }
     /** INBOUND-related ***************************************************/
     $inbound_provider = wp_filter_nohtml_kses($input['inbound-provider']);
     if (!empty($inbound_provider)) {
         $output['inbound-provider'] = $inbound_provider;
     }
     $inbound_domain = isset($input['inbound-domain']) ? wp_filter_nohtml_kses($input['inbound-domain']) : '';
     if (!empty($inbound_domain)) {
         $output['inbound-domain'] = $inbound_domain;
     }
     /** IMAP-related ******************************************************/
     $username = wp_filter_nohtml_kses($input['username']);
     $password = wp_filter_nohtml_kses($input['password']);
     if ($email = is_email($input['email'])) {
         $output['email'] = $email;
         if ($input['gmail'] == 1) {
             $output['username'] = $email;
         }
     }
     if (!empty($username)) {
         $output['username'] = $username;
         if (is_email($username)) {
             $output['email'] = $username;
         }
     }
     if (!empty($password)) {
         $output['password'] = $password;
     }
     // check if port is numeric
     if (is_numeric($input['port'])) {
         $output['port'] = $input['port'];
     }
     // check if address tag is one character
     if (strlen($input['tag']) == 1) {
         $output['tag'] = $input['tag'];
     }
     // override certain settings if "gmail" setting is true
     if (!empty($input['gmail']) && $input['gmail'] == 1) {
         $output['servername'] = 'imap.gmail.com';
         $output['port'] = 993;
         $output['tag'] = '+';
         $output['gmail'] = 1;
         $output['email'] = $username;
         // use alternate server settings as defined by the user
     } else {
         $output['servername'] = wp_filter_nohtml_kses($input['servername']);
         $output['port'] = absint($input['port']);
         $output['tag'] = wp_filter_nohtml_kses($input['tag']);
         $output['gmail'] = 0;
     }
     if (is_numeric($input['keepalive']) && $input['keepalive'] < 30) {
         $output['keepalive'] = $input['keepalive'];
     }
     // keepalive for safe mode will never exceed the execution time
     if (ini_get('safe_mode')) {
         $output['keepalive'] = $input['keepalive'] = bp_rbe_get_execution_time('minutes');
     }
     // automatically reconnect after keep-alive limit
     if (!empty($input['keepaliveauto'])) {
         $output['keepaliveauto'] = 1;
     } else {
         $output['keepaliveauto'] = 0;
     }
     // do a quick imap check if we have valid credentials to check
     if ($output['mode'] == 'imap' && !empty($output['servername']) && !empty($output['port']) && !empty($output['username']) && !empty($output['password'])) {
         if (function_exists('imap_open')) {
             $imap = BP_Reply_By_Email_Connect::init(array('host' => $output['servername'], 'port' => $output['port'], 'username' => $output['username'], 'password' => $output['password']));
             // if connection failed, add an error
             if ($imap === false) {
                 $errors = imap_errors();
                 $messages['connect_error'] = sprintf(__('Error: Unable to connect to inbox - %s', 'bp-rbe'), $errors[0]);
                 $output['connect'] = 0;
                 // connection was successful, now close our temporary connection
             } else {
                 // this tells bp_rbe_is_required_completed() that we're good to go!
                 $output['connect'] = 1;
                 imap_close($imap);
             }
         }
     }
     // encode the password
     if (!empty($password)) {
         $output['password'] = bp_rbe_encode(array('string' => $password, 'key' => wp_salt()));
     }
     /**********************************************************************/
     /* error time! */
     if (strlen($input['tag']) > 1 && !$output['tag']) {
         $messages['tag_error'] = __('Error: <strong>Address tag</strong> must only be one character.', 'bp-rbe');
     }
     if (!empty($input['port']) && !is_numeric($input['port']) && !$output['port']) {
         $messages['port_error'] = __('Error: <strong>Port</strong> must be numeric.', 'bp-rbe');
     }
     if (!empty($input['key']) && !$output['key']) {
         $messages['key_error'] = __('Error: <strong>Key</strong> must only contain letters and / or numbers.', 'bp-rbe');
     }
     if (!empty($input['keepalive']) && !is_numeric($input['keepalive']) && !$output['keepalive']) {
         $messages['keepalive_error'] = __('Error: <strong>Keep Alive Connection</strong> value must be less than 30.', 'bp-rbe');
     }
     if (is_array($messages)) {
         $output['messages'] = $messages;
     }
     // For sites using an external object cache, make sure they get the new value.
     // This is all sorts of ugly! :(
     // I think this is a WP core bug with the WP Settings API...
     wp_cache_delete('alloptions', 'options');
     return $output;
 }