/**
  * Set up and return an API connection using the currently saved options..
  *
  * @return WP2D_API The API object.
  */
 public static function api_quick_connect()
 {
     $options = WP2D_Options::instance();
     $pod = (string) $options->get_option('pod');
     $is_secure = true;
     $username = (string) $options->get_option('username');
     $password = WP2D_Helpers::decrypt((string) $options->get_option('password'));
     $api = new WP2D_API($pod, $is_secure);
     // This is necessary for correct error handline!
     if ($api->init()) {
         $api->login($username, $password);
     }
     return $api;
 }
 /**
  * Post to diaspora* when saving a post.
  *
  * @since 1.5.0
  *
  * @todo Maybe somebody wants to share a password protected post to a closed aspect.
  *
  * @param integer $post_id ID of the post being saved.
  * @param WP_Post $post    Post object being saved.
  * @return boolean If the post was posted successfully.
  */
 public function post($post_id, $post)
 {
     $this->_assign_wp_post($post);
     $options = WP2D_Options::instance();
     // Is this post type enabled for posting?
     if (!in_array($post->post_type, $options->get_option('enabled_post_types'))) {
         return false;
     }
     // Make sure we're posting to diaspora* and the post isn't password protected.
     if (!($this->post_to_diaspora && 'publish' === $post->post_status && '' === $post->post_password)) {
         return false;
     }
     $status_message = $this->_get_title_link();
     // Post the full post text or just the excerpt?
     if ('full' === $this->display) {
         $status_message .= $this->_get_full_content();
     } else {
         $status_message .= $this->_get_excerpt_content();
     }
     // Add the tags assigned to the post.
     $status_message .= $this->_get_tags_to_add();
     // Add the original entry link to the post?
     $status_message .= $this->_get_posted_at_link();
     $status_converter = new HtmlConverter(array('strip_tags' => true));
     $status_message = $status_converter->convert($status_message);
     // Set up the connection to diaspora*.
     $conn = WP2D_Helpers::api_quick_connect();
     if (!empty($status_message)) {
         if ($conn->last_error) {
             // Save the post error as post meta data, so we can display it to the user.
             update_post_meta($post_id, '_wp_to_diaspora_post_error', $conn->last_error);
             return false;
         }
         // Add services to share to via diaspora*.
         $extra_data = array('services' => $this->services);
         // Try to post to diaspora*.
         if ($response = $conn->post($status_message, $this->aspects, $extra_data)) {
             // Save certain diaspora* post data as meta data for future reference.
             $this->_save_to_history((object) $response);
             // If there is still a previous post error around, remove it.
             delete_post_meta($post_id, '_wp_to_diaspora_post_error');
         }
     } else {
         return false;
     }
 }
 /**
  * Send an http(s) request via cURL.
  *
  * @param string       $url     The URL to request.
  * @param array|string $data    Data to be posted with the request.
  * @param array        $headers Headers to assign to the request.
  * @return object An object containing details about this request.
  */
 private function _http_request($url, $data = array(), $headers = array())
 {
     // Prefix the full pod URL if necessary.
     if (0 === strpos($url, '/')) {
         $url = $this->get_pod_url($url);
     }
     // Call address via cURL.
     $ch = curl_init($url);
     // Set up cURL options.
     curl_setopt($ch, CURLOPT_HEADER, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     if (file_exists(WP2D_DIR . '/cacert.pem')) {
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
         curl_setopt($ch, CURLOPT_CAINFO, WP2D_DIR . '/cacert.pem');
     }
     if (!empty($this->_cookie)) {
         curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);
     }
     // Add the passed headers.
     if (!empty($headers)) {
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     }
     // Add the passed post data.
     if (!empty($data)) {
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
     }
     // Get the response from the cURL call.
     $response = curl_exec($ch);
     // Get the headers and the html response.
     $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
     $headers = substr($response, 0, $header_size);
     $response = substr($response, $header_size);
     // Remember this request.
     $this->_last_request = new stdClass();
     $this->_last_request->headers = $headers;
     $this->_last_request->response = $response;
     $this->_last_request->error = curl_error($ch);
     $this->_last_request->errno = curl_errno($ch);
     $this->_last_request->info = curl_getinfo($ch);
     curl_close($ch);
     // Save the new token.
     if ($token = $this->_parse_regex('token', $response)) {
         $this->_token = $token;
     }
     // Save the latest cookie.
     if ($cookie = $this->_parse_regex('cookie', $headers)) {
         $this->_cookie = $cookie;
     }
     // Once we're logged in, can we load new aspects and services while we're at it?
     if ($this->is_logged_in()) {
         // Load the aspects.
         if (empty($this->_aspects) && ($aspects_raw = json_decode($this->_parse_regex('aspects', $response)))) {
             // Add the 'public' aspect, as it's global and not user specific.
             $aspects = array('public' => __('Public'));
             // Create an array of all the aspects and save them to the settings.
             foreach ($aspects_raw as $aspect) {
                 $aspects[$aspect->id] = $aspect->name;
             }
             $this->_aspects = $aspects;
         }
         // Load the services.
         if (empty($this->_services) && ($services_raw = json_decode($this->_parse_regex('services', $response)))) {
             $services = array();
             foreach ($services_raw as $service) {
                 $services[$service] = ucfirst($service);
             }
             $this->_services = $services;
         }
     }
     // Add debug info.
     WP2D_Helpers::add_debugging(sprintf("code %s on %s\n", $this->_last_request->info['http_code'], $this->_last_request->info['url']));
     // Return the last request details.
     return $this->_last_request;
 }
 /**
  * Clean up the passed tags. Keep only alphanumeric, hyphen and underscore characters.
  *
  * @param array|string $tags Tags to be cleaned as array or comma seperated values.
  * @return array The cleaned tags.
  */
 public function validate_tags(&$tags)
 {
     WP2D_Helpers::str_to_arr($tags);
     $tags = array_map(array($this, 'validate_tag'), array_unique(array_filter($tags, 'trim')));
     return $tags;
 }
 /**
  * Check the connection to the pod and return the status for use with AJAX.
  */
 public function check_pod_connection_status_callback()
 {
     if (isset($_REQUEST['debugging']) && !defined('WP2D_DEBUGGING')) {
         define('WP2D_DEBUGGING', true);
     }
     $status = $this->_check_pod_connection_status();
     $data = array('debug' => esc_textarea(WP2D_Helpers::get_debugging()), 'message' => __('Connection successful.', 'wp-to-diaspora'));
     if (true === $status) {
         wp_send_json_success($data);
     } elseif (false === $status) {
         $data['message'] = $this->_load_api()->last_error;
         wp_send_json_error($data);
     }
     // If $status === null, do nothing.
 }