Completed
Push — master ( e39583...d1854b )
by mains
03:08
created

php/Requests/AbstractRequest.php (1 issue)

Severity

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
{	
5
    const CLIENTID = '81e8a76e-1e02-4d17-9ba0-8a7020261b26';
6
    const APIURL = 'https://api.go-tellm.com/api';
7
    const SECRET = 'plerFToqEdWlzShdZlTywaCHRuzlKIMsNmOJVDGE';
8
    const USERAGENT = 'Jodel/4.31.1 Dalvik/2.1.0 (Linux; U; Android 5.1.1; )';
9
    const CLIENT_TYPE = 'android_4.31.1';
10
    
11
    private $accessToken = null;
12
    private $payLoad;
13
    public $expects = "";
14
    public $version = 'v2';
15
16
    public function execute()
17
    {
18
		$result = new \stdClass();
19
		        
20
		$this->payLoad = $this->getPayload();
21
		$device_uid = "";
22
		if(isset($this->payLoad["device_uid"])) {
23
			$device_uid = $this->payLoad["device_uid"];
24
		}
25
				
26
				
27
        $this->payLoad = json_encode($this->payLoad);
28
        $header = $this->getSignHeaders();
29
        $url = $this->getFullUrl();
30
31
        if ($this->getAccessToken()) {
32
            $header['Authorization'] = "Bearer " . $this->getAccessToken();
33
        }
34
        //Comment out to debug the Request:
35
        
36
        /*
37
        var_dump($url);
38
        var_dump($header);
39
        var_dump($this->payLoad);
40
        */
41
42
        $options = array(
43
            'timeout' => 100,
44
            'connect_timeout' => 100,
45
            'proxy' => '186.103.169.165:8080',
46
        );
47
48
        switch ($this->getMethod()) {
49
            case 'POST':
50
                $result = Requests::post($url, $header, $this->payLoad, $options);
51
                break;
52
            case 'GET':
53
                if($this->version == 'v3')
54
                {
55
                    $result = Requests::get($url, $header);
56
                }
57
                else
58
                {
59
                    $result = Requests::get($url, $header);
60
                }
61
                break;
62
            case 'PUT':
63
                $result = Requests::put($url, $header, $this->payLoad);
64
                break;
65
        }
66
        switch ($result->status_code) {
67
            case 200:
68
                $result = json_decode($result->body, true);
69
                break;
70
            case 204:
71
                $result = "Success";
72
                break;
73
            case 401:
74
				//throw new \Exception('Unauthorized');
75
                break;
76
            case 404:
77
                //echo "Es wurde bereits gevoted";
78
			case 477:
79
                //echo "Es wurde bereits gevoted";
80
                //throw  new \Exception('Signing failed!');
81
                break;
82
            case 429:
83
            	exit("Error 429: Too Many Requests");
84
            	break;
0 ignored issues
show
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
85
            case 403:
86
                exit("Error 403: Access denied");
87
                break;
88
            default:
89
                throw  new \Exception('Unknown Error: '.$result->status_code);
90
        }
91
92
        if($device_uid != "")
93
        {
94
			$result[0] = $result;
95
			$result[1] = $device_uid;
96
		}
97
98
        
99
        /*var_dump($result);*/
100
        
101
102
        return $result;
103
    }
104
    abstract function getPayload();
105
    /**
106
     * Gets Sign headers
107
     * @return array headers
108
     */
109
    private function getSignHeaders()
110
    {
111
			if($this->getAccessToken() == null) {
112
				$payload_accessToken = "";
113
			}
114
			else {
115
				$payload_accessToken = $this->getAccessToken();
116
			}
117
			
118
			
119
        $headers = array(
120
            "Connection" => "keep-alive",
121
            "Accept-Encoding" => "gzip",
122
            "Content-Type" => "application/json; charset=UTF-8",
123
            "User-Agent" => self::USERAGENT
124
        );
125
        $timestamp = new DateTime();
126
        $timestamp = $timestamp->format(DateTime::ATOM);
127
        $timestamp = substr($timestamp, 0, -6);
128
        $timestamp .= "Z";
129
        $urlParts = parse_url($this->getFullUrl());
130
        $url2 = "";
131
        $req = [$this->getMethod(),
132
            $urlParts['host'],
133
            "443",
134
            $urlParts['path'],
135
            $payload_accessToken,
136
            $timestamp,
137
            $url2,
138
            $this->payLoad];
139
        $reqString = implode("%", $req);
140
        $secret = self::SECRET;
141
        $signature = hash_hmac('sha1', $reqString, $secret);
142
        $signature = strtoupper($signature);
143
        $headers['X-Authorization'] = 'HMAC ' . $signature;
144
        $headers['X-Client-Type'] = self::CLIENT_TYPE;
145
        $headers['X-Timestamp'] = $timestamp;
146
        $headers['X-Api-Version'] = '0.2';
147
        return $headers;
148
    }
149
    private function getFullUrl()
150
    {
151
        return self::APIURL . $this->getApiEndPoint();
152
    }
153
    abstract function getApiEndPoint();
154
    abstract function getMethod();
155
    /**
156
     * @return string
157
     */
158
    private function getAccessToken()
159
    {
160
        return $this->accessToken;
161
    }
162
    /**
163
     * @param string $accessToken
164
     */
165
    public function setAccessToken($accessToken)
166
    {
167
        $this->accessToken = $accessToken;
168
    }
169
}
170