summaryrefslogtreecommitdiff
path: root/src/NeonXP/DozorApi.php
blob: 4865e96ebda86b91220ef870287c6523ee23f669 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
 * @author Alexander "NeonXP" Kiryukhin
 */

namespace NeonXP;

use Doctrine\Common\Cache\CacheProvider;
use NeonXP\TelegramApi\Types\Chat;
use Symfony\Component\DomCrawler\Crawler;

/**
 * Class DozorApi.
 */
class DozorApi
{
    /**
     * @var CacheProvider
     */
    private $cache;

    /**
     * DozorApi constructor.
     *
     * @param CacheProvider $cache
     */
    public function __construct(CacheProvider $cache)
    {
        $this->cache = $cache;
    }

    public function sendCode(Chat $chat, $code)
    {
        $params = [
            'log' => 'on',
            'mes' => '',
            'legend' => 'on',
            'nostat' => '',
            'notext' => '',
            'refresh' => '0',
            'bonus' => '',
            'kladMap' => '',
            'notags' => '',
            'cod' => $code,
            'action' => 'entcod',
        ];
        $data = http_build_query($params);
        $contextData = [
            'http' => [
                'method' => 'POST',
                'header' => 'Content-type: application/x-www-form-urlencoded',
                'content' => $data,
            ],
        ];
        $context = stream_context_create($contextData);
        if (!$this->getLink($chat)) {
            throw new \Exception('Не задана ссылка');
        }
        $result = file_get_contents($this->getLink($chat), false, $context);
        $contextData['result'] = $result;

        file_put_contents(getenv('OPENSHIFT_DATA_DIR').'log.txt', serialize($contextData));

        return $this->parseResult($result);
    }

    private function parseResult($html)
    {
        $crawler = new Crawler($html);

        $answer = [
            $this->parseCodeEnter($html),
            $this->parseTask($crawler),
        ];

        $answer = array_merge($answer,  $this->parseCodes($crawler));
        $answer = array_filter($answer);

        return implode(PHP_EOL, $answer);
    }

    private function parseCodeEnter($html)
    {
        $html = iconv('cp1251', 'utf8', $html);
        $regex = '/\<\!\-\-errorText\-\-\>\<p\>\<strong\>(.+?)\<\/strong\>\<\/p\>\<\!\-\-errorTextEnd\-\-\>/i';
        $result = [];
        preg_match($regex, $html, $result);

        return $result[1];
    }

    private function parseTask(Crawler $crawler)
    {
        return $crawler->filter('body > p')->reduce(function ($node) {
            return strpos($node->text(), 'Задание ');
        })->text();
    }

    private function parseCodes(Crawler $crawler)
    {
        $codesHtml = $crawler->filter('body > .dcodes')->html();
        $codesHtml = preg_replace('/\<span\ style\=\"color\:red\"\>(.+?)\<\/span\>/i', '*[$1]*', $codesHtml);
        $codesHtml = preg_replace('/\<br(\s*)?\/?\>/i', PHP_EOL, $codesHtml);
        $codesHtml = strip_tags($codesHtml);
        $codesHtml = explode(PHP_EOL, $codesHtml);
        array_shift($codesHtml);

        return $codesHtml;
    }

    private function getLink(Chat $chat)
    {
        return $this->cache->fetch("link_{$chat->getId()}");
    }
}