Пример #1
0
 /**
  * @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;
 }
Пример #2
0
 /**
  * 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';
 }
Пример #3
0
 /**
  *
  * @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);
 }
Пример #4
0
 /**
  *
  * @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;
 }
Пример #5
0
 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'));
 }
Пример #6
0
 /**
  * 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);
 }
Пример #7
0
 /**
  * Make a reschedulable request to acknowledge updates.
  *
  * @since 1.3.0
  *
  * @param array $updates Array as returned by pull_updates
  * @param int $retry_wait_seconds Optional. One second default as this is a time-sensitive request.
  * @return bool|WP_Error status
  */
 public static function acknowledge_updates($updates, $retry_wait_seconds = 1)
 {
     $messenger = Prompt_Factory::make_inbound_messenger();
     $result = $messenger->acknowledge_updates($updates);
     $rescheduler = new Prompt_Rescheduler($result, $retry_wait_seconds);
     if ($rescheduler->found_temporary_error()) {
         $rescheduler->reschedule('prompt/inbound_handling/acknowledge_updates', array($updates));
         return $result;
     }
     if (is_wp_error($result)) {
         Prompt_Logging::add_wp_error($result);
     }
     return $result;
 }
Пример #8
0
 /**
  * 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;
 }
Пример #9
0
 /**
  * Reschedule a job on an exponential decay timing scheme that increases wait times by a factor of 4.
  *
  * @since 1.3.0
  *
  * @param string $hook The hook to run
  * @param array $args The arguments minus retry_wait_seconds, which will be calculated as the last argument.
  */
 public function reschedule($hook, $args = array())
 {
     if (is_null($this->wait_seconds)) {
         Prompt_Logging::add_wp_error($this->job_result);
         return;
     }
     // Add a random factor to spread things out a little
     $retry_time = time() + $this->wait_seconds + rand(0, $this->wait_seconds / 50);
     $next_wait = $this->wait_seconds * 4;
     $one_day_in_seconds = 60 * 60 * 24;
     if ($next_wait > $one_day_in_seconds) {
         $next_wait = null;
     }
     $args[] = $next_wait;
     wp_schedule_single_event($retry_time, $hook, $args);
 }
 /**
  * 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;
 }
Пример #12
0
 /**
  * @since 1.0.0
  */
 protected function submit_errors()
 {
     Prompt_Logging::submit();
     add_action('admin_notices', array($this, 'submitted_errors_admin_msg'));
 }
Пример #13
0
 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']);
     }
 }
Пример #14
0
 /**
  * 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;
 }