| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | include('GPC.php');
|
|---|
| 4 |
|
|---|
| 5 | class Fio
|
|---|
| 6 | {
|
|---|
| 7 | public string $UserName;
|
|---|
| 8 | public string $Password;
|
|---|
| 9 | public int $Account;
|
|---|
| 10 |
|
|---|
| 11 | function Import(int $TimeFrom, int $TimeTo): array
|
|---|
| 12 | {
|
|---|
| 13 | if ($this->UserName == '') throw new Exception('Missing value for UserName property.');
|
|---|
| 14 | if ($this->Password == '') throw new Exception('Missing value for Password property.');
|
|---|
| 15 | if (!is_numeric($this->Account)) throw new Exception('Missing or not numeric value for Account property.');
|
|---|
| 16 |
|
|---|
| 17 | $fp = fsockopen('ssl://www.fio.cz', 443, $errno, $errstr, 30);
|
|---|
| 18 | if (!$fp)
|
|---|
| 19 | {
|
|---|
| 20 | throw new Exception('Connection error: '.$errstr);
|
|---|
| 21 | } else
|
|---|
| 22 | {
|
|---|
| 23 | // Send request
|
|---|
| 24 | $RequestURL = "/scgi-bin/hermes/dz-pohyby.cgi?ID_ucet=".$this->Account.
|
|---|
| 25 | "&LOGIN_USERNAME=".$this->UserName."&SUBMIT=Odeslat&LOGIN_TIME=".time().
|
|---|
| 26 | "&LOGIN_PASSWORD=".$this->Password."&pohyby_DAT_od=".date('d.m.Y', $TimeFrom).
|
|---|
| 27 | "&pohyby_DAT_do=".date('d.m.Y', $TimeTo)."&export_gpc=1";
|
|---|
| 28 | $Request = "GET ".$RequestURL." HTTP/1.1\r\n";
|
|---|
| 29 | $Request .= "Host: www.fio.cz\r\n";
|
|---|
| 30 | $Request .= "User-Agent: PHP Script\r\n";
|
|---|
| 31 | $Request .= "Content-Type: text/html\r\n";
|
|---|
| 32 | $Request .= "Connection: Close\r\n\r\n";
|
|---|
| 33 | fwrite($fp, $Request);
|
|---|
| 34 |
|
|---|
| 35 | // Read response
|
|---|
| 36 | $Response = array();
|
|---|
| 37 | while (!feof($fp))
|
|---|
| 38 | {
|
|---|
| 39 | $Response[] = trim(fgets($fp, 1024));
|
|---|
| 40 | }
|
|---|
| 41 | fclose($fp);
|
|---|
| 42 |
|
|---|
| 43 | // Strip HTTP header
|
|---|
| 44 | while ($Response[0] != '') array_shift($Response);
|
|---|
| 45 | array_shift($Response); // Remove empty line
|
|---|
| 46 |
|
|---|
| 47 | // Parse all GPC lines
|
|---|
| 48 | $GPC = new GPC();
|
|---|
| 49 | $Result = array();
|
|---|
| 50 | foreach ($Response as $Index => $Line)
|
|---|
| 51 | {
|
|---|
| 52 | if (($Index == 0) and (substr($Line, 0, strlen(GPC_TYPE_REPORT)) != GPC_TYPE_REPORT)) $this->NoValidDataError($Response);
|
|---|
| 53 | $GPCLine = $GPC->ParseLine($Line);
|
|---|
| 54 | if ($GPCLine != NULL) $Result[] = $GPCLine;
|
|---|
| 55 | }
|
|---|
| 56 | return $Result;
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | function NoValidDataError(array $Response): void
|
|---|
| 61 | {
|
|---|
| 62 | // Try to get error message
|
|---|
| 63 | // If something go wrong fio shows HTML login page and display error message
|
|---|
| 64 | $Response = implode('', $Response);
|
|---|
| 65 | $ErrorMessageStart = '<div id="oldform_warning">';
|
|---|
| 66 | if (strpos($Response, $ErrorMessageStart) !== false)
|
|---|
| 67 | {
|
|---|
| 68 | $Response = substr($Response, strpos($Response, $ErrorMessageStart) + strlen($ErrorMessageStart));
|
|---|
| 69 | $ErrorMessage = trim(substr($Response, 0, strpos($Response, '</div>')));
|
|---|
| 70 | } else $ErrorMessage = '';
|
|---|
| 71 | throw new Exception('No valid GPC data: '.$ErrorMessage);
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|