GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 83375e...480fbc )
by Krzysztof
02:21
created

src/Orange.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
namespace zembrowski\SMS;
4
5
/**
6
 * Class Orange
7
 * @package zembrowski\phpsms-poland
8
 */
9
class Orange
10
{
11
    public $url = 'https://www.orange.pl'; // orange.pl URL
12
    private $user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4';
13
    private $login_request_uri = '/zaloguj.phtml'; // login form request uri
14
    private $login_post_query_string = '?_DARGS=/ocp/gear/infoportal/portlets/login/login-box.jsp'; // login form POST query string
15
    private $send_request_uri = '/portal/map/map/message_box?mbox_edit=new&mbox_view=newsms'; // request uri of form for sending new messages
16
    private $send_post_request_uri = '/portal/map/map/message_box?_DARGS=/gear/mapmessagebox/smsform.jsp'; // action target for POST request of the sending new messages form
17
    private $messages_request_uri = '/portal/map/map/message_box?mbox_view=sentmessageslist'; // request uri of the sent messages list
0 ignored issues
show
The property $messages_request_uri is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
    public $max_length = '640'; // max. length of one SMS message according to the sending new messages form
19
20
    /**
21
     * @var session
22
     * @var html
23
     * @var dynSess
24
     * @var token
25
     */
26
    private $session;
27
    private $html;
28
    private $dynSess;
29
    private $token;
30
31
    /**
32
     * Instantiates the Requests handler with Session support.
33
     */
34
    public function __construct()
35
    {
36
        $session = new \Requests_Session($this->url);
37
        $session->useragent = $this->user_agent;
38
        $this->session = $session;
39
        $response = $this->session->get($this->login_request_uri);
0 ignored issues
show
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
41
        $html = new \simple_html_dom();
42
        $this->html = $html;
43
44
        $random = rand(1000000000, 2147483647);
45
        $this->dynSess = $random . $random;
46
    }
47
48
    /**
49
     * Login at orange.pl
50
     *
51
     * You have to be register at orange.pl
52
     * Head to: https://www.orange.pl/rejestracja.phtml
53
     *
54
     * @param string $login - login or the number assosciated with the service you use
55
     * @param string $password - password (pol. "Hasło")
56
     */
57
    public function login($login, $password)
58
    {
59
        // Referer header set only to act more like a browser
60
        $this->session->headers['Referer'] = $this->url . $this->login_request_uri;
61
62
        $this->session->data = array(
63
            '_dyncharset' => 'UTF-8',
64
            '_dynSessConf' => $this->dynSess,
65
            '/tp/core/profile/login/ProfileLoginFormHandler.loginErrorURL' => $this->send_request_uri,
66
            '_D:/tp/core/profile/login/ProfileLoginFormHandler.loginErrorURL' => '',
67
            '/tp/core/profile/login/ProfileLoginFormHandler.loginSuccessURL' => $this->send_request_uri,
68
            '_D:/tp/core/profile/login/ProfileLoginFormHandler.loginSuccessURL' => '',
69
            '/tp/core/profile/login/ProfileLoginFormHandler.firstEnter' => 'true',
70
            '_D:/tp/core/profile/login/ProfileLoginFormHandler.firstEnter' => '',
71
            '/tp/core/profile/login/ProfileLoginFormHandler.value.login' => $login,
72
            '_D:/tp/core/profile/login/ProfileLoginFormHandler.value.login' => '',
73
            '/tp/core/profile/login/ProfileLoginFormHandler.value.password' => $password,
74
            '_D:/tp/core/profile/login/ProfileLoginFormHandler.value.password' => '',
75
            '/tp/core/profile/login/ProfileLoginFormHandler.rememberMe' => 'false',
76
            '_D:/tp/core/profile/login/ProfileLoginFormHandler.rememberMe' => '',
77
            '/tp/core/profile/login/ProfileLoginFormHandler.login' => 'Zaloguj się',
78
            '_D:/tp/core/profile/login/ProfileLoginFormHandler.login' => '',
79
            '_DARGS' => '/ocp/gear/infoportal/portlets/login/login-box.jsp'
80
        );
81
82
        $response = $this->session->post($this->login_request_uri . $this->login_post_query_string);
83
84
        $this->token = $this->token($response->body);
85
86
        $result = array('check' => $this->check($response->body, 'div.box-error p'), 'free' => $this->free($response->body));
87
88
        return $result;
89
    }
90
91
    /**
92
     * Retrieves the token from the webform
93
     *
94
     * @param string $content - content to be searched through
95
     * @return string - token
96
     */
97
    private function token($content)
98
    {
99
        $element = $this->find($content, 'div#box-smsform form input[name=/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.token]', 0);
100
101
        return $element->value;
102
    }
103
104
    /**
105
     * Send a SMS through the webform at $this->send_post_request_uri
106
     *
107
     * @param string $recipient - number of the recipient (9 digits without leading zero for national numbers e.g. 501234567; two leading zeros followed by prefix for international numbers e.g. 004912345678901; no spaces or special chars)
108
     * @param string $text - content of the SMS
109
     */
110
     // TODO: check number of recipient for validaty
111
    public function send($recipient, $text)
112
    {
113
        if (strlen($text) <= 0 || strlen($text) > $this->max_length) {
114
            throw new Exception('The message must be longer than 0 characters, but shorter than ' . $this->max_length . ' characters');
115
        }
116
117
        $this->session->options['timeout'] = 30;
118
119
        // Referer header set only to act more like a browser
120
        $this->session->headers['Referer'] = $this->url . $this->send_request_uri;
121
122
        $this->session->data = array(
123
            '_dyncharset' => 'UTF-8',
124
            '_dynSessConf' => $this->dynSess,
125
            '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.type' => 'sms',
126
            '_D:/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.type' => '',
127
            '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.errorURL' => '',
128
            '_D:/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.errorURL' => '',
129
            '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.successURL' => '',
130
            '_D:/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.successURL' =>'',
131
            '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.to' => $recipient,
132
            '_D:/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.to' => '',
133
            '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.body' => $text,
134
            '_D:/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.body' => '',
135
            '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.token' => $this->token,
136
            '_D:/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.token' => '',
137
            'enabled' => true,
138
            '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.create.x' => rand(0, 50),
139
            '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.create.y' => rand(0, 25),
140
            '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.create' => 'Wyślij',
141
            '_D:/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.create' => '',
142
            '_DARGS' => '/gear/mapmessagebox/smsform.jsp'
143
        );
144
145
        $response = $this->session->post($this->send_post_request_uri);
146
147
        $result = array('status_code' => $response->status_code, 'check' => $this->check($response->body, 'div.box-error p'), 'free' => $this->free($response->body));
148
149
        return $result;
150
    }
151
152
    /**
153
     * Find element in HTML Dom
154
     *
155
     * @param string $content - content to be searched through
156
     * @return object
157
     */
158
    private function find($content, $selector, $nth = null)
159
    {
160
        $this->html->load($content);
161
        if (is_int($nth) || $nth === 0) {
162
            $result = $this->html->find($selector, $nth);
163
        } else {
164
            $result = $this->html->find($selector);
165
        }
166
167
        return $result;
168
    }
169
170
    /**
171
     * Checks the remaining SMS left this month from the response body
172
     *
173
     * @param string $content - content to be searched through
174
     * @return boolean/int/string - free SMS this month; false if no content; int if int value present; other cases string
0 ignored issues
show
The doc-type boolean/int/string could not be parsed: Unknown type name "boolean/int/string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
175
     */
176
    private function free($content)
177
    {
178
        if ($content) {
179
            $element = $this->find($content, '#syndication p.item span.value', 0);
180
            $value = $element->plaintext;
181
            if (!empty($element->plaintext)) {
182
                $value = trim($value);
183
                $value_int = intval($value);
184
                if (is_int($value_int)) $result = $value_int;
185
                else $result = $value;
186
            } else {
187
                $result = false;
188
            }
189
        } else {
190
            $result = false;
191
        }
192
193
        return $result;
194
    }
195
196
    /**
197
     * Checks the remaining SMS left this month making a GET request
198
     *
199
     * @return int - free SMS this month
200
     */
201
    public function getFree()
202
    {
203
        $response = $this->session->get($this->send_request_uri);
204
        $element = $this->find($response->body, '#syndication p.item span.value', 0);
205
        $result = intval(trim($element->plaintext));
206
207
        return $result;
208
    }
209
210
    /**
211
     * Check whether errors have been returned
212
     *
213
     * @param string $content - response body of a request
214
     * @return boolean - false if an element described by the selector exists
215
     */
216
    private function check($content, $selector)
217
    {
218
        $elements = $this->find($content, $selector);
219
        if (count($elements) > 0) {
220
            foreach ($elements as $element) {
221
                throw new Exception(trim($element->plaintext));
222
            }
223
            $result = false;
224
        } else {
225
            $result = true;
226
        }
227
228
        return $result;
229
    }
230
}
231