fediembedi-client.php 5.2 KB
Newer Older
MediaFormat's avatar
MediaFormat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
<?php

class Client
{
	private $instance_url;
	private $access_token;
	private $app;
	private static $acct_id;

	public function __construct($instance_url, $access_token = '') {
		$this->instance_url = $instance_url;
		$this->access_token = $access_token;
	}

	public function setStatic($param){
			 self::$acct_id = $param;
	 }

	 public function getStatic(){
       return self::$acct_id;
   }

	public function register_app($redirect_uri) {

		$response = $this->_post('/api/v1/apps', array(
			'client_name' => 'FediEmbedi for WordPress',
			'redirect_uris' => $redirect_uri,
			'scopes' => 'read',
			'website' => get_site_url()
		));

		if (!isset($response->client_id)){
			return "ERROR";
		}

		$this->app = $response;

		$params = http_build_query(array(
			'response_type' => 'code',
			'redirect_uri' => $redirect_uri,
			'scope' => 'read',
			'client_id' =>$this->app->client_id
		));

		return $this->instance_url.'/oauth/authorize?'.$params;
	}

	public function verify_credentials($access_token){

		$headers = array(
			'Authorization'=>'Bearer '.$access_token
		);

		$response = $this->_get('/api/v1/accounts/verify_credentials', null, $headers);

		if(property_exists($response, 'id')){
				$this->setStatic($response->id);
		}

		return $response;
	}

	public function get_bearer_token($client_id, $client_secret, $code, $redirect_uri) {

		$response = $this->_post('/oauth/token',array(
			'grant_type' => 'authorization_code',
			'redirect_uri' => $redirect_uri,
			'client_id' => $client_id,
			'client_secret' => $client_secret,
			'code' => $code
		));

		return $response;
	}

	public function get_client_id() {
		return $this->app->client_id;
	}

	public function get_client_secret() {
		return $this->app->client_secret;
	}

	public function getStatus($media = 'false', $pinned = 'false', $replies = 'false', $max_id = null, $since_id = null, $min_id = null, $limit = 10, $reblogs = 'false') {

		$headers = array(
			'Authorization'=> 'Bearer '.$this->access_token
		);

		$account_id = self::$acct_id;

		$query = http_build_query(array(
			'only_media' => $media,
			'pinned' => $pinned,
			'exclude_replies' => $replies,
			'max_id' => $max_id,
			'since_id' => $since_id,
			'min_id' => $min_id,
			'limit' => $limit,
			'exclude_reblogs' => $reblogs
		));

		$response = $this->_get("/api/v1/accounts/{$account_id}/statuses?{$query}", null, $headers);

		return $response;
	}

	public function getAccount() {

		$headers = array(
			'Authorization'=> 'Bearer '.$this->access_token
		);

		$account_id = self::$acct_id;

		$response = $this->_get("/api/v1/accounts/{$account_id}", null, $headers);

		return $response;
	}

	public function getInstance() {

		$headers = array(
			'Authorization'=> 'Bearer '.$this->access_token
		);

		$account_id = self::$acct_id;

		$response = $this->_get("/api/v1/instance", null, $headers);

		return $response;
	}

	public function postStatus($status, $mode, $media = '', $spoiler_text = '') {

		$headers = array(
			'Authorization'=> 'Bearer '.$this->access_token
		);

		$response = $this->_post('/api/v1/statuses', array(
			'status' => $status,
			'visibility' => $mode,
			'spoiler_text' => $spoiler_text,
			'media_ids[]' => $media
		), $headers);

		return $response;
	}

	public function create_attachment($media_path) {

		$filename =basename($media_path);
		$mime_type = mime_content_type($media_path);

		$boundary ='hlx'.time();

		$headers = array (
			'Authorization'=> 'Bearer '.$this->access_token,
			'Content-Type' => 'multipart/form-data; boundary='. $boundary,
		);

		$nl = "\r\n";

		$data = '--'.$boundary.$nl;
		$data .= 'Content-Disposition: form-data; name="file"; filename="'.$filename.'"'.$nl;
		$data .= 'Content-Type: '. $mime_type .$nl.$nl;
		$data .= file_get_contents($media_path) .$nl;
		$data .= '--'.$boundary.'--';

		$response = $this->_post('/api/v1/media', $data, $headers);

		return $response;
	}

	private function _post($url, $data = array(), $headers = array()) {
		return $this->post($this->instance_url.$url, $data, $headers);
	}

	public function _get($url, $data = array(), $headers = array()) {
		return $this->get($this->instance_url.$url, $data, $headers);
	}

	private function post($url, $data = array(), $headers = array()) {
		$args = array(
		    'headers' => $headers,
		    'body'=> $data,
		    'redirection' => 5
		);

		$response = wp_remote_post( $this->getValidURL($url), $args );
		if ( is_wp_error( $response ) ) {
		    $error_message = $response->get_error_message();

		} else {
		$responseBody = wp_remote_retrieve_body($response);
		return json_decode($responseBody);
	}

		return $response;
	}

	public function get($url, $data = array(), $headers = array()) {
		$args = array(
		    'headers' => $headers,
		    'redirection' => 5
		);
		$response = wp_remote_get( $this->getValidURL($url), $args );
		if ( is_wp_error( $response ) ) {
		    $error_message = $response->get_error_message();

		} else {
		$responseBody = wp_remote_retrieve_body($response);
		    return json_decode($responseBody);
		}

		return $response;
	}

	public function dump($value){
		echo '<pre>';
		print_r($value);
		echo '</pre>';
	}

	private function getValidURL($url){
		 if  ( $ret = parse_url($url) ) {
 			if ( !isset($ret["scheme"]) ){
				$url = "http://{$url}";
			}
		}
		return $url;

	}
}