Completed
Push — master ( 62cb27...c16962 )
by mains
03:25
created

php/Requests/AbstractRequest.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
abstract class AbstractRequest
4
{	
0 ignored issues
show
The opening class brace should be on a newline by itself.
Loading history...
5
    const CLIENTID = '81e8a76e-1e02-4d17-9ba0-8a7020261b26';
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
6
    const APIURL = 'https://api.go-tellm.com/api';
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
7
    const SECRET = 'hFvMqLauMtnodakokftuKETbIsVLxpqfjAXiRoih';
8
    const USERAGENT = 'Jodel/4.41.0 Dalvik/2.1.0 (Linux; U; Android 5.1.1; )';
9
    const CLIENT_TYPE = 'android_4.41.0';
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();
20
		        
21
		$this->payLoad = $this->getPayload();
22
		$device_uid = '';
23
		if(isset($this->payLoad['device_uid'])) {
24
			$device_uid = $this->payLoad['device_uid'];
25
		}
26
				
27
				
28
        $this->payLoad = json_encode($this->payLoad);
29
        $header = $this->getSignHeaders();
30
        $url = $this->getFullUrl();
31
32
        if ($this->getAccessToken()) {
33
            $header['Authorization'] = "Bearer " . $this->getAccessToken();
34
        }
35
        //Comment out to debug the Request:
36
37
        /*
38
        printf("URL: ");
39
        var_dump($url);
40
        echo "<br />";
41
        printf("Header: ");
42
        var_dump($header);
43
        echo "<br />";
44
        printf("Payload: ");
45
        var_dump($this->payLoad);
46
        echo "<br />";
47
        */
48
        /*
49
        $options = array(
50
            'timeout' => 100,
51
            'connect_timeout' => 100,
52
            'proxy' => '186.103.169.165:8080',
53
        );*/
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 View Code Duplication
            case 400:
85
                $result = json_decode($result->body, true);
86
                error_log('Error 400 - ' . print_r($result, true));
87
                break;
88
            case 401:
89
                //$result = json_decode($result->body, true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
91
                if(is_array($result) && $result['error'] == 'length')
0 ignored issues
show
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
92
                {
93
94
                }
95
                else
96
                {
97
                    error_log('Error 401 - ' . print_r($result, true));
98
                }
99
                break;
100 View Code Duplication
            case 404:
101
                error_log('Error 404 - ' . print_r($result, true));
102
                $result = json_decode($result->body, true);
103
                break;
104 View Code Duplication
			case 477:
105
                $result = json_decode($result->body, true);
106
                error_log('Error 477 - ' . print_r($result, true));
107
                break;
108 View Code Duplication
            case 429:
109
                error_log('Error 429 - Too Many Requests' . print_r(json_decode($result->body, true), true));
110
            	exit("Error 429: Too Many Requests");
111
            	break;
112
            case 403:
113
                error_log('Error 403 - Access denied:' . print_r(json_decode($result->body, true), true));
114
                $result = json_decode($result->body, true);
115
                break;
116
            case 502:
117
                error_log('Error 502 - ' . print_r($result, true));
118
                $result = json_decode($result->body, true);
119
                header('location:'.$_SERVER['PHP_SELF']);
120
                break;
121
            case 503:
122
                error_log('Error 503 - ' . print_r($result, true));
123
                $result = json_decode($result->body, true);
124
125
                if(array_key_exists('error', $result) && $result['error'] == 'Service Unavailable')
126
                {
127
                    header('location:'.$_SERVER['PHP_SELF']); 
128
                }
129
                break;
130
            default:
131
                error_log('Error '.$result->status_code.' - unknown error');
132
                $result = json_decode($result->body, true);
133
        }
134
135
        //important for account refresh
136
        if($device_uid != '')
137
        {
138
			$result[0] = $result;
139
			$result[1] = $device_uid;
140
        }
141
142
        
143
        /*
144
        var_dump($result);
145
        */
146
        
147
        return $result;
148
    }
149
    abstract function getPayload();
150
    /**
151
     * Gets Sign headers
152
     * @return array headers
153
     */
154
    private function getSignHeaders()
155
    {
156
			if($this->getAccessToken() == null) {
157
				$payload_accessToken = "";
158
			}
159
			else {
160
				$payload_accessToken = $this->getAccessToken();
161
			}
162
			
163
			
164
        $headers = array(
165
            "Connection" => "keep-alive",
166
            "Accept-Encoding" => "gzip",
167
            "Content-Type" => "application/json; charset=UTF-8",
168
            "User-Agent" => self::USERAGENT
169
        );
170
        $timestamp = new DateTime();
171
        $timestamp = $timestamp->format(DateTime::ATOM);
172
        $timestamp = substr($timestamp, 0, -6);
173
        $timestamp .= "Z";
174
        $urlParts = parse_url($this->getFullUrl());
175
        $url2 = "";
176
        $req = [$this->getMethod(),
177
            $urlParts['host'],
178
            "443",
179
            $urlParts['path'],
180
            $payload_accessToken,
181
            $timestamp,
182
            $url2,
183
            $this->payLoad];
184
        $reqString = implode("%", $req);
185
        $secret = self::SECRET;
186
        $signature = hash_hmac('sha1', $reqString, $secret);
187
        $signature = strtoupper($signature);
188
        $headers['X-Authorization'] = 'HMAC ' . $signature;
189
        $headers['X-Client-Type'] = self::CLIENT_TYPE;
190
        $headers['X-Timestamp'] = $timestamp;
191
        $headers['X-Api-Version'] = '0.2';
192
        return $headers;
193
    }
194
    private function getFullUrl()
195
    {
196
        return self::APIURL . $this->getApiEndPoint();
197
    }
198
    abstract function getApiEndPoint();
199
    abstract function getMethod();
200
    /**
201
     * @return string
202
     */
203
    private function getAccessToken()
204
    {
205
        return $this->accessToken;
206
    }
207
    /**
208
     * @param string $accessToken
209
     */
210
    public function setAccessToken($accessToken)
211
    {
212
        $this->accessToken = $accessToken;
213
    }
214
}
0 ignored issues
show
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
215