Commit 69f25966 authored by Thomas's avatar Thomas
Browse files

Remove curl and use wordpress http api

parent 063d994f
......@@ -30,28 +30,25 @@ class Mastodon_api {
/**
* _post
*
* curl post
* HTTP API post
*
* @param string $url
* @param array $data
* @param array $parameters
*
* @return array $response
*/
private function _post ($url,$data = array()) {
$parameters = array();
$parameters[CURLOPT_POST] = 1;
private function _post ($url, $parameters = array()) {
$params["method"] = "POST";
// set access_token
if (isset($this->token['access_token'])) {
$data['access_token'] = $this->token['access_token'];
}
if (count($data)) {
$parameters[CURLOPT_POSTFIELDS] = preg_replace('/([(%5B)]{1})[0-9]+([(%5D)]{1})/','$1$2',http_build_query($data));
}
if (isset($this->token['access_token'])) {
$parameters['headers'] = array(
'Authorization' => $this->token['token_type'] . ' ' . $this->token['access_token']
);
}
$params['body'] = $parameters;
$url = $this->mastodon_url.$url;
$response = $this->get_content_curl($url,$parameters);
$response = $this->get_content_remote_get($url,$params);
return $response;
}
......@@ -59,25 +56,23 @@ class Mastodon_api {
* _get
*
* @param string $url
* @param array $data
* @param array $parameters
*
* @return array $response
*/
private function _get ($url,$data = array()) {
$parameters = array();
private function _get ($url, $parameters = array()) {
$params["method"] = "POST";
// set authorization bearer
if (isset($this->token['access_token'])) {
$authorization = 'Authorization: '.$this->token['token_type'].' '.$this->token['access_token'];
$parameters[CURLOPT_HTTPHEADER] = array('Content-Type: application/json',$authorization);
}
if (isset($this->token['access_token'])) {
$parameters['headers'] = array(
'Authorization' => $this->token['token_type'] . ' ' . $this->token['access_token']
);
}
$url = $this->mastodon_url.$url;
if (count($data)) {
$url .= '?' . http_build_query($data);
}
$response = $this->get_content_curl($url,$parameters);
$response = $this->get_content_remote_get($url,$parameters);
return $response;
}
......@@ -91,20 +86,21 @@ class Mastodon_api {
*/
private function _patch ($url,$data = array()) {
$parameters = array();
$parameters[CURLOPT_CUSTOMREQUEST] = 'PATCH';
$parameters["PATCH"] = 1;
// set authorization bearer
if (isset($this->token['access_token'])) {
$authorization = 'Authorization: '.$this->token['token_type'].' '.$this->token['access_token'];
$parameters[CURLOPT_HTTPHEADER] = array('Content-Type: application/json',$authorization);
$parameters['headers'] = array(
'Authorization' => $this->token['token_type'] . ' ' . $this->token['access_token']
);
}
if (count($data)) {
$parameters[CURLOPT_POSTFIELDS] = json_encode($data);
$parameters["POST_FIELDS"] = json_encode($data);
}
$url = $this->mastodon_url.$url;
$response = $this->get_content_curl($url,$parameters);
$response = $this->get_content_remote_get($url,$parameters);
return $response;
}
......@@ -117,88 +113,43 @@ class Mastodon_api {
*/
private function _delete ($url) {
$parameters = array();
$parameters[CURLOPT_CUSTOMREQUEST] = 'DELETE';
$parameters["DELETE"] = 1;
// set authorization bearer
if (isset($this->token['access_token'])) {
$authorization = 'Authorization: '.$this->token['token_type'].' '.$this->token['access_token'];
$parameters[CURLOPT_HTTPHEADER] = array('Content-Type: application/json',$authorization);
$parameters["HTTP_HEADER"] = array('Content-Type: application/json',$authorization);
}
$url = $this->mastodon_url.$url;
$response = $this->get_content_curl($url,$parameters);
$response = $this->get_content_remote_get($url,$parameters);
return $response;
}
/**
* get_content_curl
* get_content_remote_get
*
* @param string $url
* @param array $parameters
*
* @return array $data
*/
public function get_content_curl ($url,$parameters = array()) {
public function get_content_remote_get ($url,$parameters = array()) {
$data = array();
// set CURLOPT_USERAGENT
if (!isset($parameters[CURLOPT_USERAGENT])) {
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$parameters[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT'];
} else {
// default IE11
$parameters[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko';
}
}
// check curl_init
if (function_exists('curl_init')) {
$ch = curl_init();
// url 설정
curl_setopt($ch,CURLOPT_URL,$url);
foreach ($parameters as $key => $value) {
curl_setopt($ch,$key,$value);
}
// https
if (!isset($parameters[CURLOPT_SSL_VERIFYPEER])) {
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
}
if (!isset($parameters[CURLOPT_SSLVERSION])) {
curl_setopt($ch,CURLOPT_SSLVERSION,6);
}
// no header
if (!isset($parameters[CURLOPT_HEADER])) {
curl_setopt($ch,CURLOPT_HEADER,0);
}
// POST / GET (default : GET)
if (!isset($parameters[CURLOPT_POST]) && !isset($parameters[CURLOPT_CUSTOMREQUEST])) {
curl_setopt($ch,CURLOPT_POST,0);
}
// response get php value
if (!isset($parameters[CURLOPT_RETURNTRANSFER])) {
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
}
// HTTP2
if (!isset($parameters[CURLOPT_HTTP_VERSION])) {
curl_setopt($ch,CURLOPT_HTTP_VERSION,3);
}
if (!isset($parameters[CURLINFO_HEADER_OUT])) {
curl_setopt($ch,CURLINFO_HEADER_OUT,TRUE);
}
$data['html'] = json_decode(curl_exec($ch),true);
$data['response'] = curl_getinfo($ch);
curl_close($ch);
}
// set USERAGENT
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$parameters['headers'][] = $_SERVER['HTTP_USER_AGENT'];
} else {
// default IE11
$parameters['headers'][] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko';
}
if( isset($parameters["method"]) && $parameters['method'] == "POST" ) {
$response = wp_remote_post( $url, $parameters );
}else
$response = wp_remote_get( $url, $parameters );
$data['html'] = json_decode($response['body'], true);
$data['response'] = json_decode($response['body'], true);
return $data;
}
......@@ -709,42 +660,7 @@ class Mastodon_api {
return $response;
}
/**
* media
*
* Uploading a media attachment
*
* @param string $file_path local path / http path
*
* @return array $response
* int $response['id'] ID of the attachment
* string $response['type'] One of: "image", "video", "gifv"
* string $response['url'] URL of the locally hosted version of the image
* string $response['remote_url'] For remote images, the remote URL of the original image
* string $response['preview_url'] URL of the preview image
* string $response['text_url'] Shorter URL for the image, for insertion into text (only present on local images)
*/
public function media ($file_path) {
$url = $this->mastodon_url.'/api/v1/media';
$parameters = $data = array();
$parameters[CURLOPT_HTTPHEADER] = array('Content-Type'=>'multipart/form-data');
$parameters[CURLOPT_POST] = true;
// set access_token
if (isset($this->token['access_token'])) {
$parameters[CURLOPT_POSTFIELDS]['access_token'] = $this->token['access_token'];
}
if (is_file($file_path)) {
$mime_type = mime_content_type($file_path);
$cf = curl_file_create($file_path,$mime_type,'file');
$parameters[CURLOPT_POSTFIELDS]['file'] = $cf;
}
$response = $this->get_content_curl($url,$parameters);
return $response;
}
/**
* mutes
......
......@@ -127,7 +127,7 @@ class Mastodon_comments_Admin {
function mw_enqueue_color_picker( $hook_suffix ) {
// first check that $hook_suffix is appropriate for your admin page
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'my-script-handle', plugins_url('my-script.js', __FILE__ ), array( 'wp-color-picker' ), false, true );
wp_enqueue_script( 'my-script-handle', plugins_url('js/mastodon_comments-admin.js', __FILE__ ), array( 'wp-color-picker' ), false, true );
}
}
......@@ -61,6 +61,7 @@ if( ! isset($_SESSION['mc_step']))
$currentUser = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix . "mastodon_comment_users ORDER BY date DESC LIMIT 1", OBJECT );
//Check if there is a valid account in DB
if( !empty($currentUser) && !empty($currentUser[0]->mastodon_instance) && strlen(trim($currentUser[0]->mastodon_instance)) > 0 )
{
......@@ -123,14 +124,14 @@ if ( isset($options['oauth']) ){
$Mastodon_api->set_client($_SESSION['client_id'], $_SESSION['client_secret']);
$data = $Mastodon_api->loginAuthorization($options['oauth']);
if (! isset($data['html']['access_token'])) {
if ( isset($data['html']['error'])) {
$_SESSION['mc_step'] = 1;
unset($_SESSION['instance_name']);
unset($_SESSION['client_secret']);
unset($_SESSION['client_id']);
delete_option( $this->plugin_name."settings");
if(isset($data['html']['error_description']))
$error_message = $data['html']['error_description'];
if(isset($data['html']['error']))
$error_message = $data['html']['error'];
else
$error_message = esc_html__('An error occured!', $this->plugin_name);
}else{
......
......@@ -144,7 +144,7 @@ class Mastodon_comments_Public {
$needRefresh = true;
}
if ($needRefresh) {
$searchResult = $MastodonAPI->get_content_curl( $search );
$searchResult = $MastodonAPI->get_content_remote_get( $search );
$dateInsert = new DateTime();
//Cache the status
if( empty($cachedURL)) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment