Completed
Pull Request — master (#28)
by
unknown
02:57
created
php/Requests/AbstractRequest.php 1 patch
Indentation   +186 added lines, -186 removed lines patch added patch discarded remove patch
@@ -2,21 +2,21 @@  discard block
 block discarded – undo
2 2
 
3 3
 abstract class AbstractRequest
4 4
 {	
5
-    const CLIENTID = '81e8a76e-1e02-4d17-9ba0-8a7020261b26';
6
-    const APIURL = 'https://api.go-tellm.com/api';
7
-    const SECRET = 'hyTBJcvtpDLSgGUWjybbYUNKSSoVvMcfdjtjiQvf';
8
-    const USERAGENT = 'Jodel/4.47.0 Dalvik/2.1.0 (Linux; U; Android 5.1.1; )';
9
-    const CLIENT_TYPE = 'android_4.47.0';
5
+	const CLIENTID = '81e8a76e-1e02-4d17-9ba0-8a7020261b26';
6
+	const APIURL = 'https://api.go-tellm.com/api';
7
+	const SECRET = 'hyTBJcvtpDLSgGUWjybbYUNKSSoVvMcfdjtjiQvf';
8
+	const USERAGENT = 'Jodel/4.47.0 Dalvik/2.1.0 (Linux; U; Android 5.1.1; )';
9
+	const CLIENT_TYPE = 'android_4.47.0';
10 10
     
11
-    private $accessToken = null;
12
-    private $payLoad;
13
-    public $expects = '';
14
-    public $version = 'v2';
15
-    public $hasPayload = FALSE;
16
-
17
-    public function execute()
18
-    {
19
-        $result = new \stdClass();
11
+	private $accessToken = null;
12
+	private $payLoad;
13
+	public $expects = '';
14
+	public $version = 'v2';
15
+	public $hasPayload = FALSE;
16
+
17
+	public function execute()
18
+	{
19
+		$result = new \stdClass();
20 20
 		        
21 21
 		$this->payLoad = $this->getPayload();
22 22
 		$device_uid = '';
@@ -25,16 +25,16 @@  discard block
 block discarded – undo
25 25
 		}
26 26
 				
27 27
 				
28
-        $this->payLoad = json_encode($this->payLoad);
29
-        $header = $this->getSignHeaders();
30
-        $url = $this->getFullUrl();
28
+		$this->payLoad = json_encode($this->payLoad);
29
+		$header = $this->getSignHeaders();
30
+		$url = $this->getFullUrl();
31 31
 
32
-        if ($this->getAccessToken()) {
33
-            $header['Authorization'] = "Bearer " . $this->getAccessToken();
34
-        }
35
-        //Comment out to debug the Request:
32
+		if ($this->getAccessToken()) {
33
+			$header['Authorization'] = "Bearer " . $this->getAccessToken();
34
+		}
35
+		//Comment out to debug the Request:
36 36
 
37
-        /*
37
+		/*
38 38
         printf("URL: ");
39 39
         var_dump($url);
40 40
         echo "<br />";
@@ -45,134 +45,134 @@  discard block
 block discarded – undo
45 45
         var_dump($this->payLoad);
46 46
         echo "<br />";
47 47
         */
48
-        /*
48
+		/*
49 49
         $options = array(
50 50
             'timeout' => 100,
51 51
             'connect_timeout' => 100,
52 52
             'proxy' => '186.103.169.165:8080',
53 53
         );*/
54 54
 
55
-        switch ($this->getMethod()) {
56
-            case 'POST':
57
-                $result = Requests::post($url, $header, $this->payLoad);
58
-                break;
59
-            case 'GET':
60
-                if($this->hasPayload)
61
-                {
62
-                    $result = Requests::get($url, $header, $this->payLoad);
63
-                }
64
-                else
65
-                {
66
-                    $result = Requests::get($url, $header);
67
-                }
68
-                break;
69
-            case 'PUT':
70
-                $result = Requests::put($url, $header, $this->payLoad);
71
-                break;
72
-        }
73
-
74
-        http_response_code($result->status_code);
75
-
76
-        switch ($result->status_code) {
77
-            case 200:
78
-                $result = json_decode($result->body, true);
79
-                break;
80
-            case 204:
81
-                $result = 'Success';
82
-                http_response_code(200);
83
-                break;
84
-            case 400:
85
-                $result = json_decode($result->body, true);
86
-                error_log('Error 400 - ' . print_r($result, true));
87
-                break;
88
-            case 401:
89
-
90
-                if($result == "Unauthorized")
91
-                {
92
-                    error_log("Error 401: Unauthorized");
93
-                }
94
-                else
95
-                {
96
-                    $result = json_decode($result->body, true);
97
-
98
-                    if(is_array($result) && $result['error'] == 'length')
99
-                    {
100
-
101
-                    }
102
-                    else
103
-                    {
104
-                        error_log('Error 401 - ' . print_r($result, true));
105
-                    }
106
-                }
107
-
108
-
109
-                break;
110
-            case 404:
111
-
112
-
113
-                error_log('Error 404 - ' . print_r($result, true));
114
-                $result = json_decode($result->body, true);
115
-
116
-                if(array_key_exists('error', $result) && $result['error'] == 'post_blocked')
117
-                {
118
-                    header('HTTP/1.0 404 Not Found');
119
-                    include('error-pages/404.php');
120
-                    exit();
121
-                }
122
-
123
-                break;
55
+		switch ($this->getMethod()) {
56
+			case 'POST':
57
+				$result = Requests::post($url, $header, $this->payLoad);
58
+				break;
59
+			case 'GET':
60
+				if($this->hasPayload)
61
+				{
62
+					$result = Requests::get($url, $header, $this->payLoad);
63
+				}
64
+				else
65
+				{
66
+					$result = Requests::get($url, $header);
67
+				}
68
+				break;
69
+			case 'PUT':
70
+				$result = Requests::put($url, $header, $this->payLoad);
71
+				break;
72
+		}
73
+
74
+		http_response_code($result->status_code);
75
+
76
+		switch ($result->status_code) {
77
+			case 200:
78
+				$result = json_decode($result->body, true);
79
+				break;
80
+			case 204:
81
+				$result = 'Success';
82
+				http_response_code(200);
83
+				break;
84
+			case 400:
85
+				$result = json_decode($result->body, true);
86
+				error_log('Error 400 - ' . print_r($result, true));
87
+				break;
88
+			case 401:
89
+
90
+				if($result == "Unauthorized")
91
+				{
92
+					error_log("Error 401: Unauthorized");
93
+				}
94
+				else
95
+				{
96
+					$result = json_decode($result->body, true);
97
+
98
+					if(is_array($result) && $result['error'] == 'length')
99
+					{
100
+
101
+					}
102
+					else
103
+					{
104
+						error_log('Error 401 - ' . print_r($result, true));
105
+					}
106
+				}
107
+
108
+
109
+				break;
110
+			case 404:
111
+
112
+
113
+				error_log('Error 404 - ' . print_r($result, true));
114
+				$result = json_decode($result->body, true);
115
+
116
+				if(array_key_exists('error', $result) && $result['error'] == 'post_blocked')
117
+				{
118
+					header('HTTP/1.0 404 Not Found');
119
+					include('error-pages/404.php');
120
+					exit();
121
+				}
122
+
123
+				break;
124 124
 			case 477:
125
-                $result = json_decode($result->body, true);
126
-                error_log('Error 477 - ' . print_r($result, true));
127
-                break;
128
-            case 429:
129
-                error_log('Error 429 - Too Many Requests' . print_r(json_decode($result->body, true), true));
130
-            	exit("Error 429: Too Many Requests");
131
-            	break;
132
-            case 403:
133
-                error_log('Error 403 - Access denied:' . print_r(json_decode($result->body, true), true));
134
-                $result = json_decode($result->body, true);
135
-                break;
136
-            case 502:
137
-                error_log('Error 502 - ' . print_r($result, true));
138
-                $result = json_decode($result->body, true);
139
-                header('location:'.$_SERVER['PHP_SELF']);
140
-                break;
141
-            case 503:
142
-                error_log('Error 503 - ' . print_r($result, true));
143
-                $result = json_decode($result->body, true);
144
-
145
-                if(array_key_exists('error', $result) && $result['error'] == 'Service Unavailable')
146
-                {
147
-                    header('location:'.$_SERVER['PHP_SELF']); 
148
-                }
149
-                break;
150
-            default:
151
-                error_log('Error '.$result->status_code.' - unknown error');
152
-                $result = json_decode($result->body, true);
153
-        }
154
-
155
-        //important for account refresh
156
-        if($device_uid != '')
157
-        {
125
+				$result = json_decode($result->body, true);
126
+				error_log('Error 477 - ' . print_r($result, true));
127
+				break;
128
+			case 429:
129
+				error_log('Error 429 - Too Many Requests' . print_r(json_decode($result->body, true), true));
130
+				exit("Error 429: Too Many Requests");
131
+				break;
132
+			case 403:
133
+				error_log('Error 403 - Access denied:' . print_r(json_decode($result->body, true), true));
134
+				$result = json_decode($result->body, true);
135
+				break;
136
+			case 502:
137
+				error_log('Error 502 - ' . print_r($result, true));
138
+				$result = json_decode($result->body, true);
139
+				header('location:'.$_SERVER['PHP_SELF']);
140
+				break;
141
+			case 503:
142
+				error_log('Error 503 - ' . print_r($result, true));
143
+				$result = json_decode($result->body, true);
144
+
145
+				if(array_key_exists('error', $result) && $result['error'] == 'Service Unavailable')
146
+				{
147
+					header('location:'.$_SERVER['PHP_SELF']); 
148
+				}
149
+				break;
150
+			default:
151
+				error_log('Error '.$result->status_code.' - unknown error');
152
+				$result = json_decode($result->body, true);
153
+		}
154
+
155
+		//important for account refresh
156
+		if($device_uid != '')
157
+		{
158 158
 			$result[0] = $result;
159 159
 			$result[1] = $device_uid;
160
-        }
160
+		}
161 161
 
162 162
         
163
-        /*
163
+		/*
164 164
         var_dump($result);
165 165
         */
166 166
         
167
-        return $result;
168
-    }
169
-    abstract function getPayload();
170
-    /**
171
-     * Gets Sign headers
172
-     * @return array headers
173
-     */
174
-    private function getSignHeaders()
175
-    {
167
+		return $result;
168
+	}
169
+	abstract function getPayload();
170
+	/**
171
+	 * Gets Sign headers
172
+	 * @return array headers
173
+	 */
174
+	private function getSignHeaders()
175
+	{
176 176
 			if($this->getAccessToken() == null) {
177 177
 				$payload_accessToken = "";
178 178
 			}
@@ -181,54 +181,54 @@  discard block
 block discarded – undo
181 181
 			}
182 182
 			
183 183
 			
184
-        $headers = array(
185
-            "Connection" => "keep-alive",
186
-            "Accept-Encoding" => "gzip",
187
-            "Content-Type" => "application/json; charset=UTF-8",
188
-            "User-Agent" => self::USERAGENT
189
-        );
190
-        $timestamp = new DateTime();
191
-        $timestamp = $timestamp->format(DateTime::ATOM);
192
-        $timestamp = substr($timestamp, 0, -6);
193
-        $timestamp .= "Z";
194
-        $urlParts = parse_url($this->getFullUrl());
195
-        $url2 = "";
196
-        $req = [$this->getMethod(),
197
-            $urlParts['host'],
198
-            "443",
199
-            $urlParts['path'],
200
-            $payload_accessToken,
201
-            $timestamp,
202
-            $url2,
203
-            $this->payLoad];
204
-        $reqString = implode("%", $req);
205
-        $secret = self::SECRET;
206
-        $signature = hash_hmac('sha1', $reqString, $secret);
207
-        $signature = strtoupper($signature);
208
-        $headers['X-Authorization'] = 'HMAC ' . $signature;
209
-        $headers['X-Client-Type'] = self::CLIENT_TYPE;
210
-        $headers['X-Timestamp'] = $timestamp;
211
-        $headers['X-Api-Version'] = '0.2';
212
-        return $headers;
213
-    }
214
-    private function getFullUrl()
215
-    {
216
-        return self::APIURL . $this->getApiEndPoint();
217
-    }
218
-    abstract function getApiEndPoint();
219
-    abstract function getMethod();
220
-    /**
221
-     * @return string
222
-     */
223
-    private function getAccessToken()
224
-    {
225
-        return $this->accessToken;
226
-    }
227
-    /**
228
-     * @param string $accessToken
229
-     */
230
-    public function setAccessToken($accessToken)
231
-    {
232
-        $this->accessToken = $accessToken;
233
-    }
184
+		$headers = array(
185
+			"Connection" => "keep-alive",
186
+			"Accept-Encoding" => "gzip",
187
+			"Content-Type" => "application/json; charset=UTF-8",
188
+			"User-Agent" => self::USERAGENT
189
+		);
190
+		$timestamp = new DateTime();
191
+		$timestamp = $timestamp->format(DateTime::ATOM);
192
+		$timestamp = substr($timestamp, 0, -6);
193
+		$timestamp .= "Z";
194
+		$urlParts = parse_url($this->getFullUrl());
195
+		$url2 = "";
196
+		$req = [$this->getMethod(),
197
+			$urlParts['host'],
198
+			"443",
199
+			$urlParts['path'],
200
+			$payload_accessToken,
201
+			$timestamp,
202
+			$url2,
203
+			$this->payLoad];
204
+		$reqString = implode("%", $req);
205
+		$secret = self::SECRET;
206
+		$signature = hash_hmac('sha1', $reqString, $secret);
207
+		$signature = strtoupper($signature);
208
+		$headers['X-Authorization'] = 'HMAC ' . $signature;
209
+		$headers['X-Client-Type'] = self::CLIENT_TYPE;
210
+		$headers['X-Timestamp'] = $timestamp;
211
+		$headers['X-Api-Version'] = '0.2';
212
+		return $headers;
213
+	}
214
+	private function getFullUrl()
215
+	{
216
+		return self::APIURL . $this->getApiEndPoint();
217
+	}
218
+	abstract function getApiEndPoint();
219
+	abstract function getMethod();
220
+	/**
221
+	 * @return string
222
+	 */
223
+	private function getAccessToken()
224
+	{
225
+		return $this->accessToken;
226
+	}
227
+	/**
228
+	 * @param string $accessToken
229
+	 */
230
+	public function setAccessToken($accessToken)
231
+	{
232
+		$this->accessToken = $accessToken;
233
+	}
234 234
 }
Please login to merge, or discard this patch.