File indexing completed on 2025-05-04 05:29:11
0001 <?php 0002 0003 /** 0004 * ocs-webserver 0005 * 0006 * Copyright 2016 by pling GmbH. 0007 * 0008 * This file is part of ocs-webserver. 0009 * 0010 * This program is free software: you can redistribute it and/or modify 0011 * it under the terms of the GNU Affero General Public License as 0012 * published by the Free Software Foundation, either version 3 of the 0013 * License, or (at your option) any later version. 0014 * 0015 * This program is distributed in the hope that it will be useful, 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0018 * GNU Affero General Public License for more details. 0019 * 0020 * You should have received a copy of the GNU Affero General Public License 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0022 * 0023 * Created: 10.09.2018 0024 */ 0025 class Default_Model_Ocs_Mastodon 0026 { 0027 protected $config; 0028 protected $messages; 0029 protected $httpClient; 0030 protected $isRateLimitError; 0031 protected $rateLimitWaitSeconds; 0032 0033 /** 0034 * @inheritDoc 0035 * @param array|null $config 0036 * @throws Default_Model_Ocs_Gitlab_Exception 0037 * @throws Zend_Exception 0038 */ 0039 public function __construct($config = null) 0040 { 0041 if (isset($config)) { 0042 $this->config = $config; 0043 } else { 0044 $this->config = Zend_Registry::get('config')->settings->server->mastodon; 0045 } 0046 $uri = $this->config->host; 0047 $this->httpClient = $this->getHttpClient($uri); 0048 } 0049 0050 /** 0051 * @param $uri 0052 * @return Zend_Http_Client 0053 * @throws Default_Model_Ocs_Gitlab_Exception 0054 */ 0055 protected function getHttpClient($uri) 0056 { 0057 try { 0058 if (empty($uri)) { 0059 return new Zend_Http_Client(null, array('keepalive' => true, 'strictredirects' => true)); 0060 } 0061 0062 return new Zend_Http_Client($uri, array('keepalive' => true, 'strictredirects' => true)); 0063 } catch (Zend_Exception $e) { 0064 throw new Default_Model_Ocs_Gitlab_Exception('Can not create http client for uri: ' . $uri, 0, $e); 0065 } 0066 } 0067 0068 0069 /** 0070 * @param string $uri 0071 * @param string $uid 0072 * @param string $method 0073 * @param array|null $post_param 0074 * 0075 * @return bool|array 0076 * @throws Zend_Exception 0077 */ 0078 protected function httpRequest($uri, $uid, $method = Zend_Http_Client::GET, $post_param = null, $isAdmin=false) 0079 { 0080 $this->isRateLimitError = false; 0081 $this->rateLimitWaitSeconds = 0; 0082 0083 $this->httpClient->resetParameters(); 0084 try { 0085 $this->httpClient->setUri($uri); 0086 $this->httpClient->setHeaders('User-Agent', $this->config->user_agent); 0087 if($isAdmin) 0088 { 0089 $this->httpClient->setHeaders('Authorization','Bearer '.$this->config->private_token); 0090 } 0091 $this->httpClient->setMethod($method); 0092 0093 0094 $this->httpClient->setHeaders('Content-Type', 'application/json'); 0095 $this->httpClient->setHeaders('Accept', 'application/json'); 0096 0097 } catch (Zend_Http_Client_Exception $e) { 0098 $this->messages[] = 'Request failed.(' . $uri . ') httpClient error message: ' . $e->getMessage(); 0099 0100 return false; 0101 } catch (Zend_Uri_Exception $e) { 0102 $this->messages[] = 'Request failed.(' . $uri . ') httpClient error message: ' . $e->getMessage(); 0103 0104 return false; 0105 } 0106 if (isset($post_param)) { 0107 $this->httpClient->setParameterPost($post_param); 0108 } 0109 0110 try { 0111 $response = $this->httpClient->request(); 0112 0113 0114 } catch (Zend_Http_Client_Exception $e) { 0115 $this->messages[] = 'Request failed.(' . $uri . ') httpClient error message: ' . $e->getMessage(); 0116 0117 return false; 0118 } 0119 if ($response->getStatus() < 200 OR $response->getStatus() >= 500) { 0120 $this->messages[] = 'Request failed.(' . $uri . ') OCS Forum server send message: ' . $response->getBody(); 0121 0122 return false; 0123 } 0124 0125 $body = $response->getRawBody(); 0126 $content_encoding = $response->getHeader('Content-encoding'); 0127 $transfer_encoding = $response->getHeader('Transfer-encoding'); 0128 if ($transfer_encoding == 'chunked') { 0129 $body = Zend_Http_Response::decodeChunkedBody($body); 0130 } 0131 if ($content_encoding == 'gzip') { 0132 $body = Zend_Http_Response::decodeGzip($body); 0133 } 0134 if (substr($body, 0, strlen('<html>')) === '<html>') { 0135 $this->messages[] = $body; 0136 0137 return false; 0138 } 0139 try { 0140 $body = Zend_Json::decode($body); 0141 } catch (Zend_Json_Exception $e) { 0142 $this->messages[] = 'Request failed.(' . $uri . ') Zend_Json::decode error message: ' . $e->getMessage(); 0143 0144 return false; 0145 } 0146 0147 if (empty($body)) { 0148 return array('message' => 'empty body received'); 0149 } 0150 0151 if (array_key_exists("error_type", $body) OR array_key_exists("errors", $body)) { 0152 $this->messages[] = "id: {$uid} ($uri) - " . $response->getBody(); 0153 0154 if (isset($body['error_type']) AND ($body['error_type'] == "rate_limit")) { 0155 0156 $this->isRateLimitError = true; 0157 $this->rateLimitWaitSeconds = $body['extras']['wait_seconds']; 0158 0159 throw new Zend_Exception($body['errors'][0]); 0160 } 0161 0162 return false; 0163 } 0164 0165 if (array_key_exists('success', $body) AND $body['success'] == false) { 0166 $this->messages[] = "id: {$uid} ($uri) - " . $body['message']; 0167 0168 return false; 0169 } 0170 0171 return $body; 0172 } 0173 0174 0175 0176 /** 0177 * @return mixed 0178 */ 0179 public function hasRateLimitError() 0180 { 0181 return $this->isRateLimitError; 0182 } 0183 0184 /** 0185 * @return mixed 0186 */ 0187 public function getRateLimitWaitSeconds() 0188 { 0189 return $this->rateLimitWaitSeconds; 0190 } 0191 0192 public function getTimelines() 0193 { 0194 $uri = $this->config->host . "/api/v1/timelines/public?limit=5"; 0195 $method = Zend_Http_Client::GET; 0196 $uid = 'getTimelines'; 0197 $timelines = null; 0198 try { 0199 $timelines = $this->httpRequest($uri, $uid, $method); 0200 if (false === $timelines) { 0201 $this->messages[] = "Fail "; 0202 0203 return false; 0204 } 0205 } catch (Zend_Exception $e) { 0206 $this->messages[] = "Fail " . $e->getMessage(); 0207 0208 return false; 0209 } 0210 return $timelines; 0211 } 0212 0213 public function getUserByUsername($username) 0214 { 0215 $uri = $this->config->host . "/api/v1/admin/accounts?username=".$username; 0216 $method = Zend_Http_Client::GET; 0217 $uid = 'getUserByUsername'; 0218 $user = null; 0219 0220 try { 0221 $user = $this->httpRequest($uri, $uid, $method,null,true); 0222 0223 if (false === $user) { 0224 $this->messages[] = "Fail "; 0225 0226 return false; 0227 } 0228 } catch (Zend_Exception $e) { 0229 $this->messages[] = "Fail " . $e->getMessage(); 0230 0231 return false; 0232 } 0233 return $user; 0234 } 0235 0236 public function getUserStatuses($id) 0237 { 0238 $uri = $this->config->host . "/api/v1/accounts/".$id."/statuses"; 0239 $method = Zend_Http_Client::GET; 0240 $uid = 'getUserAccount'; 0241 $user = null; 0242 0243 try { 0244 $user = $this->httpRequest($uri, $uid, $method); 0245 0246 if (false === $user) { 0247 $this->messages[] = "Fail "; 0248 0249 return false; 0250 } 0251 } catch (Zend_Exception $e) { 0252 $this->messages[] = "Fail " . $e->getMessage(); 0253 0254 return false; 0255 } 0256 return $user; 0257 } 0258 0259 0260 }