Completed
Push — master ( dc3cec...6a6184 )
by mains
03:04
created

php/Requests/AbstractRequest.php (3 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
{	
5
    CONST CLIENTID = '81e8a76e-1e02-4d17-9ba0-8a7020261b26';
6
    CONST APIURL = 'https://api.go-tellm.com/api';
7
    const SECRET = "dIHNtHWOxFmoFouufSflpTKYjPmCIhWUCQHgbNzR";
8
    const USERAGENT = "Jodel/4.29.1 Dalvik/2.1.0 (Linux; U; Android 5.1.1; )";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Jodel/4.29.1 Dalvik/2.1....ux; U; Android 5.1.1; ) does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
9
    const CLIENT_TYPE = 'android_4.29.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
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
36
        var_dump($url);
37
        var_dump($header);
38
        var_dump($this->payLoad);
39
        */
40
        
41
42
        switch ($this->getMethod()) {
43
            case 'POST':
44
                $result = Requests::post($url, $header, $this->payLoad);
45
                break;
46
            case 'GET':
47
                if($this->version == 'v3')
48
                {
49
                    $result = Requests::get($url, $header);
50
                }
51
                else
52
                {
53
                    $result = Requests::get($url, $header);
54
                }
55
                break;
56
            case 'PUT':
57
                $result = Requests::put($url, $header, $this->payLoad);
58
                break;
59
        }
60
        switch ($result->status_code) {
61
            case 200:
62
                $result = json_decode($result->body, true);
63
                break;
64
            case 204:
65
                $result = "Success";
66
                break;
67
            case 401:
68
				throw new \Exception('Unauthorized');
69
                break;
70
            case 404:
71
                //echo "Es wurde bereits gevoted";
72
			case 477:
73
                //echo "Es wurde bereits gevoted";
74
                //throw  new \Exception('Signing failed!');
75
                break;
76
            default:
77
                throw  new \Exception('Unknown Error: '.$result->status_code);
78
        }
79
80
        if($device_uid != "")
81
        {
82
			$result[0] = $result;
83
			$result[1] = $device_uid;
84
		}
85
86
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
87
        var_dump($result);
88
        */
89
90
        return $result;
91
    }
92
    abstract function getPayload();
93
    /**
94
     * Gets Sign headers
95
     * @return array headers
96
     */
97
    private function getSignHeaders()
98
    {
99
			if($this->getAccessToken() == null) {
100
				$payload_accessToken = "";
101
			}
102
			else {
103
				$payload_accessToken = $this->getAccessToken();
104
			}
105
			
106
			
107
        $headers = array(
108
            "Connection" => "keep-alive",
109
            "Accept-Encoding" => "gzip",
110
            "Content-Type" => "application/json; charset=UTF-8",
111
            "User-Agent" => self::USERAGENT
112
        );
113
        $timestamp = new DateTime();
114
        $timestamp = $timestamp->format(DateTime::ATOM);
115
        $timestamp = substr($timestamp, 0, -6);
116
        $timestamp .= "Z";
117
        $urlParts = parse_url($this->getFullUrl());
118
        $url2 = "";
119
        $req = [$this->getMethod(),
120
            $urlParts['host'],
121
            "443",
122
            $urlParts['path'],
123
            $payload_accessToken,
124
            $timestamp,
125
            $url2,
126
            $this->payLoad];
127
        $reqString = implode("%", $req);
128
        $secret = self::SECRET;
129
        $signature = hash_hmac('sha1', $reqString, $secret);
130
        $signature = strtoupper($signature);
131
        $headers['X-Authorization'] = 'HMAC ' . $signature;
132
        $headers['X-Client-Type'] = self::CLIENT_TYPE;
133
        $headers['X-Timestamp'] = $timestamp;
134
        $headers['X-Api-Version'] = '0.2';
135
        return $headers;
136
    }
137
    private function getFullUrl()
138
    {
139
        return self::APIURL . $this->getApiEndPoint();
140
    }
141
    abstract function getApiEndPoint();
142
    abstract function getMethod();
143
    /**
144
     * @return string
145
     */
146
    private function getAccessToken()
147
    {
148
        return $this->accessToken;
149
    }
150
    /**
151
     * @param string $accessToken
152
     */
153
    public function setAccessToken($accessToken)
154
    {
155
        $this->accessToken = $accessToken;
156
    }
157
}
158