]> git.mxchange.org Git - friendica-addons.git/blob - statusnet/library/statusnetoauth.php
SV addon translation update THX Kristoffer Grundström
[friendica-addons.git] / statusnet / library / statusnetoauth.php
1 <?php
2
3 use Friendica\DI;
4
5 require_once __DIR__ . DIRECTORY_SEPARATOR . 'twitteroauth.php';
6
7 /*
8  * We have to alter the TwitterOAuth class a little bit to work with any GNU Social
9  * installation abroad. Basically it's only make the API path variable and be happy.
10  *
11  * Thank you guys for the Twitter compatible API!
12  */
13 class StatusNetOAuth extends TwitterOAuth
14 {
15         function get_maxlength()
16         {
17                 $config = $this->get($this->host . 'statusnet/config.json');
18                 if (empty($config)) {
19                         return 0;
20                 }
21                 return $config->site->textlimit;
22         }
23
24         function accessTokenURL()
25         {
26                 return $this->host . 'oauth/access_token';
27         }
28
29         function authenticateURL()
30         {
31                 return $this->host . 'oauth/authenticate';
32         }
33
34         function authorizeURL()
35         {
36                 return $this->host . 'oauth/authorize';
37         }
38
39         function requestTokenURL()
40         {
41                 return $this->host . 'oauth/request_token';
42         }
43
44         function __construct($apipath, $consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL)
45         {
46                 parent::__construct($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
47                 $this->host = $apipath;
48         }
49
50         /**
51          * Make an HTTP request
52          *
53          * Copied here from the TwitterOAuth library and complemented by applying the proxy settings of Friendica
54          *
55          * @param string $method
56          * @param string $host
57          * @param string $path
58          * @param array  $parameters
59          *
60          * @return array|object API results
61          */
62         function http($url, $method, $postfields = NULL)
63         {
64                 $this->http_info = [];
65                 $ci = curl_init();
66                 /* Curl settings */
67                 $prx = DI::config()->get('system', 'proxy');
68                 if (strlen($prx)) {
69                         curl_setopt($ci, CURLOPT_HTTPPROXYTUNNEL, 1);
70                         curl_setopt($ci, CURLOPT_PROXY, $prx);
71                         $prxusr = DI::config()->get('system', 'proxyuser');
72                         if (strlen($prxusr)) {
73                                 curl_setopt($ci, CURLOPT_PROXYUSERPWD, $prxusr);
74                         }
75                 }
76                 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
77                 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
78                 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
79                 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
80                 curl_setopt($ci, CURLOPT_HTTPHEADER, ['Expect:']);
81                 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
82                 curl_setopt($ci, CURLOPT_HEADERFUNCTION, [$this, 'getHeader']);
83                 curl_setopt($ci, CURLOPT_HEADER, FALSE);
84
85                 switch ($method) {
86                         case 'POST':
87                                 curl_setopt($ci, CURLOPT_POST, TRUE);
88                                 if (!empty($postfields)) {
89                                         curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
90                                 }
91                                 break;
92                         case 'DELETE':
93                                 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
94                                 if (!empty($postfields)) {
95                                         $url = "{$url}?{$postfields}";
96                                 }
97                 }
98
99                 curl_setopt($ci, CURLOPT_URL, $url);
100                 $response = curl_exec($ci);
101                 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
102                 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
103                 $this->url = $url;
104                 curl_close($ci);
105                 return $response;
106         }
107 }