/** * @return bool Whether request is valid. */ protected static function validate_request() { $timestamp = intval($_GET['timestamp']); if (!isset($_GET['timestamp']) or !is_numeric($_GET['timestamp']) or abs(time() - $timestamp) > 60 * 60 * 6) { Prompt_Logging::add_error('inbound_invalid_timestamp', __('Rejected an inbound request with an invalid timestamp. Could be bot activity.', 'Postmatic'), $_GET); return false; } if (empty($_GET['token'])) { Prompt_Logging::add_error('inbound_invalid_token', __('Rejected an inbound request with an invalid token. Could be bot activity.', 'Postmatic'), $_GET); return false; } $token = sanitize_key($_GET['token']); if (!isset($_GET['signature']) or strlen($_GET['signature']) != 64) { Prompt_Logging::add_error('inbound_invalid_signature', __('Rejected an inbound request with an invalid signature. Could be bot activity.', 'Postmatic'), $_GET); return false; } $signature = $_GET['signature']; if (hash_hmac('sha256', $timestamp . $token, Prompt_Core::$options->get('prompt_key')) != $signature) { Prompt_Logging::add_error('inbound_invalid_signature', __('Rejected an inbound request with an invalid signature. Could be bot activity.', 'Postmatic'), $_GET); return false; } if (!Prompt_Core::$options->get('connected')) { Prompt_Core::$options->set('connected', true); } return true; }
/** * Process updates according to their type. * @param object $update * @return string Status: 'delivered', 'lost' */ public function process_update($update) { if ('inbound-email' == $update->type) { return $this->process_inbound_email($update); } Prompt_Logging::add_error('unknown_update_type', __('Unable to deliver a message of unknown type.', 'Postmatic'), $update); return 'lost'; }
/** * * @since 2.0.0 * * @param object $data */ protected function record_successful_outbound_message_batch($data) { if (empty($data->id)) { Prompt_Logging::add_error(Prompt_Enum_Error_Codes::OUTBOUND, __('Got an unrecognized outbound message batch response.', 'Postmatic'), array('result' => $data, 'post_id' => $this->batch->get_context()->get_post()->id())); return; } $this->batch->get_context()->get_post()->add_outbound_message_batch_ids($data->id); }
/** * * @since 2.0.0 * * @param object $data * @return $this */ protected function record_successful_outbound_message_batch($data) { if (empty($data->id)) { Prompt_Logging::add_error(Prompt_Enum_Error_Codes::OUTBOUND, __('Got an unrecognized outbound message batch response.', 'Postmatic'), array('result' => $data, 'comment_id' => $this->comment->comment_ID)); return $this; } $sent_ids = get_comment_meta($this->comment->comment_ID, self::$outbound_message_batch_ids_meta_key, true); $sent_ids = $sent_ids ? $sent_ids : array(); $sent_ids[] = $data->id; update_comment_meta($this->comment->comment_ID, self::$outbound_message_batch_ids_meta_key, $sent_ids); return $this; }
public function form_handler() { $environment = new Prompt_Environment(); $user = wp_get_current_user(); $email = Prompt_Email_Batch::make_for_single_recipient(array('to_address' => Prompt_Core::SUPPORT_EMAIL, 'from_address' => $user->user_email, 'from_name' => $user->display_name, 'subject' => sprintf(__('Diagnostics from %s', 'Postmatic'), html_entity_decode(get_option('blogname'))), 'html_content' => json_encode($environment->to_array()), 'message_type' => Prompt_Enum_Message_Types::ADMIN)); $sent = Prompt_Factory::make_mailer($email)->send(); if (is_wp_error($sent)) { Prompt_Logging::add_error('diagnostic_submission_error', __('Diagnostics could not be sent, please try a bug report.', 'Postmatic'), $sent); return; } $this->add_notice(__('Diagnostics <strong>sent</strong>.', 'Postmatic')); }
/** * Check for site updates. * @return boolean|WP_Error status */ public function pull_configuration() { $response = $this->client->get_site(); if (is_wp_error($response) or 200 != $response['response']['code']) { return Prompt_Logging::add_error('pull_configuration_http', __('A request for site configuration failed.', 'Postmatic'), $response); } $data = json_decode($response['body']); if (!isset($data->site)) { return Prompt_Logging::add_error('pull_configuration_site_missing', __('Configuration data arrived in an unrecognized format.', 'Postmatic'), $data); } return $this->update_configuration($data); }
/** * Get the metadata required to reproduce a command instance. * * @since 2.0.0 * * @param Prompt_Interface_Command $command * @return stdClass */ public static function get_command_metadata(Prompt_Interface_Command $command) { $metadata = new stdClass(); $class = get_class($command); $class_id = self::get_class_id($class); if (is_null($class_id)) { Prompt_Logging::add_error('invalid_command_id', __('Tried to create an email with an unrecognized reply command.', 'Postmatic'), compact('command', 'email')); return $metadata; } $data = $command->get_keys(); array_unshift($data, $class_id); $metadata->ids = $data; return $metadata; }
/** * Augment sending to add chunking. * * @return array */ public function send() { // Bail if we've already sent this chunk if ($this->chunk <= $this->get_delivered_chunk()) { return array(); } // Block other processes from sending this chunk $batch_key = $this->set_delivered_chunk(); $chunks = array_chunk($this->batch->get_individual_message_values(), 30); $this->batch->set_individual_message_values($chunks[$this->chunk]); $result = parent::send(); if (is_wp_error($result)) { Prompt_Logging::add_error(Prompt_Enum_Error_Codes::OUTBOUND, __('A subscription agreement sending operation encountered a problem.', 'Postmatic'), array('error' => $result, 'batch' => $this->batch, 'chunk' => $this->chunk)); } if (!empty($chunks[$this->chunk + 1])) { $this->schedule_next_chunk($batch_key); } return $result; }
public function subscribe($user_id) { $user_id = intval($user_id); if ($user_id <= 0) { Prompt_Logging::add_error('subscribe_user_invalid', __('Refused an attempt to subscribe an invalid user ID.', 'Postmatic'), array('meta_type' => $this->meta_type, 'object_id' => $this->id, 'user_id' => $user_id)); return $this; } $subscriber_ids = $this->subscriber_ids(); if (!in_array($user_id, $subscriber_ids)) { array_push($subscriber_ids, $user_id); update_metadata($this->meta_type, $this->id, self::SUBSCRIBED_META_KEY, $subscriber_ids); /** * A new subscription has been added. * * @param int $subscriber_id * @param Prompt_Interface_Subscribable $object The thing subscribed to. */ do_action('prompt/subscribed', $user_id, $this, $this->meta_type); } return $this; }
/** * Validate a key * * @since 1.0.0 * @param string $key * @return mixed|string|WP_Error */ public function validate_key($key) { if (empty($key)) { return ''; } $key = preg_replace('/\\s/', '', sanitize_text_field($key)); $response = $this->api_client($key)->get_site(); if (is_wp_error($response) or !in_array($response['response']['code'], array(200, 401, 503))) { return Prompt_Logging::add_error('key_http_error', __('There\'s a problem verifying your key. Please try later or report this error.', 'Postmatic'), $response); } if (503 == $response['response']['code']) { $message = sprintf(__('Postmatic service is temporarily unavailable, see <a href="%s">our twitter feed</a> for updates.', 'Postmatic'), Prompt_Enum_Urls::TWITTER); return new WP_Error('service_unavailable', $message); } if (401 == $response['response']['code']) { $message = sprintf(__('We didn\'t recognize the key "%s". Please make sure it exactly matches the key we supplied you. <a href="%s" target="_blank">Visit your Postmatic dashboard for assistance</a>. ', 'Postmatic'), $key, Prompt_Enum_Urls::MANAGE); return new WP_Error('invalid_key', $message); } $configuration = json_decode($response['body']); if (!self::site_matches($configuration->site->url)) { $message = sprintf(__('Your key was registered for a different site. Please request a key for this site\'s dedicated use, or <a href="%s" target="_blank">contact us</a> for assistance. Thanks!', 'Postmatic'), Prompt_Enum_Urls::BUG_REPORTS); return new WP_Error('wrong_key', $message); } if (Prompt_Core::$options->get('enable_digests') and !in_array(Prompt_Enum_Message_Types::DIGEST, $configuration->configuration->enabled_message_types)) { $configuration->configuration->enable_digests = false; } $configurator = Prompt_Factory::make_configurator($this->api_client()); $configurator->update_configuration($configuration); return $key; }
protected function fetch_next_page() { $this->page_index++; $args = array('noheader' => 'true', 'proxy' => '', 'page' => 'stats', 'blog' => $this->blog_id, 'charset' => get_option('blog_charset'), 'color' => get_user_option('admin_color'), 'ssl' => is_ssl(), 'j' => sprintf('%s:%s', $this->api_version, $this->version), 'blog_subscribers' => 0, 'type' => 'email', 'pagenum' => $this->page_index); $url = add_query_arg($args, $this->dashboard_server_url); $method = 'GET'; $timeout = 90; $user_id = $this->master_user; $get = call_user_func($this->remote_request, compact('url', 'method', 'timeout', 'user_id')); if (is_wp_error($get) or 200 != $get['response']['code']) { $this->error = Prompt_Logging::add_error('prompt_jetpack_import_http', __('A request to the Jetpack servers failed.', 'Postmatic'), compact('get')); return; } $dom = new DOMDocument(); $dom->loadHTML($get['body']); $xml = simplexml_import_dom($dom); if (is_null($this->page_count)) { $this->set_counts($xml); } $rows = $xml->xpath("//table/tbody/tr"); foreach ($rows as $row) { $this->subscribers[] = array('email_address' => $row->td[1], 'subscribe_date' => $row->td[3]->span['title']); } }
/** * Submit emails to Postmatic for tracking addresses. * * @return object|WP_Error results */ protected function request_tracking_addresses() { $email_data = new stdClass(); $email_data->actions = array('track-replies'); $email_data->outboundMessages = array(); $message_values = $this->batch->get_individual_message_values(); if (empty($message_values)) { return $email_data; } foreach ($message_values as $value_set) { $email_data->outboundMessages[] = $this->make_prompt_message($value_set); } $response = $this->client->post_outbound_messages($email_data); if ($this->reschedule($response)) { return $response; } $error = $this->translate_error($response); if ($error) { return $error; } $results = json_decode($response['body']); if (!isset($results->outboundMessages) or count($results->outboundMessages) != count($this->batch->get_individual_message_values())) { return Prompt_Logging::add_error('invalid_outbound_results', __('An email sending operation behaved erratically and may have failed.', 'Postmatic'), compact('email_data', 'results')); } return $results; }
/** * @since 1.0.0 * @return bool */ protected function validate() { if (!is_array($this->keys) or count($this->keys) != 1) { Prompt_Logging::add_error('register_subscribe_keys_invalid', __('Received invalid metadata with a subscription agreement.', 'Postmatic'), array('keys' => $this->keys, 'message' => $this->message)); return false; } $int_keys = array_filter($this->keys, 'is_int'); if ($int_keys != $this->keys) { Prompt_Logging::add_error('register_subscribe_keys_invalid', __('Received invalid metadata with a subscription agreement.', 'Postmatic'), array('keys' => $this->keys, 'message' => $this->message)); return false; } if (empty($this->message)) { Prompt_Logging::add_error('register_subscribe_message_invalid', __('Received no message with a subscription agreement.', 'Postmatic'), array('keys' => $this->keys, 'message' => $this->message)); return false; } return true; }